kill(all)$

general code "reduced row echelon form of a matrix :  (rows ≠ columns )",version 1.0
peter.vlasschaert@gmail.com,18/10/2019

rem : Prof Denis Constales  ( UGent),   orginal version maple

load  packages
============

load(linearalgebra)$
load(lrats)$

a)  intro programming general code : (r)educed (r)ow (e)chelon (f)orm
------------------------------------------------------------------------------------------------

How to enter matrix :
================

Menu => Algebra => Enter  Matrix

aa: matrix(
 [1,2], 
 [3,4], 
 [5,6]
);

find element in a matrix:
---------------------------------

q:aa[3,2];

p1:matrix_size(aa);

number of rows      : p1a
number of columns :p1b
===================

p1a: p1[1];     /* number of rows        */
p1b: p1[2];     /*  number of columns */

How to use  'while' command in Maxima:
=============================== 

google :  =>  search:programming in wxmaxima
------------------------------------------------------------------

http://maxima.sourceforge.net/docs/tutorial/en/gaertner-tutorial-revision/Pages/Programming0002.htm

n1:10;
ii:3;
while ( ii ≤ n1)  and (ii >2) do
(ii:ii+1)$

ii;

remvalue(n1,ii);

p2:[n1,ii];

rem : exit command ( = analog 'break' instruction)
--------------------------------------------------------------------
use : while instruction

a0:copy(aa);

remvalue(a0);

a0;

b) translation code Denis C: reduced row echelon form.
----------------------------------------------------------------------------
use : block statement : ' wxmaxima'

(r)educed (r)ow (e)chelon (f)orm = RREF.

function : RREF (matrix):
===================

syntax  matrix_dim (matrix) := block([local var],code for the function,return(result))

How to build a function to find : size of matrix
--------------------------------------------------------------

input =>

aa;

matrix_dim(A0):= block(
[m0, n0] /*local variables */,
    A: copy(A0),
    m0:matrix_size(A)[1],  /*load linearalgebra */
    n0 :matrix_size(A)[2], /*load  linearalgebra */
    m:m0,
    n:n0,
        return([m,n])/*output*/)$

output =>

p3:matrix_dim(aa);

rem : if instruction
------------------------

x:3;

ss:0;

if x=3 then (
    for ix:4 thru 12 step  2 do ( ix :ss+ix,print(ix)));

ix;

How to build a function to find : RREF(matrix)
--------------------------------------------------------------

RREF(A0):=  block (
  [m, n, i0, j0, i, j, t, p, i1,A],
     A : copy(A0),  
  [m,n]:matrix_size(A),
  i0 : 1,
  j0 : 1,
  while (i0 <= m and j0 <= n) do (
    i1 : 0,
    for i : i0 thru m do
      if  A[i, j0] # 0 then i1 : i,
    if i1 = 0 then
      j0 : j0 + 1    
    else (
      if i1 # i0 then
        for j : j0 thru n do (
          t : A[i0, j],
          A[i0, j] : A[i1, j],
          A[i1, j] : t
        ),                                              
        p : A[i0, j0],
        for j : j0 thru n do
          A[i0, j] : A[i0, j]/p,              
        for i : 1  thru m do (
          if i # i0 then (
            p : A[i, j0],
            for j : j0 thru n do
                       (A[i, j] : A[i, j] - p*A[i0, j])
            )),      
        i0 : i0 + 1,        
        j0 : j0 +1)),
  return(A)
)$

example  (1):
==========

A1: matrix(
 [1,1,2,3,2], 
 [1,1,3,1,4]
);

RREF(A1);

example (2):
=========

A2:matrix([12,-27,36,44,59],
          [26,41,-54,24,23],
          [33,70,59,15,-68],
          [43,16,29,-52,-61],
          [-43,20,71,88,11]);

RREF(A2);

example (3):
=========;

A3: matrix(
 [1,2,3,8], 
 [4,8,12,16]
);

RREF(A3);