<br><div class="gmail_quote">On Wed, Sep 9, 2009 at 8:32 AM, Mark Johnson <span dir="ltr">&lt;<a href="mailto:mj1@cog.brown.edu">mj1@cog.brown.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

I&#39;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&#39;t know or care whether its arguments are ordinary vectors or projected from something more complex.<br>

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

}<br><br>or some variation of the above.<br><br>Sometimes you are going to have trouble with the overload set of generic functions where you want specializations that have clashes and can&#39;t use completely general types (e.g. a my_norm function for matrices as well as for vectors).<br>

<br>One way to do it is to use the vector_expression and matrix_expression types:<br><br>For example, you could use the following:<br>template&lt;typename VectorT&gt;<br>
VectorT::value_type my_norm_1(const vector_expression&lt;VectorT&gt;&amp; vec)<br>
{<br>
  return norm_1( vec() );<br>
}<br>
<br>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:<br>

<br>For example, you could use the following:<br>
template&lt;typename VectorT&gt;<br>

VectorT::value_type my_norm_1(const vector_expression&lt;VectorT&gt;&amp; vec_exp)<br>

{<br>  Vector&amp; vec = vec_exp();<br><br>

  return norm_1( vec);<br>

}<br>

