ziko 0.51.0 → 0.52.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.
Files changed (37) hide show
  1. package/dist/ziko.cjs +1638 -1456
  2. package/dist/ziko.js +1446 -1385
  3. package/dist/ziko.min.js +2 -2
  4. package/dist/ziko.mjs +1446 -1385
  5. package/package.json +1 -1
  6. package/src/data/converter/csv.js +1 -1
  7. package/src/{__helpers__ → helpers}/register/index.js +1 -1
  8. package/src/hooks/use-ipc.js +1 -3
  9. package/src/math/adapted/index.js +7 -0
  10. package/src/math/complex/index.js +7 -5
  11. package/src/math/discret/Conversion/index.js +1 -1
  12. package/src/math/discret/Logic/index.js +1 -1
  13. package/src/math/functions/helper.js +68 -17
  14. package/src/math/functions/index.js +11 -11
  15. package/src/math/functions/primitives/index.js +176 -0
  16. package/src/math/mapfun/index.js +19 -0
  17. package/src/math/matrix/helpers/det.js +36 -0
  18. package/src/math/matrix/helpers/index.js +3 -0
  19. package/src/math/matrix/helpers/inverse.js +52 -0
  20. package/src/math/matrix/helpers/stack.js +24 -0
  21. package/src/math/matrix/index.js +1 -1
  22. package/src/math/matrix/matrix.js +601 -0
  23. package/src/math/random/index.js +88 -92
  24. package/src/math/signal/functions.js +23 -25
  25. package/src/math/utils/arithmetic.js +15 -17
  26. package/src/math/utils/index.js +1 -1
  27. package/src/math/utils/mapfun.js +31 -35
  28. package/src/ui/constructors/UIElement.js +9 -9
  29. package/src/ui/constructors/UIElementCore.js +2 -2
  30. package/types/math/adapted/index.d.ts +4 -0
  31. package/types/math/complex/index.d.ts +1 -1
  32. package/types/math/mapfun/index.d.ts +87 -0
  33. package/src/math/matrix/Matrix.js +0 -675
  34. /package/src/{__helpers__ → helpers}/checkers/index.js +0 -0
  35. /package/src/{__helpers__ → helpers}/index.js +0 -0
  36. /package/src/{__helpers__ → helpers}/register/register-to-class.js +0 -0
  37. /package/src/{__helpers__ → helpers}/register/register-to-instance.js +0 -0
@@ -1,675 +0,0 @@
1
- import{
2
- pow,
3
- min,
4
- max,
5
- } from "../functions/index.js"
6
- import {Utils} from "../utils/index.js";
7
- import {Complex } from "../complex/index.js";
8
- import {Random} from "../random/index.js"
9
- import { arr2str } from "../../data/index.js";
10
- class Matrix{
11
- constructor(rows, cols, element = [] ) {
12
- if(rows instanceof Matrix){
13
- this.arr=rows.arr;
14
- this.rows=rows.rows;
15
- this.cols=rows.cols;
16
- }
17
- else {
18
- let arr = [],
19
- i,
20
- j;
21
- if (arguments[0] instanceof Array) {
22
- rows = arguments[0].length;
23
- cols = arguments[0][0].length;
24
- arr = arguments[0];
25
- } else {
26
- for (i = 0; i < rows; i++) {
27
- arr.push([]);
28
- arr[i].push(new Array(cols));
29
- for (j = 0; j < cols; j++) {
30
- arr[i][j] = element[i * cols + j];
31
- if (element[i * cols + j] == undefined) arr[i][j] = 0;
32
- }
33
- }
34
- }
35
- this.rows = rows;
36
- this.cols = cols;
37
- this.arr = arr;
38
- }
39
- this.#maintain();
40
- //Object.seal(this);
41
- }
42
- toString(){
43
- return arr2str(this.arr,false);
44
- }
45
- at(i=0,j=undefined){
46
- if(i<0)i=this.rows+i;
47
- if(j==undefined) return this.arr[i];
48
- if(j<0)j=this.cols+j;
49
- return this.arr[i][j];
50
- }
51
- reshape(newRows, newCols) {
52
- let check = newRows * newCols === this.rows * this.cols;
53
- if (check) return new Matrix(newRows, newCols, this.arr.flat(1));
54
- else console.error("Err");
55
- }
56
- static eye(size) {
57
- let result = new Matrix(size, size);
58
- for (let i = 0; i < size; i++) for (let j = 0; j < size; j++) i === j ? (result.arr[i][j] = 1) : (result.arr[i][j] = 0);
59
- return result;
60
- }
61
- get clone() {
62
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
63
- }
64
- get size() {
65
- return this.rows * this.cols;
66
- }
67
- get shape() {
68
- return [this.rows, this.cols];
69
- }
70
- get reel() {
71
- return new Matrix(this.cols, this.rows, this.arr.flat(1).reel);
72
- }
73
- get imag() {
74
- return new Matrix(this.cols, this.rows, this.arr.flat(1).imag);
75
- }
76
- [Symbol.iterator]() {
77
- return this.arr[Symbol.iterator]();
78
- }
79
- #maintain() {
80
- for (let i = 0; i < this.arr.length; i++) {
81
- Object.defineProperty(this, i, {
82
- value: this.arr[i],
83
- writable: true,
84
- configurable: true,
85
- enumerable: false
86
- });
87
- }
88
- }
89
- get(row = 0, col = 0) {
90
- if (col == -1) return this.arr[row];
91
- else if (row == -1) return this.arr.map((n) => n[col]);
92
- else return this.arr[row][col];
93
- }
94
- set(row = 0, col = 0, value) {
95
- if (col == -1) return (this.arr[row] = value);
96
- else if (row == -1) {
97
- for (let i = 0; i < this.cols; i++) {
98
- this.arr[i][col] = value[i] || 0;
99
- }
100
- return this.arr;
101
- }
102
- return (this.arr[row][col] = value);
103
- }
104
- get isSquare() {
105
- return this.rows / this.cols === 1;
106
- }
107
- get isSym() {
108
- if (!this.isSquare) return false;
109
- const T = this.T;
110
- const M = this.clone;
111
- return Matrix.sub(M, T).max == 0 && Matrix.sub(M, T).min == 0;
112
- }
113
- get isAntiSym() {
114
- if (!this.isSquare) return false;
115
- const T = this.T;
116
- const M = this.clone;
117
- return Matrix.add(M, T).max == 0 && Matrix.add(M, T).min == 0;
118
- }
119
- get isDiag() {
120
- if (!this.isSquare) return false;
121
- const T = this.T;
122
- const M = this.clone;
123
- const MT = Matrix.mul(M, T);
124
- const TM = Matrix.dot(T, M);
125
- return Matrix.sub(MT, TM).max == 0 && Matrix.sub(MT, TM).min == 0;
126
- }
127
- get isOrtho() {
128
- if (!this.isSquare) return false;
129
- return this.isDiag && (this.det == 1 || this.det == -1);
130
- }
131
- get isIdemp() {
132
- if (!this.isSquare) return false;
133
- const M = this.clone;
134
- const MM = Matrix.dot(M, M);
135
- return Matrix.sub(MM, M).max == 0 && Matrix.sub(MM, M).min == 0;
136
- }
137
- get T() {
138
- let transpose = [];
139
- for (let i = 0; i < this.arr[0].length; i++) {
140
- transpose[i] = [];
141
- for (let j = 0; j < this.arr.length; j++) {
142
- transpose[i][j] = this.arr[j][i];
143
- }
144
- }
145
- return new Matrix(this.cols, this.rows, transpose.flat(1));
146
- }
147
- get det() {
148
- if (!this.isSquare) return new Error("is not square matrix");
149
- if (this.rows == 1) return this.arr[0][0];
150
- function determinat(M) {
151
- if (M.length == 2) {
152
- if (M.flat(1).some((n) => n instanceof Matrix)) {
153
- console.warn("Tensors are not completely supported yet ...");
154
- return;
155
- }
156
- return Utils.sub(Utils.mul(M[0][0],M[1][1]),Utils.mul(M[0][1],M[1][0]))
157
- }
158
- var answer = 0;
159
- for (var i = 0; i < M.length; i++) {
160
- //console.log(M[0][i]);
161
- /*answer = answer.add(
162
- pow(-1, i)
163
- .mul(M[0][i])
164
- .mul(determinat(deleteRowAndColumn(M, i)))
165
- );*/
166
- //const to_be_added=Utils.add(Utils.mul(pow(-1, i),Utils.mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
167
- const to_be_added=Utils.add(Utils.mul(pow(-1, i),Utils.mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
168
- answer=Utils.add(answer,to_be_added)
169
- }
170
- return answer;
171
- }
172
- function deleteRowAndColumn(M, index) {
173
- var temp = [];
174
- for (let i = 0; i < M.length; i++) temp.push(M[i].slice(0));
175
- temp.splice(0, 1);
176
- for (let i = 0; i < temp.length; i++) temp[i].splice(index, 1);
177
- return temp;
178
- }
179
- return determinat(this.arr);
180
- }
181
- get inv() {
182
- if (!this.isSquare) return new Error("is not square matrix");
183
- if (this.det === 0) return "determinat = 0 !!!";
184
- let A = InverseMatrixe(this.arr);
185
- return new Matrix(this.rows, this.cols, A.flat(1));
186
- }
187
- static zeros(rows, cols) {
188
- let result = new Matrix(rows, cols);
189
- for (let i = 0; i < rows; i++) for (var j = 0; j < cols; j++) result.arr[i][j] = 0;
190
- return result;
191
- }
192
- static ones(rows, cols) {
193
- let result = new Matrix(rows, cols);
194
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = 1;
195
- return result;
196
- }
197
- static nums(rows, cols, number) {
198
- let result = new Matrix(rows, cols);
199
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = number;
200
- return result;
201
- }
202
- static get rand(){
203
- return {
204
- int:(rows, cols, a, b)=>{
205
- let result = new Matrix(rows, cols);
206
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randInt(a, b);
207
- return result;
208
- },
209
- bin:(rows,cols)=>{
210
- let result = new Matrix(rows, cols);
211
- for (let i = 0; i < rows; i++) {
212
- for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randBin;
213
- }
214
- return result;
215
- },
216
- hex:(rows,cols)=>{
217
- let result = new Matrix(rows, cols);
218
- for (let i = 0; i < rows; i++) {
219
- for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randHex;
220
- }
221
- return result;
222
- },
223
- choices:(rows, cols, choices, p)=>{
224
- let result = new Matrix(rows, cols);
225
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.choice(choices, p);
226
- return result
227
- },
228
- permutation:(rows,cols,arr)=>{
229
- //return new Matrix(rows, cols, Random.permutation(...arr))
230
- }
231
- }
232
- }
233
- static rands(rows, cols, a = 1, b) {
234
- let result = new Matrix(rows, cols);
235
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.rand(a, b);
236
- return result;
237
- }
238
- map(Imin, Imax, Fmin, Fmax) {
239
- return Utils.map(this, Imin, Imax, Fmin, Fmax);
240
- }
241
- lerp(min, max) {
242
- return Utils.lerp(this, min, max);
243
- }
244
- norm(min, max) {
245
- return Utils.norm(this, min, max);
246
- }
247
- clamp(min, max) {
248
- return Utils.clamp(this, min, max);
249
- }
250
- static map(matrix, Imin, Imax, Fmin, Fmax) {
251
- return Utils.map(matrix, Imin, Imax, Fmin, Fmax);
252
- }
253
- static lerp(matrix, min, max) {
254
- return Utils.lerp(matrix, min, max);
255
- }
256
- static norm(matrix, min, max) {
257
- return Utils.norm(matrix, min, max);
258
- }
259
- static clamp(m, min, max) {
260
- return Utils.clamp(matrix, min, max);
261
- }
262
- toPrecision(p) {
263
- for (let i = 0; i < this.cols; i++) for (let j = 0; j < this.rows; j++) this.arr[i][j] = +this.arr[i][j].toPrecision(p);
264
- return this;
265
- }
266
- get toBin() {
267
- let newArr = this.arr.flat(1).toBin;
268
- return new Matrix(this.rows, this.cols, newArr);
269
- }
270
- get toOct() {
271
- let newArr = this.arr.flat(1).toOct;
272
- return new Matrix(this.rows, this.cols, newArr);
273
- }
274
- get toHex() {
275
- let newArr = this.arr.flat(1).toHex;
276
- return new Matrix(this.rows, this.cols, newArr);
277
- }
278
- /*get isOdd() {
279
- let newArr = this.arr.flat(1).isOdd;
280
- return new Matrix(this.rows, this.cols, newArr);
281
- }*/
282
- max2min() {
283
- let newArr = this.arr.flat(1).max2min;
284
- return new Matrix(this.rows, this.cols, newArr);
285
- }
286
- min2max() {
287
- let newArr = this.arr.flat(1).min2max;
288
- return new Matrix(this.rows, this.cols, newArr);
289
- }
290
- sortRows(calback=undefined){
291
- let newArr=this.arr.map(n=>n.sort(calback)).flat(1);
292
- return new Matrix(this.rows, this.cols, newArr);
293
- }
294
- sortCols(calback=undefined){
295
- let m=this.T;
296
- let newArr=m.arr.map(n=>n.sort(calback)).flat(1);
297
- return new Matrix(this.rows, this.cols, newArr).T;
298
- }
299
- filterByRows(item){
300
- var truth=this.arr.map(n=>n.map(m=>+(""+m).includes(item)))
301
- var mask=truth.map(n=>!!Logic.or(...n))
302
- var filtredArray=this.arr.filter((n,i)=>mask[i]===true)
303
- if(filtredArray.length===0)filtredArray.push([])
304
- console.log(filtredArray)
305
- return new Matrix(filtredArray)
306
- }
307
- filterByCols(item){
308
- return new Matrix(this.T.arr.filter(n=>n.includes(item)))
309
- }
310
- sortAll(calback=undefined){
311
- let newArr=this.arr.flat(1).sort(calback);
312
- return new Matrix(this.rows, this.cols, newArr);
313
- }
314
- count(n) {
315
- return this.arr.flat(1).count(n);
316
- }
317
- toBase(n) {
318
- let newArr = this.arr.flat(1).toBase(n);
319
- return new Matrix(this.rows, this.cols, newArr);
320
- }
321
- #hstack(matrix){
322
- if (this.rows !== matrix.rows) return;
323
- let newArr = this.arr;
324
- for (let i = 0; i < this.rows; i++) for (let j = this.cols; j < this.cols + matrix.cols; j++) newArr[i][j] = matrix.arr[i][j - this.cols];
325
- this.cols += matrix.cols;
326
- return new Matrix(this.rows, this.cols, newArr.flat(1));
327
- }
328
- hstack(...matrices) {
329
- const M=[this,...matrices].reduce((a,b)=>a.#hstack(b));
330
- Object.assign(this,M)
331
- return this;
332
- }
333
- static hstack(matrix,...matrices) {
334
- return matrix.clone.hstack(...matrices);
335
- }
336
- #vstack(matrix) {
337
- if (this.cols !== matrix.cols) return;
338
- let newArr = this.arr;
339
- for (let i = this.rows; i < this.rows + matrix.rows; i++) {
340
- newArr[i] = [];
341
- for (let j = 0; j < this.cols; j++) newArr[i][j] = matrix.arr[i - this.rows][j];
342
- }
343
- this.rows += matrix.rows;
344
- return new Matrix(this.rows, this.cols, newArr.flat(1));
345
- }
346
- vstack(...matrices) {
347
- const M=[this,...matrices].reduce((a,b)=>a.#vstack(b));
348
- Object.assign(this,M)
349
- return this;
350
- }
351
- static vstack(matrix,...matrices) {
352
- return matrix.clone.vstack(...matrices);
353
- }
354
- hqueue(...matrices){
355
- const M=[this,...matrices].reverse().reduce((a,b)=>a.#hstack(b));
356
- Object.assign(this,M)
357
- return this;
358
- }
359
- vqueue(...matrices){
360
- const M=[this,...matrices].reverse().reduce((a,b)=>a.#vstack(b));
361
- Object.assign(this,M)
362
- return this;
363
- }
364
- static hqueue(matrix,...matrices) {
365
- return matrix.clone.hqueue(...matrices);
366
- }
367
- static vqueue(matrix,...matrices) {
368
- return matrix.clone.vqueue(...matrices);
369
- }
370
- slice(r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
371
- let newRow = r1 - r0,
372
- newCol = c1 - c0;
373
- let newArr = new Array(newCol);
374
- for (let i = 0; i < newRow; i++) {
375
- newArr[i] = [];
376
- for (let j = 0; j < newCol; j++) newArr[i][j] = this.arr[i + r0][j + c0];
377
- }
378
- return new Matrix(newRow, newCol, newArr.flat(1));
379
- }
380
- static slice(m1,r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
381
- return m1.slice(r0, c0, r1, c1);
382
- }
383
- splice(r0,c0,deleteCount,...items){
384
-
385
- }
386
- getRows(ri, rf = ri + 1) {
387
- return this.slice(ri, 0, rf, this.cols);
388
- }
389
- getCols(ci, cf = ci + 1) {
390
- return this.slice(0, ci, this.rows, cf);
391
- }
392
- static getRows(m, ri, rf = ri + 1) {
393
- return m.slice(ri, 0, rf, m.cols);
394
- }
395
- static getCols(m, ci, cf = ci + 1) {
396
- return m.slice(0, ci, m.rows, cf);
397
- }
398
- add(...matr) {
399
- for (let k = 0; k < matr.length; k++) {
400
- if (typeof matr[k] == "number"||matr[k] instanceof Complex) matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
401
- for (let i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++) this.arr[i][j] = Utils.add(this.arr[i][j],matr[k].arr[i][j]);
402
- }
403
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
404
- }
405
- sub(...matr) {
406
- for (let k = 0; k < matr.length; k++) {
407
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
408
- for (let i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++) this.arr[i][j] = Utils.sub(this.arr[i][j],matr[k].arr[i][j]);
409
- }
410
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
411
- }
412
- static add(m1, ...m2) {
413
- return m1.clone.add(...m2);
414
- }
415
- static sub(m1, ...m2) {
416
- return m1.clone.sub(...m2);
417
- }
418
- mul(...matr) {
419
- for (let k = 0; k < matr.length; k++) {
420
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
421
- for (var i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++) this.arr[i][j] = Utils.mul(this.arr[i][j],matr[k].arr[i][j]);
422
- }
423
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
424
- }
425
- div(...matr) {
426
- for (let k = 0; k < matr.length; k++) {
427
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
428
- for (let i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++) this.arr[i][j] = Utils.div(this.arr[i][j],matr[k].arr[i][j]);
429
- }
430
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
431
- }
432
- static div(m1, ...m2) {
433
- return m1.clone.div(...m2);
434
- }
435
- static mul(m1, ...m2) {
436
- return m1.clone.mul(...m2);
437
- }
438
- modulo(...matr) {
439
- for (let k = 0; k < matr.length; k++) {
440
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
441
- for (let i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++)this.arr[i][j]=Utils.modulo(this.arr[i][j],matr[k].arr[i][j]);
442
- }
443
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
444
- }
445
- static modulo(m1, ...m2) {
446
- return m1.clone.modulo(...m2);
447
- }
448
- dot(matrix) {
449
- var res = [];
450
- for (var i = 0; i < this.arr.length; i++) {
451
- res[i] = [];
452
- for (var j = 0; j < matrix.arr[0].length; j++) {
453
- res[i][j] = 0;
454
- for (var k = 0; k < this.arr[0].length; k++) {
455
- res[i][j] = Utils.add(
456
- res[i][j],
457
- Utils.mul(this.arr[i][k],matrix.arr[k][j])
458
- )
459
- }
460
- }
461
- }
462
- return new Matrix(this.arr.length, matrix.arr[0].length, res.flat(1));
463
- }
464
- static dot(matrix1, matrix2) {
465
- return matrix1.dot(matrix2);
466
- }
467
- pow(n) {
468
- let a = this.clone,
469
- p = this.clone;
470
- for (let i = 0; i < n - 1; i++) p = p.dot(a);
471
- return p;
472
- }
473
- static pow(m, n) {
474
- return m.clone.pow(n);
475
- }
476
- get somme() {
477
- let S = 0;
478
- for (let i = 0; i < this.rows; i++) for (let j = 0; j < this.cols; j++) S += this.arr[i][j];
479
- return S;
480
- }
481
- get DoesItContainComplexNumbers() {
482
- return this.arr.flat(Infinity).some((n) => n instanceof Complex);
483
- }
484
- get min() {
485
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
486
- let minRow = [];
487
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
488
- return min(...minRow);
489
- }
490
- get max() {
491
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
492
- let maxRow = [];
493
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
494
- return max(...maxRow);
495
- }
496
- get minRows() {
497
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
498
- let minRow = [];
499
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
500
- return minRow;
501
- }
502
- get maxRows() {
503
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
504
- let maxRow = [];
505
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
506
- return maxRow;
507
- }
508
- get minCols() {
509
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
510
- return this.T.minRows;
511
- }
512
- get maxCols() {
513
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
514
- return this.T.maxRows;
515
- }
516
- static fromVector(v) {
517
- return new Matrix(v.length, 1, v);
518
- }
519
- get toArray() {
520
- let arr = [];
521
- for (let i = 0; i < this.rows; i++) {
522
- for (let j = 0; j < this.cols; j++) {
523
- arr.push(this.arr[i][j]);
524
- }
525
- }
526
- return arr;
527
- }
528
- get print() {
529
- //"pretty print" the matrix
530
- let fstring = "[";
531
- for (let i = 0; i < this.arr.length; i++) {
532
- fstring += (i != 0 ? " " : "") + ` [${this.arr[i].map((n) => " " + n.toString() + " ")}],\n`;
533
- }
534
- console.log(fstring.substring(0, fstring.length - 2) + " ]");
535
- document.write(fstring.substring(0, fstring.length - 2) + " ]");
536
- }
537
- get table() {
538
- console.table(this.arr);
539
- }
540
- get serialize() {
541
- return JSON.stringify(this);
542
- }
543
- static deserialize(data) {
544
- if (typeof data == "string") {
545
- data = JSON.parse(data);
546
- }
547
- let matrix = new Matrix(data.rows, data.cols);
548
- matrix.arr = data.arr;
549
- return matrix;
550
- }
551
-
552
- toTable() {
553
- var table = new DocumentFragment();
554
- var Tr = new Array(this.rows).fill(null).map(() => document?.createElement("tr"));
555
- var Td = this.arr.map((n) => n.map(() => document?.createElement("td")));
556
- for (let i = 0; i < Td.length; i++) {
557
- for (let j = 0; j < Td[0].length; j++) {
558
- Td[i][j].innerHTML = this.arr[i][j];
559
- Tr[i].appendChild(Td[i][j]);
560
- }
561
- }
562
- Tr.map((n) => table.appendChild(n));
563
- return table;
564
- }
565
- toGrid(element, style = {}) {
566
- let a = Grid();
567
- a.append(
568
- ...this.map(element)
569
- .arr.flat(1)
570
- .map((n) => n.style(style))
571
- );
572
- a.Columns(this.cols);
573
- return a;
574
- }
575
- sortTable(n=0,{type="num",order="asc"}={}) {
576
- var obj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
577
- var newObj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
578
- if(type==="num"){
579
- if(order==="asc")obj[n].sort((a,b)=>a.x-b.x);
580
- else if(order==="desc")obj[n].sort((a,b)=>b.x-a.x);
581
- else if(order==="toggle"){
582
- // console.log(obj[n][0])
583
- //console.log(obj[n][1])
584
- if(obj[n][0].x>obj[n][1].x)obj[n].sort((a,b)=>b.x-a.x);
585
- else obj[n].sort((a,b)=>a.x-b.x);
586
- }
587
- }
588
- else if(type==="alpha"){
589
- if(order==="asc")obj[n].sort((a,b)=>(""+a.x).localeCompare(""+b.x));
590
- else if(order==="desc")obj[n].sort((a,b)=>(""+b.x).localeCompare(""+a.x));
591
- }
592
- //var order=obj[n].map(n=>n.y);
593
- order=obj[n].map(n=>n.y);
594
- for(let i=0;i<obj.length;i++){
595
- if(i!==n)obj[i].map((n,j)=>n.y=order[j]);
596
- }
597
- for(let i=0;i<obj.length;i++){
598
- if(i!==n)newObj[i].map((n,j)=>n.x=obj[i][order[j]].x)
599
- }
600
- newObj[n]=obj[n];
601
- var newArr=newObj.map(n=>n.map(m=>m.x));
602
- return new Matrix(newArr).T;
603
- }
604
- }
605
-
606
- function InverseMatrixe(M) {
607
- if (M.length !== M[0].length) {
608
- return;
609
- }
610
- var i = 0,
611
- ii = 0,
612
- j = 0,
613
- dim = M.length,
614
- e = 0;
615
- //t = 0;
616
- var I = [],
617
- C = [];
618
- for (i = 0; i < dim; i += 1) {
619
- I[I.length] = [];
620
- C[C.length] = [];
621
- for (j = 0; j < dim; j += 1) {
622
- if (i == j) {
623
- I[i][j] = 1;
624
- } else {
625
- I[i][j] = 0;
626
- }
627
- C[i][j] = M[i][j];
628
- }
629
- }
630
- for (i = 0; i < dim; i += 1) {
631
- e = C[i][i];
632
- if (e == 0) {
633
- for (ii = i + 1; ii < dim; ii += 1) {
634
- if (C[ii][i] != 0) {
635
- for (j = 0; j < dim; j++) {
636
- e = C[i][j];
637
- C[i][j] = C[ii][j];
638
- C[ii][j] = e;
639
- e = I[i][j];
640
- I[i][j] = I[ii][j];
641
- I[ii][j] = e;
642
- }
643
- break;
644
- }
645
- }
646
- e = C[i][i];
647
- if (e == 0) {
648
- return;
649
- }
650
- }
651
- for (j = 0; j < dim; j++) {
652
- C[i][j] = C[i][j] / e;
653
- I[i][j] = I[i][j] / e;
654
- }
655
- for (ii = 0; ii < dim; ii++) {
656
- if (ii == i) {
657
- continue;
658
- }
659
- e = C[ii][i];
660
- for (j = 0; j < dim; j++) {
661
- C[ii][j] -= e * C[i][j];
662
- I[ii][j] -= e * I[i][j];
663
- }
664
- }
665
- }
666
- return I;
667
- }
668
- /**
669
- * @returns {Matrix}
670
- */
671
- const matrix=(r, c, element)=>new Matrix(r, c, element);
672
- const matrix2=(...element)=>new Matrix(2, 2, element);
673
- const matrix3=(...element)=>new Matrix(3, 3, element);
674
- const matrix4=(...element)=>new Matrix(4, 4, element);
675
- export{Matrix,matrix,matrix2,matrix3,matrix4}
File without changes
File without changes