6
$\begingroup$

I would like to exclude the point {x=0,y=0} in the function definition

f = Function[{x, y}, {x/(x^2 + y^2), -(y/(x^2 + y^2))}] 

So far I tried ConditionalExpressionand /; without success.

Thanks!

$\endgroup$
5
  • $\begingroup$ What should happen for f[0,0]? $\endgroup$ Commented Aug 8, 2019 at 11:14
  • $\begingroup$ Function shouldn't be applied to ` {0,0}` or should return Null! $\endgroup$ Commented Aug 8, 2019 at 11:16
  • 1
    $\begingroup$ Assuming it needs to be a pure function for some reason, I guess your only real option is to use If or similar to do the check. As far as I am aware, Function will always evaluate when it is applied to something. Depending on the amount of conditions, you could use e.g. Replace[{##},{{0,0}->Null,{x_,y_}:>{x^2,y^2}}]& or Replace[{##},{{x_,y_}/;x!=0||y!=0:>{x^2,y^2},_->Null}]& to have a syntax more similar to traditional downvalue definitions $\endgroup$ Commented Aug 8, 2019 at 11:24
  • $\begingroup$ @LukasLang Thank you for your assistance. My purpose is to use the pure function inside TransformedRegion. A possible workaround could be the definition of a region thereby excluding the point {0,0} . $\endgroup$ Commented Aug 8, 2019 at 11:27
  • $\begingroup$ If you're using it in TransformedRegion, I don't recommend using the return value Null. It's probably not the correct return for "this thing doesn't exists". Undefined is probably better. Null is a programmatic value, not a mathematical one. $\endgroup$ Commented Aug 8, 2019 at 11:31

2 Answers 2

7
$\begingroup$

As far as I know, you cannot have a pure function that holds its arguments depending on a condition on the arguments. Condition specifically works with pattern matching and pure functions don't use the pattern matcher. So the behaviour that f[0, 0] doesn't evaluate simply cannot be achieved with a pure function.

The alternative is some sort of default return value. Probably the easiest way to do this, is with something like If, Switch or Which:

f = Function[{x, y}, If[ TrueQ[x == 0 || y == 0], Undefined, {x/(x^2 + y^2), -(y/(x^2 + y^2))} ] ]; f[0, 0] 

Undefined

Note the use of TrueQ, which is necessary to deal with the case f[a, b] where a and b are symbols. The If will not evaluate otherwise, since a == 0 and b == 0 remain unevaluated (because they are equation, not truth statements). The test x === 0 is not useful here either, since 0. === 0 evaluates to False. PossibleZeroQ[x] is another test you could use in the If statement.

$\endgroup$
1
  • $\begingroup$ Thank you for your comprehensive answer. I didn't know Undefined $\endgroup$ Commented Aug 8, 2019 at 12:15
3
$\begingroup$

Here's a fun variant on Sjoerd's approach using #0 as a recursive reference

f = Function[ With[{x = #1, y = #2}, If[TrueQ[x == 0 || y == 0], Unevaluated @@ Hold[#0[##]], {x/(x^2 + y^2), -(y/(x^2 + y^2))} ] ] ]; 

This gives

f[0, 0] Unevaluated[ (With[{x = #1, y = #2}, If[TrueQ[x == 0 || y == 0], Unevaluated @@ Hold[#0[##1]], {x/(x^2 + y^2), -(y/(x^2 + y^2))}]] &)[0, 0] ] 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.