comparison libinterp/corefcn/det.cc @ 20892:c07bee629973

2015 Code Sprint: use ovl ().
author Rik <rik@octave.org>
date Mon, 14 Dec 2015 11:28:48 -0800
parents 1142cf6abc0d
children 8da80da1ac37
comparison
equal deleted inserted replaced
20891:95c0d4c07c56 20892:c07bee629973
62 For that, use any of the condition number functions: @code{cond},\n\ 62 For that, use any of the condition number functions: @code{cond},\n\
63 @code{condest}, @code{rcond}.\n\ 63 @code{condest}, @code{rcond}.\n\
64 @seealso{cond, condest, rcond}\n\ 64 @seealso{cond, condest, rcond}\n\
65 @end deftypefn") 65 @end deftypefn")
66 { 66 {
67 octave_value_list retval;
68
69 if (args.length () != 1) 67 if (args.length () != 1)
70 print_usage (); 68 print_usage ();
71 69
72 octave_value arg = args(0); 70 octave_value arg = args(0);
73 71
74 octave_idx_type nr = arg.rows (); 72 octave_idx_type nr = arg.rows ();
75 octave_idx_type nc = arg.columns (); 73 octave_idx_type nc = arg.columns ();
76 74
77 if (nr == 0 && nc == 0) 75 if (nr == 0 && nc == 0)
78 { 76 return octave_value (1.0);
79 retval(0) = 1.0;
80 return retval;
81 }
82 77
83 int arg_is_empty = empty_arg ("det", nr, nc); 78 int arg_is_empty = empty_arg ("det", nr, nc);
84 if (arg_is_empty < 0) 79 if (arg_is_empty < 0)
85 return retval; 80 return octave_value_list ();
86 if (arg_is_empty > 0) 81 if (arg_is_empty > 0)
87 return octave_value (Matrix (1, 1, 1.0)); 82 return octave_value (1.0);
88
89 83
90 if (nr != nc) 84 if (nr != nc)
91 { 85 {
92 gripe_square_matrix_required ("det"); 86 gripe_square_matrix_required ("det");
93 return retval; 87 return octave_value_list ();
94 } 88 }
89
90 octave_value_list retval (2);
95 91
96 bool isfloat = arg.is_single_type (); 92 bool isfloat = arg.is_single_type ();
97 93
98 if (arg.is_diag_matrix ()) 94 if (arg.is_diag_matrix ())
99 { 95 {
96 if (nargout <= 1)
97 retval.resize (1);
98
100 if (arg.is_complex_type ()) 99 if (arg.is_complex_type ())
101 { 100 {
102 if (isfloat) 101 if (isfloat)
103 { 102 {
104 retval(0) = arg.float_complex_diag_matrix_value () 103 retval(0) = arg.float_complex_diag_matrix_value ()
131 } 130 }
132 } 131 }
133 } 132 }
134 else if (arg.is_perm_matrix ()) 133 else if (arg.is_perm_matrix ())
135 { 134 {
135 if (nargout <= 1)
136 retval.resize (1);
137
136 retval(0) = static_cast<double> (arg.perm_matrix_value ().determinant ()); 138 retval(0) = static_cast<double> (arg.perm_matrix_value ().determinant ());
137 if (nargout > 1) 139 if (nargout > 1)
138 retval(1) = 1.0; 140 retval(1) = 1.0;
139 } 141 }
140 else if (arg.is_single_type ()) 142 else if (arg.is_single_type ())
148 FloatMatrix m = arg.float_matrix_value (); 150 FloatMatrix m = arg.float_matrix_value ();
149 151
150 MAYBE_CAST (rep, octave_float_matrix); 152 MAYBE_CAST (rep, octave_float_matrix);
151 MatrixType mtype = rep ? rep -> matrix_type () : MatrixType (); 153 MatrixType mtype = rep ? rep -> matrix_type () : MatrixType ();
152 FloatDET det = m.determinant (mtype, info, rcond); 154 FloatDET det = m.determinant (mtype, info, rcond);
155 retval(0) = info == -1 ? 0.0f : det.value ();
153 retval(1) = rcond; 156 retval(1) = rcond;
154 retval(0) = info == -1 ? 0.0f : det.value ();
155 if (rep) 157 if (rep)
156 rep->matrix_type (mtype); 158 rep->matrix_type (mtype);
157 } 159 }
158 else if (arg.is_complex_type ()) 160 else if (arg.is_complex_type ())
159 { 161 {
164 FloatComplexMatrix m = arg.float_complex_matrix_value (); 166 FloatComplexMatrix m = arg.float_complex_matrix_value ();
165 167
166 MAYBE_CAST (rep, octave_float_complex_matrix); 168 MAYBE_CAST (rep, octave_float_complex_matrix);
167 MatrixType mtype = rep ? rep -> matrix_type () : MatrixType (); 169 MatrixType mtype = rep ? rep -> matrix_type () : MatrixType ();
168 FloatComplexDET det = m.determinant (mtype, info, rcond); 170 FloatComplexDET det = m.determinant (mtype, info, rcond);
171 retval(0) = info == -1 ? FloatComplex (0.0) : det.value ();
169 retval(1) = rcond; 172 retval(1) = rcond;
170 retval(0) = info == -1 ? FloatComplex (0.0) : det.value ();
171 if (rep) 173 if (rep)
172 rep->matrix_type (mtype); 174 rep->matrix_type (mtype);
173 } 175 }
174 } 176 }
175 else 177 else
183 if (arg.is_sparse_type ()) 185 if (arg.is_sparse_type ())
184 { 186 {
185 SparseMatrix m = arg.sparse_matrix_value (); 187 SparseMatrix m = arg.sparse_matrix_value ();
186 188
187 DET det = m.determinant (info, rcond); 189 DET det = m.determinant (info, rcond);
188 retval(1) = rcond;
189 retval(0) = info == -1 ? 0.0 : det.value (); 190 retval(0) = info == -1 ? 0.0 : det.value ();
191 retval(1) = rcond;
190 } 192 }
191 else 193 else
192 { 194 {
193 Matrix m = arg.matrix_value (); 195 Matrix m = arg.matrix_value ();
194 196
195 MAYBE_CAST (rep, octave_matrix); 197 MAYBE_CAST (rep, octave_matrix);
196 MatrixType mtype = rep ? rep -> matrix_type () 198 MatrixType mtype = rep ? rep -> matrix_type ()
197 : MatrixType (); 199 : MatrixType ();
198 DET det = m.determinant (mtype, info, rcond); 200 DET det = m.determinant (mtype, info, rcond);
199 retval(1) = rcond;
200 retval(0) = info == -1 ? 0.0 : det.value (); 201 retval(0) = info == -1 ? 0.0 : det.value ();
202 retval(1) = rcond;
201 if (rep) 203 if (rep)
202 rep->matrix_type (mtype); 204 rep->matrix_type (mtype);
203 } 205 }
204 } 206 }
205 else if (arg.is_complex_type ()) 207 else if (arg.is_complex_type ())
211 if (arg.is_sparse_type ()) 213 if (arg.is_sparse_type ())
212 { 214 {
213 SparseComplexMatrix m = arg.sparse_complex_matrix_value (); 215 SparseComplexMatrix m = arg.sparse_complex_matrix_value ();
214 216
215 ComplexDET det = m.determinant (info, rcond); 217 ComplexDET det = m.determinant (info, rcond);
216 retval(1) = rcond;
217 retval(0) = info == -1 ? Complex (0.0) : det.value (); 218 retval(0) = info == -1 ? Complex (0.0) : det.value ();
219 retval(1) = rcond;
218 } 220 }
219 else 221 else
220 { 222 {
221 ComplexMatrix m = arg.complex_matrix_value (); 223 ComplexMatrix m = arg.complex_matrix_value ();
222 224
223 MAYBE_CAST (rep, octave_complex_matrix); 225 MAYBE_CAST (rep, octave_complex_matrix);
224 MatrixType mtype = rep ? rep -> matrix_type () 226 MatrixType mtype = rep ? rep -> matrix_type ()
225 : MatrixType (); 227 : MatrixType ();
226 ComplexDET det = m.determinant (mtype, info, rcond); 228 ComplexDET det = m.determinant (mtype, info, rcond);
227 retval(1) = rcond;
228 retval(0) = info == -1 ? Complex (0.0) : det.value (); 229 retval(0) = info == -1 ? Complex (0.0) : det.value ();
230 retval(1) = rcond;
229 if (rep) 231 if (rep)
230 rep->matrix_type (mtype); 232 rep->matrix_type (mtype);
231 } 233 }
232 } 234 }
233 else 235 else
234 gripe_wrong_type_arg ("det", arg); 236 gripe_wrong_type_arg ("det", arg);
235 } 237 }
238
236 return retval; 239 return retval;
237 } 240 }
238 241
239 /* 242 /*
240 %!assert (det ([1, 2; 3, 4]), -2, 10*eps) 243 %!assert (det ([1, 2; 3, 4]), -2, 10*eps)