Next: , Previous: , Up: Matrices and Tensors   [Contents][Index]

### 4.6 Tensor contraction

Command: contract matrix index1 …

The contraction operation produces a tensor of rank two less than a given tensor. It does this by performing a summation over two of the indices of the given tensor, as clarified in the examples below.

`contract` takes at least one argument which is the tensor on which the contraction operation is to be performed. One or two additional arguments may be provided to specify the indices to be used in the summation. If no additional arguments are provided, the summation is performed over the first and second indices. With one additional argument, the summation is over the specified index and the one following it (e.g., if 3 is specified, the third and fourth indices are used). With two additional arguments, the summation is performed over the indices specified. The actual indices used will be constrained to be between 1 and the rank of the tensor.

Examples:

1) For a square matrix (tensor of rank 2), `contract` returns a scalar that is the sum of the diagonal elements of the matrix.

2) Given `x[i,j,k,l]`, the command

```y:contract(x,2,4);
```

produces:

```                        length
-----
\
y[a,b] =  >   x[a,q,b,q]
/
-----
q = 1
```

Special cases: If `contract` is given a scalar (rank 0 tensor) as input, it just returns the scalar. For a vector (tensor of rank 1), `contract` returns a scalar that is the sum of the elements of the vector.