# How to implement this simple product rule in mathematica [duplicate]

Firstly i have defined a simple function below.

dotPro[a_, b_] := a[1]*b[1] + a[2]*b[2];

Then i create two terms using the above function.

t1=dotPro[q,σ];
t2=dotPro[ϵ,σ];

Then i have some rules regarding how these parameters multiply

r1 = Rule[σ[1]^2, 1];
r2 = Rule[σ[2]^2, 1];
r3 = Rule[σ[1] σ[2], I*σ[3]];
r4 =Rule[σ[2] σ[1], -I*σ[3]];

Now i expand the product

res = Expand[t1*t2]

Finally i apply the said rules to my expanded terms

res /. r1 /. r2 /. r3 /. r4

The answer i get is the following

(q[1] ϵ[1] + q[2] ϵ[2] + I q[2] ϵ[1] σ[3] + I q[1] ϵ[2] σ[3])

What i want to get is this(minus sign)

q[1] ϵ[1] + q[2] ϵ[2] -I q[2] ϵ[1] σ[3] + I q[1] ϵ[2] σ[3]

I know what the problem is,its related to mathematica assuming commutative product.so at the heart it deals with implementing non-commutative algebra.

So i did find a link to a package here but it was not working and i think this simple thing can be achieved without resort to any packages.

All i want is whenever there is a term with σ[1] σ[2] it should be replaced by I*σ[3] and for σ[2] σ[1] it should be replaced by -I*σ[3]

I tried to achieve it like this,but no success

list = {{1, 1}, {1, 2}, {2, 1}, {2, 2}};
t1[[#[[1]]]]*t2[[#[[2]]]] & /@ list /. r1 /. r2 /. r3 /. r4
• Could you please specify the commutators of q, σ, ε. That is, what are [q[i],σ[j]], [q[i],ε[j]], and [σ[i],ε[j]]? – Hector Aug 30 '13 at 15:08
• q, σ, ε all commute with each other, namely [q[i],σ[j]]=0, [q[i],ε[j]]=0 and [σ[i],ε[j]]=0 – Hubble07 Aug 30 '13 at 15:26
• The σ are exactly the matrices PauliMatrix[i], so it's easiest to realize this algebra using them explicitly. You can always re-express results of explicit matrix algebra in terms of the Pauli matrices because the latter (together with IdentityMatrix[2] or PauliMatrix[0]) form a basis of the space of $2\times 2$ matrices. – Jens Aug 30 '13 at 16:01
• @Jens Thanks for the link.i think it will help.i will look at it later and see if can apply it my problem.Also as you said if i actually introduced the sigma matrix like s[x_] := PauliMatrix[x]; Then off-course i get the correct matrix representation but what i really want is the symbolic sigma3 in my answer because i have to use that in my further calculations. – Hubble07 Aug 30 '13 at 16:28

The following code is a quick and dirty solution to your request. The basic idea is to avoid using the usual multiplication and use Dot instead.

dotPro[a_, b_] := a[1].b[1] + a[2].b[2];
dotPro[q, σ].dotPro[ε, σ]
Distribute[%]
% /. {Dot[x___, a_, y___, a_, z___] :> Dot[x, y, z]
, Dot[x___, a_[1], y___, a_[2], z___] :> I Dot[x, y, a[3]]
, Dot[x___, a_[2], y___, a_[1], z___] :> -I Dot[x, y, a[3]]}
• @ hector thanks, this is good enough for me. – Hubble07 Aug 31 '13 at 5:49

To flesh out what I mentioned in the comment: one can also attack this problem by implementing the non-commuting objects $\sigma$ using PauliMatrix, because the given rules in the question are faithfully represented by these $2\times 2$ matrices. All algebraic simplifications then occur in the matrix form.

But the goal is to get back a result in terms of the symbolic matrices $\sigma$. Fortunately, this can always be done because the Pauli matrices when combined with the unit matrix form a basis of the vector space of two-dimensional matrices. This means that for a given matrix a, the equation Solve[{x1, x2, x3, x4}.PauliMatrix[{0, 1, 2, 3}] == a] has one and only one solution {x1, x2, x3, x4}. These coefficients can be used to form a linear expression using the symbolic matrices $\sigma$ in the end.

So here is a function pauliReduce which takes an expression involving the symbolic σ[i] (where i = 0, 1, 2, 3), and returns a simplified result in terms of the same symbols, and potentially the unit matrix which I called $\hat{1}$ to distinguish it from the number 1. The unit matrix is also given by σ[0].

Clear[pauliReduce]
pauliReduce[a_] := Module[
{x, symbolicPauliIndices, expression},
x = Array[\[FormalX], 4];
symbolicPauliIndices =
DeleteDuplicates[Cases[a, σ[i_Symbol] :> i, Infinity]];
expression = {OverHat[1], σ[1], σ[2], σ[3]}.(
x /. First[
Solve[
x.PauliMatrix[{0, 1, 2, 3}]
==
a /. σ[i_] :>
Sum[KroneckerDelta[i, k] PauliMatrix[k], {k, 0, 3}],
x
]
]
);
FullSimplify[
expression,
Assumptions ->
Map[# \[Element] Integers && 1 <= # <= 3 &,
symbolicPauliIndices]]
]

Here are some relations that can now be proved:

pauliReduce[σ[1].σ[2]]

$\text{I}$ σ$[3]$

pauliReduce[σ[1].σ[2] - σ[2].σ[1]]

2 $\text{I}$ σ$[3]$

pauliReduce[σ[1].σ[1]]

$\hat{1}$

pauliReduce[σ[1].σ[2] + σ[2].σ[1]]

0

pauliReduce[MatrixExp[α σ[1]]]

Cosh[α] $\hat{1}$ + Sinh[α] σ$[1]$

pauliReduce[σ[i].σ[j] + σ[j].σ[i]]

For the example in the question, I get this (after correcting the multiplications to make them matrix products where needed):

res =
q[1] ϵ[1] σ[1].σ[1] +
q[2] ϵ[1] σ[1].σ[2] +
q[1] ϵ[2] σ[1].σ[2] +
q[2] ϵ[2] σ[2].σ[1];

pauliReduce[res]

$\hat{1}\,q[1] ϵ[1] + \text{I} (q[2] (ϵ[1] - ϵ[2]) + q[1] ϵ[2])\, σ[3]$

The advantage of this approach is in particular shown by the application of MatrixExp which is automatically simplified. In a more algebraic approach without matrix representation, you would first have to introduce the series expansion for such matrix functions by hand.