Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I got Mathematica code from my friend:

n = 100
l = 0
gw = GaussianQuadratureWeights[n, 0, 2]
(*the result of gw maybe like this {{0.000286273, 0.000734634}, {0.00150805, 0.00170939}, {0.00370487, 0.00268393},...}}*)
J = Array[SphericalBesselJ[l, gw[[#, 1]]] &, n]

The first, I'm not sure with syntax because i'm not a Mathematica user.

But in my understand this code just try to create Spherical Bessel array by using 2 parameters such as

l just a constant. its value is 1 

qw this is two dimensional array 

On Mathematica code up there for gw[[#, 1]] i think they just tried to call {{0.000286273, 0.00150805, 0.00370487, ...}} and put them one by one to second parameters of SphericalBesselJ . And finally, we will get a one dimensional J array

If write it as pseudo code from my understanding it might be like this:

l = 1
n = 100
gw = gaussiannode(100) //generate 100 gaussian nodes
for (i==1, i<=n, i++):
    J[i] = SphericalBesselJ(l, gw[1][i])

My question is "Is it the Mathematica code up there equivalent to my pseudo code?"

My point is just want to know Array can work as Loop or not? if not please correct Mathematica code up there to the right way by using loop.

share|improve this question

closed as off-topic by Yves Klett, m_goldberg, Szabolcs, RunnyKine, Oleksandr R. Jun 25 at 16:29

  • The question does not concern the technical computing software Mathematica by Wolfram Research. Please see the help center to find out about the topics that can be asked here.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
J=SphericalBesselJ[l, gw[[;; NN, 1]]]... Also, try not to use uppercase letters/initials for symbols, you might clash with built-ins. –  rasher Jun 25 at 10:08
    
Thank you @rasher. but i know nothing about mathematica you can skip all syntax but focus on the procedure. I just try to check my friend code that he was used Array as Loop. –  terces907 Jun 25 at 10:14
    
Welcome! This question is very localized and will not be useful for future visitors in its current form. I hope you get advice in any case, but otherwise I am voting to close. PS: reference.wolfram.com/wolfram-language/ref/Array.html –  Yves Klett Jun 25 at 10:17
    
Your pseudocode seems to do the same as your friend's code (this depends on how your pseudocode addresses cells in a two dimensional table; if your 1 indicates a column it's ok). Rasher's comment contains a more succinct way of doing the same. –  Sjoerd C. de Vries Jun 25 at 10:43
3  
This question appears to be off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. –  m_goldberg Jun 25 at 12:28

2 Answers 2

The answer is simple enough: yes, Array can function as a "loop" operator (as can many other Mathematica functions like Outer, Table, Do, Map, MapThread, etc). Consider the simplest situation (found in the help file for Array).

Array[f, 5]

{f[1], f[2], f[3], f[4], f[5]}

This creates an output that is the same as if you had created a loop and indexed through 5 entries.

share|improve this answer
Needs["NumericalDifferentialEquationAnalysis`"]

n = 100;
m = 0;
(* used m rather than letter l to avoid confusion with number 1 *)

gw = GaussianQuadratureWeights[n, 0, 2];

j = Array[SphericalBesselJ[m, gw[[#, 1]]] &, n];

(* avoid using capital letters for first or only letter in name for \
user-defined variable/fuunction to avoid potential naming conflicts with \
built-in Mathematica names *)

Some alternate ways of generating j

j == SphericalBesselJ[m, gw[[All, 1]]]

True

(* This works since SphericalBesselJ is Listable *)

Attributes[SphericalBesselJ]

{Listable, NumericFunction, Protected, ReadProtected}

j == (SphericalBesselJ[m, gw[[#, 1]]] & /@ Range[n])

True

j == Table[SphericalBesselJ[m, gw[[k, 1]]], {k, n}]

True

For[i = 1; a = {} (*initialize as an empty list *),
 i <= n, i++,
 AppendTo[a, SphericalBesselJ[m, gw[[i, 1]]]]]; j == a

True

(* Note that j[[i]] is used refer to refer to a part of a list rather than \
j[i] which would be a function call *)
share|improve this answer

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