1
votes

I use Wolfram Mathematica. Let's set a natural number n >= 3. I want to define W[i] -- pure function. I do something like:

For[i = 3, i <= n, i++, W[[i]] = (1 - #^2)^2 JacobiP[i - 3, 2, 2, #] &; Print[W[[i]]]]

I get something like:

(1-#1^2)^2 JacobiP[i-3,2,2,#1]&
(1-#1^2)^2 JacobiP[i-3,2,2,#1]&
(1-#1^2)^2 JacobiP[i-3,2,2,#1]&
...

How to force Wolfram to understand that I want this concrete i from the definition of the loop? So I mean that I want smth like

(1-#1^2)^2 JacobiP[3-3,2,2,#1]&
(1-#1^2)^2 JacobiP[4-3,2,2,#1]&
...

Thank you! Andrew Bzikadze

1

1 Answers

0
votes

It's an interesting question. In the form of a pure function it resists substitution, but it can be done without changing your For loop structure using Hold.

n = 5;
For[i = 3, i <= n, i++,
 expr = (1 - #^2)^2 JacobiP[i - 3, 2, 2, #] &;
 frozen = Map[Hold, expr, 3];
 frozen[[1, 1, 2, 1, 1]] = i - 3;
 w[i] = frozen //. Hold[x_] :> x;
 Print[w[i]]]

Alternatively, without the part specification:

n = 5;
For[i = 3, i <= n, i++,
 expr = (1 - #^2)^2 JacobiP[i - 3, 2, 2, #] &;
 frozen = Map[Hold, expr, 3];
 pos = Position[frozen, Hold[i - 3]];
 frozen[[Sequence @@ Append[First[pos], 1]]] = i - 3;
 w[i] = frozen //. Hold[x_] :> x;
 Print[w[i]]]

w[4][3]

576

Another form is simpler but does not address your question.

f[i_] := (1 - #^2)^2 JacobiP[i - 3, 2, 2, #] &;

f[4][3]

576

Note even in this form the parameter is not evaluated but the substitution works well enough to use.

f[4]

(1 - #1^2)^2 JacobiP[4 - 3, 2, 2, #1] &

Also in this case:-

g[i_] := i + 1 &
g[2]

2 + 1 &

This is because Function has attribute HoldAll. See the details section here:

http://reference.wolfram.com/language/ref/Function.html