Mercurial > octave
annotate libinterp/corefcn/pinv.cc @ 25003:2365c2661b3c stable
doc: Spellcheck documentation ahead of 4.4 release.
* aspell-octave.en.pws: Add new words to Octave-only spelling dictionary.
Remove exception words which are no longer used in manual.
* basics.txi, bugs.txi, func.txi, geometry.txi, install.txi, matrix.txi,
package.txi, plot.txi, poly.txi, preface.txi, quad.txi, sparse.txi,
strings.txi, vectorize.txi, data.cc, defaults.cc, file-io.cc, pinv.cc,
quadcc.cc, qz.cc, rand.cc, schur.cc, syscalls.cc, sysdep.cc, toplev.cc, amd.cc,
audioread.cc, colamd.cc, dmperm.cc, symrcm.cc, quadgk.m, quadl.m, imfinfo.m,
rgb2gray.m, javachk.m, usejava.m, unpack.m, fzero.m, glpk.m, pqpnonneg.m,
stemleaf.m, print.m, polyfit.m, blackman.m, bicgstab.m, cgs.m, eigs.m, pcg.m,
tfqmr.m, gallery.m, rosser.m, toeplitz.m, vander.m, isstrprop.m:
Add @nospell{} macro around proper names and other words which aspell should
not check. Correct misspellings identified by aspell.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 26 Mar 2018 10:45:04 -0700 |
parents | 194eb4bd202b |
children | 6652d3823428 |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
3 Copyright (C) 1996-2017 John W. Eaton |
2928 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24201
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24201
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
10 (at your option) any later version. |
2928 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
15 GNU General Public License for more details. |
2928 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24201
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
2928 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
24 # include "config.h" |
2928 | 25 #endif |
26 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14179
diff
changeset
|
27 #include "defun.h" |
2928 | 28 #include "error.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20941
diff
changeset
|
29 #include "errwarn.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
30 #include "ovl.h" |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
31 #include "ops.h" |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
32 #include "ov-re-diag.h" |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
33 #include "ov-cx-diag.h" |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
34 #include "ov-flt-re-diag.h" |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
35 #include "ov-flt-cx-diag.h" |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
36 #include "ov-perm.h" |
2928 | 37 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14179
diff
changeset
|
38 DEFUN (pinv, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
39 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
40 @deftypefn {} {} pinv (@var{x}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
41 @deftypefnx {} {} pinv (@var{x}, @var{tol}) |
25003
2365c2661b3c
doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
42 Return the @nospell{Moore-Penrose} pseudoinverse of @var{x}. |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
43 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
44 Singular values less than @var{tol} are ignored. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
45 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
46 If the second argument is omitted, it is taken to be |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
47 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
48 @example |
24197
63fc7a75a3d7
doc: Update docstring for pinv and inv.
Rik <rik@octave.org>
parents:
23219
diff
changeset
|
49 tol = max ([rows(@var{x}), columns(@var{x})]) * norm (@var{x}) * eps |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
50 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
51 |
24200
140f1403d603
doc: Update docstring for pinv.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
24197
diff
changeset
|
52 @seealso{inv, ldivide} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
53 @end deftypefn */) |
2928 | 54 { |
55 int nargin = args.length (); | |
56 | |
57 if (nargin < 1 || nargin > 2) | |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
58 print_usage (); |
2928 | 59 |
60 octave_value arg = args(0); | |
61 | |
23577
80c42f4cca13
maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
62 if (arg.isempty ()) |
21127
df7891224709
maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
63 return ovl (Matrix ()); |
2928 | 64 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
65 octave_value retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
66 |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
67 bool isfloat = arg.is_single_type (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
68 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
69 if (arg.is_diag_matrix ()) |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
70 { |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
71 if (isfloat) |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
72 { |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
73 float tol = 0.0; |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
74 if (nargin == 2) |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
75 tol = args(1).float_value (); |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
76 |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
77 if (tol < 0.0) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20802
diff
changeset
|
78 error ("pinv: TOL must be greater than zero"); |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
79 |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
80 if (arg.isreal ()) |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
81 retval = arg.float_diag_matrix_value ().pseudo_inverse (tol); |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
82 else |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
83 retval = arg.float_complex_diag_matrix_value ().pseudo_inverse (tol); |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
84 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
85 else |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
86 { |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
87 double tol = 0.0; |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
88 if (nargin == 2) |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
89 tol = args(1).double_value (); |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
90 |
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
91 if (tol < 0.0) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20802
diff
changeset
|
92 error ("pinv: TOL must be greater than zero"); |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
93 |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
94 if (arg.isreal ()) |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
95 retval = arg.diag_matrix_value ().pseudo_inverse (tol); |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
96 else |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
97 retval = arg.complex_diag_matrix_value ().pseudo_inverse (tol); |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
98 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
99 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
100 else if (arg.is_perm_matrix ()) |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
101 { |
8960
93f18f166aba
remove float perm matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
102 retval = arg.perm_matrix_value ().inverse (); |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
103 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
104 else if (isfloat) |
2928 | 105 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
106 float tol = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
107 if (nargin == 2) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
108 tol = args(1).float_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
109 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
110 if (tol < 0.0) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20802
diff
changeset
|
111 error ("pinv: TOL must be greater than zero"); |
2928 | 112 |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
113 if (arg.isreal ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
114 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
115 FloatMatrix m = arg.float_matrix_value (); |
2928 | 116 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
117 retval = m.pseudo_inverse (tol); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
118 } |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
119 else if (arg.iscomplex ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
120 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
121 FloatComplexMatrix m = arg.float_complex_matrix_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
122 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
123 retval = m.pseudo_inverse (tol); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
124 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
125 else |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20941
diff
changeset
|
126 err_wrong_type_arg ("pinv", arg); |
2928 | 127 } |
128 else | |
129 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
130 double tol = 0.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
131 if (nargin == 2) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
132 tol = args(1).double_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
133 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
134 if (tol < 0.0) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20802
diff
changeset
|
135 error ("pinv: TOL must be greater than zero"); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
136 |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
137 if (arg.isreal ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
138 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
139 Matrix m = arg.matrix_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
140 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
141 retval = m.pseudo_inverse (tol); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
142 } |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
143 else if (arg.iscomplex ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
144 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
145 ComplexMatrix m = arg.complex_matrix_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
146 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
147 retval = m.pseudo_inverse (tol); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
148 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
149 else |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20941
diff
changeset
|
150 err_wrong_type_arg ("pinv", arg); |
2928 | 151 } |
152 | |
153 return retval; | |
154 } | |
13042
ca7aaf2689c3
codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12642
diff
changeset
|
155 |
ca7aaf2689c3
codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12642
diff
changeset
|
156 /* |
13079
9512d7272d5b
codesprint: Fix tolerance for pinv test
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
13042
diff
changeset
|
157 %!shared a, b, tol, hitol, d, u, x, y |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
15195
diff
changeset
|
158 %! a = reshape (rand*[1:16], 4, 4); # Rank 2 matrix |
13042
ca7aaf2689c3
codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12642
diff
changeset
|
159 %! b = pinv (a); |
14179
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
160 %! tol = 4e-14; |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
161 %! hitol = 40*sqrt (eps); |
13042
ca7aaf2689c3
codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12642
diff
changeset
|
162 %! d = diag ([rand, rand, hitol, hitol]); |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
15195
diff
changeset
|
163 %! u = rand (4); # Could be singular by freak accident |
13042
ca7aaf2689c3
codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12642
diff
changeset
|
164 %! x = inv (u)*d*u; |
14179
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
165 %! y = pinv (x, sqrt (eps)); |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
166 %! |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
167 %!assert (a*b*a, a, tol) |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
168 %!assert (b*a*b, b, tol) |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
169 %!assert ((b*a)', b*a, tol) |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
170 %!assert ((a*b)', a*b, tol) |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
171 %!assert (x*y*x, x, -hitol) |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
172 %!assert (y*x*y, y, -hitol) |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
173 %!assert ((x*y)', x*y, hitol) |
a7a020cd6106
Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
174 %!assert ((y*x)', y*x, hitol) |
18494 | 175 |
176 ## Clear shared variables | |
177 %!shared | |
178 | |
179 ## Test pinv for Diagonal matrices | |
180 %!test | |
181 %! x = diag ([3 2 1 0 -0.5]); | |
182 %! y = pinv (x); | |
183 %! assert (typeinfo (y)(1:8), "diagonal"); | |
184 %! assert (isa (y, "double")); | |
185 %! assert (diag (y), [1/3, 1/2, 1, 0 1/-0.5]'); | |
186 %! y = pinv (x, 1); | |
187 %! assert (diag (y), [1/3 1/2 1 0 0]'); | |
188 %! y = pinv (x, 2); | |
189 %! assert (diag (y), [1/3 1/2 0 0 0]'); | |
190 | |
24195
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
191 ## Test special case of 0 scalars and vectors |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
192 %!assert (pinv (0), 0) |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
193 %!assert (pinv ([0, 0, 0]), [0; 0; 0]) |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
194 %!assert (pinv (single (0)), single (0)) |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
195 %!assert (pinv (single ([0, 0, 0])), single ([0; 0; 0])) |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
196 %!assert (pinv (complex (0,0)), 0) |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
197 %!assert (pinv (complex ([0,0,0], [0,0,0])), [0; 0; 0]) |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
198 %!assert (pinv (complex (single (0),0)), single (0)) |
d3dc76efb38b
Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
199 %!assert (pinv (complex (single ([0,0,0]), [0,0,0])), single ([0; 0; 0])) |
13042
ca7aaf2689c3
codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12642
diff
changeset
|
200 */ |