Function Repository Resource:

HyperTr

Source Notebook

Compute the hypertrace of a given hypermatrix

Contributed by: Riccardo Gatti

ResourceFunction["HyperTr"][h]

gives the hypertrace of the cubical hypermatrix h.

Details

A hypermatrix generalizes an ordinary matrix to an arbitrary-dimensional rectangular array. A cubical hypermatrix is the corresponding generalization of a square matrix.
The trace of a matrix is defined as the sum of the elements on the main diagonal. The main diagonal of the hypermatrix H is the set of values such that where 1in. Their sum is the hypertrace, thus generalizing the ordinary matrix trace to tensors of arbitrary rank.

Examples

Basic Examples

Calculate the hypertrace of a cubical hypermatrix with d=4 and n=2:

In[1]:=
ResourceFunction["HyperTr"][
 Array[Subscript[W, #1, #2, #3, #4] &, {2, 2, 2, 2}]]
Out[1]=

Scope

Numeric values:

In[2]:=
ResourceFunction["HyperTr"][{\!\(\*
TagBox[
RowBox[{"(", "", GridBox[{
{
RowBox[{"(", "", 
TagBox[GridBox[{
{"0"},
{"3"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}], 
RowBox[{"(", "", 
TagBox[GridBox[{
{"1"},
{"4"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}]},
{
RowBox[{"(", "", 
TagBox[GridBox[{
{"7"},
{"6"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}], 
RowBox[{"(", "", 
TagBox[GridBox[{
{"1"},
{"7"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}]}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}], "", ")"}],
Function[BoxForm`e$, 
MatrixForm[BoxForm`e$]]]\), \!\(\*
TagBox[
RowBox[{"(", "", GridBox[{
{
RowBox[{"(", "", 
TagBox[GridBox[{
{"4"},
{"7"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}], 
RowBox[{"(", "", 
TagBox[GridBox[{
{"0"},
{"5"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}]},
{
RowBox[{"(", "", 
TagBox[GridBox[{
{"9"},
{"7"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}], 
RowBox[{"(", "", 
TagBox[GridBox[{
{"8"},
{"7"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.5599999999999999]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
Column], "", ")"}]}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}], "", ")"}],
Function[BoxForm`e$, 
MatrixForm[BoxForm`e$]]]\)}]
Out[2]=

Publisher

Riccardo Gatti

Version History

  • 1.0.0 – 21 November 2022

Source Metadata

Related Resources

License Information