boost::运算符混合算术

发布于 2024-11-11 06:07:37 字数 3457 浏览 3 评论 0原文

基于此处的示例 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转换为某种向量表达式)和我的版本(2 转换为 const std::complex&)。

我希望能够在我的类中使用混合算术,如上面的代码片段所示,但 boost::operators 网站上的解释对我来说并不清楚。我必须在班级中添加或更改哪些内容才能实现此目的?

此外,在该示例中,继承列表将每个类都放在前一个类的最后一个 > 内。无论我是这样写还是按照上面的方式编写,我都没有看到编译器的输出有任何差异。哪种写法才是正确的呢?

最好的问候,布雷特。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-11-18 06:07:37

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文