本文整理汇总了C#中UserDefinedMatrix类的典型用法代码示例。如果您正苦于以下问题:C# UserDefinedMatrix类的具体用法?C# UserDefinedMatrix怎么用?C# UserDefinedMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UserDefinedMatrix类属于命名空间,在下文中一共展示了UserDefinedMatrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanFactorizeRandomMatrix
public void CanFactorizeRandomMatrix(int row, int column)
{
var matrixA = new UserDefinedMatrix(Matrix<double>.Build.Random(row, column, 1).ToArray());
var factorSvd = matrixA.Svd();
var u = factorSvd.U;
var vt = factorSvd.VT;
var w = factorSvd.W;
// Make sure the U has the right dimensions.
Assert.AreEqual(row, u.RowCount);
Assert.AreEqual(row, u.ColumnCount);
// Make sure the VT has the right dimensions.
Assert.AreEqual(column, vt.RowCount);
Assert.AreEqual(column, vt.ColumnCount);
// Make sure the W has the right dimensions.
Assert.AreEqual(row, w.RowCount);
Assert.AreEqual(column, w.ColumnCount);
// Make sure the U*W*VT is the original matrix.
var matrix = u*w*vt;
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
Assert.AreEqual(matrixA[i, j], matrix[i, j], 1.0e-11);
}
}
}
示例2: CanFactorizeRandomMatrix
public void CanFactorizeRandomMatrix(int order)
{
var matrixA = new UserDefinedMatrix(Matrix<Complex>.Build.Random(order, order, 1).ToArray());
var factorEvd = matrixA.Evd();
var eigenVectors = factorEvd.EigenVectors;
var d = factorEvd.D;
Assert.AreEqual(order, eigenVectors.RowCount);
Assert.AreEqual(order, eigenVectors.ColumnCount);
Assert.AreEqual(order, d.RowCount);
Assert.AreEqual(order, d.ColumnCount);
// Make sure the A*V = λ*V
var matrixAv = matrixA * eigenVectors;
var matrixLv = eigenVectors * d;
for (var i = 0; i < matrixAv.RowCount; i++)
{
for (var j = 0; j < matrixAv.ColumnCount; j++)
{
AssertHelpers.AlmostEqualRelative(matrixAv[i, j], matrixLv[i, j], 7);
}
}
}
示例3: CanFactorizeRandomMatrix
public void CanFactorizeRandomMatrix(int order)
{
var matrixX = new UserDefinedMatrix(Matrix<float>.Build.RandomPositiveDefinite(order, 1).ToArray());
var chol = matrixX.Cholesky();
var factorC = chol.Factor;
// Make sure the Cholesky factor has the right dimensions.
Assert.AreEqual(order, factorC.RowCount);
Assert.AreEqual(order, factorC.ColumnCount);
// Make sure the Cholesky factor is lower triangular.
for (var i = 0; i < factorC.RowCount; i++)
{
for (var j = i + 1; j < factorC.ColumnCount; j++)
{
Assert.AreEqual(0.0, factorC[i, j]);
}
}
// Make sure the cholesky factor times it's transpose is the original matrix.
var matrixXfromC = factorC * factorC.Transpose();
for (var i = 0; i < matrixXfromC.RowCount; i++)
{
for (var j = 0; j < matrixXfromC.ColumnCount; j++)
{
Assert.AreEqual(matrixX[i, j], matrixXfromC[i, j], 1e-3);
}
}
}
示例4: CanCheckRankOfNonSquare
public void CanCheckRankOfNonSquare(int row, int column)
{
var matrixA = new UserDefinedMatrix(Matrix<float>.Build.Random(row, column, 1).ToArray());
var factorSvd = matrixA.Svd();
var mn = Math.Min(row, column);
Assert.AreEqual(factorSvd.Rank, mn);
}
示例5: CanCheckRankOfSquareSingular
public void CanCheckRankOfSquareSingular(int order)
{
var matrixA = new UserDefinedMatrix(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);
}
示例6: CanCheckRankOfSquareSingular
public void CanCheckRankOfSquareSingular([Values(10, 50, 100)] int order)
{
var matrixA = new UserDefinedMatrix(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 factorEvd = matrixA.Evd();
Assert.AreEqual(factorEvd.Determinant, Complex32.Zero);
Assert.AreEqual(factorEvd.Rank, order - 1);
}
示例7: CanWriteTabDelimitedData
public void CanWriteTabDelimitedData()
{
var matrix = new UserDefinedMatrix(new[,] { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } });
var headers = new[] { "a", "b", "c" };
var writer = new DelimitedWriter('\t')
{
ColumnHeaders = headers
};
var stream = new MemoryStream();
writer.WriteMatrix(matrix, stream);
var data = stream.ToArray();
var reader = new StreamReader(new MemoryStream(data));
var text = reader.ReadToEnd();
var expected = "a\tb\tc"
+ Environment.NewLine
+ "1.1\t2.2\t3.3"
+ Environment.NewLine
+ "4.4\t5.5\t6.6"
+ Environment.NewLine
+ "7.7\t8.8\t9.9";
Assert.AreEqual(expected, text);
}
示例8: CanSolveForRandomVector
public void CanSolveForRandomVector(int row, int column)
{
var matrixA = new UserDefinedMatrix(Matrix<Complex>.Build.Random(row, column, 1).ToArray());
var matrixACopy = matrixA.Clone();
var factorSvd = matrixA.Svd();
var vectorb = new UserDefinedVector(Vector<Complex>.Build.Random(row, 1).ToArray());
var resultx = factorSvd.Solve(vectorb);
Assert.AreEqual(matrixA.ColumnCount, resultx.Count);
var matrixBReconstruct = matrixA*resultx;
// Check the reconstruction.
for (var i = 0; i < vectorb.Count; i++)
{
AssertHelpers.AlmostEqual(vectorb[i], matrixBReconstruct[i], 10);
}
// Make sure A didn't change.
for (var i = 0; i < matrixA.RowCount; i++)
{
for (var j = 0; j < matrixA.ColumnCount; j++)
{
Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
}
}
}
示例9: LUFailsWithNonSquareMatrix
public void LUFailsWithNonSquareMatrix()
{
var matrix = new UserDefinedMatrix(3, 2);
Assert.Throws<ArgumentException>(() => matrix.LU());
}
示例10: CanSolveForRandomMatrixWhenResultMatrixGiven
public void CanSolveForRandomMatrixWhenResultMatrixGiven(int order)
{
var matrixA = new UserDefinedMatrix(Matrix<Complex>.Build.Random(order, order, 1).ToArray());
var matrixACopy = matrixA.Clone();
var factorGramSchmidt = matrixA.GramSchmidt();
var matrixB = new UserDefinedMatrix(Matrix<Complex>.Build.Random(order, order, 1).ToArray());
var matrixBCopy = matrixB.Clone();
var matrixX = new UserDefinedMatrix(order, order);
factorGramSchmidt.Solve(matrixB, matrixX);
// The solution X row dimension is equal to the column dimension of A
Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount);
// The solution X has the same number of columns as B
Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);
var matrixBReconstruct = matrixA * matrixX;
// Check the reconstruction.
for (var i = 0; i < matrixB.RowCount; i++)
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
AssertHelpers.AlmostEqual(matrixB[i, j], matrixBReconstruct[i, j], 10);
}
}
// Make sure A didn't change.
for (var i = 0; i < matrixA.RowCount; i++)
{
for (var j = 0; j < matrixA.ColumnCount; j++)
{
Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
}
}
// Make sure B didn't change.
for (var i = 0; i < matrixB.RowCount; i++)
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
Assert.AreEqual(matrixBCopy[i, j], matrixB[i, j]);
}
}
}
示例11: CanSolveForRandomMatrix
public void CanSolveForRandomMatrix(int row, int col)
{
var matrixA = new UserDefinedMatrix(Matrix<Complex32>.Build.RandomPositiveDefinite(row, 1).ToArray());
var matrixACopy = matrixA.Clone();
var chol = matrixA.Cholesky();
var matrixB = new UserDefinedMatrix(Matrix<Complex32>.Build.Random(row, col, 1).ToArray());
var matrixX = chol.Solve(matrixB);
Assert.AreEqual(matrixB.RowCount, matrixX.RowCount);
Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);
var matrixBReconstruct = matrixA * matrixX;
// Check the reconstruction.
for (var i = 0; i < matrixB.RowCount; i++)
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
Assert.AreEqual(matrixB[i, j].Real, matrixBReconstruct[i, j].Real, 0.01f);
Assert.AreEqual(matrixB[i, j].Imaginary, matrixBReconstruct[i, j].Imaginary, 0.01f);
}
}
// Make sure A didn't change.
for (var i = 0; i < matrixA.RowCount; i++)
{
for (var j = 0; j < matrixA.ColumnCount; j++)
{
Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
}
}
}
示例12: CanSolveForRandomMatrixWhenResultMatrixGiven
public void CanSolveForRandomMatrixWhenResultMatrixGiven(int row, int col)
{
var matrixA = new UserDefinedMatrix(Matrix<float>.Build.RandomPositiveDefinite(row, 1).ToArray());
var matrixACopy = matrixA.Clone();
var chol = matrixA.Cholesky();
var matrixB = new UserDefinedMatrix(Matrix<float>.Build.Random(row, col, 1).ToArray());
var matrixBCopy = matrixB.Clone();
var matrixX = new UserDefinedMatrix(row, col);
chol.Solve(matrixB, matrixX);
Assert.AreEqual(matrixB.RowCount, matrixX.RowCount);
Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);
var matrixBReconstruct = matrixA * matrixX;
// Check the reconstruction.
for (var i = 0; i < matrixB.RowCount; i++)
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
Assert.AreEqual(matrixB[i, j], matrixBReconstruct[i, j], 1.0);
}
}
// Make sure A didn't change.
for (var i = 0; i < matrixA.RowCount; i++)
{
for (var j = 0; j < matrixA.ColumnCount; j++)
{
Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
}
}
// Make sure B didn't change.
for (var i = 0; i < matrixB.RowCount; i++)
{
for (var j = 0; j < matrixB.ColumnCount; j++)
{
Assert.AreEqual(matrixBCopy[i, j], matrixB[i, j]);
}
}
}
示例13: CholeskyFailsWithNonSquareMatrix
public void CholeskyFailsWithNonSquareMatrix()
{
var matrixI = new UserDefinedMatrix(3, 2);
Assert.That(() => matrixI.Cholesky(), Throws.ArgumentException);
}
示例14: CanSolveForRandomVector
public void CanSolveForRandomVector(int order)
{
var matrixA = new UserDefinedMatrix(Matrix<Complex32>.Build.Random(order, order, 1).ToArray());
var matrixACopy = matrixA.Clone();
var factorGramSchmidt = matrixA.GramSchmidt();
var vectorb = new UserDefinedVector(Vector<Complex32>.Build.Random(order, 1).ToArray());
var resultx = factorGramSchmidt.Solve(vectorb);
Assert.AreEqual(matrixA.ColumnCount, resultx.Count);
var matrixBReconstruct = matrixA * resultx;
// Check the reconstruction.
for (var i = 0; i < order; i++)
{
Assert.AreEqual(vectorb[i].Real, matrixBReconstruct[i].Real, 1e-3f);
Assert.AreEqual(vectorb[i].Imaginary, matrixBReconstruct[i].Imaginary, 1e-3f);
}
// Make sure A didn't change.
for (var i = 0; i < matrixA.RowCount; i++)
{
for (var j = 0; j < matrixA.ColumnCount; j++)
{
Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
}
}
}
示例15: CanSolveForRandomVectorWhenResultVectorGivenUsingThinQR
public void CanSolveForRandomVectorWhenResultVectorGivenUsingThinQR(int order)
{
var matrixA = new UserDefinedMatrix(Matrix<double>.Build.Random(order, order, 1).ToArray());
var matrixACopy = matrixA.Clone();
var factorQR = matrixA.QR(QRMethod.Thin);
var vectorb = new UserDefinedVector(Vector<double>.Build.Random(order, 1).ToArray());
var vectorbCopy = vectorb.Clone();
var resultx = new UserDefinedVector(order);
factorQR.Solve(vectorb, resultx);
Assert.AreEqual(vectorb.Count, resultx.Count);
var matrixBReconstruct = matrixA * resultx;
// Check the reconstruction.
for (var i = 0; i < vectorb.Count; i++)
{
Assert.AreEqual(vectorb[i], matrixBReconstruct[i], 1.0e-11);
}
// Make sure A didn't change.
for (var i = 0; i < matrixA.RowCount; i++)
{
for (var j = 0; j < matrixA.ColumnCount; j++)
{
Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
}
}
// Make sure b didn't change.
for (var i = 0; i < vectorb.Count; i++)
{
Assert.AreEqual(vectorbCopy[i], vectorb[i]);
}
}