C++中是否有一个功能,可以像Python那样创建一个块矩阵numpy.bmat公司`?

2024-09-27 00:21:00 发布

您现在位置:Python中文网/ 问答频道 /正文

<>是否有C++中的函数创建一个简单的矩阵,如numpy.bmat?你知道吗

示例:

Python:

m = np.bmat([[A, B], [C, D]])
<> C++ +eEM>我所知道的唯一方法是使用^ a2}:

#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

Tags: 方法函数numpy示例nprange矩阵matrix
1条回答
网友
1楼 · 发布于 2024-09-27 00:21:00

结果证明使用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;
}

相关问题 更多 >

    热门问题