This question is motivated by the following implementation of the Runge Kutta formula for integration of first order ordinary differential equations (ODE).
template <typename Field, typename Vector>
auto
runge_kutta(const Vector& y,
std::function<Vector(Field, const Vector&)> rhs,
Field t, Field delta_t)
{
Vector k1 = rhs(t, y);
Vector k2 = rhs(t + delta_t/2, y + delta_t/2*k1);
Vector k3 = rhs(t + delta_t/2, y + delta_t/2*k2);
Vector k4 = rhs(t + delta_t, y + delta_t * k3);
return y + delta_t/6*(k1 + 2*k2 + 2*k3 + k4);
}
This may be used to integrate functions like the following
double f(double t, double y) { return y; }
But the Runge Kutta code above may also be used to solve higher order ODE or ODE with more than one dependent variable. For this the template parameter Vector has to be a container of numbers (or different kind of vectors) with suitably defined arithmetic operators.
Therefore I am looking for an implementation of a type for Vector
with the following properties:
- The type is a container of numbers or different kind of vectors.
- Suitably overloaded operators
operator+
,operator-
,operator*
andoperator/
. - No using declaration in the calling code necessary to use these operators.
- The standard library is reused.
The best, I came up with, is to create the operators for std::array
in a new namespace
namespace StackOverflow {
template<typename Field, typename Element, std::size_t Dim>
auto
operator*(Field lhs, const std::array<Element, Dim>& rhs);
// etc., etc.
}
and have suitable using declaration in the calling code (violating point 2 above)
using StackOverflow::operator+; ...
Another possibility is to derive publicly from std::array
. This works, but, as I have learned, this is explicitly forbidden by the C++ language standard.
Is it possible to reuse std::array
- retaining the first four properties above - without rewriting std::array
completely?