R中有Galois字段(GF)的包吗?我想用GF定义以下矩阵运算。
显然,R不理解1+1
而没有指定它:
> 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
答案 0 :(得分:3)
我对Galois字段一无所知,但从this question来看,加法和减法似乎等同于xor
。使用这个事实进行计算可能更容易。
as.integer(xor(k,p))
或者,如果你想变得非常花哨,你可以重载运算符并定义你自己的类:
`+.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
答案 1 :(得分:2)
您可以自己定义一个功能。看起来你只是做布尔算法。
myadd <- function(x, y){(x+y) %% 2}
实施例
> 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