boost::operators mixed arithmetic
based on the example here http://www.boost.org/doc/libs/release/libs/utility/operators.htm#example, I have implemented the following derived class of boost::numeric::ublas::vector
:
namespace Chebyshev
{
template<开发者_高级运维typename T>
class function_data : public boost::numeric::ublas::vector<T>,
boost::addable<function_data<T> >,
boost::subtractable<function_data<T> >,
boost::multipliable2<function_data<T>, T>,
boost::dividable2<function_data<T>, T>
{
public:
char dataflag;
function_data() : boost::numeric::ublas::vector<T>() {dataflag=0;} ///< The default empty constructor
function_data(const boost::numeric::ublas::vector<T>& vec) : boost::numeric::ublas::vector<T>(vec) {dataflag=0;} ///< The copy constructor without a flag.
function_data(const boost::numeric::ublas::vector<T>& vec, char flag) : boost::numeric::ublas::vector<T>(vec), dataflag(flag) {} ///< The copy constructor with a flag.
~function_data() {} ///< The destructor.
function_data<T>& operator= (const boost::numeric::ublas::vector<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); return *this;} ///< The assignment operator from a boost::numeric::ublas::vector<T>.
function_data<T>& operator= (const function_data<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); dataflag=in.dataflag; return *this;} ///< The assignment operator.
function_data<T>& operator+= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator+=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator-= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator-=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator*= (T in) {this->boost::numeric::ublas::vector<T>::operator*=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator/= (T in) {this->boost::numeric::ublas::vector<T>::operator/=(in); this->dataflag=this->dataflag; return *this;}
friend std::ostream& operator<< (std::ostream& os, const function_data<T>& fd) {os << "[type " << fd.dataflag << "] " << static_cast<boost::numeric::ublas::vector<T> >(fd); return os;} ///< The << operator.
};
}
However, compiling the following snippet of code
int main( int argc, char ** argv)
{
Chebyshev::function_data<std::complex<double> > u;
/* some stuff putting values in u */
std::cout << u*2 << std::endl;
return 0;
}
gives a "ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second" warning and proceeds to give the ublas vector_expression version (with u
cast as some kind of vector_expression) and my version (with 2
cast as a const std::complex<double>&
).
I would like to be able to use mixed arithmetic in my class as in the above snippet of code, but the explanation on the boost::operators website isn't clear to me. What do I have to add or change in my class to allow this?
Also, in the example, the inheritance list has each class inside the last >
of the previous class. I don't see any difference in the output of the compiler whether I write it that way or the way I have above. Which is the proper way to write it?
Best regards, Brett.
Boost uBLAS has all the operators that you need to vector arithmetic. Here is a sample program to highlight it:
#include <iostream>
#include "boost/numeric/ublas/vector.hpp"
#include "boost/numeric/ublas/io.hpp"
using namespace boost::numeric::ublas;
int main()
{
vector<int> v1(4);
vector<int> v2(4);
vector<int> v3(4);
vector< std::complex<double> > v4(4);
for (size_t i = 0; i < v1.size(); i++)
v1(i) = (i + 1) * 3;
for (size_t i = 0; i < v1.size(); i++)
v2(i) = (i + 1) * 10;
for (size_t i = 0; i < v4.size(); i++)
v4(i) = std::complex<double>(v1(i), v2(i));
std::cout << "v1: " << v1 << std::endl;
std::cout << "v2: " << v2 << std::endl;
std::cout << "v3 = v2 * 3: " << (v3 = v2 * 3) << std::endl;
std::cout << "v4: " << v4 << std::endl;
std::cout << "v1 + v2: " << v1 + v2 << std::endl;
std::cout << "v1 - v2: " << v1 - v2 << std::endl;
std::cout << "v1 * 3: " << v1 * 3 << std::endl;
std::cout << "(v2 * 2) / 3: " << (v2 * 2) / 3 << std::endl;
std::cout << "v4 * 3: " << v4 * 3 << std::endl;
std::cout << "v4 + v4" << v4 + v4 << std::endl;
std::cout << "element_prod(v1, v2)" << element_prod(v1, v2) << std::endl;
std::cout << "element_div(v2, v1)" << element_div(v2, v1) << std::endl;
return 0;
}
Here is the output I got when I compiled and ran using g++ v4.1.2:
[ublas]$ g++ -o vector vector.cpp
[ublas]$ ./vector
v1: [4](3,6,9,12)
v2: [4](10,20,30,40)
v3 = v2 * 3: [4](30,60,90,120)
v4: [4]((3,10),(6,20),(9,30),(12,40))
v1 + v2: [4](13,26,39,52)
v1 - v2: [4](-7,-14,-21,-28)
v1 * 3: [4](9,18,27,36)
(v2 * 2) / 3: [4](6,13,20,26)
v4 * 3: [4]((9,30),(18,60),(27,90),(36,120))
v4 + v4[4]((6,20),(12,40),(18,60),(24,80))
element_prod(v1, v2)[4](30,120,270,480)
element_div(v2, v1)[4](3,3,3,3)
精彩评论