boost::运算符混合运算
boost::operators mixed arithmetic
基于这里的示例http://www.boost.org/doc/libs/release/libs/utility/operators.htm#example,我已经实现了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.
};
}
然而,编译以下代码片段
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;
}
给出一个"ISO C++说这些都是模糊的,即使第一个的最差转换比第二个的最坏转换好"警告,并继续给出ublas vector_expression版本(u
转换为某种vector_eexpression)和我的版本(2
转换为const std::complex<double>&
)。
我希望能够像上面的代码片段一样在课堂上使用混合算术,但boost::operators网站上的解释我不清楚。我必须在课堂上添加或更改什么才能实现这一点?
此外,在本例中,继承列表中的每个类都位于前一个类的最后一个>
中。无论我是用这种方式还是用上面的方式写,我都看不出编译器的输出有什么不同。哪种写法正确?
致以最良好的问候,布雷特。
Boost uBLAS具有矢量运算所需的所有运算符。以下是一个突出显示它的示例程序:
#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;
}
以下是我使用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)
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- GCC本机矩阵运算库
- 混合组合和继承的C++问题
- 在混合代码库中将C转换为C++时出现许多包含错误
- 位阵列上的快速AND运算
- 字符串中int的加法运算
- D3D11-将混合权重和索引传递到顶点着色器
- 算术运算的结果类似于:C浮点变量中的1/3
- 如何使用OpenMP并行化此矩阵时间矢量运算
- 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- C++分数混合比较运算符错误
- 是否可以混合使用SFINAE和模板专业化?
- 如何在 Python C++ 混合库中调试非确定性分段错误?
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 如何在 vtk Poly 数据上进行布尔运算?
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- C++11 中的混合列表初始化
- C++以迭代方式搜索混合类型地图
- C++中混合数据类型的算术运算
- boost::运算符混合运算