Mercurial > hg > octave-lyh
diff src/data.cc @ 7620:36594d5bbe13
Move diag function into the octave_value class
author | David Bateman <dbateman@free.fr> |
---|---|
date | Fri, 21 Mar 2008 00:08:24 +0100 |
parents | 3209a584e1ac |
children | 431f3788f5c4 |
line wrap: on
line diff
--- a/src/data.cc +++ b/src/data.cc @@ -883,305 +883,6 @@ DATA_REDUCTION (cumsum); } -template <class T> -static octave_value -make_diag (const T& v, octave_idx_type k) -{ - octave_value retval; - dim_vector dv = v.dims (); - octave_idx_type nd = dv.length (); - - if (nd > 2) - error ("diag: expecting 2-dimensional matrix"); - else - { - octave_idx_type nr = dv (0); - octave_idx_type nc = dv (1); - - if (nr == 0 || nc == 0) - retval = T (); - else if (nr != 1 && nc != 1) - retval = v.diag (k); - else - { - octave_idx_type roff = 0; - octave_idx_type coff = 0; - if (k > 0) - { - roff = 0; - coff = k; - } - else if (k < 0) - { - roff = -k; - coff = 0; - } - - if (nr == 1) - { - octave_idx_type n = nc + std::abs (k); - T m (dim_vector (n, n), T::resize_fill_value ()); - - for (octave_idx_type i = 0; i < nc; i++) - m (i+roff, i+coff) = v (0, i); - retval = m; - } - else - { - octave_idx_type n = nr + std::abs (k); - T m (dim_vector (n, n), T::resize_fill_value ()); - for (octave_idx_type i = 0; i < nr; i++) - m (i+roff, i+coff) = v (i, 0); - retval = m; - } - } - } - - return retval; -} - -#if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) -static octave_value -make_diag (const Matrix& v, octave_idx_type k); - -static octave_value -make_diag (const ComplexMatrix& v, octave_idx_type k); - -static octave_value -make_diag (const charMatrix& v, octave_idx_type k); - -static octave_value -make_diag (const boolMatrix& v, octave_idx_type k); - -static octave_value -make_diag (const int8NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const int16NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const int32NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const int64NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const uint8NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const uint16NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const uint32NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const uint64NDArray& v, octave_idx_type k); - -static octave_value -make_diag (const Cell& v, octave_idx_type k); -#endif - -template <class T> -static octave_value -make_spdiag (const T& v, octave_idx_type k) -{ - octave_value retval; - dim_vector dv = v.dims (); - octave_idx_type nr = dv (0); - octave_idx_type nc = dv (1); - - if (nr == 0 || nc == 0) - retval = T (); - else if (nr != 1 && nc != 1) - retval = v.diag (k); - else - { - octave_idx_type roff = 0; - octave_idx_type coff = 0; - if (k > 0) - { - roff = 0; - coff = k; - } - else if (k < 0) - { - roff = -k; - coff = 0; - } - - if (nr == 1) - { - octave_idx_type n = nc + std::abs (k); - octave_idx_type nz = v.nzmax (); - T r (n, n, nz); - for (octave_idx_type i = 0; i < coff+1; i++) - r.xcidx (i) = 0; - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = v.cidx(j); i < v.cidx(j+1); i++) - { - r.xdata (i) = v.data (i); - r.xridx (i) = j + roff; - } - r.xcidx (j+coff+1) = v.cidx(j+1); - } - for (octave_idx_type i = nc+coff+1; i < n+1; i++) - r.xcidx (i) = nz; - retval = r; - } - else - { - octave_idx_type n = nr + std::abs (k); - octave_idx_type nz = v.nzmax (); - octave_idx_type ii = 0; - octave_idx_type ir = v.ridx(0); - T r (n, n, nz); - for (octave_idx_type i = 0; i < coff+1; i++) - r.xcidx (i) = 0; - for (octave_idx_type i = 0; i < nr; i++) - { - if (ir == i) - { - r.xdata (ii) = v.data (ii); - r.xridx (ii++) = ir + roff; - if (ii != nz) - ir = v.ridx (ii); - } - r.xcidx (i+coff+1) = ii; - } - for (octave_idx_type i = nr+coff+1; i < n+1; i++) - r.xcidx (i) = nz; - retval = r; - } - } - - return retval; -} - -#if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) -static octave_value -make_spdiag (const SparseMatrix& v, octave_idx_type k); - -static octave_value -make_spdiag (const SparseComplexMatrix& v, octave_idx_type k); - -static octave_value -make_spdiag (const SparseBoolMatrix& v, octave_idx_type k); -#endif - -static octave_value -make_diag (const octave_value& a, octave_idx_type k) -{ - octave_value retval; - std::string result_type = a.class_name (); - - if (result_type == "double") - { - if (a.is_sparse_type ()) - { - if (a.is_real_type ()) - { - SparseMatrix m = a.sparse_matrix_value (); - if (!error_state) - retval = make_spdiag (m, k); - } - else - { - SparseComplexMatrix m = a.sparse_complex_matrix_value (); - if (!error_state) - retval = make_spdiag (m, k); - } - } - else - { - if (a.is_real_type ()) - { - Matrix m = a.matrix_value (); - if (!error_state) - retval = make_diag (m, k); - } - else - { - ComplexMatrix m = a.complex_matrix_value (); - if (!error_state) - retval = make_diag (m, k); - } - } - } -#if 0 - else if (result_type == "single") - retval = make_diag (a.single_array_value (), k); -#endif - else if (result_type == "char") - { - charMatrix m = a.char_matrix_value (); - if (!error_state) - { - retval = make_diag (m, k); - if (a.is_sq_string ()) - retval = octave_value (retval.char_array_value (), true, '\''); - } - } - else if (result_type == "logical") - { - if (a.is_sparse_type ()) - { - SparseBoolMatrix m = a.sparse_bool_matrix_value (); - if (!error_state) - retval = make_spdiag (m, k); - } - else - { - boolMatrix m = a.bool_matrix_value (); - if (!error_state) - retval = make_diag (m, k); - } - } - else if (result_type == "int8") - retval = make_diag (a.int8_array_value (), k); - else if (result_type == "int16") - retval = make_diag (a.int16_array_value (), k); - else if (result_type == "int32") - retval = make_diag (a.int32_array_value (), k); - else if (result_type == "int64") - retval = make_diag (a.int64_array_value (), k); - else if (result_type == "uint8") - retval = make_diag (a.uint8_array_value (), k); - else if (result_type == "uint16") - retval = make_diag (a.uint16_array_value (), k); - else if (result_type == "uint32") - retval = make_diag (a.uint32_array_value (), k); - else if (result_type == "uint64") - retval = make_diag (a.uint64_array_value (), k); - else if (result_type == "cell") - retval = make_diag (a.cell_value (), k); - else - gripe_wrong_type_arg ("diag", a); - - return retval; -} - -static octave_value -make_diag (const octave_value& arg) -{ - return make_diag (arg, 0); -} - -static octave_value -make_diag (const octave_value& a, const octave_value& b) -{ - octave_value retval; - - octave_idx_type k = b.int_value (); - - if (error_state) - error ("diag: invalid second argument"); - else - retval = make_diag (a, k); - - return retval; -} - DEFUN (diag, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} diag (@var{v}, @var{k})\n\ @@ -1211,9 +912,16 @@ int nargin = args.length (); if (nargin == 1 && args(0).is_defined ()) - retval = make_diag (args(0)); + retval = args(0).diag(); else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) - retval = make_diag (args(0), args(1)); + { + octave_idx_type k = args(1).int_value (); + + if (error_state) + error ("diag: invalid second argument"); + else + retval = args(0).diag(k); + } else print_usage ();