You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++是否有类似Python numpy.bmat的便捷分块矩阵创建功能?

C++中类似numpy.bmat的分块矩阵创建方案

好问题!在C++生态里,确实没有像Python的numpy.bmat那样开箱即用、语法极度简洁的分块矩阵创建工具,但我们有几个替代方案,能实现类似的便捷性,比手动用boost::numeric::ublas::project拼接要舒服不少。

推荐方案:用Eigen库实现类numpy.bmat的分块构造

Eigen是C++里非常流行的线性代数库,它的分块矩阵构造语法非常直观,和numpy.bmat的思路几乎一致。举个例子,假设你有四个小矩阵,要拼成一个大矩阵:

#include <Eigen/Dense>
#include <iostream>

int main() {
    // 定义四个小矩阵
    Eigen::MatrixXd A(2, 2); A << 1, 2, 3, 4;
    Eigen::MatrixXd B(2, 1); B << 5, 6;
    Eigen::MatrixXd C(1, 2); C << 7, 8;
    Eigen::MatrixXd D(1, 1); D << 9;

    // 像numpy.bmat一样拼接成大矩阵
    Eigen::MatrixXd big_matrix(3, 3);
    big_matrix << A, B,
                  C, D;

    std::cout << big_matrix << std::endl;
    return 0;
}

运行后输出的就是分块拼接后的矩阵,语法和numpy.bmat一样清爽,而且Eigen的性能也非常出色,适合各种规模的矩阵操作。

Boost uBlas的简化实现

如果你已经在使用Boost uBlas,其实可以自己封装一个类似bmat的辅助函数,避免每次手动调用project。比如针对2x2分块的场景,我们可以写一个通用模板函数:

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

namespace ublas = boost::numeric::ublas;

// 封装2x2分块矩阵拼接函数
template<typename T>
ublas::matrix<T> bmat_2x2(const ublas::matrix<T>& top_left, 
                          const ublas::matrix<T>& top_right,
                          const ublas::matrix<T>& bottom_left,
                          const ublas::matrix<T>& bottom_right) {
    int total_rows = top_left.size1() + bottom_left.size1();
    int total_cols = top_left.size2() + top_right.size2();
    ublas::matrix<T> result(total_rows, total_cols);

    // 逐个复制分块矩阵到对应位置
    ublas::project(result, ublas::range(0, top_left.size1()), ublas::range(0, top_left.size2())) = top_left;
    ublas::project(result, ublas::range(0, top_right.size1()), ublas::range(top_left.size2(), total_cols)) = top_right;
    ublas::project(result, ublas::range(top_left.size1(), total_rows), ublas::range(0, bottom_left.size2())) = bottom_left;
    ublas::project(result, ublas::range(top_left.size1(), total_rows), ublas::range(top_left.size2(), total_cols)) = bottom_right;

    return result;
}

int main() {
    ublas::matrix<double> A(2,2); A <<= 1,2,3,4;
    ublas::matrix<double> B(2,1); B <<=5,6;
    ublas::matrix<double> C(1,2); C <<=7,8;
    ublas::matrix<double> D(1,1); D <<=9;

    auto big_matrix = bmat_2x2(A, B, C, D);

    // 打印拼接后的大矩阵
    for (std::size_t i = 0; i < big_matrix.size1(); ++i) {
        for (std::size_t j = 0; j < big_matrix.size2(); ++j) {
            std::cout << big_matrix(i,j) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}

这个封装后的函数就可以像numpy.bmat一样,直接传入分块矩阵得到大矩阵,比每次重复写project要省心多了。如果需要支持更多分块形式(比如3x3、1xN等),还可以扩展这个函数模板的参数和逻辑。

总结

如果追求最接近numpy.bmat的简洁体验,Eigen库是首选,它的分块赋值语法几乎和Python一样直观;如果已经依赖Boost uBlas,封装一个辅助函数也能大幅简化操作。

内容的提问来源于stack exchange,提问作者Pedro H. N. Vieira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:49:08