在C ++重用标准库的容器中实现数学向量

这个问题是由以下对一阶常微分方程(ODE)进行积分的Runge Kutta公式的实现引起的。

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);
}

这可以用于集成如下功能

double f(double t, double y) { return y; }

但是上面的Runge Kutta代码也可以用于求解高阶ODE或具有多个因变量的ODE。为此,模板参数Vector必须是带有适当定义的算术运算符的数字(或不同种类的矢量)容器。

Therefore I am looking for an implementation of a type for Vector with the following properties:

  1. The type is a container of numbers or different kind of vectors.
  2. Suitably overloaded operators operator+, operator-, operator* and operator/.
  3. No using declaration in the calling code necessary to use these operators.
  4. 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.
}

并在调用代码中有适当的using声明(违反上述第2点)

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?