Hi guys. I hope i do this correctly...
So I want to have this:
f(p) := proc( p )
if p::even then
print(((((p)!)/((2^(p/2))*(p/2)!))^2))
else
print(0)
end if
end proc:
as an initial condition, b[p,0], for an array, b := array(0..500, 0..500).
It doesn't work if I just do a for loop. At least I don't think it works; say I do
for p from 0 to 500 do
b[p,0] := proc( p )
if p::even then
print(((((p)!)/((2^(p/2))*(p/2)!))^2))
else
print(0)
end if
end proc
od:
Then when I try
b[4,0];
it just returns b[4,0], and not 9.
Any pointers would be very much appreciated.
Thanks,
Tom
suggestion
Your loop is assigning a procedure, not a value. Try
Here is one way to
PS: just noted that Joe Riel was faster ...
Thanks so much guys! That
Thanks so much guys! That was exactly what I was after, and I understand why my way didn't/couldn't work. TY again.
initializer
Note that you can pass an initializer procedure to Array, it is called with the indices and the value it returns is used for the initial value at that location. So you could do
In this case, since only 1/1000 of the Array is initialized to a nonzero value, it is faster to explicitly initialize the first column using an external do-loop.
OK. So this did work
OK. So this did work perfectly last week and now when I come back to it it doesn't.
a := array(0..501, 0..501):
for p from 0 to 500 by 2 do
a[p,0]:=((p!)/((2^(p/2))*((p/2)!)))^2
od:
Then a[3,0]; just returns a[3,0], and not 0.
Any wisdom?
Array
Use Array, not array. The latter is an older data-structure (related to tables). The reason that the new one works is that any unassigned elements are automatically zeroed.
Also it's only the values
Also it's only the values for odd p (which are all 0) that it doesn't work for. It works fine for even p.