axiom-developer
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[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 [1]: A = Matrix([[1,2],[5,9],[7,11],[3,1]])

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


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

In [4]: B
Out[4]:
1 2 3
5 7 9


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


In [6]: 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 [1]: from sympy.modules.paulialgebra import Pauli

In [2]: sigma1=Pauli(1)

In [3]: sigma2=Pauli(2)

In [4]: sigma3=Pauli(3)

In [5]: sigma1*sigma2
Out[5]: I*sigma3

In [6]: sigma1*2*sigma1
Out[6]: 2

In [7]: sigma1**1
Out[7]: sigma1

In [8]: sigma1**2
Out[8]: 1

In [9]: sigma1*sigma2
Out[9]: I*sigma3

In [10]: sigma2*sigma1
Out[10]: -I*sigma3

In [11]: sigma1*sigma2+sigma2*sigma1
Out[11]: 0

In [12]: sigma1*sigma2-sigma2*sigma1
Out[12]: 2*I*sigma3

In [13]: (sigma1+sigma2)**2
Out[13]: (sigma2+sigma1)**2

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

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


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

Ondrej




reply via email to

[Prev in Thread] Current Thread [Next in Thread]