1
$\begingroup$

I have the function

f[x_,y_]:=1/(Sin[x]+Sin[y])

which obviously becomes infinite for $x=\pi n, \;y=\pi n$ where $n\,\epsilon \, \mathbf{N}$. I want to give f the value 0 for this case, i.e f[x,y]=0

I am trying

While[Mod[x, π] == 0 && Mod[y, π] == 0, f[x, y] == 0]

but it doesn't work.

I also tried

f[x_ /; Mod[x, π] == 0, y_ /; Mod[y, π]] := 0

didn't work either

How can I do it?

$\endgroup$
3
  • $\begingroup$ For $y=-x$ your function is also ComplexInfinity. Do you want to similarly treat this case? $\endgroup$
    – yarchik
    Commented Jan 4, 2021 at 20:47
  • $\begingroup$ @yarchik I didn't need that for what I had in mind, but now that you mention it why not? Please post your answer $\endgroup$
    – geom
    Commented Jan 4, 2021 at 22:58
  • 1
    $\begingroup$ Your solution below is good. In the case you want to make sure that there is no overflow you can do something like f[x_, y_] := If[Sin[x] + Sin[y] == 0, 0, 1/(Sin[x] + Sin[y])] $\endgroup$
    – yarchik
    Commented Jan 5, 2021 at 10:18

2 Answers 2

2
$\begingroup$
f[x_, y_] := 0 /; Mod[x, π] == 0 && Mod[y, π] == 0

does the trick. Just figured it.

Any comments are most welcome.

$\endgroup$
1
$\begingroup$

You can use a Piecewise definition as well:

Clear[f]
f[x_, y_] := 
   Piecewise[
     {{1/(Sin[x] + Sin[y]), Mod[x, Pi] != 0 || Mod[y, Pi] != 0}}, 
     0
   ]

f[2, 3.]      (* 0.952002 *)
f[Pi, 3 Pi]   (* 0        *)
f[2, 10 Pi]   (* Csc[2]   *)
f[Pi, 2 Pi]   (* 0        *)

Here I made the default value $0$ explicit for future code readability, but technically Piecewise already defaults to $0$ if none of the conditions are met, so you could omit it.

$\endgroup$
0

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.