On Wed, Sep 9, 2009 at 8:32 AM, Mark Johnson <mj1@cog.brown.edu> wrote:
I'd like to write my own functions that take ublas entities as arguments.  Sometimes these will be ordinary ublas vectors, but sometimes they will be columns or rows projected from a larger matrix.  What type should I use for the arguments of my functions?  Of course my function shouldn't know or care whether its arguments are ordinary vectors or projected from something more complex.

The easiest, of course, is to use generic types in all of your functions.  For example:
template<typename VectorT>
VectorT::value_type my_norm_1(const VectorT& vec)
{
  return norm_1(vec);
}

or some variation of the above.

Sometimes you are going to have trouble with the overload set of generic functions where you want specializations that have clashes and can't use completely general types (e.g. a my_norm function for matrices as well as for vectors).

One way to do it is to use the vector_expression and matrix_expression types:

For example, you could use the following:
template<typename VectorT>
VectorT::value_type my_norm_1(const vector_expression<VectorT>& vec)
{
  return norm_1( vec() );
}

Note the use of the vec() instead of just vec now.  The operator() overloads to return a reference to VectorT from the expression.  (This is called the Barton-Nackman trick).  I believe you could do the following to make the code a little cleaner if you have lots of operations:

For example, you could use the following:
template<typename VectorT>
VectorT::value_type my_norm_1(const vector_expression<VectorT>& vec_exp)
{
  Vector& vec = vec_exp();

  return norm_1( vec);
}