GNU Octave
3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Pages
libinterp
corefcn
mgorth.cc
Go to the documentation of this file.
1
/*
2
3
Copyright (C) 2009-2013 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
#ifdef 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 "
gripes.h
"
32
33
template
<
class
ColumnVector,
class
Matrix,
class
RowVector>
34
static
void
35
do_mgorth
(
ColumnVector
&
x
,
const
Matrix
&
V
,
RowVector
& h)
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
(
real
(h(Vc)) > 0)
48
x = x / h(Vc);
49
}
50
51
DEFUN
(mgorth, args, nargout,
52
"-*- texinfo -*-\n\
53
@deftypefn {Built-in Function} {[@var{y}, @var{h}] =} mgorth (@var{x}, @var{v})\n\
54
Orthogonalize a given column vector @var{x} with respect to a set of\n\
55
orthonormal vectors comprising the columns of @var{v}\n\
56
using the modified Gram-Schmidt method.\n\
57
On exit, @var{y} is a unit vector such that:\n\
58
\n\
59
@example\n\
60
@group\n\
61
norm (@var{y}) = 1\n\
62
@var{v}' * @var{y} = 0\n\
63
@var{x} = [@var{v}, @var{y}]*@var{h}'\n\
64
@end group\n\
65
@end example\n\
66
\n\
67
@end deftypefn"
)
68
{
69
octave_value_list
retval;
70
71
int
nargin = args.
length
();
72
73
if
(nargin != 2 || nargout > 2)
74
{
75
print_usage
();
76
return
retval;
77
}
78
79
octave_value
arg_x = args(0);
80
octave_value
arg_v = args(1);
81
82
if
(arg_v.
ndims
() != 2 || arg_x.
ndims
() != 2 || arg_x.
columns
() != 1
83
|| arg_v.
rows
() != arg_x.
rows
())
84
{
85
error
(
"mgorth: V should be a matrix, and X a column vector with"
86
" the same number of rows as V."
);
87
return
retval;
88
}
89
90
if
(! arg_x.
is_numeric_type
() && ! arg_v.
is_numeric_type
())
91
{
92
error
(
"mgorth: X and V must be numeric"
);
93
}
94
95
bool
iscomplex = (arg_x.
is_complex_type
() || arg_v.
is_complex_type
());
96
if
(arg_x.
is_single_type
() || arg_v.
is_single_type
())
97
{
98
if
(iscomplex)
99
{
100
FloatComplexColumnVector
x
101
= arg_x.
float_complex_column_vector_value
();
102
FloatComplexMatrix
V
= arg_v.
float_complex_matrix_value
();
103
FloatComplexRowVector
h;
104
do_mgorth
(x, V, h);
105
retval(1) = h;
106
retval(0) =
x
;
107
}
108
else
109
{
110
FloatColumnVector
x
= arg_x.
float_column_vector_value
();
111
FloatMatrix
V
= arg_v.
float_matrix_value
();
112
FloatRowVector
h;
113
do_mgorth
(x, V, h);
114
retval(1) = h;
115
retval(0) =
x
;
116
}
117
}
118
else
119
{
120
if
(iscomplex)
121
{
122
ComplexColumnVector
x
= arg_x.
complex_column_vector_value
();
123
ComplexMatrix
V
= arg_v.
complex_matrix_value
();
124
ComplexRowVector
h;
125
do_mgorth
(x, V, h);
126
retval(1) = h;
127
retval(0) =
x
;
128
}
129
else
130
{
131
ColumnVector
x
= arg_x.
column_vector_value
();
132
Matrix
V
= arg_v.
matrix_value
();
133
RowVector
h;
134
do_mgorth
(x, V, h);
135
retval(1) = h;
136
retval(0) =
x
;
137
}
138
}
139
140
return
retval;
141
}
142
143
/*
144
%!test
145
%! for ii=1:100
146
%! assert (abs (mgorth (randn (5, 1), eye (5, 4))), [0 0 0 0 1]', eps);
147
%! endfor
148
149
%!test
150
%! a = hilb (5);
151
%! a(:, 1) /= norm (a(:, 1));
152
%! for ii = 1:5
153
%! a(:, ii) = mgorth (a(:, ii), a(:, 1:ii-1));
154
%! endfor
155
%! assert (a' * a, eye (5), 1e10);
156
*/
Generated on Mon Dec 30 2013 03:04:27 for GNU Octave by
1.8.1.2