Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

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 MathematicaAlternatives to procedural loops and iterating over lists in Mathematica.

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.

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.

Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

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: