# How to fill an array from maple code

I have simple Maple code which fills 2d array with coefficients which i want to use in Mathematica for my custom functions:

f := array(-100..100,-100..100):
for n from - 100 by 1 to 100 do
for j from - 100 by 1 to 100 do
f[n,j] := 0:
end do:
end do:
f[0,-1] := 1;
f[1,-2] := 1;
for n from     1 by 1 to  99 do
for j from - 100 by 1 to  99 do
f[n + 1,j] := - f[n - 1,j] + (2*n + 1)*f[n,j + 1]:
end do:
end do:
for n from     0 by - 1 to - 99 do
for j from - 100 by   1 to   99 do
f[n - 1,j] := - f[n + 1,j] + (2*n + 1)*f[n,j + 1]:
end do:
end do:


Is it possible to mechanically convert this to Wolfram language? Its even not allowed to create Array[f, {-100, 100}, {-100, 100}] because it contains negative numbers.

f := array(-100..100,-100..100):
for n from - 100 by 1 to 100 do
for j from - 100 by 1 to 100 do
f[n,j] := 0:
end do:
end do:


can be converted to

n = 100;
f = ConstantArray[0, {2 n + 1, 2 n + 1}];


The rest can be translated to the following Do-loops.

o = n + 1;
f[[o, o - 1]] = 1;
f[[o + 1, o - 2]] = 1;
Do[
f[[i + 1, j]] = -f[[i - 1, j]] + (2 i + 1) f[[i, j + 1]],
{i, o + 1, o + (n - 1)},
{j, o - n, o + (n - 1)}
];
Do[
f[[i - 1, j]] = -f[[i + 1, j]] + (2 i + 1) f[[i, j + 1]],
{i, o, o - (n - 1), -1},
{j, o - n, o + (n - 1)}
];


But I give no guarantee for correctness. It is not the most efficient or most Mathematica-like way of doing it, but it should get you started.

• @satoru Was this helpful or not? Some feedback of any kind would be appreciated. Sep 10, 2018 at 23:02

The following is an incomplete imitation for the array function of maple:

Clear[array, eval, print]
array /: (symbol_ = array[index__]) :=
eval@symbol ^= Array[symbol, 1 - Subtract @@@ {index}, {index}];
print = eval;


I've only mimic the behavior related to index, eval, print. Let's test it with examples in its document:

v = array[{1, 4}];
Do[v[i] = i^2, {i, 4}]
v[2]
(* 4 *)
v[0]
(* v[0] *)
eval@v
(* {1, 4, 9, 16} *)

A = array[{1, 2}, {1, 2}];
A[1, 2] = x;
print@A // MatrixForm


Similar enough, I think. With this array at hand, your code can be translated to:

f = array[{-100, 100}, {-100, 100}];
Do[
f[n, j] = 0,
{n, -100, 100}, {j, -100, 100}]
f[0, -1] = 1;
f[1, -2] = 1;
Do[
f[n + 1, j] = -f[n - 1, j] + (2 n + 1) f[n, j + 1],
{n, 1, 99}, {j, -100, 99}]
Do[
f[n - 1, j] = -f[n + 1, j] + (2 n + 1) f[n, j + 1],
{n, 0, -99, -1}, {j, -100, 99}]


Notice it's important to use = instead of :=.

Finally, a casual test:

f[-100, -99]
(*
-3349903854308016956990560304891976966494718286268537053448836660676342843505978149957098\
44989101930873893181794380748417680500201772219929015360140799620182827564370450377464294\
43359375
*)


OK, the conversion seems to succeed.

Or could use Association

n = 100;
rr = Flatten[Outer[List, Range[-n, n], Range[-n, n]], 1];

ff = Association[Thread[rr -> 0]];
ff[{0, -1}] = 1;
ff[{1, -2}] = 1;
Do[ff[{i + 1, j}] = -ff[{i - 1, j}] + (2*i + 1)*ff[{i, j + 1}]
, {i, 1, n - 1}, {j, -n, n - 1}]
Do[ff[{i - 1, j}] = -ff[{i + 1, j}] + (2*i + 1)*ff[{i, j + 1}]
, {i, 0, -n + 1, -1}, {j, -n, n - 1}]


This uses 7-8 times as much memory as the method indicated by @HenrikSchumacher. The key advantage, if you will, is that you can use negative numbers in the keys.