conditionals with more than two conditions #3324
Replies: 3 comments 7 replies
-
Cool project Here is an idea by indexing with an array of booleans and then extracting the first value of the resultant array. f_y = 1e5;
[1, 1.15, 1.3][[f_y <= 6e4, f_y < 1e5, f_y >= 1e5]][1] For readability maybe assign significant variable names: f_y = 1e5;
psi_values = [1, 1.15, 1.3];
psi_conditions = [f_y <= 6e4, f_y < 1e5, f_y >= 1e5];
psi_values[psi_conditions][1] Maybe it's more convoluted than your initial solution. |
Beta Was this translation helpful? Give feedback.
-
I agree that multi-way conditionals are common in mathematical notation. If you are only using mathjs as a convenient means of generating visual representations of formulas, I suggest you check out https://www.plurimath.org/. If you are also actually using the computational aspects of mathjs and would also like it to produce nice multi-way conditionals, then a code issue and PR will be necessary to the best of my knowledge. There are two possible ways I can think of off of the top of my head that such an enhancement could go: (a) Somehow recognize certain nested (b) Add an explicit multi-way 'cond' function à la Lisp, perhaps something like |
Beta Was this translation helpful? Give feedback.
-
Oops, I forgot, there is another alternative that would let you render nested conditionals as multi-line LaTeX alternatives with a single bracket right now, without any code changes. That is to pass a custom handler to the |
Beta Was this translation helpful? Give feedback.
-
Is there a way to create conditionals with more than two conditions that render into a single bracket?
for example, see the image below.
f_y <= 60000 ? 1.0 : f_y<100000 ? 1.15 : 1.3
I am nesting a conditional within another as a workaround, but what I really want to do is condense this to a single bracket, with three states:
I've been having fun using math.js on a hobby project with structural engineering calculators (example) where i use the solver and tex functions to do the heavy lifting. This functionality would help improve the readability of the output
Beta Was this translation helpful? Give feedback.
All reactions