C ++矩阵表达式模板 [英] C++ matrix expression template

查看:142
本文介绍了C ++矩阵表达式模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是表达模板编程的新手,我浏览了这个教程。我很了解一些事情,但却无法将代码复制到工作代码中。有人可以帮忙吗?

我做了什么 - >从 [列表3] 我保存为matrix.h文件,但不知道我怎么能制作一个新的矩阵对象(确切地说,我不知道最后两个参数是什么)

谢谢。



< b>我尝试了什么:



Matrix.h文件是此处形成和在main.cpp中我不知道如何创建矩阵。请指教?

Hi, I am new to expression template programming, I went through this tutorial. It's pretty good I understand things a bit but not able to replicate code to working one. Can someone help ?
What I did -> From [listing 3] I saved it as matrix.h file but don't know how can I make a new matrix object(precisely, I don't know what will be last two params)
Thanks.

What I have tried:

Matrix.h file is form here and In main.cpp I don't how to create matrix. Please any guidance ?

推荐答案

左右参数的类型为LeftOp和RightOp,类型为Matrix。您可以通过查看清单中的模板代码来查看。以下是两个片段:
The right and left parameters would be of type LeftOp and RightOp and those are of type Matrix. You can see this by looking at the template code in the listing. Here are two snippets :
template< typename T, size_t n, size_t m, typename LeftOp, typename RightOp>
class EtMatrixAdd
{
public:
  EtMatrixAdd(const LeftOp& lhs, const RightOp& rhs) : 
       m_lhs(lhs), m_rhs(rhs) {}
 
  T ElementAt(size_t n, size_t m) const
  {
     return m_lhs.ElementAt(n, m) + m_rhs.ElementAt(n, m);
  }
 
private:
  const LeftOp& m_lhs;
  const RightOp& m_rhs;
};

// Replaces operator+ of Listing 2
template<typename T, size_t n, size_t m>
inline EtMatrixAdd<T, n, m, Matrix<T, n, m>, Matrix<T, n, m> >
operator + ( const Matrix<T, n, m>& lhs, const Matrix<T, n, m>& rhs )
{
   return EtMatrixAdd<T, n, m, Matrix<T, n, m>, Matrix<T, n, m>>( lhs, rhs );
}

正如您在第二个片段中看到的,两者都是以下类型:

As you can see in the second snippet, both are of type :

Matrix<T, n, m>;

其中,T是矩阵所包含的数据类型,n和m是矩阵中要添加的项的索引。有关声明矩阵的示例,请查看本文的第一页。

Where, T is the type of data the matrix holds and n and m are indexes of the items in the matrix that will be added. Look at the first page of the article for examples of declaring the matrix.


这篇关于C ++矩阵表达式模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆