axiom-developer
[Top][All Lists]

## [Axiom-developer] Re: Commutative symbols

 From: Ondrej Certik Subject: [Axiom-developer] Re: Commutative symbols Date: Mon, 26 Mar 2007 17:17:51 +0200

```On 3/26/07, Ralf Hemmecke <address@hidden> wrote:
```
```On 03/26/2007 02:36 PM, Ondrej Certik wrote:
>> Hmmm, I would have thought that commutativity is a property of the
>> multiplication of the domain you are working in and not a property of a
>> symbol.
>
> I know - originaly I had a special class NCMul, for noncommutative
> multiplication. But first it duplicates some code and second - some
> symbols are commutative and some are not and I want to mix that. It's
> like when computing with matrices, like:
>
> A*3*x*B,
>
> where x is a variable and A,B matrices, then you want this to evaluate to:
>
> 3*x *A*B

Maybe this is not what you want...

(6) -> A: Matrix Integer := [[1,2],[5,9],[7,11],[3,1]]
(6) ->
+1  2 +
|     |
|5  9 |
(6)  |     |
|7  11|
|     |
+3  1 +
Type: Matrix Integer
(7) -> B: Matrix Integer := [[1,2,3],[5,7,9]]
(7) ->
+1  2  3+
(7)  |       |
+5  7  9+
Type: Matrix Integer
(8) -> A*3*x*B
+33x   48x   63x +
|                |
|150x  219x  288x|
(8)  |                |
|186x  273x  360x|
|                |
+24x   39x   54x +
Type: Matrix Polynomial Integer
(11) -> B*3*x*A
11) ->
>> Error detected within library code:
can't multiply matrices of incompatible dimensions

> and when you think about it, it's actually the symbols, that have this
> property - either you can commute it out of the expression, or you
> cannot.

Yes, here A and B are actually matrices, not symbols. It depends on what
you want.

Ralf
```
```
You can do the same in SymPy:

In : A = Matrix([[1,2],[5,9],[7,11],[3,1]])

In : A
Out:
1 2
5 9
7 11
3 1

In : B = Matrix([[1,2,3],[5,7,9]])

In : B
Out:
1 2 3
5 7 9

In : A*3*x*B
Out:
33*x 48*x
150*x 219*x
186*x 273*x
24*x 39*x

In : B*3*x*A
---------------------------------------------------------------------------
exceptions.AssertionError                            Traceback (most
recent call last)

/home/ondra/sympy/<ipython console>

/home/ondra/sympy/sympy/modules/matrices.py in __mul__(self, a)
157     def __mul__(self,a):
158         if isinstance(a,Matrix):
--> 159             return self.multiply(a)

.....

and a traceback. When there is something wrong, SymPy just raises an
exception, and the Python console then shows you a full traceback (if
the user wants).

But matrices are different kind of objects. I was talking about just
noncommutative symbols, for example the  Pauli algebra:

http://en.wikipedia.org/wiki/Pauli_matrices

In : from sympy.modules.paulialgebra import Pauli

In : sigma1=Pauli(1)

In : sigma2=Pauli(2)

In : sigma3=Pauli(3)

In : sigma1*sigma2
Out: I*sigma3

In : sigma1*2*sigma1
Out: 2

In : sigma1**1
Out: sigma1

In : sigma1**2
Out: 1

In : sigma1*sigma2
Out: I*sigma3

In : sigma2*sigma1
Out: -I*sigma3

In : sigma1*sigma2+sigma2*sigma1
Out: 0

In : sigma1*sigma2-sigma2*sigma1
Out: 2*I*sigma3

In : (sigma1+sigma2)**2
Out: (sigma2+sigma1)**2

In : ((sigma1+sigma2)**2).expand()
Out: 2

In : ((sigma1-sigma2)**2).expand()
Out: 2

But anyway, those are just minor details. I am sure every CAS can work
with such objects in some way.

Ondrej

```