Operator overloading for Boost interval (vectors) I am a complete newbie to C++ and programming in general. I need to write something for scientific purposes and as such, performance is crucial. 

I introduced two types, matrices and vectors with intervals as entries, and now want to make them "able to work with", i.e. define basic operations. The type definitions:

#include &lt;eigen3/Eigen/Dense&gt;
#include &lt;boost/numeric/interval.hpp&gt;

namespace bn = boost::numeric;
namespace bi = bn::interval_lib;

// Interval typedefs
using Interval = bn::interval&lt;
        double,
        bi::policies&lt;
            bi::save_state&lt;bi::rounded_transc_std&lt;double&gt; &gt;,
            bi::checking_base&lt;double&gt;
        &gt;
    &gt;;

using Matrix = Eigen::Matrix&lt;Interval, 3, 3&gt;;
using Vector = Eigen::Matrix&lt;Interval, 3, 1&gt;;


Luckily, matrix products work without extra definitions (although I need to call a.lazyProduct(b), otherwise I receive an "operator unambiguous" error, which I don't understand), but inner products and multiplications with constants do not. It seems that I have to manually overload the multiplications, which I did as follows:

Vector operator* (const double&amp; x, const Vector&amp; y)
  {
  Vector res;
  for(int i = 0; i&lt;3;i++) {
    res[i] = x*y[i];
  }
  return res;
  }

Matrix operator* (const double&amp; x, const Matrix&amp; y)
  {
  Matrix res;
  for(int i = 0; i&lt;3; i++) {
    for(int j = 0; j&lt;3; j++){
      res(i,j) = x* y(i,j);
    }
  }
  return res;
  }

Interval inner_prod(const Vector&amp; x, const Vector&amp; y){
  Interval res(0.0);
  for(int i = 0; i&lt;3; i++){
    res += x[i]*y[i];
  }
  return res;
}

Interval inner_prod(const std::vector&lt;double&gt;&amp; x, const Vector&amp; y){
  Interval res(0.0);
  for(int i = 0; i&lt;3; i++){
    res += x[i] * y[i];
  }
  return res;
}


sizes are hard-coded as I will not be working with any others. I would love to have some feedback and improvement suggestions that focus on performance. Also for the inner product, I was hoping there was a way to make use of std::inner_product for possible better performance, but I can't figure out how. 

The boost/numeric/interval.hpp header gives a definition for multiplication of intervals with scalars, I must have made some mistake before that this didn't work. That just leaves my custom types.
I'll start by saying I haven't used boost::numeric, so what I say here is based on a quick skim of its documentation and source.

Instead of writing the constant 3 in so much of the code, we should be using rows() or cols() as appropriate (or perhaps even RowsAtCompileTime and ColsAtCompileTime).

Don't pass simple value types by reference - double values should be simply passed by value.

In fact, since Eigen::Matrix has a member that multiplies by a scalar, we can simplify these functions by simply calling that.  All we have to do is swap the argument order:

Vector operator*(double x, Vector y)
{
    return y *= x;
}

Matrix operator*(double x, const Matrix&amp; y)
{
    return y * x;
}


We can also simplify the dot-product function by using the standard inner_product algorithm.  We need to know that a vector's start and end iterators are obtained by data() and data() + size() respectively:

#include &lt;numeric&gt;

Interval inner_prod(const Vector&amp; x, const Vector&amp; y)
{
    return std::inner_product(x.data(), x.data() + x.size(),
                              y.data(), Interval{});
}

Interval inner_prod(const std::vector&lt;double&gt;&amp; x, const Vector&amp; y)
{
    if (x.size() != y.size())
        return {};              // or throw an exception, or something
    return std::inner_product(x.begin(), x.end(),
                              y.data(), Interval{});
}