-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSparseLUTest.cs
More file actions
80 lines (60 loc) · 2.38 KB
/
SparseLUTest.cs
File metadata and controls
80 lines (60 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace CSparse.Tests.Complex.Factorization
{
using CSparse.Complex;
using CSparse.Complex.Factorization;
using NUnit.Framework;
using System;
using Complex = System.Numerics.Complex;
public class SparseLUTest
{
private const double EPS = 1.0e-6;
[Test]
public void TestSolve()
{
// Load matrix from a file.
var A = ResourceLoader.Get<Complex>("general-40x40.mat");
// Create test data.
var x = Helper.CreateTestVector(A.ColumnCount);
var b = Helper.Multiply(A, x);
var r = Vector.Clone(b);
// Create LU factorization.
var lu = SparseLU.Create(A, ColumnOrdering.MinimumDegreeAtPlusA, 1.0);
// Solve Ax = b.
lu.Solve(b, x);
// Compute residual r = b - Ax.
A.Multiply(-1.0, x, 1.0, r);
Assert.That(Vector.Norm(r.Length, r) < EPS, Is.True);
// Test exceptions:
var e1 = Assert.Throws<ArgumentNullException>(() => lu.Solve(b, null));
var e2 = Assert.Throws<ArgumentNullException>(() => lu.Solve(null, x));
Assert.That(e1.ParamName, Is.EqualTo("result"));
Assert.That(e2.ParamName, Is.EqualTo("input"));
}
[Test]
public void TestSolveTranspose()
{
// Load matrix from a file.
var A = ResourceLoader.Get<Complex>("general-40x40.mat");
var AT = A.Transpose();
// Create test data.
var x = Helper.CreateTestVector(A.ColumnCount);
var b = Helper.Multiply(AT, x);
var r = Vector.Clone(b);
// Create LU factorization.
var lu = SparseLU.Create(A, ColumnOrdering.MinimumDegreeAtPlusA, 1.0);
// Solve A'x = b.
lu.SolveTranspose(b, x);
// Compute residual r = b - A'x.
AT.Multiply(-1.0, x, 1.0, r);
Assert.That(Vector.Norm(r.Length, r) < EPS, Is.True);
}
[Test]
public void TestEmptyFactorize()
{
var A = new SparseMatrix(0, 0, 0);
var lu = SparseLU.Create(A, ColumnOrdering.MinimumDegreeAtPlusA, 1.0);
Assert.That(lu, Is.Not.Null);
Assert.That(lu.NonZerosCount == 0, Is.True);
}
}
}