【问题标题】:Is there a functionality in C++ to create a block matrix as simple as Python's `numpy.bmat`?C++ 中是否有创建像 Python 的 `numpy.bmat` 一样简单的块矩阵的功能?
【发布时间】:2018-02-12 19:50:44
【问题描述】:

C++ 中是否有创建像numpy.bmat 这样简单的块矩阵的功能?

例子:

Python

m = np.bmat([[A, B], [C, D]])

C++ 我知道的唯一方法是使用boost::numeric::ublas::project

#include <boost/numeric/ublas/matrix.hpp>

boost::numeric::ublas::matrix m;

//...

r1 = boost::numeric::ublas::range(i1, i2);
r2 = boost::numeric::ublas::range(i3, i4);
boost::numeric::ublas::project(m, r1, r2) = A;

// repeat code above for other submatrices, each time calculating the appropriate indices

【问题讨论】:

  • bmat 可能有一个简单的语法,但代码本身并不是那么简单。单击文档的[source] 链接。
  • 它似乎基于串联。如果 C++ 中没有这样的函数,也许可以创建它......我会尝试考虑一个。

标签: python c++ numpy boost


【解决方案1】:

结果证明Eigen 更易于使用。这是一个示例程序:

#include <eigen3/Eigen/Dense>
#include <iostream>
using Eigen::MatrixXd;
int main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;

  MatrixXd mx(4,4);
  mx.block<2,2>(0,0) << m;
  mx.block<2,2>(2,2) << m;
  std::cout << mx << std::endl;
}

【讨论】:

猜你喜欢
  • 2022-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 2017-10-08
相关资源
最近更新 更多