GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mgorth.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2017 Carlo de Falco
4 Copyright (C) 2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "oct-norm.h"
29 #include "defun.h"
30 #include "error.h"
31 #include "errwarn.h"
32 
33 template <typename ColumnVector, typename Matrix, typename RowVector>
34 static void
36 {
37  octave_idx_type Vc = V.columns ();
38  h = RowVector (Vc + 1);
39  for (octave_idx_type j = 0; j < Vc; j++)
40  {
41  ColumnVector Vcj = V.column (j);
42  h(j) = RowVector (Vcj.hermitian ()) * x;
43  x -= h(j) * Vcj;
44  }
45 
46  h(Vc) = xnorm (x);
47  if (octave::math::real (h(Vc)) > 0)
48  x /= h(Vc);
49 }
50 
51 DEFUN (mgorth, args, ,
52  doc: /* -*- texinfo -*-
53 @deftypefn {} {[@var{y}, @var{h}] =} mgorth (@var{x}, @var{v})
54 Orthogonalize a given column vector @var{x} with respect to a set of
55 orthonormal vectors comprising the columns of @var{v} using the modified
56 Gram-Schmidt method.
57 
58 On exit, @var{y} is a unit vector such that:
59 
60 @example
61 @group
62  norm (@var{y}) = 1
63  @var{v}' * @var{y} = 0
64  @var{x} = [@var{v}, @var{y}]*@var{h}'
65 @end group
66 @end example
67 
68 @end deftypefn */)
69 {
70  if (args.length () != 2)
71  print_usage ();
72 
73  octave_value arg_x = args(0);
74  octave_value arg_v = args(1);
75 
76  if (arg_v.ndims () != 2 || arg_x.ndims () != 2 || arg_x.columns () != 1
77  || arg_v.rows () != arg_x.rows ())
78  error ("mgorth: V should be a matrix, and X a column vector with"
79  " the same number of rows as V.");
80 
81  if (! arg_x.is_numeric_type () && ! arg_v.is_numeric_type ())
82  error ("mgorth: X and V must be numeric");
83 
85 
86  bool iscomplex = (arg_x.is_complex_type () || arg_v.is_complex_type ());
87  if (arg_x.is_single_type () || arg_v.is_single_type ())
88  {
89  if (iscomplex)
90  {
95  do_mgorth (x, V, h);
96  retval = ovl (x, h);
97  }
98  else
99  {
101  FloatMatrix V = arg_v.float_matrix_value ();
103  do_mgorth (x, V, h);
104  retval = ovl (x, h);
105  }
106  }
107  else
108  {
109  if (iscomplex)
110  {
114  do_mgorth (x, V, h);
115  retval = ovl (x, h);
116  }
117  else
118  {
120  Matrix V = arg_v.matrix_value ();
121  RowVector h;
122  do_mgorth (x, V, h);
123  retval = ovl (x, h);
124  }
125  }
126 
127  return retval;
128 }
129 
130 /*
131 %!test
132 %! for ii=1:100
133 %! assert (abs (mgorth (randn (5, 1), eye (5, 4))), [0 0 0 0 1]', eps);
134 %! endfor
135 
136 %!test
137 %! a = hilb (5);
138 %! a(:, 1) /= norm (a(:, 1));
139 %! for ii = 1:5
140 %! a(:, ii) = mgorth (a(:, ii), a(:, 1:ii-1));
141 %! endfor
142 %! assert (a' * a, eye (5), 1e10);
143 */
ColumnVector column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
Definition: ov.cc:1766
F77_RET_T const F77_INT const F77_INT const F77_INT const F77_DBLE const F77_DBLE F77_INT F77_DBLE * V
int ndims(void) const
Definition: ov.h:495
octave_idx_type rows(void) const
Definition: ov.h:489
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:809
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
FloatColumnVector float_column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
Definition: ov.cc:1959
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
bool is_numeric_type(void) const
Definition: ov.h:679
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
static void do_mgorth(ColumnVector &x, const Matrix &V, RowVector &h)
Definition: mgorth.cc:35
double real(double x)
Definition: lo-mappers.h:113
JNIEnv void * args
Definition: ov-java.cc:67
double h
Definition: graphics.cc:11205
octave_idx_type columns(void) const
Definition: ov.h:491
bool is_complex_type(void) const
Definition: ov.h:670
octave_value retval
Definition: data.cc:6294
OCTAVE_API double xnorm(const ColumnVector &x, double p)
Definition: oct-norm.cc:549
Definition: dMatrix.h:37
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
MArray< T > hermitian(T(*fcn)(const T &)=0) const
Definition: MArray.h:106
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:805
FloatComplexColumnVector float_complex_column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
Definition: ov.cc:1967
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:790
ColumnVector column(octave_idx_type i) const
Definition: dMatrix.cc:423
ComplexColumnVector complex_column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
Definition: ov.cc:1774
bool is_single_type(void) const
Definition: ov.h:627
octave_idx_type columns(void) const
Definition: Array.h:410
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x