本文整理汇总了C#中UserDefinedMatrix.GramSchmidt方法的典型用法代码示例。如果您正苦于以下问题:C# UserDefinedMatrix.GramSchmidt方法的具体用法?C# UserDefinedMatrix.GramSchmidt怎么用?C# UserDefinedMatrix.GramSchmidt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserDefinedMatrix
的用法示例。
在下文中一共展示了UserDefinedMatrix.GramSchmidt方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanSolveForRandomVectorWhenResultVectorGiven
public void CanSolveForRandomVectorWhenResultVectorGiven(int order)
{
var matrixA = new UserDefinedMatrix(Matrix<Complex>.Build.Random(order, order, 1).ToArray());
var matrixACopy = matrixA.Clone();
var factorGramSchmidt = matrixA.GramSchmidt();
var vectorb = new UserDefinedVector(Vector<Complex>.Build.Random(order, 1).ToArray());
var vectorbCopy = vectorb.Clone();
var resultx = new UserDefinedVector(order);
factorGramSchmidt.Solve(vectorb, resultx);
Assert.AreEqual(vectorb.Count, 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]);
}
}
// Make sure b didn't change.
for (var i = 0; i < vectorb.Count; i++)
{
Assert.AreEqual(vectorbCopy[i], vectorb[i]);
}
}
示例2: 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]);
}
}
}
示例3: 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]);
}
}
}
示例4: CanFactorizeRandomMatrix
public void CanFactorizeRandomMatrix(int row, int column)
{
var matrixA = new UserDefinedMatrix(Matrix<Complex>.Build.Random(row, column, 1).ToArray());
var factorGramSchmidt = matrixA.GramSchmidt();
var q = factorGramSchmidt.Q;
var r = factorGramSchmidt.R;
// Make sure the Q has the right dimensions.
Assert.AreEqual(row, q.RowCount);
Assert.AreEqual(column, q.ColumnCount);
// Make sure the R has the right dimensions.
Assert.AreEqual(column, r.RowCount);
Assert.AreEqual(column, r.ColumnCount);
// Make sure the R factor is upper triangular.
for (var i = 0; i < r.RowCount; i++)
{
for (var j = 0; j < r.ColumnCount; j++)
{
if (i > j)
{
Assert.AreEqual(Complex.Zero, r[i, j]);
}
}
}
// Make sure the Q*R is the original matrix.
var matrixQfromR = q * r;
for (var i = 0; i < matrixQfromR.RowCount; i++)
{
for (var j = 0; j < matrixQfromR.ColumnCount; j++)
{
AssertHelpers.AlmostEqualRelative(matrixA[i, j], matrixQfromR[i, j], 9);
}
}
// Make sure the Q is unitary --> (Q*)x(Q) = I
var matrixQсtQ = q.ConjugateTranspose() * q;
for (var i = 0; i < matrixQсtQ.RowCount; i++)
{
for (var j = 0; j < matrixQсtQ.ColumnCount; j++)
{
if (i == j)
{
AssertHelpers.AlmostEqualRelative(matrixQсtQ[i, j], Complex.One, 9);
}
else
{
AssertHelpers.AlmostEqualRelative(matrixQсtQ[i, j], Complex.Zero, 9);
}
}
}
}
示例5: CanFactorizeRandomMatrix
public void CanFactorizeRandomMatrix(int row, int column)
{
var matrixA = new UserDefinedMatrix(Matrix<float>.Build.Random(row, column, 1).ToArray());
var factorGramSchmidt = matrixA.GramSchmidt();
var q = factorGramSchmidt.Q;
var r = factorGramSchmidt.R;
// Make sure the Q has the right dimensions.
Assert.AreEqual(row, q.RowCount);
Assert.AreEqual(column, q.ColumnCount);
// Make sure the R has the right dimensions.
Assert.AreEqual(column, r.RowCount);
Assert.AreEqual(column, r.ColumnCount);
// Make sure the R factor is upper triangular.
for (var i = 0; i < r.RowCount; i++)
{
for (var j = 0; j < r.ColumnCount; j++)
{
if (i > j)
{
Assert.AreEqual(0.0, r[i, j]);
}
}
}
// Make sure the Q*R is the original matrix.
var matrixQfromR = q * r;
for (var i = 0; i < matrixQfromR.RowCount; i++)
{
for (var j = 0; j < matrixQfromR.ColumnCount; j++)
{
Assert.AreEqual(matrixA[i, j], matrixQfromR[i, j], 1e-4);
}
}
}