Hello Klass,
you should use expressions for that (i.e. vector_expression or matrix_expression). Here is a sort example:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas;
template <class AE>
void test(const vector_expression<AE> &ve) {
std::cout << ve()(10) << std::endl;
}
int main()
{
vector<double> v1(20);
v1(10) = -10; v1(11)=-9;
scalar_vector<double> v2(20,2);
test(v1);
test(project(v1, range(1,20)));
test(v1+v2);
return 0;
}
Take note of the following:
- It is very important that in the function you have to reach the underlying object by calling operator() (due to the Curriously Reccuring Pattern ublas is using). After v() the rest are he same as when normally calling a member function of the class.
- The last call (test(v1+v2)) also does not copy the objects. The power of expression templates allows for just passing references to them and only the required operations will be executed (i.e. when in this function v()(10) is called, only the operation v1(10)+v2(10) will be executed). This will hold no matter how complicated the expression will be (as long as the elementary operations are supported by the expression template mechanism).
Take a look at this for other tips and techniques with ublas:
http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Effective_UBLAS
http://www.guwi17.de/ublas/index.html
Best
Nasos
> Date: Thu, 25 Mar 2010 17:17:36 +0100
> From: K.Vantournhout@gsi.de
> To: ublas@lists.boost.org
> Subject: [ublas] pass by reference for vector(_proxy)
>
> Hi all,
>
> I recently started to use uBlas and I could not figure the following out.
>
> If you want to pass a vector or vector_proxy as a reference to a function,
> how do you do this so that the data is not coppied?
>
> The function
>
> void foo(const vector<double> &v) {
> }
>
> makes a pure reference to any pure vector<double> passed to the function,
> however when passing a vector_proxy to it, it starts to copy the data.
>
> Is there a way to avoid this?
>
> Regards
>
> --
> ZAPHOD Hey, this rock...
> FORD Marble...
> ZAPHOD Marble...
> FORD Ice-covered marble...
> ZAPHOD Right... it's as slippery as... as... What's the slipperiest
> thing you can think of?
> FORD At the moment? This marble.
> ZAPHOD Right. This marble is as slippery as this marble.
>
> - Zaphod and Ford trying to get a grip on things in Brontitall, Fit the Tenth.
> _______________________________________________
> ublas mailing list
> ublas@lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: nasos_i@hotmail.com
Hotmail: Trusted email with Microsoft’s powerful SPAM protection. Sign up now.