Suppose we want to get the dot product for the following lists aa
and bb
, whose elements (a
, b
, etc.) are also lists. We can get the symbolic expression of aa.bb
first, and then assign numerical values of the elements in aa
and bb
.
aa = {{a, b}, {c, d}};
bb = {{{a, b}, {c, d}}, {{a, b}, {c, d}}};
cc = aa.bb;
(* -> {{{a^2 + a b, a b + b^2}, {a c + b c, a d + b d}}, {{a c + a d, b c + b d}, {c^2 + c d, c d + d^2}}} *)
a = b = c = d = {1, 2, 3}
cc
(* -> {{{{2, 8, 18}, {2, 8, 18}}, {{2, 8, 18}, {2, 8, 18}}}, {{{2, 8, 18}, {2, 8, 18}}, {{2, 8, 18}, {2, 8, 18}}}} *)
However, if we do not want to get the symbolic expression for aa.bb
(because it is too long in some situations), and only use numerical calculations, we cannot directly use Dot
in the same way as follows. Is there a simple way to fix this?
aa = {{a, b}, {c, d}};
bb = {{{a, b}, {c, d}}, {{a, b}, {c, d}}};
a = b = c = d = {1, 2, 3};
cc = aa.bb
(* -> Dot::dotsh: Tensors {{{1,2,3},{1,2,3}},{{1,2,3},{1,2,3}}} and {{{{1,2,3},{1,2,3}},{{1,2,3},{1,2,3}}},{{{1,2,3},{1,2,3}},{{1,2,3},{1,2,3}}}} have incompatible shapes. >> *)
Inner[]
? $\endgroup$Dot
works as you desire only when the dimensions are compatible; for example, witha=b=c=d={1,2}
. $\endgroup$