Mercurial > octave-nkf
view libinterp/corefcn/inv.cc @ 20574:dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
* lo-array-gripes.h, lo-array-gripes.cc (index_exception):
New base class for indexing errors.
(invalid_index, out_of_range): New classes.
(gripe_index_out_of_range): New overloaded function.
(gripe_invalid_index): New overloaded functions.
Delete version with no arguments.
(gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch):
Delete.
Change uses of gripe functions as needed.
* Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use
exceptions to collect error info about and handle indexing errors.
* data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__,
do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__):
Likewise.
* oct-map.cc (octave_map::index, octave_map::assign,
octave_map::delete_elements): Likewise.
* sparse.cc (Fsparse): Likewise.
* sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests.
* utils.cc (dims_to_numel): Likewise.
* ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op,
octave_base_diag<DMT, MT>::subsasgn): Likewise.
* ov-base-mat.cc (octave_base_matrix<MT>::subsref,
octave_base_matrix<MT>::assign): Likewise.
* ov-base-sparse.cc (octave_base_sparse<T>::do_index_op,
octave_base_sparse<T>::assign,
octave_base_sparse<MT>::delete_elements): Likewise.
* ov-classdef.cc (cdef_object_array::subsref,
cdef_object_array::subsasgn): Likewise.
* ov-java.cc (make_java_index): Likewise.
* ov-perm.cc (octave_perm_matrix::do_index_op): Likewise.
* ov-range.cc (octave_range::do_index_op): Likewise.
* ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise.
* ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise.
* pt-assign.cc (tree_simple_assignment::rvalue1): Likewise.
* pt-idx.cc (tree_index_expression::rvalue,
tree_index_expression::lvalue): Likewise.
* Array-util.cc (sub2ind): Likewise.
* toplev.cc (main_loop): Also catch unhandled index_exception
exceptions.
* ov-base.cc (octave_base_value::index_vector): Improve error message.
* ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise.
* ov-complex.cc (complex_index): New class.
(gripe_complex_index): New function.
(octave_complex::index_vector): Use it.
* pt-id.h, pt-id.cc (tree_identifier::is_variable,
tree_black_hole::is_variable): Now const.
* pt-idx.cc (final_index_error): New static function.
(tree_index_expression::rvalue, tree_index_expression::lvalue):
Use it.
* index.tst: New tests.
author | Lachlan Andrew <lachlanbis@gmail.com> |
---|---|
date | Fri, 02 Oct 2015 15:07:37 -0400 |
parents | 4f45eaf83908 |
children |
line wrap: on
line source
/* Copyright (C) 1996-2015 John W. Eaton This file is part of Octave. Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, see <http://www.gnu.org/licenses/>. */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include "defun.h" #include "error.h" #include "gripes.h" #include "oct-obj.h" #include "ops.h" #include "ov-re-diag.h" #include "ov-cx-diag.h" #include "ov-flt-re-diag.h" #include "ov-flt-cx-diag.h" #include "ov-perm.h" #include "utils.h" DEFUN (inv, args, nargout, "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {@var{x} =} inv (@var{A})\n\ @deftypefnx {Built-in Function} {[@var{x}, @var{rcond}] =} inv (@var{A})\n\ Compute the inverse of the square matrix @var{A}.\n\ \n\ Return an estimate of the reciprocal condition number if requested,\n\ otherwise warn of an ill-conditioned matrix if the reciprocal condition\n\ number is small.\n\ \n\ In general it is best to avoid calculating the inverse of a matrix directly.\n\ For example, it is both faster and more accurate to solve systems of\n\ equations (@var{A}*@math{x} = @math{b}) with\n\ @code{@var{y} = @var{A} \\ @math{b}}, rather than\n\ @code{@var{y} = inv (@var{A}) * @math{b}}.\n\ \n\ If called with a sparse matrix, then in general @var{x} will be a full\n\ matrix requiring significantly more storage. Avoid forming the inverse of a\n\ sparse matrix if possible.\n\ @seealso{ldivide, rdivide}\n\ @end deftypefn") { octave_value_list retval; int nargin = args.length (); if (nargin != 1) { print_usage (); return retval; } octave_value arg = args(0); octave_idx_type nr = arg.rows (); octave_idx_type nc = arg.columns (); int arg_is_empty = empty_arg ("inverse", nr, nc); if (arg_is_empty < 0) return retval; else if (arg_is_empty > 0) return octave_value (Matrix ()); if (nr != nc) { gripe_square_matrix_required ("inverse"); return retval; } octave_value result; octave_idx_type info; double rcond = 0.0; float frcond = 0.0; bool isfloat = arg.is_single_type (); if (arg.is_diag_matrix ()) { rcond = 1.0; frcond = 1.0f; if (arg.is_complex_type ()) { if (isfloat) { result = arg.float_complex_diag_matrix_value ().inverse (info); if (nargout > 1) frcond = arg.float_complex_diag_matrix_value ().rcond (); } else { result = arg.complex_diag_matrix_value ().inverse (info); if (nargout > 1) rcond = arg.complex_diag_matrix_value ().rcond (); } } else { if (isfloat) { result = arg.float_diag_matrix_value ().inverse (info); if (nargout > 1) frcond = arg.float_diag_matrix_value ().rcond (); } else { result = arg.diag_matrix_value ().inverse (info); if (nargout > 1) rcond = arg.diag_matrix_value ().rcond (); } } } else if (arg.is_perm_matrix ()) { rcond = 1.0; info = 0; result = arg.perm_matrix_value ().inverse (); } else if (isfloat) { if (arg.is_real_type ()) { FloatMatrix m = arg.float_matrix_value (); if (! error_state) { MatrixType mattyp = args(0).matrix_type (); result = m.inverse (mattyp, info, frcond, 1); args(0).matrix_type (mattyp); } } else if (arg.is_complex_type ()) { FloatComplexMatrix m = arg.float_complex_matrix_value (); if (! error_state) { MatrixType mattyp = args(0).matrix_type (); result = m.inverse (mattyp, info, frcond, 1); args(0).matrix_type (mattyp); } } } else { if (arg.is_real_type ()) { if (arg.is_sparse_type ()) { SparseMatrix m = arg.sparse_matrix_value (); if (! error_state) { MatrixType mattyp = args(0).matrix_type (); result = m.inverse (mattyp, info, rcond, 1); args(0).matrix_type (mattyp); } } else { Matrix m = arg.matrix_value (); if (! error_state) { MatrixType mattyp = args(0).matrix_type (); result = m.inverse (mattyp, info, rcond, 1); args(0).matrix_type (mattyp); } } } else if (arg.is_complex_type ()) { if (arg.is_sparse_type ()) { SparseComplexMatrix m = arg.sparse_complex_matrix_value (); if (! error_state) { MatrixType mattyp = args(0).matrix_type (); result = m.inverse (mattyp, info, rcond, 1); args(0).matrix_type (mattyp); } } else { ComplexMatrix m = arg.complex_matrix_value (); if (! error_state) { MatrixType mattyp = args(0).matrix_type (); result = m.inverse (mattyp, info, rcond, 1); args(0).matrix_type (mattyp); } } } else gripe_wrong_type_arg ("inv", arg); } if (! error_state) { if (nargout > 1) retval(1) = isfloat ? octave_value (frcond) : octave_value (rcond); retval(0) = result; bool rcond_plus_one_eq_one = false; if (isfloat) { volatile float xrcond = frcond; rcond_plus_one_eq_one = xrcond + 1.0F == 1.0F; } else { volatile double xrcond = rcond; rcond_plus_one_eq_one = xrcond + 1.0 == 1.0; } if (nargout < 2 && (info == -1 || rcond_plus_one_eq_one)) gripe_singular_matrix (isfloat ? frcond : rcond); } return retval; } /* %!assert (inv ([1, 2; 3, 4]), [-2, 1; 1.5, -0.5], sqrt (eps)) %!assert (inv (single ([1, 2; 3, 4])), single ([-2, 1; 1.5, -0.5]), sqrt (eps ("single"))) %!error inv () %!error inv ([1, 2; 3, 4], 2) %!error <argument must be a square matrix> inv ([1, 2; 3, 4; 5, 6]) %!test %! [xinv, rcond] = inv (single ([1,2;3,4])); %! assert (isa (xinv, 'single')); %! assert (isa (rcond, 'single')); %!test %! [xinv, rcond] = inv ([1,2;3,4]); %! assert (isa (xinv, 'double')); %! assert (isa (rcond, 'double')); */ // FIXME: this should really be done with an alias, but // alias_builtin() won't do the right thing if we are actually using // dynamic linking. DEFUN (inverse, args, nargout, "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {@var{x} =} inverse (@var{A})\n\ @deftypefnx {Built-in Function} {[@var{x}, @var{rcond}] =} inverse (@var{A})\n\ Compute the inverse of the square matrix @var{A}.\n\ \n\ This is an alias for @code{inv}.\n\ @seealso{inv}\n\ @end deftypefn") { return Finv (args, nargout); }