3
$\begingroup$

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}
$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – Edmund
    Oct 25, 2018 at 21:56
  • $\begingroup$ @Edmund Thanks for your explanation! I understand now! $\endgroup$
    – cwei
    Oct 25, 2018 at 22:11

2 Answers 2

7
$\begingroup$

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

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

{}

$\endgroup$
5
  • $\begingroup$ Thank you for your reply! However, what I really want is to make sure this result is a scalar. This is only an Intermediate result. I need to use it do more complicated linear algebra computation. $\endgroup$
    – cwei
    Oct 25, 2018 at 21:32
  • 1
    $\begingroup$ @cwei I fail to see why my code doesn't suffice. Perhaps you could use ListQ[B.M.A] instead. $\endgroup$
    – Carl Woll
    Oct 25, 2018 at 21:34
  • $\begingroup$ Or ArrayQ. Maybe also TensorQ? $\endgroup$ Oct 25, 2018 at 21:41
  • $\begingroup$ @CarlWoll Problem solved! Your answer is correct! Thanks! $\endgroup$
    – cwei
    Oct 25, 2018 at 21:49
  • $\begingroup$ @HenrikSchumacher Yes! They work as well! Thanks! $\endgroup$
    – cwei
    Oct 25, 2018 at 21:50
2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – cwei
    Oct 25, 2018 at 21:58
  • $\begingroup$ I was showing you that your result is a scalar, irrespective of what Dimensions is telling you. $\endgroup$
    – bill s
    Oct 25, 2018 at 22:00
  • $\begingroup$ Yes! I understand your example. $\endgroup$
    – cwei
    Oct 25, 2018 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.