mgorth.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 Carlo de Falco
00004 Copyright (C) 2010 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include "oct-norm.h"
00029 #include "defun-dld.h"
00030 #include "error.h"
00031 #include "gripes.h"
00032 
00033 template <class ColumnVector, class Matrix, class RowVector>
00034 static void
00035 do_mgorth (ColumnVector& x, const Matrix& V, RowVector& h)
00036 {
00037   octave_idx_type Vc = V.columns ();
00038   h = RowVector (Vc + 1);
00039   for (octave_idx_type j = 0; j < Vc; j++)
00040     {
00041       ColumnVector Vcj = V.column (j);
00042       h(j) = RowVector (Vcj.hermitian ()) * x;
00043       x -= h(j) * Vcj;
00044     }
00045 
00046   h(Vc) = xnorm (x);
00047   if (real (h(Vc)) > 0)
00048     x = x / h(Vc);
00049 }
00050 
00051 DEFUN_DLD (mgorth, args, nargout,
00052   "-*- texinfo -*-\n\
00053 @deftypefn {Loadable Function} {[@var{y}, @var{h}] =} mgorth (@var{x}, @var{v})\n\
00054 Orthogonalize a given column vector @var{x} with respect to a given\n\
00055 orthonormal basis @var{v} using a modified Gram-Schmidt orthogonalization.  \n\
00056 On exit, @var{y} is a unit vector such that:\n\
00057 \n\
00058 @example\n\
00059 @group\n\
00060   norm (@var{y}) = 1\n\
00061   @var{v}' * @var{y} = 0\n\
00062   @var{x} = @var{h}*[@var{v}, @var{y}]\n\
00063 @end group\n\
00064 @end example\n\
00065 \n\
00066 @end deftypefn")
00067 {
00068   octave_value_list retval;
00069 
00070   int nargin = args.length();
00071 
00072   if (nargin != 2 || nargout > 2)
00073   {
00074     print_usage ();
00075     return retval;
00076   }
00077 
00078   octave_value arg_x = args(0);
00079   octave_value arg_v = args(1);
00080 
00081   if (arg_v.ndims () != 2 || arg_x.ndims () != 2 || arg_x.columns () != 1
00082       || arg_v.rows () != arg_x.rows ())
00083     {
00084       error ("mgorth: V should me a matrix, and X a column vector with"
00085              " the same number of rows as V.");
00086       return retval;
00087     }
00088 
00089   if (! arg_x.is_numeric_type () && ! arg_v.is_numeric_type ())
00090     {
00091       error ("mgorth: X and V must be numeric");
00092     }
00093 
00094   bool iscomplex = (arg_x.is_complex_type () || arg_v.is_complex_type ());
00095   if (arg_x.is_single_type () || arg_v.is_single_type ())
00096     {
00097       if (iscomplex)
00098         {
00099           FloatComplexColumnVector x = arg_x.float_complex_column_vector_value ();
00100           FloatComplexMatrix V = arg_v.float_complex_matrix_value ();
00101           FloatComplexRowVector h;
00102           do_mgorth (x, V, h);
00103           retval(1) = h;
00104           retval(0) = x;
00105         }
00106       else
00107         {
00108           FloatColumnVector x = arg_x.float_column_vector_value ();
00109           FloatMatrix V = arg_v.float_matrix_value ();
00110           FloatRowVector h;
00111           do_mgorth (x, V, h);
00112           retval(1) = h;
00113           retval(0) = x;
00114         }
00115     }
00116   else
00117     {
00118       if (iscomplex)
00119         {
00120           ComplexColumnVector x = arg_x.complex_column_vector_value ();
00121           ComplexMatrix V = arg_v.complex_matrix_value ();
00122           ComplexRowVector h;
00123           do_mgorth (x, V, h);
00124           retval(1) = h;
00125           retval(0) = x;
00126         }
00127       else
00128         {
00129           ColumnVector x = arg_x.column_vector_value ();
00130           Matrix V = arg_v.matrix_value ();
00131           RowVector h;
00132           do_mgorth (x, V, h);
00133           retval(1) = h;
00134           retval(0) = x;
00135         }
00136     }
00137 
00138   return retval;
00139 }
00140 
00141 /*
00142 
00143 %!test
00144 %! for ii=1:100; assert (abs (mgorth (randn (5, 1), eye (5, 4))), [0 0 0 0 1]', eps); endfor
00145 
00146 %!test
00147 %! a = hilb (5);
00148 %! a(:, 1) /= norm (a(:, 1));
00149 %! for ii = 1:5
00150 %!   a(:, ii) = mgorth (a(:, ii), a(:, 1:ii-1));
00151 %! endfor
00152 %! assert (a' * a, eye (5), 1e10);
00153 
00154 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines