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

Ubuntu环境下Polymake合并矩阵报错:无法确定列数

解决polymake中合并矩阵时的"can't determine the number of columns"错误

问题分析

你遇到的报错根源是:polymake的Matrix构造函数不支持直接将多个Matrix对象作为参数实现行堆叠。尽管每个输入矩阵的列数都是6,但构造函数无法正确解析这种参数格式,导致无法推断最终矩阵的列数。

解决方案

要合并多个同列数的矩阵(行堆叠),可以用以下两种方法解决:

方法1:使用行拼接操作符//

polymake提供//作为垂直拼接(行堆叠)的操作符,要求所有输入矩阵列数一致,直接用它连接所有矩阵即可:

use application "fan";
use application "common";

my $dense_unit_matrix = dense(unit_matrix<Int>(6));
my $m_int = new Matrix<Int>(unit_matrix<Int>(5));
my $arr2= new Vector<Int>([(1)x 5]);
my $extended_matrix = ($arr2 | $m_int);
my $arr3 = new Matrix<Int>([[ (1) x 6 ]]);
my $ar = new Matrix<Int>([[2, (-1) x 5]]);

# 用//实现行堆叠
$sp = $dense_unit_matrix // $ar // $extended_matrix // $arr3;

print $dense_unit_matrix->cols,$ar->cols,$extended_matrix->cols,$arr3->cols;
print "\n合并后矩阵列数:", $sp->cols, "\n";

方法2:收集所有行再构造矩阵

先将每个矩阵的行收集到数组中,再用数组引用构造最终矩阵:

use application "fan";
use application "common";

my $dense_unit_matrix = dense(unit_matrix<Int>(6));
my $m_int = new Matrix<Int>(unit_matrix<Int>(5));
my $arr2= new Vector<Int>([(1)x 5]);
my $extended_matrix = ($arr2 | $m_int);
my $arr3 = new Matrix<Int>([[ (1) x 6 ]]);
my $ar = new Matrix<Int>([[2, (-1) x 5]]);

# 收集所有矩阵的行
my @all_rows;
push @all_rows, @{$dense_unit_matrix->rows};
push @all_rows, @{$ar->rows};
push @all_rows, @{$extended_matrix->rows};
push @all_rows, @{$arr3->rows};

# 从行数组构造矩阵
$sp = new Matrix<Int>(\@all_rows);

print $dense_unit_matrix->cols,$ar->cols,$extended_matrix->cols,$arr3->cols;
print "\n合并后矩阵列数:", $sp->cols, "\n";

关键说明

  • polymake中,//用于垂直拼接(行堆叠),要求输入矩阵列数一致;|用于水平拼接(列合并),要求输入矩阵行数一致。
  • 直接将多个Matrix对象传入new Matrix<Int>(...)构造函数,会被解析为零散元素而非完整矩阵堆叠,这是报错的核心原因。

内容的提问来源于stack exchange,提问作者Jovana Dubak Ivanović

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 08:40:03