ziko 0.54.4 → 0.55.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.54.4",
3
+ "version": "0.55.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,12 @@
1
+ // import { powerSet , subSet } from "./Set/index.js";
2
+ // import { Base } from "./Conversion/index.js";
3
+ // import {
4
+ // Permutation,
5
+ // } from "./Permutation/index.js"
6
+ // import {
7
+ // Combinaison,
8
+ // combinaison,
9
+ // } from "./Combinaison/index.js"
10
+
11
+ // export{
12
+ // Base,Permutation,Combinaison,combinaison,powerSet,subSet}
@@ -1,5 +1,4 @@
1
- // import {sum,prod,deg2rad} from "../utils/index.js";
2
- // Should avoid CD
1
+ import { Random } from "../random/index.js";
3
2
  class Complex{
4
3
  constructor(a = 0, b = 0) {
5
4
  if(a instanceof Complex){
@@ -74,10 +73,23 @@ class Complex{
74
73
  get phi(){
75
74
  return Math.atan2(this.b , this.a);
76
75
  }
77
- static Zero() {
76
+ static zero() {
78
77
  return new Complex(0, 0);
79
78
  }
80
- static Twiddle(N, K){
79
+ static fromPolar(z, phi) {
80
+ return new Complex(
81
+ +(z * cos(phi)).toFixed(13),
82
+ +(z * sin(phi)).toFixed(13)
83
+ );
84
+ }
85
+
86
+ static get random(){
87
+ return {
88
+ int : (a, b)=> new Complex(...Random.sample.int(2, a, b) ),
89
+ float : (a, b)=> new Complex(...Random.sample.float(2, a, b) ),
90
+ }
91
+ }
92
+ static twiddle(N, K){
81
93
  const phi = -2 * Math.PI * K / N;
82
94
  return new Complex(
83
95
  Math.cos(phi),
@@ -150,12 +162,6 @@ class Complex{
150
162
  this.b = z * Math.sin(phi)
151
163
  return this;
152
164
  }
153
- static fromExpo(z, phi) {
154
- return new Complex(
155
- +(z * cos(phi)).toFixed(13),
156
- +(z * sin(phi)).toFixed(13)
157
- );
158
- }
159
165
  get expo() {
160
166
  return [this.z, this.phi];
161
167
  }
@@ -176,10 +182,10 @@ class Complex{
176
182
  return complex({z: this.z ** (1/n), phi: this.phi / n});
177
183
  }
178
184
  get sqrt(){
179
- return this.nrth(2);
185
+ return this.nthr(2);
180
186
  }
181
187
  get cbrt(){
182
- return this.nrth(3);
188
+ return this.nthr(3);
183
189
  }
184
190
  get log(){
185
191
  return complex(this.z, this.phi);
@@ -0,0 +1,61 @@
1
+ export const base2base = (value, fromBase, toBase) => {
2
+ if (fromBase < 2 || fromBase > 36 || toBase < 2 || toBase > 36)
3
+ throw new TypeError('Base must be between 2 and 36');
4
+
5
+ const dec = parseInt(value, fromBase);
6
+ if (Number.isNaN(dec)) throw new TypeError('Invalid value for the given base');
7
+
8
+ return dec.toString(toBase);
9
+ };
10
+
11
+ export const bin2oct = (...x) => mapfun(
12
+ n => base2base(n, 2, 8),
13
+ ...x
14
+ )
15
+ export const bin2dec = (...x) => mapfun(
16
+ n => base2base(n, 2, 10),
17
+ ...x
18
+ )
19
+ export const bin2hex = (...x) => mapfun(
20
+ n => base2base(n, 2, 16),
21
+ ...x
22
+ )
23
+
24
+ export const oct2bin = (...x) => mapfun(
25
+ n => base2base(n, 8, 2),
26
+ ...x
27
+ )
28
+ export const oct2dec = (...x) => mapfun(
29
+ n => base2base(n, 8, 10),
30
+ ...x
31
+ )
32
+ export const oct2hex = (...x) => mapfun(
33
+ n => base2base(n, 8, 16),
34
+ ...x
35
+ )
36
+
37
+ export const dec2bin = (...x) => mapfun(
38
+ n => base2base(n, 10, 2),
39
+ ...x
40
+ )
41
+ export const dec2oct = (...x) => mapfun(
42
+ n => base2base(n, 10, 8),
43
+ ...x
44
+ )
45
+ export const dec2hex = (...x) => mapfun(
46
+ n => base2base(n, 10, 16),
47
+ ...x
48
+ )
49
+
50
+ export const hex2bin = (...x) => mapfun(
51
+ n => base2base(n, 16, 2),
52
+ ...x
53
+ )
54
+ export const hex2oct = (...x) => mapfun(
55
+ n => base2base(n, 16, 8),
56
+ ...x
57
+ )
58
+ export const hex2dec = (...x) => mapfun(
59
+ n => base2base(n, 16, 10),
60
+ ...x
61
+ )
@@ -1,8 +1,9 @@
1
1
  export * from './mapfun/index.js'
2
- export * from './nested/index.js'
2
+ export * from './ufunc/index.js'
3
3
  export * from './arithmetic/index.js'
4
4
  export * from './utils/index.js'
5
5
  export * from './stats/index.js'
6
+ export * from './logic/index.js'
6
7
  // export const atan2=(x,y,rad=true)=>{
7
8
  // if(typeof x === "number"){
8
9
  // if(typeof y === "number")return rad?Math.atan2(x,y):Math.atan2(x,y)*180/Math.PI;
@@ -0,0 +1,54 @@
1
+ const not = x => {
2
+ if(x.isComplex?.()) return new x.constructor(not(x.a), not(x.b))
3
+ if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, x.arr.flat(1).map(not))
4
+ return + !x;
5
+ }
6
+ const handle_complex_and_matrix = (x, operation) => {
7
+ if (x.every(n => n.isComplex?.())) {
8
+ const Re = x.map(n => n.a);
9
+ const Im = x.map(n => n.b);
10
+ return new x[0].constructor(
11
+ operation(...Re),
12
+ operation(...Im)
13
+ );
14
+ }
15
+
16
+ if (x.every(n => n.isMatrix?.())) {
17
+ if (!x.every(mat => mat.rows === x[0].rows && mat.cols === x[0].cols)) {
18
+ return TypeError('All matrices must have the same shape');
19
+ }
20
+
21
+ const { rows, cols } = x[0];
22
+ const Y = Array.from({ length: rows }, (_, i) =>
23
+ Array.from({ length: cols }, (_, j) =>
24
+ operation(...x.map(mat => mat.arr[i][j]))
25
+ )
26
+ );
27
+ return new x[0].constructor(Y);
28
+ }
29
+
30
+ return null; // Return null if no Complex or Matrix found
31
+ };
32
+
33
+ export const and = (...x) => {
34
+ const result = handle_complex_and_matrix(x, and);
35
+ if (result !== null) return result;
36
+ return x.reduce((n, m) => (n &= m), 1);
37
+ };
38
+
39
+ export const or = (...x) => {
40
+ const result = handle_complex_and_matrix(x, or);
41
+ if (result !== null) return result;
42
+ return x.reduce((n, m) => (n |= m), 0);
43
+ };
44
+
45
+ export const xor = (...x) => {
46
+ const result = handle_complex_and_matrix(x, xor);
47
+ if (result !== null) return result;
48
+ return x.reduce((n, m) => (n ^= m), 0);
49
+ };
50
+
51
+ export const nand = (...x) => not(and(...x));
52
+ export const nor = (...x) => not(or(...x));
53
+ export const xnor = (...x) => not(xor(...x));
54
+
@@ -1,5 +1,5 @@
1
1
  import { mapfun } from "../mapfun/index.js";
2
- import { nthr, pow } from "../nested/index.js";
2
+ import { nthr, pow } from "../fundamentals/index.js";
3
3
 
4
4
  export const zeros = n => new Array(n).fill(0);
5
5
  export const ones = n => new Array(n).fill(1);
@@ -3,6 +3,18 @@ import {add, mul} from '../arithmetic/index.js';
3
3
  export const min = (...x) => Math.min(...x);
4
4
  export const max = (...x) => Math.max(...x);
5
5
 
6
+ export const binomial = (n, k) =>{
7
+ if(n !== Math.floor(n)) return TypeError('n must be an integer');
8
+ if(k !== Math.floor(k)) return TypeError('k must be an integer');
9
+ if (n < 0) return TypeError('n must be non-negative');
10
+ if (k < 0 || n < 0 || k > n) return 0;
11
+ if (k > n - k) k = n - k;
12
+ let c = 1, i;
13
+ for (i = 0; i < k; i++)
14
+ c = c * (n - i) / (i + 1);
15
+ return c;
16
+ }
17
+
6
18
  export const mean = (...x) => x.reduce((a, b) => a + b) / x.length;
7
19
 
8
20
  export const variance = (...x) => {
@@ -16,7 +28,7 @@ export const std = (...x) => Math.sqrt(variance(...x));
16
28
 
17
29
  export const accum_sum = (...x) => {
18
30
  let result = [];
19
- let total = 0, i; n = x.length;
31
+ let total = 0, i, n = x.length;
20
32
  for(i = 0; i < n ; i++){
21
33
  total = add(total, x[i])
22
34
  result.push(total);
@@ -26,7 +38,7 @@ export const accum_sum = (...x) => {
26
38
 
27
39
  export const accum_prod = (...x) => {
28
40
  let result = [];
29
- let prod = 1, i; n = x.length;
41
+ let prod = 1, i, n = x.length;
30
42
  for(i = 0; i < n ; i++){
31
43
  prod = mul(prod, x[i])
32
44
  result.push(prod);
@@ -20,64 +20,6 @@ export const map = (x, a, b, c, d) => apply_fun(
20
20
  v => lerp(norm(v, a, b), c, d)
21
21
  );
22
22
 
23
-
24
- // export const norm = (x, min, max) => {
25
- // if(x.isComplex?.()) return new x.constructor(
26
- // norm(x.a, min, max),
27
- // norm(x.b, min, max)
28
- // )
29
- // if(x.isMatrix?.()) return new x.constructor(
30
- // x.rows,
31
- // x.cols,
32
- // norm(x.arr.flat(1), min, max)
33
- // );
34
- // if(x instanceof Array) return mapfun(n => norm(n, min, max), ...x);
35
- // return min !== max ? (x - min) / (max - min) : 0;
36
- // }
37
-
38
-
39
- // export const lerp = (x, min, max) => {
40
- // if(x.isComplex?.()) return new x.constructor(
41
- // lerp(x.a, min, max),
42
- // lerp(x.b, min, max)
43
- // )
44
- // if(x.isMatrix?.()) return new x.constructor(
45
- // x.rows,
46
- // x.cols,
47
- // lerp(x.arr.flat(1), min, max)
48
- // );
49
- // if(x instanceof Array) return mapfun(n => lerp(n, min, max), ...x);
50
- // return (max - min) * x + min;
51
- // }
52
-
53
- // export const map = (x, a, b, c, d) => {
54
- // if(x.isComplex?.()) return new x.constructor(
55
- // map(x.a, a, b, c, d),
56
- // map(x.b, a, b, c, d)
57
- // )
58
- // if(x.isMatrix?.()) return new x.constructor(
59
- // x.rows,
60
- // x.cols,
61
- // map(x.arr.flat(1), a, b, c, d)
62
- // );
63
- // if(x instanceof Array) return mapfun(n => map(n, a, b, c, d), ...x);
64
- // return lerp(norm(x, a, b), c, d);
65
- // }
66
-
67
- // export const clamp = (x, min, max) => {
68
- // if(x.isComplex?.()) return new x.constructor(
69
- // clamp(x.a, min, max),
70
- // clamp(x.b, min, max)
71
- // )
72
- // if(x.isMatrix?.()) return new x.constructor(
73
- // x.rows,
74
- // x.cols,
75
- // clamp(x.arr.flat(1), min, max)
76
- // );
77
- // if(x instanceof Array) return mapfun(n => clamp(n, min, max), ...x);
78
- // return Math.min(Math.max(x, min), max)
79
- // }
80
-
81
23
  export const hypot = (...x) => {
82
24
  const c0 = x.find(a => a.isComplex?.());
83
25
  if (c0) {
package/src/math/index.js CHANGED
@@ -18,9 +18,9 @@ export * from "./const.js"
18
18
  export * from "./functions/index.js"
19
19
  export * from "./complex"
20
20
  export * from "./matrix"
21
- export * from "./discret"
21
+ // export * from "./discret"
22
22
  export * from "./random"
23
23
  export * from "./utils"
24
- export * from "./statistics"
24
+ // export * from "./statistics"
25
25
  // export default Math;
26
26
 
@@ -20,6 +20,7 @@ import {
20
20
  vstack
21
21
  } from "./helpers/index.js";
22
22
  import { mapfun } from '../functions/index.js';
23
+ import { Random } from '../random/index.js';
23
24
  class Matrix{
24
25
  constructor(rows, cols, element = [] ) {
25
26
  if(rows instanceof Matrix){
@@ -93,13 +94,16 @@ class Matrix{
93
94
  if (j < 0 || j >= this.cols) throw new Error('Column index out of bounds');
94
95
  return this.arr[i][j];
95
96
  }
96
- slice(r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
97
+ slice(r0=0, c0=0, r1 = this.rows-1, c1 = this.cols-1) {
98
+ if(r1 < 0) r1 = this.rows + r1
99
+ if(c1 < 0 ) c1 = this.cols + c1
97
100
  let newRow = r1 - r0,
98
101
  newCol = c1 - c0;
99
102
  let newArr = new Array(newCol);
100
103
  for (let i = 0; i < newRow; i++) {
101
104
  newArr[i] = [];
102
- for (let j = 0; j < newCol; j++) newArr[i][j] = this.arr[i + r0][j + c0];
105
+ for (let j = 0; j < newCol; j++)
106
+ newArr[i][j] = this.arr[i + r0][j + c0];
103
107
  }
104
108
  return new Matrix(newRow, newCol, newArr.flat(1));
105
109
  }
@@ -125,6 +129,7 @@ class Matrix{
125
129
  get inv() {
126
130
  return matrix_inverse(this)
127
131
  }
132
+ // normalize names
128
133
  static eye(size) {
129
134
  let result = new Matrix(size, size);
130
135
  for (let i = 0; i < size; i++)
@@ -149,6 +154,20 @@ class Matrix{
149
154
  for (let j = 0; j < cols; j++) result.arr[i][j] = number;
150
155
  return result;
151
156
  }
157
+ static get random(){
158
+ return {
159
+ int : (r, c, a, b)=> new Matrix(
160
+ r,
161
+ c,
162
+ Random.sample.int(r*c, a, b)
163
+ ),
164
+ float : (r, c, a,)=> new Matrix(
165
+ r,
166
+ c,
167
+ Random.sample.float(r*c, a, b)
168
+ ),
169
+ }
170
+ }
152
171
  hstack(...matrices) {
153
172
  const M=[this, ...matrices].reduce((a,b)=>hstack(a, b));
154
173
  Object.assign(this, M);
@@ -184,19 +203,31 @@ class Matrix{
184
203
  return matrix.clone().vqueue(...matrices);
185
204
  }
186
205
  shuffle(){
187
- this.arr = this.arr.sort(()=>0.5-Math.random());
188
- return this;
206
+ const arr = this.arr.flat(1).sort(() => 0.5-Math.random())
207
+ return new Matrix(
208
+ this.rows,
209
+ this.cols,
210
+ arr
211
+ )
189
212
  }
190
213
  static shuffle(M){
191
214
  return M.clone().shuffle()
192
215
  }
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
- // }
199
-
216
+ shuffleRows(){
217
+ this.arr = this.arr.sort(() => 0.5-Math.random());
218
+ return this;
219
+ }
220
+ static shuffleRows(M){
221
+ return M.clone().shuffleRows()
222
+ }
223
+
224
+ shuffleCols(){
225
+ this.arr = this.clone().T.arr.sort(() => 0.5-Math.random());
226
+ return this.T
227
+ }
228
+ static shuffleCols(M){
229
+ return M.clone().shuffleCols()
230
+ }
200
231
  // Checkers
201
232
  get isSquare() {
202
233
  return this.rows === this.cols;
@@ -351,22 +382,6 @@ class Matrix{
351
382
  this.arr[i][j] = +this.arr[i][j].toPrecision(p);
352
383
  return this;
353
384
  }
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;
360
- // return new Matrix(this.rows, this.cols, newArr);
361
- // }
362
- // get toHex() {
363
- // let newArr = this.arr.flat(1).toHex;
364
- // return new Matrix(this.rows, this.cols, newArr);
365
- // }
366
- /*get isOdd() {
367
- let newArr = this.arr.flat(1).isOdd;
368
- return new Matrix(this.rows, this.cols, newArr);
369
- }*/
370
385
  max2min() {
371
386
  let newArr = this.arr.flat(1).max2min;
372
387
  return new Matrix(this.rows, this.cols, newArr);
@@ -504,42 +519,44 @@ class Matrix{
504
519
  }
505
520
  get somme() {
506
521
  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];
522
+ for (let i = 0; i < this.rows; i++)
523
+ for (let j = 0; j < this.cols; j++)
524
+ S += this.arr[i][j];
508
525
  return S;
509
526
  }
510
- get hasComplex() {
527
+ hasComplex(){
511
528
  return this.arr.flat(Infinity).some((n) => n instanceof Complex);
512
529
  }
513
530
  get min() {
514
- if (this.hasComplex) console.error("Complex numbers are not comparable");
531
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
515
532
  let minRow = [];
516
533
  for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
517
534
  return min(...minRow);
518
535
  }
519
536
  get max() {
520
- if (this.hasComplex) console.error("Complex numbers are not comparable");
537
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
521
538
  let maxRow = [];
522
539
  for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
523
540
  return max(...maxRow);
524
541
  }
525
542
  get minRows() {
526
- if (this.hasComplex) console.error("Complex numbers are not comparable");
543
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
527
544
  let minRow = [];
528
545
  for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
529
546
  return minRow;
530
547
  }
531
548
  get maxRows() {
532
- if (this.hasComplex) console.error("Complex numbers are not comparable");
549
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
533
550
  let maxRow = [];
534
551
  for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
535
552
  return maxRow;
536
553
  }
537
554
  get minCols() {
538
- if (this.hasComplex) console.error("Complex numbers are not comparable");
555
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
539
556
  return this.T.minRows;
540
557
  }
541
558
  get maxCols() {
542
- if (this.hasComplex) console.error("Complex numbers are not comparable");
559
+ if (this.hasComplex()) console.error("Complex numbers are not comparable");
543
560
  return this.T.maxRows;
544
561
  }
545
562
  static fromVector(v) {