5
$\begingroup$

I would like to replace the expresion $(n1-1)$ to $z1$ in following:

2*n1*p[n1-1,n2]

2*(z1+1)*p[z1,n2]

I have tried Replace, ReplaceAll, /., //. but nothing seems to be working. Only part inside $p[n1-1, n2]$ is replaced. Can anyone help me with this?

$\endgroup$
1
  • 1
    $\begingroup$ (* stuff *) /. n1 -> z1 +1 doesn't do what you want? $\endgroup$ Commented Apr 8, 2013 at 12:58

2 Answers 2

6
$\begingroup$

If you type:

2 n1 p[n1-1, n2] /. {n1 -> z1 +1}

then the answer is:

2 (1 + z1) p[z1, n2]

This is shorthand for the function:

ReplaceAll[2 n1 p[n1 - 1, n2], n1 -> z1+1]

which explicitly uses the rule (in the second argument) to change the first argumemnt.

$\endgroup$
5
  • $\begingroup$ Ah strange asterixes appear in your answer. I figured I'd (propose to) edit them, but you made an edit as well in the meantime. Oh well, please fix the asterixes :). $\endgroup$ Commented Apr 8, 2013 at 13:24
  • $\begingroup$ thanks Jacob -- I don't know where they came from... $\endgroup$
    – bill s
    Commented Apr 8, 2013 at 13:27
  • $\begingroup$ No problem, I think they came from the original problem statement, that has now been fixed by Istvan Zachar :). Hm.. I think Maria was trying to also replace the n1 that appears outside the function p, so I think this is probably not what she wants.. $\endgroup$ Commented Apr 8, 2013 at 13:30
  • $\begingroup$ I just realized that and changed it. But now I realize this is exactly what JM suggested... sorry JM.. I'm happy to delete and give to you... $\endgroup$
    – bill s
    Commented Apr 8, 2013 at 13:33
  • $\begingroup$ Yesterday I posted the silliest answer only to delete it a minute later, I can relate :P $\endgroup$ Commented Apr 8, 2013 at 13:37
4
$\begingroup$

If $n1$ is set initially, when any function that contains this variable is entered, the conversion will be automatic.

n1 = z1 + 1; 
2*n1*p[n1 - 1, n2]
$\endgroup$

Your Answer

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

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