[mlpack-svn] r10732 - mlpack/trunk/src/mlpack/core/arma_extend/sparse

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Dec 12 14:30:39 EST 2011


Author: rcurtin
Date: 2011-12-12 14:30:39 -0500 (Mon, 12 Dec 2011)
New Revision: 10732

Modified:
   mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_bones.hpp
   mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_meat.hpp
   mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_bones.hpp
   mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_meat.hpp
Log:
Copy code over from arma-sparse branch.


Modified: mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_bones.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_bones.hpp	2011-12-12 19:13:17 UTC (rev 10731)
+++ mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_bones.hpp	2011-12-12 19:30:39 UTC (rev 10732)
@@ -134,18 +134,18 @@
   /**
    * Submatrix methods.
    */
-  arma_inline       SpSubview_row<eT> row(const uword row_num);
-  arma_inline const SpSubview_row<eT> row(const uword row_num) const;
+  arma_inline       SpSubview<eT> row(const uword row_num);
+  arma_inline const SpSubview<eT> row(const uword row_num) const;
 
-  inline            SpSubview_row<eT> operator()(const uword row_num, const span& col_span);
-  inline      const SpSubview_row<eT> operator()(const uword row_num, const span& col_span) const;
+  inline            SpSubview<eT> operator()(const uword row_num, const span& col_span);
+  inline      const SpSubview<eT> operator()(const uword row_num, const span& col_span) const;
 
 
-  arma_inline       SpSubview_col<eT> col(const uword col_num);
-  arma_inline const SpSubview_col<eT> col(const uword col_num) const;
+  arma_inline       SpSubview<eT> col(const uword col_num);
+  arma_inline const SpSubview<eT> col(const uword col_num) const;
 
-  inline            SpSubview_col<eT> operator()(const span& row_span, const uword col_num);
-  inline      const SpSubview_col<eT> operator()(const span& row_span, const uword col_num) const;
+  inline            SpSubview<eT> operator()(const span& row_span, const uword col_num);
+  inline      const SpSubview<eT> operator()(const span& row_span, const uword col_num) const;
 
   /**
    * Row- and column-related functions.

Modified: mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_meat.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_meat.hpp	2011-12-12 19:13:17 UTC (rev 10731)
+++ mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpMat_meat.hpp	2011-12-12 19:30:39 UTC (rev 10732)
@@ -1697,9 +1697,6 @@
   {
   arma_extra_debug_sigprint_this(this);
 
-  std::cout << x.n_rows << " x " << x.n_cols << std::endl;
-  x.print();
-
   (*this).operator=(x);
   }
 
@@ -1712,8 +1709,6 @@
   {//No easy init function, will have to generate matrix manually.
   arma_extra_debug_sigprint();
 
-  std::cout << x.n_rows << " " << x.n_cols << std::endl;
-
   init(x.n_rows, x.n_cols);
 
   for(uword i = 0; i < x.n_rows; i++)
@@ -2586,66 +2581,82 @@
 
 template<typename eT>
 arma_inline
-SpSubview_row<eT>
+SpSubview<eT>
 SpMat<eT>::row(const uword row_num)
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, row_num, 0, 1, n_cols);
   }
 
 template<typename eT>
 arma_inline
-const SpSubview_row<eT>
+const SpSubview<eT>
 SpMat<eT>::row(const uword row_num) const
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, row_num, 0, 1, n_cols);
   }
 
 template<typename eT>
 inline
-SpSubview_row<eT>
+SpSubview<eT>
 SpMat<eT>::operator()(const uword row_num, const span& col_span)
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, row_num, col_span.a, col_span.b - col_span.a + 1);
   }
 
 template<typename eT>
 inline
-const SpSubview_row<eT>
+const SpSubview<eT>
 SpMat<eT>::operator()(const uword row_num, const span& col_span) const
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, row_num, col_span.a, col_span.b - col_span.a + 1);
   }
 
 template<typename eT>
 arma_inline
-SpSubview_col<eT>
+SpSubview<eT>
 SpMat<eT>::col(const uword col_num)
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, 0, col_num, n_rows, 1);
   }
 
 template<typename eT>
 arma_inline
-const SpSubview_col<eT>
+const SpSubview<eT>
 SpMat<eT>::col(const uword col_num) const
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, 0, col_num, n_rows, 1);
   }
 
 template<typename eT>
 inline
-SpSubview_col<eT>
+SpSubview<eT>
 SpMat<eT>::operator()(const span& row_span, const uword col_num)
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, row_span.a, col_num, row_span.b - row_span.a + 1, 0);
   }
 
 template<typename eT>
 inline
-const SpSubview_col<eT>
+const SpSubview<eT>
 SpMat<eT>::operator()(const span& row_span, const uword col_num) const
   {
   arma_extra_debug_sigprint();
+
+  return SpSubview<eT>(*this, row_span.a, col_num, row_span.b - row_span.a + 1, 0);
   }
 
 template<typename eT>
@@ -4813,13 +4824,19 @@
   {
   arma_extra_debug_sigprint();
 
+  arma_debug_check
+    (
+    (in_row1 >= n_rows) || (in_row2 >= n_rows),
+    "SpMat::swap_rows(): out of bounds"
+    );
+
   // Sanity check.
   if (in_row1 == in_row2)
     {
     return;
     }
 
-  // THe easier way to do this, instead of collecting all the elements in one row and then swapping with the other, will be
+  // The easier way to do this, instead of collecting all the elements in one row and then swapping with the other, will be
   // to iterate over each column of the matrix (since we store in column-major format) and then swap the two elements in the two rows at that time.
   // We will try to avoid using the at() call since it is expensive, instead preferring to use an iterator to track our position.
   uword col1 = (in_row1 < in_row2) ? in_row1 : in_row2;
@@ -4925,23 +4942,15 @@
     );
 
   // Create pointers to the beginning and end of each column for values and row_indices.
-  typename std::vector<eT>::iterator v_c1_beg = values.begin() +
-    col_ptrs[in_col1];
-  typename std::vector<eT>::iterator v_c1_end = values.begin() +
-    col_ptrs[in_col1+1];
-  typename std::vector<eT>::iterator v_c2_beg = values.begin() +
-    col_ptrs[in_col2];
-  typename std::vector<eT>::iterator v_c2_end = values.begin() +
-    col_ptrs[in_col2+1];
+  typename std::vector<eT>::iterator v_c1_beg = values.begin() + col_ptrs[in_col1];
+  typename std::vector<eT>::iterator v_c1_end = values.begin() + col_ptrs[in_col1 + 1];
+  typename std::vector<eT>::iterator v_c2_beg = values.begin() + col_ptrs[in_col2];
+  typename std::vector<eT>::iterator v_c2_end = values.begin() + col_ptrs[in_col2 + 1];
 
-  typename std::vector<uword>::iterator r_c1_beg = row_indices.begin() +
-    col_ptrs[in_col1];
-  typename std::vector<uword>::iterator r_c1_end = row_indices.begin() +
-    col_ptrs[in_col1+1];
-  typename std::vector<uword>::iterator r_c2_beg = row_indices.begin() +
-    col_ptrs[in_col2];
-  typename std::vector<uword>::iterator r_c2_end = row_indices.begin() +
-    col_ptrs[in_col2+1];
+  typename std::vector<uword>::iterator r_c1_beg = row_indices.begin() + col_ptrs[in_col1];
+  typename std::vector<uword>::iterator r_c1_end = row_indices.begin() + col_ptrs[in_col1 + 1];
+  typename std::vector<uword>::iterator r_c2_beg = row_indices.begin() + col_ptrs[in_col2];
+  typename std::vector<uword>::iterator r_c2_end = row_indices.begin() + col_ptrs[in_col2 + 1];
 
   // Calculate the difference in column sizes.
   long diff = (v_c1_end - v_c1_beg) - (v_c2_end - v_c2_beg);

Modified: mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_bones.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_bones.hpp	2011-12-12 19:13:17 UTC (rev 10731)
+++ mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_bones.hpp	2011-12-12 19:30:39 UTC (rev 10732)
@@ -3,15 +3,8 @@
 template<typename eT>
 class SpSubview : public Base<eT, SpSubview<eT> >
   {
-  public: const SpMat<eT>& m;
-  /*
-  protected:
+  public: SpMat<eT>& m;
 
-  std::vector<eT>* values;
-  std::vector<uword>* row_indices;
-  std::vector<uword>* col_ptrs;
-  */
-
   public:
 
   typedef eT elem_type;
@@ -44,12 +37,23 @@
   template<typename T1> inline void operator%= (const Base<eT,T1>& x);
   template<typename T1> inline void operator/= (const Base<eT,T1>& x);
   
+  /* not doing anything special
   inline void operator=  (const SpSubview& x);
   inline void operator+= (const SpSubview& x);
   inline void operator-= (const SpSubview& x);
   inline void operator%= (const SpSubview& x);
   inline void operator/= (const SpSubview& x);
+  */
 
+  /*
+  inline static void extract(SpMat<eT>& out, const SpSubview& in);
+
+  inline static void  plus_inplace(Mat<eT>& out, const subview& in);
+  inline static void minus_inplace(Mat<eT>& out, const subview& in);
+  inline static void schur_inplace(Mat<eT>& out, const subview& in);
+  inline static void   div_inplace(Mat<eT>& out, const subview& in);
+  */
+
   inline void fill(const eT val);
   inline void zeros();
   inline void ones();
@@ -71,6 +75,7 @@
   
   inline bool is_vec() const;
   
+/* not yet
   inline       SpSubview_row<eT> row(const uword row_num);
   inline const SpSubview_row<eT> row(const uword row_num) const;
   
@@ -103,6 +108,7 @@
   
   inline       diagview<eT> diag(const s32 in_id = 0);
   inline const diagview<eT> diag(const s32 in_id = 0) const;
+*/
   
   inline void swap_rows(const uword in_row1, const uword in_row2);
   inline void swap_cols(const uword in_col1, const uword in_col2);
@@ -113,6 +119,7 @@
 
   };
 
+/*
 template<typename eT>
 class SpSubview_col : public SpSubview<eT>
   {
@@ -190,4 +197,4 @@
   
   SpSubview_row();
   };
-
+*/

Modified: mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_meat.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_meat.hpp	2011-12-12 19:13:17 UTC (rev 10731)
+++ mlpack/trunk/src/mlpack/core/arma_extend/sparse/SpSubview_meat.hpp	2011-12-12 19:30:39 UTC (rev 10732)
@@ -1,14 +1,9 @@
-#include <algorithm>
 
-template<typename eT>
-inline
-SpSubview<eT>::~SpSubview()
-  {
-  arma_extra_debug_sigprint();
-  }
 
+
+
 template<typename eT>
-arma_inline 
+arma_inline
 SpSubview<eT>::SpSubview(const SpMat<eT>& in_m, const uword in_row1, const uword in_col1, const uword in_n_rows, const uword in_n_cols)
   : m(in_m)
   , aux_row1(in_row1)
@@ -26,15 +21,20 @@
   uword count   = 0;
 
   for(uword i = m.col_ptrs[in_col1]; i < end; ++i)
+    {
     if(m.row_indices[i] >= in_row1 && m.row_indices[i] < end_row)
+      {
       ++count;
+      }
+    }
 
-
   access::rw(n_nonzero) = count;
   }
 
+
+
 template<typename eT>
-arma_inline 
+arma_inline
 SpSubview<eT>::SpSubview(SpMat<eT>& in_m, const uword in_row1, const uword in_col1, const uword in_n_rows, const uword in_n_cols)
   : m(in_m)
   , aux_row1(in_row1)
@@ -52,164 +52,540 @@
   uword count   = 0;
 
   for(uword i = m.col_ptrs[in_col1]; i < end; ++i)
+    {
     if(m.row_indices[i] >= in_row1 && m.row_indices[i] < end_row)
+      {
       ++count;
+      }
+    }
 
   access::rw(n_nonzero) = count;
   }
 
+
+
 template<typename eT>
-inline void 
+inline
+SpSubview<eT>::~SpSubview()
+  {
+  arma_extra_debug_sigprint();
+  }
+
+
+
+template<typename eT>
+inline
+void
 SpSubview<eT>::operator+= (const eT val)
   {
   arma_extra_debug_sigprint();
 
-  uword size = n_elem - n_nonzero;
+  if(val == 0)
+    {
+    return;
+    }
 
-  std::vector<eT>& values = m.values;
-  std::vector<uword>& row_indices = m.row_indices;
-  std::vector<uword>& col_ptrs = m.col_ptrs;
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
 
-  uword mnz = m.n_nonzero;
+  // iterate over our part of the sparse matrix
+  for(uword col = start_col; col < end_col; ++col)
+    {
+    for(uword row = start_row; row < end_row; ++row)
+      {
+      m.at(row, col) += val;
+      }
+    }
+  }
 
-  m.print();
-  std::cout << values.size() << '\t' << m.values.size() << std::endl;
-  values.resize(mnz + size, 0);
-  row_indices.resize(mnz + size, 0);
-  std::cout << n_elem << '\t' << n_nonzero << '\t' << size << '\t' << values.size() << '\t' << m.values.size() << std::endl;
 
-  uword start_row = aux_row1;
-  uword end_row   = aux_row1 + n_rows;
-  uword start_col = aux_col1;
-  // The this->n_nonzero + 1'th element in m
-  uword end_col   = aux_col1 + n_cols; 
 
-  if(size == 0)
+template<typename eT>
+inline
+void
+SpSubview<eT>::operator-= (const eT val)
+  {
+  arma_extra_debug_sigprint();
+
+  if(val == 0)
     {
-    for(uword c = start_col; c < end_col;)
+    return;
+    }
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+
+  for(uword col = start_col; col < end_col; ++col)
+    {
+    for(uword row = start_row; row < end_row; ++row)
       {
-      for(uword r = m.col_ptrs[c]; r < m.col_ptrs[++c]; ++r)
+      m.at(row, col) -= val;
+      }
+    }
+  }
+
+
+
+template<typename eT>
+inline
+void
+SpSubview<eT>::operator*= (const eT val)
+  {
+  arma_extra_debug_sigprint();
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col;)
+    {
+    for(uword r = m.col_ptrs[c]; r < m.col_ptrs[++c]; ++r)
+      {
+      if(m.row_indices[r] >= start_row && m.row_indices[r] < end_row)
         {
-        if(m.row_indices[r] >= start_row && m.row_indices[r] < end_row)
-          m.values[r] += val;
+        m.values[r] *= val;
         }
       }
+    }
+  }
 
-    return;
+
+
+template<typename eT>
+inline
+void
+SpSubview<eT>::operator/= (const eT val)
+  {
+  arma_extra_debug_sigprint();
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col; ++c)
+    {
+    for(uword r = m.col_ptrs[c]; r < m.col_ptrs[++c]; ++r)
+      {
+      m.values[r] /= val;
+      }
     }
+  }
 
-  typename std::vector<eT>::iterator v_beg_iter = values.begin() + col_ptrs[end_col];
-  typename std::vector<eT>::iterator v_end_iter = values.end()   - size;
 
-  std::vector<uword>::iterator r_beg_iter = row_indices.begin() + col_ptrs[end_col];
-  std::vector<uword>::iterator r_end_iter = row_indices.end()   - size;
-  
-  // Move the elements in the full matrix after our submatrix to the right by size positions
-  std::copy_backward(v_beg_iter, v_end_iter, values.end());
-  std::copy_backward(r_beg_iter, r_end_iter, row_indices.end());
 
-  // Adjust the column pointers in the full matrix after our submatrix to account for the shifting
-  std::vector<uword>::iterator c_beg_iter = col_ptrs.begin() + end_col;
-  std::vector<uword>::iterator c_end_iter = col_ptrs.end();
-  for(std::vector<uword>::iterator i = c_beg_iter; i != c_end_iter; ++i)
+template<typename eT>
+template<typename T1>
+inline
+void
+SpSubview<eT>::operator=(const Base<eT, T1>& x)
+  {
+  arma_extra_debug_sigprint();
+
+  const Proxy<T1> P(x.get_ref());
+
+  arma_debug_assert_same_size(*this, P, "insert into sparse submatrix");
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col; ++c)
     {
-    *i += size;
+    for(uword r = start_row; r < end_row; ++r)
+      {
+      m.at(r, c) = P.at(r, c);
+      }
     }
+  }
 
-  uword col;
-  uword dest;
-  uword row;
 
-  uword front_iter = col_ptrs[end_col] - 1;
-  uword back_iter  = front_iter + size - 1;
 
-  uword offset     = col_ptrs[start_col];
+template<typename eT>
+template<typename T1>
+inline
+void
+SpSubview<eT>::operator+=(const Base<eT, T1>& x)
+  {
+  arma_extra_debug_sigprint();
 
-  // Shift the elements of our submatrix to the right positions
-  // Adjust the values to the correct new values
-  // Add in the new elements
-  for(col = 0; col < n_cols; ++col)
+  const Proxy<T1> P(x.get_ref());
+
+  arma_debug_assert_same_size(*this, P, "insert into sparse submatrix");
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col; ++c)
     {
-    for(row = 0; row < n_rows; ++row)
+    for(uword r = start_row; r < end_row; ++r)
       {
-      dest = offset + row + row * col;
-      if(row_indices[front_iter] == row)
-        {
-        values[back_iter] = values[front_iter] + val;
+      m.at(r, c) += P.at(r, c);
+      }
+    }
+  }
 
-        --front_iter;
-        }
-      else
-        {
-        values[back_iter] = val;
-        ++col_ptrs[col+1];
-        }
 
-      row_indices[back_iter] = row;
-      --back_iter;
+
+template<typename eT>
+template<typename T1>
+inline
+void
+SpSubview<eT>::operator-=(const Base<eT, T1>& x)
+  {
+  arma_extra_debug_sigprint();
+
+  const Proxy<T1> P(x.get_ref());
+
+  arma_debug_assert_same_size(*this, P, "insert into sparse submatrix");
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col; ++c)
+    {
+    for(uword r = start_row; r < end_row; ++r)
+      {
+      m.at(r, c) += P.at(r, c);
       }
     }
+  }
 
-  for(uword i = 0; i < values.size(); ++i)
-    std::cout << values[i] << '\t';
-  std::cout << std::endl;
-  access::rw(n_nonzero)   += size - 1;
-  access::rw(m.n_nonzero) += size - 1;
-  m.print();
+
+
+template<typename eT>
+template<typename T1>
+inline
+void
+SpSubview<eT>::operator%=(const Base<eT, T1>& x)
+  {
+  arma_extra_debug_sigprint();
+
+  const Proxy<T1> P(x.get_ref());
+
+  arma_debug_assert_same_size(*this, P, "insert into sparse submatrix");
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col; ++c)
+    {
+    for(uword r = start_row; r < end_row; ++r)
+      {
+      m.at(r, c) %= P.at(r, c);
+      }
+    }
   }
 
+
+
 template<typename eT>
-inline void 
-SpSubview<eT>::operator-= (const eT val)
+template<typename T1>
+inline
+void
+SpSubview<eT>::operator/=(const Base<eT, T1>& x)
   {
   arma_extra_debug_sigprint();
+
+  const Proxy<T1> P(x.get_ref());
+
+  arma_debug_assert_same_size(*this, P, "insert into sparse submatrix");
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col; ++c)
+    {
+    for(uword r = start_row; r < end_row; ++r)
+      {
+      m.at(r, c) /= P.at(r, c);
+      }
+    }
   }
 
+
+
+
 template<typename eT>
-inline void 
-SpSubview<eT>::operator*= (const eT val)
+inline
+void
+SpSubview<eT>::fill(const eT val)
   {
   arma_extra_debug_sigprint();
 
-  uword start_row = aux_row1;
-  uword end_row   = aux_row1 + n_rows;
-  uword start_col = aux_col1;
-  uword end_col   = aux_col1 + n_rows;
+  (*this).operator=(val);
+  }
 
-  for(uword c = start_col; c < end_col;)
+
+
+template<typename eT>
+inline
+void
+SpSubview<eT>::zeros()
+  {
+  arma_extra_debug_sigprint();
+
+  // we can be a little smarter here
+  for(typename SpMat<eT>::iterator it(m, aux_row1, aux_col1);
+      (it.col < (aux_col1 + n_cols - 1)) || (it.col == (aux_col1 + n_cols - 1) && it.row < (aux_row1 + n_rows));
+      ++it)
     {
-    for(uword r = m.col_ptrs[c]; r < m.col_ptrs[++c]; ++r)
+    // column will always be valid; no need to check that
+    if((it.row >= aux_row1) && (it.row < (aux_row1 + n_rows)))
       {
-      if(m.row_indices[r] >= start_row && m.row_indices[r] < end_row)
-        m.values[r] *= val;
+      (*it) = 0;
       }
     }
   }
 
+
+
 template<typename eT>
-inline void 
-SpSubview<eT>::operator/= (const eT val)
+inline
+void
+SpSubview<eT>::ones()
   {
   arma_extra_debug_sigprint();
 
-  uword start_row = aux_row1;
-  uword end_row   = aux_row1 + n_rows;
-  uword start_col = aux_col1;
-  uword end_col   = aux_col1 + n_rows;
+  (*this).fill(eT(1));
+  }
 
-  for(uword c = start_col; c < end_col;)
+
+
+template<typename eT>
+inline
+void
+SpSubview<eT>::eye()
+  {
+  arma_extra_debug_sigprint();
+
+  // clear other things
+  (*this).zeros();
+
+  // now the diagonal ones
+  const uword end_index = std::min(n_rows, n_cols);
+
+  for(uword ind = 0; ind < end_index; ++ind)
     {
-    for(uword r = m.col_ptrs[c]; r < m.col_ptrs[++c]; ++r)
+    m.at(ind + aux_row1, ind + aux_col1) = eT(1);
+    }
+  }
+
+
+
+template<typename eT>
+inline
+eT&
+SpSubview<eT>::operator[](const uword i)
+  {
+  const uword row = i % n_rows;
+  const uword col = i / n_rows;
+
+  return (*this).at(row, col);
+  }
+
+
+
+template<typename eT>
+inline
+eT
+SpSubview<eT>::operator[](const uword i) const
+  {
+  const uword row = i % n_rows;
+  const uword col = i / n_rows;
+
+  return (*this).at(row, col);
+  }
+
+
+
+template<typename eT>
+inline
+eT&
+SpSubview<eT>::operator()(const uword i)
+  {
+  arma_debug_check( (i >= n_elem), "SpSubview::operator(): index out of bounds");
+
+  const uword row = i % n_rows;
+  const uword col = i % n_cols;
+
+  return (*this).at(row, col);
+  }
+
+
+
+template<typename eT>
+inline
+eT
+SpSubview<eT>::operator()(const uword i) const
+  {
+  arma_debug_check( (i >= n_elem), "SpSubview::operator(): index out of bounds");
+
+  const uword row = i % n_rows;
+  const uword col = i % n_cols;
+
+  return (*this).at(row, col);
+  }
+
+
+
+template<typename eT>
+inline
+eT&
+SpSubview<eT>::operator()(const uword in_row, const uword in_col)
+  {
+  arma_debug_check( (in_row >= n_rows) || (in_col >= n_cols), "SpSubview::operator(): index out of bounds");
+
+  return (*this).at(in_row, in_col);
+  }
+
+
+
+template<typename eT>
+inline
+eT
+SpSubview<eT>::operator()(const uword in_row, const uword in_col) const
+  {
+  arma_debug_check( (in_row >= n_rows) || (in_col >= n_cols), "SpSubview::operator(): index out of bounds");
+
+  return (*this).at(in_row, in_col);
+  }
+
+
+
+template<typename eT>
+inline
+eT&
+SpSubview<eT>::at(const uword in_row, const uword in_col)
+  {
+  return m.at(aux_row1 + in_row, aux_col1 + in_col);
+  }
+
+
+
+template<typename eT>
+inline
+eT
+SpSubview<eT>::at(const uword in_row, const uword in_col) const
+  {
+  return m.at(aux_row1 + in_row, aux_col1 + in_col);
+  }
+
+
+
+template<typename eT>
+inline
+bool
+SpSubview<eT>::check_overlap(const SpSubview<eT>& x) const
+  {
+  const subview<eT>& t = *this;
+
+  if(&t.m != &x.m)
+    {
+    return false;
+    }
+  else
+    {
+    if( (t.n_elem == 0) || (x.n_elem == 0) )
       {
-      if(m.row_indices[r] >= start_row && m.row_indices[r] < end_row)
-        m.values[r] /= val;
+      return false;
       }
+    else
+      {
+      const uword t_row_start  = t.aux_row1;
+      const uword t_row_end_p1 = t_row_start + t.n_rows;
+
+      const uword t_col_start  = t.aux_col1;
+      const uword t_col_end_p1 = t_col_start + t.n_cols;
+
+      const uword x_row_start  = x.aux_row1;
+      const uword x_row_end_p1 = x_row_start + x.n_rows;
+
+      const uword x_col_start  = x.aux_col1;
+      const uword x_col_end_p1 = x_col_start + x.n_cols;
+
+      const bool outside_rows = ( (x_row_start >= t_row_end_p1) || (t_row_start >= x_row_end_p1) );
+      const bool outside_cols = ( (x_col_start >= t_col_end_p1) || (t_col_start >= x_col_end_p1) );
+
+      return ( (outside_rows == false) && (outside_cols == false) );
+      }
     }
   }
 
-/***
+
+
+template<typename eT>
+inline
+bool
+SpSubview<eT>::is_vec() const
+  {
+  return ( (n_rows == 1) || (n_cols == 1) );
+  }
+
+
+
+template<typename eT>
+inline
+void
+SpSubview<eT>::swap_rows(const uword in_row1, const uword in_row2)
+  {
+  arma_extra_debug_sigprint();
+
+  arma_debug_check((in_row1 >= n_rows) || (in_row2 >= n_rows), "SpSubview::swap_rows(): invalid row index");
+
+  const uword start_col = aux_col1;
+  const uword end_col   = aux_col1 + n_cols;
+
+  for(uword c = start_col; c < end_col; ++c)
+    {
+    eT val = m.at(in_row1 + aux_row1, c);
+    m.at(in_row2 + aux_row1, c) = m.at(in_row1 + aux_row1, c);
+    m.at(in_row1 + aux_row1, c) = val;
+    }
+  }
+
+
+
+template<typename eT>
+inline
+void
+SpSubview<eT>::swap_cols(const uword in_col1, const uword in_col2)
+  {
+  arma_extra_debug_sigprint();
+
+  arma_debug_check((in_col1 >= n_cols) || (in_col2 >= n_cols), "SpSubview::swap_cols(): invalid column index");
+
+  const uword start_row = aux_row1;
+  const uword end_row   = aux_row1 + n_rows;
+
+  for(uword r = start_row; r < end_row; ++r)
+    {
+    eT val = m.at(r, in_col1 + aux_col1);
+    m.at(r, in_col1 + aux_col1) = m.at(r, in_col2 + aux_col1);
+    m.at(r, in_col2 + aux_col1) = val;
+    }
+  }
+
+
+
+/**
  * Sparse subview col
- */
+ *
 template<typename eT>
 inline
 SpSubview_col<eT>::SpSubview_col(const Mat<eT>& in_m, const uword in_col)
@@ -237,10 +613,11 @@
   {
   arma_extra_debug_sigprint();
   }
+*/
 
-/***
+/**
  * Sparse subview row
- */
+ *
 template<typename eT>
 inline
 SpSubview_row<eT>::SpSubview_row(const Mat<eT>& in_m, const uword in_row)
@@ -268,3 +645,4 @@
   {
   arma_extra_debug_sigprint();
   }
+*/




More information about the mlpack-svn mailing list