Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
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 1

up vote 2 down vote accepted

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.

share|improve this answer
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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