UNPKG

6.09 kBJavaScriptView Raw
1var assert = require('assert');
2var approx = require('../../../../tools/approx');
3var market = require('../../../../tools/matrixmarket');
4var math = require('../../../../index');
5math.import(require('../../../../lib/function/algebra/sparse/cs_permute'));
6math.import(require('../../../../lib/function/algebra/sparse/cs_lu'));
7math.import(require('../../../../lib/function/algebra/sparse/cs_sqr'));
8
9var cs_permute = math.sparse.cs_permute;
10var cs_lu = math.sparse.cs_lu;
11var cs_sqr = math.sparse.cs_sqr;
12
13describe('cs_lu', function () {
14
15 it('should decompose matrix, 2 x 2, no symbolic ordering and analysis, partial pivoting', function () {
16
17 var m = math.sparse([[2, 1], [1, 4]]);
18
19 // partial pivoting
20 var r = cs_lu(m, null, 1);
21
22 // L
23 assert.deepEqual(r.L.valueOf(), [[1, 0], [0.5, 1]]);
24 // U
25 assert.deepEqual(r.U.valueOf(), [[2, 1], [0, 3.5]]);
26 // P
27 assert.deepEqual(r.pinv, [0, 1]);
28 // verify
29 approx.deepEqual(cs_permute(m, r.pinv, null, true), math.multiply(r.L, r.U));
30 });
31
32 it('should decompose matrix, 4 x 4, natural ordering (order=0), partial pivoting', function () {
33
34 var m = math.sparse(
35 [
36 [4.5, 0, 3.2, 0],
37 [3.1, 2.9, 0, 0.9],
38 [0, 1.7, 3, 0],
39 [3.5, 0.4, 0, 1]
40 ]);
41
42 // symbolic ordering and analysis, order = 0
43 var s = cs_sqr(0, m, false);
44
45 // partial pivoting
46 var r = cs_lu(m, s, 1);
47
48 // verify
49 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
50 });
51
52 it('should decompose matrix, 4 x 4, amd(A+A\') (order=1), partial pivoting', function () {
53
54 var m = math.sparse(
55 [
56 [4.5, 0, 3.2, 0],
57 [3.1, 2.9, 0, 0.9],
58 [0, 1.7, 3, 0],
59 [3.5, 0.4, 0, 1]
60 ]);
61
62 // symbolic ordering and analysis, order = 1
63 var s = cs_sqr(1, m, false);
64
65 // partial pivoting
66 var r = cs_lu(m, s, 1);
67
68 // verify
69 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
70 });
71
72 it('should decompose matrix, 4 x 4, amd(A\'*A) (order=2), partial pivoting', function () {
73
74 var m = math.sparse(
75 [
76 [4.5, 0, 3.2, 0],
77 [3.1, 2.9, 0, 0.9],
78 [0, 1.7, 3, 0],
79 [3.5, 0.4, 0, 1]
80 ]);
81
82 // symbolic ordering and analysis, order = 2
83 var s = cs_sqr(2, m, false);
84
85 // partial pivoting
86 var r = cs_lu(m, s, 1);
87
88 // verify
89 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
90 });
91
92 it('should decompose matrix, 4 x 4, amd(A\'*A) (order=3), partial pivoting', function () {
93
94 var m = math.sparse(
95 [
96 [4.5, 0, 3.2, 0],
97 [3.1, 2.9, 0, 0.9],
98 [0, 1.7, 3, 0],
99 [3.5, 0.4, 0, 1]
100 ]);
101
102 // symbolic ordering and analysis, order = 3
103 var s = cs_sqr(3, m, false);
104
105 // partial pivoting
106 var r = cs_lu(m, s, 1);
107
108 // verify
109 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
110 });
111
112 it('should decompose matrix, 48 x 48, natural ordering (order=0), full pivoting, matrix market', function (done) {
113 // import matrix
114 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
115 .then(function (matrices) {
116 // matrix
117 var m = matrices[0];
118
119 // symbolic ordering and analysis, order = 0
120 var s = cs_sqr(0, m, false);
121
122 // full pivoting
123 var r = cs_lu(m, s, 0.001);
124
125 // verify
126 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
127
128 // indicate test has completed
129 done();
130 })
131 .fail(function (error) {
132 // indicate test has completed
133 done(error);
134 });
135 });
136
137 it('should decompose matrix, 48 x 48, amd(A+A\') (order=1), full pivoting, matrix market', function (done) {
138 // import matrix
139 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
140 .then(function (matrices) {
141 // matrix
142 var m = matrices[0];
143
144 // symbolic ordering and analysis, order = 1
145 var s = cs_sqr(1, m, false);
146
147 // full pivoting
148 var r = cs_lu(m, s, 0.001);
149
150 // verify
151 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
152
153 // indicate test has completed
154 done();
155 })
156 .fail(function (error) {
157 // indicate test has completed
158 done(error);
159 });
160 });
161
162 it('should decompose matrix, 48 x 48, amd(A\'*A) (order=2), full pivoting, matrix market', function (done) {
163 // import matrix
164 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
165 .then(function (matrices) {
166 // matrix
167 var m = matrices[0];
168
169 // symbolic ordering and analysis, order = 2
170 var s = cs_sqr(2, m, false);
171
172 // full pivoting
173 var r = cs_lu(m, s, 0.001);
174
175 // verify
176 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
177
178 // indicate test has completed
179 done();
180 })
181 .fail(function (error) {
182 // indicate test has completed
183 done(error);
184 });
185 });
186
187 it('should decompose matrix, 48 x 48, amd(A\'*A) (order=3), full pivoting, matrix market', function (done) {
188 // import matrix
189 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
190 .then(function (matrices) {
191 // matrix
192 var m = matrices[0];
193
194 // symbolic ordering and analysis, order = 3
195 var s = cs_sqr(3, m, false);
196
197 // full pivoting
198 var r = cs_lu(m, s, 0.001);
199
200 // verify
201 approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
202
203 // indicate test has completed
204 done();
205 })
206 .fail(function (error) {
207 // indicate test has completed
208 done(error);
209 });
210 });
211});
\No newline at end of file