Writing a vector variable in Magma.

438 Views Asked by At

So let's assume I am working with $n$-dimensional functions. In Magma I have a code that goes like this:

n:=4;
function fun(x1,x2,x3,x4)
return (x1*x2+x3*x4) mod 2;
end function;

Now, if I want to increase $n$, I have to manually write all the variables from $x_1$ to $x_8$. Is there a more convenient way to do this, by saying that $x$ is in some Vector space $GF(2)^8$ or something similar, to avoid this manual writing.

2

There are 2 best solutions below

0
On BEST ANSWER

You can do this several ways.

If $x$ is a vector in $V = {GF(2)}^{n}$, then you can define

f := func<x | &+[x[i] : i in [1..OverDimension(x)]]>;

and call through

f(V![x1, x2, x3, .., xn]);

but this is a pain if you want to use different vectors of different lengths. You might as well just have $x$ be a sequence of $GF(2)$ elements, so you can define

f := func<x | &+x>;

and call through

f([x1, x2, x3, .., xn]);

A third option is to use a variadic function, that can take varying number of inputs and stores them all as a list. Here you would define

f := func<x, ... | &+[a : a in x]>;

This is nicest in terms of calling since these will all work:

f(a);
f(a,b);
f(a,b,c,d,e,f,g);
0
On

Yes, this can be done: you can pull the dimension of the parent of the input. See the following code:

function f(x)
  V := Parent(x);
  F := BaseField(V);
  d := Dimension(V);
  ans := F!0;
  for i := 1 to (d div 2) do
    ans := ans + x[2*i-1]*x[2*i];
  end for;
  return ans;
end function;

F := GF(2);
V := VectorSpace(F,8);
my_x := Random(V);

f(my_x);

For more, see the Magma handbook.