문제

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