How to rewrite my expression using two abbreviations?

I tried Rewrite Expression Using Abbreviations example in https://au.mathworks.com/help/symbolic/subexpr.html for subexpr() and I can't reproduce it - the code renders a new expression with sigma, but w/o sigma1 and sigma2.
This is a link to the answer to my another question that uses two abbreviations (sigma1 and sigma2):
How do I get this result in MATLAB?

2 Comments

The question is not clear, can you tell what do you mean by sigma1 and sigma2
Both refs above have greek sigmas rendered as in https://en.m.wikipedia.org/wiki/Greek_letters_used_in_mathematics,_science,_and_engineering

Sign in to comment.

 Accepted Answer

You get the pretty output only with Live Script

11 Comments

You don't mean that I can not have an abbreviated result, just a pretty one, do you?
I mean that function displayVariable near line 81 of toolbox/symbolic/symbolic/@sym/display.m tests whether Live Script is being used, and if so then it invokes createMathML to create a MathML representation of the expression, and then calls matlab.internal.language.signalVariableDisplay to render the MathML .
The createMathML function runs code to detect useful sub-expressions and substitute generated variable names in place of sub-expressions, and adds nicely-formatted display of the generated variables after the main display.
The code to detect the sub-expressions and display them is not invoked if you are not using Live Script.
If you are not using Live Script, then you can use pretty(), which also does sub-expression detection and output, using a different form, using # followed by a number to indicate a replacement instead of using a named variable such as . pretty() is useful sometimes, but the output can be kind of ugly.
So, to emphasize, that subexpr() example that you are looking at: they ran that example using Live Script, and the output with the was generated by the functions that are called for Live Script. The is not explicitly generated by the code you are executing in that example, which is why you have not been able to replicate the output.
Thanks, Walter. I can't help but condemn Rewrite Expression Using Abbreviations example ;-). So-o-o confusing. Makes an impression that subexpr() detects useful subexpressions and substitutes them into the original expression.
My intention was to find out if MATLAB (w/o Live Script) can detect & substitute. The rewritten expressions would've been useful for very large number repeatative evaluations (guilty of premature code optimization).
You can get further by making additional calls, such as
syms a b c d x
solutions = solve(a*x^3 + b*x^2 + c*x + d == 0, x, 'MaxDegree', 3);
[r, sigma] = subexpr(solutions, 'sigma')
[r2, sigma2] = subexpr(r, 'sigma2')
I tried it before raising this question. It is better then nothing but it does not find common subexprs in r and sigma. This does not help either:
[r2, sigma2] = subexpr([r, sigma], 'sigma2')
This is syntactically erroneous
[[r2, sigma], sigma2] = subexpr([r, sigma], 'sigma2')
Thanks.
syms a b c d x
solutions = solve(a*x^3 + b*x^2 + c*x + d == 0, x, 'MaxDegree', 3);
[r, sigma] = subexpr(solutions, 'sigma')
[r2, sigma2] = subexpr([r(:); sigma(:)], 'sigma2')
but then you have to split apart r2. The first three rows will be in terms of sigma and sigma2 and the last row will not have any sigma . The last row will be the definition of sigma (used in the first three rows) with common factors relative to r replaced with 'sigma2'
Nice. I have no idea why it works, though ...
It makes sense to me ;-)
You do a change of variable of subexpressions into sigma. Then you look for common subexpressions between the changed expressions and the definition of sigma.
That's the role of []. Unlike myself you have added (:). How does it make a difference?
That was mostly for consistency. The important part is that I used semi-colon. It could have been coded [r;sigma] but not [r,sigma] because r is a column vector with more than one entry and adding a column with only one entry to that does not work.
A loosely-typed language like that may become challenging for a casual user like myself. I would say that the prompt help available from MATLAB gurus is a crucial piece of MATLAB usefulness. Thanks a lot.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!