node-red-contrib-prib-functions 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,338 @@
1
+ const assert=require('assert');
2
+ const Matrix=require("../../matrix/matrix");
3
+
4
+ const m22=new Matrix([[1,2],[3,4]]);
5
+ const m44=new Matrix([
6
+ [ 1, 0, 2, -1 ],
7
+ [ 3, 0, 0, 5 ],
8
+ [ 2, 1, 4, -3 ],
9
+ [ 1, 0, 5, 0 ]]);
10
+
11
+ describe("matrix 01base", function() {
12
+ it('create no rows', function(done) {
13
+ assert.throws(()=>new Matrix(),Error)
14
+ assert.throws(()=>new Matrix(2),Error)
15
+ assert.throws(()=>new Matrix({rows:2}),Error("columns not specified"))
16
+ assert.throws(()=>new Matrix([]),Error)
17
+ assert.throws(()=>new Matrix([1,2,4]),Error)
18
+ assert.throws(()=>new Matrix([[]]),Error)
19
+ done();
20
+ });
21
+ it('create 2,3', function(done) {
22
+ const m=new Matrix(2,3);
23
+ assert.strictEqual(m.rows,2);
24
+ assert.strictEqual(m.columns,3);
25
+ assert.strictEqual(m.size,6);
26
+ done();
27
+ });
28
+ it('create {rows:2,columns:3} ', function(done) {
29
+ const m=new Matrix({rows:2,columns:3});
30
+ assert.strictEqual(m.rows,2);
31
+ assert.strictEqual(m.columns,3);
32
+ assert.strictEqual(m.size,6);
33
+ done();
34
+ });
35
+ it('getIndex', function(done) {
36
+ const m=new Matrix({rows:2,columns:3});
37
+ assert.strictEqual(m.getIndex(0,0),0);
38
+ assert.strictEqual(m.getIndex(0,1),1);
39
+ assert.strictEqual(m.getIndex(1,1),4);
40
+ assert.strictEqual(m.getIndex(1,2),5);
41
+ done();
42
+ });
43
+ it('equalsNearlyValues', function(done) {
44
+ const m=new Matrix({rows:2,columns:3});
45
+ assert.throws(()=>m.equalsNearlyValues(1,0))
46
+ assert.throws(()=>m.equalsNearlyValues(0,1))
47
+ assert.doesNotThrow(()=>m.equalsNearlyValues(0,0));
48
+ assert.doesNotThrow(()=>m.equalsNearlyValues(1,1));
49
+ assert.throws(()=>m.equalsNearlyValues(1,1.00001));
50
+ assert.doesNotThrow(()=>m.equalsNearlyValues(1,1.00001,2));
51
+ assert.throws(()=>m.equalsNearlyValues(1.000001,1));
52
+ assert.doesNotThrow(()=>m.equalsNearlyValues(1.000001,1,2));
53
+ done();
54
+ });
55
+ it('equalsNearlyVector', function(done) {
56
+ const m=new Matrix({rows:2,columns:3});
57
+ assert.doesNotThrow(()=>m.equalsNearlyVector([0,0,0,0,0,0]));
58
+ assert.throws(()=>m.equalsNearlyVector([1,2,3,4,5,6]));
59
+ done();
60
+ });
61
+ it('equalsNearly', function(done) {
62
+ assert.doesNotThrow(()=>m22.equalsNearly(m22));
63
+ assert.doesNotThrow(()=>m22.equalsNearly([[1,2],[3,4]]));
64
+ assert.throws(()=>m.equalsNearly([1,1],[1,1]));
65
+ done();
66
+ });
67
+ it('fill', function(done) {
68
+ const m=new Matrix({rows:2,columns:3});
69
+ m.fill(1,0,2)
70
+ assert.doesNotThrow(()=>m.equalsNearlyVector([1,1,0,0,0,0]));
71
+ m.fill(3,3,6)
72
+ assert.doesNotThrow(()=>m.equalsNearlyVector([1,1,0,3,3,3]));
73
+ done();
74
+ });
75
+ it('setRow', function(done) {
76
+ const m=new Matrix({rows:2,columns:3});
77
+ m.setRow([1,2,3],1)
78
+ assert.doesNotThrow(()=>m.equalsNearlyVector([0,0,0,1,2,3]));
79
+ done();
80
+ });
81
+ it('findColumnRow', function(done) {
82
+ const m=new Matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]);
83
+ const row=m.findColumnRow(1,(value)=>value==10);
84
+ assert.deepEqual(row,2);
85
+ done();
86
+ });
87
+ it('findRowColumn', function(done) {
88
+ const m=new Matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]);
89
+ const column=m.findRowColumn(2,(value)=>value==11);
90
+ assert.deepEqual(column,2);
91
+ done();
92
+ });
93
+ it('forEachCell', function(done) {
94
+ const m=new Matrix([[00,01,02],[10,11,12]]);
95
+ m.forEachCell((v,r,c)=>assert.strictEqual(v,r*10+c));
96
+ done();
97
+ });
98
+ it('getRow', function(done) {
99
+ const m=new Matrix([[00,01,02],[10,11,12],[20,21,22]]);
100
+ assert.deepEqual(m.getRow(1),new Float32Array([10,11,12]));
101
+ done();
102
+ });
103
+ it('addRow', function(done) {
104
+ const m=new Matrix({rows:2,columns:3}); //create a full zeroed
105
+ m.addRow([00,01,02]);
106
+ assert.doesNotThrow(()=>m.equalsNearlyVector(new Float32Array([0,0,0,0,1,2])));
107
+ m.addRow([10,11,12]);
108
+ assert.doesNotThrow(()=>m.equalsNearlyVector(new Float32Array([0,01,02,10,11,12])));
109
+ m.addRow([20,21,22]);
110
+ assert.doesNotThrow(()=>m.equalsNearlyVector(new Float32Array([10,11,12,20,21,22])));
111
+ done();
112
+ });
113
+ it('addRow start empty', function(done) {
114
+ const m=new Matrix({columns:3,rowsMax:2}); //create a full zeroed
115
+ m.addRow([00,01,02]);
116
+ assert.doesNotThrow(()=>m.equalsNearly([[0,1,2]]));
117
+ m.addRow([10,11,12]);
118
+ assert.doesNotThrow(()=>m.equalsNearly([[0,1,2],[10,11,12]]));
119
+ m.addRow([20,21,22]);
120
+ assert.doesNotThrow(()=>m.equalsNearly([[10,11,12],[20,21,22]]));
121
+ done();
122
+ });
123
+ it('create with array', function(done) {
124
+ const m=new Matrix([[00,01,02],[10,11,12]]);
125
+ assert.strictEqual(m.rows,2);
126
+ assert.strictEqual(m.columns,3);
127
+ assert.strictEqual(m.size,6);
128
+ assert.doesNotThrow(()=>m.equalsNearlyVector(new Float32Array([00,01,02,10,11,12])));
129
+ done();
130
+ });
131
+ it('reduceRow + sumRow', function(done) {
132
+ const m=new Matrix([[00,01,02],[10,11,12]]);
133
+ assert.strictEqual(m.sumRow(0),3);
134
+ assert.strictEqual(m.sumRow(1),33);
135
+ done();
136
+ });
137
+ it('set', function(done) {
138
+ const m=new Matrix([[00,01,02],[10,11,12],[20,21,22]]);
139
+ m.set(0,0,-1);
140
+ m.set(1,1,-11);
141
+ m.set(2,2,-22);
142
+ assert.doesNotThrow(()=>m.equalsNearly([[-1,01,02],[10,-11,12],[20,21,-22]]));
143
+ done();
144
+ });
145
+ it('toArray', function(done) {
146
+ const a=[[0,1,2],[10,11,12]];
147
+ const m=new Matrix(a);
148
+ assert.deepEqual(m.toArray(),a);
149
+ done();
150
+ });
151
+ it('multiply', function(done) {
152
+ const left=new Matrix([[1,0,1],[2,1,1],[0,1,1],[1,1,2]]);
153
+ const right=new Matrix([[1,2,1],[2,3,1],[4,2,2]]);
154
+ const result=left.multiply(right);
155
+ assert.deepEqual(result.toArray(),[[5,4,3],[8,9,5],[6,5,3],[11,9,6]])
156
+ done();
157
+ });
158
+ it('transpose', function(done) {
159
+ const m=new Matrix([[00,01,02],[10,11,12]]);
160
+ assert.deepEqual(m.transpose().toArray(),[[00,10],[01,11],[02,12]])
161
+ done();
162
+ });
163
+ it('swapRows', function(done) {
164
+ const m=new Matrix([[00,01],[10,11],[20,21],[30,31]]);
165
+ assert.deepEqual(m.swapRows(1,2).toArray(),[[00,01],[20,21],[10,11],[30,31]])
166
+ done();
167
+ });
168
+ it('multiplyRow', function(done) {
169
+ const m=new Matrix([[00,01],[10,11],[20,21],[30,31]]);
170
+ assert.deepEqual(m.multiplyRow(1,2).toArray(),[[00,01],[20,22],[20,21],[30,31]])
171
+ done();
172
+ });
173
+ it('addRow2Row', function(done) {
174
+ const m=new Matrix([[00,01],[10,11],[20,21],[30,31]]);
175
+ assert.deepEqual(m.addRow2Row(1,2,2).toArray(),[[00,01],[10,11],[40,43],[30,31]])
176
+ done();
177
+ });
178
+ it('add', function(done) {
179
+ const expect=[[00,02],[20,22],[40,42],[60,62]];
180
+ const m=new Matrix([[00,01],[10,11],[20,21],[30,31]]);
181
+ assert.deepEqual(m.add([[00,01],[10,11],[20,21],[30,31]]).toArray(),expect)
182
+ const m1=new Matrix([[00,01],[10,11],[20,21],[30,31]]);
183
+ assert.deepEqual(m1.add(m1).toArray(),expect)
184
+ done();
185
+ });
186
+ it('substract', function(done) {
187
+ const m=new Matrix([[00,01],[10,11],[20,21],[30,31]]);
188
+ assert.deepEqual(m.substract([[00,01],[10,11],[20,21],[30,31]]).toArray(),[[0,0],[0,0],[0,0],[0,0]])
189
+ const m1=new Matrix([[00,01],[10,11],[20,21],[30,31]]);
190
+ assert.deepEqual(m1.substract(m1).toArray(),[[0,0],[0,0],[0,0],[0,0]])
191
+ done();
192
+ });
193
+ it('identity', function(done) {
194
+ const m=new Matrix([[00,01,02],[10,11,12],[20,21,22]]);
195
+ assert.deepEqual(m.getIdentity().toArray(),[[1,0,0],[0,1,0],[0,0,1]]);
196
+ done();
197
+ });
198
+ it('getMatrix', function(done) {
199
+ const m=new Matrix([[00,01,02],[10,11,12],[20,21,22]]);
200
+ assert.deepEqual(m.getMatrix(1,1,2,2).toArray(),[[11,12],[21,22]]);
201
+ done();
202
+ });
203
+ it('divideRow', function(done) {
204
+ const m=new Matrix([[00,01,02,03],[10,11,12,13],[20,21,22,23]]);
205
+ const r=new Matrix([[00,01,02,03],[10/2,11/2/2,12/2/2,13/2],[20,21,22,23]]);
206
+ const p=new Matrix([[00,01,02,03],[10,11/2,12/2,13],[20,21,22,23]]);
207
+ assert.deepEqual(m.divideRow(1,2,1,2).toArray(),p.toArray());
208
+ assert.deepEqual(m.divideRow(1,2).toArray(),r.toArray());
209
+ done();
210
+ });
211
+ it('rowEchelonForm 1', function(done) {
212
+ const m=new Matrix([[3,4,-3],[2,5,5],[-2,-3,1]]).rowEchelonForm();
213
+ assert.doesNotThrow(()=>m.equalsNearly([[3/3,4/3,-3/3],[0,1,3],[0,0,0]]));
214
+ done();
215
+ });
216
+ it('rowEchelonForm 2', function(done) {
217
+ const m=new Matrix([[0,3,-6,6,4,-5],
218
+ [3,-7,8,-5,8,9],
219
+ [3,-9,12,-9,6,15]]);
220
+ const echelonForm =new Matrix(
221
+ [[1,-7/3,8/3,-5/3,8/3,3],
222
+ [0,1,-2,2,4/3,-5/3],
223
+ [0,0,0,0,1,4]]);
224
+ assert.doesNotThrow(()=>m.rowEchelonForm().equalsNearly(echelonForm));
225
+ done();
226
+ });
227
+ it('rowEchelonForm 4x', function(done) {
228
+ const m=new Matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]);
229
+ const a=new Matrix([[1,2,3,4],[0,1,2,3],[0,0,0,0],[0,0,0,0]]);
230
+ assert.doesNotThrow(()=>m.rowEchelonForm().equalsNearly(a));
231
+ done();
232
+ });
233
+ it('reducedRowEchelonForm 1', function(done) {
234
+ const m=new Matrix([[0,1],[0,0],[5,9]]).reducedRowEchelonForm();
235
+ // console.log(m.toArray());
236
+ assert.doesNotThrow(()=>m.equalsNearly([[1,9/5],[0,1],[0,0]]));
237
+ done();
238
+ });
239
+ it('reducedRowEchelonForm 2', function(done) {
240
+ const m=new Matrix([[0,3,-6,6,4,-5],
241
+ [3,-7,8,-5,8,9],
242
+ [3,-9,12,-9,6,15]]).reducedRowEchelonForm();
243
+ const reducedForm =[[1,0,-2,3,0,-24],
244
+ [0,1,-2,2,0,-7],
245
+ [0,0,0,0,1,4]];
246
+ // console.log(m.toArray());
247
+ assert.doesNotThrow(()=>m.equalsNearly(reducedForm));
248
+ done();
249
+ });
250
+ it('getInverseGaussJordan', function(done) {
251
+ const m=new Matrix([[1,2,-2],
252
+ [1,-1,2],
253
+ [3,2,1]]).getInverseGaussJordan();
254
+ // console.log(m.toArray());
255
+ assert.doesNotThrow(()=>m.equalsNearly([[1,6/5,-2/5],[-1,-7/5,4/5],[-1,-4/5,3/5]]));
256
+ done();
257
+ });
258
+ const m33=new Matrix([[1,2,3],
259
+ [4,5,6],
260
+ [7,8,9]]);
261
+ it('getCofactor33 0 0', function(done) {
262
+ assert.doesNotThrow(()=>m33.getCofactor(0,0).equalsNearly([[5,6],[8,9]]));
263
+ done();
264
+ });
265
+ it('getCofactor33 0 1', function(done) {
266
+ assert.doesNotThrow(()=>m33.getCofactor(0,1).equalsNearly([[4,6],[7,9]]));
267
+ done();
268
+ });
269
+ it('getCofactor33 0 2', function(done) {
270
+ assert.doesNotThrow(()=>m33.getCofactor(0,2).equalsNearly([[4,5],[7,8]]));
271
+ done();
272
+ });
273
+ it('getCofactor33 1 1', function(done) {
274
+ assert.doesNotThrow(()=>m33.getCofactor(1,1).equalsNearly([[1,3],[7,9]]));
275
+ done();
276
+ });
277
+
278
+ it('getDeterminantUsingCofactor22', function(done) {
279
+ assert.deepEqual(m22.clone().getDeterminantUsingCofactor(),1*4-2*3);
280
+ done();
281
+ });
282
+
283
+ it('getDeterminantUsingCofactor44', function(done) {
284
+ assert.deepEqual(m44.clone().getDeterminantUsingCofactor(),30);
285
+ done();
286
+ });
287
+ it('getDeterminantUsingRowEchelonForm22', function(done) {
288
+ assert.deepEqual(m22.clone().getDeterminantUsingRowEchelonForm(),1*4-2*3);
289
+ done();
290
+ });
291
+ it('getDeterminantUsingRowEchelonForm44', function(done) {
292
+ assert.deepEqual(m44.clone().getDeterminantUsingRowEchelonForm(),30);
293
+ done();
294
+ });
295
+ it('getDeterminant44', function(done) {
296
+ assert.deepEqual(m44.clone().getDeterminant(),30);
297
+ done();
298
+ });
299
+ it('forwardElimination', function(done) {
300
+ const m=new Matrix([[3.0, 2.0, -4.0, 3.0],
301
+ [2.0, 3.0, 3.0, 15.0],
302
+ [5.0, -3, 1.0, 14.0]]).forwardElimination();
303
+ assert.doesNotThrow(()=>m.equalsNearly(
304
+ [[5.00,-3.00,1.00,14.00],
305
+ [0.00,4.20,2.60,9.40],
306
+ [0.00,0.00,-6.95,-13.90]],2));
307
+ done();
308
+ });
309
+ it('gaussianElimination', function(done) {
310
+ const m=new Matrix([[3.0, 2.0, -4.0, 3.0],
311
+ [2.0, 3.0, 3.0, 15.0],
312
+ [5.0, -3, 1.0, 14.0]]);
313
+ const v=m.gaussianElimination();
314
+ assert.doesNotThrow(()=>m.equalsNearlyVector(v,6,[3,1,2]));
315
+ done();
316
+ });
317
+ it('getAdjoint', function(done) {
318
+ const m=new Matrix([[-3,2,-5],
319
+ [-1,0,-2],
320
+ [3,-4,1]]).getAdjoint();
321
+ const a=[
322
+ [-8,18,-4],
323
+ [-5,12,-1],
324
+ [4,-6,2]
325
+ ]
326
+
327
+ assert.doesNotThrow(()=>m.equalsNearly(a));
328
+ done();
329
+ });
330
+ it('getInverseAdjointMethod', function(done) {
331
+ const m=new Matrix([
332
+ [1,2,-2],
333
+ [1,-1,2],
334
+ [3,2,1]]).getInverseAdjointMethod();
335
+ assert.doesNotThrow(()=>m.equalsNearly([[1,6/5,-2/5],[-1,-7/5,4/5],[-1,-4/5,3/5]]));
336
+ done();
337
+ });
338
+ });
@@ -0,0 +1,68 @@
1
+ const assert=require('assert');
2
+ const should=require("should");
3
+ const Matrix=require("../../matrix/matrixNode.js");
4
+ const helper=require("node-red-node-test-helper");
5
+ helper.init(require.resolve('node-red'));
6
+ const helperNodeResults={id:"helperNodeResultsId",type: "helper"}
7
+
8
+ const matrixNode={
9
+ "id": "matrixNodeId",
10
+ "type": "matrixNode",
11
+ "action": "create",
12
+ // "targetProperty": "msg._matrix",
13
+ "wires": [
14
+ ["helperNodeResults"],
15
+ ]
16
+ };
17
+
18
+ function getNode(node) {
19
+ const n=helper.getNode(node.id);
20
+ if(n) return n;
21
+ throw Error("node id: "+node.id+" not found, node: "+JSON.stringify(node))
22
+ };
23
+ describe('matrix', function() {
24
+ beforeEach(function(done) {
25
+ helper.startServer(done);
26
+ });
27
+ afterEach(function(done) {
28
+ helper.unload();
29
+ helper.stopServer(done);
30
+ });
31
+
32
+ /*
33
+ it("test loading", function(done) {
34
+ helper.load(Matrix,[matrixNode], function() {
35
+ try{
36
+ const nodeMatrix=getNode(matrixNode);
37
+ done();
38
+ } catch(ex) {
39
+ done(ex);
40
+ }
41
+ });
42
+ });
43
+ const matrixFlow=[matrixNode,helperNodeResults]
44
+ it("add", function(done) {
45
+ let count=0;
46
+ helper.load(Matrix,matrixFlow, function() {
47
+ try{
48
+ const nodeResults=getNode(helperNodeResults);
49
+ const nodeMatrix=getNode(matrixNode);
50
+ nodeMatrix.should.have.property("action", "create");
51
+ nodeResults.on("input", function(msg) {
52
+ switch (msg.topic) {
53
+ case "load" :
54
+ done();
55
+ break
56
+ default: done("unknown message "+msg.topic);
57
+ }
58
+ if(--count==0) done()
59
+ });
60
+ count++;nodeMatrix.receive({topic:"load",payload:[[1,2],[3,4]]});
61
+ } catch(ex) {
62
+ done(ex);
63
+ }
64
+ });
65
+ }).timeout(5000);
66
+ */
67
+
68
+ });