MATLAB Answers

Preventing matlab from reversing my signs in symbolic expressions

10 views (last 30 days)
The symbolic toolbox reverses the signs of the expressions I type. For example
syms a b
f = (1-x)*a + b
b - a*(x-1)
Is there some option I can set somewhere to prevent this sign reversal, and have the above return
b + a*(1-x)
I know it's a small thing, but when x is always less than 1, it's annoying.


Sign in to comment.

Accepted Answer

Pranav Verma
Pranav Verma on 17 Sep 2020


Leo Simon
Leo Simon on 17 Sep 2020
Thanks Pranav, the referenced thread doesn't answer the question, it just acknowledges that the issue remains a big matlab mystery
Pranav Verma
Pranav Verma on 17 Sep 2020
I agree with you Leo that thread does not provide the exact resolution to the issue, but as Walter has indicated in his answer, the symbolic toolbox indeed has some preference rules for commutative expressions and it converts the expression according to those rules albeit that doesn't change the final result that is expected.
Walter Roberson
Walter Roberson on 17 Sep 2020
The behavior I described there is what the symbolic engine does, and there is no provision for changing it.
You could construct expressions with more specific forms by using feval(symengine, 'hold', SUBEXPRESSION) on parts of the expression and joining the resulting subexpressions together with appropriate MuPAD operators. But that will not do you much good, as MATLAB will reset the symbolic engine if it notices hold() operators in the expressions (that is, it does not expect them to show up and it resets itself when unknown things show up.) If you char() a symbolic expression that includes a hold() then you will get back a character vector that includes internal symbolic references that the user cannot make much sense of. MuPAD does take hold() into account in computation.... the problem is that it isn't Computation you want, it is Presentation.
I am not saying that it cannot be changed... but changing it would require digging hard into the MuPAD code that drives the symbolic engine, and providing an alterated version of that code to the symbolic engine. You would have to do all the development work in R2019b or earlier, as the MuPAD development tools got striped out in R2020a.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by