SparseRef: fix writable sparse Ref bindings for inner-panel subset views

libeigen/eigen!2432

Closes #2883
diff --git a/Eigen/src/SparseCore/SparseBlock.h b/Eigen/src/SparseCore/SparseBlock.h
index 3affff6..707bf7c 100644
--- a/Eigen/src/SparseCore/SparseBlock.h
+++ b/Eigen/src/SparseCore/SparseBlock.h
@@ -18,7 +18,7 @@
 // Subset of columns or rows
 template <typename XprType, int BlockRows, int BlockCols>
 class BlockImpl<XprType, BlockRows, BlockCols, true, Sparse>
-    : public SparseMatrixBase<Block<XprType, BlockRows, BlockCols, true> > {
+    : public SparseCompressedBase<Block<XprType, BlockRows, BlockCols, true> > {
   typedef internal::remove_all_t<typename XprType::Nested> MatrixTypeNested_;
   typedef Block<XprType, BlockRows, BlockCols, true> BlockType;
 
@@ -27,7 +27,7 @@
 
  protected:
   enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
-  typedef SparseMatrixBase<BlockType> Base;
+  typedef SparseCompressedBase<BlockType> Base;
   using Base::convert_index;
 
  public:
diff --git a/Eigen/src/SparseCore/SparseCompressedBase.h b/Eigen/src/SparseCore/SparseCompressedBase.h
index 420e9fa..37a2306 100644
--- a/Eigen/src/SparseCore/SparseCompressedBase.h
+++ b/Eigen/src/SparseCore/SparseCompressedBase.h
@@ -113,6 +113,13 @@
   /** \returns whether \c *this is in compressed form. */
   inline bool isCompressed() const { return innerNonZeroPtr() == 0; }
 
+ protected:
+  Index coeffsStart() const {
+    const StorageIndex* outer = outerIndexPtr();
+    return (outer && derived().outerSize() > 0) ? internal::convert_index<Index>(outer[0]) : 0;
+  }
+
+ public:
   /** \returns a read-only view of the stored coefficients as a 1D array expression.
    *
    * \warning this method is for \b compressed \b storage \b only, and it will trigger an assertion otherwise.
@@ -120,7 +127,9 @@
    * \sa valuePtr(), isCompressed() */
   const Map<const Array<Scalar, Dynamic, 1>> coeffs() const {
     eigen_assert(isCompressed());
-    return Array<Scalar, Dynamic, 1>::Map(valuePtr(), nonZeros());
+    const Index start = coeffsStart();
+    const Scalar* values = valuePtr() + start;
+    return Array<Scalar, Dynamic, 1>::Map(values, nonZeros());
   }
 
   /** \returns a read-write view of the stored coefficients as a 1D array expression
@@ -135,7 +144,9 @@
    * \sa valuePtr(), isCompressed() */
   Map<Array<Scalar, Dynamic, 1>> coeffs() {
     eigen_assert(isCompressed());
-    return Array<Scalar, Dynamic, 1>::Map(valuePtr(), nonZeros());
+    const Index start = coeffsStart();
+    Scalar* values = valuePtr() + start;
+    return Array<Scalar, Dynamic, 1>::Map(values, nonZeros());
   }
 
   /** sorts the inner vectors in the range [begin,end) with respect to `Comp`
@@ -305,8 +316,7 @@
     }
   }
 
-  explicit ReverseInnerIterator(const SparseCompressedBase& mat)
-      : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(0), m_start(0), m_id(mat.nonZeros()) {
+  explicit ReverseInnerIterator(const SparseCompressedBase& mat) : ReverseInnerIterator(mat, Index(0)) {
     EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
   }
 
@@ -529,15 +539,17 @@
   typedef typename Derived::Scalar Scalar;
   typedef typename Derived::StorageIndex StorageIndex;
   static inline void run(SparseCompressedBase<Derived>& obj, Index, Index) {
-    Index begin_offset = 0;
-    Index end_offset = obj.nonZeros();
+    const StorageIndex* outer = obj.outerIndexPtr();
+    Index begin_offset = (outer && obj.outerSize() > 0) ? internal::convert_index<Index>(outer[0]) : 0;
+    Index end_offset = begin_offset + obj.nonZeros();
     CompressedStorageIterator<Scalar, StorageIndex> begin_it(begin_offset, obj.innerIndexPtr(), obj.valuePtr());
     CompressedStorageIterator<Scalar, StorageIndex> end_it(end_offset, obj.innerIndexPtr(), obj.valuePtr());
     std::sort(begin_it, end_it, Comp());
   }
   static inline Index check(const SparseCompressedBase<Derived>& obj, Index, Index) {
-    Index begin_offset = 0;
-    Index end_offset = obj.nonZeros();
+    const StorageIndex* outer = obj.outerIndexPtr();
+    Index begin_offset = (outer && obj.outerSize() > 0) ? internal::convert_index<Index>(outer[0]) : 0;
+    Index end_offset = begin_offset + obj.nonZeros();
     const StorageIndex* begin_it = obj.innerIndexPtr() + begin_offset;
     const StorageIndex* end_it = obj.innerIndexPtr() + end_offset;
     return std::is_sorted(begin_it, end_it, Comp()) ? 1 : 0;
diff --git a/Eigen/src/SparseCore/SparseRef.h b/Eigen/src/SparseCore/SparseRef.h
index aa8ae34..29cdda1 100644
--- a/Eigen/src/SparseCore/SparseRef.h
+++ b/Eigen/src/SparseCore/SparseRef.h
@@ -89,11 +89,19 @@
  protected:
   template <typename Expression>
   void construct(Expression& expr) {
-    if (expr.outerIndexPtr() == 0)
+    if (Expression::IsVectorAtCompileTime) {
+      const Index offset = expr.outerIndexPtr() ? expr.outerIndexPtr()[0] : 0;
+      auto inner_index_ptr = expr.innerIndexPtr();
+      auto value_ptr = expr.valuePtr();
+      if (inner_index_ptr) inner_index_ptr += offset;
+      if (value_ptr) value_ptr += offset;
+      internal::construct_at<Base>(this, expr.size(), expr.nonZeros(), inner_index_ptr, value_ptr);
+    } else if (expr.outerIndexPtr() == 0) {
       internal::construct_at<Base>(this, expr.size(), expr.nonZeros(), expr.innerIndexPtr(), expr.valuePtr());
-    else
+    } else {
       internal::construct_at<Base>(this, expr.rows(), expr.cols(), expr.nonZeros(), expr.outerIndexPtr(),
                                    expr.innerIndexPtr(), expr.valuePtr(), expr.innerNonZeroPtr());
+    }
   }
 };
 
@@ -268,6 +276,8 @@
   {
     EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
     EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
+    EIGEN_STATIC_ASSERT((!std::is_same<Derived, PlainObjectType>::value),
+                        THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
     Base::construct(expr.const_cast_derived());
   }
 };
diff --git a/test/sparse_block.cpp b/test/sparse_block.cpp
index 398658c..100e175 100644
--- a/test/sparse_block.cpp
+++ b/test/sparse_block.cpp
@@ -20,6 +20,152 @@
   return A.col(i);
 }
 
+template <typename T>
+typename T::Map make_map(T& A) {
+  return typename T::Map(A.rows(), A.cols(), A.nonZeros(), A.outerIndexPtr(), A.innerIndexPtr(), A.valuePtr(),
+                         A.innerNonZeroPtr());
+}
+
+template <typename T>
+auto innerpanel(T& A, Index start, Index size)
+    -> std::enable_if_t<(T::Flags & RowMajorBit) == RowMajorBit, decltype(A.middleRows(start, size))> {
+  return A.middleRows(start, size);
+}
+
+template <typename T>
+auto innerpanel(T& A, Index start, Index size)
+    -> std::enable_if_t<(T::Flags & RowMajorBit) == 0, decltype(A.middleCols(start, size))> {
+  return A.middleCols(start, size);
+}
+
+template <int Options>
+void init_inner_panel_diag(SparseMatrix<double, Options>& m) {
+  m.resize(4, 4);
+  m.insert(0, 0) = 10.0;
+  m.insert(1, 1) = 20.0;
+  m.insert(2, 2) = 30.0;
+  m.insert(3, 3) = 40.0;
+  m.makeCompressed();
+}
+
+template <typename MatrixType>
+std::enable_if_t<(MatrixType::Flags & RowMajorBit) == 0> init_inner_panel_offset(MatrixType& m) {
+  m.resize(4, 4);
+  m.insert(3, 0) = 13.0;
+  m.insert(1, 1) = 11.0;
+  m.insert(0, 2) = 20.0;
+  m.insert(2, 2) = 30.0;
+  m.insert(3, 3) = 40.0;
+  m.makeCompressed();
+}
+
+template <typename MatrixType>
+std::enable_if_t<(MatrixType::Flags & RowMajorBit) == RowMajorBit> init_inner_panel_offset(MatrixType& m) {
+  m.resize(4, 4);
+  m.insert(0, 3) = 13.0;
+  m.insert(1, 1) = 11.0;
+  m.insert(2, 0) = 20.0;
+  m.insert(2, 2) = 30.0;
+  m.insert(3, 3) = 40.0;
+  m.makeCompressed();
+}
+
+template <typename MatrixType, typename Init, typename Verify>
+void verify_plain_ref_map(const Init& init, const Verify& verify) {
+  {
+    MatrixType matrix;
+    init(matrix);
+    verify(matrix, matrix, 0);
+  }
+
+  {
+    MatrixType matrix;
+    init(matrix);
+    Ref<MatrixType> ref(matrix);
+    verify(ref, matrix, 1);
+  }
+
+  {
+    MatrixType matrix;
+    init(matrix);
+    auto map = make_map(matrix);
+    verify(map, matrix, 2);
+  }
+}
+
+template <typename XprType, typename MatrixType>
+void verify_inner_panel_coeffs(XprType& xpr, MatrixType& storage, int variant) {
+  auto middle = innerpanel(xpr, 1, 2);
+  auto coeffs = middle.coeffs();
+  const double updated0 = 200.0 + 10.0 * variant;
+  const double updated1 = 300.0 + 10.0 * variant;
+  VERIFY_IS_EQUAL(coeffs.size(), 2);
+  VERIFY_IS_EQUAL(coeffs[0], 20.0);
+  VERIFY_IS_EQUAL(coeffs[1], 30.0);
+
+  coeffs[0] = updated0;
+  coeffs[1] = updated1;
+  VERIFY_IS_EQUAL(storage.coeff(0, 0), 10.0);
+  VERIFY_IS_EQUAL(storage.coeff(1, 1), updated0);
+  VERIFY_IS_EQUAL(storage.coeff(2, 2), updated1);
+}
+
+template <typename XprType, typename MatrixType>
+void verify_inner_vector_reverse_and_sortedness(XprType& xpr, MatrixType& storage, int) {
+  auto inner = innervec(xpr, 2);
+  VERIFY_IS_EQUAL(inner.innerIndicesAreSorted(), 1);
+
+  typedef decltype(inner) InnerType;
+  typename InnerType::ReverseInnerIterator rit(inner);
+  VERIFY(rit);
+  VERIFY_IS_EQUAL(rit.index(), 2);
+  VERIFY_IS_EQUAL(rit.value(), 30.0);
+  --rit;
+  VERIFY(rit);
+  VERIFY_IS_EQUAL(rit.index(), 0);
+  VERIFY_IS_EQUAL(rit.value(), 20.0);
+  --rit;
+  VERIFY(!rit);
+
+  const Index offset = storage.outerIndexPtr()[2];
+  typename MatrixType::StorageIndex tmp_inner = storage.innerIndexPtr()[offset];
+  storage.innerIndexPtr()[offset] = storage.innerIndexPtr()[offset + 1];
+  storage.innerIndexPtr()[offset + 1] = tmp_inner;
+  double tmp_value = storage.valuePtr()[offset];
+  storage.valuePtr()[offset] = storage.valuePtr()[offset + 1];
+  storage.valuePtr()[offset + 1] = tmp_value;
+
+  VERIFY_IS_EQUAL(inner.innerIndicesAreSorted(), 0);
+  inner.sortInnerIndices();
+  VERIFY_IS_EQUAL(inner.innerIndicesAreSorted(), 1);
+
+  typename InnerType::InnerIterator it(inner);
+  VERIFY(it);
+  VERIFY_IS_EQUAL(it.index(), 0);
+  VERIFY_IS_EQUAL(it.value(), 20.0);
+  ++it;
+  VERIFY(it);
+  VERIFY_IS_EQUAL(it.index(), 2);
+  VERIFY_IS_EQUAL(it.value(), 30.0);
+  ++it;
+  VERIFY(!it);
+}
+
+void check_inner_panel_compressed_api() {
+  verify_plain_ref_map<SparseMatrix<double>>(
+      [](SparseMatrix<double>& matrix) { init_inner_panel_diag(matrix); },
+      [](auto& xpr, auto& matrix, int variant) { verify_inner_panel_coeffs(xpr, matrix, variant); });
+  verify_plain_ref_map<SparseMatrix<double, RowMajor>>(
+      [](SparseMatrix<double, RowMajor>& matrix) { init_inner_panel_diag(matrix); },
+      [](auto& xpr, auto& matrix, int variant) { verify_inner_panel_coeffs(xpr, matrix, variant); });
+  verify_plain_ref_map<SparseMatrix<double>>(
+      [](SparseMatrix<double>& matrix) { init_inner_panel_offset(matrix); },
+      [](auto& xpr, auto& matrix, int variant) { verify_inner_vector_reverse_and_sortedness(xpr, matrix, variant); });
+  verify_plain_ref_map<SparseMatrix<double, RowMajor>>(
+      [](SparseMatrix<double, RowMajor>& matrix) { init_inner_panel_offset(matrix); },
+      [](auto& xpr, auto& matrix, int variant) { verify_inner_vector_reverse_and_sortedness(xpr, matrix, variant); });
+}
+
 template <typename SparseMatrixType>
 void sparse_block(const SparseMatrixType& ref) {
   const Index rows = ref.rows();
@@ -326,5 +472,6 @@
     AnnoyingScalar::dont_throw = true;
 #endif
     CALL_SUBTEST_5((sparse_block(SparseMatrix<AnnoyingScalar>(r, c))));
+    CALL_SUBTEST_6(check_inner_panel_compressed_api());
   }
 }
diff --git a/test/sparse_ref.cpp b/test/sparse_ref.cpp
index bfedcd9..95cc479 100644
--- a/test/sparse_ref.cpp
+++ b/test/sparse_ref.cpp
@@ -37,19 +37,29 @@
 void check_const_correctness(const PlainObjectType &) {
   // verify that ref-to-const don't have LvalueBit
   typedef std::add_const_t<PlainObjectType> ConstPlainObjectType;
-  VERIFY(!(internal::traits<Ref<ConstPlainObjectType> >::Flags & LvalueBit));
-  VERIFY(!(internal::traits<Ref<ConstPlainObjectType, Aligned> >::Flags & LvalueBit));
+  VERIFY(!(internal::traits<Ref<ConstPlainObjectType>>::Flags & LvalueBit));
+  VERIFY(!(internal::traits<Ref<ConstPlainObjectType, Aligned>>::Flags & LvalueBit));
   VERIFY(!(Ref<ConstPlainObjectType>::Flags & LvalueBit));
   VERIFY(!(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit));
 }
 
 template <typename B>
-EIGEN_DONT_INLINE void call_ref_1(Ref<SparseMatrix<float> > a, const B &b) {
+EIGEN_DONT_INLINE void call_ref_1(Ref<SparseMatrix<float>> a, const B &b) {
   VERIFY_IS_EQUAL(a.toDense(), b.toDense());
 }
 
 template <typename B>
-EIGEN_DONT_INLINE void call_ref_2(const Ref<const SparseMatrix<float> > &a, const B &b) {
+EIGEN_DONT_INLINE void call_ref_2(const Ref<const SparseMatrix<float>> &a, const B &b) {
+  VERIFY_IS_EQUAL(a.toDense(), b.toDense());
+}
+
+template <typename B>
+EIGEN_DONT_INLINE void call_ref_rm_1(Ref<SparseMatrix<float, RowMajor>> a, const B &b) {
+  VERIFY_IS_EQUAL(a.toDense(), b.toDense());
+}
+
+template <typename B>
+EIGEN_DONT_INLINE void call_ref_rm_2(const Ref<const SparseMatrix<float, RowMajor>> &a, const B &b) {
   VERIFY_IS_EQUAL(a.toDense(), b.toDense());
 }
 
@@ -60,23 +70,236 @@
 }
 
 template <typename B>
-EIGEN_DONT_INLINE void call_ref_4(Ref<SparseVector<float> > a, const B &b) {
+EIGEN_DONT_INLINE void call_ref_4(Ref<SparseVector<float>> a, const B &b) {
   VERIFY_IS_EQUAL(a.toDense(), b.toDense());
 }
 
 template <typename B>
-EIGEN_DONT_INLINE void call_ref_5(const Ref<const SparseVector<float> > &a, const B &b) {
+EIGEN_DONT_INLINE void call_ref_5(const Ref<const SparseVector<float>> &a, const B &b) {
   VERIFY_IS_EQUAL(a.toDense(), b.toDense());
 }
 
+template <typename T>
+typename T::Map make_map(T &xpr) {
+  return typename T::Map(xpr.rows(), xpr.cols(), xpr.nonZeros(), xpr.outerIndexPtr(), xpr.innerIndexPtr(),
+                         xpr.valuePtr(), xpr.innerNonZeroPtr());
+}
+
+template <typename T>
+auto innerpanel(T &xpr, Index start, Index size)
+    -> std::enable_if_t<(T::Flags & RowMajorBit) == RowMajorBit, decltype(xpr.middleRows(start, size))> {
+  return xpr.middleRows(start, size);
+}
+
+template <typename T>
+auto innerpanel(T &xpr, Index start, Index size)
+    -> std::enable_if_t<(T::Flags & RowMajorBit) == 0, decltype(xpr.middleCols(start, size))> {
+  return xpr.middleCols(start, size);
+}
+
+template <typename T>
+auto innervector(T &xpr, Index index)
+    -> std::enable_if_t<(T::Flags & RowMajorBit) == RowMajorBit, decltype(xpr.row(index))> {
+  return xpr.row(index);
+}
+
+template <typename T>
+auto innervector(T &xpr, Index index) -> std::enable_if_t<(T::Flags & RowMajorBit) == 0, decltype(xpr.col(index))> {
+  return xpr.col(index);
+}
+
+template <typename XprType, typename ExpectedType>
+void verify_coeffs(const XprType &xpr, const ExpectedType &expected) {
+  auto coeffs = xpr.coeffs();
+  VERIFY_IS_EQUAL(coeffs.size(), expected.size());
+  for (Index i = 0; i < expected.size(); ++i) VERIFY_IS_EQUAL(coeffs[i], expected[i]);
+}
+
+template <int Options>
+void init_diag(SparseMatrix<float, Options> &m) {
+  m.resize(4, 4);
+  m.insert(0, 0) = 10.f;
+  m.insert(1, 1) = 20.f;
+  m.insert(2, 2) = 30.f;
+  m.insert(3, 3) = 40.f;
+  m.makeCompressed();
+}
+
+template <typename MatrixType>
+std::enable_if_t<(MatrixType::Flags & RowMajorBit) == 0> init_noncompressed_inner_panel(MatrixType &m) {
+  m.resize(4, 4);
+  m.reserve(VectorXi::Constant(4, 2));
+  m.insert(1, 1) = 11.f;
+  m.insert(2, 1) = 12.f;
+  m.insert(0, 2) = 20.f;
+  m.insert(2, 2) = 30.f;
+  m.insert(3, 3) = 40.f;
+}
+
+template <typename MatrixType>
+std::enable_if_t<(MatrixType::Flags & RowMajorBit) == RowMajorBit> init_noncompressed_inner_panel(MatrixType &m) {
+  m.resize(4, 4);
+  m.reserve(VectorXi::Constant(4, 2));
+  m.insert(1, 1) = 11.f;
+  m.insert(1, 2) = 12.f;
+  m.insert(2, 0) = 20.f;
+  m.insert(2, 2) = 30.f;
+  m.insert(3, 3) = 40.f;
+}
+
+template <typename MatrixType, typename Init, typename Verify>
+void verify_ref_and_map(const Init &init, const Verify &verify) {
+  {
+    MatrixType matrix;
+    init(matrix);
+    Ref<MatrixType> ref(matrix);
+    verify(ref, matrix, 0);
+  }
+
+  {
+    MatrixType matrix;
+    init(matrix);
+    auto map = make_map(matrix);
+    verify(map, matrix, 1);
+  }
+}
+
+template <typename XprType, typename MatrixType>
+void verify_slice_coeffs(XprType &xpr, MatrixType &storage, int variant) {
+  Array<float, 2, 1> expected;
+  expected << 20.f, 30.f;
+
+  Ref<MatrixType> middle(innerpanel(xpr, 1, 2));
+  Ref<const MatrixType> const_middle(innerpanel(xpr, 1, 2));
+  const float updated0 = 210.f + 10.f * variant;
+  const float updated1 = 310.f + 10.f * variant;
+
+  verify_coeffs(middle, expected);
+  verify_coeffs(const_middle, expected);
+  auto coeffs = middle.coeffs();
+  coeffs[0] = updated0;
+  coeffs[1] = updated1;
+  VERIFY_IS_EQUAL(storage.coeff(0, 0), 10.f);
+  VERIFY_IS_EQUAL(storage.coeff(1, 1), updated0);
+  VERIFY_IS_EQUAL(storage.coeff(2, 2), updated1);
+}
+
+template <typename MatrixType, typename XprType>
+float innerpanel_coeff(const XprType &xpr, Index outer, Index inner) {
+  if ((MatrixType::Flags & RowMajorBit) == RowMajorBit) return xpr.coeff(outer, inner);
+  return xpr.coeff(inner, outer);
+}
+
+template <typename MatrixType, typename XprType>
+void set_innerpanel_coeff(XprType &xpr, Index outer, Index inner, float value) {
+  if ((MatrixType::Flags & RowMajorBit) == RowMajorBit)
+    xpr.coeffRef(outer, inner) = value;
+  else
+    xpr.coeffRef(inner, outer) = value;
+}
+
+template <typename MatrixType>
+float storage_innerpanel_coeff(const MatrixType &storage, Index outer, Index inner) {
+  if ((MatrixType::Flags & RowMajorBit) == RowMajorBit) return storage.coeff(outer + 1, inner);
+  return storage.coeff(inner, outer + 1);
+}
+
+template <typename MatrixType>
+float storage_innervector_coeff(const MatrixType &storage, Index outer, Index inner) {
+  if ((MatrixType::Flags & RowMajorBit) == RowMajorBit) return storage.coeff(outer, inner);
+  return storage.coeff(inner, outer);
+}
+
+template <typename XprType, typename MatrixType>
+void verify_noncompressed_middle_binding(XprType &xpr, MatrixType &storage, int variant) {
+  struct Entry {
+    Index outer;
+    Index inner;
+    float value;
+  };
+
+  Ref<MatrixType> middle(innerpanel(xpr, 1, 2));
+  Ref<const MatrixType> const_middle(innerpanel(xpr, 1, 2));
+  const float updated0 = 111.f + variant;
+  const float updated1 = 121.f + variant;
+  const Entry expected[] = {{0, 1, 11.f}, {0, 2, 12.f}, {1, 0, 20.f}, {1, 2, 30.f}};
+
+  VERIFY(!middle.isCompressed());
+  VERIFY_IS_EQUAL(middle.nonZeros(), 4);
+  VERIFY(!const_middle.isCompressed());
+  VERIFY_IS_EQUAL(const_middle.nonZeros(), 4);
+  for (const auto &entry : expected) {
+    VERIFY_IS_EQUAL(innerpanel_coeff<MatrixType>(middle, entry.outer, entry.inner), entry.value);
+    VERIFY_IS_EQUAL(innerpanel_coeff<MatrixType>(const_middle, entry.outer, entry.inner), entry.value);
+  }
+
+  set_innerpanel_coeff<MatrixType>(middle, 0, 1, updated0);
+  set_innerpanel_coeff<MatrixType>(middle, 1, 0, updated1);
+  VERIFY_IS_EQUAL(storage_innerpanel_coeff(storage, 0, 1), updated0);
+  VERIFY_IS_EQUAL(storage_innerpanel_coeff(storage, 1, 0), updated1);
+}
+
+template <typename XprType, typename MatrixType>
+void verify_noncompressed_inner_vector_binding(XprType &xpr, MatrixType &storage, int variant) {
+  Array<float, 2, 1> expected;
+  expected << 20.f, 30.f;
+
+  Ref<SparseVector<float>> inner(innervector(xpr, 2));
+  Ref<const SparseVector<float>> const_inner(innervector(xpr, 2));
+  const float updated0 = 211.f + variant;
+  const float updated1 = 221.f + variant;
+
+  VERIFY(inner.isCompressed());
+  VERIFY_IS_EQUAL(inner.rows(), 4);
+  VERIFY_IS_EQUAL(inner.cols(), 1);
+  VERIFY_IS_EQUAL(inner.nonZeros(), 2);
+  VERIFY(const_inner.isCompressed());
+  VERIFY_IS_EQUAL(const_inner.rows(), 4);
+  VERIFY_IS_EQUAL(const_inner.cols(), 1);
+  VERIFY_IS_EQUAL(const_inner.nonZeros(), 2);
+  verify_coeffs(inner, expected);
+  verify_coeffs(const_inner, expected);
+
+  auto coeffs = inner.coeffs();
+  coeffs[0] = updated0;
+  coeffs[1] = updated1;
+  VERIFY_IS_EQUAL(storage_innervector_coeff(storage, 2, 0), updated0);
+  VERIFY_IS_EQUAL(storage_innervector_coeff(storage, 2, 2), updated1);
+}
+
+void check_ref_slice_coeffs() {
+  verify_ref_and_map<SparseMatrix<float>>(
+      [](SparseMatrix<float> &matrix) { init_diag(matrix); },
+      [](auto &xpr, auto &matrix, int variant) { verify_slice_coeffs(xpr, matrix, variant); });
+  verify_ref_and_map<SparseMatrix<float, RowMajor>>(
+      [](SparseMatrix<float, RowMajor> &matrix) { init_diag(matrix); },
+      [](auto &xpr, auto &matrix, int variant) { verify_slice_coeffs(xpr, matrix, variant); });
+}
+
+void check_noncompressed_ref_slices() {
+  verify_ref_and_map<SparseMatrix<float>>(
+      [](SparseMatrix<float> &matrix) { init_noncompressed_inner_panel(matrix); },
+      [](auto &xpr, auto &matrix, int variant) { verify_noncompressed_middle_binding(xpr, matrix, variant); });
+  verify_ref_and_map<SparseMatrix<float, RowMajor>>(
+      [](SparseMatrix<float, RowMajor> &matrix) { init_noncompressed_inner_panel(matrix); },
+      [](auto &xpr, auto &matrix, int variant) { verify_noncompressed_middle_binding(xpr, matrix, variant); });
+}
+
+void check_noncompressed_ref_inner_vectors() {
+  verify_ref_and_map<SparseMatrix<float>>(
+      [](SparseMatrix<float> &matrix) { init_noncompressed_inner_panel(matrix); },
+      [](auto &xpr, auto &matrix, int variant) { verify_noncompressed_inner_vector_binding(xpr, matrix, variant); });
+  verify_ref_and_map<SparseMatrix<float, RowMajor>>(
+      [](SparseMatrix<float, RowMajor> &matrix) { init_noncompressed_inner_panel(matrix); },
+      [](auto &xpr, auto &matrix, int variant) { verify_noncompressed_inner_vector_binding(xpr, matrix, variant); });
+}
+
 void call_ref() {
   SparseMatrix<float> A = MatrixXf::Random(10, 10).sparseView(0.5, 1);
   SparseMatrix<float, RowMajor> B = MatrixXf::Random(10, 10).sparseView(0.5, 1);
   SparseMatrix<float> C = MatrixXf::Random(10, 10).sparseView(0.5, 1);
   C.reserve(VectorXi::Constant(C.outerSize(), 2));
   const SparseMatrix<float> &Ac(A);
-  Block<SparseMatrix<float> > Ab(A, 0, 1, 3, 3);
-  const Block<SparseMatrix<float> > Abc(A, 0, 1, 3, 3);
   SparseVector<float> vc = VectorXf::Random(10).sparseView(0.5, 1);
   SparseVector<float, RowMajor> vr = VectorXf::Random(10).sparseView(0.5, 1);
   SparseMatrix<float> AA = A * A;
@@ -101,17 +324,37 @@
   VERIFY(!C.isCompressed());
   VERIFY_EVALUATION_COUNT(call_ref_3(C, C), 1);
 
-  Ref<SparseMatrix<float> > Ar(A);
+  Ref<SparseMatrix<float>> Ar(A);
   VERIFY_IS_APPROX(Ar + Ar, A + A);
   VERIFY_EVALUATION_COUNT(call_ref_1(Ar, A), 0);
   VERIFY_EVALUATION_COUNT(call_ref_2(Ar, A), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_1(Ar.middleCols(1, 3), A.middleCols(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_2(Ar.middleCols(1, 3), A.middleCols(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_4(Ar.col(2), A.col(2)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_5(Ar.col(2), A.col(2)), 0);
 
-  Ref<SparseMatrix<float, RowMajor> > Br(B);
+  Ref<SparseMatrix<float, RowMajor>> Br(B);
   VERIFY_EVALUATION_COUNT(call_ref_1(Br.transpose(), Br.transpose()), 0);
   VERIFY_EVALUATION_COUNT(call_ref_2(Br, Br), 1);
   VERIFY_EVALUATION_COUNT(call_ref_2(Br.transpose(), Br.transpose()), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_rm_1(Br.middleRows(1, 3), B.middleRows(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_rm_2(Br.middleRows(1, 3), B.middleRows(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_4(Br.row(2), B.row(2).transpose()), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_5(Br.row(2), B.row(2).transpose()), 0);
 
-  Ref<const SparseMatrix<float> > Arc(A);
+  auto Am = make_map(A);
+  VERIFY_EVALUATION_COUNT(call_ref_1(Am.middleCols(1, 3), A.middleCols(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_2(Am.middleCols(1, 3), A.middleCols(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_4(Am.col(2), A.col(2)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_5(Am.col(2), A.col(2)), 0);
+
+  auto Bm = make_map(B);
+  VERIFY_EVALUATION_COUNT(call_ref_rm_1(Bm.middleRows(1, 3), B.middleRows(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_rm_2(Bm.middleRows(1, 3), B.middleRows(1, 3)), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_4(Bm.row(2), B.row(2).transpose()), 0);
+  VERIFY_EVALUATION_COUNT(call_ref_5(Bm.row(2), B.row(2).transpose()), 0);
+
+  Ref<const SparseMatrix<float>> Arc(A);
   //   VERIFY_EVALUATION_COUNT( call_ref_1(Arc, Arc),  0); // does not compile on purpose
   VERIFY_EVALUATION_COUNT(call_ref_2(Arc, Arc), 0);
 
@@ -140,6 +383,9 @@
     CALL_SUBTEST_1(check_const_correctness(SparseMatrix<float>()));
     CALL_SUBTEST_1(check_const_correctness(SparseMatrix<double, RowMajor>()));
     CALL_SUBTEST_2(call_ref());
+    CALL_SUBTEST_4(check_ref_slice_coeffs());
+    CALL_SUBTEST_4(check_noncompressed_ref_slices());
+    CALL_SUBTEST_4(check_noncompressed_ref_inner_vectors());
 
     CALL_SUBTEST_3(check_const_correctness(SparseVector<float>()));
     CALL_SUBTEST_3(check_const_correctness(SparseVector<double, RowMajor>()));