# HG changeset patch
# User Will Kelly <removed>
# Date 1339220991 14400
# Node ID 119ffe486c2e9c478cf62a7575fb6e8f0b55c3af
# Parent  ec924357b3759cdb3ef304be43928ebff5ec208e
New ref functionality. Additionally an alternative to current rref which does not handle small numbers well.

diff -r ec924357b375 -r 119ffe486c2e scripts/linear-algebra/REF.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/linear-algebra/REF.m	Sat Jun 09 01:49:51 2012 -0400
@@ -0,0 +1,123 @@
+## Copyright (C) 2012 Will Kelly
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn  {Function File} {} REF (@var{A})
+## @deftypefnx {Function File} {} REF (@var{A}, @var{walkthrough})
+## @deftypefnx {Function File} { [@var{B}, @var{number_pivots},  @var{pivot_locations}] =} rref (@dots{})
+## Return an row echelon form of @var{A}.  For a step by step 
+## guide set @var{walkthrough} to 1.
+##
+##
+## @end deftypefn
+
+## Author: Will Kelly 
+## Source: Lay, David C. Linear Algebra and its Applications Fourth Edition. 
+##         Boston: Addison-Wesley, 2012. Print. (pg. 13-17)
+
+function [fX_REF, number_pivots, pivot_list, warn] = REF (A, walkthrough)
+   if(nargin<1)
+      print_usage ();
+      ##error ("REF: expecting m,n matrix as first argument.");
+   else
+      fX_REF=A;
+      m_ref=size(fX_REF)(1);
+      n_ref=size(fX_REF)(2);
+      has_pivot = 0;
+      number_pivots = 0;
+      warn = 0;
+      pivot_list=[];
+      if(nargin==1)
+         walkthrough=0;
+      endif
+      if(walkthrough==1)
+         printf("Original Matrix:\n");
+         fX_REF
+      endif
+      for y = 1:n_ref
+         for x = number_pivots+1:m_ref
+            greatest=abs(fX_REF(x,y));
+            position=x;
+            for x1=x+1:m_ref
+               if(greatest < abs(fX_REF(x1,y)))
+                  position = x1;
+                  greatest = abs(fX_REF(x1,y));
+               endif
+            endfor
+            if(x!=position)
+               var_placeholder=fX_REF(x,:);
+               fX_REF(x,:)=fX_REF(position,:);
+               ##Interchange:
+               fX_REF(position,:)=var_placeholder;
+               if(walkthrough == 1)
+                  printf("Partial Pivoting: Interchange row %i with row %i.\n", x, position);
+                  fX_REF
+               endif
+            endif
+            if (fX_REF(x,y)!=0)
+               if (has_pivot == 0)
+                  has_pivot=1;
+                  number_pivots=number_pivots+1;
+                  pivot_list=[pivot_list, y];
+                  if (fX_REF(x,y)!=1)
+                     ##Scale:
+                     scale=1/fX_REF(x,y);
+                     if(walkthrough == 1)
+                        printf("Scale row %i by %g.\n", x, scale);
+                     endif
+                     fX_REF(x,:)=scale*fX_REF(x,:);
+                     ##Sanity Check
+                     if(scale == 0)
+                           warn=1;
+                     endif
+                     if(walkthrough == 1)
+                        fX_REF
+                     endif
+                  endif
+                  
+               else
+                  ##Row Multiplication and Addition:
+                  mult=(-fX_REF(x,y))*fX_REF(number_pivots,:);
+                  if(walkthrough == 1)
+                     printf("Multiply %g with row %g.", -fX_REF(x,y), number_pivots);
+                     printf(" Add the resulting row:");
+                     printf(mat2str(mult));
+                     printf(" to row %g.\n", x);
+                  endif
+                  addition=mult+fX_REF(x,:);
+                  fX_REF(x,:)=addition;
+                  if(walkthrough == 1)
+                     fX_REF
+                  endif
+               endif
+               ##Sanity Checks
+               if(any(isnan(fX_REF(x,:))) || any(isinf(fX_REF(x,:))))
+                  warn=1;
+               endif
+            endif
+         endfor
+         if(number_pivots==m_ref)
+            break;
+         endif
+         has_pivot=0;
+      endfor
+      if(warn==1)
+         printf("REF: There might be errors in this answer.\n");
+      endif
+   endif
+endfunction
diff -r ec924357b375 -r 119ffe486c2e scripts/linear-algebra/RREF2.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/linear-algebra/RREF2.m	Sat Jun 09 01:49:51 2012 -0400
@@ -0,0 +1,76 @@
+## Copyright (C) 2012 Will Kelly
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn  {Function File} {} RREF (@var{A})
+## @deftypefnx {Function File} {} RREF (@var{A}, @var{walkthrough})
+## Return the reduced row echelon form of @var{A}.  For a step by step 
+## guide set @var{walkthrough} to 1.
+##
+##
+## @end deftypefn
+
+## Author: Will Kelly
+## Source: Lay, David C. Linear Algebra and its Applications Fourth Edition. 
+##         Boston: Addison-Wesley, 2012. Print. (pg. 13-17)
+
+function fX_RREF = RREF2 (A, walkthrough)
+   if(nargin<1)
+      print_usage ();
+      ##error ("REF: expecting m,n matrix as first argument.");
+   else
+      warn=0;
+      if(nargin==1)
+         walkthrough=0;
+         [fX_RREF,number_pivots,pivot_list]=REF(A);
+      endif
+      if(nargin==2)
+         [fX_RREF,number_pivots,pivot_list,warn]=REF(A,1);
+      endif
+      if(walkthrough == 1)
+         printf("Entering Backward Phase.\n");
+      endif
+      for y = -number_pivots:-2
+         a=-y;
+         for x = 1:(a-1)
+            if(fX_RREF(x,pivot_list(1,a))!=0)
+               ##Row Multiplication and Addition:
+               mult = (-fX_RREF(x,pivot_list(1,a)))*fX_RREF(a,:);
+                  if(walkthrough == 1)
+                     printf("Multiply %g with row %g.", -fX_RREF(x,pivot_list(1,a)), a);
+                     printf(" Add the resulting row:");
+                     printf(mat2str(mult))
+                     printf(" to row %g.\n", x);
+                  endif
+               addition = mult+fX_RREF(x,:);
+               fX_RREF(x,:)=addition;
+                  if(walkthrough == 1)
+                     fX_RREF
+                  endif
+               ##Sanity Checks
+               if(any(isnan(fX_RREF(x,:))) || any(isinf(fX_RREF(x,:))) )
+                     warn=1;
+               endif
+            endif
+         endfor
+      endfor
+      if(warn==1)
+         printf("RREF: There might be errors in this answer.\n");
+      endif
+   endif
+endfunction
