# HG changeset patch # User Jaroslav Hajek # Date 1236238492 -3600 # Node ID 6e9f26506804a46ceb401c7da565bfdf84d5d425 # Parent 52596fe9f17806840d5a8d06448a187de78efb54 optimize diag -> sparse and perm -> sparse conversions diff -r 52596fe9f178 -r 6e9f26506804 liboctave/CSparse.cc --- a/liboctave/CSparse.cc Thu Mar 05 00:55:56 2009 -0500 +++ b/liboctave/CSparse.cc Thu Mar 05 08:34:52 2009 +0100 @@ -146,20 +146,21 @@ } SparseComplexMatrix::SparseComplexMatrix (const ComplexDiagMatrix& a) - : MSparse (a.rows (), a.cols (), a.nnz ()) -{ - octave_idx_type nz = a.nnz (), l = a.length (); - for (octave_idx_type i = 0, j = 0; i < l; i++) + : MSparse (a.rows (), a.cols (), a.length ()) +{ + octave_idx_type j = 0, l = a.length (); + for (octave_idx_type i = 0; i < l; i++) { - if (a(i, i) != Complex (0.0, 0.0)) + cidx (i) = j; + if (a(i, i) != 0.0) { data (j) = a(i, i); ridx (j) = i; - cidx (j) = j; j++; } } - cidx (nz) = nz; + for (octave_idx_type i = l; i <= a.cols (); i++) + cidx(i) = j; } bool SparseComplexMatrix::operator == (const SparseComplexMatrix& a) const diff -r 52596fe9f178 -r 6e9f26506804 liboctave/ChangeLog --- a/liboctave/ChangeLog Thu Mar 05 00:55:56 2009 -0500 +++ b/liboctave/ChangeLog Thu Mar 05 08:34:52 2009 +0100 @@ -1,3 +1,11 @@ +2009-03-05 Jaroslav Hajek + + * dSparse.h (SparseMatrix::SparseMatrix(const PermMatrix&)): New + constructor. + (SparseMatrix::SparseMatrix(const DiagMatrix&)): Fix implementation. + * CSparse.h (SparseComplexMatrix::SparseComplexMatrix(const ComplexDiagMatrix&)): + Fix implementation. + 2009-03-04 Jaroslav Hajek * PermMatrix.h (PermMatrix::elem): Fix comparisons. diff -r 52596fe9f178 -r 6e9f26506804 liboctave/dSparse.cc --- a/liboctave/dSparse.cc Thu Mar 05 00:55:56 2009 -0500 +++ b/liboctave/dSparse.cc Thu Mar 05 08:34:52 2009 +0100 @@ -141,20 +141,41 @@ } SparseMatrix::SparseMatrix (const DiagMatrix& a) - : MSparse (a.rows (), a.cols (), a.nnz ()) -{ - octave_idx_type nz = a.nnz (), l = a.length (); - for (octave_idx_type i = 0, j = 0; i < l; i++) + : MSparse (a.rows (), a.cols (), a.length ()) +{ + octave_idx_type j = 0, l = a.length (); + for (octave_idx_type i = 0; i < l; i++) { + cidx (i) = j; if (a(i, i) != 0.0) { data (j) = a(i, i); ridx (j) = i; - cidx (j) = j; j++; } } - cidx (nz) = nz; + for (octave_idx_type i = l; i <= a.cols (); i++) + cidx(i) = j; +} + +SparseMatrix::SparseMatrix (const PermMatrix& a) + : MSparse (a.rows (), a.cols (), a.rows ()) +{ + octave_idx_type n = a.rows (); + for (octave_idx_type i = 0; i <= n; i++) + cidx (i) = i; + const Array pv = a.pvec (); + + if (a.is_row_perm ()) + { + for (octave_idx_type i = 0; i < n; i++) + ridx (i) = pv (i); + } + else + { + for (octave_idx_type i = 0; i < n; i++) + ridx (pv (i)) = i; + } } bool diff -r 52596fe9f178 -r 6e9f26506804 liboctave/dSparse.h --- a/liboctave/dSparse.h Thu Mar 05 00:55:56 2009 -0500 +++ b/liboctave/dSparse.h Thu Mar 05 08:34:52 2009 +0100 @@ -83,6 +83,8 @@ explicit SparseMatrix (const DiagMatrix& a); + explicit SparseMatrix (const PermMatrix& a); + SparseMatrix (octave_idx_type r, octave_idx_type c, octave_idx_type num_nz) : MSparse (r, c, num_nz) { } SparseMatrix& operator = (const SparseMatrix& a) diff -r 52596fe9f178 -r 6e9f26506804 src/ChangeLog --- a/src/ChangeLog Thu Mar 05 00:55:56 2009 -0500 +++ b/src/ChangeLog Thu Mar 05 08:34:52 2009 +0100 @@ -1,3 +1,8 @@ +2009-03-05 Jaroslav Hajek + + * DLD-FUNCTIONS/sparse.cc (Fsparse): Handle diagonal and permutation + matrices. + 2009-03-03 John W. Eaton * ov-struct.cc (octave_struct::save_ascii, diff -r 52596fe9f178 -r 6e9f26506804 src/DLD-FUNCTIONS/sparse.cc --- a/src/DLD-FUNCTIONS/sparse.cc Thu Mar 05 00:55:56 2009 -0500 +++ b/src/DLD-FUNCTIONS/sparse.cc Thu Mar 05 08:34:52 2009 +0100 @@ -146,6 +146,24 @@ retval = new octave_sparse_matrix (sm); } } + else if (arg.is_diag_matrix ()) + { + if (arg.is_complex_type ()) + { + SparseComplexMatrix sm = arg.sparse_complex_matrix_value (); + retval = new octave_sparse_complex_matrix (sm); + } + else + { + SparseMatrix sm = arg.sparse_matrix_value (); + retval = new octave_sparse_matrix (sm); + } + } + else if (arg.is_perm_matrix ()) + { + SparseMatrix sm = arg.sparse_matrix_value (); + retval = new octave_sparse_matrix (sm); + } else { if (use_complex) diff -r 52596fe9f178 -r 6e9f26506804 src/ov-perm.cc --- a/src/ov-perm.cc Thu Mar 05 00:55:56 2009 -0500 +++ b/src/ov-perm.cc Thu Mar 05 08:34:52 2009 +0100 @@ -212,6 +212,18 @@ return to_dense ().PREFIX ## _value (frc_str_conv); \ } +SparseMatrix +octave_perm_matrix::sparse_matrix_value (bool) const +{ + return SparseMatrix (matrix); +} + +SparseComplexMatrix +octave_perm_matrix::sparse_complex_matrix_value (bool) const +{ + return SparseComplexMatrix (sparse_matrix_value ()); +} + FORWARD_MATRIX_VALUE (Matrix, matrix) FORWARD_MATRIX_VALUE (FloatMatrix, float_matrix) FORWARD_MATRIX_VALUE (ComplexMatrix, complex_matrix) @@ -225,9 +237,6 @@ FORWARD_MATRIX_VALUE (boolNDArray, bool_array) FORWARD_MATRIX_VALUE (charNDArray, char_array) -FORWARD_MATRIX_VALUE (SparseMatrix, sparse_matrix) -FORWARD_MATRIX_VALUE (SparseComplexMatrix, sparse_complex_matrix) - idx_vector octave_perm_matrix::index_vector (void) const {