繁体   English   中英

有没有一种方法可以将多项式或多项式的系数存储在 R 向量中的单个元素中?

[英]Is there a way that I can store a polynomial or the coefficients of a polynomial in a single element in an R vector?

我想创建一个生成循环有限群 F[x] 的 R function。

基本上,我需要找到一种方法将多项式或至少多项式的系数存储在 R 向量中的单个元素中。

例如,如果我有一个集合 F={0,1,x,1+x},我想将这四个多项式保存到一个 R 向量中,例如

F[1] <- 0 + 0x
F[2] <- 1 + 0x
F[3] <- 0 + x
F[4] <- 1 + x

但我不断收到错误消息:“要替换的项目数不是替换长度的倍数”

有没有办法我至少可以做类似的事情:

F[1] <- (0,0)
F[2] <- (1,0)
F[3] <- (0,1)
F[4] <- (1,1)

作为参考,以防有人对我正在尝试处理的数学问题感兴趣,到目前为止,我的整个 R function 是

gf <- function(q,p){

  ### First we need to create an irreducible polynomial of degree p
  poly <- polynomial(coef=c(floor(runif(p,min=0,max=q)),1)) #This generates the first polynomial of degree p with coefficients ranging between the integer values of 0,1,...,q
  for(i in 1:(q^5*p)){ #we generate/check our polynomial a sufficient amount of times to ensure that we get an irreducible polynomial
    poly.x <- as.function(poly) #we coerce the generated polynomial into a function
    for(j in 0:q){ #we check if the generated polynomial is irreducible
      if(poly.x(j) %% q == 0){ #if we find that a polynomial is reducible, then we generate a new polynomial
        poly <- polynomial(coef=c(floor(runif(p,min=0,max=q)),1)) #...and go through the loop again
      }
    }
  }
  list(poly.x=poly.x,poly=poly)

  ### Now, we need to construct the cyclic group F[x] given the irreducible polynomial "poly"
  F <- c(rep(0,q^p)) #initialize the vector F
  for(j in 0:(q^p-1)){
    #F[j] <- polynomial(coef = c(rep(j,p)))
    F[j] <- c(rep(0,3))  
  }
  F
}

确保F是一个列表,然后使用[[]]来放置值

F<-list()
F[[1]] <- c(0,0)
F[[2]] <- c(1,0)
F[[3]] <- c(0,1)
F[[4]] <- c(1,1)

列表可以保存异构数据类型。 如果一切都是常数和 x 的系数,那么您也可以使用矩阵。 只需使用[row, col]类型子集设置每一行值。 您需要在创建大小时对其进行初始化。 它不会像列表一样自动增长。

F <- matrix(ncol=2, nrow=4)
F[1, ] <- c(0,0)
F[2, ] <- c(1,0)
F[3, ] <- c(0,1)
F[4, ] <- c(1,1)

您必须将它们存储为字符串,否则 R 将尝试解释运算符。 你可以有

F[1] <- "0 + 0x"

甚至是一个矩阵,它对于应用和您可能想做的其他操作更灵活

mat <- matrix(c(0,1,0,1,0,0,1,1), ncol=2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM