GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
mgorth.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2009-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "oct-norm.h"
31 #include "defun.h"
32 #include "error.h"
33 #include "errwarn.h"
34 
35 template <typename ColumnVector, typename Matrix, typename RowVector>
36 static void
38 {
39  octave_idx_type Vc = V.columns ();
40  h = RowVector (Vc + 1);
41  for (octave_idx_type j = 0; j < Vc; j++)
42  {
43  ColumnVector Vcj = V.column (j);
44  h(j) = RowVector (Vcj.hermitian ()) * x;
45  x -= h(j) * Vcj;
46  }
47 
48  h(Vc) = xnorm (x);
49  if (std::real (h(Vc)) > 0)
50  x /= h(Vc);
51 }
52 
53 DEFUN (mgorth, args, ,
54  doc: /* -*- texinfo -*-
55 @deftypefn {} {[@var{y}, @var{h}] =} mgorth (@var{x}, @var{v})
56 Orthogonalize a given column vector @var{x} with respect to a set of
57 orthonormal vectors comprising the columns of @var{v} using the modified
58 Gram-Schmidt method.
59 
60 On exit, @var{y} is a unit vector such that:
61 
62 @example
63 @group
64  norm (@var{y}) = 1
65  @var{v}' * @var{y} = 0
66  @var{x} = [@var{v}, @var{y}]*@var{h}'
67 @end group
68 @end example
69 
70 @end deftypefn */)
71 {
72  if (args.length () != 2)
73  print_usage ();
74 
75  octave_value arg_x = args(0);
76  octave_value arg_v = args(1);
77 
78  if (arg_v.ndims () != 2 || arg_x.ndims () != 2 || arg_x.columns () != 1
79  || arg_v.rows () != arg_x.rows ())
80  error ("mgorth: V should be a matrix, and X a column vector with"
81  " the same number of rows as V.");
82 
83  if (! arg_x.isnumeric () && ! arg_v.isnumeric ())
84  error ("mgorth: X and V must be numeric");
85 
87 
88  bool iscomplex = (arg_x.iscomplex () || arg_v.iscomplex ());
89  if (arg_x.is_single_type () || arg_v.is_single_type ())
90  {
91  if (iscomplex)
92  {
97  do_mgorth (x, V, h);
98  retval = ovl (x, h);
99  }
100  else
101  {
103  FloatMatrix V = arg_v.float_matrix_value ();
104  FloatRowVector h;
105  do_mgorth (x, V, h);
106  retval = ovl (x, h);
107  }
108  }
109  else
110  {
111  if (iscomplex)
112  {
116  do_mgorth (x, V, h);
117  retval = ovl (x, h);
118  }
119  else
120  {
122  Matrix V = arg_v.matrix_value ();
123  RowVector h;
124  do_mgorth (x, V, h);
125  retval = ovl (x, h);
126  }
127  }
128 
129  return retval;
130 }
131 
132 /*
133 %!test
134 %! for ii=1:100
135 %! assert (abs (mgorth (randn (5, 1), eye (5, 4))), [0 0 0 0 1]', eps);
136 %! endfor
137 
138 %!test
139 %! a = hilb (5);
140 %! a(:, 1) /= norm (a(:, 1));
141 %! for ii = 1:5
142 %! a(:, ii) = mgorth (a(:, ii), a(:, 1:ii-1));
143 %! endfor
144 %! assert (a' * a, eye (5), 1e10);
145 */
MArray< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: MArray.h:108
Definition: dMatrix.h:42
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:824
octave_idx_type rows(void) const
Definition: ov.h:504
bool isnumeric(void) const
Definition: ov.h:703
FloatColumnVector float_column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
octave_idx_type columns(void) const
Definition: ov.h:506
int ndims(void) const
Definition: ov.h:510
FloatComplexColumnVector float_complex_column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:809
ComplexColumnVector complex_column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
bool is_single_type(void) const
Definition: ov.h:651
ColumnVector column_vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:806
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:828
bool iscomplex(void) const
Definition: ov.h:694
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:137
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:968
F77_RET_T const F77_INT const F77_INT const F77_INT const F77_DBLE const F77_DBLE F77_INT F77_DBLE * V
F77_RET_T const F77_DBLE * x
static void do_mgorth(ColumnVector &x, const Matrix &V, RowVector &h)
Definition: mgorth.cc:37
OCTAVE_API double xnorm(const ColumnVector &x, double p)
Definition: oct-norm.cc:551
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211