# need help with finding right syntax for an IF statement

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.

-
You get what you need if you change Or (||) to And (&&) in the first argument of If? –  kguler Jan 13 '13 at 22:19

## 1 Answer

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.

-
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. –  m_goldberg Jan 13 '13 at 22:31
@m_goldberg yes that works. –  Mike Honeychurch Jan 13 '13 at 22:44
@m_goldberg after re-reading the request I've removed that code because it didn't cover "either"/"or" –  Mike Honeychurch Jan 13 '13 at 23:06