how to add all parentheses in symbolic logic expression
3 次查看(过去 30 天)
显示 更早的评论
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,
1 个评论
Dyuman Joshi
2023-1-27
If I am understanding this correct, you want to modify a symbolic expression?
回答(1 个)
Piyush
2023-2-22
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!