       • To: mathgroup at smc.vnet.net
• From: David Bailey <dave at Remove_Thisdbailey.co.uk>
• Date: Sat, 17 May 2008 23:28:28 -0400 (EDT)
• References: <g0m918\$3n\$1@smc.vnet.net>

```gstaples wrote:
> I wrote a procedure to define the non-commutative product of two elements of the form e_X e_Y, where X and Y are multi-indices.  I do this using Times.
>
> I do a simple calculation like
>
> Expand[(e_{1} + e_{3})(e_{1} - e_{3})]
>
> and get the wrong answer.  (On the first try.)
>
> If I then re-execute the code that defines the multiplication, I repeat the calculation and get correct output.
>
> Alternatively, if I execute
>
> e=Symbol["e"]
>
> before loading the procedure, I get correct output on the first try.
>
> More troubling is that after getting correct output, if I perform calculations of a different kind (e.g. matrix multiplication), when I repeat my original calculation, I start getting incorrect output again.  (Until I re-execute the code that defines the multiplication.)
>
> It's frustrating to repeat a calculation and get different sets of output depending on what other calculations are done in between.
>
> Does anyone have suggestions?
>
You don't actually give your procedure, but the first step is not to try
to do non commutative algebra using Times. Even though you can, in
principle, use ClearAttributes[Times,Orderless] (to make Times non
commutative), re-defining the basic arithmetic operators in this way
will not produce consistent results, and can break a lot of other code.

You should represent your non commutative multiplication using ** or
\[CircleTimes] (the latter is much more elegant to view) and define
operations involving these operators as you see fit. For example:

SetAttributes[CircleTimes, Flat]
CircleTimes[a_, b_] := -CircleTimes[b, a] /; (! OrderedQ[{a, b}])

This will use anticommutation to reduce expressions such as
b\[CirceTimes]a to a standard order (alphabetical order, as written, but
you can obviously devise other orderings to suit your needs.

Note that you have to be a bit careful how you formulate the test in the
above to avoid an infinite series of replacements.

Although defining expressions involving CircleTimes explicitly in this
way is the most direct way to do noncommutative algebra, I think it is
better to leave expressions such as a\[CircleTimes]b inert and define
functions that operate on such expressions to produce various
transformations.

David Bailey
http://www.dbaileyconsultancy.co.uk

```

• Prev by Date: Re: Re: Replace non-numeric values in a list
• Next by Date: Re: Range of Use of Mathematica