本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Complex32.DenseMatrix类的典型用法代码示例。如果您正苦于以下问题:C# DenseMatrix类的具体用法?C# DenseMatrix怎么用?C# DenseMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DenseMatrix类属于MathNet.Numerics.LinearAlgebra.Complex32命名空间,在下文中一共展示了DenseMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanCheckRankOfSquareSingular
public void CanCheckRankOfSquareSingular(int order)
{
var matrixA = new DenseMatrix(order, order);
matrixA[0, 0] = 1;
matrixA[order - 1, order - 1] = 1;
for (var i = 1; i < order - 1; i++)
{
matrixA[i, i - 1] = 1;
matrixA[i, i + 1] = 1;
matrixA[i - 1, i] = 1;
matrixA[i + 1, i] = 1;
}
var factorSvd = matrixA.Svd();
Assert.AreEqual(factorSvd.Determinant, Complex32.Zero);
Assert.AreEqual(factorSvd.Rank, order - 1);
}
示例2: CanCheckRankOfSquareSingular
public void CanCheckRankOfSquareSingular([Values(10, 50, 100)] int order)
{
var A = new DenseMatrix(order, order);
A[0, 0] = 1;
A[order - 1, order - 1] = 1;
for (var i = 1; i < order - 1; i++)
{
A[i, i - 1] = 1;
A[i, i + 1] = 1;
A[i - 1, i] = 1;
A[i + 1, i] = 1;
}
var factorEvd = A.Evd();
Assert.AreEqual(factorEvd.Determinant, Complex32.Zero);
Assert.AreEqual(factorEvd.Rank, order - 1);
}
示例3: CanAddSparseMatricesBothWays
public void CanAddSparseMatricesBothWays()
{
var m1 = new SparseMatrix(1, 3);
var m2 = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } });
var sum1 = m1 + m2;
var sum2 = m2 + m1;
Assert.IsTrue(sum1.Equals(m2));
Assert.IsTrue(sum1.Equals(sum2));
var sparseResult = new SparseMatrix(1, 3);
sparseResult.Add(m2, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1));
sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } });
sparseResult.Add(m1, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1));
sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } });
m1.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(sum1));
sparseResult = SparseMatrix.OfArray(new Complex32[,] { { 0, 1, 1 } });
sparseResult.Add(sparseResult, sparseResult);
Assert.IsTrue(sparseResult.Equals(2*sum1));
var denseResult = new DenseMatrix(1, 3);
denseResult.Add(m2, denseResult);
Assert.IsTrue(denseResult.Equals(sum1));
denseResult = DenseMatrix.OfArray(new Complex32[,] {{0, 1, 1}});
denseResult.Add(m1, denseResult);
Assert.IsTrue(denseResult.Equals(sum1));
var m3 = DenseMatrix.OfArray(new Complex32[,] {{0, 1, 1}});
var sum3 = m1 + m3;
var sum4 = m3 + m1;
Assert.IsTrue(sum3.Equals(m3));
Assert.IsTrue(sum3.Equals(sum4));
}
示例4: Identity
/// <summary>
/// Initializes a square <see cref="DenseMatrix"/> with all zero's except for ones on the diagonal.
/// </summary>
/// <param name="order">the size of the square matrix.</param>
/// <returns>A dense identity matrix.</returns>
/// <exception cref="ArgumentException">
/// If <paramref name="order"/> is less than one.
/// </exception>
public static DenseMatrix Identity(int order)
{
var m = new DenseMatrix(order);
for (var i = 0; i < order; i++)
{
m.Data[(i * order) + i] = 1.0f;
}
return m;
}
示例5: OfDiagonalVector
/// <summary>
/// Create a new dense matrix with the diagonal as a copy of the given vector.
/// This new matrix will be independent from the vector.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfDiagonalVector(int rows, int columns, Vector<Complex32> diagonal)
{
var m = new DenseMatrix(rows, columns);
m.SetDiagonal(diagonal);
return m;
}
示例6: OfDiagonalArray
/// <summary>
/// Create a new dense matrix with the diagonal as a copy of the given array.
/// This new matrix will be independent from the array.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfDiagonalArray(int rows, int columns, Complex32[] diagonal)
{
var m = new DenseMatrix(rows, columns);
m.SetDiagonal(diagonal);
return m;
}
示例7: LUFailsWithNonSquareMatrix
public void LUFailsWithNonSquareMatrix()
{
var matrix = new DenseMatrix(3, 1);
Assert.That(() => matrix.LU(), Throws.ArgumentException);
}
示例8: Add
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>
/// <param name="other">The matrix to add to this matrix.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
public override Matrix<Complex32> Add(Matrix<Complex32> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw new ArgumentOutOfRangeException("other", Resources.ArgumentMatrixDimensions);
}
Matrix<Complex32> result;
if (other is DiagonalMatrix)
{
result = new DiagonalMatrix(RowCount, ColumnCount);
}
else
{
result = new DenseMatrix(RowCount, ColumnCount);
}
Add(other, result);
return result;
}
示例9: Subtract
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>
/// <param name="other">The matrix to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
public override Matrix<Complex32> Subtract(Matrix<Complex32> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
Matrix<Complex32> result;
if (other is DiagonalMatrix)
{
result = new DiagonalMatrix(RowCount, ColumnCount);
}
else
{
result = new DenseMatrix(RowCount, ColumnCount);
}
Subtract(other, result);
return result;
}
示例10: CanComputeThinQRFactorTallMatrixWithWorkArray
public void CanComputeThinQRFactorTallMatrixWithWorkArray()
{
var matrix = _matrices["Tall3x2"];
var r = new Complex32[matrix.ColumnCount*matrix.ColumnCount];
var tau = new Complex32[3];
var q = new Complex32[matrix.RowCount*matrix.ColumnCount];
Array.Copy(matrix.Values, q, q.Length);
var work = new Complex32[matrix.ColumnCount*Control.BlockSize];
Control.LinearAlgebraProvider.ThinQRFactor(q, matrix.RowCount, matrix.ColumnCount, r, tau, work);
var mq = new DenseMatrix(matrix.RowCount, matrix.ColumnCount, q);
var mr = new DenseMatrix(matrix.ColumnCount, matrix.ColumnCount, r);
var a = mq*mr;
for (var row = 0; row < matrix.RowCount; row++)
{
for (var col = 0; col < matrix.ColumnCount; col++)
{
AssertHelpers.AlmostEqualRelative(matrix[row, col], a[row, col], 5);
}
}
}
示例11: CanComputeQRFactorWideMatrix
public void CanComputeQRFactorWideMatrix()
{
var matrix = _matrices["Wide2x3"];
var r = new Complex32[matrix.RowCount*matrix.ColumnCount];
Array.Copy(matrix.Values, r, r.Length);
var tau = new Complex32[3];
var q = new Complex32[matrix.RowCount*matrix.RowCount];
Control.LinearAlgebraProvider.QRFactor(r, matrix.RowCount, matrix.ColumnCount, q, tau);
var mr = new DenseMatrix(matrix.RowCount, matrix.ColumnCount, r).UpperTriangle();
var mq = new DenseMatrix(matrix.RowCount, matrix.RowCount, q);
var a = mq*mr;
for (var row = 0; row < matrix.RowCount; row++)
{
for (var col = 0; col < matrix.ColumnCount; col++)
{
AssertHelpers.AlmostEqualRelative(matrix[row, col], a[row, col], 5);
}
}
}
示例12: CanSolveUsingCholesky
public void CanSolveUsingCholesky()
{
var matrix = new DenseMatrix(3, 3, new Complex32[] {1, 1, 1, 1, 2, 3, 1, 3, 6});
var a = new Complex32[] {1, 1, 1, 1, 2, 3, 1, 3, 6};
var b = new[] {new Complex32(1.0f, 0.0f), 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
Control.LinearAlgebraProvider.CholeskySolve(a, 3, b, 2);
AssertHelpers.AlmostEqualRelative(b[0], 0, 5);
AssertHelpers.AlmostEqualRelative(b[1], 1, 5);
AssertHelpers.AlmostEqualRelative(b[2], 0, 5);
AssertHelpers.AlmostEqualRelative(b[3], 3, 5);
AssertHelpers.AlmostEqualRelative(b[4], 1, 5);
AssertHelpers.AlmostEqualRelative(b[5], 0, 5);
NotModified(3, 3, a, matrix);
}
示例13: CanMultiplyTallAndWideMatricesWithUpdate
public void CanMultiplyTallAndWideMatricesWithUpdate()
{
var x = _matrices["Tall3x2"];
var y = _matrices["Wide2x3"];
var c = new DenseMatrix(x.RowCount, y.ColumnCount);
Control.LinearAlgebraProvider.MatrixMultiplyWithUpdate(Transpose.DontTranspose, Transpose.DontTranspose, 2.2f, x.Values, x.RowCount, x.ColumnCount, y.Values, y.RowCount, y.ColumnCount, 1.0f, c.Values);
for (var i = 0; i < c.RowCount; i++)
{
for (var j = 0; j < c.ColumnCount; j++)
{
var test = 2.2f*x.Row(i)*y.Column(j);
// if they are both close to zero, skip
if (Math.Abs(test.Real) < 1e-7 && Math.Abs(c[i, j].Real) < 1e-7)
{
continue;
}
AssertHelpers.AlmostEqualRelative(2.2f*x.Row(i)*y.Column(j), c[i, j], 5);
}
}
}
示例14: CanMultiplyWideAndTallMatricesWithUpdate
public void CanMultiplyWideAndTallMatricesWithUpdate()
{
var x = _matrices["Wide2x3"];
var y = _matrices["Tall3x2"];
var c = new DenseMatrix(x.RowCount, y.ColumnCount);
Control.LinearAlgebraProvider.MatrixMultiplyWithUpdate(Transpose.DontTranspose, Transpose.DontTranspose, 2.2f, x.Values, x.RowCount, x.ColumnCount, y.Values, y.RowCount, y.ColumnCount, 1.0f, c.Values);
for (var i = 0; i < c.RowCount; i++)
{
for (var j = 0; j < c.ColumnCount; j++)
{
AssertHelpers.AlmostEqualRelative(2.2f*x.Row(i)*y.Column(j), c[i, j], 5);
}
}
}
示例15: CanMultiplyTallAndWideMatrices
public void CanMultiplyTallAndWideMatrices()
{
var x = _matrices["Tall3x2"];
var y = _matrices["Wide2x3"];
var c = new DenseMatrix(x.RowCount, y.ColumnCount);
Control.LinearAlgebraProvider.MatrixMultiply(x.Values, x.RowCount, x.ColumnCount, y.Values, y.RowCount, y.ColumnCount, c.Values);
for (var i = 0; i < c.RowCount; i++)
{
for (var j = 0; j < c.ColumnCount; j++)
{
AssertHelpers.AlmostEqualRelative(x.Row(i)*y.Column(j), c[i, j], 5);
}
}
}