How to resolve the algorithm Reduced row echelon form step by step in the R programming language

Published on 12 May 2024 09:40 PM
#R

How to resolve the algorithm Reduced row echelon form step by step in the R programming language

Table of Contents

Problem Statement

Show how to compute the reduced row echelon form (a.k.a. row canonical form) of a matrix. The matrix can be stored in any datatype that is convenient (for most languages, this will probably be a two-dimensional array). Built-in functions or this pseudocode (from Wikipedia) may be used: For testing purposes, the RREF of this matrix: is:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Reduced row echelon form step by step in the R programming language

Source code in the r programming language

rref <- function(m) {
  pivot <- 1
  norow <- nrow(m)
  nocolumn <- ncol(m)
  for(r in 1:norow) {
    if ( nocolumn <= pivot ) break;
    i <- r
    while( m[i,pivot] == 0 ) {
      i <- i + 1
      if ( norow == i ) {
        i <- r
        pivot <- pivot + 1
        if ( nocolumn == pivot ) return(m)
      }
    }
    trow <- m[i, ]
    m[i, ] <- m[r, ]
    m[r, ] <- trow
    m[r, ] <- m[r, ] / m[r, pivot]
    for(i in 1:norow) {
      if ( i != r )
        m[i, ] <- m[i, ] - m[r, ] * m[i, pivot]
    }
    pivot <- pivot + 1
  }
  return(m)
}

m <- matrix(c(1, 2, -1, -4,
              2, 3, -1, -11,
              -2, 0, -3, 22), 3, 4, byrow=TRUE)
print(m)
print(rref(m))

  

You may also check:How to resolve the algorithm Farey sequence step by step in the Sidef programming language
You may also check:How to resolve the algorithm Include a file step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Phix programming language
You may also check:How to resolve the algorithm Tupper's self-referential formula step by step in the Julia programming language
You may also check:How to resolve the algorithm URL encoding step by step in the Ada programming language