From ba471593662c4b2fb2d79562e73af93ecf8d2e5b Mon Sep 17 00:00:00 2001
From: Pauli Virtanen <pauli.t.virtanen@jyu.fi>
Date: Tue, 2 Aug 2022 18:06:30 +0300
Subject: [PATCH] array: eigen conversion to fixed size

---
 src/array.hpp | 16 ++++++++++++++++
 src/main.cpp  |  7 ++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/array.hpp b/src/array.hpp
index b6ff32f..e53a603 100644
--- a/src/array.hpp
+++ b/src/array.hpp
@@ -160,6 +160,22 @@ public:
                 {static_cast<Eigen::Index>(stride_[1]), static_cast<Eigen::Index>(stride_[0])}};
         }
 
+    template <size_t Rows, size_t Cols>
+    Eigen::Map< Eigen::Matrix<Scalar,Rows,Cols,Eigen::RowMajor> > to_fixed_matrix() const
+        {
+            static_assert(NDim == 2, "matrix must be two-dimensional");
+
+#ifndef NO_BOUNDS_CHECK
+            if (shape_[0] != Rows || shape_[1] != Cols)
+                throw std::out_of_range("incorrect shape");
+            if (stride_[0] != shape_[1] || stride_[1] != 1)
+                throw std::out_of_range("data not in row major order");
+#endif
+
+            return {(Scalar *)data_.data() + offset_,
+                static_cast<Eigen::Index>(shape_[0]), static_cast<Eigen::Index>(shape_[1])};
+        }
+
     operator EigenMap() const { return to_matrix(); }
 };
 
diff --git a/src/main.cpp b/src/main.cpp
index b45a36e..c361b4e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -23,7 +23,7 @@ int main()
 {
     std::vector<ADComplex> v(2*2*2);
     Array<ADComplex,3> Q0(v,{2,2,2},true);
-    auto Q = Q0.slice<0>(1);
+    auto Q = Q0.slice<0>(0);
 
     std::cout << Q0.index(1u,0u,0u) << std::endl;
 
@@ -31,9 +31,10 @@ int main()
 
     std::cout << Q.index(0u,0u) << std::endl;
 
-    auto mat = Q.to_matrix();
+    //auto mat = Q.to_matrix();
+    auto mat = Q.to_fixed_matrix<2,2>();
 
-    mat = mat * mat;
+    mat = mat * mat * mat;
 
     dump(Q0);
     std::cout << mat << std::endl;
-- 
GitLab