-
-
Notifications
You must be signed in to change notification settings - Fork 25
Sparse LU example
wo80 edited this page Sep 29, 2016
·
4 revisions
This example shows how to create a sparse LU factorization:
using CSparse;
using CSparse.Double;
using CSparse.Double.Factorization;
using CSparse.IO;
using CSparse.Ordering;
public static class Example
{
public static bool Solve(string fileName)
{
// Load matrix from a file.
var A = MatrixMarketReader.ReadMatrix<double>(fileName);
int m = A.RowCount;
int n = A.ColumnCount;
// Cannot solve rectangular system.
if (m != n) return false;
// Randomized Dulmage-Mendelsohn analysis.
var dm = DulmageMendelsohn.Generate(A, 1);
// Cannot solve singular system.
if (dm.StructuralRank < n) return false;
// Create test data.
var x = Vector.Create(n, 1.0);
var b = new double[m];
// Compute right hand side vector b.
A.Multiply(1.0, x, 0.0, b);
// Apply column ordering to A to reduce fill-in.
var order = ColumnOrdering.MinimumDegreeAtPlusA;
// Partial pivoting tolerance (0.0 to 1.0)
double tolerance = 1.0;
// Create LU factorization.
var lu = SparseLU.Create(A, order, tolerance);
// Solve Ax = b (overwrite x).
lu.Solve(b, x);
// Compute residual b - Ax (overwrite b).
A.Multiply(-1.0, x, 1.0, b);
return true;
}
}