我正在尝试求解类似Ax = 0的齐次线性方程组。 这是一个为简化起见已简化的示例矩阵:
1 2 | 0
3 6 | 0
The solution I am hoping to get to is at least [ 2, -1 ].
But the fundamental solution is folow: [2C; -1C].
You can see than Det(A)=0 and Rank(A) =1;
Of course you know that such systems have trivial solution [0,0].
我正在尝试:
Matrix<double> A = Matrix<double>.Build.DenseOfArray(new double[,] {
{ 1, 2 },
{ 3, 6 },
});
Vector<double> B = Vector<double>.Build.Dense(new double[] { 0, 0 });
var result = A.Solve(B); //result = Nan, Nan
请帮助我通过MathNet查找此类任务的解决方案,因为在实际任务中,A的尺寸大于20。
此问题的解决方案:Math.Net system of linear equations with a 0 value in solution对我的情况不起作用(B = 0,Det(A)= 0)。
谢谢
答案 0 :(得分:0)
要求解线性方程,可以尝试Matrix<T>.SolveIterative(Vector<T> input, IIterativeSolver<T> solver, Iterator<T> iterator = null, IPreconditioner<T> preconditioner = null)
。
要获取可用的求解器:
var solvers = Assembly.GetAssembly(typeof(Matrix<double>))
.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(IIterativeSolver<double>)) &&
t.GetConstructors().Any(ctor => ctor.GetParameters().Count() == 0))
.Select(t => Activator.CreateInstance(t))
.Cast<IIterativeSolver<double>>();
它给出:
MathNet.Numerics.LinearAlgebra.Double.Solvers.BiCgStab
MathNet.Numerics.LinearAlgebra.Double.Solvers.GpBiCg
MathNet.Numerics.LinearAlgebra.Double.Solvers.MlkBiCgStab
MathNet.Numerics.LinearAlgebra.Double.Solvers.TFQMR
使用数据尝试所有操作:
Matrix<double> A = Matrix<double>.Build.DenseOfArray(new double[,]
{
{ 1, 2 },
{ 3, 6 }
});
Vector<double> B = Vector<double>.Build.Dense(new double[] { 0, 0 });
赞:
foreach (var solver in solvers)
{
try
{
Console.WriteLine(solver);
Console.WriteLine(A.SolveIterative(B, solver));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
在您的情况下,这不是运气。结果是:
MathNet.Numerics.LinearAlgebra.Double.Solvers.BiCgStab
Algorithm experience a numerical break down
MathNet.Numerics.LinearAlgebra.Double.Solvers.GpBiCg
DenseVector 2-Double
NaN
NaN
MathNet.Numerics.LinearAlgebra.Double.Solvers.MlkBiCgStab
Algorithm experience a numerical break down
MathNet.Numerics.LinearAlgebra.Double.Solvers.TFQMR
DenseVector 2-Double
0
0
尽管它可以像here一样工作。
无论如何,MathNet
是如此出色,以至于人们可以轻松构建解决方案。
使用大学的线性代数和库功能:
static class MatrixExtension
{
public static Vector<double>[] SolveDegenerate(this Matrix<double> matrix, Vector<double> input)
{
var augmentedMatrix =
Matrix<double>.Build.DenseOfColumnVectors(matrix.EnumerateColumns().Append(input));
if (augmentedMatrix.Rank() != matrix.Rank())
throw new ArgumentException("Augmented matrix rank does not match coefficient matrix rank.");
return augmentedMatrix.SolveAugmented();
}
private static Vector<double>[] SolveAugmented(this Matrix<double> matrix)
{
var rank = matrix.Rank();
var cut = matrix.CutByRank(rank);
// [A|R]x[X] = [B]
var A = Matrix<double>.Build.DenseOfColumnVectors(cut.EnumerateColumns().Take(rank));
var R = Matrix<double>.Build.DenseOfColumnVectors(cut.EnumerateColumns().Skip(rank).Take(cut.ColumnCount - rank - 1));
var B = cut.EnumerateColumns().Last();
var vectors = Matrix<double>.Build.DenseDiagonal(R.ColumnCount, 1)
.EnumerateColumns().ToArray();
return vectors.Select(v => A.Solve(B - R * v))
.Zip(vectors, (x, v) => Vector<double>.Build.DenseOfEnumerable(x.Concat(v)))
.ToArray();
}
private static Matrix<double> CutByRank(this Matrix<double> matrix, int rank)
{
var result = Matrix<double>.Build.DenseOfMatrix(matrix);
while (result.Rank() < result.RowCount)
result = result.EnumerateRows()
.Select((r, index) => result.RemoveRow(index))
.FirstOrDefault(m => m.Rank() == rank);
return result;
}
}
现在:
Console.WriteLine(A.SolveDegenerate(B).First());
礼物:
DenseVector 2-Double
-2
1
另一个例子:
Matrix<double> A = Matrix<double>.Build.DenseOfArray(new double[,]
{
{ 1, 2, 1 },
{ 3, 6, 3 },
{ 4, 8, 4 }
});
Vector<double> B = Vector<double>.Build.Dense(new double[3]);
foreach (var x in A.SolveDegenerate(B))
Console.WriteLine(x);
礼物:
DenseVector 3-Double
-2
1
0
DenseVector 3-Double
-1
0
1