If then statements with two indices in Magma software

316 Views Asked by At

In Magma a single variable functions can be created for use, say,

f:= func< n | n lt 2 select 2 else Self(n-1)*Self(n)+1 >;
[f(n): n in [0..12]];

These are easily modified and ready to use. This question is about either creating a function of two variables or an if-then statement. For example

f:= func< n, k | (n eq 0 and k eq 0) select 1 else (k eq 0) select 2 else (k eq n) select 4 else Self(n-1, k-1) + Self(n-1, k) >; 

and (this example gives the error message " User error: syntax error at end of input ")

f:= function(n,k);
  if n eq 0 and k eq 0 then 1;
  elif k eq 0 then 2;
  elif k eq n then 4;
  else Self(n-1, k-1) + Self(n-1, k);
end if;
[[f(n,k): k in [0..n]]: n in [0..5]];

should work but it seems that maybe Magma does not compute two variable (index) self referenced (recurrence) function. Is this the case or is there a workable model in which the if-then/function can be computed?

1

There are 1 best solutions below

2
On BEST ANSWER

Some things to note: the Self command is used when recursively constructing a sequence, to refer to entries in the sequence under construction. But it is not used in creating recursive functions. You want to either name the function (using the alternate function f construction method below), or when you are constructing a function that has not yet been assigned a name you can refer to it using $$.

Third function should be

function f(n,k)
  if n eq 0 and k eq 0 then
    val := 1;
  elif k eq 0 then
    val := 2;
  elif k eq n then
    val := 4;
  else
    val := f(n-1,k-1) + f(n-1,k);
  end if;
  return val;
end function;

or

function f(n,k)
  return (n eq 0 and k eq 0) select 1 else (k eq 0) select 2 else (n eq k) select 4 else f(n-1, k-1) + f(n-1, k);
end function;

or

function f(n,k)
  return case< <n,k> | <0,0> : 1, <n,0> : 2, <n,n> : 4, default: f(n-1, k-1) + f(n-1, k)>;
end function;

Using the function definition you were using previously, you could also write

f := function(n,k)
  return case< <n,k> | <0,0> : 1, <n,0> : 2, <n,n> : 4, default: $$(n-1, k-1) + $$(n-1, k)>;
end function;