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" |
544
|
35 #include "help.h" |
519
|
36 #include "defun-dld.h" |
1
|
37 |
519
|
38 DEFUN_DLD ("inv", Finv, Sinv, 2, 1, |
|
39 "inv (X): inverse of a square matrix") |
1
|
40 { |
519
|
41 Octave_object retval; |
|
42 |
|
43 int nargin = args.length (); |
1
|
44 |
519
|
45 if (nargin != 2) |
|
46 { |
|
47 print_usage ("inv"); |
|
48 return retval; |
|
49 } |
1
|
50 |
519
|
51 tree_constant tmp = args(1).make_numeric (); |
1
|
52 |
|
53 int nr = tmp.rows (); |
|
54 int nc = tmp.columns (); |
|
55 if (nr == 0 || nc == 0) |
|
56 { |
|
57 int flag = user_pref.propagate_empty_matrices; |
|
58 if (flag < 0) |
|
59 gripe_empty_arg ("inverse", 0); |
|
60 else if (flag == 0) |
|
61 gripe_empty_arg ("inverse", 1); |
|
62 } |
|
63 |
|
64 Matrix mtmp; |
|
65 if (nr == 0 && nc == 0) |
516
|
66 return mtmp; |
1
|
67 |
|
68 switch (tmp.const_type ()) |
|
69 { |
|
70 case tree_constant_rep::matrix_constant: |
|
71 { |
|
72 Matrix m = tmp.matrix_value (); |
|
73 if (m.rows () == m.columns ()) |
|
74 { |
|
75 int info; |
|
76 double rcond = 0.0; |
|
77 Matrix minv = m.inverse (info, rcond); |
|
78 if (info == -1) |
217
|
79 warning ("inverse: matrix singular to machine precision,\ |
|
80 rcond = %g", rcond); |
1
|
81 else |
516
|
82 retval = minv; |
1
|
83 } |
|
84 else |
|
85 gripe_square_matrix_required ("inverse"); |
|
86 } |
|
87 break; |
|
88 case tree_constant_rep::scalar_constant: |
|
89 { |
|
90 double d = 1.0 / tmp.double_value (); |
516
|
91 retval = d; |
1
|
92 } |
|
93 break; |
|
94 case tree_constant_rep::complex_matrix_constant: |
|
95 { |
|
96 ComplexMatrix m = tmp.complex_matrix_value (); |
|
97 if (m.rows () == m.columns ()) |
|
98 { |
|
99 int info; |
|
100 double rcond = 0.0; |
|
101 ComplexMatrix minv = m.inverse (info, rcond); |
|
102 if (info == -1) |
217
|
103 warning ("inverse: matrix singular to machine precision,\ |
|
104 rcond = %g", rcond); |
1
|
105 else |
516
|
106 retval = minv; |
1
|
107 } |
|
108 else |
|
109 gripe_square_matrix_required ("inverse"); |
|
110 } |
|
111 break; |
|
112 case tree_constant_rep::complex_scalar_constant: |
|
113 { |
|
114 Complex c = 1.0 / tmp.complex_value (); |
516
|
115 retval = c; |
1
|
116 } |
|
117 break; |
|
118 default: |
|
119 break; |
|
120 } |
|
121 return retval; |
|
122 } |
|
123 |
|
124 /* |
|
125 ;;; Local Variables: *** |
|
126 ;;; mode: C++ *** |
|
127 ;;; page-delimiter: "^/\\*" *** |
|
128 ;;; End: *** |
|
129 */ |