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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Reduced row echelon form step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

var [const] GSL=Import("zklGSL");	// libGSL (GNU Scientific Library)
fcn toReducedRowEchelonForm(M){  // in place
   lead,rows,columns := 0,M.rows,M.cols;
   foreach r in (rows){
      if (columns<=lead) return(M);
      i:=r;
      while(M[i,lead]==0){  // not a great check to use with real numbers
	 i+=1;
	 if(i==rows){
	    i=r; lead+=1;
	    if(lead==columns) return(M);
	 }
      }
      M.swapRows(i,r);
      if(x:=M[r,lead]) M[r]/=x;
      foreach i in (rows){ if(i!=r) M[i]-=M[r]*M[i,lead] }
      lead+=1;
   }
   M
}

A:=GSL.Matrix(3,4).set( 1, 2, -1,  -4,
		        2, 3, -1, -11,
		       -2, 0, -3,  22);
toReducedRowEchelonForm(A).format(5,1).println();

fcn toReducedRowEchelonForm(m){ // m is modified, the rows are not
   lead,rowCount,columnCount := 0,m.len(),m[1].len();
   foreach r in (rowCount){
      if(columnCount<=lead) break;
      i:=r;
      while(m[i][lead]==0){
	 i+=1;
	 if(rowCount==i){
	    i=r; lead+=1;
	    if(columnCount==lead) break;
	 }
      }//while
      m.swap(i,r); // Swap rows i and r
      if(n:=m[r][lead]) m[r]=m[r].apply('/(n)); //divide row r by M[r,lead]
      foreach i in (rowCount){
         if(i!=r) // Subtract M[i, lead] multiplied by row r from row i
	    m[i]=m[i].zipWith('-,m[r].apply('*(m[i][lead])))
      }//foreach
      lead+=1;
   }//foreach
   m
}

m:=List( T( 1, 2, -1, -4,),  // T is read only list
         T( 2, 3, -1, -11,),
	 T(-2, 0, -3,  22,));
printM(m);
println("-->");
printM(toReducedRowEchelonForm(m));

fcn printM(m){ m.pump(Console.println,rowFmt) }
fcn rowFmt(row){ ("%4d "*row.len()).fmt(row.xplode()) }

  

You may also check:How to resolve the algorithm Y combinator step by step in the PHP programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the D programming language
You may also check:How to resolve the algorithm Function composition step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Secure temporary file step by step in the Racket programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Julia programming language