diff liboctave/Sparse.cc @ 13030:b646413c3d0e

Make operators do smarter sparse conversions on permutation matrices. * Sparse.cc (Sparse<T>::Sparse): New templated ctor, plus two instantiations. * Sparse.h (Sparse<T>): Declare new ctor. * MSparse.h (MSparse): Give this class a PermMatrix ctor. * boolSparse.h (BoolSparseMatrix): Ditto. * dSparse.cc: Refactor PermMatrix ctor, moved into common parent class. * dSparse.h (SparseMatrix): Ditto. * op-pm-sm.cc: Declare and install smarter permutation matrix operators. * ov-perm.cc (octave_perm_matrix): Declare new virtual function override. * ov-perm.cc (sparse_bool_matrix_value): Override this virtual function.
author Jordi Gutiérrez Hermoso <jordigh@gmail.com>
date Tue, 30 Aug 2011 18:36:06 -0500
parents 85e87b865f71
children 7d4429c82212
line wrap: on
line diff
--- a/liboctave/Sparse.cc
+++ b/liboctave/Sparse.cc
@@ -50,6 +50,38 @@
 #include "oct-spparms.h"
 #include "mx-inlines.cc"
 
+#include "PermMatrix.h"
+
+template <class T>
+Sparse<T>::Sparse (const PermMatrix& a)
+  : rep (new typename Sparse<T>::SparseRep (a.rows (), a.cols (), a.rows ())),
+         dimensions (dim_vector (a.rows (), a.cols()))
+{
+  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 (pv (i)) = i;
+    }
+  else
+    {
+      for (octave_idx_type i = 0; i < n; i++)
+        ridx (i) = pv (i);
+    }
+
+  for (octave_idx_type i = 0; i < n; i++)
+    data (i) = 1.0;
+}
+
+// SparseMatrix and SparseBoolMatrix both need this ctor
+template Sparse<double>::Sparse (const PermMatrix& a);
+template Sparse<bool>::Sparse (const PermMatrix& a);
+
 template <class T>
 T&
 Sparse<T>::SparseRep::elem (octave_idx_type _r, octave_idx_type _c)