defining an initial condition for an array

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

b := Array(0..500, 0..500):
for i from 0 to 500 by 2 do
   b[i,0] := i!/...;
end do:
Axel Vogt's picture

Here is one way to

Here is one way to demonstrate the handling only:

  b := Array(0..500, 0..500);

  f := proc( p )
  if type(p,even) then
    return (p!/(p/2)!)^2/2^p:
  else
  #  print(0)
  end if
  end proc;

  for p from 0 to 500 do
    b[p,0] := f(p)
  end do;
  p:='p': # clean up the loop variable

Note that your expression after using 'simplify' can be seen as
(p!/(p/2)!)^2/2^p and using 'convert(%,GAMMA)' would show you
that there is no need to care for even or odd.

Otherwise you can code it as

  b := Array(0..500, 0..500);

  for p from 0 to 500 by 2 do
    b[p,0] := (p!/(p/2)!)^2/2^p;
  end do;
  p:='p':

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

Array(0..500,0..500, (i,j) -> `if`(j=0 and i::odd, i!/...,  0));

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}