[Mathematica 6.0+] Preserving order of variables
- Context: Mathematica
- Thread starter Hepth
- Start date
-
- Tags
- Variables
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 6K views
Discussion
Physics news on Phys.org
Science Advisor
Homework Helper
- 4,305
- 49
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?
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?
Science Advisor
Gold Member
- 458
- 40
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!
I guess I'll try the noncommutative, that should work.
Thanks!
Science Advisor
Homework Helper
- 4,305
- 49
If you want distributivity, you probably want to define something like
So that, for example,
gives
.
By the way, you can also try
but I don't think it's the best solution because the Orderless attribute gets re-set seemingly at random.
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
Code:
a ** b ** d + a ** c ** d
By the way, you can also try
Code:
ClearAttributes[Times, Orderless]
Last edited:
Similar threads
MATLAB Separating matrix elements by criteria while preserving order
- dpsguy
- · Replies 8 ·
- MATLAB, Maple, Mathematica, LaTeX
- Replies
- 8
Mathematica Why Does My ParametricPlot3D Look Choppy in Mathematica 6.0?
- robert spicuzza
- · Replies 1 ·
- MATLAB, Maple, Mathematica, LaTeX
- Replies
- 1
Order of variables in a Jacobian?
- peripatein
- · Replies 3 ·
- Calculus and Beyond Homework Help
- Replies
- 3
High School Order of Variables and Constants in Mathematical Expressions
- Cspeed
- · Replies 4 ·
- General Math
- Replies
- 4
Quick order preserving map question
- EV33
- · Replies 3 ·
- Calculus and Beyond Homework Help
- Replies
- 3