ARRAYTRACE Function

Computes the trace of a matrix or a tensor contraction on a multidimensional array.

enabled.

Usage

    t = ARRAYTRACE( b[, j, k] )

Input Parameters

b—Numerical n-dimensional array.

j—(Required for n>2) A positive integer (≥0) designating a dimension of b. If j is supplied then k must be supplied and distinct from j.

k—(Required for n>2) A positive integer (≥0) designating a dimension of b. If j is supplied then k must be supplied and distinct from j.

Returned Value

t—An (n–2)-dimensional array which is the tensor contraction of b over dimensions j and k. For n=2 this is the trace of the matrix b, and more generally it is the sum of all the (n–2)-D subarrays defined along the main diagonal of the j-k plane. If b is of integral data type then the data type of t is single-precision floating-point; otherwise, the data type of t is the same as that of b.

Keywords

None.

Discussion

For large or multidimensional arrays, computing traces with ARRAYTRACE is generally much faster than computing them with TOTAL and its Dimension keyword. One other difference between the two methods is that the result from TOTAL retains the contracted dimensions of length 1 (which can be eliminated with REFORM), whereas ARRAYTRACE always eliminates these degenerate dimensions from the result.

Examples

b = FINDGEN(5, 4)
PM, b
; PV-WAVE prints:
;  0.00000       5.00000       10.0000       15.0000
;  1.00000       6.00000       11.0000       16.0000
;  2.00000       7.00000       12.0000       17.0000
;  3.00000       8.00000       13.0000       18.0000
;  4.00000       9.00000       14.0000       19.0000

t = ARRAYTRACE(b)
PM, t
; PV-WAVE prints:
;  36.0000

b = FINDGEN(3, 3, 3)
PM, b
; PV-WAVE prints:
;  0.00000       3.00000       6.00000
;  1.00000       4.00000       7.00000
;  2.00000       5.00000       8.00000

;  9.00000       12.0000       15.0000
;  10.0000       13.0000       16.0000
;  11.0000       14.0000       17.0000

;  18.0000       21.0000       24.0000
;  19.0000       22.0000       25.0000
;  20.0000       23.0000       26.0000

PM, ARRAYTRACE(b, 0, 1)
; PV-WAVE prints:
;  12.0000
;  39.0000
;  66.0000

PM, ARRAYTRACE(b, 0, 2)
; PV-WAVE prints:
;  30.0000
;  39.0000
;  48.0000

PM, ARRAYTRACE(b, 1, 2)
; PV-WAVE prints:
;  36.0000
;  39.0000
;  42.0000

See Also

TOTAL