tensorprod
Tensor product and contraction of arrays along chosen dimensions.
Category: Language Fundamentals > Operators and Elementary Operations
Syntax
C = tensorprod(A, B)
Outer (tensor) product: every element of A times every element of B. The result has ndims(A) + ndims(B) dimensions.
C = tensorprod(A, B, dimA, dimB)
Contracts: multiplies and sums over dimension dimA of A and dimB of B.
C = tensorprod(A, B, 'all')
Contracts over all dimensions, which gives the full inner product as a scalar.
Description
Without contraction dimensions, tensorprod builds the outer product, so two vectors of lengths 3 and 3 give a result of size 1x3x1x3. Contracting one pair of dimensions generalizes matrix multiplication: tensorprod(A, B, 2, 1) equals A*B for matrices. Contracting [1 2] with [1 2] sums the products of all matching elements.
It is the general tool behind dot products, matrix products and Einstein-summation style contractions.
Input arguments
A, B- Numeric arrays.
dimA, dimB- Dimensions to contract. They must have equal sizes in
AandBand the same number of entries.
Output arguments
C- The result. The remaining dimensions of
Acome first, followed by those ofB.
Example
a = [1 2 3];
b = [4 5 6];
size(tensorprod(a, b))
tensorprod(a, b, 'all')
A = [1 2; 3 4];
tensorprod(A, A, 2, 1)
ans = 1×4
1 3 1 3
ans = 32
ans = 2×2
7 10
15 22