本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Double.DenseMatrix.Svd方法的典型用法代码示例。如果您正苦于以下问题:C# DenseMatrix.Svd方法的具体用法?C# DenseMatrix.Svd怎么用?C# DenseMatrix.Svd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MathNet.Numerics.LinearAlgebra.Double.DenseMatrix
的用法示例。
在下文中一共展示了DenseMatrix.Svd方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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, 0);
Assert.AreEqual(factorSvd.Rank, order - 1);
}
示例2: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Singular_value_decomposition">SVD decomposition</seealso>
public void Run()
{
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create square matrix
var matrix = new DenseMatrix(new[,] { { 4.0, 1.0 }, { 3.0, 2.0 } });
Console.WriteLine(@"Initial square matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Perform full SVD decomposition
var svd = matrix.Svd(true);
Console.WriteLine(@"Perform full SVD decomposition");
// 1. Left singular vectors
Console.WriteLine(@"1. Left singular vectors");
Console.WriteLine(svd.U().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Singular values as vector
Console.WriteLine(@"2. Singular values as vector");
Console.WriteLine(svd.S().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Singular values as diagonal matrix
Console.WriteLine(@"3. Singular values as diagonal matrix");
Console.WriteLine(svd.W().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Right singular vectors
Console.WriteLine(@"4. Right singular vectors");
Console.WriteLine(svd.VT().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 5. Multiply U matrix by its transpose
var identinty = svd.U() * svd.U().Transpose();
Console.WriteLine(@"5. Multiply U matrix by its transpose");
Console.WriteLine(identinty.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 6. Multiply V matrix by its transpose
identinty = svd.VT().TransposeAndMultiply(svd.VT());
Console.WriteLine(@"6. Multiply V matrix by its transpose");
Console.WriteLine(identinty.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 7. Reconstruct initial matrix: A = U*Σ*VT
var reconstruct = svd.U() * svd.W() * svd.VT();
Console.WriteLine(@"7. Reconstruct initial matrix: A = U*S*VT");
Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 8. Condition Number of the matrix
Console.WriteLine(@"8. Condition Number of the matrix");
Console.WriteLine(svd.ConditionNumber);
Console.WriteLine();
// 9. Determinant of the matrix
Console.WriteLine(@"9. Determinant of the matrix");
Console.WriteLine(svd.Determinant);
Console.WriteLine();
// 10. 2-norm of the matrix
Console.WriteLine(@"10. 2-norm of the matrix");
Console.WriteLine(svd.Norm2);
Console.WriteLine();
// 11. Rank of the matrix
Console.WriteLine(@"11. Rank of the matrix");
Console.WriteLine(svd.Rank);
Console.WriteLine();
// Perform partial SVD decomposition, without computing the singular U and VT vectors
svd = matrix.Svd(false);
Console.WriteLine(@"Perform partial SVD decomposition, without computing the singular U and VT vectors");
// 12. Singular values as vector
Console.WriteLine(@"12. Singular values as vector");
Console.WriteLine(svd.S().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 13. Singular values as diagonal matrix
Console.WriteLine(@"13. Singular values as diagonal matrix");
Console.WriteLine(svd.W().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 14. Access to left singular vectors when partial SVD decomposition was performed
try
{
Console.WriteLine(@"14. Access to left singular vectors when partial SVD decomposition was performed");
Console.WriteLine(svd.U().ToString("#0.00\t", formatProvider));
}
catch (Exception ex)
{
//.........这里部分代码省略.........
示例3: Run
/// <summary>
/// Run example
/// </summary>
public void Run()
{
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Solve next system of linear equations (Ax=b):
// 5*x + 2*y - 4*z = -7
// 3*x - 7*y + 6*z = 38
// 4*x + 1*y + 5*z = 43
// Create matrix "A" with coefficients
var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } });
Console.WriteLine(@"Matrix 'A' with coefficients");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Create vector "b" with the constant terms.
var vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 });
Console.WriteLine(@"Vector 'b' with the constant terms");
Console.WriteLine(vectorB.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 1. Solve linear equations using LU decomposition
var resultX = matrixA.LU().Solve(vectorB);
Console.WriteLine(@"1. Solution using LU decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Solve linear equations using QR decomposition
resultX = matrixA.QR().Solve(vectorB);
Console.WriteLine(@"2. Solution using QR decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Solve linear equations using SVD decomposition
matrixA.Svd(true).Solve(vectorB, resultX);
Console.WriteLine(@"3. Solution using SVD decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Solve linear equations using Gram-Shmidt decomposition
matrixA.GramSchmidt().Solve(vectorB, resultX);
Console.WriteLine(@"4. Solution using Gram-Shmidt decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 5. Verify result. Multiply coefficient matrix "A" by result vector "x"
var reconstructVecorB = matrixA * resultX;
Console.WriteLine(@"5. Multiply coefficient matrix 'A' by result vector 'x'");
Console.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// To use Cholesky or Eigenvalue decomposition coefficient matrix must be
// symmetric (for Evd and Cholesky) and positive definite (for Cholesky)
// Multipy matrix "A" by its transpose - the result will be symmetric and positive definite matrix
var newMatrixA = matrixA.TransposeAndMultiply(matrixA);
Console.WriteLine(@"Symmetric positive definite matrix");
Console.WriteLine(newMatrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 6. Solve linear equations using Cholesky decomposition
newMatrixA.Cholesky().Solve(vectorB, resultX);
Console.WriteLine(@"6. Solution using Cholesky decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 7. Solve linear equations using eigen value decomposition
newMatrixA.Evd().Solve(vectorB, resultX);
Console.WriteLine(@"7. Solution using eigen value decomposition");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 8. Verify result. Multiply new coefficient matrix "A" by result vector "x"
reconstructVecorB = newMatrixA * resultX;
Console.WriteLine(@"8. Multiply new coefficient matrix 'A' by result vector 'x'");
Console.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider));
Console.WriteLine();
}