changeset 8910:6e9f26506804

optimize diag -> sparse and perm -> sparse conversions
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 05 Mar 2009 08:34:52 +0100
parents 52596fe9f178
children eeed8d649811
files liboctave/CSparse.cc liboctave/ChangeLog liboctave/dSparse.cc liboctave/dSparse.h src/ChangeLog src/DLD-FUNCTIONS/sparse.cc src/ov-perm.cc
diffstat 7 files changed, 80 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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<Complex> (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<Complex> (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
--- 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  <highegg@gmail.com>
+
+	* 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  <highegg@gmail.com>
 
 	* PermMatrix.h (PermMatrix::elem): Fix comparisons.
--- 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<double> (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<double> (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<double> (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<octave_idx_type> 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
--- 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<double> (r, c, num_nz) { }
 
   SparseMatrix& operator = (const SparseMatrix& a)
--- 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  <highegg@gmail.com>
+
+	* DLD-FUNCTIONS/sparse.cc (Fsparse): Handle diagonal and permutation
+	matrices.
+
 2009-03-03  John W. Eaton  <jwe@octave.org>
 
 	* ov-struct.cc (octave_struct::save_ascii,
--- 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) 
--- 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
 {