![](/img/trans.png)
[英]Can anyone explain why I get TypeLoad Exception while using MathNet.com Numerics Library?
[英]Can I change a matrix in MathNet Numerics Library to an array?
我正在開發一個需要矩陣運算的程序。 我決定使用Math.NET Numerics
庫。 我有一個Matrix
,我想更改其中一個值,但我不知道如何更改。 我的問題是,我們可以將Matrix
作為array
嗎? 我們可以將Matrix
轉換為array
嗎? 我檢查了這個庫中的AsArray
方法,但返回值為空。
Matrix<T>
(這是所有其他MathNet
矩陣類型的基本類型)提供了一個索引器public T this[int row, int column] { get; set; }
public T this[int row, int column] { get; set; }
public T this[int row, int column] { get; set; }
. 所以你可以用它來改變元素的值。
using MathNet.Numerics.LinearAlgebra.Double;
// ...
var matrix = DenseMatrix.Create(2, 2, 0);
Console.WriteLine(matrix);
matrix[1, 1] = 1;
Console.WriteLine(matrix);
給出:
DenseMatrix 2x2-Double
0 0
0 0
DenseMatrix 2x2-Double
0 0
0 1
要將Matrix<T>
轉換為T[,]
使用ToArray()
而不是AsArray()
。
MathNet
文檔說AsArray()
當且僅當此矩陣在內部由此類數組存儲時,才返回此矩陣的內部多維數組。 否則返回空值。 對返回數組和矩陣的更改會相互影響。 如果您總是需要一個獨立的數組,請改用 ToArray。
而ToArray()
將此矩陣作為多維數組返回。 返回的數組將獨立於該矩陣。 將為數組分配一個新的內存塊。
更新:
看起來AsArray()
根本不起作用。 隨着var data = new[,] { { 1d, 1d }, { 1d, 1d } }
new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfArray(data)).AsArray()
返回null
。 DenseMatrix.OfArray(data).AsArray()
。
更新2:
用ILSpy
檢查。
MathNet.Numerics.LinearAlgebra.Matrix:
/// <summary>
/// Returns the internal multidimensional array of this matrix if, and only if, this matrix is stored by such an array internally.
/// Otherwise returns null. Changes to the returned array and the matrix will affect each other.
/// Use ToArray instead if you always need an independent array.
/// </summary>
public T[,] AsArray()
{
return Storage.AsArray();
}
MathNet.Numerics.LinearAlgebra.Storage.MatrixStorage:
public virtual T[,] AsArray()
{
return null;
}
Matrix<T>.AsArray()
總是返回null
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.