2
$\begingroup$

I have defined a function:

Y[k_, q_, {r_, θ_, ϕ_}] := SphericalHarmonicY[k, q, θ, ϕ]/r^(k + 1)

I have a list of coordinates {r, θ, ϕ}. I would like to keep k and q fixed, and evaluate the function over the list, but I'm not quite sure how to accomplish this.

$\endgroup$
3
  • $\begingroup$ k=some value1; q=some value2; Table[Y[k, q, {r, \[Theta], \[Phi]}], {r, values}, {\[Theta], values}, {\[Phi], values}] $\endgroup$
    – corey979
    Commented Dec 7, 2016 at 23:30
  • $\begingroup$ Y[kval,qval,Sequence@@#]&/@listOfTriples, or if you really are inputting the triples as a list, Y[kval,qval,#]&/@listOfTriples. $\endgroup$
    – Alan
    Commented Dec 7, 2016 at 23:46
  • $\begingroup$ Closely related, perhaps duplicate: mathematica.stackexchange.com/q/9861/121 $\endgroup$
    – Mr.Wizard
    Commented Dec 24, 2016 at 1:16

3 Answers 3

0
$\begingroup$
lst =  {{r1, r2, r3}, {θ1, θ2, θ3}, {ϕ1, ϕ2, ϕ3}}

Thread[Y[k, q, lst]]

{r1^(-1 - k) SphericalHarmonicY[k, q, θ1, ϕ1],
r2^(-1 - k) SphericalHarmonicY[k, q, θ2, ϕ2],
r3^(-1 - k) SphericalHarmonicY[k, q, θ3, ϕ3]}

$\endgroup$
1
  • $\begingroup$ FYI: I reversed my vote on this answer as I find it misleading. Please see my rebutting answer. Sorry for the downvote. $\endgroup$
    – Mr.Wizard
    Commented Dec 24, 2016 at 19:03
1
$\begingroup$

Say that your input list looks like:

inputList = {{r1, θ1, ϕ1}, {r2, θ2, ϕ2}}

(* {{r1, θ1, ϕ1}, {r2, θ2, ϕ2}} *)

Then Table is a very easy way to produce the desired result

Table[Y[k, q, {inputs}], {inputs, inputList}]

(* {Y[k, q, {{r1, θ1, ϕ1}}], Y[k, q, {{r2, θ2, ϕ2}}]} *)
$\endgroup$
0
$\begingroup$

The right-hand-side of your function is already composed of Listable functions, therefore your code will work without any modification. Please reference Case #5 in Alternatives to procedural loops and iterating over lists in Mathematica.

ClearAll[Y] (* no prior definitions *)

Y[k_, q_, {r_, θ_, ϕ_}] := SphericalHarmonicY[k, q, θ, ϕ]/r^(k + 1)

lst = {{r1, r2, r3}, {θ1, θ2, θ3}, {ϕ1, ϕ2, ϕ3}};

Y[k, q, lst]
{r1^(-1 - k) SphericalHarmonicY[k, q, θ1, ϕ1], 
 r2^(-1 - k) SphericalHarmonicY[k, q, θ2, ϕ2], 
 r3^(-1 - k) SphericalHarmonicY[k, q, θ3, ϕ3]}

If the right-hand-side were not composed of Listable functions then the presently Accepted answer would not work anyway. Observe:

Y2[k_, q_, {r_, θ_, ϕ_}] := bar[q, ϕ, foo[k, r, θ]]

Thread[Y2[k, q, lst]]
{bar[q, ϕ1, foo[k, {r1, r2, r3}, {θ1, θ2, θ3}]], 
 bar[q, ϕ2, foo[k, {r1, r2, r3}, {θ1, θ2, θ3}]], 
 bar[q, ϕ3, foo[k, {r1, r2, r3}, {θ1, θ2, θ3}]]}

Note that only bar was Threaded over its arguments; foo remains undistributed.

To solve that case I prefer manual threading through a second definition using Unevaluated to prevent premature evaluation:

Y2[k_, q_, sm : {_List, _List, _List}] := Thread @ Unevaluated @ Y2[k, q, sm]

Now:

Y2[k, q, lst]
{bar[q, r3, foo[k, r1, r2]],
 bar[q, θ3, foo[k, θ1, θ2]], 
 bar[q, ϕ3, foo[k, ϕ1, ϕ2]]}

Note that you cannot do this externally (without the definition above) because lst is atomic within the unevaluated expression, therefore there is nothing to thread over:

ClearAll[Y2]

Y2[k_, q_, {r_, θ_, ϕ_}] := bar[q, ϕ, foo[k, r, θ]]

Thread @ Unevaluated @ Y2[k, q, lst]
bar[q, {ϕ1, ϕ2, ϕ3}, foo[k, {r1, r2, r3}, {θ1, θ2, θ3}]]

To make that work you would need to insert the evaluated form of lst into the expression, e.g.:

With[{lst = lst},
  Thread @ Unevaluated @ Y2[k, q, lst]
]
{bar[q, r3, foo[k, r1, r2]],
 bar[q, θ3, foo[k, θ1, θ2]], 
 bar[q, ϕ3, foo[k, ϕ1, ϕ2]]}

Reference:

$\endgroup$

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.