ziko 0.55.0 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ziko",
3
- "version": "0.55.0",
3
+ "version": "0.56.0",
4
4
  "description": "A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...",
5
5
  "keywords": [
6
6
  "front-end",
@@ -0,0 +1,30 @@
1
+ export const matrix_constructor = (rows, cols, element) => {
2
+ if (rows.isMatrix?.()) {
3
+ arr = rows.arr;
4
+ rows = rows.rows;
5
+ cols = rows.cols;
6
+ }
7
+ else {
8
+ let arr = [], i, j;
9
+ if (rows instanceof Array) {
10
+ arr = rows;
11
+ rows = arr.length;
12
+ cols = arr[0].length;
13
+ }
14
+ else {
15
+ for (i = 0; i < rows; i++) {
16
+ arr.push([]);
17
+ arr[i].push(new Array(cols));
18
+ for (j = 0; j < cols; j++) {
19
+ arr[i][j] = element[i * cols + j];
20
+ if (element[i * cols + j] == undefined) arr[i][j] = 0;
21
+ }
22
+ }
23
+ }
24
+ return [
25
+ rows,
26
+ cols,
27
+ arr
28
+ ]
29
+ }
30
+ };
@@ -1,3 +1,4 @@
1
+ export * from './constructor.js'
1
2
  export * from './inverse.js'
2
3
  export * from './det.js'
3
4
  export * from './stack.js'
@@ -14,6 +14,7 @@ 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,
@@ -23,32 +24,12 @@ import { mapfun } from '../functions/index.js';
23
24
  import { Random } from '../random/index.js';
24
25
  class Matrix{
25
26
  constructor(rows, cols, element = [] ) {
26
- if(rows instanceof Matrix){
27
- this.arr=rows.arr;
28
- this.rows=rows.rows;
29
- this.cols=rows.cols;
30
- }
31
- else {
32
- let arr = [], i, j;
33
- if (arguments[0] instanceof Array) {
34
- rows = arguments[0].length;
35
- cols = arguments[0][0].length;
36
- arr = arguments[0];
37
- } else {
38
- for (i = 0; i < rows; i++) {
39
- arr.push([]);
40
- arr[i].push(new Array(cols));
41
- for (j = 0; j < cols; j++) {
42
- arr[i][j] = element[i * cols + j];
43
- if (element[i * cols + j] == undefined) arr[i][j] = 0;
44
- }
45
- }
46
- }
47
- this.rows = rows;
48
- this.cols = cols;
49
- this.arr = arr;
50
- }
51
- this.#maintain();
27
+ [
28
+ this.rows,
29
+ this.cols,
30
+ this.arr
31
+ ] = matrix_constructor(rows, cols, element);
32
+ this.#maintain();
52
33
  }
53
34
  isMatrix(){
54
35
  return true
@@ -87,11 +68,11 @@ class Matrix{
87
68
  return arr2str(this.arr,false);
88
69
  }
89
70
  at(i = 0, j = undefined) {
90
- if (i < 0) i += this.rows;
91
- if (i < 0 || i >= this.rows) throw new Error('Row index out of bounds');
92
- if (j === undefined) return this.arr[i];
93
- if (j < 0) j += this.cols;
94
- 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');
95
76
  return this.arr[i][j];
96
77
  }
97
78
  slice(r0=0, c0=0, r1 = this.rows-1, c1 = this.cols-1) {
@@ -202,32 +183,135 @@ class Matrix{
202
183
  static vqueue(matrix,...matrices) {
203
184
  return matrix.clone().vqueue(...matrices);
204
185
  }
205
- shuffle(){
206
- const arr = this.arr.flat(1).sort(() => 0.5-Math.random())
186
+
187
+ apply(fn){
188
+ const arr = this.arr.flat(1).map(fn)
207
189
  return new Matrix(
208
190
  this.rows,
209
191
  this.cols,
210
192
  arr
211
193
  )
212
194
  }
195
+ static apply(M, fn){
196
+ return M.clone().apply(fn)
197
+ }
198
+ applyRows(fn = ()=>{}){
199
+ this.arr = this.arr.map(fn)
200
+ return this;
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
+ }
213
225
  static shuffle(M){
214
226
  return M.clone().shuffle()
215
227
  }
216
- shuffleRows(){
217
- this.arr = this.arr.sort(() => 0.5-Math.random());
228
+ sortRows(fn = ()=>{}){
229
+ this.arr = this.arr.map(row => row.sort(fn))
218
230
  return this;
219
231
  }
232
+ static sortRows(M, fn){
233
+ return M.clone().sortRows(fn)
234
+ }
235
+ shuffleRows(){
236
+ return this.sortRows(() => 0.5-Math.random())
237
+ }
220
238
  static shuffleRows(M){
221
239
  return M.clone().shuffleRows()
222
240
  }
223
-
241
+ sortCols(fn){
242
+ return this.clone().T.sortRows(fn).T;
243
+ }
244
+ static sortCols(M, fn){
245
+ return M.clone().sortCols(fn);
246
+ }
224
247
  shuffleCols(){
225
- this.arr = this.clone().T.arr.sort(() => 0.5-Math.random());
226
- return this.T
248
+ return this.sortCols(() => 0.5-Math.random())
227
249
  }
228
250
  static shuffleCols(M){
229
251
  return M.clone().shuffleCols()
230
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
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
+ }
231
315
  // Checkers
232
316
  get isSquare() {
233
317
  return this.rows === this.cols;
@@ -311,43 +395,7 @@ class Matrix{
311
395
  }
312
396
  return true;
313
397
  }
314
-
315
- // static get rand(){
316
- // return {
317
- // int:(rows, cols, a, b)=>{
318
- // let result = new Matrix(rows, cols);
319
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randInt(a, b);
320
- // return result;
321
- // },
322
- // bin:(rows,cols)=>{
323
- // let result = new Matrix(rows, cols);
324
- // for (let i = 0; i < rows; i++) {
325
- // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randBin;
326
- // }
327
- // return result;
328
- // },
329
- // hex:(rows,cols)=>{
330
- // let result = new Matrix(rows, cols);
331
- // for (let i = 0; i < rows; i++) {
332
- // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randHex;
333
- // }
334
- // return result;
335
- // },
336
- // choices:(rows, cols, choices, p)=>{
337
- // let result = new Matrix(rows, cols);
338
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.choice(choices, p);
339
- // return result
340
- // },
341
- // permutation:(rows,cols,arr)=>{
342
- // //return new Matrix(rows, cols, Random.permutation(...arr))
343
- // }
344
- // }
345
- // }
346
- // static rands(rows, cols, a = 1, b) {
347
- // let result = new Matrix(rows, cols);
348
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.rand(a, b);
349
- // return result;
350
- // }
398
+
351
399
  map(Imin, Imax, Fmin, Fmax) {
352
400
  this.arr = map(this.arr, Imin, Imax, Fmin, Fmax)
353
401
  return this;
@@ -382,46 +430,23 @@ class Matrix{
382
430
  this.arr[i][j] = +this.arr[i][j].toPrecision(p);
383
431
  return this;
384
432
  }
385
- max2min() {
386
- let newArr = this.arr.flat(1).max2min;
387
- return new Matrix(this.rows, this.cols, newArr);
388
- }
389
- min2max() {
390
- let newArr = this.arr.flat(1).min2max;
391
- return new Matrix(this.rows, this.cols, newArr);
392
- }
393
- sortRows(calback=undefined){
394
- let newArr=this.arr.map(n=>n.sort(calback)).flat(1);
395
- return new Matrix(this.rows, this.cols, newArr);
396
- }
397
- sortCols(calback=undefined){
398
- let m=this.T;
399
- let newArr=m.arr.map(n=>n.sort(calback)).flat(1);
400
- return new Matrix(this.rows, this.cols, newArr).T;
401
- }
402
- filterByRows(item){
403
- var truth=this.arr.map(n=>n.map(m=>+(""+m).includes(item)))
404
- var mask=truth.map(n=>!!Logic.or(...n))
405
- var filtredArray=this.arr.filter((n,i)=>mask[i]===true)
406
- if(filtredArray.length===0)filtredArray.push([])
407
- console.log(filtredArray)
408
- return new Matrix(filtredArray)
409
- }
410
- filterByCols(item){
411
- return new Matrix(this.T.arr.filter(n=>n.includes(item)))
412
- }
413
- sortAll(calback=undefined){
414
- let newArr=this.arr.flat(1).sort(calback);
415
- return new Matrix(this.rows, this.cols, newArr);
416
- }
417
- count(n) {
418
- return this.arr.flat(1).count(n);
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;
419
438
  }
420
- // toBase(n) {
421
- // let newArr = this.arr.flat(1).toBase(n);
439
+ // max2min() {
440
+ // let newArr = this.arr.flat(1).max2min;
422
441
  // return new Matrix(this.rows, this.cols, newArr);
423
442
  // }
424
-
443
+ // min2max() {
444
+ // let newArr = this.arr.flat(1).min2max;
445
+ // return new Matrix(this.rows, this.cols, newArr);
446
+ // }
447
+ // count(n) {
448
+ // return this.arr.flat(1).count(n);
449
+ // }
425
450
  splice(r0,c0,deleteCount,...items){
426
451
 
427
452
  }
@@ -437,54 +462,41 @@ class Matrix{
437
462
  static getCols(m, ci, cf = ci + 1) {
438
463
  return m.slice(0, ci, m.rows, cf);
439
464
  }
440
- add(...matr) {
465
+ #arithmetic(fn, ...matr){
441
466
  for (let k = 0; k < matr.length; k++) {
442
467
  if (typeof matr[k] == "number"||matr[k] instanceof Complex) matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
443
- 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]);
444
471
  }
445
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
472
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
446
473
  }
447
- sub(...matr) {
448
- for (let k = 0; k < matr.length; k++) {
449
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
450
- 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]);
451
- }
452
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
474
+ add(...matr) {
475
+ return this.#arithmetic(add, ...matr)
453
476
  }
454
477
  static add(m1, ...m2) {
455
478
  return m1.clone().add(...m2);
456
479
  }
480
+ sub(...matr) {
481
+ return this.#arithmetic(sub, ...matr)
482
+ }
457
483
  static sub(m1, ...m2) {
458
484
  return m1.clone().sub(...m2);
459
485
  }
460
486
  mul(...matr) {
461
- for (let k = 0; k < matr.length; k++) {
462
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
463
- 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]);
464
- }
465
- 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);
466
491
  }
467
492
  div(...matr) {
468
- for (let k = 0; k < matr.length; k++) {
469
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
470
- 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]);
471
- }
472
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
493
+ return this.#arithmetic(div, ...matr)
473
494
  }
474
495
  static div(m1, ...m2) {
475
496
  return m1.clone().div(...m2);
476
497
  }
477
- static mul(m1, ...m2) {
478
- return m1.clone().mul(...m2);
479
- }
480
498
  modulo(...matr) {
481
- for (let k = 0; k < matr.length; k++) {
482
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
483
- for (let i = 0; i < this.rows; i++)
484
- for (var j = 0; j < this.cols; j++)
485
- this.arr[i][j]=modulo(this.arr[i][j],matr[k].arr[i][j]);
486
- }
487
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
499
+ return this.#arithmetic(modulo, ...matr)
488
500
  }
489
501
  static modulo(m1, ...m2) {
490
502
  return m1.clone().modulo(...m2);
@@ -530,25 +542,29 @@ class Matrix{
530
542
  get min() {
531
543
  if (this.hasComplex()) console.error("Complex numbers are not comparable");
532
544
  let minRow = [];
533
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
534
- 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);
535
548
  }
536
549
  get max() {
537
550
  if (this.hasComplex()) console.error("Complex numbers are not comparable");
538
551
  let maxRow = [];
539
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
540
- 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);
541
555
  }
542
556
  get minRows() {
543
557
  if (this.hasComplex()) console.error("Complex numbers are not comparable");
544
558
  let minRow = [];
545
- 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]));
546
561
  return minRow;
547
562
  }
548
563
  get maxRows() {
549
564
  if (this.hasComplex()) console.error("Complex numbers are not comparable");
550
565
  let maxRow = [];
551
- 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]));
552
568
  return maxRow;
553
569
  }
554
570
  get minCols() {
@@ -612,11 +628,14 @@ class Matrix{
612
628
  }
613
629
 
614
630
 
615
- /**
616
- * @returns {Matrix}
617
- */
618
631
  const matrix=(r, c, element)=>new Matrix(r, c, element);
619
632
  const matrix2=(...element)=>new Matrix(2, 2, element);
620
633
  const matrix3=(...element)=>new Matrix(3, 3, element);
621
634
  const matrix4=(...element)=>new Matrix(4, 4, element);
622
- export{Matrix,matrix,matrix2,matrix3,matrix4}
635
+ export{
636
+ Matrix,
637
+ matrix,
638
+ matrix2,
639
+ matrix3,
640
+ matrix4
641
+ }