1
$\begingroup$

I am working with two multi-variable functions A and B. They depend on some counting factors n and n primed (integers, larger than, or equal to zero) and additional real variables x, y, z. My functions are actually complicated, but for simplicity assume, for example,

 A[nn_, np_, x_, y_, z_] := (1/nn + 1/np) (2 x + 2 y + 2 z);

 B[nn_, np_, x_, y_,z_] := (1/Factorial[nn] + 1/Factorial[np]) (3 x + 3 y + 3 z);

I would like to use an If statement to assign either A or B to become the definition of a third function V. In the example above this is needed to prevent division by zero. I tried different variations of

 V[nn_, np_, x_, y_, z_] := 
  If[nn > 0 || np > 0, A[nn, np, x, y, z], B[nn, np, x, y, z]];

but I cannot figure out what the correct syntax is. What I would like to accomplish is to use a statement that assigns the expression given by function A to function V when n or n prime are NOT zero and assigns function B to function V when either n or n primed are zero.

$\endgroup$
1
  • 2
    $\begingroup$ You get what you need if you change Or (||) to And (&&) in the first argument of If? $\endgroup$
    – kglr
    Jan 13, 2013 at 22:19

1 Answer 1

2
$\begingroup$

What I would like to accomplish is to use a statement that assigns the expression given by function A to function V when n or n prime are NOT zero and assigns function B to function V when either n or n primed are zero.

After re-reading your request you are specifically asking for Or (since you use "or" and "either" in your request). Whether that is actually your intention is not for me to assume, I'll let you clarify if required. This can be done with:

V[nn_, np_, x_, y_, z_] := Which[
   nn != 0 || np != 0, "answer 1",
   nn == 0 || np == 0, "answer 2"];

So what happens if one of nn or np is greater than zero and the other is zero? Under this definition the first case of True is returned.

@Artes has noted in a comment below that your right hand side requires these parameters to be non negative. I'd say it is almost certain that you are going to clarify your intentions but until then the above seems to answer your question.

$\endgroup$
3
  • 1
    $\begingroup$ Why not just V[0, 0, x_, y_, z_] := B[0, 0, x, y, z] V[nn_, np_, x_, y_, z_] := A[nn, np, x, y, z] in that order? I think Mathematica will apply the two definitions to the correct cases. $\endgroup$
    – m_goldberg
    Jan 13, 2013 at 22:31
  • $\begingroup$ @m_goldberg yes that works. $\endgroup$ Jan 13, 2013 at 22:44
  • $\begingroup$ @m_goldberg after re-reading the request I've removed that code because it didn't cover "either"/"or" $\endgroup$ Jan 13, 2013 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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