7
|
1 // f-det.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
453
|
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 "dbleDET.h" |
|
29 #include "CmplxDET.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 |
519
|
39 DEFUN_DLD ("det", Fdet, Sdet, 2, 1, |
|
40 "det (X): determinant of a square matrix") |
1
|
41 { |
519
|
42 Octave_object retval; |
|
43 |
|
44 int nargin = args.length (); |
1
|
45 |
519
|
46 if (nargin != 2) |
|
47 { |
|
48 print_usage ("det"); |
|
49 return retval; |
|
50 } |
1
|
51 |
636
|
52 tree_constant arg = args(1); |
1
|
53 |
636
|
54 int nr = arg.rows (); |
|
55 int nc = arg.columns (); |
|
56 |
|
57 if (nr == 0 && nc == 0) |
1
|
58 { |
636
|
59 retval = 1.0; |
|
60 return retval; |
1
|
61 } |
|
62 |
636
|
63 if (empty_arg ("det", nr, nc) < 0) |
|
64 return retval; |
1
|
65 |
636
|
66 if (nr != nc) |
1
|
67 { |
636
|
68 gripe_square_matrix_required ("det"); |
|
69 return retval; |
|
70 } |
|
71 |
|
72 if (arg.is_real_type ()) |
|
73 { |
|
74 Matrix m = arg.matrix_value (); |
|
75 |
|
76 if (! error_state) |
620
|
77 { |
|
78 int info; |
|
79 double rcond = 0.0; |
636
|
80 |
620
|
81 DET det = m.determinant (info, rcond); |
636
|
82 |
620
|
83 double d = 0.0; |
636
|
84 |
620
|
85 if (info == -1) |
|
86 warning ("det: matrix singular to machine precision, rcond = %g", |
|
87 rcond); |
|
88 else |
|
89 d = det.value (); |
480
|
90 |
620
|
91 retval = d; |
|
92 } |
|
93 } |
636
|
94 else if (arg.is_complex_matrix ()) |
620
|
95 { |
636
|
96 ComplexMatrix m = arg.complex_matrix_value (); |
|
97 |
|
98 if (! error_state) |
620
|
99 { |
|
100 int info; |
|
101 double rcond = 0.0; |
636
|
102 |
620
|
103 ComplexDET det = m.determinant (info, rcond); |
636
|
104 |
620
|
105 Complex c = 0.0; |
636
|
106 |
620
|
107 if (info == -1) |
|
108 warning ("det: matrix singular to machine precision, rcond = %g", |
|
109 rcond); |
|
110 else |
|
111 c = det.value (); |
480
|
112 |
620
|
113 retval = c; |
|
114 } |
|
115 } |
|
116 else |
|
117 { |
636
|
118 gripe_wrong_type_arg ("det", arg); |
620
|
119 } |
|
120 |
1
|
121 return retval; |
|
122 } |
|
123 |
|
124 /* |
|
125 ;;; Local Variables: *** |
|
126 ;;; mode: C++ *** |
|
127 ;;; page-delimiter: "^/\\*" *** |
|
128 ;;; End: *** |
|
129 */ |