How to remove commutativity from symbolic multiplication

31 vues (au cours des 30 derniers jours)
Harpal Toor
Harpal Toor le 15 Fév 2020
Hello, I am attempting to multiply two 4th dimensional vectors with varying coefficients depending on inputs, however I do not have commutivity in this scenario. It seems that MATLAB is assuming there is, is it possible to bypass this using symbolic variables or otherwise?
The code I'm using is attached. I am multiplying q_1 and q_2 and each of the symbolic substitutions (subs) are rules I am applying i.e and .
  2 commentaires
James Tursa
James Tursa le 15 Fév 2020
To be clear, you are trying to implement quaternion multiplication symbolically, and the quaternion i, j, and k multiplication is not commutative ... they behave like the cross product rule.
Been there, done that (or tried to do that), and gave up myself ...
If you ever figure out a reasonable way to do it without rewriting all the symbolic stuff from scratch you deserve a gold star and I will buy you lunch!
Walter Roberson
Walter Roberson le 15 Fév 2020
Quaternions are built in to the symbolic engine, https://www.mathworks.com/help/symbolic/mupad_ref/dom-quaternion.html
You would need to use evalin(symengine) or feval(symengine) to access those functions. And the results might potentially mess up the display interface :(

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 15 Fév 2020
In MATLAB, individual symbolic variables are always assumed to be scalars, and commutivity applies to scalars.
It is not impossible to create symbolic mathematics in MATLAB that do not assume commutivity, but it would require creating a number of routines at the MuPad level to define new data structures and define as much mathematics around them as you need. For example if you needed calculus, you would have to program the Chain Rule yourself. To do a thorough job you would have to know quite a bit about how the mathematics routines are implemented in the MuPad engine, and that is something that Mathworks is discouraging. For example the Notebook interface to program at the MuPad level is going away any day now.
Maplesoft's Maple is slightly better in that it has a noncommutative multiplication operation available that does not directly simplify to commutative, but a lot of Maple assumes that individual symbolic variables are scalars...
  1 commentaire
Walter Roberson
Walter Roberson le 15 Fév 2020
To amplify a bit:
At the MATLAB level, the way that symbolic functions are created is to evaluate a symbolic expression fully, and then to mark the resulting symbolic object as being a symbolic function, storing the parameter names as it goes. For example if you have
syms y(t)
y(t) = t*t';
then the expression t*t' is fully evaluated first. MATLAB would look and see that t is a scalar symbolic variable, and that for scalars, ' is equivalent to conj() and conj would convert a scalar to a scalar, so it demotes the * between the t and the conj(t) to commutative element-by-element multiplication, which it represents as t*conj(t) but which is really t.*conj(t) -- same as you would get if you had coded t'*t . MATLAB absolutely does not consider the possibility that t is standing in for a matrix that might need matrix-multiplication: at the time the right hand side is evaluated, t is a scalar and that is good enough for it. After that, the resulting t*conj(t) is blessed into a symbolic function with parameter t.
Maplesoft's Maple works a bit diffferently. If you were to evaluate or at the maple command line, you would get the same effect, that t would be treated as a scalar, and you would get . However, when you are defining a maple function, maple understands that the variable is a placeholder for something else, and maple would not simplify the or to at the time the code is written. If you later happen to pass in a scalar for t, then it will do the simplification, but if you pass in an array of appropriate variety, then it would do the conjugation and matrix multiplication appropriate.
The MATLAB symbolic engine, MuPAD, does have the ability to not automatically simplify these kinds of operations at the time that a function is being defined. Unfortunately, the only way to define a function at the MuPAD level from within MATLAB is to use evalin(symengine) or feval(symengine) with the approriate magic code -- and if you try to display what you get back, then MATLAB will get confused and will reset the symbolic engine. Bleh.
There are work-arounds, but it is hacker level stuff. I guess I could probably program up something appropriate. but as an outside volunteer, I would be more likely to say that MATLAB is not currently a viable tool for this kind of processing, and that it would be more cost effective to use other tools. Heck, it would even be more cost effective to purchase Maplesoft's maple and install that, as it can install linkages into MATLAB so that you can call maple from MATLAB.

Connectez-vous pour commenter.

Plus de réponses (1)

Ahallya Jaladeep
Ahallya Jaladeep le 7 Mar 2022
A simpler solution is to define your symbols as matrices. This will remove commutativity.
Example:
syms Ip Im Ix Iy Iz Sx Sy Sz Sp Sm E a b c r r1 r2 [2 2] matrix
Now all the defined symbols will be considered as 2*2 matrices and a*b is not equal to b*a.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by