繁体   English   中英

有没有一种方法可以在Math.Net上支持0x0矩阵?

[英]Is there a way to suport 0x0 Matrix on Math.Net?

我正在使用Matrix Nx2存储形成多边形的Point的列表。

我有一个函数返回一个子矩阵Nx2 ,该子矩阵包含点,这些点位于带有简单方程(例如y = 6的直线上方。

问题是有时子矩阵没有点。

然后我想做类似的事情:

using MathNet.Numerics.LinearAlgebra.Double;
double[,] pontos = { { }, { } };
Matrix mat = DenseMatrix.OfArray(pontos);

有没有一种方法可以支持0x0 Matrix并使Matrix.RowCount == 0

您在寻找什么是不可能的。 在创建任何Matrix MathNet总是会使用构造函数创建MatrixStorage

// MathNet.Numerics.LinearAlgebra.Storage.MatrixStorage<T>
using MathNet.Numerics.Properties;
using System;
using System.Runtime.Serialization;

protected MatrixStorage(int rowCount, int columnCount)
{
    if (rowCount <= 0)
    {
        throw new ArgumentOutOfRangeException("rowCount", Resources.MatrixRowsMustBePositive);
    }
    if (columnCount <= 0)
    {
        throw new ArgumentOutOfRangeException("columnCount", Resources.MatrixColumnsMustBePositive);
    }
    RowCount = rowCount;
    ColumnCount = columnCount;
}

因此,可以看到MathNet无法使用0x0 Matrix

更新:

您可以进行这样的修改:

static class EmptyDenseMatrix
{
    public static DenseMatrix Create()
    {
        var storage = DenseColumnMajorMatrixStorage<double>.OfArray(new double[1, 1]);
        var type = typeof(DenseColumnMajorMatrixStorage<double>);
        type.GetField("RowCount").SetValue(storage, 0);
        type.GetField("ColumnCount").SetValue(storage, 0);
        type.GetField("Data").SetValue(storage, new double[0]);

        return new DenseMatrix(storage);
    }
}

用法:

Console.WriteLine(EmptyDenseMatrix.Create());

得到:

DenseMatrix 0x0-Double

但是,在MathNet ,使用这种矩阵没有任何有意义的事情,例如

Console.WriteLine(EmptyDenseMatrix.Create()* EmptyDenseMatrix.Create());

得到:

System.ArgumentOutOfRangeException:矩阵的行数必须为正。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM