Message Boards Message Boards

[✓] Factor out matrices in expressions?

GROUPS:

Hi Community,

I am working on a tutorial for matrix algebra, I cannot figure out how to make Mathematica identify and replace previously defined symbols in calculations. For example. I have defined

rules = {IdentityMatrix[2] -> Subscript[\[Sigma], 0], 
  PauliMatrix[1] -> Subscript[\[Sigma], 1], 
  PauliMatrix[2] -> Subscript[\[Sigma], 2], 
  PauliMatrix[3] -> Subscript[\[Sigma], 3]}

So when I make

Grid[Join[{Table[PauliMatrix[i] // MatrixForm, {i, 3}]}, 
  Table[MatrixForm[PauliMatrix[i].PauliMatrix[j] /. rules], {i, 
    3}, {j, 3}]]]

I get Subscript[\[Sigma], 0] in the diagonal, but cannot figure out how I make Mathematica to identify the other elements as simply products of +/- I and the other Pauli matrices.

Or simpler put: how do I make Mathematica answer I PauliMatrix[3] to the input

PauliMatrix[1].PauliMatrix[2]

Kind regards

Mogens

Ps. Is it true that Solve etc. does not work on matrices, or is it just me? (I have tried to specify Matrices[{2,2},Complexes] as the domain but I get errors or nothing back)

POSTED BY: jallberg
Answer
7 months ago

PauliMatrix[1].PauliMatrix[2] and I PauliMatrix[3] evaluate to the same matrix.

In[1119]:= {PauliMatrix[1].PauliMatrix[2], I PauliMatrix[3]}

(* Out[1119]= {{{I, 0}, {0, -I}}, {{I, 0}, {0, -I}}} *)

Given that, it is quite unclear what is wanted here.

On a separate note, you are correct that Solve does not work with the Matrices[...] domains (It works fine with explicit matrices but that is not the same thing).

POSTED BY: Daniel Lichtblau
Answer
7 months ago

Thanks Daniel,

What I meant was, can I make Mathematica recognise PauliMatrix[1].PauliMarix[2] as I PauliMatrix[3]? So rather than answer {{I, 0}, {0, -I}} it would say I PauliMatrix[3] (and ultimately use the sigma symbols I have defined)?

I guess it would be similar in simpler expressions. Suppose I defined

a = x+3

how would I make Mathematica reduce

x^2 + 9 + 6x

to

a^2

?

POSTED BY: jallberg
Answer
7 months ago

You should understand that evaluation in the Wolfram Language means something like I PauliMatrix[3] will automatically become an explicit matrix {I, 0}, {0, -I}}. There are ways to preclude this using Hold but they are more trouble than they are worth. An alternative route might be to work with your own purely symbolic pauliMatrix (note lower casing of first word, to distinguish from the built-in function). When you want to do symbolic manipulations you might use NonCommutativeMultiply as operator, and only when you want to convert to explicit matrices do you use substitutions such as {NonCommutativeMultiply->Dot,pauliMatrix->PauliMatrix}.

Also for the purpose at hand you might want to consider introducing a commutator, so that the comcommutative products can be "canonicalized" prior to using a substitution such as this: pauliMatrix[1] ** pauliMatrix[2] -> I*pauliMatrix[3]. One way to go about this is shown here.

https://www.researchgate.net/publication/262223580_Symbolic_FAQ

See the section "Some noncommutative algebraic manipulation", in particular the example that defines and uses a function called ncTimes.

POSTED BY: Daniel Lichtblau
Answer
7 months ago

Thanks again Daniel,

It really is a shame! I had hoped Mathematica would be smart enough to recognise, say, that some matrix was actually the product of an integer and the identity matrix and such; especially if I hinted it by saying "look for multiples of the identity marix" somehow. Can't be far off in the future with a bit of machine learning though.

Great link - there are lots of useful information there. I had started on commute and anti-commute functions but this helps me leap forward.

/Mogens

POSTED BY: jallberg
Answer
7 months ago

In fact, giving it a little more thought, this is a rather general request. There are numerous situations where you would want to reduce your expressions by substituting parts of them by named (sub-)expressions, or rather: where you would want to use Mathematica to explore the possibility too see if it leads to a simpler result. Using the above example with

a = x+3

rewriting x+4 as a+1 or identify Exp[3] Exp[x] as Exp[a] are just a couple of other situations.

So I search for a way to tell Mathematica to look for certain patterns that I provide in an expression, and show what the expression would look like if the pattern were to be "eliminated". I realise that in some instances this would require a hand-held effort on my side, because the would be ambiguities, but in the example with the Pauli Matrices I think is pretty straight forward.

Hope some of you experienced users can point me in the right direction.

/Mogens

POSTED BY: jallberg
Answer
7 months ago

It seems to me that your principal objective here is to write tutorials by presenting derivations and proofs in a clear manner but have somehow gotten a little bit on a false path. But your examples are too simple to make clear how you want to present them by "searching for patterns". Let's take the Pauli matrix example. We could define numeric and symbolic variables.

PMat1 = {{0, 1}, {1, 0}};
PMat2 = {{0, -I}, {I, 0}};
PMat3 = {{1, 0}, {0, -1}};
symbolicRules = 
 Thread[{\[Sigma]1, \[Sigma]2, \[Sigma]3} -> 
   MatrixForm /@ {PMat1, PMat2, PMat3}] 

enter image description here

Then we can do a simple validation of the identity. We can add annotation for the steps. The Mathematica Inactive and Activate commands are very useful for step by step derivations. (But I don't like the light red font for the Inactive command and changed my style sheet to regular black. WRI used to have a Tooltip to identify Inactive commands but seem to have taken that away.)

simpleDerivation =
 pagelet[
  phrase2["Pauli matrix identity:"],
  step1 = Inactive[Equal][\[Sigma]1.\[Sigma]2, I \[Sigma]3],
  phrase2["Substitute the numeric Pauli matrices."],
  step2 = step1 /. symbolicRules,
  phrase2["Evaluate"],
  step3 = Activate[step2] /. MatrixForm -> Identity,
  Spacings -> 1.25
  ]

enter image description here

So, is that good enough for your readers? Here is a second derivation.

secondDerivation =
 pagelet[
  phrase2["Pauli matrix identity:"],
  step1 = Inactive[Equal][\[Sigma]1.\[Sigma]2, I \[Sigma]3],
  phrase2["Substitute the numeric Pauli matrices."],
  step2 = step1 /. symbolicRules,
  phrase2["Factor \[ImaginaryI] from \[Sigma]2 and the product."],
  step3 = 
   step2 /. \[Sigma]2 : MatrixForm[PMat2] -> 
     FactorOut[I, MatrixForm][PMat2],
  step4 = MapAt[LinearBreakout[Dot][_], step3, 1],
  phrase2["Evaluate the left hand side and then the equality."],
  step5 = 
   step4 // EvaluateAt[1, (MatrixForm[# /. MatrixForm -> Identity] &)],
  step6 = Activate[step5] /. MatrixForm -> Identity,
  Spacings -> 1.25
  ]

enter image description here

I've used a number of constructs from a Presentations application that I've developed over the years. The phrase and pagelet routines are just Row and Column constructs without the extra brackets. FactorOut will remove a factor from a larger expression. LinearBreakout is useful in expanding and factoring out constants from "linear" expressions.

There are also routines for defining subexpressions and holding them unevaluated in Mathematica simplifications. Also a set of routines for primitive operations on annotated matrix structures, mostly for didactic purposes. If you contact me, and tell me something about yourself, I can send a download link.

POSTED BY: David J M Park Jr
Answer
7 months ago

Thanks David,

I will write you directly as you suggested, but for the sake of this thread I wanted to thank you for the good advice and try to clarify what I am looking for.

Example

I want to present to reader with a table of the product of the Pauli matrices like

Table[PauliMatrix[i].PauliMatrix[j] // MatrixForm, {i, 3}, {j, 
   3}] // Grid

I would go on to show the results in terms of the Pauli matrices as well, i.e.

Grid[{{s0, I s3, I s2}, {-I s3, s0, I s1}, {-I s2, -I s1, s0}}]

(with s0 being the 2x2 Identity matrix, and si the i'th Pauli matrix).

Clearly, I can do this by hand, but then I might as well work in a typesetting program (like LaTeX). Also as I advance to to work on Dirac's Gamma matrices, (anti-)commute relations, contractions and such, it would be helpful to have Mathematica recognise e.g. that [s1,s2] = 2 I s3 without necessary being aware of it myself.

The "magic" command would be something like ExpressInTermsOf[myExpression,myFavoriteMatrixList], looking in myExpression for possible ways to rewrite it using the elements in myFavoriteMatrixList.

So I am very curious to see what your FactorOut does.

/Mogens

POSTED BY: jallberg
Answer
7 months ago

Group Abstract Group Abstract