From 75ae4de0b52fab92f1e9f1e4c1630a9901b90e5f Mon Sep 17 00:00:00 2001
From: Christoph Lehmann <christoph.lehmann@ufz.de>
Date: Sat, 23 Apr 2016 13:20:06 +0200
Subject: [PATCH] [MaL] pass Eigen::Map's by reference

---
 MathLib/ODE/FunctionHandles.h | 11 +++++++++--
 MathLib/ODE/OdeSolverTypes.h  |  8 ++++----
 Tests/MathLib/TestCVode.cpp   | 18 +++++++++---------
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/MathLib/ODE/FunctionHandles.h b/MathLib/ODE/FunctionHandles.h
index c268f21b68f..03023aeb085 100644
--- a/MathLib/ODE/FunctionHandles.h
+++ b/MathLib/ODE/FunctionHandles.h
@@ -44,7 +44,11 @@ struct FunctionHandlesImpl : FunctionHandles
 	bool call(const double t, const double* const y,
 	          double* const ydot) override
 	{
-		if (f) return f(t, MappedConstVector<N>{y}, MappedVector<N>{ydot});
+		if (f)
+		{
+			MappedVector<N> ydot_mapped{ydot};
+			return f(t, MappedConstVector<N>{y}, ydot_mapped);
+		}
 		return false;
 	}
 
@@ -52,10 +56,13 @@ struct FunctionHandlesImpl : FunctionHandles
 	                  double* const jac) override
 	{
 		if (df)
+		{
+			MappedMatrix<N, N> jac_mapped{jac};
 			return df(t,
 			          MappedConstVector<N>{y},
 			          MappedConstVector<N>{ydot},
-			          MappedMatrix<N, N>{jac});
+			          jac_mapped);
+		}
 		return false;
 	}
 
diff --git a/MathLib/ODE/OdeSolverTypes.h b/MathLib/ODE/OdeSolverTypes.h
index 5d8affc7876..b7d83163644 100644
--- a/MathLib/ODE/OdeSolverTypes.h
+++ b/MathLib/ODE/OdeSolverTypes.h
@@ -30,13 +30,13 @@ using MappedConstVector = MappedConstMatrix<N, 1>;
 
 template <unsigned N>
 using Function = std::function<bool(
-    const double t, MappedConstVector<N> const y, MappedVector<N> ydot)>;
+    const double t, MappedConstVector<N> const& y, MappedVector<N>& ydot)>;
 
 template <unsigned N>
 using JacobianFunction = std::function<bool(const double t,
-                                            MappedConstVector<N> const y,
-                                            MappedConstVector<N> ydot,
-                                            MappedMatrix<N, N> jac)>;
+                                            MappedConstVector<N> const& y,
+                                            MappedConstVector<N> const& ydot,
+                                            MappedMatrix<N, N>& jac)>;
 
 }  // namespace MathLib
 
diff --git a/Tests/MathLib/TestCVode.cpp b/Tests/MathLib/TestCVode.cpp
index 50f54bce121..75b2db5a56f 100644
--- a/Tests/MathLib/TestCVode.cpp
+++ b/Tests/MathLib/TestCVode.cpp
@@ -16,8 +16,8 @@ const double abs_tol = 1e-8;
 const double rel_tol = 1e-8;
 
 bool f(const double,
-       MathLib::MappedConstVector<1> const y,
-       MathLib::MappedVector<1> ydot)
+       MathLib::MappedConstVector<1> const& y,
+       MathLib::MappedVector<1>& ydot)
 {
 	if (y[0] <= 0.0) return false;
 
@@ -26,9 +26,9 @@ bool f(const double,
 }
 
 bool df(const double /*t*/,
-        MathLib::MappedConstVector<1> const y,
-        MathLib::MappedConstVector<1> /*ydot*/,
-        MathLib::MappedMatrix<1, 1> jac)
+        MathLib::MappedConstVector<1> const& y,
+        MathLib::MappedConstVector<1> const& /*ydot*/,
+        MathLib::MappedMatrix<1, 1>& jac)
 {
 	if (y[0] <= 0.0) return false;
 
@@ -42,8 +42,8 @@ struct ExtraData
 };
 
 bool f_extra(const double,
-             MathLib::MappedConstVector<1> const y,
-             MathLib::MappedVector<1> ydot,
+             MathLib::MappedConstVector<1> const& y,
+             MathLib::MappedVector<1>& ydot,
              ExtraData& data)
 {
 	if (y[0] <= 0.0) return false;
@@ -153,8 +153,8 @@ TEST(MathLibCVodeTest, ExponentialExtraData)
 
 	ExtraData data;
 	auto f_lambda = [&](double t,
-	                    MathLib::MappedConstVector<1> const y,
-	                    MathLib::MappedVector<1> ydot)
+	                    MathLib::MappedConstVector<1> const& y,
+	                    MathLib::MappedVector<1>& ydot)
 	{
 		return f_extra(t, y, ydot, data);
 	};
-- 
GitLab