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
有帮助吗?

解决方案

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

其他提示

You could just define a function yourself. It looks like you're just doing boolean arithmetic.

myadd <- function(x, y){(x+y) %% 2}

Example

> a <- matrix(c(0,0,1,1),2,2)
> b <- matrix(c(0,1,0,1),2,2)
> a
     [,1] [,2]
[1,]    0    1
[2,]    0    1
> b
     [,1] [,2]
[1,]    0    0
[2,]    1    1
> myadd(a,b)
     [,1] [,2]
[1,]    0    1
[2,]    1    0
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top