1
$\begingroup$

I am writing a function with a pair of values {x,y}, I have issues when I apply this function to list such as{{x1,y1},{x2,y1},{x1,y2},{x2,y2}}.

I have tried with:

f[{x_, y_}] := If[Sqrt[x^2 + y^2] < 1., ArcTan[x, y], 0.];

And it only works with f[x,y].

I've also tried to use the pure function with mapping

Map[If[Sqrt[#1^2 + #2^2] < 1., ArcTan[#1, #2], 0.] &, {{0.1, 0.2}}]

Both are not working as expected. I am so confused about the syntax with other programming languages such as Java, C, etc.

What I want actually to pass a single parameter m, and pair of values {x,y} at the same time:

expfun[m_, {x_, y_}] := 
If[Sqrt[x^2 + y^2] > 1., 0., 
Cos[m ArcTan[x, y]] + I Sin[m ArcTan[x, y]];
SetAttributes[expfun, Listable];

It's not working with e.g:

expfun[2, {{0.1, 0.2}, {0.2, 0.1}}]

$\endgroup$
  • $\begingroup$ is this what you want? expfun[x_, y_] := Block[{m = 2}, If[Sqrt[x^2 + y^2] > 1., 0., Cos[m ArcTan[x, y]] + I Sin[m ArcTan[x, y]]]]; expfun @@@ {{0.1, 0.2}, {0.2, 0.1}} $\endgroup$ – user49047 Oct 1 at 14:20
  • 1
    $\begingroup$ It should work, f[{x_, y_}] := x + y, f[{1, 2}] -> 3. What do you mean by "it only works with f[x,y]"? $\endgroup$ – Rohit Namjoshi Oct 1 at 14:24
  • $\begingroup$ Perhaps you want expfun[2,#]&/@{{0.1,0.2},{0.2,0.1}}? $\endgroup$ – Somos Oct 2 at 11:59
1
$\begingroup$

Try this:

f[m_Real, lst_List] := 
 Map[If[Sqrt[#[[1]]^2 + #[[2]]^2] > 1., Table[0, Length[lst]], 
    Cos[m ArcTan[#[[1]], #[[2]]]] + 
     I Sin[m ArcTan[#[[1]], #[[2]]]]] &, lst]

Let us check it:

f[2., {{0.1, 0.2}, {0.2, 0.1}}]

(* {-0.6 + 0.8 I, 0.6 + 0.8 I}  *)

and

f[2., {{1, 2}, {2, 1}}]
(* {{0, 0}, {0, 0}}  *)

Have fun!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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