本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Double.DenseMatrix.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# DenseMatrix.ToString方法的具体用法?C# DenseMatrix.ToString怎么用?C# DenseMatrix.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MathNet.Numerics.LinearAlgebra.Double.DenseMatrix
的用法示例。
在下文中一共展示了DenseMatrix.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Triangular_matrix">Triangular matrix</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(10);
var k = 0;
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix[i, j] = k++;
}
}
Console.WriteLine(@"Initial square matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 1. Retrieve a new matrix containing the lower triangle of the matrix
var lower = matrix.LowerTriangle();
// Puts the lower triangle of the matrix into the result matrix.
matrix.LowerTriangle(lower);
Console.WriteLine(@"1. Lower triangle of the matrix");
Console.WriteLine(lower.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Retrieve a new matrix containing the upper triangle of the matrix
var upper = matrix.UpperTriangle();
// Puts the upper triangle of the matrix into the result matrix.
matrix.UpperTriangle(lower);
Console.WriteLine(@"2. Upper triangle of the matrix");
Console.WriteLine(upper.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Retrieve a new matrix containing the strictly lower triangle of the matrix
var strictlylower = matrix.StrictlyLowerTriangle();
// Puts the strictly lower triangle of the matrix into the result matrix.
matrix.StrictlyLowerTriangle(strictlylower);
Console.WriteLine(@"3. Strictly lower triangle of the matrix");
Console.WriteLine(strictlylower.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Retrieve a new matrix containing the strictly upper triangle of the matrix
var strictlyupper = matrix.StrictlyUpperTriangle();
// Puts the strictly upper triangle of the matrix into the result matrix.
matrix.StrictlyUpperTriangle(strictlyupper);
Console.WriteLine(@"4. Strictly upper triangle of the matrix");
Console.WriteLine(strictlyupper.ToString("#0.00\t", formatProvider));
Console.WriteLine();
}
示例2: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Transpose">Transpose</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Invertible_matrix">Invertible matrix</seealso>
public void Run()
{
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create random square matrix
var matrix = new DenseMatrix(5);
var rnd = new Random(1);
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix[i, j] = rnd.NextDouble();
}
}
Console.WriteLine(@"Initial matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 1. Get matrix inverse
var inverse = matrix.Inverse();
Console.WriteLine(@"1. Matrix inverse");
Console.WriteLine(inverse.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Matrix multiplied by its inverse gives identity matrix
var identity = matrix * inverse;
Console.WriteLine(@"2. Matrix multiplied by its inverse");
Console.WriteLine(identity.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Get matrix transpose
var transpose = matrix.Transpose();
Console.WriteLine(@"3. Matrix transpose");
Console.WriteLine(transpose.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Get orthogonal matrix, i.e. do QR decomposition and get matrix Q
var orthogonal = matrix.QR().Q;
Console.WriteLine(@"4. Orthogonal matrix");
Console.WriteLine(orthogonal.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 5. Transpose and multiply orthogonal matrix by iteslf gives identity matrix
identity = orthogonal.TransposeAndMultiply(orthogonal);
Console.WriteLine(@"Transpose and multiply orthogonal matrix by iteslf");
Console.WriteLine(identity.ToString("#0.00\t", formatProvider));
Console.WriteLine();
}
示例3: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Cholesky_decomposition">Cholesky decomposition</seealso>
public void Run()
{
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create square, symmetric, positive definite matrix
var matrix = new DenseMatrix(new[,] { { 2.0, 1.0 }, { 1.0, 2.0 } });
Console.WriteLine(@"Initial square, symmetric, positive definite matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Perform Cholesky decomposition
var cholesky = matrix.Cholesky();
Console.WriteLine(@"Perform Cholesky decomposition");
// 1. Lower triangular form of the Cholesky matrix
Console.WriteLine(@"1. Lower triangular form of the Cholesky matrix");
Console.WriteLine(cholesky.Factor.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Reconstruct initial matrix: A = L * LT
var reconstruct = cholesky.Factor * cholesky.Factor.Transpose();
Console.WriteLine(@"2. Reconstruct initial matrix: A = L*LT");
Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Get determinant of the matrix
Console.WriteLine(@"3. Determinant of the matrix");
Console.WriteLine(cholesky.Determinant);
Console.WriteLine();
// 4. Get log determinant of the matrix
Console.WriteLine(@"4. Log determinant of the matrix");
Console.WriteLine(cholesky.DeterminantLn);
Console.WriteLine();
}
示例4: Run
/// <summary>
/// Run example
/// </summary>
public void Run()
{
// 1. Initialize a new instance of the matrix from a 2D array. This constructor will allocate a completely new memory block for storing the dense matrix.
var matrix1 = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } });
// 2. Initialize a new instance of the empty square matrix with a given order.
var matrix2 = new DenseMatrix(3);
// 3. Initialize a new instance of the empty matrix with a given size.
var matrix3 = new DenseMatrix(2, 3);
// 4. Initialize a new instance of the matrix with all entries set to a particular value.
var matrix4 = new DenseMatrix(2, 3, 3.0);
// 4. Initialize a new instance of the matrix from a one dimensional array. This array should store the matrix in column-major order.
var matrix5 = new DenseMatrix(2, 3, new[] { 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 });
// 5. Initialize a square matrix with all zero's except for ones on the diagonal. Identity matrix (http://en.wikipedia.org/wiki/Identity_matrix).
var matrixI = DenseMatrix.Identity(5);
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
Console.WriteLine(@"Matrix 1");
Console.WriteLine(matrix1.ToString("#0.00\t", formatProvider));
Console.WriteLine();
Console.WriteLine(@"Matrix 2");
Console.WriteLine(matrix2.ToString("#0.00\t", formatProvider));
Console.WriteLine();
Console.WriteLine(@"Matrix 3");
Console.WriteLine(matrix3.ToString("#0.00\t", formatProvider));
Console.WriteLine();
Console.WriteLine(@"Matrix 4");
Console.WriteLine(matrix4.ToString("#0.00\t", formatProvider));
Console.WriteLine();
Console.WriteLine(@"Matrix 5");
Console.WriteLine(matrix5.ToString("#0.00\t", formatProvider));
Console.WriteLine();
Console.WriteLine(@"Identity matrix");
Console.WriteLine(matrixI.ToString("#0.00\t", formatProvider));
Console.WriteLine();
}
示例5: Run
/// <summary>
/// Run example
/// </summary>
public void Run()
{
// Format vector output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create new empty square matrix
var matrix = new DenseMatrix(10);
Console.WriteLine(@"Empty matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 1. Fill matrix by data using indexer []
var k = 0;
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix[i, j] = k++;
}
}
Console.WriteLine(@"1. Fill matrix by data using indexer []");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Fill matrix by data using At. The element is set without range checking.
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, k--);
}
}
Console.WriteLine(@"2. Fill matrix by data using At");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Clone matrix
var clone = matrix.Clone();
Console.WriteLine(@"3. Clone matrix");
Console.WriteLine(clone.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Clear matrix
clone.Clear();
Console.WriteLine(@"4. Clear matrix");
Console.WriteLine(clone.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 5. Copy matrix into another matrix
matrix.CopyTo(clone);
Console.WriteLine(@"5. Copy matrix into another matrix");
Console.WriteLine(clone.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 6. Get submatrix into another matrix
var submatrix = matrix.SubMatrix(2, 2, 3, 3);
Console.WriteLine(@"6. Copy submatrix into another matrix");
Console.WriteLine(submatrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 7. Get part of the row as vector. In this example: get 4 elements from row 5 starting from column 3
var row = matrix.Row(5, 3, 4);
Console.WriteLine(@"7. Get part of the row as vector");
Console.WriteLine(row.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 8. Get part of the column as vector. In this example: get 3 elements from column 2 starting from row 6
var column = matrix.Column(2, 6, 3);
Console.WriteLine(@"8. Get part of the column as vector");
Console.WriteLine(column.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 9. Get columns using column enumerator. If you need all columns you may use ColumnEnumerator without parameters
Console.WriteLine(@"9. Get columns using column enumerator");
foreach (var keyValuePair in matrix.ColumnEnumerator(2, 4))
{
Console.WriteLine(@"Column {0}: {1}", keyValuePair.Item1, keyValuePair.Item2.ToString("#0.00\t", formatProvider));
}
Console.WriteLine();
// 10. Get rows using row enumerator. If you need all rows you may use RowEnumerator without parameters
Console.WriteLine(@"10. Get rows using row enumerator");
foreach (var keyValuePair in matrix.RowEnumerator(4, 3))
{
Console.WriteLine(@"Row {0}: {1}", keyValuePair.Item1, keyValuePair.Item2.ToString("#0.00\t", formatProvider));
}
Console.WriteLine();
// 11. Convert matrix into multidimensional array
var data = matrix.ToArray();
Console.WriteLine(@"11. Convert matrix into multidimensional array");
for (var i = 0; i < data.GetLongLength(0); i++)
//.........这里部分代码省略.........
示例6: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Determinant">Determinant</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Rank_%28linear_algebra%29">Rank (linear algebra)</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Trace_%28linear_algebra%29">Trace (linear algebra)</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Condition_number">Condition number</seealso>
public void Run()
{
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create random square matrix
var matrix = new DenseMatrix(5);
var rnd = new Random(1);
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix[i, j] = rnd.NextDouble();
}
}
Console.WriteLine(@"Initial matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 1. Determinant
Console.WriteLine(@"1. Determinant");
Console.WriteLine(matrix.Determinant());
Console.WriteLine();
// 2. Rank
Console.WriteLine(@"2. Rank");
Console.WriteLine(matrix.Rank());
Console.WriteLine();
// 3. Condition number
Console.WriteLine(@"2. Condition number");
Console.WriteLine(matrix.ConditionNumber());
Console.WriteLine();
// 4. Trace
Console.WriteLine(@"4. Trace");
Console.WriteLine(matrix.Trace());
Console.WriteLine();
}
示例7: 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)
{
//.........这里部分代码省略.........
示例8: 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();
}
示例9: 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();
// Create stop criteriums to monitor an iterative calculation. There are next available stop criteriums:
// - DivergenceStopCriterium: monitors an iterative calculation for signs of divergence;
// - FailureStopCriterium: monitors residuals for NaN's;
// - IterationCountStopCriterium: monitors the numbers of iteration steps;
// - ResidualStopCriterium: monitors residuals if calculation is considered converged;
// Stop calculation if 1000 iterations reached during calculation
var iterationCountStopCriterium = new IterationCountStopCriterium(1000);
// Stop calculation if residuals are below 1E-10 --> the calculation is considered converged
var residualStopCriterium = new ResidualStopCriterium(1e-10);
// Create monitor with defined stop criteriums
var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium });
// Create Multiple-Lanczos Bi-Conjugate Gradient Stabilized solver
var solver = new MlkBiCgStab(monitor);
// 1. Solve the matrix equation
var resultX = solver.Solve(matrixA, vectorB);
Console.WriteLine(@"1. Solve the matrix equation");
Console.WriteLine();
// 2. Check solver status of the iterations.
// Solver has property IterationResult which contains the status of the iteration once the calculation is finished.
// Possible values are:
// - CalculationCancelled: calculation was cancelled by the user;
// - CalculationConverged: calculation has converged to the desired convergence levels;
// - CalculationDiverged: calculation diverged;
// - CalculationFailure: calculation has failed for some reason;
// - CalculationIndetermined: calculation is indetermined, not started or stopped;
// - CalculationRunning: calculation is running and no results are yet known;
// - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved;
Console.WriteLine(@"2. Solver status of the iterations");
Console.WriteLine(solver.IterationResult);
Console.WriteLine();
// 3. Solution result vector of the matrix equation
Console.WriteLine(@"3. Solution result vector of the matrix equation");
Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Verify result. Multiply coefficient matrix "A" by result vector "x"
var reconstructVecorB = matrixA * resultX;
Console.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'");
Console.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider));
Console.WriteLine();
}
示例10: ToString
public string ToString(DenseMatrix m)
{
return m.ToString("",formatProvider);
}
示例11: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/QR_decomposition">QR decomposition</seealso>
public void Run()
{
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create 3 x 2 matrix
var matrix = new DenseMatrix(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 } });
Console.WriteLine(@"Initial 3x2 matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Perform QR decomposition (Householder transformations)
var qr = matrix.QR();
Console.WriteLine(@"QR decomposition (Householder transformations)");
// 1. Orthogonal Q matrix
Console.WriteLine(@"1. Orthogonal Q matrix");
Console.WriteLine(qr.Q.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Multiply Q matrix by its transpose gives identity matrix
Console.WriteLine(@"2. Multiply Q matrix by its transpose gives identity matrix");
Console.WriteLine(qr.Q.TransposeAndMultiply(qr.Q).ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Upper triangular factor R
Console.WriteLine(@"3. Upper triangular factor R");
Console.WriteLine(qr.R.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Reconstruct initial matrix: A = Q * R
var reconstruct = qr.Q * qr.R;
Console.WriteLine(@"4. Reconstruct initial matrix: A = Q*R");
Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Perform QR decomposition (Gram–Schmidt process)
var gramSchmidt = matrix.GramSchmidt();
Console.WriteLine(@"QR decomposition (Gram–Schmidt process)");
// 5. Orthogonal Q matrix
Console.WriteLine(@"5. Orthogonal Q matrix");
Console.WriteLine(gramSchmidt.Q.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 6. Multiply Q matrix by its transpose gives identity matrix
Console.WriteLine(@"6. Multiply Q matrix by its transpose gives identity matrix");
Console.WriteLine((gramSchmidt.Q.Transpose() * gramSchmidt.Q).ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 7. Upper triangular factor R
Console.WriteLine(@"7. Upper triangular factor R");
Console.WriteLine(gramSchmidt.R.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 8. Reconstruct initial matrix: A = Q * R
reconstruct = gramSchmidt.Q * gramSchmidt.R;
Console.WriteLine(@"8. Reconstruct initial matrix: A = Q*R");
Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
Console.WriteLine();
}
示例12: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Matrix_norm">Matrix norm</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[,] { { 1.0, 2.0, 3.0 }, { 6.0, 5.0, 4.0 }, { 8.0, 9.0, 7.0 } });
Console.WriteLine(@"Initial square matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 1. 1-norm of the matrix
Console.WriteLine(@"1. 1-norm of the matrix");
Console.WriteLine(matrix.L1Norm());
Console.WriteLine();
// 2. 2-norm of the matrix
Console.WriteLine(@"2. 2-norm of the matrix");
Console.WriteLine(matrix.L2Norm());
Console.WriteLine();
// 3. Frobenius norm of the matrix
Console.WriteLine(@"3. Frobenius norm of the matrix");
Console.WriteLine(matrix.FrobeniusNorm());
Console.WriteLine();
// 4. Infinity norm of the matrix
Console.WriteLine(@"4. Infinity norm of the matrix");
Console.WriteLine(matrix.InfinityNorm());
Console.WriteLine();
// 5. Normalize matrix columns
Console.WriteLine(@"5. Normalize matrix columns: before normalize");
foreach (var keyValuePair in matrix.ColumnEnumerator())
{
Console.WriteLine(@"Column {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
}
Console.WriteLine();
var normalized = matrix.NormalizeColumns(2);
Console.WriteLine(@"5. Normalize matrix columns: after normalize");
foreach (var keyValuePair in normalized.ColumnEnumerator())
{
Console.WriteLine(@"Column {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
}
Console.WriteLine();
// 6. Normalize matrix columns
Console.WriteLine(@"6. Normalize matrix rows: before normalize");
foreach (var keyValuePair in matrix.RowEnumerator())
{
Console.WriteLine(@"Row {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
}
Console.WriteLine();
normalized = matrix.NormalizeRows(2);
Console.WriteLine(@"6. Normalize matrix rows: after normalize");
foreach (var keyValuePair in normalized.RowEnumerator())
{
Console.WriteLine(@"Row {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2));
}
}
示例13: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/LU_decomposition">LU decomposition</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Invertible_matrix">Invertible matrix</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[,] { { 1.0, 2.0 }, { 3.0, 4.0 } });
Console.WriteLine(@"Initial square matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Perform LU decomposition
var lu = matrix.LU();
Console.WriteLine(@"Perform LU decomposition");
// 1. Lower triangular factor
Console.WriteLine(@"1. Lower triangular factor");
Console.WriteLine(lu.L.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Upper triangular factor
Console.WriteLine(@"2. Upper triangular factor");
Console.WriteLine(lu.U.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Permutations applied to LU factorization
Console.WriteLine(@"3. Permutations applied to LU factorization");
for (var i = 0; i < lu.P.Dimension; i++)
{
if (lu.P[i] > i)
{
Console.WriteLine(@"Row {0} permuted with row {1}", lu.P[i], i);
}
}
Console.WriteLine();
// 4. Reconstruct initial matrix: PA = L * U
var reconstruct = lu.L * lu.U;
// The rows of the reconstructed matrix should be permuted to get the initial matrix
reconstruct.PermuteRows(lu.P.Inverse());
Console.WriteLine(@"4. Reconstruct initial matrix: PA = L*U");
Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 5. Get the determinant of the matrix
Console.WriteLine(@"5. Determinant of the matrix");
Console.WriteLine(lu.Determinant);
Console.WriteLine();
// 6. Get the inverse of the matrix
var matrixInverse = lu.Inverse();
Console.WriteLine(@"6. Inverse of the matrix");
Console.WriteLine(matrixInverse.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 7. Matrix multiplied by its inverse
var identity = matrix * matrixInverse;
Console.WriteLine(@"7. Matrix multiplied by its inverse ");
Console.WriteLine(identity.ToString("#0.00\t", formatProvider));
Console.WriteLine();
}
示例14: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Eigenvalue,_eigenvector_and_eigenspace">EVD decomposition</seealso>
public void Run()
{
// Format matrix output to console
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create square symmetric matrix
var matrix = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 4.0 }, { 3.0, 4.0, 1.0 } });
Console.WriteLine(@"Initial square symmetric matrix");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Perform eigenvalue decomposition of symmetric matrix
var evd = matrix.Evd();
Console.WriteLine(@"Perform eigenvalue decomposition of symmetric matrix");
// 1. Eigen vectors
Console.WriteLine(@"1. Eigen vectors");
Console.WriteLine(evd.EigenVectors().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Eigen values as a complex vector
Console.WriteLine(@"2. Eigen values as a complex vector");
Console.WriteLine(evd.EigenValues().ToString("N", formatProvider));
Console.WriteLine();
// 3. Eigen values as the block diagonal matrix
Console.WriteLine(@"3. Eigen values as the block diagonal matrix");
Console.WriteLine(evd.D().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 4. Multiply V by its transpose VT
var identity = evd.EigenVectors().TransposeAndMultiply(evd.EigenVectors());
Console.WriteLine(@"4. Multiply V by its transpose VT: V*VT = I");
Console.WriteLine(identity.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 5. Reconstruct initial matrix: A = V*D*V'
var reconstruct = evd.EigenVectors() * evd.D() * evd.EigenVectors().Transpose();
Console.WriteLine(@"5. Reconstruct initial matrix: A = V*D*V'");
Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 6. Determinant of the matrix
Console.WriteLine(@"6. Determinant of the matrix");
Console.WriteLine(evd.Determinant);
Console.WriteLine();
// 7. Rank of the matrix
Console.WriteLine(@"7. Rank of the matrix");
Console.WriteLine(evd.Rank);
Console.WriteLine();
// Fill matrix by random values
var rnd = new Random(1);
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix[i, j] = rnd.NextDouble();
}
}
Console.WriteLine(@"Fill matrix by random values");
Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Perform eigenvalue decomposition of non-symmetric matrix
evd = matrix.Evd();
Console.WriteLine(@"Perform eigenvalue decomposition of non-symmetric matrix");
// 8. Eigen vectors
Console.WriteLine(@"8. Eigen vectors");
Console.WriteLine(evd.EigenVectors().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 9. Eigen values as a complex vector
Console.WriteLine(@"9. Eigen values as a complex vector");
Console.WriteLine(evd.EigenValues().ToString("N", formatProvider));
Console.WriteLine();
// 10. Eigen values as the block diagonal matrix
Console.WriteLine(@"10. Eigen values as the block diagonal matrix");
Console.WriteLine(evd.D().ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 11. Multiply A * V
var av = matrix * evd.EigenVectors();
Console.WriteLine(@"11. Multiply A * V");
Console.WriteLine(av.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 12. Multiply V * D
var vd = evd.EigenVectors() * evd.D();
Console.WriteLine(@"12. Multiply V * D");
Console.WriteLine(vd.ToString("#0.00\t", formatProvider));
//.........这里部分代码省略.........
示例15: Run
/// <summary>
/// Run example
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Matrix_multiplication#Scalar_multiplication">Multiply matrix by scalar</seealso>
/// <seealso cref="http://reference.wolfram.com/mathematica/tutorial/MultiplyingVectorsAndMatrices.html">Multiply matrix by vector</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product">Multiply matrix by matrix</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Matrix_multiplication#Hadamard_product">Pointwise multiplies matrix with another matrix</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Matrix_%28mathematics%29#Basic_operations">Addition and subtraction</seealso>
public void Run()
{
// Initialize IFormatProvider to print matrix/vector data
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create matrix "A"
var matrixA = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } });
Console.WriteLine(@"Matrix A");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Create matrix "B"
var matrixB = new DenseMatrix(new[,] { { 1.0, 3.0, 5.0 }, { 2.0, 4.0, 6.0 }, { 3.0, 5.0, 7.0 } });
Console.WriteLine(@"Matrix B");
Console.WriteLine(matrixB.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Multiply matrix by scalar
// 1. Using operator "*"
var resultM = 3.0 * matrixA;
Console.WriteLine(@"Multiply matrix by scalar using operator *. (result = 3.0 * A)");
Console.WriteLine(resultM.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using Multiply method and getting result into different matrix instance
resultM = (DenseMatrix)matrixA.Multiply(3.0);
Console.WriteLine(@"Multiply matrix by scalar using method Multiply. (result = A.Multiply(3.0))");
Console.WriteLine(resultM.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Using Multiply method and updating matrix itself
matrixA.Multiply(3.0, matrixA);
Console.WriteLine(@"Multiply matrix by scalar using method Multiply. (A.Multiply(3.0, A))");
Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Multiply matrix by vector (right-multiply)
var vector = new DenseVector(new[] { 1.0, 2.0, 3.0 });
Console.WriteLine(@"Vector");
Console.WriteLine(vector.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 1. Using operator "*"
var resultV = matrixA * vector;
Console.WriteLine(@"Multiply matrix by vector using operator *. (result = A * vec)");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using Multiply method and getting result into different vector instance
resultV = (DenseVector)matrixA.Multiply(vector);
Console.WriteLine(@"Multiply matrix by vector using method Multiply. (result = A.Multiply(vec))");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Using Multiply method and updating vector itself
matrixA.Multiply(vector, vector);
Console.WriteLine(@"Multiply matrix by vector using method Multiply. (A.Multiply(vec, vec))");
Console.WriteLine(vector.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Multiply vector by matrix (left-multiply)
// 1. Using operator "*"
resultV = vector * matrixA;
Console.WriteLine(@"Multiply vector by matrix using operator *. (result = vec * A)");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using LeftMultiply method and getting result into different vector instance
resultV = (DenseVector)matrixA.LeftMultiply(vector);
Console.WriteLine(@"Multiply vector by matrix using method LeftMultiply. (result = A.LeftMultiply(vec))");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Using LeftMultiply method and updating vector itself
matrixA.LeftMultiply(vector, vector);
Console.WriteLine(@"Multiply vector by matrix using method LeftMultiply. (A.LeftMultiply(vec, vec))");
Console.WriteLine(vector.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Multiply matrix by matrix
// 1. Using operator "*"
resultM = matrixA * matrixB;
Console.WriteLine(@"Multiply matrix by matrix using operator *. (result = A * B)");
Console.WriteLine(resultM.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using Multiply method and getting result into different matrix instance
resultM = (DenseMatrix)matrixA.Multiply(matrixB);
Console.WriteLine(@"Multiply matrix by matrix using method Multiply. (result = A.Multiply(B))");
Console.WriteLine(resultM.ToString("#0.00\t", formatProvider));
Console.WriteLine();
//.........这里部分代码省略.........