'Why does TensorContract differ from reshaped arrays in Mathematica?
I am recently trying to perform tensor calculations with Mathematica as I came across problems like these:
Given two tensors M,N (here matrices), I want to compute something like
m = RandomReal[{}, {2, 4}];
n = RandomReal[{}, {2, 4}];
TensorContract[TensorProduct[m, n], {2, 4}]
={{1.2092, 0.892127}, {0.497884, 0.14624}}
i.e. the contraction w.r.t. the second index respectively. But whenever I try to alternatively compute
m . Transpose[n, {2, 1}]
={{1.2092, 0.892127}, {0.497884, 0.14624}}
it looks like the same value but still the difference is non-vanishing:
TensorContract[TensorProduct[m, n], {2, 4}] - m . Transpose[n, {2, 1}]
={{0., 0.}, {-5.55112*10^-17, -2.77556*10^-17}}
I would be very thankful if some of you guys would try and help me understand why there is a difference between these two approaches, maybe what they have to do with MachinePrecision and perhaps what value is more accurate. Any help is appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
