0

I am new to Mathematica. Here is my problem:

Given the matrix

$\qquad M = \begin{pmatrix} 2 & -1 \\ 1 & 2 \end{pmatrix}$,

sum the overlap (inner-product) of it's eigenvectors

$\qquad \bigg\{\begin{pmatrix}i \\1 \end{pmatrix},\begin{pmatrix}-i \\1 \end{pmatrix} \bigg\}$.

To this end, I first reshape to form arrays with correct dimensions. I am then able to generate and print the overlap values in the form of a 1x1 array for each overlap pair, but am then having difficulty appending the result to a list as can bee seen by the empty list L2 in the output.

Can anyone advise me on what the problem is wrong with the attached code?

Appending Matrices to Lists

M = {{2, -1}, {1, 2}};
Eig = Eigenvectors[M]
Eval = Eigenvalues[M] ;
L1 = {};
L2 = {};

For[i = 1, i <= Length[M], i++,
AppendTo[L1, ArrayReshape[Eig[[i]], {Length[M], 1}]]]

For[i = 1, i <= Length[M], i++, For[j = 1, j <= Length[M], j++ , Print[Dot[ConjugateTranspose[L1[[i]]], L1[[j]]]]]]

For[i = 1, i <= Length[Gd2], i++, For[j = 1, j <= Length[Gd2], j++ , Append[L2 , Dot[ConjugateTranspose[L1[[i]]], L1[[j]]] ]]]

Print[L1] Print[L2]

{{I, 1}, {-I, 1}}

During evaluation of In[2198]:= {{2}}
During evaluation of In[2198]:= {{0}}
During evaluation of In[2198]:= {{0}}
During evaluation of In[2198]:= {{2}}
During evaluation of In[2198]:= {{{I},{1}},{{-I},{1}}}
During evaluation of In[2198]:= {}

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
John Doe
  • 371
  • 1
  • 11
  • 1
    I don't understand exactly what "sum the overlap (inner-product) of it's eigenvectors" means, but if you want the inner (dot) product of its eigenvectors, then m = {{2, -1}, {1, 2}}; Dot @@ Eigenvectors[m]? – MarcoB Jan 13 '21 at 15:32
  • 4
    For and AppendTo is almost always very slow. I think, for example, your For[i = 1, i <= Length[M], i++, AppendTo[L1, ArrayReshape[Eig[[i]], {Length[M], 1}]]] is better stated as L1 = List /@ Eig. See https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/18396#18396 – Patrick Stevens Jan 13 '21 at 15:33
  • 5
    But you've made one of the classic mistakes, I think: https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/19804#19804 – Patrick Stevens Jan 13 '21 at 15:34
  • @MarcoB I want to save each overlap pair (as printed in my output) to a list. – John Doe Jan 13 '21 at 15:51
  • @PatrickStevens Thanks for your response, I think you are right. I come from a Python coding background hence still learning the Mathematica conventions Is there a simpler way to do the main double for loop? – John Doe Jan 13 '21 at 16:00
  • 1
    Mathematica has a number of looping and nesting functions that are often easier than traditional For or Do. Have a look at Map, MapThread, Fold, FoldList, among others. – rhomboidRhipper Jan 13 '21 at 16:47
  • @JohnDoe Could you specify exactly what the output(s) should be in this case? I cannot understand your intentions from your code. – MarcoB Jan 13 '21 at 17:40
  • 1
    You've used Append where I think you wanted AppendTo – Simon Woods Jan 13 '21 at 19:39

0 Answers0