ziko 0.54.5 → 0.56.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.
@@ -14,40 +14,22 @@ import {
14
14
  import { Complex } from "../complex/index.js";
15
15
  import { arr2str } from "../../data/index.js";
16
16
  import {
17
+ matrix_constructor,
17
18
  matrix_inverse,
18
19
  matrix_det,
19
20
  hstack,
20
21
  vstack
21
22
  } from "./helpers/index.js";
22
23
  import { mapfun } from '../functions/index.js';
24
+ import { Random } from '../random/index.js';
23
25
  class Matrix{
24
26
  constructor(rows, cols, element = [] ) {
25
- if(rows instanceof Matrix){
26
- this.arr=rows.arr;
27
- this.rows=rows.rows;
28
- this.cols=rows.cols;
29
- }
30
- else {
31
- let arr = [], i, j;
32
- if (arguments[0] instanceof Array) {
33
- rows = arguments[0].length;
34
- cols = arguments[0][0].length;
35
- arr = arguments[0];
36
- } else {
37
- for (i = 0; i < rows; i++) {
38
- arr.push([]);
39
- arr[i].push(new Array(cols));
40
- for (j = 0; j < cols; j++) {
41
- arr[i][j] = element[i * cols + j];
42
- if (element[i * cols + j] == undefined) arr[i][j] = 0;
43
- }
44
- }
45
- }
46
- this.rows = rows;
47
- this.cols = cols;
48
- this.arr = arr;
49
- }
50
- this.#maintain();
27
+ [
28
+ this.rows,
29
+ this.cols,
30
+ this.arr
31
+ ] = matrix_constructor(rows, cols, element);
32
+ this.#maintain();
51
33
  }
52
34
  isMatrix(){
53
35
  return true
@@ -86,20 +68,23 @@ class Matrix{
86
68
  return arr2str(this.arr,false);
87
69
  }
88
70
  at(i = 0, j = undefined) {
89
- if (i < 0) i += this.rows;
90
- if (i < 0 || i >= this.rows) throw new Error('Row index out of bounds');
91
- if (j === undefined) return this.arr[i];
92
- if (j < 0) j += this.cols;
93
- if (j < 0 || j >= this.cols) throw new Error('Column index out of bounds');
71
+ if(i < 0) i += this.rows;
72
+ if(i < 0 || i >= this.rows) throw new Error('Row index out of bounds');
73
+ if(j === undefined) return this.arr[i];
74
+ if(j < 0) j += this.cols;
75
+ if(j < 0 || j >= this.cols) throw new Error('Column index out of bounds');
94
76
  return this.arr[i][j];
95
77
  }
96
- slice(r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
78
+ slice(r0=0, c0=0, r1 = this.rows-1, c1 = this.cols-1) {
79
+ if(r1 < 0) r1 = this.rows + r1
80
+ if(c1 < 0 ) c1 = this.cols + c1
97
81
  let newRow = r1 - r0,
98
82
  newCol = c1 - c0;
99
83
  let newArr = new Array(newCol);
100
84
  for (let i = 0; i < newRow; i++) {
101
85
  newArr[i] = [];
102
- for (let j = 0; j < newCol; j++) newArr[i][j] = this.arr[i + r0][j + c0];
86
+ for (let j = 0; j < newCol; j++)
87
+ newArr[i][j] = this.arr[i + r0][j + c0];
103
88
  }
104
89
  return new Matrix(newRow, newCol, newArr.flat(1));
105
90
  }
@@ -125,6 +110,7 @@ class Matrix{
125
110
  get inv() {
126
111
  return matrix_inverse(this)
127
112
  }
113
+ // normalize names
128
114
  static eye(size) {
129
115
  let result = new Matrix(size, size);
130
116
  for (let i = 0; i < size; i++)
@@ -149,6 +135,20 @@ class Matrix{
149
135
  for (let j = 0; j < cols; j++) result.arr[i][j] = number;
150
136
  return result;
151
137
  }
138
+ static get random(){
139
+ return {
140
+ int : (r, c, a, b)=> new Matrix(
141
+ r,
142
+ c,
143
+ Random.sample.int(r*c, a, b)
144
+ ),
145
+ float : (r, c, a,)=> new Matrix(
146
+ r,
147
+ c,
148
+ Random.sample.float(r*c, a, b)
149
+ ),
150
+ }
151
+ }
152
152
  hstack(...matrices) {
153
153
  const M=[this, ...matrices].reduce((a,b)=>hstack(a, b));
154
154
  Object.assign(this, M);
@@ -183,20 +183,135 @@ class Matrix{
183
183
  static vqueue(matrix,...matrices) {
184
184
  return matrix.clone().vqueue(...matrices);
185
185
  }
186
- shuffle(){
187
- this.arr = this.arr.sort(()=>0.5-Math.random());
186
+
187
+ apply(fn){
188
+ const arr = this.arr.flat(1).map(fn)
189
+ return new Matrix(
190
+ this.rows,
191
+ this.cols,
192
+ arr
193
+ )
194
+ }
195
+ static apply(M, fn){
196
+ return M.clone().apply(fn)
197
+ }
198
+ applyRows(fn = ()=>{}){
199
+ this.arr = this.arr.map(fn)
188
200
  return this;
189
201
  }
202
+ static applyRows(M, fn){
203
+ return M.clone().applyRows(fn)
204
+ }
205
+ applyCols(fn){
206
+ return this.clone().T.applyRows(fn).T;
207
+ }
208
+ static applyCols(M, fn){
209
+ return M.clone().applyCols(fn);
210
+ }
211
+ sort(fn = ()=>{}){
212
+ const arr = this.arr.flat(1).sort(fn)
213
+ return new Matrix(
214
+ this.rows,
215
+ this.cols,
216
+ arr
217
+ )
218
+ }
219
+ static sort(M, fn){
220
+ return M.clone().sort(fn)
221
+ }
222
+ shuffle(){
223
+ return this.sort(() => 0.5-Math.random())
224
+ }
190
225
  static shuffle(M){
191
226
  return M.clone().shuffle()
192
227
  }
193
- // get reel() {
194
- // return new Matrix(this.cols, this.rows, this.arr.flat(1).reel);
195
- // }
196
- // get imag() {
197
- // return new Matrix(this.cols, this.rows, this.arr.flat(1).imag);
198
- // }
228
+ sortRows(fn = ()=>{}){
229
+ this.arr = this.arr.map(row => row.sort(fn))
230
+ return this;
231
+ }
232
+ static sortRows(M, fn){
233
+ return M.clone().sortRows(fn)
234
+ }
235
+ shuffleRows(){
236
+ return this.sortRows(() => 0.5-Math.random())
237
+ }
238
+ static shuffleRows(M){
239
+ return M.clone().shuffleRows()
240
+ }
241
+ sortCols(fn){
242
+ return this.clone().T.sortRows(fn).T;
243
+ }
244
+ static sortCols(M, fn){
245
+ return M.clone().sortCols(fn);
246
+ }
247
+ shuffleCols(){
248
+ return this.sortCols(() => 0.5-Math.random())
249
+ }
250
+ static shuffleCols(M){
251
+ return M.clone().shuffleCols()
252
+ }
253
+
254
+ reduce(fn, initialValue){
255
+ const value = initialValue
256
+ ? this.arr.flat(1).reduce(fn, initialValue)
257
+ : this.arr.flat(1).reduce(fn);
258
+ return new Matrix([[value]])
259
+ }
260
+ static reduce(M, fn, initialValue){
261
+ return M.clone().reduce(fn, initialValue)
262
+ }
263
+ reduceRows(fn, initialValue){
264
+ const values = initialValue
265
+ ? this.arr.map(row => row.reduce(fn, initialValue))
266
+ : this.arr.map(row => row.reduce(fn))
267
+ return new Matrix(1, this.cols, values)
268
+ }
269
+ static reduceRows(M, fn, initialValue){
270
+ return M.clone().reduceRows(fn, initialValue)
271
+ }
272
+ reduceCols(fn, initialValue){
273
+ return this.T.reduceRows(fn, initialValue).T
199
274
 
275
+ }
276
+ static reduceCols(M, fn, initialValue){
277
+ return M.clone().reduceCols(fn, initialValue)
278
+ }
279
+ filterRows(fn){
280
+ const mask = this.arr.map(n => n.some(m => fn(m)));
281
+ const arr = [];
282
+ let i;
283
+ for(i = 0; i < mask.length; i++)
284
+ if(mask[i]) arr.push(this.arr[i])
285
+ return new Matrix(arr)
286
+ }
287
+ static filterCols(M, fn){
288
+ return M.clone().filterRows(fn)
289
+ }
290
+ filterCols(fn){
291
+ const arr = this.T.filterRows(fn);
292
+ return new Matrix(arr).T
293
+ }
294
+ static filterCols(M, fn){
295
+ return M.clone().filterCols(fn);
296
+ }
297
+ every(fn){
298
+ return this.arr.flat(1).every(fn)
299
+ }
300
+ everyRow(fn){
301
+ return this.arr.map(n => n.every(fn))
302
+ }
303
+ everyCol(fn){
304
+ return this.T.arr.map(n => n.every(fn))
305
+ }
306
+ some(fn){
307
+ return this.arr.flat(1).some(fn)
308
+ }
309
+ someRow(fn){
310
+ return this.arr.map(n => n.some(fn))
311
+ }
312
+ someCol(fn){
313
+ return this.T.arr.map(n => n.some(fn))
314
+ }
200
315
  // Checkers
201
316
  get isSquare() {
202
317
  return this.rows === this.cols;
@@ -280,43 +395,7 @@ class Matrix{
280
395
  }
281
396
  return true;
282
397
  }
283
-
284
- // static get rand(){
285
- // return {
286
- // int:(rows, cols, a, b)=>{
287
- // let result = new Matrix(rows, cols);
288
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randInt(a, b);
289
- // return result;
290
- // },
291
- // bin:(rows,cols)=>{
292
- // let result = new Matrix(rows, cols);
293
- // for (let i = 0; i < rows; i++) {
294
- // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randBin;
295
- // }
296
- // return result;
297
- // },
298
- // hex:(rows,cols)=>{
299
- // let result = new Matrix(rows, cols);
300
- // for (let i = 0; i < rows; i++) {
301
- // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randHex;
302
- // }
303
- // return result;
304
- // },
305
- // choices:(rows, cols, choices, p)=>{
306
- // let result = new Matrix(rows, cols);
307
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.choice(choices, p);
308
- // return result
309
- // },
310
- // permutation:(rows,cols,arr)=>{
311
- // //return new Matrix(rows, cols, Random.permutation(...arr))
312
- // }
313
- // }
314
- // }
315
- // static rands(rows, cols, a = 1, b) {
316
- // let result = new Matrix(rows, cols);
317
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.rand(a, b);
318
- // return result;
319
- // }
398
+
320
399
  map(Imin, Imax, Fmin, Fmax) {
321
400
  this.arr = map(this.arr, Imin, Imax, Fmin, Fmax)
322
401
  return this;
@@ -351,62 +430,23 @@ class Matrix{
351
430
  this.arr[i][j] = +this.arr[i][j].toPrecision(p);
352
431
  return this;
353
432
  }
354
- // get toBin() {
355
- // let newArr = this.arr.flat(1).toBin;
356
- // return new Matrix(this.rows, this.cols, newArr);
357
- // }
358
- // get toOct() {
359
- // let newArr = this.arr.flat(1).toOct;
433
+ toFixed(p) {
434
+ for (let i = 0; i < this.cols; i++)
435
+ for (let j = 0; j < this.rows; j++)
436
+ this.arr[i][j] = +this.arr[i][j].toFixed(p);
437
+ return this;
438
+ }
439
+ // max2min() {
440
+ // let newArr = this.arr.flat(1).max2min;
360
441
  // return new Matrix(this.rows, this.cols, newArr);
361
442
  // }
362
- // get toHex() {
363
- // let newArr = this.arr.flat(1).toHex;
443
+ // min2max() {
444
+ // let newArr = this.arr.flat(1).min2max;
364
445
  // return new Matrix(this.rows, this.cols, newArr);
365
446
  // }
366
- /*get isOdd() {
367
- let newArr = this.arr.flat(1).isOdd;
368
- return new Matrix(this.rows, this.cols, newArr);
369
- }*/
370
- max2min() {
371
- let newArr = this.arr.flat(1).max2min;
372
- return new Matrix(this.rows, this.cols, newArr);
373
- }
374
- min2max() {
375
- let newArr = this.arr.flat(1).min2max;
376
- return new Matrix(this.rows, this.cols, newArr);
377
- }
378
- sortRows(calback=undefined){
379
- let newArr=this.arr.map(n=>n.sort(calback)).flat(1);
380
- return new Matrix(this.rows, this.cols, newArr);
381
- }
382
- sortCols(calback=undefined){
383
- let m=this.T;
384
- let newArr=m.arr.map(n=>n.sort(calback)).flat(1);
385
- return new Matrix(this.rows, this.cols, newArr).T;
386
- }
387
- filterByRows(item){
388
- var truth=this.arr.map(n=>n.map(m=>+(""+m).includes(item)))
389
- var mask=truth.map(n=>!!Logic.or(...n))
390
- var filtredArray=this.arr.filter((n,i)=>mask[i]===true)
391
- if(filtredArray.length===0)filtredArray.push([])
392
- console.log(filtredArray)
393
- return new Matrix(filtredArray)
394
- }
395
- filterByCols(item){
396
- return new Matrix(this.T.arr.filter(n=>n.includes(item)))
397
- }
398
- sortAll(calback=undefined){
399
- let newArr=this.arr.flat(1).sort(calback);
400
- return new Matrix(this.rows, this.cols, newArr);
401
- }
402
- count(n) {
403
- return this.arr.flat(1).count(n);
404
- }
405
- // toBase(n) {
406
- // let newArr = this.arr.flat(1).toBase(n);
407
- // return new Matrix(this.rows, this.cols, newArr);
447
+ // count(n) {
448
+ // return this.arr.flat(1).count(n);
408
449
  // }
409
-
410
450
  splice(r0,c0,deleteCount,...items){
411
451
 
412
452
  }
@@ -422,54 +462,41 @@ class Matrix{
422
462
  static getCols(m, ci, cf = ci + 1) {
423
463
  return m.slice(0, ci, m.rows, cf);
424
464
  }
425
- add(...matr) {
465
+ #arithmetic(fn, ...matr){
426
466
  for (let k = 0; k < matr.length; k++) {
427
467
  if (typeof matr[k] == "number"||matr[k] instanceof Complex) 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] = add(this.arr[i][j],matr[k].arr[i][j]);
468
+ for (let i = 0; i < this.rows; i++)
469
+ for (var j = 0; j < this.cols; j++)
470
+ this.arr[i][j] = fn(this.arr[i][j], matr[k].arr[i][j]);
429
471
  }
430
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
472
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
431
473
  }
432
- sub(...matr) {
433
- for (let k = 0; k < matr.length; k++) {
434
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
435
- for (let i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++) this.arr[i][j] = sub(this.arr[i][j],matr[k].arr[i][j]);
436
- }
437
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
474
+ add(...matr) {
475
+ return this.#arithmetic(add, ...matr)
438
476
  }
439
477
  static add(m1, ...m2) {
440
478
  return m1.clone().add(...m2);
441
479
  }
480
+ sub(...matr) {
481
+ return this.#arithmetic(sub, ...matr)
482
+ }
442
483
  static sub(m1, ...m2) {
443
484
  return m1.clone().sub(...m2);
444
485
  }
445
486
  mul(...matr) {
446
- for (let k = 0; k < matr.length; k++) {
447
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
448
- for (var i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++) this.arr[i][j] = mul(this.arr[i][j],matr[k].arr[i][j]);
449
- }
450
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
487
+ return this.#arithmetic(mul, ...matr)
488
+ }
489
+ static mul(m1, ...m2) {
490
+ return m1.clone().mul(...m2);
451
491
  }
452
492
  div(...matr) {
453
- for (let k = 0; k < matr.length; k++) {
454
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
455
- for (let i = 0; i < this.rows; i++) for (var j = 0; j < this.cols; j++) this.arr[i][j] = div(this.arr[i][j],matr[k].arr[i][j]);
456
- }
457
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
493
+ return this.#arithmetic(div, ...matr)
458
494
  }
459
495
  static div(m1, ...m2) {
460
496
  return m1.clone().div(...m2);
461
497
  }
462
- static mul(m1, ...m2) {
463
- return m1.clone().mul(...m2);
464
- }
465
498
  modulo(...matr) {
466
- for (let k = 0; k < matr.length; k++) {
467
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
468
- for (let i = 0; i < this.rows; i++)
469
- for (var j = 0; j < this.cols; j++)
470
- this.arr[i][j]=modulo(this.arr[i][j],matr[k].arr[i][j]);
471
- }
472
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
499
+ return this.#arithmetic(modulo, ...matr)
473
500
  }
474
501
  static modulo(m1, ...m2) {
475
502
  return m1.clone().modulo(...m2);
@@ -504,42 +531,48 @@ class Matrix{
504
531
  }
505
532
  get somme() {
506
533
  let S = 0;
507
- for (let i = 0; i < this.rows; i++) for (let j = 0; j < this.cols; j++) S += this.arr[i][j];
534
+ for (let i = 0; i < this.rows; i++)
535
+ for (let j = 0; j < this.cols; j++)
536
+ S += this.arr[i][j];
508
537
  return S;
509
538
  }
510
- get hasComplex() {
539
+ hasComplex(){
511
540
  return this.arr.flat(Infinity).some((n) => n instanceof Complex);
512
541
  }
513
542
  get min() {
514
- if (this.hasComplex) console.error("Complex numbers are not comparable");
543
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
515
544
  let minRow = [];
516
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
517
- return min(...minRow);
545
+ for (let i = 0; i < this.rows; i++)
546
+ minRow.push(Math.min(...this.arr[i]));
547
+ return Math.min(...minRow);
518
548
  }
519
549
  get max() {
520
- if (this.hasComplex) console.error("Complex numbers are not comparable");
550
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
521
551
  let maxRow = [];
522
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
523
- return max(...maxRow);
552
+ for (let i = 0; i < this.rows; i++)
553
+ maxRow.push(Math.max(...this.arr[i]));
554
+ return Math.max(...maxRow);
524
555
  }
525
556
  get minRows() {
526
- if (this.hasComplex) console.error("Complex numbers are not comparable");
557
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
527
558
  let minRow = [];
528
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
559
+ for (let i = 0; i < this.rows; i++)
560
+ minRow.push(Math.min(...this.arr[i]));
529
561
  return minRow;
530
562
  }
531
563
  get maxRows() {
532
- if (this.hasComplex) console.error("Complex numbers are not comparable");
564
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
533
565
  let maxRow = [];
534
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
566
+ for (let i = 0; i < this.rows; i++)
567
+ maxRow.push(Math.max(...this.arr[i]));
535
568
  return maxRow;
536
569
  }
537
570
  get minCols() {
538
- if (this.hasComplex) console.error("Complex numbers are not comparable");
571
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
539
572
  return this.T.minRows;
540
573
  }
541
574
  get maxCols() {
542
- if (this.hasComplex) console.error("Complex numbers are not comparable");
575
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
543
576
  return this.T.maxRows;
544
577
  }
545
578
  static fromVector(v) {
@@ -595,11 +628,14 @@ class Matrix{
595
628
  }
596
629
 
597
630
 
598
- /**
599
- * @returns {Matrix}
600
- */
601
631
  const matrix=(r, c, element)=>new Matrix(r, c, element);
602
632
  const matrix2=(...element)=>new Matrix(2, 2, element);
603
633
  const matrix3=(...element)=>new Matrix(3, 3, element);
604
634
  const matrix4=(...element)=>new Matrix(4, 4, element);
605
- export{Matrix,matrix,matrix2,matrix3,matrix4}
635
+ export{
636
+ Matrix,
637
+ matrix,
638
+ matrix2,
639
+ matrix3,
640
+ matrix4
641
+ }