how to add all parentheses in symbolic logic expression

10 views (last 30 days)
Hello,
I am working on matlab 2020b and I have a relatively long logic phrase, an example would be:
a & b & c & (d & e & f | g & h & j) & (k | p)
The computation is done first on the & and later on the |, if I understood correctly.
This logic phrase is obtained after running :
simplify(evalin(symengine, someLogicPhrase))
My issue is that this phrase is used by some users and I would like to add parentheses when the logic phrases between | are long. I would change the previous one to something like this:
a & b & c & ( (d & e & f) | (g & h & j)) & (k | p)
Where there are new parentheses that will make it more user-friendly in my case.
Is there any easy way to do it using the symbolic toolbox? or any other.
Thank you in advance,

Answers (1)

Piyush
Piyush on 22 Feb 2023
When you have long logic phrase, you may use newlines to improve readability. Place operators at the end of a line, rather than at the beginning of a line.
You can use extra newlines when it helps with readability, e.g.
if (c > 30 && // is cost per unit must be high?
d > 40) { // and distance travelled high?
// code
}
Generally, Use of parentheses is an indicator that standard operator precedence rules are not in use, for example, "a = b * (c + d)" indicates to the reader that standard operator precedence is not in use.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!