我一直在尝试让 MathNet.Numerics 库工作。每次尝试初始化矩阵时,我都会收到这个奇怪的运行时错误。我已经在互联网上搜索过类似问题的帖子,但没有任何运气。这让我相信我可能错过了一些可能很明显的事情。我将描述我如何包含库以及产生错误的代码,尽管我不认为代码是问题所在,因为它取自 math.net 示例站点如何使用矩阵。
所以!我尝试了两种导入库的方法。第一个是打开NuGet包管理器控制台并编写Install-Package MathNet.Numerics就是这样!第二种方法是打开 Manage NuGet Packages。然后搜索 mathnet.numerics 然后安装它。对我来说,这与以前的方法完全相同,对吧?
无论如何,现在该库似乎已导入,这是因为我可以构建以下代码而不会出现任何错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathNet.Numerics.LinearAlgebra; // Maybe unnecessary to include this one as well as the one below
using MathNet.Numerics.LinearAlgebra.Double;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Matrix<double> A = DenseMatrix.OfArray(new double[,] {
{1,1,1,1},
{1,2,3,4},
{4,3,2,1}});
Vector<double>[] nullspace = A.Kernel();
}
}
}
然后我构建它,这可能就是问题所在。我有我的怀疑!我只是构建它,即右键单击解决方案并构建!没有错误!
然后当我运行程序时,当 DeseMatrix.OfArray.... 被执行时,我得到以下异常。
MathNet.Numerics.dll 中发生了“System.TypeInitializationException”类型的未处理异常附加信息:“MathNet.Numerics.LinearAlgebra.Storage.MatrixStorage`1”的类型初始化程序引发了异常。
所以总结一下这个相当长的问题:我错过了什么?
PS。该项目必须使用 .Net 3.5 运行。别担心,我也尝试在 .net 4.5 中做同样的事情。我也在多台计算机上尝试过这个解决方案,包括 win 7 和 8。