I am quite new to Mathematica and have an issue regarding which statement.

cut[x_, y_] := which[x>y, doFunctionX[], y>x, doFunctionY[]]

I wish to display only the true case. However, I am unable to find an operation to do it.

  • 2
    $\begingroup$ NOt really sure what you're trying to do with the last line, since X and Y are not numbers (and it should be cut[X, Y] anyway). But, if you change which to Which (all built-in functions in Mathematica are capitalized), your code should be fine. When you change that, cut[5, 3] evaluates to doFunctionX[]. $\endgroup$ – march Nov 1 '19 at 15:35
  • 1
    $\begingroup$ Here is a good post with common pitfalls for new users. I anchored it to your issue (Mathematica is case sensitive and the built-in system functions always start with a capital letter): mathematica.stackexchange.com/questions/18393/… $\endgroup$ – Arnoud Buzing Nov 1 '19 at 15:52
  • $\begingroup$ You may also want to consider handling the case when x == y. $\endgroup$ – Rohit Namjoshi Nov 1 '19 at 17:54
  • $\begingroup$ Thank you. The issue was due to not capitalizing the first letter. $\endgroup$ – Siddeshwar Raghavan Nov 2 '19 at 16:36

In Mathematica you can do this with function name overloading. Like so:

cut[x_, y_] /; x > y := doFunctionX[]
cut[x_, y_] /; x < y := doFunctionY[]

where the Condition (/;) operator enforces the conditions you want to place on the arguments.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.