Gram-Schmidt



On Sun, Oct 28, 2007 at 09:12:29PM -0500, Peter Danenberg wrote:
> I've attached a patch against the current CVS which:
> 
>      * adds an optional `inprod' parameter to gramschmidt in eigen.mac;
> 
>      * updates the docs in Matrices.texi;
> 
>      * adds a test to test_vector_analysis.mac.

Whoops; the attached patch supplants the first one, and:

    * supports the `gschmit' (sic) alias;

    * adds an example to Matrices.texi.
-------------- next part --------------
diff -Naur maxima-cvs/ChangeLog maxima/ChangeLog
--- maxima-cvs/ChangeLog	2007-10-28 17:45:12.000000000 -0700
+++ maxima/ChangeLog	2007-10-28 20:06:14.000000000 -0700
@@ -1,3 +1,14 @@
+2007-10-28  Peter Danenberg  <pcd at wikitex.org>
+
+	* share/matrix/eigen.mac: added optional inner-product
+	  to gramschmidt and gschmit.
+
+	* doc/info/Matrices.texi:
+	  Added documentation for optional inner-product.
+
+	* tests/wester_problems/test_vector_analysis.mac: added
+	  a test for gramschmidt with inner-product.
+
 2005-03-13 12:00  robert_dodier
 
 	* src/nset.lisp, tests/rtestnset.mac:
diff -Naur maxima-cvs/doc/info/Matrices.texi maxima/doc/info/Matrices.texi
--- maxima-cvs/doc/info/Matrices.texi	2007-10-28 17:45:09.000000000 -0700
+++ maxima/doc/info/Matrices.texi	2007-10-28 20:06:14.000000000 -0700
@@ -751,10 +751,13 @@
 
 @end deffn
 
- at deffn {Function} gramschmidt (@var{x})
+ at deffn {Function} gramschmidt (@var{x}, @var{inprod})
+ at deffnx {Function} gramschmidt (@var{x})
+ at deffnx {Function} gschmit (@var{x}, @var{inprod})
 @deffnx {Function} gschmit (@var{x})
 Carries out the Gram-Schmidt orthogonalization algorithm on @var{x},
-which is either a matrix or a list of lists.
+which is either a matrix or a list of lists; using the optional
+inner-product @var{inprod}.
 @var{x} is not modified by @code{gramschmidt}.
 
 If @var{x} is a matrix, the algorithm is applied to the rows of @var{x}.
@@ -793,6 +796,12 @@
 (%i4) i: innerproduct$
 (%i5) [i (y[1], y[2]), i (y[2], y[3]), i (y[3], y[1])];
 (%o5)                       [0, 0, 0]
+(%i6) ip(f, g) := integrate(f * g, 'x, a, b)$
+(%i7) gramschmidt([1, sin(x), cos(x)], ip),
+a = -%pi/2, b = %pi/2;
+                                      %pi cos(x) - 2
+(%o7)                     [1, sin(x), --------------]
+                                           %pi
 @end example
 
 @end deffn
diff -Naur maxima-cvs/share/matrix/eigen.mac maxima/share/matrix/eigen.mac
--- maxima-cvs/share/matrix/eigen.mac	2007-10-28 17:45:10.000000000 -0700
+++ maxima/share/matrix/eigen.mac	2007-10-28 20:06:23.000000000 -0700
@@ -58,17 +58,21 @@
 columnvector(x):=transpose(matrix(x))$
 
 
-gramschmidt(x):=
+gramschmidt(x,[inprod]):=
 		block([listarith,dimnsn,listall,intern,count,denom,unit,index1,
 		index2],
+                local(inprod),
+                /* flattening required by `gschmit' alias */
+                if inprod=[] or inprod=[[]] then inprod:innerproduct
+                else inprod:first(flatten(inprod)),
                 mode_declare([dimnsn,count,index1,index2],fixnum,
                               [listall],list,[intern,denom,unit],any),
 		listarith:true,dimnsn:mi(length(x)),listall:[part(x,1)],
 		count:1,if dimnsn=1 then return(x)
 		else (for index1:2 thru dimnsn do
 		(unit:part(x,index1),for index2 thru count do
-		(intern:part(listall,index2),denom:innerproduct(intern,intern),
-		unit:factor(ratsimp(unit-innerproduct(intern,unit)*intern/denom
+		(intern:part(listall,index2),denom:inprod(intern,intern),
+		unit:factor(ratsimp(unit-inprod(intern,unit)*intern/denom
 		))),
 		count:count+1,listall:endcons(unit,listall)),
 		return(listall)))$
@@ -179,7 +183,7 @@
 
 covect(x):=columnvector(x)$
 
-gschmit(x):=gramschmidt(x)$
+gschmit(x,[inprod]):=gramschmidt(x,inprod)$
 
 eivals(mat):=eigenvalues(mat)$
 
diff -Naur maxima-cvs/tests/wester_problems/test_vector_analysis.mac maxima/tests/wester_problems/test_vector_analysis.mac
--- maxima-cvs/tests/wester_problems/test_vector_analysis.mac	2007-10-28 17:45:11.000000000 -0700
+++ maxima/tests/wester_problems/test_vector_analysis.mac	2007-10-28 20:06:14.000000000 -0700
@@ -68,5 +68,8 @@
    Company, 1981, p. 104 => [[0 1 2 1], [0 -1 1 -1], [2 1 0 -1]]^T */
 [[0, 1, 2, 1], [0, 1, 3, 1], [1, 1, 1, 0], [1, 3, 6, 2]];
 gramschmidt(%);
+/* Optional inner-product */
+gramschmidt([1,sin(x),cos(x)], lambda([f, g], integrate(f * g, x, a, b))),
+a = -%pi/2, b = %pi/2;
 /* ---------- Quit ---------- */
 quit();