【问题标题】:Is there a way that I can store a polynomial or the coefficients of a polynomial in a single element in an R vector?有没有一种方法可以将多项式或多项式的系数存储在 R 向量的单个元素中?
【发布时间】:2020-08-12 19:16:12
【问题描述】:

我想创建一个 R 函数来生成循环有限群 F[x]。

基本上,我需要找到一种方法将多项式或至少多项式的系数存储在 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 函数是

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
}

【问题讨论】:

    标签: r vector abstract polynomials coefficients


    【解决方案1】:

    确保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)
    

    【讨论】:

    • 感谢分享列表和矩阵方式!
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 感谢您分享这个!
    猜你喜欢
    • 2020-05-14
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    相关资源
    最近更新 更多