1 | var assert = require('assert');
|
2 | var approx = require('../../../../tools/approx');
|
3 | var market = require('../../../../tools/matrixmarket');
|
4 | var math = require('../../../../index');
|
5 | math.import(require('../../../../lib/function/algebra/sparse/cs_permute'));
|
6 | math.import(require('../../../../lib/function/algebra/sparse/cs_lu'));
|
7 | math.import(require('../../../../lib/function/algebra/sparse/cs_sqr'));
|
8 |
|
9 | var cs_permute = math.sparse.cs_permute;
|
10 | var cs_lu = math.sparse.cs_lu;
|
11 | var cs_sqr = math.sparse.cs_sqr;
|
12 |
|
13 | describe('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 |
|
20 | var r = cs_lu(m, null, 1);
|
21 |
|
22 |
|
23 | assert.deepEqual(r.L.valueOf(), [[1, 0], [0.5, 1]]);
|
24 |
|
25 | assert.deepEqual(r.U.valueOf(), [[2, 1], [0, 3.5]]);
|
26 |
|
27 | assert.deepEqual(r.pinv, [0, 1]);
|
28 |
|
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 |
|
43 | var s = cs_sqr(0, m, false);
|
44 |
|
45 |
|
46 | var r = cs_lu(m, s, 1);
|
47 |
|
48 |
|
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 |
|
63 | var s = cs_sqr(1, m, false);
|
64 |
|
65 |
|
66 | var r = cs_lu(m, s, 1);
|
67 |
|
68 |
|
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 |
|
83 | var s = cs_sqr(2, m, false);
|
84 |
|
85 |
|
86 | var r = cs_lu(m, s, 1);
|
87 |
|
88 |
|
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 |
|
103 | var s = cs_sqr(3, m, false);
|
104 |
|
105 |
|
106 | var r = cs_lu(m, s, 1);
|
107 |
|
108 |
|
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 |
|
114 | market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
|
115 | .then(function (matrices) {
|
116 |
|
117 | var m = matrices[0];
|
118 |
|
119 |
|
120 | var s = cs_sqr(0, m, false);
|
121 |
|
122 |
|
123 | var r = cs_lu(m, s, 0.001);
|
124 |
|
125 |
|
126 | approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
|
127 |
|
128 |
|
129 | done();
|
130 | })
|
131 | .fail(function (error) {
|
132 |
|
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 |
|
139 | market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
|
140 | .then(function (matrices) {
|
141 |
|
142 | var m = matrices[0];
|
143 |
|
144 |
|
145 | var s = cs_sqr(1, m, false);
|
146 |
|
147 |
|
148 | var r = cs_lu(m, s, 0.001);
|
149 |
|
150 |
|
151 | approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
|
152 |
|
153 |
|
154 | done();
|
155 | })
|
156 | .fail(function (error) {
|
157 |
|
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 |
|
164 | market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
|
165 | .then(function (matrices) {
|
166 |
|
167 | var m = matrices[0];
|
168 |
|
169 |
|
170 | var s = cs_sqr(2, m, false);
|
171 |
|
172 |
|
173 | var r = cs_lu(m, s, 0.001);
|
174 |
|
175 |
|
176 | approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
|
177 |
|
178 |
|
179 | done();
|
180 | })
|
181 | .fail(function (error) {
|
182 |
|
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 |
|
189 | market.import('tools/matrices/bcsstk01.tar.gz', ['bcsstk01/bcsstk01.mtx'])
|
190 | .then(function (matrices) {
|
191 |
|
192 | var m = matrices[0];
|
193 |
|
194 |
|
195 | var s = cs_sqr(3, m, false);
|
196 |
|
197 |
|
198 | var r = cs_lu(m, s, 0.001);
|
199 |
|
200 |
|
201 | approx.deepEqual(cs_permute(m, r.pinv, s.q, true).valueOf(), math.multiply(r.L, r.U).valueOf());
|
202 |
|
203 |
|
204 | done();
|
205 | })
|
206 | .fail(function (error) {
|
207 |
|
208 | done(error);
|
209 | });
|
210 | });
|
211 | }); |
\ | No newline at end of file |