mytensorprod

Tensor products between two tensors. It supports GPU computing
74 Downloads
Updated 9 Apr 2023

View License

C = mytensorprod(A,B,dimA,dimB) returns the tensor product of tensors A and B. The arguments dimA and dimB are vectors that specify which dimensions to contract in A and B. The size of the output tensor is the size of the uncontracted dimensions of A followed by the size of the uncontracted dimensions of B.
C = mytensorprod(A,B,dim) specifies the same dimensions to contract in both A and B.
C = mytensorprod(A,B) returns the outer product between tensors A and B. This syntax is equivalent to using one of the previous syntaxes with dimA = dimB = [] or dim = []. The size of the output tensor is [size(A) size(B)].
C = mytensorprod(A,B,"all") returns the inner product between tensors A and B, which must be the same size. The output is a scalar.
A and B cannot have singleton dimensions.
mytensorprod is very similar to the built-in function tensorprod introduced in R2022a.
The differences are:
mytensorprod does not support singletons;
mytensorprod supports GPU computing. That is, A and B can be gpuArray. Note that, for GPU computing, single precision is suggested as the underlyingType of A and B. mytensorprod on GPU RTX 2080 could be 30 times faster than tensorprod on CPU i7-9700K.

Cite As

yi yang (2026). mytensorprod (https://uk.mathworks.com/matlabcentral/fileexchange/116860-mytensorprod), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2022a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Version Published Release Notes
1.0.3

The built-in function "tensorprod" also supports GPU computing starting from MATLAB 2023a. If you have MATLAB 2023a or later version, please use "tensorprod" instead of "mytensorprod".

1.0.2

Fix the bug if A or B is a vector

1.0.1

Summary changed

1.0.0