3
$\begingroup$
f[equation_] := Module[{},
  ContourPlot[equation[x, y] == 1, {x, -2, 2}, {y, -2, 2}]
  ]
equation = (#1^2 + #2^2) &;
f[equation]

The above code draws a circle. But I want to translate this equation by (1,1) via

old=(#1^2 + #2^2) &
a=#1+1;b=#2+1;
new=old[a,b]

Then f[new] can't draw any more. a and b can generally be functions of #1 and #2. How to resolve this?

$\endgroup$

1 Answer 1

7
$\begingroup$

"old" does not return a function as you can see (& is missing):

old[a, b]

(1 + #1)^2 + (1 + #2)^2

This is only an expression. To change "new" into a function, you need the operator "&" (Function). However, as this operator has the attribut "HoldAll" you need "Evaluate" to evaluate "old[a,b]" like:

old = (#1^2 + #2^2) &
a = #1 + 1; b = #2 + 1;
new = Evaluate[old[a, b]] &

Now f[new] will draw:

f[new]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Personally, I wouldn't recommend messing around with Evaluate in pure functions unless it's really necessary. new = old[#1 + 1, #2 + 1] & would be a more idiomatic solution to this problem IMO. $\endgroup$ Mar 13, 2023 at 11:13

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.