UNPKG

5.44 kBJavaScriptView Raw
1var approx = require('../../../../tools/approx'),
2 math = require('../../../../index'),
3 market = require('../../../../tools/matrixmarket');
4
5describe('slu', function () {
6
7 it('should decompose matrix, 4 x 4, natural ordering (order=0), partial pivoting', function () {
8
9 var m = math.sparse(
10 [
11 [4.5, 0, 3.2, 0],
12 [3.1, 2.9, 0, 0.9],
13 [0, 1.7, 3, 0],
14 [3.5, 0.4, 0, 1]
15 ]);
16
17 // partial pivoting
18 var r = math.slu(m, 0, 1);
19
20 // verify M[p,q]=L*U
21 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
22 });
23
24 it('should decompose matrix, 4 x 4, amd(A+A\') (order=1)', function () {
25
26 var m = math.sparse(
27 [
28 [4.5, 0, 3.2, 0],
29 [3.1, 2.9, 0, 0.9],
30 [0, 1.7, 3, 0],
31 [3.5, 0.4, 0, 1]
32 ]);
33
34 // partial pivoting
35 var r = math.slu(m, 1, 1);
36
37 // verify M[p,q]=L*U
38 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
39 });
40
41 it('should decompose matrix, 4 x 4, amd(A\'*A) (order=2), partial pivoting', function () {
42
43 var m = math.sparse(
44 [
45 [4.5, 0, 3.2, 0],
46 [3.1, 2.9, 0, 0.9],
47 [0, 1.7, 3, 0],
48 [3.5, 0.4, 0, 1]
49 ]);
50
51 // partial pivoting
52 var r = math.slu(m, 2, 1);
53
54 // verify M[p,q]=L*U
55 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
56 });
57
58 it('should decompose matrix, 4 x 4, amd(A\'*A) (order=3), partial pivoting', function () {
59
60 var m = math.sparse(
61 [
62 [4.5, 0, 3.2, 0],
63 [3.1, 2.9, 0, 0.9],
64 [0, 1.7, 3, 0],
65 [3.5, 0.4, 0, 1]
66 ]);
67
68 // partial pivoting
69 var r = math.slu(m, 3, 1);
70
71 // verify M[p,q]=L*U
72 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
73 });
74
75 it('should decompose matrix, 48 x 48, natural ordering (order=0), full pivoting, matrix market', function (done) {
76 // import matrix
77 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
78 .then(function (matrices) {
79 // matrix
80 var m = matrices[0];
81
82 // full pivoting
83 var r = math.slu(m, 0, 0.001);
84
85 // verify M[p,q]=L*U
86 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
87
88 // indicate test has completed
89 done();
90 })
91 .fail(function (error) {
92 // indicate test has completed
93 done(error);
94 });
95 });
96
97 it('should decompose matrix, 48 x 48, amd(A+A\') (order=1), full pivoting, matrix market', function (done) {
98 // import matrix
99 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
100 .then(function (matrices) {
101 // matrix
102 var m = matrices[0];
103
104 // full pivoting
105 var r = math.slu(m, 1, 0.001);
106
107 // verify M[p,q]=L*U
108 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
109
110 // indicate test has completed
111 done();
112 })
113 .fail(function (error) {
114 // indicate test has completed
115 done(error);
116 });
117 });
118
119 it('should decompose matrix, 48 x 48, amd(A\'*A) (order=2), full pivoting, matrix market', function (done) {
120 // import matrix
121 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
122 .then(function (matrices) {
123 // matrix
124 var m = matrices[0];
125
126 // full pivoting
127 var r = math.slu(m, 2, 0.001);
128
129 // verify M[p,q]=L*U
130 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
131
132 // indicate test has completed
133 done();
134 })
135 .fail(function (error) {
136 // indicate test has completed
137 done(error);
138 });
139 });
140
141 it('should decompose matrix, 48 x 48, amd(A\'*A) (order=3), full pivoting, matrix market', function (done) {
142 // import matrix
143 market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
144 .then(function (matrices) {
145 // matrix
146 var m = matrices[0];
147
148 // full pivoting
149 var r = math.slu(m, 3, 0.001);
150
151 // verify M[p,q]=L*U
152 approx.deepEqual(_permute(m, r.p, r.q).valueOf(), math.multiply(r.L, r.U).valueOf());
153
154 // indicate test has completed
155 done();
156 })
157 .fail(function (error) {
158 // indicate test has completed
159 done(error);
160 });
161 });
162
163 /**
164 * C = A(p,q) where p is the row permutation vector and q the column permutation vector.
165 */
166 var _permute = function (A, pinv, q) {
167 // matrix arrays
168 var values = A._values;
169 var index = A._index;
170 var ptr = A._ptr;
171 var size = A._size;
172 // columns
173 var n = size[1];
174 // c arrays
175 var cvalues = [];
176 var cindex = [];
177 var cptr = [];
178 // loop columns
179 for (var k = 0 ; k < n ; k++) {
180 cptr[k] = cindex.length;
181 // column in C
182 var j = q ? (q[k]) : k;
183 // values in column j
184 for (var t = ptr[j]; t < ptr[j + 1]; t++) {
185 cvalues.push(values[t]);
186 cindex.push(pinv ? (pinv[index[t]]) : index[t]);
187 }
188 }
189 cptr[n] = cindex.length;
190 // return matrix
191 return new math.type.SparseMatrix({
192 values: cvalues,
193 index: cindex,
194 ptr: cptr,
195 size: size,
196 datatype: A._datatype
197 });
198 };
199});
\No newline at end of file