[Mathematica 6.0+] Preserving order of variables

  • Mathematica
  • Thread starter Hepth
  • Start date
  • Tags
    Variables
In summary, the conversation discusses setting Mathematica to preserve the order of variables in a specific way, using NonCommutativeMultiply and matrix multiplication functions. The conversation also mentions a possible solution for distributivity using ExpandNCM and the potential use of ClearAttributes to achieve the desired results.
  • #1
Hepth
Gold Member
464
40
Is there a way to set Mathematica so that it preserves the order of variables.
i.e. I put:

F[x_] = A B x
F[a]
output : aAB

where I would want it: ABa

Thanks.
 
Physics news on Phys.org
  • #2
I am having the same problem.
The best solution I have found in general is using NonCommutativeMultiply:
F[x_] := A ** B ** x
although you usually have to define lots of other stuff to get everything working out as you want.
In some cases (like where A and B are explicitly defined matrices) you can use matrix multiplication
F[x_] := A . B . x
or a function
F[x_] := AB[x]

Do you have anything specific in mind?
 
  • #3
I just have a long string of things that could either be matrices or vectors or scalars or tensors or etc. I'm trying to do some simplifications and then use the output. Its nothing special, I just need to preserve order. I'm not doing any REAL products (like inner, outer, contractions, etc) just representative products. If I define everything as matrices/vectors etc then it'll try to simplify it which i don't want. It needs to stay symbolic, yet perform basic distribution multiplication.

I guess I'll try the noncommutative, that should work.
Thanks!
 
  • #4
If you want distributivity, you probably want to define something like
Code:
ExpandNCM[x_] := x //. {
   NonCommutativeMultiply[a__, b_ + c_, d__] :> a ** b ** d + a ** c ** d
   }

So that, for example,
Code:
a ** (b + c) ** d // ExpandNCM
gives
Code:
a ** b ** d + a ** c ** d
.

By the way, you can also try
Code:
ClearAttributes[Times, Orderless]
but I don't think it's the best solution because the Orderless attribute gets re-set seemingly at random.
 
Last edited:

Related to [Mathematica 6.0+] Preserving order of variables

1. How can I preserve the order of variables in my Mathematica 6.0+ code?

In Mathematica 6.0+ (and newer versions), the order of variables can be preserved by using the HoldAll attribute in functions or by using the HoldForm function. This prevents the automatic reordering of variables and preserves the order in which they are entered.

2. Why is it important to preserve the order of variables in Mathematica 6.0+?

The order of variables is important in Mathematica 6.0+ because it affects the evaluation and simplification of expressions. If the order is changed, the result may also change, leading to incorrect calculations.

3. Can I change the order of variables in Mathematica 6.0+?

Yes, it is possible to change the order of variables in Mathematica 6.0+ by using the function ReplaceAll (or its shorthand notation, /.) to substitute variables with different orderings.

4. How do I check if the order of variables is preserved in my Mathematica 6.0+ code?

To check if the order of variables is preserved, you can use the function HoldForm to display the unevaluated form of the expression. This will show if the variables are in the correct order or if they have been automatically reordered.

5. Are there any drawbacks to preserving the order of variables in Mathematica 6.0+?

One potential drawback is that it may make the code more difficult to read and understand, especially for beginners. It may also be more time-consuming to manually specify the order of variables in every function or expression. However, in most cases, the benefits of preserving the order outweigh any potential drawbacks.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
944
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top