1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1352
|
27 #include "CmplxDET.h" |
453
|
28 #include "dbleDET.h" |
1
|
29 |
1352
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "help.h" |
1740
|
34 #include "oct-obj.h" |
636
|
35 #include "utils.h" |
1
|
36 |
2465
|
37 DEFUN_DLD (det, args, , |
519
|
38 "det (X): determinant of a square matrix") |
1
|
39 { |
2086
|
40 octave_value_list retval; |
519
|
41 |
|
42 int nargin = args.length (); |
1
|
43 |
712
|
44 if (nargin != 1) |
519
|
45 { |
|
46 print_usage ("det"); |
|
47 return retval; |
|
48 } |
1
|
49 |
2086
|
50 octave_value arg = args(0); |
1
|
51 |
636
|
52 int nr = arg.rows (); |
|
53 int nc = arg.columns (); |
|
54 |
|
55 if (nr == 0 && nc == 0) |
1
|
56 { |
636
|
57 retval = 1.0; |
|
58 return retval; |
1
|
59 } |
|
60 |
718
|
61 int arg_is_empty = empty_arg ("det", nr, nc); |
|
62 if (arg_is_empty < 0) |
636
|
63 return retval; |
718
|
64 if (arg_is_empty > 0) |
|
65 return Matrix (1, 1, 1.0); |
1
|
66 |
636
|
67 if (nr != nc) |
1
|
68 { |
636
|
69 gripe_square_matrix_required ("det"); |
|
70 return retval; |
|
71 } |
|
72 |
|
73 if (arg.is_real_type ()) |
|
74 { |
|
75 Matrix m = arg.matrix_value (); |
|
76 |
|
77 if (! error_state) |
620
|
78 { |
|
79 int info; |
|
80 double rcond = 0.0; |
636
|
81 |
620
|
82 DET det = m.determinant (info, rcond); |
636
|
83 |
620
|
84 double d = 0.0; |
636
|
85 |
620
|
86 if (info == -1) |
|
87 warning ("det: matrix singular to machine precision, rcond = %g", |
|
88 rcond); |
|
89 else |
|
90 d = det.value (); |
480
|
91 |
620
|
92 retval = d; |
|
93 } |
|
94 } |
785
|
95 else if (arg.is_complex_type ()) |
620
|
96 { |
636
|
97 ComplexMatrix m = arg.complex_matrix_value (); |
|
98 |
|
99 if (! error_state) |
620
|
100 { |
|
101 int info; |
|
102 double rcond = 0.0; |
636
|
103 |
620
|
104 ComplexDET det = m.determinant (info, rcond); |
636
|
105 |
620
|
106 Complex c = 0.0; |
636
|
107 |
620
|
108 if (info == -1) |
|
109 warning ("det: matrix singular to machine precision, rcond = %g", |
|
110 rcond); |
|
111 else |
|
112 c = det.value (); |
480
|
113 |
620
|
114 retval = c; |
|
115 } |
|
116 } |
|
117 else |
|
118 { |
636
|
119 gripe_wrong_type_arg ("det", arg); |
620
|
120 } |
|
121 |
1
|
122 return retval; |
|
123 } |
|
124 |
|
125 /* |
|
126 ;;; Local Variables: *** |
|
127 ;;; mode: C++ *** |
|
128 ;;; End: *** |
|
129 */ |