【问题标题】:Reduced row echelon form减少排梯队形式
【发布时间】:2011-03-08 19:01:19
【问题描述】:

R 中是否有生成矩阵的reduced row echelon form 的函数? This 参考说没有。你同意吗?

【问题讨论】:

    标签: r matrix linear-algebra


    【解决方案1】:

    看起来没有内置的,但我在this 页面上找到了这个 rref 函数。

     rref <- function(A, tol=sqrt(.Machine$double.eps),verbose=FALSE,
                     fractions=FALSE){
      ## A: coefficient matrix
      ## tol: tolerance for checking for 0 pivot
      ## verbose: if TRUE, print intermediate steps
      ## fractions: try to express nonintegers as rational numbers
      ## Written by John Fox
      if (fractions) {
        mass <- require(MASS)
        if (!mass) stop("fractions=TRUE needs MASS package")
      }
      if ((!is.matrix(A)) || (!is.numeric(A)))
        stop("argument must be a numeric matrix")
      n <- nrow(A)
      m <- ncol(A)
      for (i in 1:min(c(m, n))){
        col <- A[,i]
        col[1:n < i] <- 0
        # find maximum pivot in current column at or below current row
        which <- which.max(abs(col))
        pivot <- A[which, i]
        if (abs(pivot) <= tol) next     # check for 0 pivot
        if (which > i) A[c(i, which),] <- A[c(which, i),]  # exchange rows
        A[i,] <- A[i,]/pivot            # pivot
        row <- A[i,]
        A <- A - outer(A[,i], row)      # sweep
        A[i,] <- row                    # restore current row
        if (verbose)
          if (fractions) print(fractions(A))
          else print(round(A,round(abs(log(tol,10)))))
      }
      for (i in 1:n)
        if (max(abs(A[i,1:m])) <= tol)
          A[c(i,n),] <- A[c(n,i),] # 0 rows to bottom
      if (fractions) fractions (A)
      else round(A, round(abs(log(tol,10))))
    }
    

    【讨论】:

      【解决方案2】:

      pracma 包还包含一个实现。见 pracma::rref。

      【讨论】:

        【解决方案3】:

        我没有足够的代表发表评论,但士兵.moth 在接受的答案 [编辑 2018: 不再接受的答案] 中给出的 above 函数是错误的 - 它没有处理 RREF 解的主对角线上为零的矩阵。试试例如

        m

        并注意输出不在 RREF 中。

        我认为我可以正常工作,但您可能想自己检查输出:

        rref <- function(A, tol=sqrt(.Machine$double.eps),verbose=FALSE,
                         fractions=FALSE){
          ## A: coefficient matrix
          ## tol: tolerance for checking for 0 pivot
          ## verbose: if TRUE, print intermediate steps
          ## fractions: try to express nonintegers as rational numbers
          ## Written by John Fox
          # Modified by Geoffrey Brent 2014-12-17 to fix a bug
          if (fractions) {
            mass <- require(MASS)
            if (!mass) stop("fractions=TRUE needs MASS package")
          }
          if ((!is.matrix(A)) || (!is.numeric(A)))
            stop("argument must be a numeric matrix")
          n <- nrow(A)
          m <- ncol(A)
          x.position<-1
          y.position<-1
          # change loop:
          while((x.position<=m) & (y.position<=n)){
            col <- A[,x.position]
            col[1:n < y.position] <- 0
            # find maximum pivot in current column at or below current row
            which <- which.max(abs(col))
            pivot <- col[which]
            if (abs(pivot) <= tol) x.position<-x.position+1     # check for 0 pivot
            else{
              if (which > y.position) { A[c(y.position,which),]<-A[c(which,y.position),] } # exchange rows
              A[y.position,]<-A[y.position,]/pivot # pivot
              row <-A[y.position,]
              A <- A - outer(A[,x.position],row) # sweep
              A[y.position,]<-row # restore current row
              if (verbose)
                if (fractions) print(fractions(A))
                else print(round(A,round(abs(log(tol,10)))))
              x.position<-x.position+1
              y.position<-y.position+1
            }
          }
          for (i in 1:n)
            if (max(abs(A[i,1:m])) <= tol)
              A[c(i,n),] <- A[c(n,i),] # 0 rows to bottom
          if (fractions) fractions (A)
          else round(A, round(abs(log(tol,10))))
        }
        

        【讨论】:

        • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
        • 对不起,我是新来的,可能遗漏了一些东西,但是:上面由士兵.moth 提供的“接受的答案”是错误的(当我自己尝试使用它时发现了困难的方法!)所以我认为标记它很重要。我没有足够的代表直接评论士兵.moth 的答案,所以我创建了一个新答案 - 我应该在这里做什么?
        • 获得足够的声誉以先发表评论?
        • 耸耸肩我真的不认为有人会因为我指出一个不明显的错误而烦恼,该错误已经四年没有更正,并且考虑到错误的“解决方案” 上面发布的内容被接受为答案,我很困惑为什么认为相同代码的固定版本不那么重要。
        • @GeoffreyBrent,欢迎使用 Stackoverflow。规则取代常识。
        【解决方案4】:

        最近还开发了一个用于教学线性代数的软件包 (matlib),它既可以计算矩阵的梯形形式,也可以显示沿途使用的步骤。

        来自reference docs的示例:

        library('matlib')
        A <- matrix(c(2, 1, -1,-3, -1, 2,-2, 1, 2), 3, 3, byrow=TRUE)
        b <- c(8, -11, -3)
        echelon(A, b, verbose=TRUE, fractions=TRUE)
        
        Initial matrix:
             [,1] [,2] [,3] [,4]
        [1,]   2    1   -1    8 
        [2,]  -3   -1    2  -11 
        [3,]  -2    1    2   -3 
        
        row: 1 
        
         exchange rows 1 and 2 
             [,1] [,2] [,3] [,4]
        [1,]  -3   -1    2  -11 
        [2,]   2    1   -1    8 
        [3,]  -2    1    2   -3 
        
         multiply row 1 by -1/3 
             [,1] [,2] [,3] [,4]
        [1,]    1  1/3 -2/3 11/3
        [2,]    2    1   -1    8
        [3,]   -2    1    2   -3
        
         multiply row 1 by 2 and subtract from row 2 
             [,1] [,2] [,3] [,4]
        [1,]    1  1/3 -2/3 11/3
        [2,]    0  1/3  1/3  2/3
        [3,]   -2    1    2   -3
        
         multiply row 1 by 2 and add to row 3 
             [,1] [,2] [,3] [,4]
        [1,]    1  1/3 -2/3 11/3
        [2,]    0  1/3  1/3  2/3
        [3,]    0  5/3  2/3 13/3
        
        row: 2 
        
         exchange rows 2 and 3 
             [,1] [,2] [,3] [,4]
        [1,]    1  1/3 -2/3 11/3
        [2,]    0  5/3  2/3 13/3
        [3,]    0  1/3  1/3  2/3
        
         multiply row 2 by 3/5 
             [,1] [,2] [,3] [,4]
        [1,]    1  1/3 -2/3 11/3
        [2,]    0    1  2/5 13/5
        [3,]    0  1/3  1/3  2/3
        
         multiply row 2 by 1/3 and subtract from row 1 
             [,1] [,2] [,3] [,4]
        [1,]    1    0 -4/5 14/5
        [2,]    0    1  2/5 13/5
        [3,]    0  1/3  1/3  2/3
        
         multiply row 2 by 1/3 and subtract from row 3 
             [,1] [,2] [,3] [,4]
        [1,]    1    0 -4/5 14/5
        [2,]    0    1  2/5 13/5
        [3,]    0    0  1/5 -1/5
        
        row: 3 
        
         multiply row 3 by 5 
             [,1] [,2] [,3] [,4]
        [1,]    1    0 -4/5 14/5
        [2,]    0    1  2/5 13/5
        [3,]    0    0    1   -1
        
         multiply row 3 by 4/5 and add to row 1 
             [,1] [,2] [,3] [,4]
        [1,]    1    0    0    2
        [2,]    0    1  2/5 13/5
        [3,]    0    0    1   -1
        
         multiply row 3 by 2/5 and subtract from row 2 
             [,1] [,2] [,3] [,4]
        [1,]  1    0    0    2  
        [2,]  0    1    0    3  
        [3,]  0    0    1   -1  
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-14
          • 1970-01-01
          • 2022-07-28
          • 1970-01-01
          • 2014-08-05
          • 1970-01-01
          • 2016-02-03
          • 2019-01-18
          相关资源
          最近更新 更多