[Mathematica 6.0+] Preserving order of variables

  • Context: Mathematica 
  • Thread starter Thread starter Hepth
  • Start date Start date
  • Tags Tags
    Variables
Click For Summary

Discussion Overview

The discussion focuses on how to preserve the order of variables in Mathematica, particularly when defining functions that involve symbolic multiplication. Participants explore various methods to achieve this while maintaining the symbolic nature of the expressions.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant asks if there is a way to set Mathematica to preserve the order of variables in expressions, providing an example where the output does not meet their expectations.
  • Another participant suggests using NonCommutativeMultiply as a potential solution, noting that it may require additional definitions to work correctly.
  • A different participant describes their need to handle a variety of mathematical objects (matrices, vectors, scalars, tensors) while keeping the output symbolic and avoiding simplifications that Mathematica might apply.
  • One participant proposes a function, ExpandNCM, to facilitate distributivity while preserving order, providing an example of its use.
  • Another participant mentions the possibility of using ClearAttributes[Times, Orderless] but expresses doubt about its effectiveness due to unpredictable behavior of the Orderless attribute.

Areas of Agreement / Disagreement

Participants express a range of approaches to the problem, with no consensus on a single best solution. Multiple competing methods are discussed, indicating ongoing exploration and debate.

Contextual Notes

Participants mention the need for symbolic representation and the challenges posed by Mathematica's simplification processes. There are references to specific mathematical structures that may complicate the preservation of order.

Who May Find This Useful

This discussion may be useful for Mathematica users dealing with symbolic mathematics who need to maintain the order of variables in their expressions, particularly in contexts involving noncommutative operations.

Hepth
Science Advisor
Gold Member
Messages
458
Reaction score
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
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?
 
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!
 
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:

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K