Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/housh.m @ 17521:c3a3532e3d98
linsolve.m: Add new function for Matlab compatibility.
* scripts/linear-algebra/linsolve.m: New function.
* scripts/linear-algebra/module.mk: Add linsolve.m to build system.
* NEWS: Announce new function.
author | Nir Krakauer < nkrakauer@ccny.cuny.edu> |
---|---|
date | Thu, 26 Sep 2013 08:30:26 -0700 |
parents | a4969508008e |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13048
diff
changeset
|
1 ## Copyright (C) 1995-2012 A. Scottedward Hodel |
3427 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
7016 | 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. | |
3427 | 9 ## |
7016 | 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. | |
3427 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3211 | 18 |
3439 | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {[@var{housv}, @var{beta}, @var{zer}] =} housh (@var{x}, @var{j}, @var{z}) | |
7201 | 21 ## Compute Householder reflection vector @var{housv} to reflect @var{x} |
8494 | 22 ## to be the j-th column of identity, i.e., |
7201 | 23 ## |
24 ## @example | |
25 ## @group | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
26 ## (I - beta*housv*housv')x = norm (x)*e(j) if x(j) < 0, |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
27 ## (I - beta*housv*housv')x = -norm (x)*e(j) if x(j) >= 0 |
7201 | 28 ## @end group |
29 ## @end example | |
30 ## | |
31 ## @noindent | |
32 ## Inputs | |
33 ## | |
34 ## @table @var | |
35 ## @item x | |
36 ## vector | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
37 ## |
7201 | 38 ## @item j |
39 ## index into vector | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
40 ## |
7201 | 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 | |
16826
a4969508008e
doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
50 ## If beta = 0, then no reflection need be applied (@nospell{zer} set to 0) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
51 ## |
7201 | 52 ## @item housv |
53 ## householder vector | |
54 ## @end table | |
3439 | 55 ## @end deftypefn |
3211 | 56 |
3427 | 57 ## Author: A. S. Hodel |
3439 | 58 ## Created: August 1995 |
3427 | 59 |
60 function [housv, beta, zer] = housh (x, j, z) | |
3211 | 61 |
7125 | 62 if (nargin != 3) |
63 print_usage (); | |
64 endif | |
65 | |
66 ## Check for valid inputs. | |
67 if (! isvector (x) && ! isscalar (x)) | |
8664 | 68 error ("housh: first input must be a vector"); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
69 elseif (! isscalar (j)) |
8664 | 70 error ("housh: second argment must be an integer scalar"); |
3211 | 71 else |
72 housv = x; | |
3427 | 73 m = max (abs (housv)); |
74 if (m != 0.0) | |
75 housv = housv / m; | |
76 alpha = norm (housv); | |
3426 | 77 if (alpha > z) |
3427 | 78 beta = 1.0 / (alpha * (alpha + abs (housv(j)))); |
79 sg = sign (housv(j)); | |
80 if (sg == 0) | |
3211 | 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 | |
3427 | 92 |
3211 | 93 endfunction |
13048
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
94 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 |
13048
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
96 %!test |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
97 %! x = [1 2 3]'; |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
98 %! j = 3; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
99 %! [hv, b, z] = housh (x, j, 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
100 %! r = (eye (3) - b*hv*hv') * x; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 %! d = - norm (x) * [0 0 1]'; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %! assert (r, d, 2e-8); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 %! assert (z, 0, 2e-8); |
13048
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
104 |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
105 %!test |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
106 %! x = [7 -3 1]'; |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
107 %! j = 2; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
108 %! [hv, b, z] = housh (x, j, 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
109 %! r = (eye (3) - b*hv*hv') * x; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
110 %! d = norm (x) * [0 1 0]'; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
111 %! assert (r, d, 2e-8); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
112 %! assert (z, 0, 2e-8); |
13048
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
113 |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
114 %!test |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
115 %! x = [1 0 0]'; |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
116 %! j = 1; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
117 %! [hv, b, z] = housh (x, j, 10); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
118 %! r = (eye (3) - b*hv*hv') * x; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
119 %! d = norm (x) * [1 0 0]'; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
120 %! assert (r, d, 2e-8); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
121 %! assert (z, 1, 2e-8); |
13048
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
122 |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
123 %!test |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
124 %! x = [5 0 4 1]'; |
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
125 %! j = 2; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
126 %! [hv, b, z] = housh (x, j, 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
127 %! r = (eye (4) - b*hv*hv') * x; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
128 %! d = - norm (x) * [0 1 0 0]'; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
129 %! assert (r, d, 2e-8); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
130 %! assert (z, 0, 2e-8); |
13048
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
131 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
132 %!error housh ([0]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
133 %!error housh () |
13048
c5c94b63931f
codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents:
11523
diff
changeset
|
134 |