annotate src/DLD-FUNCTIONS/inv.cc @ 8377:25bc2d31e1bf

improve OCTAVE_LOCAL_BUFFER
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 29 Oct 2008 16:52:10 +0100
parents c3f7e2549abb
children a2878ba31a9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
1 /*
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
2
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
3 Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
4 2007 John W. Eaton
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
5
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
7
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6207
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6207
diff changeset
11 option) any later version.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
12
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
16 for more details.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
17
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6207
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6207
diff changeset
20 <http://www.gnu.org/licenses/>.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
21
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
22 */
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
23
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25 #include <config.h>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
26 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
27
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
28 #include "defun-dld.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
29 #include "error.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
30 #include "gripes.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
31 #include "oct-obj.h"
8366
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
32 #include "ops.h"
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
33 #include "ov-re-diag.h"
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
34 #include "ov-cx-diag.h"
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
35 #include "ov-flt-re-diag.h"
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
36 #include "ov-flt-cx-diag.h"
8371
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
37 #include "ov-perm.h"
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
38 #include "ov-flt-perm.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
39 #include "utils.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
40
3808
885b296ef83a [project @ 2001-03-27 19:12:58 by jwe]
jwe
parents: 3548
diff changeset
41 DEFUN_DLD (inv, args, nargout,
3548
ab7fa5a8f23f [project @ 2000-02-03 01:17:15 by jwe]
jwe
parents: 3458
diff changeset
42 "-*- texinfo -*-\n\
7650
eb7bdde776f2 Texinfo fixes
John W. Eaton <jwe@octave.org>
parents: 7515
diff changeset
43 @deftypefn {Loadable Function} {[@var{x}, @var{rcond}] =} inv (@var{a})\n\
eb7bdde776f2 Texinfo fixes
John W. Eaton <jwe@octave.org>
parents: 7515
diff changeset
44 @deftypefnx {Loadable Function} {[@var{x}, @var{rcond}] =} inverse (@var{a})\n\
3808
885b296ef83a [project @ 2001-03-27 19:12:58 by jwe]
jwe
parents: 3548
diff changeset
45 Compute the inverse of the square matrix @var{a}. Return an estimate\n\
885b296ef83a [project @ 2001-03-27 19:12:58 by jwe]
jwe
parents: 3548
diff changeset
46 of the reciprocal condition number if requested, otherwise warn of an\n\
885b296ef83a [project @ 2001-03-27 19:12:58 by jwe]
jwe
parents: 3548
diff changeset
47 ill-conditioned matrix if the reciprocal condition number is small.\n\
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
48 \n\
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
49 If called with a sparse matrix, then in general @var{x} will be a full\n\
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
50 matrix, and so if possible forming the inverse of a sparse matrix should\n\
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
51 be avoided. It is significantly more accurate and faster to do\n\
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
52 @code{@var{y} = @var{a} \\ @var{b}}, rather than\n\
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
53 @code{@var{y} = inv (@var{a}) * @var{b}}.\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3325
diff changeset
54 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
55 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
56 octave_value_list retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
57
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
58 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
59
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
60 if (nargin != 1)
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
61 {
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
62 print_usage ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
63 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
64 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
65
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
66 octave_value arg = args(0);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
67
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5064
diff changeset
68 octave_idx_type nr = arg.rows ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5064
diff changeset
69 octave_idx_type nc = arg.columns ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
70
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
71 int arg_is_empty = empty_arg ("inverse", nr, nc);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
72
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
73 if (arg_is_empty < 0)
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
74 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
75 else if (arg_is_empty > 0)
4233
ccfdb55c8156 [project @ 2002-12-20 22:43:54 by jwe]
jwe
parents: 3808
diff changeset
76 return octave_value (Matrix ());
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
77
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
78 if (nr != nc)
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
79 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
80 gripe_square_matrix_required ("inverse");
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
81 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
82 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
83
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
84 octave_value result;
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
85 octave_idx_type info;
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
86 double rcond = 0.0;
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
87 float frcond = 0.0;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
88 bool isfloat = arg.is_single_type ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
89
8366
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
90 if (arg.is_diag_matrix ())
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
91 {
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
92 rcond = 1.0;
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
93 frcond = 1.0f;
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
94 const octave_base_value& a = arg.get_rep ();
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
95 if (arg.is_complex_type ())
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
96 {
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
97 if (isfloat)
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
98 {
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
99 CAST_CONV_ARG (const octave_float_complex_diag_matrix&);
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
100 result = v.float_complex_diag_matrix_value ().inverse (info);
8371
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
101 if (nargout > 1)
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
102 frcond = v.float_complex_diag_matrix_value ().rcond ();
8366
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
103 }
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
104 else
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
105 {
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
106 CAST_CONV_ARG (const octave_complex_diag_matrix&);
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
107 result = v.complex_diag_matrix_value ().inverse (info);
8371
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
108 if (nargout > 1)
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
109 rcond = v.complex_diag_matrix_value ().rcond ();
8366
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
110 }
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
111 }
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
112 else
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
113 {
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
114 if (isfloat)
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
115 {
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
116 CAST_CONV_ARG (const octave_float_diag_matrix&);
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
117 result = v.float_diag_matrix_value ().inverse (info);
8371
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
118 if (nargout > 1)
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
119 frcond = v.float_diag_matrix_value ().rcond ();
8366
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
120 }
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
121 else
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
122 {
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
123 CAST_CONV_ARG (const octave_diag_matrix&);
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
124 result = v.diag_matrix_value ().inverse (info);
8371
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
125 if (nargout > 1)
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
126 rcond = v.diag_matrix_value ().rcond ();
8366
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
127 }
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
128 }
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
129 }
8371
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
130 else if (arg.is_perm_matrix ())
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
131 {
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
132 rcond = 1.0;
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
133 frcond = 1.0f;
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
134 info = 0;
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
135 const octave_base_value& a = arg.get_rep ();
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
136 if (isfloat)
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
137 {
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
138 CAST_CONV_ARG (const octave_float_perm_matrix&);
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
139 result = v.perm_matrix_value ().inverse ();
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
140 }
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
141 else
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
142 {
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
143 CAST_CONV_ARG (const octave_perm_matrix&);
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
144 result = v.perm_matrix_value ().inverse ();
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
145 }
c3f7e2549abb make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8366
diff changeset
146 }
8366
8b1a2555c4e2 implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7911
diff changeset
147 else if (isfloat)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
148 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
149 if (arg.is_real_type ())
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
150 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
151 FloatMatrix m = arg.float_matrix_value ();
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
152 if (! error_state)
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
153 {
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
154 MatrixType mattyp = args(0).matrix_type ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
155 result = m.inverse (mattyp, info, frcond, 1);
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
156 args(0).matrix_type (mattyp);
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
157 }
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
158 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
159 else if (arg.is_complex_type ())
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
160 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
161 FloatComplexMatrix m = arg.float_complex_matrix_value ();
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
162 if (! error_state)
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
163 {
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
164 MatrixType mattyp = args(0).matrix_type ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
165 result = m.inverse (mattyp, info, frcond, 1);
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
166 args(0).matrix_type (mattyp);
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
167 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
168 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
169 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
170 else
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
171 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
172 if (arg.is_real_type ())
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
173 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
174 if (arg.is_sparse_type ())
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
175 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
176 SparseMatrix m = arg.sparse_matrix_value ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
177 if (! error_state)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
178 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
179 MatrixType mattyp = args(0).matrix_type ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
180 result = m.inverse (mattyp, info, rcond, 1);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
181 args(0).matrix_type (mattyp);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
182 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
183 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
184 else
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
185 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
186 Matrix m = arg.matrix_value ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
187 if (! error_state)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
188 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
189 MatrixType mattyp = args(0).matrix_type ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
190 result = m.inverse (mattyp, info, rcond, 1);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
191 args(0).matrix_type (mattyp);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
192 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
193 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
194 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
195 else if (arg.is_complex_type ())
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
196 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
197 if (arg.is_sparse_type ())
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
198 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
199 SparseComplexMatrix m = arg.sparse_complex_matrix_value ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
200 if (! error_state)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
201 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
202 MatrixType mattyp = args(0).matrix_type ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
203 result = m.inverse (mattyp, info, rcond, 1);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
204 args(0).matrix_type (mattyp);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
205 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
206 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
207 else
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
208 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
209 ComplexMatrix m = arg.complex_matrix_value ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
210 if (! error_state)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
211 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
212 MatrixType mattyp = args(0).matrix_type ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
213 result = m.inverse (mattyp, info, rcond, 1);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
214 args(0).matrix_type (mattyp);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
215 }
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
216 }
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
217 }
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
218 else
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7650
diff changeset
219 gripe_wrong_type_arg ("inv", arg);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
220 }
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
221
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
222 if (! error_state)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
223 {
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
224 if (nargout > 1)
7911
3b46230f7a4d DLD-FUNCTIONS/inv.cc (Finv): Avoid GCC warning
John W. Eaton <jwe@octave.org>
parents: 7814
diff changeset
225 retval(1) = isfloat ? octave_value (frcond) : octave_value (rcond);
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
226
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
227 retval(0) = result;
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
228
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
229 volatile double xrcond = rcond;
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
230 xrcond += 1.0;
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
231 if (nargout < 2 && (info == -1 || xrcond == 1.0))
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
232 warning ("inverse: matrix singular to machine precision, rcond = %g",
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
233 rcond);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
234 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
235
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
236 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
237 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
238
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
239 /*
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
240
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
241 %!assert(inv ([1, 2; 3, 4]), [-2, 1; 1.5, -0.5], sqrt (eps))
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
242 %!assert(inv (single([1, 2; 3, 4])), single([-2, 1; 1.5, -0.5]), sqrt (eps ('single')))
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
243
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
244 %!error <Invalid call to inv.*> inv ();
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
245 %!error <Invalid call to inv.*> inv ([1, 2; 3, 4], 2);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
246 %!error inv ([1, 2; 3, 4; 5, 6]);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
247
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
248 */
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
249
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5307
diff changeset
250 // FIXME -- this should really be done with an alias, but
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
251 // alias_builtin() won't do the right thing if we are actually using
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
252 // dynamic linking.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
253
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
254 DEFUN_DLD (inverse, args, nargout,
3458
d25bc039237b [project @ 2000-01-19 09:36:14 by jwe]
jwe
parents: 3372
diff changeset
255 "-*- texinfo -*-\n\
d25bc039237b [project @ 2000-01-19 09:36:14 by jwe]
jwe
parents: 3372
diff changeset
256 @deftypefn {Loadable Function} {} inverse (@var{a})\n\
d25bc039237b [project @ 2000-01-19 09:36:14 by jwe]
jwe
parents: 3372
diff changeset
257 See inv.\n\
d25bc039237b [project @ 2000-01-19 09:36:14 by jwe]
jwe
parents: 3372
diff changeset
258 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
259 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
260 return Finv (args, nargout);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
261 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
262
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
263 /*
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
264 ;;; Local Variables: ***
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
265 ;;; mode: C++ ***
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
266 ;;; End: ***
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
267 */