Skip to main content
added 10 characters in body
Source Link
VP06
  • 35
  • 3

I have a very large function f[x_,y_,z_] and it's too cumbersome to type it directly. So I wish to break the definition and use (/.) multiple times, as follows (indicatively):

f[x_, y_,z_] := (int1 + int2 + int3)/.rule1/.rule2;
rule1 = {int1 -> iint1, int2 -> iint2, int3 -> iint3};
rule2 = {iint1 -> 1*x, iint2 -> 2*y, iint3 -> 3*z};

But this does not work. When I try to evaluate f[1,2,3], it does not perform the transformation rule2 and gives me f[1,2,3]=x + 2y + 3z instead of the evaluated number value 1*1 + 2*2 + 3*3 = 14.

I think this is because rule2's definition on line 3 does not come into the scope of my function declaration on line 1, andso the x I write in rule2 is not the same x_ I'm trying to relate to.

How do I solve this? Thank you.

I have a very large function f[x_,y_,z_] and it's too cumbersome to type it directly. So I wish to break the definition and use (/.) multiple times, as follows (indicatively):

f[x_, y_,z_] := (int1 + int2 + int3)/.rule1/.rule2;
rule1 = {int1 -> iint1, int2 -> iint2, int3 -> iint3};
rule2 = {iint1 -> 1*x, iint2 -> 2*y, iint3 -> 3*z};

But this does not work. When I try to evaluate f[1,2,3], it does not perform the transformation rule2 and gives me f[1,2,3]=x + 2y + 3z instead of the evaluated number value 1*1 + 2*2 + 3*3 = 14.

I think this is because rule2's definition on line 3 does not come into the scope of my function declaration, and the x I write in rule2 is not the same x_ I'm trying to relate to.

How do I solve this? Thank you.

I have a very large function f[x_,y_,z_] and it's too cumbersome to type it directly. So I wish to break the definition and use (/.) multiple times, as follows (indicatively):

f[x_, y_,z_] := (int1 + int2 + int3)/.rule1/.rule2;
rule1 = {int1 -> iint1, int2 -> iint2, int3 -> iint3};
rule2 = {iint1 -> 1*x, iint2 -> 2*y, iint3 -> 3*z};

But this does not work. When I try to evaluate f[1,2,3], it does not perform the transformation rule2 and gives me f[1,2,3]=x + 2y + 3z instead of the evaluated number value 1*1 + 2*2 + 3*3 = 14.

I think this is because rule2's definition on line 3 does not come into the scope of my function declaration on line 1, so the x I write in rule2 is not the same x_ I'm trying to relate to.

How do I solve this? Thank you.

Source Link
VP06
  • 35
  • 3

Replace function arguments multiple times?

I have a very large function f[x_,y_,z_] and it's too cumbersome to type it directly. So I wish to break the definition and use (/.) multiple times, as follows (indicatively):

f[x_, y_,z_] := (int1 + int2 + int3)/.rule1/.rule2;
rule1 = {int1 -> iint1, int2 -> iint2, int3 -> iint3};
rule2 = {iint1 -> 1*x, iint2 -> 2*y, iint3 -> 3*z};

But this does not work. When I try to evaluate f[1,2,3], it does not perform the transformation rule2 and gives me f[1,2,3]=x + 2y + 3z instead of the evaluated number value 1*1 + 2*2 + 3*3 = 14.

I think this is because rule2's definition on line 3 does not come into the scope of my function declaration, and the x I write in rule2 is not the same x_ I'm trying to relate to.

How do I solve this? Thank you.