3

If A, B are two lists with length n and M is a n*n Matrices, then I want to compute the inner product of them B.M.A. The result should be a scalar however, what I get is a matrix. Any one knows what happened here? Thanks! My code is below

A = {Subscript[z, 0], Subscript[z, 1], Subscript[z, 2],
    Subscript[z, 3], Subscript[z, 4]};
B = {Subscript[OverBar[z], 0], Subscript[OverBar[z], 1], Subscript[OverBar[z], 2], 
    Subscript[OverBar[z], 3], Subscript[OverBar[z], 4]};

    M = Chop[{{0.9947525414706575`, 
        0.014318078739690324` + 
         0.012070727204725996` I, -0.022576963511297447` + 
         0.017365770023498133` I, 
        0.028293920126770736` + 0.0041202947110136594` I, 
        0.02281394023398197` - 
         0.0005272617323759155` I}, {0.014318078739690316` - 
         0.012070727204725998` I, 1.0089738927082252`, 
        0.02221807191535088` - 
         0.018113027609091715` I, -0.012682665041359427` - 
         0.010424540405231546` I, -0.055382503060447716` + 
         0.035624018712441564` I}, {-0.02257696351129744` - 
         0.017365770023498137` I, 
        0.022218071915350877` + 0.018113027609091715` I, 
        1.0204773571789212`, -0.0007522563609968502` + 
         0.026534550066808438` I, 
        0.0003575302509902032` - 
         0.0635032495006343` I}, {0.028293920126770733` - 
         0.00412029471101366` I, -0.012682665041359427` + 
         0.010424540405231544` I, -0.000752256360996853` - 
         0.026534550066808438` I, 1.0132613424630528`, 
        0.059304690914936876` - 
         0.01648460212183861` I}, {0.02281394023398197` + 
         0.0005272617323759158` I, -0.055382503060447716` - 
         0.035624018712441564` I, 
        0.0003575302509902011` + 0.06350324950063431` I, 
        0.059304690914936876` + 0.016484602121838613` I, 
        0.9911763824117488`}}]

In[31]:= Dimensions[B.M.A]

Out[31]= {5}
Carl Woll
  • 130,679
  • 6
  • 243
  • 355
cwei
  • 155
  • 5
  • 1
    You must keep in mind that everything is an expression. Your calculation results in a symbolic sum of five terms; see FullForm[A.M.B]. Dimensions is returning the dimensions of the head of the expression (Plus) which has 5 terms. – Edmund Oct 25 '18 at 21:56
  • @Edmund Thanks for your explanation! I understand now! – cwei Oct 25 '18 at 22:11

2 Answers2

7

Dimensions works for arbitrary heads. If you restrict to only allowing a List head:

Dimensions[B.M.A, AllowedHeads->List]

{}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
2

Here's a simple example:

a = RandomReal[{-1, 1}, 10];
b = RandomReal[{-1, 1}, 10];
m = RandomReal[{-1, 1}, {10, 10}];
a.m.b

This does give a scalar answer, as expected.

bill s
  • 68,936
  • 4
  • 101
  • 191
  • Thanks! Yes! It works for numbers. But for an expression involved with variables. Dimension[expr] does not work properly if you don't specify head explicitly. – cwei Oct 25 '18 at 21:58
  • I was showing you that your result is a scalar, irrespective of what Dimensions is telling you. – bill s Oct 25 '18 at 22:00
  • Yes! I understand your example. – cwei Oct 25 '18 at 22:09