]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/housh.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / housh.m
1 ## Copyright (C) 1995-2012 A. Scottedward Hodel
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{housv}, @var{beta}, @var{zer}] =} housh (@var{x}, @var{j}, @var{z})
21 ## Compute Householder reflection vector @var{housv} to reflect @var{x}
22 ## to be the j-th column of identity, i.e.,
23 ##
24 ## @example
25 ## @group
26 ## (I - beta*housv*housv')x =  norm(x)*e(j) if x(j) < 0,
27 ## (I - beta*housv*housv')x = -norm(x)*e(j) if x(j) >= 0
28 ## @end group
29 ## @end example
30 ##
31 ## @noindent
32 ## Inputs
33 ##
34 ## @table @var
35 ## @item x
36 ## vector
37 ##
38 ## @item j
39 ## index into vector
40 ##
41 ## @item z
42 ## threshold for zero  (usually should be the number 0)
43 ## @end table
44 ##
45 ## @noindent
46 ## Outputs (see Golub and Van Loan):
47 ##
48 ## @table @var
49 ## @item beta
50 ## If beta = 0, then no reflection need be applied (zer set to 0)
51 ##
52 ## @item housv
53 ## householder vector
54 ## @end table
55 ## @end deftypefn
56
57 ## Author: A. S. Hodel
58 ## Created: August 1995
59
60 function [housv, beta, zer] = housh (x, j, z)
61
62   if (nargin != 3)
63     print_usage ();
64   endif
65
66   ## Check for valid inputs.
67   if (! isvector (x) && ! isscalar (x))
68     error ("housh: first input must be a vector");
69   elseif (! isscalar(j))
70     error ("housh: second argment must be an integer scalar");
71   else
72     housv = x;
73     m = max (abs (housv));
74     if (m != 0.0)
75       housv = housv / m;
76       alpha = norm (housv);
77       if (alpha > z)
78         beta = 1.0 / (alpha * (alpha + abs (housv(j))));
79         sg = sign (housv(j));
80         if (sg == 0)
81           sg = 1;
82         endif
83         housv(j) = housv(j) + alpha*sg;
84       else
85         beta = 0.0;
86       endif
87     else
88       beta = 0.0;
89     endif
90     zer = (beta == 0);
91   endif
92
93 endfunction
94
95 %!test
96 %! x = [1 2 3]';
97 %! j = 3;
98 %! [hv, b, z] = housh(x, j, 0);
99 %! r = (eye(3) - b*hv*hv') * x;
100 %! d = - norm(x) * [0 0 1]';
101 %! assert(r, d, 2e-8);
102 %! assert(z, 0, 2e-8);
103
104 %!test
105 %! x = [7 -3 1]';
106 %! j = 2;
107 %! [hv, b, z] = housh(x, j, 0);
108 %! r = (eye(3) - b*hv*hv') * x;
109 %! d = norm(x) * [0 1 0]';
110 %! assert(r, d, 2e-8);
111 %! assert(z, 0, 2e-8);
112
113 %!test
114 %! x = [1 0 0]';
115 %! j = 1;
116 %! [hv, b, z] = housh(x, j, 10);
117 %! r = (eye(3) - b*hv*hv') * x;
118 %! d = norm(x) * [1 0 0]';
119 %! assert(r, d, 2e-8);
120 %! assert(z, 1, 2e-8);
121
122 %!test
123 %! x = [5 0 4 1]';
124 %! j = 2;
125 %! [hv, b, z] = housh(x, j, 0);
126 %! r = (eye(4) - b*hv*hv') * x;
127 %! d = - norm(x) * [0 1 0 0]';
128 %! assert(r, d, 2e-8);
129 %! assert(z, 0, 2e-8);
130
131 %!error housh([0]);
132 %!error housh();
133