Search code examples
rgalois-field

Galois field (GF) with R


Is there any package for Galois fields (GF) in R? I would like to define the following matrix operations with GF.

  • 1+1=0
  • 1+0=1
  • 0+1=1
  • 0+0=0

Obviously R doesn't understand the 1+1 without specifying it:

> k <- matrix(c(0,1,1,0,1,0,0,0,0,1), ncol=10);k
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    1    1    0    1    0    0    0    0     1
> p <- matrix(c(0,0,0,1,1,1,0,1,0,1), ncol=10);p
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    1    1    1    0    1    0     1
> c <- k+p;c
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    1    1    1    2    1    0    1    0     2

Solution

  • I don't know anything about Galois fields, but from this question it appears that addition and subtraction are equivalent to xor. It might be easier to just use this fact to do your computations.

    as.integer(xor(k,p))
    

    Or, if you want to get really fancy, you can overload the operator and define your own class:

    `+.GF`<-function (x, y) as.integer((x | y) & !(x & y))
    class(k)<-'GF'
    class(p)<-'GF'
    k+p
    [1] 0 1 1 1 0 1 0 1 0 0