-
-
Notifications
You must be signed in to change notification settings - Fork 25
Sparse QR example
wo80 edited this page Sep 29, 2016
·
5 revisions
This example shows how to create a sparse QR factorization:
using CSparse;
using CSparse.Double;
using CSparse.Double.Factorization;
using CSparse.IO;
public static class Example
{
public static bool Solve(string filePath)
{
// Load matrix from a file.
var A = MatrixMarketReader.ReadMatrix<double>(filePath);
int m = A.RowCount;
int n = A.ColumnCount;
// 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.MinimumDegreeAtA;
var qr = SparseQR.Create(A, order);
// Solve Ax = b (overwrite x).
qr.Solve(b, x);
// Compute residual b - Ax (overwrite b).
A.Multiply(-1.0, x, 1.0, b);
return true;
}
}