# Getting mathematica to factor out constants and apply euler's identity to a table

I'm doing some quantum mechanics homework that I'm going to eventually generalize to higher spin states, so the solutions are going to need to be decently independent of the dimensions of the table and constants that I'm using.

I'm trying to solve the eigenvalue problem for a generalized spin matrix (defined by angles in spherical coordinates) as shown below (currently spin-1/2, but will be generalized to higher dimensions soon). S is a column vector in complex 3-space for now. The components are going to be the b by b square spin matrices (x, y, and z) for whatever spin we are in. They'll be Pauli matrices times a constant- obviously for larger spin particles, I'm going to have to calculate them separately. n will always be a column vector, b long, that defines our basis for the eigenvalue problem. It will always be a unit vector and will always be in terms of sines and cosines of ungodly angles- like spherical coordinates. Sn is the dot product of these, but of course you can't calculate it like a dot product because the components of S are matrices. :(

b = 3
S = h/2*Table[PauliMatrix[k], {k, b}]
n = {Sin[\[Theta]]*Cos[\[Phi]], Sin[\[Theta]]*Sin[\[Phi]], Cos[theta]}
Sn = Sum[S[[k]]*n[[k]], {k, b}] // MatrixForm


To progress in this (pretty fundamental) problem, especially in more tedious higher dimensional matrices, I need to make the resulting matrix a lot prettier by factoring out constants and applying Euler's identity.

So Snis what I'm trying to solve for. I get a 2x2 matrix with ugly terms, and every term has a factor of h/2 in it. I'd like to factor this out here, but with higher spin states, I'm going to have different weird multiples of h. These will always be integers times the constant at the beginning of S's definition. Not really sure how to do this- Hold would screw up the intermediate matrix multiplication and table lookups, right?

Also, in the off-diagonal slots for this specific matrix, you can see that there are factors of Cos[phi]+iSin[phi] in both slots. I need to apply Euler's identity, ie, turn each factor of that into e^(i*phi). The issue, again, is that when I generalize into higher dimensions, there definitely will be angles that aren't phi that need euler's identity applied, and I have no idea how ugly the factoring will get. Replace won't work here, unless I'm confused about how to get it to replace with unspecified variables.

If anyone has ideas about how I can get Mathematica to do the leg work of factoring in those nasty higher spin states, I would really appreciate it.

Tl;dr? I need to have Mathematica RIGOROUSLY pull constants out of an n*n matrix and apply Euler's identity wherever it possibly can. Everything will be awfully symbolic and stuck in tables upon tables. Good luck, wizards.

• You've seen TrigToExp[]? – J. M.'s technical difficulties Nov 15 '15 at 5:38
• Hold on wow... That could be really useful. is there a way to get that to work so that it doesn't convert /ALL/ the trig, just the ones that fit perfectly into euler's identity? I don't want a whole mess of e^i*theta, just where it makes the answer more algebraically simple. – laudiacay Nov 15 '15 at 6:29
• It would be useful if you can give an example where TrigToExp[] "over-converts", and what you were expecting instead. – J. M.'s technical difficulties Nov 15 '15 at 6:48
• Well, like, the matrix in that example- it turns those cosines on the diagnoal of the matrix in terms of e. In this example, I would have wanted a TrigToExp conversion where Euler's identity applies on the off-diagonal terms, but not on any of the terms with theta in this case. – laudiacay Nov 15 '15 at 7:31
• Then, you use MapIndexed[] for that purpose:MapIndexed[If[Equal @@ #2, #1, TrigToExp[#1]] &, Sn, {2}]. – J. M.'s technical difficulties Nov 15 '15 at 7:59

You can intersect the results of FactorList to pull out common factors involving h.

The setup:

b = 3;
sS = h/2*Table[PauliMatrix[k], {k, b}];
n = {Sin[\[Theta]]*Cos[\[Phi]], Sin[\[Theta]]*Sin[\[Phi]], Cos[theta]};
sSn = Sum[sS[[k]]*n[[k]], {k, b}]

(* Out[62]= {{1/2 h Cos[theta],
1/2 h Cos[\[Phi]] Sin[\[Theta]] -
1/2 I h Sin[\[Theta]] Sin[\[Phi]]}, {1/
2 h Cos[\[Phi]] Sin[\[Theta]] +
1/2 I h Sin[\[Theta]] Sin[\[Phi]], -(1/2) h Cos[theta]}} *)


Use FactorList to find all factors, Intersect to get the ones that are common to all entries, and convert from sublists to a product.

commonFaxList = Intersection @@ Map[FactorList, Flatten@sSn]

(* Out[58]= {{2, -1}, {h, 1}} *)

commonFax = Times @@ Map[#[[1]]^#[[2]] &, commonFaxList]

(* Out[64]= h/2 *)


Now remove that common factor and maybe do more factoring.

Factor[Together[sSn/commonFax], Trig -> True]

(* Out[70]= {{Cos[theta],
Sin[\[Theta]] (Cos[\[Phi]] -
I Sin[\[Phi]])}, {Sin[\[Theta]] (Cos[\[Phi]] +
I Sin[\[Phi]]), -Cos[theta]}} *)


Also (as noted in comments) there is TrigToExp.

TrigToExp[Together[sSn/commonFax]]

(* Out[67]= {{E^(-I theta)/2 + E^(I theta)/2,
1/2 I E^(-I \[Theta] - I \[Phi]) -
1/2 I E^(I \[Theta] - I \[Phi])}, {1/
2 I E^(-I \[Theta] + I \[Phi]) -
1/2 I E^(I \[Theta] + I \[Phi]), -(1/2) E^(-I theta) - E^(I theta)/
2}} *)