7
|
1 // f-det.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1352
|
28 #include "CmplxDET.h" |
453
|
29 #include "dbleDET.h" |
1
|
30 |
1352
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "gripes.h" |
|
34 #include "help.h" |
1
|
35 #include "tree-const.h" |
|
36 #include "user-prefs.h" |
636
|
37 #include "utils.h" |
1
|
38 |
1488
|
39 DEFUN_DLD_BUILTIN ("det", Fdet, Sdet, 10, |
519
|
40 "det (X): determinant 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 ("det"); |
|
49 return retval; |
|
50 } |
1
|
51 |
712
|
52 tree_constant arg = args(0); |
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 |
718
|
63 int arg_is_empty = empty_arg ("det", nr, nc); |
|
64 if (arg_is_empty < 0) |
636
|
65 return retval; |
718
|
66 if (arg_is_empty > 0) |
|
67 return Matrix (1, 1, 1.0); |
1
|
68 |
636
|
69 if (nr != nc) |
1
|
70 { |
636
|
71 gripe_square_matrix_required ("det"); |
|
72 return retval; |
|
73 } |
|
74 |
|
75 if (arg.is_real_type ()) |
|
76 { |
|
77 Matrix m = arg.matrix_value (); |
|
78 |
|
79 if (! error_state) |
620
|
80 { |
|
81 int info; |
|
82 double rcond = 0.0; |
636
|
83 |
620
|
84 DET det = m.determinant (info, rcond); |
636
|
85 |
620
|
86 double d = 0.0; |
636
|
87 |
620
|
88 if (info == -1) |
|
89 warning ("det: matrix singular to machine precision, rcond = %g", |
|
90 rcond); |
|
91 else |
|
92 d = det.value (); |
480
|
93 |
620
|
94 retval = d; |
|
95 } |
|
96 } |
785
|
97 else if (arg.is_complex_type ()) |
620
|
98 { |
636
|
99 ComplexMatrix m = arg.complex_matrix_value (); |
|
100 |
|
101 if (! error_state) |
620
|
102 { |
|
103 int info; |
|
104 double rcond = 0.0; |
636
|
105 |
620
|
106 ComplexDET det = m.determinant (info, rcond); |
636
|
107 |
620
|
108 Complex c = 0.0; |
636
|
109 |
620
|
110 if (info == -1) |
|
111 warning ("det: matrix singular to machine precision, rcond = %g", |
|
112 rcond); |
|
113 else |
|
114 c = det.value (); |
480
|
115 |
620
|
116 retval = c; |
|
117 } |
|
118 } |
|
119 else |
|
120 { |
636
|
121 gripe_wrong_type_arg ("det", arg); |
620
|
122 } |
|
123 |
1
|
124 return retval; |
|
125 } |
|
126 |
|
127 /* |
|
128 ;;; Local Variables: *** |
|
129 ;;; mode: C++ *** |
|
130 ;;; page-delimiter: "^/\\*" *** |
|
131 ;;; End: *** |
|
132 */ |