7
|
1 // f-inv.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
277
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
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 2, or (at your option) any |
|
11 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, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
453
|
28 #include "dMatrix.h" |
|
29 #include "CMatrix.h" |
1
|
30 |
|
31 #include "tree-const.h" |
|
32 #include "user-prefs.h" |
|
33 #include "gripes.h" |
|
34 #include "error.h" |
636
|
35 #include "utils.h" |
544
|
36 #include "help.h" |
519
|
37 #include "defun-dld.h" |
1
|
38 |
701
|
39 DEFUN_DLD_BUILTIN ("inv", Finv, Sinv, 2, 1, |
519
|
40 "inv (X): inverse of a square matrix") |
1
|
41 { |
519
|
42 Octave_object retval; |
|
43 |
|
44 int nargin = args.length (); |
1
|
45 |
712
|
46 if (nargin != 1) |
519
|
47 { |
|
48 print_usage ("inv"); |
|
49 return retval; |
|
50 } |
1
|
51 |
712
|
52 tree_constant arg = args(0); |
636
|
53 |
|
54 int nr = arg.rows (); |
|
55 int nc = arg.columns (); |
1
|
56 |
718
|
57 int arg_is_empty = empty_arg ("inverse", nr, nc); |
|
58 |
|
59 if (arg_is_empty < 0) |
636
|
60 return retval; |
718
|
61 else if (arg_is_empty > 0) |
|
62 return Matrix (); |
636
|
63 |
|
64 if (nr != nc) |
1
|
65 { |
636
|
66 gripe_square_matrix_required ("inverse"); |
|
67 return retval; |
1
|
68 } |
|
69 |
636
|
70 if (arg.is_real_type ()) |
|
71 { |
|
72 Matrix m = arg.matrix_value (); |
1
|
73 |
636
|
74 if (! error_state) |
620
|
75 { |
|
76 int info; |
|
77 double rcond = 0.0; |
636
|
78 |
620
|
79 Matrix minv = m.inverse (info, rcond); |
636
|
80 |
620
|
81 if (info == -1) |
|
82 warning ("inverse: matrix singular to machine precision,\ |
217
|
83 rcond = %g", rcond); |
620
|
84 else |
|
85 retval = minv; |
|
86 } |
|
87 } |
636
|
88 else if (arg.is_complex_type ()) |
620
|
89 { |
636
|
90 ComplexMatrix m = arg.complex_matrix_value (); |
|
91 |
|
92 if (! error_state) |
620
|
93 { |
|
94 int info; |
|
95 double rcond = 0.0; |
636
|
96 |
620
|
97 ComplexMatrix minv = m.inverse (info, rcond); |
636
|
98 |
620
|
99 if (info == -1) |
|
100 warning ("inverse: matrix singular to machine precision,\ |
217
|
101 rcond = %g", rcond); |
620
|
102 else |
|
103 retval = minv; |
|
104 } |
|
105 } |
|
106 else |
|
107 { |
636
|
108 gripe_wrong_type_arg ("inv", arg); |
620
|
109 } |
|
110 |
1
|
111 return retval; |
|
112 } |
|
113 |
|
114 /* |
|
115 ;;; Local Variables: *** |
|
116 ;;; mode: C++ *** |
|
117 ;;; page-delimiter: "^/\\*" *** |
|
118 ;;; End: *** |
|
119 */ |