How to write matrix elements in Mathematica? [closed]

For a matrix $$A$$, one computes the $$ij$$-$$th$$ matrix elements in a basis $$\{|e_i\rangle\}$$ as

$$A_{ij} = \langle e_i|A|e_j\rangle$$

How can one implement this in Mathematica? As an example, consider a matrix A = ( { {a11, a12}, {a21, a22} } );, how can one compute its matrix elements with respect to the basis $$\{|e_1\rangle, |e_2\rangle\}$$ where $$|e_{1(2)}\rangle$$ are the eigen vectors of matrix B= ( { {b11, b12}, {b21, b22} } ); Note that $$|e \rangle$$ is the Dirac "ket-vector" notation for the column vector, and $$\langle e|$$ is its Hermitian conjugate (a row vector) called as "bra-vector".

Failed attempt: I tried computing $$A_{11}$$ as follows (which doesn't seem to work):

    A11 = ConjugateTranspose[
Eigenvectors[B][[1]]].A.Eigenvectors[B][[1]]


displays following:

ConjugateTranspose: The first two levels of {-((-b11+b22+Sqrt[b11^2+4 b12 b21-2 b11 b22+b22^2])/(2 b21)),1} cannot be transposed.

• You don't need to transpose a vector, so Conjugate[v].A.v should suffice. Commented Feb 11, 2021 at 22:12
• But don't we need row.A.column?
– Mike
Commented Feb 11, 2021 at 22:22
• Please read Sjoerd's answer in the thread I linked to first, and if anything remains unclear, edit your question to say what you're having trouble with. Commented Feb 11, 2021 at 22:24
• I think my question is very explicit. And the suggestions you provided do not work either!
– Mike
Commented Feb 11, 2021 at 22:58
• @Mike To be clear: Define v = Eigenvectors[B][[1]]; then run Conjugate[v].A.v. Is the result not what you expect? If not, then please explain why not by editing the question like @JM had mentioned. Commented Feb 11, 2021 at 23:02

In MMA the rule for the dot product is: the last index of the left hand side is contracted with the first index of the right hand side. Therefore, no need for row and column vectors.

Let's define some matrix a and a matrix b with orthogonal eigenvectors:

SeedRandom[1];
a = RandomReal[{-1, 1}, {3, 3}];
b = RandomReal[{-1, 1}, {3, 3}]; b = b + Transpose[b];


Now to get the basis of eigenvectors of b (note the eigenvectors are in rows ) and show that they are orthonormal:

ev = Eigenvectors[b];
ev.Transpose[ev]


with this we can easily transform matrix a:

anew = Transpose[ev].a.ev


To show that this is still the same matrix in an other basis, we may compare the eigenvalues:

Eigenvalues /@ {a, anew}

(*{{1.17276, -1.15262, -0.110618}, {1.17276, -1.15262, -0.110618}}{{1.17276, -1.15262, -0.110618}, {1.17276, -1.15262, -0.110618}}*)