Thursday, June 18, 2009

Outer: Construct matrix from lists

In Mathematica, a quick way to construct a matrix from lists is through Outer. Let us consider two lists defined as
a={a1,a2,a3};
b={b1,b2};
Then the following command
Outer[f, a, b]
gives a matrix of the form
f[a1,b1] f[a1,b2]
f[a2,b1] f[a2,b2]
f[a3,b1] f[a3,b2]

A practical example is given below. Consider d-band electrons with spins. There are five d-orbitals and two spin states. Suppose orbital and spin stores the orbital and spin part of the state vector. The complete basis can be obtained by
Flatten[Outer[Join, orbital, spin, 1], 1]