0
$\begingroup$

Very new to Mathematica so this might be really simple.

I have the function

pow[x_,r_]:= x^r

where r is rational due to

rationalQ[r_] := IntegerQ[Numerator[r]] && IntegerQ[Denominator[r]]

I'm trying to use conditions so that when I test pow[0,0] where x=0 and r=0, the output uses the string "undefined" instead of Indeterminate.

Any idea how it works?

$\endgroup$
2
  • $\begingroup$ you can simply do pow[0,0]="undefined" $\endgroup$ – george2079 May 10 '18 at 18:27
  • $\begingroup$ thanks for the help! so I have a second case where I'm still using the same function and r is still rational, except this time I have to set up a condition where I make it so that when x is any number except 0 and r=0, the output should be 1. How would that work? $\endgroup$ – user58133 May 11 '18 at 23:32
1
$\begingroup$

You want to do something like this,

pow[x_,r_?rationalQ]:= x^r
pow[___] := "Undefined"

These function definitions are called DownValues in Mahtematica, and they are applied in order of increasing generality.

So what is going on here, if you evaluate pow with two variables, and the second variable gives True when fed to rationalQ, then that definition is used. Otherwise the evaluator will continue to look for other matching patterns. The pow[___] is the most general pattern, and so anything that doesn't match the first pattern will match it.

For more information, see https://mathematica.stackexchange.com/a/1852/9490.

$\endgroup$
1
  • $\begingroup$ thanks so much, this is really helpful. I already posted a comment on my second case but I figured I'd comment it here too so you get the notification? I have a second case where I'm still using the same function and r is still rational, except this time I have to set up a condition where I make it so that when x is any number except 0 and r=0, the output should be 1. How am I supposed to set up the condition? $\endgroup$ – user58133 May 12 '18 at 0:39

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.