ziko 0.51.0 → 0.51.1

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/dist/ziko.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  /*
3
3
  Project: ziko.js
4
4
  Author: Zakaria Elalaoui
5
- Date : Wed Dec 03 2025 10:58:16 GMT+0100 (UTC+01:00)
5
+ Date : Thu Dec 04 2025 12:08:41 GMT+0100 (UTC+01:00)
6
6
  Git-Repo : https://github.com/zakarialaoui10/ziko.js
7
7
  Git-Wiki : https://github.com/zakarialaoui10/ziko.js/wiki
8
8
  Released under MIT License
@@ -43,7 +43,6 @@ Fixed = new Proxy(Fixed, {
43
43
  });
44
44
 
45
45
  // To generalise
46
-
47
46
  const mapfun$1=(fun,...X)=>{
48
47
  const Y=X.map(x=>{
49
48
  if(x===null) return fun(null);
@@ -52,10 +51,9 @@ const mapfun$1=(fun,...X)=>{
52
51
  if(ArrayBuffer.isView(x)) return x.map(n=>fun(n));
53
52
  if(x instanceof Set) return new Set(mapfun$1(fun,...[...x]));
54
53
  if(x instanceof Map) return new Map([...x].map(n=>[n[0],mapfun$1(fun,n[1])]));
55
- if(x instanceof Matrix){
56
- return new Matrix(x.rows,x.cols, mapfun$1(x.arr.flat(1)))
57
- }
58
- if(x instanceof Complex){
54
+ if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, mapfun$1(x.arr.flat(1)))
55
+ if(x.isComplex?.()){
56
+ const complex = (...args) => new x.constructor(...args);
59
57
  const [a,b,z,phi]=[x.a,x.b,x.z,x.phi];
60
58
  switch(fun){
61
59
  case Math.log: return complex(ln(z),phi); // Done
@@ -84,14 +82,215 @@ const mapfun$1=(fun,...X)=>{
84
82
  return Y.length==1?Y[0]:Y;
85
83
  };
86
84
 
85
+ // Mixed calcul
86
+ const sum=(...x)=>{
87
+ if(x.every(n=>typeof n==="number")){
88
+ let s = x[0];
89
+ for (let i = 1; i < x.length; i++) s += x[i];
90
+ return s;
91
+ }
92
+ const Y=[];
93
+ for(let i=0;i<x.length;i++){
94
+ if(x[i] instanceof Array)Y.push(sum(...x[i]));
95
+ else if(x[i] instanceof Object){
96
+ Y.push(sum(...Object.values(x[i])));
97
+ }
98
+ }
99
+ return Y.length===1?Y[0]:Y;
100
+ };
101
+ const prod=(...x)=>{
102
+ if(x.every(n=>typeof n==="number")){
103
+ let p = x[0];
104
+ for (let i = 1; i < x.length; i++) p *= x[i];
105
+ return p;
106
+ }
107
+ const Y=[];
108
+ for(let i=0;i<x.length;i++){
109
+ if(x[i] instanceof Array)Y.push(prod(...x[i]));
110
+ else if(x[i] instanceof Object){
111
+ Y.push(prod(...Object.values(x[i])));
112
+ }
113
+ }
114
+ return Y.length===1?Y[0]:Y;
115
+ };
116
+ const min=(...num)=>{
117
+ if(num.every(n=>typeof n==="number"))return Math.min(...num);
118
+ const Y=[];
119
+ for(let i=0;i<num.length;i++){
120
+ if(num[i] instanceof Array)Y.push(min(...num[i]));
121
+ else if(num[i] instanceof Object){
122
+ Y.push(
123
+ Object.fromEntries(
124
+ [Object.entries(num[i]).sort((a,b)=>a[1]-b[1])[0]]
125
+ )
126
+ );
127
+ }
128
+ }
129
+ return Y.length===1?Y[0]:Y;
130
+ };
131
+ const max=(...num)=>{
132
+ if(num.every(n=>typeof n==="number"))return Math.max(...num);
133
+ const Y=[];
134
+ for(let i=0;i<num.length;i++){
135
+ if(num[i] instanceof Array)Y.push(min(...num[i]));
136
+ else if(num[i] instanceof Object){
137
+ Y.push(
138
+ Object.fromEntries(
139
+ [Object.entries(num[i]).sort((a,b)=>b[1]-a[1])[0]]
140
+ )
141
+ );
142
+ }
143
+ }
144
+ return Y.length===1?Y[0]:Y;
145
+ };
146
+ const accum=(...num)=>{
147
+ if(num.every(n=>typeof n==="number")){
148
+ let acc = num.reduce((x, y) => [...x, x[x.length - 1] + y], [0]);
149
+ acc.shift();
150
+ return acc;
151
+ }
152
+ const Y=[];
153
+ for(let i=0;i<num.length;i++){
154
+ if(num[i] instanceof Array)Y.push(accum(...num[i]));
155
+ else if(num[i] instanceof Object){
156
+ Y.push(null
157
+ // Object.fromEntries(
158
+ // [Object.entries(num[i]).sort((a,b)=>b[1]-a[1])[0]]
159
+ // )
160
+ );
161
+ }
162
+ }
163
+ return Y.length===1?Y[0]:Y;
164
+ };
165
+
166
+ //moy
167
+ //med
168
+ //variance
169
+ //std
170
+ //mode
171
+ //acccum
172
+ //min2max
173
+ //max2min
174
+ //percentile
175
+
176
+ const abs=(...x)=>mapfun$1(Math.abs,...x);
177
+ const sqrt$2=(...x)=>mapfun$1(Math.sqrt,...x);
178
+ const pow$1=(x,n)=>{
179
+ if(typeof x === "number"){
180
+ if(typeof n === "number")return Math.pow(x,n);
181
+ else if(n?.isComplex?.())return n.constructor.fromExpo(x**n.a,n.b*ln(x))
182
+ else return mapfun$1(a=>pow$1(x,a),...n);
183
+ }
184
+ else if(x.isComplex?.()){
185
+ if(typeof n === "number")return x.constructor.fromExpo(x.z**n,x.phi*n);
186
+ else if(n.isComplex?.())return x.constructor.fromExpo(
187
+ x.z**n.a*e(-x.phi*n.b),
188
+ ln(x.z)*n.b+n.a*x.phi
189
+ )
190
+ else return mapfun$1(a=>pow$1(x,a),...n);
191
+ }
192
+ else if(x instanceof Array){
193
+ if(typeof n === "number") return mapfun$1(a=>pow$1(a,n),...x);
194
+ else if(n instanceof Array){
195
+ const Y=[];
196
+ for(let i=0;i<x.length;i++){
197
+ Y.push(mapfun$1(a=>pow$1(x[i],a),...n));
198
+ }
199
+ return Y;
200
+ }
201
+ }
202
+ };
203
+ const sqrtn=(x,n)=>{
204
+ if(typeof x === "number"){
205
+ if(typeof n === "number")return Math.pow(x,1/n);
206
+ else return mapfun$1(a=>sqrtn(x,a),...n);
207
+ }
208
+ else if(x.isComplex?.()){
209
+ if(typeof n === "number")return x.constructor.fromExpo(sqrtn(x.z,n),x.phi/n);
210
+ else return mapfun$1(a=>sqrtn(x,a),...n);
211
+ }
212
+ else if(x instanceof Array){
213
+ if(typeof n === "number") return mapfun$1(a=>sqrtn(a,n),...x);
214
+ else if(n instanceof Array){
215
+ const Y=[];
216
+ for(let i=0;i<x.length;i++){
217
+ Y.push(mapfun$1(a=>sqrtn(x[i],a),...n));
218
+ }
219
+ return Y;
220
+ }
221
+ }
222
+ };
223
+ const e=(...x) => mapfun$1(Math.exp,...x);
224
+ const ln=(...x) => mapfun$1(Math.log,...x);
225
+ const cos$2=(...x) => mapfun$1(Fixed.cos,...x);
226
+ const sin$2=(...x) => mapfun$1(Fixed.sin,...x);
227
+ const tan=(...x) => mapfun$1(Fixed.tan,...x);
228
+ const sec=(...x) => mapfun$1(Fixed.sec,...x);
229
+ const sinc=(...x) => mapfun$1(Fixed.sinc,...x);
230
+ const csc=(...x) => mapfun$1(Fixed.csc,...x);
231
+ const cot=(...x) => mapfun$1(Fixed.cot,...x);
232
+ const acos$1=(...x) => mapfun$1(Fixed.acos,...x);
233
+ const asin=(...x) => mapfun$1(Fixed.asin,...x);
234
+ const atan=(...x) => mapfun$1(Fixed.atan,...x);
235
+ const acot=(...x) => mapfun$1(Fixed.acot,...x);
236
+ const cosh$1=(...x) => mapfun$1(Fixed.cosh,...x);
237
+ const sinh$1=(...x) => mapfun$1(Fixed.sinh,...x);
238
+ const tanh=(...x) => mapfun$1(Fixed.tanh,...x);
239
+ const coth=(...x) => mapfun$1(Fixed.coth,...x);
240
+ const acosh=(...x) => mapfun$1(Fixed.acosh,...x);
241
+ const asinh=(...x) => mapfun$1(Fixed.asinh,...x);
242
+ const atanh=(...x) => mapfun$1(Fixed.atanh,...x);
243
+ const ceil=(...x) => mapfun$1(Math.ceil,...x);
244
+ const floor=(...x) => mapfun$1(Math.floor,...x);
245
+ const round=(...x) => mapfun$1(Math.round,...x);
246
+ const atan2=(x,y,rad=true)=>{
247
+ if(typeof x === "number"){
248
+ if(typeof y === "number")return rad?Math.atan2(x,y):Math.atan2(x,y)*180/Math.PI;
249
+ else return mapfun$1(a=>atan2(x,a,rad),...y);
250
+ }
251
+ // else if(x.isComplex?.()){
252
+ // if(typeof n === "number")return x.constructor.fromExpo(x.z**n,x.phi*n);
253
+ // else return mapfun(a=>pow(x,a),...n);
254
+ // }
255
+ else if(x instanceof Array){
256
+ if(typeof y === "number") return mapfun$1(a=>atan2(a,y,rad),...x);
257
+ else if(y instanceof Array){
258
+ const Y=[];
259
+ for(let i=0;i<x.length;i++){
260
+ Y.push(mapfun$1(a=>pow$1(x[i],a),...y));
261
+ }
262
+ return Y;
263
+ }
264
+ }
265
+ };
266
+ const fact=(...x)=>mapfun$1(n=> {
267
+ let i,
268
+ y = 1;
269
+ if (n == 0) y = 1;
270
+ else if (n > 0) for (i = 1; i <= n; i++) y *= i;
271
+ else y = NaN;
272
+ return y;
273
+ },...x);
274
+ const sign=(...x)=>mapfun$1(Math.sign,...x);
275
+
276
+ const sig=(...x)=>mapfun$1(n=>1/(1+e(-n)),...x);
277
+
278
+ const hypot=(...x)=>{
279
+ if(x.every(n=>typeof n === "number"))return Math.hypot(...x);
280
+ if(x.every(n=>n instanceof Array))return mapfun$1(
281
+ Math.hypot,
282
+ ...x
283
+ )
284
+ };
285
+
87
286
  const _add=(a,b)=>{
88
287
  if(typeof(a)==="number"){
89
288
  if (typeof b == "number") return a + b;
90
- else if (b instanceof Complex)return complex(a + b.a, b.b);
91
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).add(b);
289
+ else if (b.isComplex?.())return new b.constructor(a + b.a, b.b);
290
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).add(b);
92
291
  else if (b instanceof Array)return b.map(n=>add(n,a));
93
292
  }
94
- else if(a instanceof Complex||a instanceof Matrix){
293
+ else if(a.isComplex?.()||a.isMatrix?.()){
95
294
  if(b instanceof Array)return b.map(n=>a.clone.add(n));
96
295
  return a.clone.add(b);
97
296
  }
@@ -107,11 +306,11 @@ const _add=(a,b)=>{
107
306
  const _sub=(a,b)=>{
108
307
  if(typeof(a)==="number"){
109
308
  if (typeof b == "number") return a - b;
110
- else if (b instanceof Complex)return complex(a - b.a, -b.b);
111
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).sub(b);
309
+ else if (b.isComplex?.())return new b.constructor(a - b.a, -b.b);
310
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).sub(b);
112
311
  else if (b instanceof Array)return b.map(n=>sub(n,a));
113
312
  }
114
- else if(a instanceof Complex||a instanceof Matrix){
313
+ else if(a.isComplex?.()||a.isMatrix?.()){
115
314
  if(b instanceof Array)return b.map(n=>a.clone.sub(n));
116
315
  return a.clone.sub(b);
117
316
  }
@@ -129,11 +328,11 @@ const _sub=(a,b)=>{
129
328
  const _mul=(a,b)=>{
130
329
  if(typeof(a)==="number"){
131
330
  if (typeof b == "number") return a * b;
132
- else if (b instanceof Complex)return complex(a * b.a,a * b.b);
133
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).mul(b);
331
+ else if (b.isComplex?.())return new b.constructor(a * b.a,a * b.b);
332
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).mul(b);
134
333
  else if (b instanceof Array)return b.map(n=>mul(a,n));
135
334
  }
136
- else if(a instanceof Complex||a instanceof Matrix){
335
+ else if(a.isComplex?.()||a.isMatrix?.()){
137
336
  if(b instanceof Array)return b.map(n=>a.clone.mul(n));
138
337
  return a.clone.mul(b);
139
338
  }
@@ -151,11 +350,11 @@ const _mul=(a,b)=>{
151
350
  const _div=(a,b)=>{
152
351
  if(typeof(a)==="number"){
153
352
  if (typeof b == "number") return a / b;
154
- else if (b instanceof Complex)return complex(a / b.a,a / b.b);
155
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).div(b);
353
+ else if (b.isComplex?.())return new b.constructor(a / b.a,a / b.b);
354
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).div(b);
156
355
  else if (b instanceof Array)return b.map(n=>div(a,n));
157
356
  }
158
- else if(a instanceof Complex||a instanceof Matrix){
357
+ else if(a.isComplex?.()||a.isMatrix?.()){
159
358
  if(b instanceof Array)return b.map(n=>a.clone.div(n));
160
359
  return a.clone.div(b);
161
360
  }
@@ -173,11 +372,11 @@ const _div=(a,b)=>{
173
372
  const _modulo=(a,b)=>{
174
373
  if(typeof(a)==="number"){
175
374
  if (typeof b == "number") return a % b;
176
- else if (b instanceof Complex)return complex(a % b.a,a % b.b);
177
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).modulo(b);
375
+ else if (b.isComplex?.())return new b.constructor(a % b.a,a % b.b);
376
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).modulo(b);
178
377
  else if (b instanceof Array)return b.map(n=>div(a,n));
179
378
  }
180
- else if(a instanceof Complex||a instanceof Matrix){
379
+ else if(a.isComplex?.()||a.isMatrix?.()){
181
380
  if(b instanceof Array)return b.map(n=>a.clone.div(n));
182
381
  return a.clone.div(b);
183
382
  }
@@ -219,8 +418,8 @@ const ones=(n)=>new Array(n).fill(1);
219
418
  const nums=(num,n)=>new Array(n).fill(num);
220
419
  const norm=(value, min, max)=>{
221
420
  if (typeof value === "number") return min !== max ? (value - min) / (max - min) : 0;
222
- else if (value instanceof Matrix) return new Matrix(value.rows, value.cols, norm(value.arr.flat(1), min, max));
223
- else if (value instanceof Complex) return new Complex(norm(value.a, min, max), norm(value.b, min, max));
421
+ else if (value.isMatrix?.()) return new value.constructor(value.rows, value.cols, norm(value.arr.flat(1), min, max));
422
+ else if (value.isComplex?.()) return new value.constructor(norm(value.a, min, max), norm(value.b, min, max));
224
423
  else if (value instanceof Array) {
225
424
  if (value.every((n) => typeof (n === "number"))) {
226
425
  return value.map((n) => norm(n, min, max));
@@ -234,8 +433,8 @@ const norm=(value, min, max)=>{
234
433
  };
235
434
  const lerp=(value, min, max)=>{
236
435
  if (typeof value === "number") return (max - min) * value + min;
237
- else if (value instanceof Matrix) return new Matrix(value.rows, value.cols, lerp(value.arr.flat(1), min, max));
238
- else if (value instanceof Complex) return new Complex(lerp(value.a, min, max), lerp(value.b, min, max));
436
+ else if (value.isMatrix?.()) return new value.constructor(value.rows, value.cols, lerp(value.arr.flat(1), min, max));
437
+ else if (value.isComplex?.()) return new value.constructor(lerp(value.a, min, max), lerp(value.b, min, max));
239
438
  else if (value instanceof Array) {
240
439
  if (value.every((n) => typeof (n === "number"))) {
241
440
  return value.map((n) => lerp(n, min, max));
@@ -247,17 +446,17 @@ const lerp=(value, min, max)=>{
247
446
  }
248
447
  }
249
448
  };
250
- const map$1=(value, a, b, c, d)=>{
251
- if (typeof value === "number") return lerp(norm(value, a, b), c, d);
252
- else if (value instanceof Matrix) return new Matrix(value.rows, value.cols, map$1(value.arr.flat(1), a, b, c, d));
253
- else if (value instanceof Complex) return new Complex(map$1(value.a, b, c, d), map$1(value.b, a, b, c, d));
254
- else if (value instanceof Array) {
255
- if (value.every((n) => typeof (n === "number"))) {
256
- return value.map((n) => map$1(n, a, b, c, d));
449
+ const map$1=(x, a, b, c, d)=>{
450
+ if (typeof x === "number") return lerp(norm(x, a, b), c, d);
451
+ else if (x.isMatrix?.()) return new x.constructor(x.rows, x.cols, map$1(x.arr.flat(1), a, b, c, d));
452
+ else if (x.isComplex?.()) return new x.constructor(map$1(x.a, b, c, d), map$1(x.b, a, b, c, d));
453
+ else if (x instanceof Array) {
454
+ if (x.every((n) => typeof (n === "number"))) {
455
+ return x.map((n) => map$1(n, a, b, c, d));
257
456
  } else {
258
- let y = new Array(value.length);
259
- for (let i = 0; i < value.length; i++) {
260
- y[i] = map$1(value[i], a, b, c, d);
457
+ let y = new Array(x.length);
458
+ for (let i = 0; i < x.length; i++) {
459
+ y[i] = map$1(x[i], a, b, c, d);
261
460
  }
262
461
  }
263
462
  }
@@ -265,8 +464,8 @@ const map$1=(value, a, b, c, d)=>{
265
464
  const clamp=(x, a , b)=>{
266
465
  const [min_value,max_value]=[min(a,b),max(a,b)];
267
466
  if (typeof x === "number") return min(max(x, min_value), max_value);
268
- else if (x instanceof Matrix) return new Matrix(x.rows, x.cols, clamp(x.arr.flat(1), min_value, max_value));
269
- else if (x instanceof Complex) return new Complex(clamp(x.a, min_value, max_value), clamp(x.b, min_value, max_value));
467
+ else if (x.isMatrix?.()) return new x.constructor(x.rows, x.cols, clamp(x.arr.flat(1), min_value, max_value));
468
+ else if (x.isComplex?.()) return new x.constructor(clamp(x.a, min_value, max_value), clamp(x.b, min_value, max_value));
270
469
  else if (x instanceof Array) {
271
470
  if (x.every((n) => typeof (n === "number"))) {
272
471
  return x.map((n) => clamp(n, min_value, max_value));
@@ -301,14 +500,14 @@ const linspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
301
500
  return Y
302
501
  }
303
502
 
304
- if([a,b].some(n=>n instanceof Complex)){
305
- const z1=complex(a);
306
- const z2=complex(b);
503
+ if([a,b].some(n=>n.isComplex?.())){
504
+ const z1 = new n.constructor(a);
505
+ const z2 = new n.constructor(b);
307
506
  n=n||Math.abs(z1.a-z2.a)+1;
308
507
  const X=linspace(z1.a,z2.a,n,endpoint);
309
508
  const Y=linspace(z1.b,z2.b,n,endpoint);
310
509
  let Z=new Array(n).fill(null);
311
- Z=Z.map((n,i)=>complex(X[i],Y[i]));
510
+ Z=Z.map((n,i)=> new n.constructor(X[i],Y[i]));
312
511
  return Z;
313
512
  }
314
513
  };
@@ -328,9 +527,9 @@ const geomspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
328
527
  return a<b?Y:Y.reverse()
329
528
  }
330
529
 
331
- if([a,b].some(n=>n instanceof Complex)){
332
- const z1=complex(a);
333
- const z2=complex(b);
530
+ if([a,b].some(n=>n.isComplex?.())){
531
+ const z1 = new n.constructor(a);
532
+ const z2 = new n.constructor(b);
334
533
  n=n||Math.abs(z1.a-z2.a)+1;
335
534
  let base;
336
535
  endpoint ? base = sqrtn(z2.div(z1),n-1) : base = sqrtn(z2.div(z1),n) ;
@@ -357,109 +556,18 @@ const deg2rad = (...deg) => mapfun(x => x * Math.PI / 180, ...deg);
357
556
  */
358
557
  const rad2deg = (...rad) => mapfun(x => x / Math.PI * 180, ...rad);
359
558
 
360
- // Mixed calcul
361
- const sum=(...x)=>{
362
- if(x.every(n=>typeof n==="number")){
363
- let s = x[0];
364
- for (let i = 1; i < x.length; i++) s += x[i];
365
- return s;
366
- }
367
- const Y=[];
368
- for(let i=0;i<x.length;i++){
369
- if(x[i] instanceof Array)Y.push(sum(...x[i]));
370
- else if(x[i] instanceof Object){
371
- Y.push(sum(...Object.values(x[i])));
372
- }
373
- }
374
- return Y.length===1?Y[0]:Y;
375
- };
376
- const prod=(...x)=>{
377
- if(x.every(n=>typeof n==="number")){
378
- let p = x[0];
379
- for (let i = 1; i < x.length; i++) p *= x[i];
380
- return p;
381
- }
382
- const Y=[];
383
- for(let i=0;i<x.length;i++){
384
- if(x[i] instanceof Array)Y.push(prod(...x[i]));
385
- else if(x[i] instanceof Object){
386
- Y.push(prod(...Object.values(x[i])));
387
- }
388
- }
389
- return Y.length===1?Y[0]:Y;
390
- };
391
- const min=(...num)=>{
392
- if(num.every(n=>typeof n==="number"))return Math.min(...num);
393
- const Y=[];
394
- for(let i=0;i<num.length;i++){
395
- if(num[i] instanceof Array)Y.push(min(...num[i]));
396
- else if(num[i] instanceof Object){
397
- Y.push(
398
- Object.fromEntries(
399
- [Object.entries(num[i]).sort((a,b)=>a[1]-b[1])[0]]
400
- )
401
- );
402
- }
403
- }
404
- return Y.length===1?Y[0]:Y;
405
- };
406
- const max=(...num)=>{
407
- if(num.every(n=>typeof n==="number"))return Math.max(...num);
408
- const Y=[];
409
- for(let i=0;i<num.length;i++){
410
- if(num[i] instanceof Array)Y.push(min(...num[i]));
411
- else if(num[i] instanceof Object){
412
- Y.push(
413
- Object.fromEntries(
414
- [Object.entries(num[i]).sort((a,b)=>b[1]-a[1])[0]]
415
- )
416
- );
417
- }
418
- }
419
- return Y.length===1?Y[0]:Y;
420
- };
421
- const accum=(...num)=>{
422
- if(num.every(n=>typeof n==="number")){
423
- let acc = num.reduce((x, y) => [...x, x[x.length - 1] + y], [0]);
424
- acc.shift();
425
- return acc;
426
- }
427
- const Y=[];
428
- for(let i=0;i<num.length;i++){
429
- if(num[i] instanceof Array)Y.push(accum(...num[i]));
430
- else if(num[i] instanceof Object){
431
- Y.push(null
432
- // Object.fromEntries(
433
- // [Object.entries(num[i]).sort((a,b)=>b[1]-a[1])[0]]
434
- // )
435
- );
436
- }
437
- }
438
- return Y.length===1?Y[0]:Y;
439
- };
440
-
441
- //moy
442
- //med
443
- //variance
444
- //std
445
- //mode
446
- //acccum
447
- //min2max
448
- //max2min
449
- //percentile
450
-
451
- /** @module Math */
452
- /**
453
- * Checks if a value is within the specified range.
454
- * @function
455
- * @param {number} x - The value to check.
456
- * @param {number} a - The start of the range.
457
- * @param {number} b - The end of the range.
458
- * @returns {boolean} Returns true if the value is within the range [a, b], otherwise false.
459
- */
460
- const inRange = (x, a, b) => {
461
- const [min, max] = [Math.min(a, b), Math.max(a, b)];
462
- return x >= min && x <= max;
559
+ /** @module Math */
560
+ /**
561
+ * Checks if a value is within the specified range.
562
+ * @function
563
+ * @param {number} x - The value to check.
564
+ * @param {number} a - The start of the range.
565
+ * @param {number} b - The end of the range.
566
+ * @returns {boolean} Returns true if the value is within the range [a, b], otherwise false.
567
+ */
568
+ const inRange = (x, a, b) => {
569
+ const [min, max] = [Math.min(a, b), Math.max(a, b)];
570
+ return x >= min && x <= max;
463
571
  };
464
572
 
465
573
  /**
@@ -552,385 +660,182 @@ const Utils={
552
660
  isApproximatlyEqual
553
661
  };
554
662
 
555
- const powerSet=originalSet=>{
556
- const subSets = [];
557
- const NUMBER_OF_COMBINATIONS = 2 ** originalSet.length;
558
- for (let i = 0; i < NUMBER_OF_COMBINATIONS; i += 1) {
559
- const subSet = [];
560
- for (let j = 0; j < originalSet.length; j += 1) {
561
- if (i & (1 << j)) {
562
- subSet.push(originalSet[j]);
663
+ class Complex{
664
+ constructor(a = 0, b = 0) {
665
+ if(a instanceof Complex){
666
+ this.a=a.a;
667
+ this.b=a.b;
668
+ }
669
+ else if(typeof(a)==="object"){
670
+ if(("a" in a && "b" in a)){
671
+ this.a=a.a;
672
+ this.b=a.b;
673
+ }
674
+ else if(("a" in a && "z" in a)){
675
+ this.a=a.a;
676
+ this.b=sqrt$2((a.z**2)-(a.a**2));
677
+ }
678
+ else if(("a" in a && "phi" in a)){
679
+ this.a=a.a;
680
+ this.b=a.a*tan(a.phi);
681
+ }
682
+ else if(("b" in a && "z" in a)){
683
+ this.b=a.b;
684
+ this.a=sqrt$2((a.z**2)-(a.b**2));
685
+ }
686
+ else if(("b" in a && "phi" in a)){
687
+ this.b=b;
688
+ this.a=a.b/tan(a.phi);
689
+ }
690
+ else if(("z" in a && "phi" in a)){
691
+ this.a=a.z*cos$2(a.phi);
692
+ this.a=a.z*sin$2(a.phi);
563
693
  }
564
694
  }
565
- subSets.push(subSet);
695
+ else if(typeof(a)==="number"&&typeof(b)==="number"){
696
+ this.a = +a.toFixed(32);
697
+ this.b = +b.toFixed(32);
698
+ }
566
699
  }
567
- return subSets;
568
- };
569
-
570
- // const subSet = (...arr) => {
571
- // let list = arange(0, 2 ** arr.length, 1);
572
- // let bin = list.map((n) => n.toString(2).padStart(arr.length, '0')).map((n) => n.split("").map((n) => +n));
573
- // let sub = bin.map((n) => n.map((m, i) => (m === 1 ? arr[i] : null))).map((n) => n.filter((x) => x !== null));
574
- // return sub;
575
- // };
576
- const subSet = null;
577
-
578
- const Base={
579
- _mode:Number,
580
- _map:function(func,number,toBase){
581
- if (number instanceof Matrix)
582
- return new Matrix(
583
- number.rows,
584
- number.cols,
585
- number.arr.flat(1).map(n=>func(n,toBase))
586
- );
587
- else if (number instanceof Complex) return new Complex(func(number.a,toBase),func(number.b,toBase));
588
- else if (number instanceof Array) return number.map((n) =>func(n,toBase));
589
- },
590
- dec2base(dec,base){
591
- base<=10?this._mode=Number:this._mode=String;
592
- //this._mode=String
593
- if (typeof dec === "number") return this._mode((dec >>> 0).toString(base));
594
- return this._map(this.dec2base,dec,base)
595
- },
596
- dec2bin(dec){
597
- return this.dec2base(dec,2);
598
- },
599
- dec2oct(dec){
600
- return this.dec2base(dec,8);
601
- },
602
- dec2hex(dec){
603
- return this.dec2base(dec,16);
604
- },
605
- bin2base(bin, base) {
606
- return this.dec2base(this.bin2dec(bin),base)
607
- },
608
- bin2dec(bin){
609
- return this._mode("0b"+bin);
610
- },
611
- bin2oct(bin){
612
- return this.bin2base(bin,8);
613
- },
614
- bin2hex(bin){
615
- return this.bin2base(bin,16);
616
- },
617
- oct2dec(oct){
618
- return this._mode("0o"+oct);
619
- },
620
- oct2bin(oct){
621
- return this.dec2bin(this.oct2dec(oct))
622
- },
623
- oct2hex(oct){
624
- return this.dec2hex(this.oct2dec(oct))
625
- },
626
- oct2base(oct, base) {
627
- return this.dec2base(this.oct2dec(oct),base)
628
- },
629
- hex2dec(hex){
630
- return this._mode("0x"+hex);
631
- },
632
- hex2bin(hex){
633
- return this.dec2bin(this.hex2dec(hex))
634
- },
635
- hex2oct(hex){
636
- return this.dec2oct(this.hex2dec(hex))
637
- },
638
- hex2base(hex, base) {
639
- return this.dec2base(this.hex2dec(hex),base)
640
- },
641
- IEEE32toDec(Bin){
642
- let IEEE32=Bin.split(" ").join("").padEnd(32,"0");
643
- let s=IEEE32[0];
644
- let e=2**(+("0b"+IEEE32.slice(1,9))-127);
645
- let m=IEEE32.slice(9,32).split("").map(n=>+n);
646
- let M=m.map((n,i)=>n*(2**(-i-1))).reduce((a,b)=>a+b,0);
647
- let dec=(-1)**s*(1+M)*e;
648
- return dec
649
- },
650
- IEEE64toDec(Bin){
651
- let IEEE64=Bin.split(" ").join("").padEnd(64,"0");
652
- let s=IEEE64[0];
653
- let e=2**(+("0b"+IEEE64.slice(1,12))-1023);
654
- let m=IEEE64.slice(13,64).split("").map(n=>+n);
655
- let M=m.map((n,i)=>n*(2**(-i-1))).reduce((a,b)=>a+b,0);
656
- let dec=(-1)**s*(1+M)*e;
657
- return dec;
700
+ isComplex(){
701
+ return true
658
702
  }
659
- };
660
-
661
- const Logic$1={
662
- _mode:Number,
663
- _map:function(func,a,b){
664
- if (a instanceof Matrix)
665
- return new Matrix(
666
- a.rows,
667
- a.cols,
668
- a.arr.flat(1).map((n) => func(n, b))
669
- );
670
- else if (a instanceof Complex) return new Complex(func(a.a, b), func(a.b, b));
671
- else if (a instanceof Array) return a.map((n) => func(n, b));
672
- },
673
- not:function(input){
674
- if(["number","boolean"].includes(typeof input)) return Logic$1._mode(!input);
675
- else return this._map(this.not,input)
676
- },
677
- and:function(a, ...b){
678
- if(["number","boolean"].includes(typeof a))return Logic$1._mode(b.reduce((n, m) => (n &= m), a));
679
- else return this._map(this.and,a,b)
680
- },
681
- or:function(a, ...b) {
682
- if(["number","boolean"].includes(typeof a)) return Logic$1._mode(b.reduce((n, m) => (n |= m), a));
683
- else return this._map(this.or,a,b);
684
- },
685
- nand:function(a, ...b) {
686
- return this.not(this.and(a, b));
687
- },
688
- nor:function(a, ...b) {
689
- return this.not(this.or(a, b));
690
- },
691
- xor:function(a,...b){
692
- let arr=[a,...b];
693
- if(["number","boolean"].includes(typeof a))return this._mode(arr.reduce((length,cur)=>{
694
- if(+cur===1)length+=1;
695
- return length;
696
- },0)===1);
697
- else return this._map(this.xor,a,b);
698
- },
699
- xnor:function(a,...b){
700
- return Logic$1.not(Logic$1.xor(a,b))
703
+ toString(){
704
+ let str = "";
705
+ if (this.a !== 0)
706
+ this.b >= 0
707
+ ? (str = `${this.a}+${this.b}*i`)
708
+ : (str = `${this.a}-${Math.abs(this.b)}*i`);
709
+ else
710
+ this.b >= 0
711
+ ? (str = `${this.b}*i`)
712
+ : (str = `-${Math.abs(this.b)}*i`);
713
+ return str;
701
714
  }
702
-
703
- };
704
-
705
- class Permutation {
706
- static withDiscount(arr, l = arr.length) {
707
- if (l === 1) return arr.map((n) => [n]);
708
- const permutations = [];
709
- let smallerPermutations;
710
- smallerPermutations = this.withDiscount(arr, l - 1);
711
- arr.forEach((currentOption) => {
712
- smallerPermutations.forEach((smallerPermutation) => {
713
- permutations.push([currentOption].concat(smallerPermutation));
714
- });
715
- });
716
- return permutations;
717
- }
718
- static withoutDiscount(arr) {
719
- const l = arr.length;
720
- if (l === 1) return arr.map((n) => [n]);
721
- const permutations = [];
722
- const smallerPermutations = this.withoutDiscount(arr.slice(1));
723
- const firstOption = arr[0];
724
- for (let i = 0; i < smallerPermutations.length; i++) {
725
- const smallerPermutation = smallerPermutations[i];
726
- for (let j = 0; j <= smallerPermutation.length; j++) {
727
- const permutationPrefix = smallerPermutation.slice(0, j);
728
- const permutationSuffix = smallerPermutation.slice(j);
729
- permutations.push(permutationPrefix.concat([firstOption], permutationSuffix));
730
- }
731
- }
732
- return permutations;
733
- }
734
- }
735
-
736
- class Combinaison {
737
- static withDiscount(comboOptions, comboLength) {
738
- if (comboLength === 1) {
739
- return comboOptions.map((comboOption) => [comboOption]);
740
- }
741
- const combos = [];
742
- comboOptions.forEach((currentOption, optionIndex) => {
743
- const smallerCombos = this.withDiscount(comboOptions.slice(optionIndex), comboLength - 1);
744
- smallerCombos.forEach((smallerCombo) => {
745
- combos.push([currentOption].concat(smallerCombo));
746
- });
747
- });
748
- return combos;
749
- }
750
- static withoutDiscount(comboOptions, comboLength) {
751
- if (comboLength === 1) {
752
- return comboOptions.map((comboOption) => [comboOption]);
753
- }
754
- const combos = [];
755
- comboOptions.forEach((currentOption, optionIndex) => {
756
- const smallerCombos = this.withoutDiscount(comboOptions.slice(optionIndex + 1), comboLength - 1);
757
- smallerCombos.forEach((smallerCombo) => {
758
- combos.push([currentOption].concat(smallerCombo));
759
- });
760
- });
761
-
762
- return combos;
763
- }
764
- }
765
- const combinaison=(comboOptions, comboLength, discount=false)=>Combinaison[discount?"withDiscount":"withoutDiscount"](comboOptions, comboLength);
766
-
767
- class Random {
768
- static float(a = 1, b) {
769
- return b ? Math.random() * (b - a) + a : a * Math.random();
770
- }
771
- static int(a, b) {
772
- return Math.floor(this.float(a, b));
773
- }
774
- static char(upperCase){
775
- upperCase=upperCase??this.bool();
776
- const Char=String.fromCharCode(this.int(97,120));
777
- return upperCase?Char.toUpperCase():Char;
778
- }
779
- static bool(){
780
- return [false,true][Math.floor(Math.random()*2)];
781
- }
782
- static string(length,upperCase){
783
- return length instanceof Array?
784
- new Array(this.int(...length)).fill(0).map(() => this.char(upperCase)).join(""):
785
- new Array(length).fill(0).map(() => this.char(upperCase)).join("");
786
- }
787
- static bin() {
788
- return this.int(2);
789
- }
790
- static oct() {
791
- return this.int(8);
792
- }
793
- static dec() {
794
- return this.int(8);
795
- }
796
- static hex() {
797
- return this.int(16);
798
- }
799
- static choice(choices = [1, 2, 3], p = new Array(choices.length).fill(1 / choices.length)) {
800
- let newchoice = new Array(100);
801
- p=Utils.accum(...p).map(n=>n*100);
802
- newchoice.fill(choices[0], 0, p[0]);
803
- for (let i = 1; i < choices.length; i++) newchoice.fill(choices[i], p[i - 1], p[i]);
804
- return newchoice[this.int(newchoice.length - 1)];
805
- }
806
- static shuffleArr(arr){
807
- return arr.sort(()=>0.5-Math.random())
808
- }
809
- static shuffleMatrix(M){
810
- const {rows,cols,arr}=M;
811
- return matrix(rows,cols,arr.flat().sort(()=>0.5-Math.random()))
812
- }
813
- static floats(n, a, b) {
814
- return new Array(n).fill(0).map(() => this.float(a, b));
815
- }
816
- static ints(n, a, b) {
817
- return new Array(n).fill(0).map(() => this.int(a, b));
715
+
716
+ get clone() {
717
+ return new Complex(this.a, this.b);
818
718
  }
819
- static bools(n){
820
- return new Array(n).fill(0).map(() => this.bool());
719
+ get z(){
720
+ return hypot(this.a,this.b);
821
721
  }
822
- static bins(n) {
823
- return new Array(n).fill(0).map(() => this.int(2));
722
+ get phi(){
723
+ return atan2(this.b , this.a);
824
724
  }
825
- static octs(n) {
826
- return new Array(n).fill(0).map(() => this.int(8));
725
+ static Zero() {
726
+ return new Complex(0, 0);
827
727
  }
828
- static decs(n) {
829
- return new Array(n).fill(0).map(() => this.int(10));
728
+ get conj() {
729
+ return new Complex(this.a, -this.b);
830
730
  }
831
- static hexs(n) {
832
- return new Array(n).fill(0).map(() => this.int(16));
731
+ get inv() {
732
+ return new Complex(this.a / (pow$1(this.a, 2) + pow$1(this.b, 2)), -this.b / (pow$1(this.a, 2) + pow$1(this.b, 2)));
833
733
  }
834
- static choices(n, choices, p) {
835
- return new Array(n).fill(0).map(() => this.choice(choices, p));
734
+ add(...z) {
735
+ for (let i = 0; i < z.length; i++) {
736
+ if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
737
+ }
738
+ let re = z.map((n) => n.a);
739
+ let im = z.map((n) => n.b);
740
+ this.a+=+sum(...re).toFixed(15);
741
+ this.b+=+sum(...im).toFixed(15);
742
+ return this;
836
743
  }
837
- static perm(...arr) {
838
- // permutation
839
- return arr.permS[this.int(arr.length)];
744
+ sub(...z) {
745
+ for (let i = 0; i < z.length; i++) {
746
+ if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
747
+ }
748
+ let re = z.map((n) => n.a);
749
+ let im = z.map((n) => n.b);
750
+ this.a-=+sum(...re).toFixed(15);
751
+ this.b-=+sum(...im).toFixed(15);
752
+ return this;
840
753
  }
841
- static color() {
842
- return "#" + Base.dec2hex(this.float(16777216)).padStart(6,0);
754
+ mul(...z){
755
+ for (let i = 0; i < z.length; i++) {
756
+ if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
757
+ }
758
+ let Z=+prod(this.z,...z.map(n=>n.z)).toFixed(15);
759
+ let phi=+sum(this.phi,...z.map(n=>n.phi)).toFixed(15);
760
+ this.a=+(Z*cos$2(phi).toFixed(15)).toFixed(14);
761
+ this.b=+(Z*sin$2(phi).toFixed(15)).toFixed(14);
762
+ return this;
843
763
  }
844
- static colors(n) {
845
- return new Array(n).fill(null).map(()=>this.color());
764
+ div(...z) {
765
+ for (let i = 0; i < z.length; i++) {
766
+ if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
767
+ }
768
+ let Z=+(this.z/prod(...z.map(n=>n.z))).toFixed(15);
769
+ let phi=+(this.phi-sum(...z.map(n=>n.phi))).toFixed(15);
770
+ this.a=+(Z*cos$2(phi).toFixed(15)).toFixed(15);
771
+ this.b=+(Z*sin$2(phi).toFixed(15)).toFixed(15);
772
+ return this;
846
773
  }
847
- static complex(a = [0,1], b = [0,1]) {
848
- return a instanceof Array?
849
- new Complex(
850
- this.float(a[0], a[1]),
851
- this.float(b[0], b[1])
852
- ):
853
- new Complex(
854
- ...this.floats(2,a,b)
855
- )
856
-
774
+ pow(n) {
775
+ if (floor(n) === n && n > 0) {
776
+ let z=+(this.z**n).toFixed(15);
777
+ let phi=+(this.phi*n).toFixed(15);
778
+ this.a=+(z*cos$2(phi).toFixed(15)).toFixed(15);
779
+ this.b=+(z*sin$2(phi).toFixed(15)).toFixed(15);
780
+ }
781
+ return this;
857
782
  }
858
- static complexInt(a = [0,1], b = [0,1]) {
783
+ static fromExpo(z, phi) {
859
784
  return new Complex(
860
- this.int(a[0], a[1]),
861
- this.int(b[0], b[1])
785
+ +(z * cos$2(phi)).toFixed(13),
786
+ +(z * sin$2(phi)).toFixed(13)
862
787
  );
863
788
  }
864
- static complexBin() {
865
- return new Complex(...this.bins(2));
866
- }
867
- static complexOct() {
868
- return new Complex(...this.octs(2));
869
- }
870
- static complexDec() {
871
- return new Complex(...this.decs(10));
872
- }
873
- static complexHex() {
874
- return new Complex(...this.octs(2));
875
- }
876
- static complexes(n, a = 0, b = 1) {
877
- return new Array(n).fill(0).map(() => this.complex(a, b));
878
- }
879
- static complexesInt(n, a = 0, b = 1) {
880
- return new Array(n).fill(0).map(() => this.complexInt(a, b));
881
- }
882
- static complexesBin(n) {
883
- return new Array(n).fill(0).map(() => this.complexBin());
884
- }
885
- static complexesOct(n) {
886
- return new Array(n).fill(0).map(() => this.complexOct());
887
- }
888
- static complexesDec(n) {
889
- return new Array(n).fill(0).map(() => this.complexDec());
890
- }
891
- static complexesHex(n) {
892
- return new Array(n).fill(0).map(() => this.complexHex());
789
+ get expo() {
790
+ return [this.z, this.phi];
893
791
  }
894
- static matrix(r,c,min,max){
895
- return matrix(r,c,this.floats(r*c,min,max))
792
+ static add(c,...z) {
793
+ return c.clone.add(...z);
896
794
  }
897
- static matrixInt(r,c,min,max){
898
- return matrix(r,c,this.ints(r*c,min,max))
795
+ static sub(c,...z) {
796
+ return c.clone.sub(...z);
899
797
  }
900
- static matrixBin(r,c){
901
- return matrix(r,c,this.bins(r*c))
798
+ static mul(c,...z) {
799
+ return c.clone.mul(...z);
902
800
  }
903
- static matrixOct(r,c){
904
- return matrix(r,c,this.octs(r*c))
801
+ static div(c,...z) {
802
+ return c.clone.div(...z);
905
803
  }
906
- static matrixDec(r,c){
907
- return matrix(r,c,this.decs(r*c))
804
+ static pow(z,n){
805
+ return z.clone.pow(n);
908
806
  }
909
- static matrixHex(r,c){
910
- return matrix(r,c,this.hex(r*c))
807
+ static xpowZ(x){
808
+ return complex((x**this.a)*cos$2(this.b*ln(x)),(x**this.a)*sin$2(this.b*ln(x)));
911
809
  }
912
- static matrixColor(r,c){
913
- return matrix(r,c,this.colors(r*c))
810
+ sqrtn(n=2){
811
+ return complex(sqrtn(this.z,n)*cos$2(this.phi/n),sqrtn(this.z,n)*sin$2(this.phi/n));
914
812
  }
915
- static matrixComplex(r,c,a,b){
916
- return matrix(r,c,this.complexes(r*c,a,b))
813
+ get sqrt(){
814
+ return this.sqrtn(2);
917
815
  }
918
- static matrixComplexInt(r,c,a,b){
919
- return matrix(r,c,this.complexesInt(r*c,a,b))
816
+ get log(){
817
+ return complex(this.z,this.phi);
920
818
  }
921
- static matrixComplexBin(r,c){
922
- return matrix(r,c,this.complexesBin(r*c))
819
+ get cos(){
820
+ return complex(cos$2(this.a)*cosh$1(this.b),sin$2(this.a)*sinh$1(this.b))
923
821
  }
924
- static matrixComplexOct(r,c){
925
- return matrix(r,c,this.complexesBin(r*c))
822
+ get sin(){
823
+ return complex(sin$2(this.a)*cosh$1(this.b),cos$2(this.a)*sinh$1(this.b))
926
824
  }
927
- static matrixComplexDec(r,c){
928
- return matrix(r,c,this.complexesBin(r*c))
825
+ get tan(){
826
+ const de=cos$2(this.a*2)+cosh$1(this.b*2);
827
+ return complex(sin$2(2*this.a)/de,sinh$1(2*this.b)/de);
929
828
  }
930
- static matrixComplexHex(r,c){
931
- return matrix(r,c,this.complexesBin(r*c))
829
+ }
830
+ const complex=(a,b)=>{
831
+ if((a instanceof Array||ArrayBuffer.isView(a)) && (b instanceof Array||ArrayBuffer.isView(a)))return a.map((n,i)=>complex(a[i],b[i]));
832
+ if(a.isMatrix?.() && b.isMatrix?.()){
833
+ if((a.shape[0]!==b.shape[0])||(a.shape[1]!==b.shape[1]))return Error(0)
834
+ const arr=a.arr.map((n,i)=>complex(a.arr[i],b.arr[i]));
835
+ return new a.constructor(a.rows,a.cols,...arr)
932
836
  }
933
- }
837
+ return new Complex(a,b)
838
+ };
934
839
 
935
840
  const preload=(url)=>{
936
841
  const xhr = new XMLHttpRequest();
@@ -1356,29 +1261,407 @@ const __CACHE__ = {
1356
1261
  }
1357
1262
  };
1358
1263
 
1359
- class UseIPC {
1360
- #channel;
1361
- #eventData;
1362
- #handlers;
1363
- #uuid;
1364
- #subscribers;
1365
- #currentRooms;
1366
- constructor(name = "") {
1367
- this.#channel = new BroadcastChannel(name);
1368
- this.#eventData = new Map();
1369
- this.#handlers = new Map(); // Map<event, Array<{fn, rooms}>>
1370
- this.#uuid = "ziko-channel:" + Random.string(10);
1371
- this.#subscribers = new Set([this.#uuid]);
1372
- this.#currentRooms = new Set();
1373
- this.#channel.addEventListener("message", (e) => {
1374
- const { last_sent_event, userId, eventData, rooms } = e.data;
1375
- if (userId === this.#uuid) return; // ignore own messages
1376
- // broadcast if no rooms, else check intersection
1377
- if (rooms && rooms.length && !rooms.some(r => this.#currentRooms.has(r))) return;
1378
- this.#subscribers.add(userId);
1379
- this.#eventData = new Map(eventData);
1380
- const handlersList = this.#handlers.get(last_sent_event);
1381
- if (!handlersList) return;
1264
+ const powerSet=originalSet=>{
1265
+ const subSets = [];
1266
+ const NUMBER_OF_COMBINATIONS = 2 ** originalSet.length;
1267
+ for (let i = 0; i < NUMBER_OF_COMBINATIONS; i += 1) {
1268
+ const subSet = [];
1269
+ for (let j = 0; j < originalSet.length; j += 1) {
1270
+ if (i & (1 << j)) {
1271
+ subSet.push(originalSet[j]);
1272
+ }
1273
+ }
1274
+ subSets.push(subSet);
1275
+ }
1276
+ return subSets;
1277
+ };
1278
+
1279
+ // const subSet = (...arr) => {
1280
+ // let list = arange(0, 2 ** arr.length, 1);
1281
+ // let bin = list.map((n) => n.toString(2).padStart(arr.length, '0')).map((n) => n.split("").map((n) => +n));
1282
+ // let sub = bin.map((n) => n.map((m, i) => (m === 1 ? arr[i] : null))).map((n) => n.filter((x) => x !== null));
1283
+ // return sub;
1284
+ // };
1285
+ const subSet = null;
1286
+
1287
+ const Base={
1288
+ _mode:Number,
1289
+ _map:function(func,number,toBase){
1290
+ if (number instanceof Matrix)
1291
+ return new Matrix(
1292
+ number.rows,
1293
+ number.cols,
1294
+ number.arr.flat(1).map(n=>func(n,toBase))
1295
+ );
1296
+ else if (number instanceof Complex) return new Complex(func(number.a,toBase),func(number.b,toBase));
1297
+ else if (number instanceof Array) return number.map((n) =>func(n,toBase));
1298
+ },
1299
+ dec2base(dec,base){
1300
+ base<=10?this._mode=Number:this._mode=String;
1301
+ //this._mode=String
1302
+ if (typeof dec === "number") return this._mode((dec >>> 0).toString(base));
1303
+ return this._map(this.dec2base,dec,base)
1304
+ },
1305
+ dec2bin(dec){
1306
+ return this.dec2base(dec,2);
1307
+ },
1308
+ dec2oct(dec){
1309
+ return this.dec2base(dec,8);
1310
+ },
1311
+ dec2hex(dec){
1312
+ return this.dec2base(dec,16);
1313
+ },
1314
+ bin2base(bin, base) {
1315
+ return this.dec2base(this.bin2dec(bin),base)
1316
+ },
1317
+ bin2dec(bin){
1318
+ return this._mode("0b"+bin);
1319
+ },
1320
+ bin2oct(bin){
1321
+ return this.bin2base(bin,8);
1322
+ },
1323
+ bin2hex(bin){
1324
+ return this.bin2base(bin,16);
1325
+ },
1326
+ oct2dec(oct){
1327
+ return this._mode("0o"+oct);
1328
+ },
1329
+ oct2bin(oct){
1330
+ return this.dec2bin(this.oct2dec(oct))
1331
+ },
1332
+ oct2hex(oct){
1333
+ return this.dec2hex(this.oct2dec(oct))
1334
+ },
1335
+ oct2base(oct, base) {
1336
+ return this.dec2base(this.oct2dec(oct),base)
1337
+ },
1338
+ hex2dec(hex){
1339
+ return this._mode("0x"+hex);
1340
+ },
1341
+ hex2bin(hex){
1342
+ return this.dec2bin(this.hex2dec(hex))
1343
+ },
1344
+ hex2oct(hex){
1345
+ return this.dec2oct(this.hex2dec(hex))
1346
+ },
1347
+ hex2base(hex, base) {
1348
+ return this.dec2base(this.hex2dec(hex),base)
1349
+ },
1350
+ IEEE32toDec(Bin){
1351
+ let IEEE32=Bin.split(" ").join("").padEnd(32,"0");
1352
+ let s=IEEE32[0];
1353
+ let e=2**(+("0b"+IEEE32.slice(1,9))-127);
1354
+ let m=IEEE32.slice(9,32).split("").map(n=>+n);
1355
+ let M=m.map((n,i)=>n*(2**(-i-1))).reduce((a,b)=>a+b,0);
1356
+ let dec=(-1)**s*(1+M)*e;
1357
+ return dec
1358
+ },
1359
+ IEEE64toDec(Bin){
1360
+ let IEEE64=Bin.split(" ").join("").padEnd(64,"0");
1361
+ let s=IEEE64[0];
1362
+ let e=2**(+("0b"+IEEE64.slice(1,12))-1023);
1363
+ let m=IEEE64.slice(13,64).split("").map(n=>+n);
1364
+ let M=m.map((n,i)=>n*(2**(-i-1))).reduce((a,b)=>a+b,0);
1365
+ let dec=(-1)**s*(1+M)*e;
1366
+ return dec;
1367
+ }
1368
+ };
1369
+
1370
+ const Logic$1={
1371
+ _mode:Number,
1372
+ _map:function(func,a,b){
1373
+ if (a instanceof Matrix)
1374
+ return new Matrix(
1375
+ a.rows,
1376
+ a.cols,
1377
+ a.arr.flat(1).map((n) => func(n, b))
1378
+ );
1379
+ else if (a instanceof Complex) return new Complex(func(a.a, b), func(a.b, b));
1380
+ else if (a instanceof Array) return a.map((n) => func(n, b));
1381
+ },
1382
+ not:function(input){
1383
+ if(["number","boolean"].includes(typeof input)) return Logic$1._mode(!input);
1384
+ else return this._map(this.not,input)
1385
+ },
1386
+ and:function(a, ...b){
1387
+ if(["number","boolean"].includes(typeof a))return Logic$1._mode(b.reduce((n, m) => (n &= m), a));
1388
+ else return this._map(this.and,a,b)
1389
+ },
1390
+ or:function(a, ...b) {
1391
+ if(["number","boolean"].includes(typeof a)) return Logic$1._mode(b.reduce((n, m) => (n |= m), a));
1392
+ else return this._map(this.or,a,b);
1393
+ },
1394
+ nand:function(a, ...b) {
1395
+ return this.not(this.and(a, b));
1396
+ },
1397
+ nor:function(a, ...b) {
1398
+ return this.not(this.or(a, b));
1399
+ },
1400
+ xor:function(a,...b){
1401
+ let arr=[a,...b];
1402
+ if(["number","boolean"].includes(typeof a))return this._mode(arr.reduce((length,cur)=>{
1403
+ if(+cur===1)length+=1;
1404
+ return length;
1405
+ },0)===1);
1406
+ else return this._map(this.xor,a,b);
1407
+ },
1408
+ xnor:function(a,...b){
1409
+ return Logic$1.not(Logic$1.xor(a,b))
1410
+ }
1411
+
1412
+ };
1413
+
1414
+ class Permutation {
1415
+ static withDiscount(arr, l = arr.length) {
1416
+ if (l === 1) return arr.map((n) => [n]);
1417
+ const permutations = [];
1418
+ let smallerPermutations;
1419
+ smallerPermutations = this.withDiscount(arr, l - 1);
1420
+ arr.forEach((currentOption) => {
1421
+ smallerPermutations.forEach((smallerPermutation) => {
1422
+ permutations.push([currentOption].concat(smallerPermutation));
1423
+ });
1424
+ });
1425
+ return permutations;
1426
+ }
1427
+ static withoutDiscount(arr) {
1428
+ const l = arr.length;
1429
+ if (l === 1) return arr.map((n) => [n]);
1430
+ const permutations = [];
1431
+ const smallerPermutations = this.withoutDiscount(arr.slice(1));
1432
+ const firstOption = arr[0];
1433
+ for (let i = 0; i < smallerPermutations.length; i++) {
1434
+ const smallerPermutation = smallerPermutations[i];
1435
+ for (let j = 0; j <= smallerPermutation.length; j++) {
1436
+ const permutationPrefix = smallerPermutation.slice(0, j);
1437
+ const permutationSuffix = smallerPermutation.slice(j);
1438
+ permutations.push(permutationPrefix.concat([firstOption], permutationSuffix));
1439
+ }
1440
+ }
1441
+ return permutations;
1442
+ }
1443
+ }
1444
+
1445
+ class Combinaison {
1446
+ static withDiscount(comboOptions, comboLength) {
1447
+ if (comboLength === 1) {
1448
+ return comboOptions.map((comboOption) => [comboOption]);
1449
+ }
1450
+ const combos = [];
1451
+ comboOptions.forEach((currentOption, optionIndex) => {
1452
+ const smallerCombos = this.withDiscount(comboOptions.slice(optionIndex), comboLength - 1);
1453
+ smallerCombos.forEach((smallerCombo) => {
1454
+ combos.push([currentOption].concat(smallerCombo));
1455
+ });
1456
+ });
1457
+ return combos;
1458
+ }
1459
+ static withoutDiscount(comboOptions, comboLength) {
1460
+ if (comboLength === 1) {
1461
+ return comboOptions.map((comboOption) => [comboOption]);
1462
+ }
1463
+ const combos = [];
1464
+ comboOptions.forEach((currentOption, optionIndex) => {
1465
+ const smallerCombos = this.withoutDiscount(comboOptions.slice(optionIndex + 1), comboLength - 1);
1466
+ smallerCombos.forEach((smallerCombo) => {
1467
+ combos.push([currentOption].concat(smallerCombo));
1468
+ });
1469
+ });
1470
+
1471
+ return combos;
1472
+ }
1473
+ }
1474
+ const combinaison=(comboOptions, comboLength, discount=false)=>Combinaison[discount?"withDiscount":"withoutDiscount"](comboOptions, comboLength);
1475
+
1476
+ class Random {
1477
+ static float(a = 1, b) {
1478
+ return b ? Math.random() * (b - a) + a : a * Math.random();
1479
+ }
1480
+ static int(a, b) {
1481
+ return Math.floor(this.float(a, b));
1482
+ }
1483
+ static char(upperCase){
1484
+ upperCase=upperCase??this.bool();
1485
+ const Char=String.fromCharCode(this.int(97,120));
1486
+ return upperCase?Char.toUpperCase():Char;
1487
+ }
1488
+ static bool(){
1489
+ return [false,true][Math.floor(Math.random()*2)];
1490
+ }
1491
+ static string(length,upperCase){
1492
+ return length instanceof Array?
1493
+ new Array(this.int(...length)).fill(0).map(() => this.char(upperCase)).join(""):
1494
+ new Array(length).fill(0).map(() => this.char(upperCase)).join("");
1495
+ }
1496
+ static bin() {
1497
+ return this.int(2);
1498
+ }
1499
+ static oct() {
1500
+ return this.int(8);
1501
+ }
1502
+ static dec() {
1503
+ return this.int(8);
1504
+ }
1505
+ static hex() {
1506
+ return this.int(16);
1507
+ }
1508
+ static choice(choices = [1, 2, 3], p = new Array(choices.length).fill(1 / choices.length)) {
1509
+ let newchoice = new Array(100);
1510
+ p=Utils.accum(...p).map(n=>n*100);
1511
+ newchoice.fill(choices[0], 0, p[0]);
1512
+ for (let i = 1; i < choices.length; i++) newchoice.fill(choices[i], p[i - 1], p[i]);
1513
+ return newchoice[this.int(newchoice.length - 1)];
1514
+ }
1515
+ static shuffleArr(arr){
1516
+ return arr.sort(()=>0.5-Math.random())
1517
+ }
1518
+ static floats(n, a, b) {
1519
+ return new Array(n).fill(0).map(() => this.float(a, b));
1520
+ }
1521
+ static ints(n, a, b) {
1522
+ return new Array(n).fill(0).map(() => this.int(a, b));
1523
+ }
1524
+ static bools(n){
1525
+ return new Array(n).fill(0).map(() => this.bool());
1526
+ }
1527
+ static bins(n) {
1528
+ return new Array(n).fill(0).map(() => this.int(2));
1529
+ }
1530
+ static octs(n) {
1531
+ return new Array(n).fill(0).map(() => this.int(8));
1532
+ }
1533
+ static decs(n) {
1534
+ return new Array(n).fill(0).map(() => this.int(10));
1535
+ }
1536
+ static hexs(n) {
1537
+ return new Array(n).fill(0).map(() => this.int(16));
1538
+ }
1539
+ static choices(n, choices, p) {
1540
+ return new Array(n).fill(0).map(() => this.choice(choices, p));
1541
+ }
1542
+ static perm(...arr) {
1543
+ // permutation
1544
+ return arr.permS[this.int(arr.length)];
1545
+ }
1546
+ static color() {
1547
+ return "#" + Base.dec2hex(this.float(16777216)).padStart(6,0);
1548
+ }
1549
+ static colors(n) {
1550
+ return new Array(n).fill(null).map(()=>this.color());
1551
+ }
1552
+
1553
+ // Should be Moved to Matrix and Complex to avoid Circular dependencies
1554
+ // static complex(a = [0,1], b = [0,1]) {
1555
+ // return a instanceof Array?
1556
+ // new Complex(
1557
+ // this.float(a[0], a[1]),
1558
+ // this.float(b[0], b[1])
1559
+ // ):
1560
+ // new Complex(
1561
+ // ...this.floats(2,a,b)
1562
+ // )
1563
+
1564
+ // }
1565
+ // static complexInt(a = [0,1], b = [0,1]) {
1566
+ // return new Complex(
1567
+ // this.int(a[0], a[1]),
1568
+ // this.int(b[0], b[1])
1569
+ // );
1570
+ // }
1571
+ // static complexBin() {
1572
+ // return new Complex(...this.bins(2));
1573
+ // }
1574
+ // static complexOct() {
1575
+ // return new Complex(...this.octs(2));
1576
+ // }
1577
+ // static complexDec() {
1578
+ // return new Complex(...this.decs(10));
1579
+ // }
1580
+ // static complexHex() {
1581
+ // return new Complex(...this.octs(2));
1582
+ // }
1583
+ // static complexes(n, a = 0, b = 1) {
1584
+ // return new Array(n).fill(0).map(() => this.complex(a, b));
1585
+ // }
1586
+ // static complexesInt(n, a = 0, b = 1) {
1587
+ // return new Array(n).fill(0).map(() => this.complexInt(a, b));
1588
+ // }
1589
+ // static complexesBin(n) {
1590
+ // return new Array(n).fill(0).map(() => this.complexBin());
1591
+ // }
1592
+ // static complexesOct(n) {
1593
+ // return new Array(n).fill(0).map(() => this.complexOct());
1594
+ // }
1595
+ // static complexesDec(n) {
1596
+ // return new Array(n).fill(0).map(() => this.complexDec());
1597
+ // }
1598
+ // static complexesHex(n) {
1599
+ // return new Array(n).fill(0).map(() => this.complexHex());
1600
+ // }
1601
+ // static matrix(r,c,min,max){
1602
+ // return matrix(r,c,this.floats(r*c,min,max))
1603
+ // }
1604
+ // static matrixInt(r,c,min,max){
1605
+ // return matrix(r,c,this.ints(r*c,min,max))
1606
+ // }
1607
+ // static matrixBin(r,c){
1608
+ // return matrix(r,c,this.bins(r*c))
1609
+ // }
1610
+ // static matrixOct(r,c){
1611
+ // return matrix(r,c,this.octs(r*c))
1612
+ // }
1613
+ // static matrixDec(r,c){
1614
+ // return matrix(r,c,this.decs(r*c))
1615
+ // }
1616
+ // static matrixHex(r,c){
1617
+ // return matrix(r,c,this.hex(r*c))
1618
+ // }
1619
+ // static matrixColor(r,c){
1620
+ // return matrix(r,c,this.colors(r*c))
1621
+ // }
1622
+ // static matrixComplex(r,c,a,b){
1623
+ // return matrix(r,c,this.complexes(r*c,a,b))
1624
+ // }
1625
+ // static matrixComplexInt(r,c,a,b){
1626
+ // return matrix(r,c,this.complexesInt(r*c,a,b))
1627
+ // }
1628
+ // static matrixComplexBin(r,c){
1629
+ // return matrix(r,c,this.complexesBin(r*c))
1630
+ // }
1631
+ // static matrixComplexOct(r,c){
1632
+ // return matrix(r,c,this.complexesBin(r*c))
1633
+ // }
1634
+ // static matrixComplexDec(r,c){
1635
+ // return matrix(r,c,this.complexesBin(r*c))
1636
+ // }
1637
+ // static matrixComplexHex(r,c){
1638
+ // return matrix(r,c,this.complexesBin(r*c))
1639
+ // }
1640
+ }
1641
+
1642
+ class UseIPC {
1643
+ #channel;
1644
+ #eventData;
1645
+ #handlers;
1646
+ #uuid;
1647
+ #subscribers;
1648
+ #currentRooms;
1649
+ constructor(name = "") {
1650
+ this.#channel = new BroadcastChannel(name);
1651
+ this.#eventData = new Map();
1652
+ this.#handlers = new Map(); // Map<event, Array<{fn, rooms}>>
1653
+ this.#uuid = "ziko-channel:" + Random.string(10);
1654
+ this.#subscribers = new Set([this.#uuid]);
1655
+ this.#currentRooms = new Set();
1656
+ this.#channel.addEventListener("message", (e) => {
1657
+ const { last_sent_event, userId, eventData, rooms } = e.data;
1658
+ if (userId === this.#uuid) return; // ignore own messages
1659
+ // broadcast if no rooms, else check intersection
1660
+ if (rooms && rooms.length && !rooms.some(r => this.#currentRooms.has(r))) return;
1661
+ this.#subscribers.add(userId);
1662
+ this.#eventData = new Map(eventData);
1663
+ const handlersList = this.#handlers.get(last_sent_event);
1664
+ if (!handlersList) return;
1382
1665
  handlersList.forEach(({ fn, rooms: handlerRooms }) => {
1383
1666
  // trigger if listener has no room filter, or intersects subscriber rooms
1384
1667
  if (!handlerRooms || handlerRooms.length === 0 ||
@@ -3308,365 +3591,183 @@ function trimKeys(obj) {
3308
3591
  }, Array.isArray(obj) ? [] : {});
3309
3592
  }
3310
3593
 
3311
- class Matrix{
3312
- constructor(rows, cols, element = [] ) {
3313
- if(rows instanceof Matrix){
3314
- this.arr=rows.arr;
3315
- this.rows=rows.rows;
3316
- this.cols=rows.cols;
3317
- }
3318
- else {
3319
- let arr = [],
3320
- i,
3321
- j;
3322
- if (arguments[0] instanceof Array) {
3323
- rows = arguments[0].length;
3324
- cols = arguments[0][0].length;
3325
- arr = arguments[0];
3326
- } else {
3327
- for (i = 0; i < rows; i++) {
3328
- arr.push([]);
3329
- arr[i].push(new Array(cols));
3330
- for (j = 0; j < cols; j++) {
3331
- arr[i][j] = element[i * cols + j];
3332
- if (element[i * cols + j] == undefined) arr[i][j] = 0;
3333
- }
3334
- }
3335
- }
3336
- this.rows = rows;
3337
- this.cols = cols;
3338
- this.arr = arr;
3339
- }
3340
- this.#maintain();
3341
- //Object.seal(this);
3342
- }
3343
- toString(){
3344
- return arr2str(this.arr);
3345
- }
3346
- at(i=0,j=undefined){
3347
- if(i<0)i=this.rows+i;
3348
- if(j==undefined) return this.arr[i];
3349
- if(j<0)j=this.cols+j;
3350
- return this.arr[i][j];
3351
- }
3352
- reshape(newRows, newCols) {
3353
- let check = newRows * newCols === this.rows * this.cols;
3354
- if (check) return new Matrix(newRows, newCols, this.arr.flat(1));
3355
- else console.error("Err");
3356
- }
3357
- static eye(size) {
3358
- let result = new Matrix(size, size);
3359
- 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);
3360
- return result;
3361
- }
3362
- get clone() {
3363
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
3364
- }
3365
- get size() {
3366
- return this.rows * this.cols;
3367
- }
3368
- get shape() {
3369
- return [this.rows, this.cols];
3370
- }
3371
- get reel() {
3372
- return new Matrix(this.cols, this.rows, this.arr.flat(1).reel);
3373
- }
3374
- get imag() {
3375
- return new Matrix(this.cols, this.rows, this.arr.flat(1).imag);
3376
- }
3377
- [Symbol.iterator]() {
3378
- return this.arr[Symbol.iterator]();
3379
- }
3380
- #maintain() {
3381
- for (let i = 0; i < this.arr.length; i++) {
3382
- Object.defineProperty(this, i, {
3383
- value: this.arr[i],
3384
- writable: true,
3385
- configurable: true,
3386
- enumerable: false
3387
- });
3388
- }
3389
- }
3390
- get(row = 0, col = 0) {
3391
- if (col == -1) return this.arr[row];
3392
- else if (row == -1) return this.arr.map((n) => n[col]);
3393
- else return this.arr[row][col];
3394
- }
3395
- set(row = 0, col = 0, value) {
3396
- if (col == -1) return (this.arr[row] = value);
3397
- else if (row == -1) {
3398
- for (let i = 0; i < this.cols; i++) {
3399
- this.arr[i][col] = value[i] || 0;
3400
- }
3401
- return this.arr;
3402
- }
3403
- return (this.arr[row][col] = value);
3404
- }
3405
- get isSquare() {
3406
- return this.rows / this.cols === 1;
3407
- }
3408
- get isSym() {
3409
- if (!this.isSquare) return false;
3410
- const T = this.T;
3411
- const M = this.clone;
3412
- return Matrix.sub(M, T).max == 0 && Matrix.sub(M, T).min == 0;
3413
- }
3414
- get isAntiSym() {
3415
- if (!this.isSquare) return false;
3416
- const T = this.T;
3417
- const M = this.clone;
3418
- return Matrix.add(M, T).max == 0 && Matrix.add(M, T).min == 0;
3419
- }
3420
- get isDiag() {
3421
- if (!this.isSquare) return false;
3422
- const T = this.T;
3423
- const M = this.clone;
3424
- const MT = Matrix.mul(M, T);
3425
- const TM = Matrix.dot(T, M);
3426
- return Matrix.sub(MT, TM).max == 0 && Matrix.sub(MT, TM).min == 0;
3427
- }
3428
- get isOrtho() {
3429
- if (!this.isSquare) return false;
3430
- return this.isDiag && (this.det == 1 || this.det == -1);
3431
- }
3432
- get isIdemp() {
3433
- if (!this.isSquare) return false;
3434
- const M = this.clone;
3435
- const MM = Matrix.dot(M, M);
3436
- return Matrix.sub(MM, M).max == 0 && Matrix.sub(MM, M).min == 0;
3437
- }
3438
- get T() {
3439
- let transpose = [];
3440
- for (let i = 0; i < this.arr[0].length; i++) {
3441
- transpose[i] = [];
3442
- for (let j = 0; j < this.arr.length; j++) {
3443
- transpose[i][j] = this.arr[j][i];
3444
- }
3594
+ function matrix_inverse(M) {
3595
+ if(M.row !== M.cols) throw Error('is not a square matrix"')
3596
+ if (M.det === 0) throw Error("determinant should not equal 0");
3597
+ const { arr } = M;
3598
+ if (arr.length !== arr[0].length) return;
3599
+ var i = 0, ii = 0, j = 0, dim = arr.length, e = 0;
3600
+ var I = [], C = [];
3601
+ for (i = 0; i < dim; i += 1) {
3602
+ I[I.length] = [];
3603
+ C[C.length] = [];
3604
+ for (j = 0; j < dim; j += 1) {
3605
+ if (i == j) I[i][j] = 1;
3606
+ else I[i][j] = 0;
3607
+ C[i][j] = arr[i][j];
3445
3608
  }
3446
- return new Matrix(this.cols, this.rows, transpose.flat(1));
3447
3609
  }
3448
- get det() {
3449
- if (!this.isSquare) return new Error("is not square matrix");
3450
- if (this.rows == 1) return this.arr[0][0];
3451
- function determinat(M) {
3452
- if (M.length == 2) {
3453
- if (M.flat(1).some((n) => n instanceof Matrix)) {
3454
- console.warn("Tensors are not completely supported yet ...");
3455
- return;
3610
+ for (i = 0; i < dim; i += 1) {
3611
+ e = C[i][i];
3612
+ if (e == 0) {
3613
+ for (ii = i + 1; ii < dim; ii += 1) {
3614
+ if (C[ii][i] != 0) {
3615
+ for (j = 0; j < dim; j++) {
3616
+ e = C[i][j];
3617
+ C[i][j] = C[ii][j];
3618
+ C[ii][j] = e;
3619
+ e = I[i][j];
3620
+ I[i][j] = I[ii][j];
3621
+ I[ii][j] = e;
3622
+ }
3623
+ break;
3456
3624
  }
3457
- return Utils.sub(Utils.mul(M[0][0],M[1][1]),Utils.mul(M[0][1],M[1][0]))
3458
3625
  }
3459
- var answer = 0;
3460
- for (var i = 0; i < M.length; i++) {
3461
- //console.log(M[0][i]);
3462
- /*answer = answer.add(
3463
- pow(-1, i)
3464
- .mul(M[0][i])
3465
- .mul(determinat(deleteRowAndColumn(M, i)))
3466
- );*/
3467
- //const to_be_added=Utils.add(Utils.mul(pow(-1, i),Utils.mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
3468
- const to_be_added=Utils.add(Utils.mul(pow$1(-1, i),Utils.mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
3469
- answer=Utils.add(answer,to_be_added);
3470
- }
3471
- return answer;
3626
+ e = C[i][i];
3627
+ if (e == 0) return;
3472
3628
  }
3473
- function deleteRowAndColumn(M, index) {
3474
- var temp = [];
3475
- for (let i = 0; i < M.length; i++) temp.push(M[i].slice(0));
3476
- temp.splice(0, 1);
3477
- for (let i = 0; i < temp.length; i++) temp[i].splice(index, 1);
3478
- return temp;
3629
+ for (j = 0; j < dim; j++) {
3630
+ C[i][j] = C[i][j] / e;
3631
+ I[i][j] = I[i][j] / e;
3479
3632
  }
3480
- return determinat(this.arr);
3481
- }
3482
- get inv() {
3483
- if (!this.isSquare) return new Error("is not square matrix");
3484
- if (this.det === 0) return "determinat = 0 !!!";
3485
- let A = InverseMatrixe(this.arr);
3486
- return new Matrix(this.rows, this.cols, A.flat(1));
3487
- }
3488
- static zeros(rows, cols) {
3489
- let result = new Matrix(rows, cols);
3490
- for (let i = 0; i < rows; i++) for (var j = 0; j < cols; j++) result.arr[i][j] = 0;
3491
- return result;
3492
- }
3493
- static ones(rows, cols) {
3494
- let result = new Matrix(rows, cols);
3495
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = 1;
3496
- return result;
3497
- }
3498
- static nums(rows, cols, number) {
3499
- let result = new Matrix(rows, cols);
3500
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = number;
3501
- return result;
3502
- }
3503
- static get rand(){
3504
- return {
3505
- int:(rows, cols, a, b)=>{
3506
- let result = new Matrix(rows, cols);
3507
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randInt(a, b);
3508
- return result;
3509
- },
3510
- bin:(rows,cols)=>{
3511
- let result = new Matrix(rows, cols);
3512
- for (let i = 0; i < rows; i++) {
3513
- for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randBin;
3514
- }
3515
- return result;
3516
- },
3517
- hex:(rows,cols)=>{
3518
- let result = new Matrix(rows, cols);
3519
- for (let i = 0; i < rows; i++) {
3520
- for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randHex;
3521
- }
3522
- return result;
3523
- },
3524
- choices:(rows, cols, choices, p)=>{
3525
- let result = new Matrix(rows, cols);
3526
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.choice(choices, p);
3527
- return result
3528
- },
3529
- permutation:(rows,cols,arr)=>{
3530
- //return new Matrix(rows, cols, Random.permutation(...arr))
3633
+ for (ii = 0; ii < dim; ii++) {
3634
+ if (ii == i) {
3635
+ continue;
3531
3636
  }
3532
- }
3533
- }
3534
- static rands(rows, cols, a = 1, b) {
3535
- let result = new Matrix(rows, cols);
3536
- for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.rand(a, b);
3537
- return result;
3538
- }
3539
- map(Imin, Imax, Fmin, Fmax) {
3540
- return Utils.map(this, Imin, Imax, Fmin, Fmax);
3541
- }
3542
- lerp(min, max) {
3543
- return Utils.lerp(this, min, max);
3544
- }
3545
- norm(min, max) {
3546
- return Utils.norm(this, min, max);
3547
- }
3548
- clamp(min, max) {
3549
- return Utils.clamp(this, min, max);
3550
- }
3551
- static map(matrix, Imin, Imax, Fmin, Fmax) {
3552
- return Utils.map(matrix, Imin, Imax, Fmin, Fmax);
3553
- }
3554
- static lerp(matrix, min, max) {
3555
- return Utils.lerp(matrix, min, max);
3556
- }
3557
- static norm(matrix, min, max) {
3558
- return Utils.norm(matrix, min, max);
3559
- }
3560
- static clamp(m, min, max) {
3561
- return Utils.clamp(matrix, min, max);
3562
- }
3563
- toPrecision(p) {
3564
- 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);
3565
- return this;
3566
- }
3567
- get toBin() {
3568
- let newArr = this.arr.flat(1).toBin;
3569
- return new Matrix(this.rows, this.cols, newArr);
3570
- }
3571
- get toOct() {
3572
- let newArr = this.arr.flat(1).toOct;
3573
- return new Matrix(this.rows, this.cols, newArr);
3574
- }
3575
- get toHex() {
3576
- let newArr = this.arr.flat(1).toHex;
3577
- return new Matrix(this.rows, this.cols, newArr);
3578
- }
3579
- /*get isOdd() {
3580
- let newArr = this.arr.flat(1).isOdd;
3581
- return new Matrix(this.rows, this.cols, newArr);
3582
- }*/
3583
- max2min() {
3584
- let newArr = this.arr.flat(1).max2min;
3585
- return new Matrix(this.rows, this.cols, newArr);
3586
- }
3587
- min2max() {
3588
- let newArr = this.arr.flat(1).min2max;
3589
- return new Matrix(this.rows, this.cols, newArr);
3590
- }
3591
- sortRows(calback=undefined){
3592
- let newArr=this.arr.map(n=>n.sort(calback)).flat(1);
3593
- return new Matrix(this.rows, this.cols, newArr);
3594
- }
3595
- sortCols(calback=undefined){
3596
- let m=this.T;
3597
- let newArr=m.arr.map(n=>n.sort(calback)).flat(1);
3598
- return new Matrix(this.rows, this.cols, newArr).T;
3599
- }
3600
- filterByRows(item){
3601
- var truth=this.arr.map(n=>n.map(m=>+(""+m).includes(item)));
3602
- var mask=truth.map(n=>!!Logic.or(...n));
3603
- var filtredArray=this.arr.filter((n,i)=>mask[i]===true);
3604
- if(filtredArray.length===0)filtredArray.push([]);
3605
- console.log(filtredArray);
3606
- return new Matrix(filtredArray)
3607
- }
3608
- filterByCols(item){
3609
- return new Matrix(this.T.arr.filter(n=>n.includes(item)))
3610
- }
3611
- sortAll(calback=undefined){
3612
- let newArr=this.arr.flat(1).sort(calback);
3613
- return new Matrix(this.rows, this.cols, newArr);
3614
- }
3615
- count(n) {
3616
- return this.arr.flat(1).count(n);
3617
- }
3618
- toBase(n) {
3619
- let newArr = this.arr.flat(1).toBase(n);
3620
- return new Matrix(this.rows, this.cols, newArr);
3637
+ e = C[ii][i];
3638
+ for (j = 0; j < dim; j++) {
3639
+ C[ii][j] -= e * C[i][j];
3640
+ I[ii][j] -= e * I[i][j];
3641
+ }
3642
+ }
3621
3643
  }
3622
- #hstack(matrix){
3623
- if (this.rows !== matrix.rows) return;
3624
- let newArr = this.arr;
3625
- 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];
3626
- this.cols += matrix.cols;
3627
- return new Matrix(this.rows, this.cols, newArr.flat(1));
3644
+ return new M.constructor(I);
3645
+ }
3646
+
3647
+ function matrix_det(M) {
3648
+ if (!M.isSquare) return new Error("is not square matrix");
3649
+ if (M.rows == 1) return M.arr[0][0];
3650
+ function determinat(M) {
3651
+ if (M.length == 2) {
3652
+ if (M.flat(1).some((n) => n?.isMatrix?.())) {
3653
+ console.warn("Tensors are not completely supported yet ...");
3654
+ return;
3655
+ }
3656
+ return sub(mul(M[0][0],M[1][1]),mul(M[0][1],M[1][0]))
3657
+ }
3658
+ var answer = 0;
3659
+ for (var i = 0; i < M.length; i++) {
3660
+ //console.log(M[0][i]);
3661
+ /*answer = answer.add(
3662
+ pow(-1, i)
3663
+ .mul(M[0][i])
3664
+ .mul(determinat(deleteRowAndColumn(M, i)))
3665
+ );*/
3666
+ //const to_be_added=add(mul(pow(-1, i),mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
3667
+ const to_be_added=add(mul(pow$1(-1, i),mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
3668
+ answer=add(answer,to_be_added);
3669
+ }
3670
+ return answer;
3628
3671
  }
3629
- hstack(...matrices) {
3630
- const M=[this,...matrices].reduce((a,b)=>a.#hstack(b));
3631
- Object.assign(this,M);
3632
- return this;
3672
+ return determinat(M.arr);
3673
+ }
3674
+ function deleteRowAndColumn(M, index) {
3675
+ var temp = [];
3676
+ for (let i = 0; i < M.length; i++) temp.push(M[i].slice(0));
3677
+ temp.splice(0, 1);
3678
+ for (let i = 0; i < temp.length; i++) temp[i].splice(index, 1);
3679
+ return temp;
3680
+ }
3681
+
3682
+ function hstack(M1, M2){
3683
+ M1 = M1.clone();
3684
+ M2 = M2.clone();
3685
+ if (M1.rows !== M2.rows) return;
3686
+ let newArr = M1.arr;
3687
+ for (let i = 0; i < M1.rows; i++)
3688
+ for (let j = M1.cols; j < M1.cols + M2.cols; j++)
3689
+ newArr[i][j] = M2.arr[i][j - M1.cols];
3690
+ M1.cols += M2.cols;
3691
+ return new M1.constructor(M1.rows, M1.cols, newArr.flat(1));
3692
+ }
3693
+
3694
+ function vstack(M1, M2){
3695
+ M1 = M1.clone();
3696
+ M2 = M2.clone();
3697
+ if (M1.cols !== M2.cols) return;
3698
+ let newArr = M1.arr;
3699
+ for (let i = M1.rows; i < M1.rows + M2.rows; i++) {
3700
+ newArr[i] = [];
3701
+ for (let j = 0; j < M1.cols; j++) newArr[i][j] = M2.arr[i - M1.rows][j];
3702
+ }
3703
+ M1.rows += M2.rows;
3704
+ return new M1.constructor(M1.rows, M1.cols, newArr.flat(1));
3705
+ }
3706
+
3707
+ class Matrix{
3708
+ constructor(rows, cols, element = [] ) {
3709
+ if(rows instanceof Matrix){
3710
+ this.arr=rows.arr;
3711
+ this.rows=rows.rows;
3712
+ this.cols=rows.cols;
3713
+ }
3714
+ else {
3715
+ let arr = [], i, j;
3716
+ if (arguments[0] instanceof Array) {
3717
+ rows = arguments[0].length;
3718
+ cols = arguments[0][0].length;
3719
+ arr = arguments[0];
3720
+ } else {
3721
+ for (i = 0; i < rows; i++) {
3722
+ arr.push([]);
3723
+ arr[i].push(new Array(cols));
3724
+ for (j = 0; j < cols; j++) {
3725
+ arr[i][j] = element[i * cols + j];
3726
+ if (element[i * cols + j] == undefined) arr[i][j] = 0;
3727
+ }
3728
+ }
3729
+ }
3730
+ this.rows = rows;
3731
+ this.cols = cols;
3732
+ this.arr = arr;
3633
3733
  }
3634
- static hstack(matrix,...matrices) {
3635
- return matrix.clone.hstack(...matrices);
3734
+ this.#maintain();
3636
3735
  }
3637
- #vstack(matrix) {
3638
- if (this.cols !== matrix.cols) return;
3639
- let newArr = this.arr;
3640
- for (let i = this.rows; i < this.rows + matrix.rows; i++) {
3641
- newArr[i] = [];
3642
- for (let j = 0; j < this.cols; j++) newArr[i][j] = matrix.arr[i - this.rows][j];
3643
- }
3644
- this.rows += matrix.rows;
3645
- return new Matrix(this.rows, this.cols, newArr.flat(1));
3736
+ isMatrix(){
3737
+ return true
3646
3738
  }
3647
- vstack(...matrices) {
3648
- const M=[this,...matrices].reduce((a,b)=>a.#vstack(b));
3649
- Object.assign(this,M);
3650
- return this;
3739
+ clone() {
3740
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
3651
3741
  }
3652
- static vstack(matrix,...matrices) {
3653
- return matrix.clone.vstack(...matrices);
3742
+ [Symbol.iterator]() {
3743
+ return this.arr[Symbol.iterator]();
3654
3744
  }
3655
- hqueue(...matrices){
3656
- const M=[this,...matrices].reverse().reduce((a,b)=>a.#hstack(b));
3657
- Object.assign(this,M);
3658
- return this;
3745
+ #maintain() {
3746
+ for (let i = 0; i < this.arr.length; i++) {
3747
+ Object.defineProperty(this, i, {
3748
+ value: this.arr[i],
3749
+ writable: true,
3750
+ configurable: true,
3751
+ enumerable: false
3752
+ });
3753
+ }
3659
3754
  }
3660
- vqueue(...matrices){
3661
- const M=[this,...matrices].reverse().reduce((a,b)=>a.#vstack(b));
3662
- Object.assign(this,M);
3663
- return this;
3755
+ get size() {
3756
+ return this.rows * this.cols;
3757
+ }
3758
+ get shape() {
3759
+ return [this.rows, this.cols];
3664
3760
  }
3665
- static hqueue(matrix,...matrices) {
3666
- return matrix.clone.hqueue(...matrices);
3761
+ toString(){
3762
+ return arr2str(this.arr);
3667
3763
  }
3668
- static vqueue(matrix,...matrices) {
3669
- return matrix.clone.vqueue(...matrices);
3764
+ at(i = 0, j = undefined) {
3765
+ if (i < 0) i += this.rows;
3766
+ if (i < 0 || i >= this.rows) throw new Error('Row index out of bounds');
3767
+ if (j === undefined) return this.arr[i];
3768
+ if (j < 0) j += this.cols;
3769
+ if (j < 0 || j >= this.cols) throw new Error('Column index out of bounds');
3770
+ return this.arr[i][j];
3670
3771
  }
3671
3772
  slice(r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
3672
3773
  let newRow = r1 - r0,
@@ -3681,585 +3782,502 @@ class Matrix{
3681
3782
  static slice(m1,r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
3682
3783
  return m1.slice(r0, c0, r1, c1);
3683
3784
  }
3684
- splice(r0,c0,deleteCount,...items){
3685
-
3686
- }
3687
- getRows(ri, rf = ri + 1) {
3688
- return this.slice(ri, 0, rf, this.cols);
3689
- }
3690
- getCols(ci, cf = ci + 1) {
3691
- return this.slice(0, ci, this.rows, cf);
3692
- }
3693
- static getRows(m, ri, rf = ri + 1) {
3694
- return m.slice(ri, 0, rf, m.cols);
3695
- }
3696
- static getCols(m, ci, cf = ci + 1) {
3697
- return m.slice(0, ci, m.rows, cf);
3698
- }
3699
- add(...matr) {
3700
- for (let k = 0; k < matr.length; k++) {
3701
- if (typeof matr[k] == "number"||matr[k] instanceof Complex) matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
3702
- 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]);
3703
- }
3704
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
3705
- }
3706
- sub(...matr) {
3707
- for (let k = 0; k < matr.length; k++) {
3708
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
3709
- 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]);
3710
- }
3711
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
3712
- }
3713
- static add(m1, ...m2) {
3714
- return m1.clone.add(...m2);
3715
- }
3716
- static sub(m1, ...m2) {
3717
- return m1.clone.sub(...m2);
3718
- }
3719
- mul(...matr) {
3720
- for (let k = 0; k < matr.length; k++) {
3721
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
3722
- 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]);
3723
- }
3724
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
3725
- }
3726
- div(...matr) {
3727
- for (let k = 0; k < matr.length; k++) {
3728
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
3729
- 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]);
3730
- }
3731
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
3732
- }
3733
- static div(m1, ...m2) {
3734
- return m1.clone.div(...m2);
3735
- }
3736
- static mul(m1, ...m2) {
3737
- return m1.clone.mul(...m2);
3738
- }
3739
- modulo(...matr) {
3740
- for (let k = 0; k < matr.length; k++) {
3741
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
3742
- 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]);
3743
- }
3744
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
3745
- }
3746
- static modulo(m1, ...m2) {
3747
- return m1.clone.modulo(...m2);
3785
+ reshape(newRows, newCols) {
3786
+ if(!(newRows * newCols === this.rows * this.cols)) throw Error('size not matched')
3787
+ return new Matrix(newRows, newCols, this.arr.flat(1));
3748
3788
  }
3749
- dot(matrix) {
3750
- var res = [];
3751
- for (var i = 0; i < this.arr.length; i++) {
3752
- res[i] = [];
3753
- for (var j = 0; j < matrix.arr[0].length; j++) {
3754
- res[i][j] = 0;
3755
- for (var k = 0; k < this.arr[0].length; k++) {
3756
- res[i][j] = Utils.add(
3757
- res[i][j],
3758
- Utils.mul(this.arr[i][k],matrix.arr[k][j])
3759
- );
3760
- }
3761
- }
3789
+ get T() {
3790
+ let transpose = [];
3791
+ for (let i = 0; i < this.arr[0].length; i++) {
3792
+ transpose[i] = [];
3793
+ for (let j = 0; j < this.arr.length; j++)
3794
+ transpose[i][j] = this.arr[j][i];
3762
3795
  }
3763
- return new Matrix(this.arr.length, matrix.arr[0].length, res.flat(1));
3764
- }
3765
- static dot(matrix1, matrix2) {
3766
- return matrix1.dot(matrix2);
3767
- }
3768
- pow(n) {
3769
- let a = this.clone,
3770
- p = this.clone;
3771
- for (let i = 0; i < n - 1; i++) p = p.dot(a);
3772
- return p;
3773
- }
3774
- static pow(m, n) {
3775
- return m.clone.pow(n);
3776
- }
3777
- get somme() {
3778
- let S = 0;
3779
- for (let i = 0; i < this.rows; i++) for (let j = 0; j < this.cols; j++) S += this.arr[i][j];
3780
- return S;
3781
- }
3782
- get DoesItContainComplexNumbers() {
3783
- return this.arr.flat(Infinity).some((n) => n instanceof Complex);
3784
- }
3785
- get min() {
3786
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
3787
- let minRow = [];
3788
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
3789
- return min(...minRow);
3790
- }
3791
- get max() {
3792
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
3793
- let maxRow = [];
3794
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
3795
- return max(...maxRow);
3796
- }
3797
- get minRows() {
3798
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
3799
- let minRow = [];
3800
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
3801
- return minRow;
3802
- }
3803
- get maxRows() {
3804
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
3805
- let maxRow = [];
3806
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
3807
- return maxRow;
3808
- }
3809
- get minCols() {
3810
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
3811
- return this.T.minRows;
3812
- }
3813
- get maxCols() {
3814
- if (this.DoesItContainComplexNumbers) console.error("Complex numbers are not comparable");
3815
- return this.T.maxRows;
3796
+ return new Matrix(this.cols, this.rows, transpose.flat(1));
3816
3797
  }
3817
- static fromVector(v) {
3818
- return new Matrix(v.length, 1, v);
3798
+ get det() {
3799
+ return matrix_det(this)
3819
3800
  }
3820
- get toArray() {
3821
- let arr = [];
3822
- for (let i = 0; i < this.rows; i++) {
3823
- for (let j = 0; j < this.cols; j++) {
3824
- arr.push(this.arr[i][j]);
3825
- }
3826
- }
3827
- return arr;
3801
+ get inv() {
3802
+ return matrix_inverse(this)
3828
3803
  }
3829
- get print() {
3830
- //"pretty print" the matrix
3831
- let fstring = "[";
3832
- for (let i = 0; i < this.arr.length; i++) {
3833
- fstring += (i != 0 ? " " : "") + ` [${this.arr[i].map((n) => " " + n.toString() + " ")}],\n`;
3834
- }
3835
- console.log(fstring.substring(0, fstring.length - 2) + " ]");
3836
- document.write(fstring.substring(0, fstring.length - 2) + " ]");
3804
+ static eye(size) {
3805
+ let result = new Matrix(size, size);
3806
+ for (let i = 0; i < size; i++)
3807
+ for (let j = 0; j < size; j++) i === j ? (result.arr[i][j] = 1) : (result.arr[i][j] = 0);
3808
+ return result;
3837
3809
  }
3838
- get table() {
3839
- console.table(this.arr);
3810
+ static zeros(rows, cols) {
3811
+ let result = new Matrix(rows, cols);
3812
+ for (let i = 0; i < rows; i++)
3813
+ for (var j = 0; j < cols; j++) result.arr[i][j] = 0;
3814
+ return result;
3840
3815
  }
3841
- get serialize() {
3842
- return JSON.stringify(this);
3816
+ static ones(rows, cols) {
3817
+ let result = new Matrix(rows, cols);
3818
+ for (let i = 0; i < rows; i++)
3819
+ for (let j = 0; j < cols; j++) result.arr[i][j] = 1;
3820
+ return result;
3843
3821
  }
3844
- static deserialize(data) {
3845
- if (typeof data == "string") {
3846
- data = JSON.parse(data);
3847
- }
3848
- let matrix = new Matrix(data.rows, data.cols);
3849
- matrix.arr = data.arr;
3850
- return matrix;
3822
+ static nums(rows, cols, number) {
3823
+ let result = new Matrix(rows, cols);
3824
+ for (let i = 0; i < rows; i++)
3825
+ for (let j = 0; j < cols; j++) result.arr[i][j] = number;
3826
+ return result;
3851
3827
  }
3852
-
3853
- toTable() {
3854
- var table = new DocumentFragment();
3855
- var Tr = new Array(this.rows).fill(null).map(() => document?.createElement("tr"));
3856
- var Td = this.arr.map((n) => n.map(() => document?.createElement("td")));
3857
- for (let i = 0; i < Td.length; i++) {
3858
- for (let j = 0; j < Td[0].length; j++) {
3859
- Td[i][j].innerHTML = this.arr[i][j];
3860
- Tr[i].appendChild(Td[i][j]);
3861
- }
3862
- }
3863
- Tr.map((n) => table.appendChild(n));
3864
- return table;
3865
- }
3866
- toGrid(element, style = {}) {
3867
- let a = Grid();
3868
- a.append(
3869
- ...this.map(element)
3870
- .arr.flat(1)
3871
- .map((n) => n.style(style))
3872
- );
3873
- a.Columns(this.cols);
3874
- return a;
3828
+ hstack(...matrices) {
3829
+ const M=[this, ...matrices].reduce((a,b)=>hstack(a, b));
3830
+ Object.assign(this, M);
3831
+ this.#maintain();
3832
+ return this;
3875
3833
  }
3876
- sortTable(n=0,{type="num",order="asc"}={}) {
3877
- var obj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
3878
- var newObj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
3879
- if(type==="num"){
3880
- if(order==="asc")obj[n].sort((a,b)=>a.x-b.x);
3881
- else if(order==="desc")obj[n].sort((a,b)=>b.x-a.x);
3882
- else if(order==="toggle"){
3883
- // console.log(obj[n][0])
3884
- //console.log(obj[n][1])
3885
- if(obj[n][0].x>obj[n][1].x)obj[n].sort((a,b)=>b.x-a.x);
3886
- else obj[n].sort((a,b)=>a.x-b.x);
3887
- }
3888
- }
3889
- else if(type==="alpha"){
3890
- if(order==="asc")obj[n].sort((a,b)=>(""+a.x).localeCompare(""+b.x));
3891
- else if(order==="desc")obj[n].sort((a,b)=>(""+b.x).localeCompare(""+a.x));
3892
- }
3893
- //var order=obj[n].map(n=>n.y);
3894
- order=obj[n].map(n=>n.y);
3895
- for(let i=0;i<obj.length;i++){
3896
- if(i!==n)obj[i].map((n,j)=>n.y=order[j]);
3897
- }
3898
- for(let i=0;i<obj.length;i++){
3899
- if(i!==n)newObj[i].map((n,j)=>n.x=obj[i][order[j]].x);
3900
- }
3901
- newObj[n]=obj[n];
3902
- var newArr=newObj.map(n=>n.map(m=>m.x));
3903
- return new Matrix(newArr).T;
3834
+ static hstack(matrix,...matrices) {
3835
+ return matrix.clone().hstack(...matrices);
3904
3836
  }
3905
- }
3837
+ vstack(...matrices){
3838
+ const M=[this, ...matrices].reduce((a,b)=>vstack(a, b));
3839
+ Object.assign(this, M);
3840
+ this.#maintain();
3841
+ return this;
3842
+ }
3843
+ static vstack(matrix,...matrices) {
3844
+ return matrix.clone().vstack(...matrices);
3845
+ }
3846
+ hqueue(...matrices){
3847
+ const M=[this, ...matrices].reverse().reduce((a,b)=>hstack(a, b));
3848
+ Object.assign(this, M);
3849
+ return this;
3850
+ }
3851
+ static hqueue(matrix,...matrices) {
3852
+ return matrix.clone().hqueue(...matrices);
3853
+ }
3854
+ vqueue(...matrices){
3855
+ const M=[this,...matrices].reverse().reduce((a, b)=>vstack(a, b));
3856
+ Object.assign(this, M);
3857
+ return this;
3858
+ }
3859
+ static vqueue(matrix,...matrices) {
3860
+ return matrix.clone().vqueue(...matrices);
3861
+ }
3862
+ shuffle(){
3863
+ this.arr = this.arr.sort(()=>0.5-Math.random());
3864
+ return this;
3865
+ }
3866
+ static shuffle(M){
3867
+ return M.clone().shuffle()
3868
+ }
3869
+ // get reel() {
3870
+ // return new Matrix(this.cols, this.rows, this.arr.flat(1).reel);
3871
+ // }
3872
+ // get imag() {
3873
+ // return new Matrix(this.cols, this.rows, this.arr.flat(1).imag);
3874
+ // }
3906
3875
 
3907
- function InverseMatrixe(M) {
3908
- if (M.length !== M[0].length) {
3909
- return;
3876
+ // Checkers
3877
+ get isSquare() {
3878
+ return this.rows === this.cols;
3910
3879
  }
3911
- var i = 0,
3912
- ii = 0,
3913
- j = 0,
3914
- dim = M.length,
3915
- e = 0;
3916
- //t = 0;
3917
- var I = [],
3918
- C = [];
3919
- for (i = 0; i < dim; i += 1) {
3920
- I[I.length] = [];
3921
- C[C.length] = [];
3922
- for (j = 0; j < dim; j += 1) {
3923
- if (i == j) {
3924
- I[i][j] = 1;
3925
- } else {
3926
- I[i][j] = 0;
3880
+ get isSym() {
3881
+ if (!this.isSquare) return false;
3882
+ for (let i = 0; i < this.rows; i++) {
3883
+ for (let j = i + 1; j < this.cols; j++) {
3884
+ if (this.arr[i][j] !== this.arr[j][i]) return false;
3927
3885
  }
3928
- C[i][j] = M[i][j];
3929
3886
  }
3887
+ return true;
3930
3888
  }
3931
- for (i = 0; i < dim; i += 1) {
3932
- e = C[i][i];
3933
- if (e == 0) {
3934
- for (ii = i + 1; ii < dim; ii += 1) {
3935
- if (C[ii][i] != 0) {
3936
- for (j = 0; j < dim; j++) {
3937
- e = C[i][j];
3938
- C[i][j] = C[ii][j];
3939
- C[ii][j] = e;
3940
- e = I[i][j];
3941
- I[i][j] = I[ii][j];
3942
- I[ii][j] = e;
3943
- }
3944
- break;
3945
- }
3946
- }
3947
- e = C[i][i];
3948
- if (e == 0) {
3949
- return;
3889
+ get isAntiSym() {
3890
+ if (!this.isSquare) return false;
3891
+ const n = this.rows;
3892
+ for (let i = 0; i < n; i++) {
3893
+ if (this.arr[i][i] !== 0) return false;
3894
+ for (let j = i + 1; j < n; j++) {
3895
+ if (this.arr[i][j] !== -this.arr[j][i]) return false;
3950
3896
  }
3951
3897
  }
3952
- for (j = 0; j < dim; j++) {
3953
- C[i][j] = C[i][j] / e;
3954
- I[i][j] = I[i][j] / e;
3955
- }
3956
- for (ii = 0; ii < dim; ii++) {
3957
- if (ii == i) {
3958
- continue;
3959
- }
3960
- e = C[ii][i];
3961
- for (j = 0; j < dim; j++) {
3962
- C[ii][j] -= e * C[i][j];
3963
- I[ii][j] -= e * I[i][j];
3898
+ return true;
3899
+ }
3900
+ get isDiag() {
3901
+ if (!this.isSquare) return false;
3902
+ const n = this.rows;
3903
+ for (let i = 0; i < n; i++) {
3904
+ for (let j = i + 1; j < n; j++) {
3905
+ if (this.arr[i][j] !== 0 || this.arr[j][i] !== 0) return false;
3964
3906
  }
3965
3907
  }
3908
+ return true;
3966
3909
  }
3967
- return I;
3968
- }
3969
- /**
3970
- * @returns {Matrix}
3971
- */
3972
- const matrix=(r, c, element)=>new Matrix(r, c, element);
3973
- const matrix2=(...element)=>new Matrix(2, 2, element);
3974
- const matrix3=(...element)=>new Matrix(3, 3, element);
3975
- const matrix4=(...element)=>new Matrix(4, 4, element);
3976
-
3977
- class Complex{
3978
- constructor(a = 0, b = 0) {
3979
- if(a instanceof Complex){
3980
- this.a=a.a;
3981
- this.b=a.b;
3982
- }
3983
- else if(typeof(a)==="object"){
3984
- if(("a" in a && "b" in a)){
3985
- this.a=a.a;
3986
- this.b=a.b;
3987
- }
3988
- else if(("a" in a && "z" in a)){
3989
- this.a=a.a;
3990
- this.b=sqrt$2((a.z**2)-(a.a**2));
3991
- }
3992
- else if(("a" in a && "phi" in a)){
3993
- this.a=a.a;
3994
- this.b=a.a*tan(a.phi);
3995
- }
3996
- else if(("b" in a && "z" in a)){
3997
- this.b=a.b;
3998
- this.a=sqrt$2((a.z**2)-(a.b**2));
3910
+ get isOrtho() {
3911
+ if (!this.isSquare) return false;
3912
+ return this.isDiag && (this.det == 1 || this.det == -1);
3913
+ }
3914
+ get isIdemp() {
3915
+ if (!this.isSquare) return false;
3916
+ const n = this.rows;
3917
+ const A = this.arr;
3918
+ // Compute A * A
3919
+ const MM = [];
3920
+ for (let i = 0; i < n; i++) {
3921
+ MM[i] = [];
3922
+ for (let j = 0; j < n; j++) {
3923
+ let sum = 0;
3924
+ for (let k = 0; k < n; k++) {
3925
+ sum += A[i][k] * A[k][j];
3926
+ }
3927
+ MM[i][j] = sum;
3999
3928
  }
4000
- else if(("b" in a && "phi" in a)){
4001
- this.b=b;
4002
- this.a=a.b/tan(a.phi);
3929
+ }
3930
+ // Check if A * A == A
3931
+ for (let i = 0; i < n; i++) {
3932
+ for (let j = 0; j < n; j++) {
3933
+ if (MM[i][j] !== A[i][j]) return false;
4003
3934
  }
4004
- else if(("z" in a && "phi" in a)){
4005
- this.a=a.z*cos$2(a.phi);
4006
- this.a=a.z*sin$2(a.phi);
3935
+ }
3936
+ return true;
3937
+ }
3938
+
3939
+ get isUpperTri() {
3940
+ if (!this.isSquare) return false;
3941
+ const n = this.rows;
3942
+ for (let i = 1; i < n; i++) {
3943
+ for (let j = 0; j < i; j++) {
3944
+ if (this.arr[i][j] !== 0) return false;
4007
3945
  }
4008
3946
  }
4009
- else if(typeof(a)==="number"&&typeof(b)==="number"){
4010
- this.a = +a.toFixed(32);
4011
- this.b = +b.toFixed(32);
3947
+ return true;
3948
+ }
3949
+ get isLowerTri() {
3950
+ if (!this.isSquare) return false;
3951
+ const n = this.rows;
3952
+ for (let i = 0; i < n - 1; i++) {
3953
+ for (let j = i + 1; j < n; j++) {
3954
+ if (this.arr[i][j] !== 0) return false;
3955
+ }
4012
3956
  }
3957
+ return true;
4013
3958
  }
4014
- isComplex(){
4015
- return true
3959
+
3960
+ // static get rand(){
3961
+ // return {
3962
+ // int:(rows, cols, a, b)=>{
3963
+ // let result = new Matrix(rows, cols);
3964
+ // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randInt(a, b);
3965
+ // return result;
3966
+ // },
3967
+ // bin:(rows,cols)=>{
3968
+ // let result = new Matrix(rows, cols);
3969
+ // for (let i = 0; i < rows; i++) {
3970
+ // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randBin;
3971
+ // }
3972
+ // return result;
3973
+ // },
3974
+ // hex:(rows,cols)=>{
3975
+ // let result = new Matrix(rows, cols);
3976
+ // for (let i = 0; i < rows; i++) {
3977
+ // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randHex;
3978
+ // }
3979
+ // return result;
3980
+ // },
3981
+ // choices:(rows, cols, choices, p)=>{
3982
+ // let result = new Matrix(rows, cols);
3983
+ // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.choice(choices, p);
3984
+ // return result
3985
+ // },
3986
+ // permutation:(rows,cols,arr)=>{
3987
+ // //return new Matrix(rows, cols, Random.permutation(...arr))
3988
+ // }
3989
+ // }
3990
+ // }
3991
+ // static rands(rows, cols, a = 1, b) {
3992
+ // let result = new Matrix(rows, cols);
3993
+ // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.rand(a, b);
3994
+ // return result;
3995
+ // }
3996
+ map(Imin, Imax, Fmin, Fmax) {
3997
+ this.arr = map$1(this.arr, Imin, Imax, Fmin, Fmax);
3998
+ return this;
4016
3999
  }
4017
- toString(){
4018
- let str = "";
4019
- if (this.a !== 0)
4020
- this.b >= 0
4021
- ? (str = `${this.a}+${this.b}*i`)
4022
- : (str = `${this.a}-${Math.abs(this.b)}*i`);
4023
- else
4024
- this.b >= 0
4025
- ? (str = `${this.b}*i`)
4026
- : (str = `-${Math.abs(this.b)}*i`);
4027
- return str;
4000
+ lerp(min, max) {
4001
+ this.arr = lerp(this.arr, min, max);
4002
+ return this;
4028
4003
  }
4029
-
4030
- get clone() {
4031
- return new Complex(this.a, this.b);
4004
+ norm(min, max) {
4005
+ this.arr = norm(this.arr, min, max);
4006
+ return this;
4032
4007
  }
4033
- get z(){
4034
- return hypot(this.a,this.b);
4008
+ clamp(min, max) {
4009
+ this.arr = clamp(this.arr, min, max);
4010
+ return this;
4035
4011
  }
4036
- get phi(){
4037
- return atan2(this.b , this.a);
4012
+ static map(M, Imin, Imax, Fmin, Fmax) {
4013
+ return M.clone().map(Imin, Imax, Fmin, Fmax)
4038
4014
  }
4039
- static Zero() {
4040
- return new Complex(0, 0);
4015
+ static lerp(M, min, max) {
4016
+ return M.clone().lerp(min, max)
4041
4017
  }
4042
- get conj() {
4043
- return new Complex(this.a, -this.b);
4018
+ static norm(M, min, max) {
4019
+ return M.clone().norm(min, max)
4044
4020
  }
4045
- get inv() {
4046
- return new Complex(this.a / (pow$1(this.a, 2) + pow$1(this.b, 2)), -this.b / (pow$1(this.a, 2) + pow$1(this.b, 2)));
4021
+ static clamp(M, min, max) {
4022
+ return M.clone().clamp(min, max)
4047
4023
  }
4048
- add(...z) {
4049
- for (let i = 0; i < z.length; i++) {
4050
- if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
4051
- }
4052
- let re = z.map((n) => n.a);
4053
- let im = z.map((n) => n.b);
4054
- this.a+=+sum(...re).toFixed(15);
4055
- this.b+=+sum(...im).toFixed(15);
4024
+ toPrecision(p) {
4025
+ for (let i = 0; i < this.cols; i++)
4026
+ for (let j = 0; j < this.rows; j++)
4027
+ this.arr[i][j] = +this.arr[i][j].toPrecision(p);
4056
4028
  return this;
4057
4029
  }
4058
- sub(...z) {
4059
- for (let i = 0; i < z.length; i++) {
4060
- if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
4061
- }
4062
- let re = z.map((n) => n.a);
4063
- let im = z.map((n) => n.b);
4064
- this.a-=+sum(...re).toFixed(15);
4065
- this.b-=+sum(...im).toFixed(15);
4066
- return this;
4030
+ // get toBin() {
4031
+ // let newArr = this.arr.flat(1).toBin;
4032
+ // return new Matrix(this.rows, this.cols, newArr);
4033
+ // }
4034
+ // get toOct() {
4035
+ // let newArr = this.arr.flat(1).toOct;
4036
+ // return new Matrix(this.rows, this.cols, newArr);
4037
+ // }
4038
+ // get toHex() {
4039
+ // let newArr = this.arr.flat(1).toHex;
4040
+ // return new Matrix(this.rows, this.cols, newArr);
4041
+ // }
4042
+ /*get isOdd() {
4043
+ let newArr = this.arr.flat(1).isOdd;
4044
+ return new Matrix(this.rows, this.cols, newArr);
4045
+ }*/
4046
+ max2min() {
4047
+ let newArr = this.arr.flat(1).max2min;
4048
+ return new Matrix(this.rows, this.cols, newArr);
4067
4049
  }
4068
- mul(...z){
4069
- for (let i = 0; i < z.length; i++) {
4070
- if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
4050
+ min2max() {
4051
+ let newArr = this.arr.flat(1).min2max;
4052
+ return new Matrix(this.rows, this.cols, newArr);
4053
+ }
4054
+ sortRows(calback=undefined){
4055
+ let newArr=this.arr.map(n=>n.sort(calback)).flat(1);
4056
+ return new Matrix(this.rows, this.cols, newArr);
4057
+ }
4058
+ sortCols(calback=undefined){
4059
+ let m=this.T;
4060
+ let newArr=m.arr.map(n=>n.sort(calback)).flat(1);
4061
+ return new Matrix(this.rows, this.cols, newArr).T;
4062
+ }
4063
+ filterByRows(item){
4064
+ var truth=this.arr.map(n=>n.map(m=>+(""+m).includes(item)));
4065
+ var mask=truth.map(n=>!!Logic.or(...n));
4066
+ var filtredArray=this.arr.filter((n,i)=>mask[i]===true);
4067
+ if(filtredArray.length===0)filtredArray.push([]);
4068
+ console.log(filtredArray);
4069
+ return new Matrix(filtredArray)
4070
+ }
4071
+ filterByCols(item){
4072
+ return new Matrix(this.T.arr.filter(n=>n.includes(item)))
4073
+ }
4074
+ sortAll(calback=undefined){
4075
+ let newArr=this.arr.flat(1).sort(calback);
4076
+ return new Matrix(this.rows, this.cols, newArr);
4077
+ }
4078
+ count(n) {
4079
+ return this.arr.flat(1).count(n);
4080
+ }
4081
+ // toBase(n) {
4082
+ // let newArr = this.arr.flat(1).toBase(n);
4083
+ // return new Matrix(this.rows, this.cols, newArr);
4084
+ // }
4085
+
4086
+ splice(r0,c0,deleteCount,...items){
4087
+
4088
+ }
4089
+ getRows(ri, rf = ri + 1) {
4090
+ return this.slice(ri, 0, rf, this.cols);
4091
+ }
4092
+ getCols(ci, cf = ci + 1) {
4093
+ return this.slice(0, ci, this.rows, cf);
4094
+ }
4095
+ static getRows(m, ri, rf = ri + 1) {
4096
+ return m.slice(ri, 0, rf, m.cols);
4097
+ }
4098
+ static getCols(m, ci, cf = ci + 1) {
4099
+ return m.slice(0, ci, m.rows, cf);
4100
+ }
4101
+ add(...matr) {
4102
+ for (let k = 0; k < matr.length; k++) {
4103
+ if (typeof matr[k] == "number"||matr[k] instanceof Complex) matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
4104
+ 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]);
4071
4105
  }
4072
- let Z=+prod(this.z,...z.map(n=>n.z)).toFixed(15);
4073
- let phi=+sum(this.phi,...z.map(n=>n.phi)).toFixed(15);
4074
- this.a=+(Z*cos$2(phi).toFixed(15)).toFixed(14);
4075
- this.b=+(Z*sin$2(phi).toFixed(15)).toFixed(14);
4076
- return this;
4106
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
4107
+ }
4108
+ sub(...matr) {
4109
+ for (let k = 0; k < matr.length; k++) {
4110
+ if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
4111
+ 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]);
4112
+ }
4113
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
4077
4114
  }
4078
- div(...z) {
4079
- for (let i = 0; i < z.length; i++) {
4080
- if (typeof z[i] === "number") z[i] = new Complex(z[i], 0);
4115
+ static add(m1, ...m2) {
4116
+ return m1.clone().add(...m2);
4117
+ }
4118
+ static sub(m1, ...m2) {
4119
+ return m1.clone().sub(...m2);
4120
+ }
4121
+ mul(...matr) {
4122
+ for (let k = 0; k < matr.length; k++) {
4123
+ if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
4124
+ 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]);
4081
4125
  }
4082
- let Z=+(this.z/prod(...z.map(n=>n.z))).toFixed(15);
4083
- let phi=+(this.phi-sum(...z.map(n=>n.phi))).toFixed(15);
4084
- this.a=+(Z*cos$2(phi).toFixed(15)).toFixed(15);
4085
- this.b=+(Z*sin$2(phi).toFixed(15)).toFixed(15);
4086
- return this;
4126
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
4087
4127
  }
4088
- pow(n) {
4089
- if (floor(n) === n && n > 0) {
4090
- let z=+(this.z**n).toFixed(15);
4091
- let phi=+(this.phi*n).toFixed(15);
4092
- this.a=+(z*cos$2(phi).toFixed(15)).toFixed(15);
4093
- this.b=+(z*sin$2(phi).toFixed(15)).toFixed(15);
4128
+ div(...matr) {
4129
+ for (let k = 0; k < matr.length; k++) {
4130
+ if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
4131
+ 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]);
4094
4132
  }
4095
- return this;
4133
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
4096
4134
  }
4097
- static fromExpo(z, phi) {
4098
- return new Complex(
4099
- +(z * cos$2(phi)).toFixed(13),
4100
- +(z * sin$2(phi)).toFixed(13)
4101
- );
4135
+ static div(m1, ...m2) {
4136
+ return m1.clone().div(...m2);
4102
4137
  }
4103
- get expo() {
4104
- return [this.z, this.phi];
4138
+ static mul(m1, ...m2) {
4139
+ return m1.clone().mul(...m2);
4105
4140
  }
4106
- static add(c,...z) {
4107
- return c.clone.add(...z);
4141
+ modulo(...matr) {
4142
+ for (let k = 0; k < matr.length; k++) {
4143
+ if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
4144
+ for (let i = 0; i < this.rows; i++)
4145
+ for (var j = 0; j < this.cols; j++)
4146
+ this.arr[i][j]=modulo(this.arr[i][j],matr[k].arr[i][j]);
4147
+ }
4148
+ return new Matrix(this.rows, this.cols, this.arr.flat(1));
4108
4149
  }
4109
- static sub(c,...z) {
4110
- return c.clone.sub(...z);
4150
+ static modulo(m1, ...m2) {
4151
+ return m1.clone().modulo(...m2);
4111
4152
  }
4112
- static mul(c,...z) {
4113
- return c.clone.mul(...z);
4153
+ dot(matrix) {
4154
+ var res = [];
4155
+ for (var i = 0; i < this.arr.length; i++) {
4156
+ res[i] = [];
4157
+ for (var j = 0; j < matrix.arr[0].length; j++) {
4158
+ res[i][j] = 0;
4159
+ for (var k = 0; k < this.arr[0].length; k++) {
4160
+ res[i][j] = add(
4161
+ res[i][j],
4162
+ mul(this.arr[i][k],matrix.arr[k][j])
4163
+ );
4164
+ }
4165
+ }
4166
+ }
4167
+ return new Matrix(this.arr.length, matrix.arr[0].length, res.flat(1));
4114
4168
  }
4115
- static div(c,...z) {
4116
- return c.clone.div(...z);
4169
+ static dot(matrix1, matrix2) {
4170
+ return matrix1.dot(matrix2);
4117
4171
  }
4118
- static pow(z,n){
4119
- return z.clone.pow(n);
4172
+ pow(n) {
4173
+ let a = this.clone(),
4174
+ p = this.clone();
4175
+ for (let i = 0; i < n - 1; i++) p = p.dot(a);
4176
+ return p;
4120
4177
  }
4121
- static xpowZ(x){
4122
- return complex((x**this.a)*cos$2(this.b*ln(x)),(x**this.a)*sin$2(this.b*ln(x)));
4178
+ static pow(m, n) {
4179
+ return m.clone().pow(n);
4123
4180
  }
4124
- sqrtn(n=2){
4125
- return complex(sqrtn(this.z,n)*cos$2(this.phi/n),sqrtn(this.z,n)*sin$2(this.phi/n));
4181
+ get somme() {
4182
+ let S = 0;
4183
+ for (let i = 0; i < this.rows; i++) for (let j = 0; j < this.cols; j++) S += this.arr[i][j];
4184
+ return S;
4126
4185
  }
4127
- get sqrt(){
4128
- return this.sqrtn(2);
4186
+ get hasComplex() {
4187
+ return this.arr.flat(Infinity).some((n) => n instanceof Complex);
4129
4188
  }
4130
- get log(){
4131
- return complex(this.z,this.phi);
4189
+ get min() {
4190
+ if (this.hasComplex) console.error("Complex numbers are not comparable");
4191
+ let minRow = [];
4192
+ for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
4193
+ return min(...minRow);
4132
4194
  }
4133
- get cos(){
4134
- return complex(cos$2(this.a)*cosh$1(this.b),sin$2(this.a)*sinh$1(this.b))
4195
+ get max() {
4196
+ if (this.hasComplex) console.error("Complex numbers are not comparable");
4197
+ let maxRow = [];
4198
+ for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
4199
+ return max(...maxRow);
4135
4200
  }
4136
- get sin(){
4137
- return complex(sin$2(this.a)*cosh$1(this.b),cos$2(this.a)*sinh$1(this.b))
4201
+ get minRows() {
4202
+ if (this.hasComplex) console.error("Complex numbers are not comparable");
4203
+ let minRow = [];
4204
+ for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
4205
+ return minRow;
4138
4206
  }
4139
- get tan(){
4140
- const de=cos$2(this.a*2)+cosh$1(this.b*2);
4141
- return complex(sin$2(2*this.a)/de,sinh$1(2*this.b)/de);
4207
+ get maxRows() {
4208
+ if (this.hasComplex) console.error("Complex numbers are not comparable");
4209
+ let maxRow = [];
4210
+ for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
4211
+ return maxRow;
4142
4212
  }
4143
- }
4144
- const complex=(a,b)=>{
4145
- if((a instanceof Array||ArrayBuffer.isView(a)) && (b instanceof Array||ArrayBuffer.isView(a)))return a.map((n,i)=>complex(a[i],b[i]));
4146
- if(a instanceof Matrix && b instanceof Matrix){
4147
- if((a.shape[0]!==b.shape[0])||(a.shape[1]!==b.shape[1]))return Error(0)
4148
- const arr=a.arr.map((n,i)=>complex(a.arr[i],b.arr[i]));
4149
- return new Matrix(a.rows,a.cols,...arr)
4213
+ get minCols() {
4214
+ if (this.hasComplex) console.error("Complex numbers are not comparable");
4215
+ return this.T.minRows;
4150
4216
  }
4151
- return new Complex(a,b)
4152
- };
4153
-
4154
- const abs=(...x)=>mapfun$1(Math.abs,...x);
4155
- const sqrt$2=(...x)=>mapfun$1(Math.sqrt,...x);
4156
- const pow$1=(x,n)=>{
4157
- if(typeof x === "number"){
4158
- if(typeof n === "number")return Math.pow(x,n);
4159
- else if(n instanceof Complex)return Complex.fromExpo(x**n.a,n.b*ln(x))
4160
- else return mapfun$1(a=>pow$1(x,a),...n);
4217
+ get maxCols() {
4218
+ if (this.hasComplex) console.error("Complex numbers are not comparable");
4219
+ return this.T.maxRows;
4161
4220
  }
4162
- else if(x instanceof Complex){
4163
- if(typeof n === "number")return Complex.fromExpo(x.z**n,x.phi*n);
4164
- else if(n instanceof Complex)return Complex.fromExpo(
4165
- x.z**n.a*e(-x.phi*n.b),
4166
- ln(x.z)*n.b+n.a*x.phi
4167
- )
4168
- else return mapfun$1(a=>pow$1(x,a),...n);
4221
+ static fromVector(v) {
4222
+ return new Matrix(v.length, 1, v);
4169
4223
  }
4170
- else if(x instanceof Array){
4171
- if(typeof n === "number") return mapfun$1(a=>pow$1(a,n),...x);
4172
- else if(n instanceof Array){
4173
- const Y=[];
4174
- for(let i=0;i<x.length;i++){
4175
- Y.push(mapfun$1(a=>pow$1(x[i],a),...n));
4224
+ get toArray() {
4225
+ let arr = [];
4226
+ for (let i = 0; i < this.rows; i++) {
4227
+ for (let j = 0; j < this.cols; j++) {
4228
+ arr.push(this.arr[i][j]);
4176
4229
  }
4177
- return Y;
4178
4230
  }
4231
+ return arr;
4179
4232
  }
4180
- };
4181
- const sqrtn=(x,n)=>{
4182
- if(typeof x === "number"){
4183
- if(typeof n === "number")return Math.pow(x,1/n);
4184
- else return mapfun$1(a=>sqrtn(x,a),...n);
4233
+ get serialize() {
4234
+ return JSON.stringify(this);
4185
4235
  }
4186
- else if(x instanceof Complex){
4187
- if(typeof n === "number")return Complex.fromExpo(sqrtn(x.z,n),x.phi/n);
4188
- else return mapfun$1(a=>sqrtn(x,a),...n);
4236
+ static deserialize(data) {
4237
+ if (typeof data == "string") data = JSON.parse(data);
4238
+ let matrix = new Matrix(data.rows, data.cols);
4239
+ matrix.arr = data.arr;
4240
+ return matrix;
4189
4241
  }
4190
- else if(x instanceof Array){
4191
- if(typeof n === "number") return mapfun$1(a=>sqrtn(a,n),...x);
4192
- else if(n instanceof Array){
4193
- const Y=[];
4194
- for(let i=0;i<x.length;i++){
4195
- Y.push(mapfun$1(a=>sqrtn(x[i],a),...n));
4242
+ sortTable(n=0,{type="num",order="asc"}={}) {
4243
+ var obj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
4244
+ var newObj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
4245
+ if(type==="num"){
4246
+ if(order==="asc")obj[n].sort((a,b)=>a.x-b.x);
4247
+ else if(order==="desc")obj[n].sort((a,b)=>b.x-a.x);
4248
+ else if(order==="toggle"){
4249
+ // console.log(obj[n][0])
4250
+ //console.log(obj[n][1])
4251
+ if(obj[n][0].x>obj[n][1].x)obj[n].sort((a,b)=>b.x-a.x);
4252
+ else obj[n].sort((a,b)=>a.x-b.x);
4196
4253
  }
4197
- return Y;
4198
4254
  }
4199
- }
4200
- };
4201
- const e=(...x) => mapfun$1(Math.exp,...x);
4202
- const ln=(...x) => mapfun$1(Math.log,...x);
4203
- const cos$2=(...x) => mapfun$1(Fixed.cos,...x);
4204
- const sin$2=(...x) => mapfun$1(Fixed.sin,...x);
4205
- const tan=(...x) => mapfun$1(Fixed.tan,...x);
4206
- const sec=(...x) => mapfun$1(Fixed.sec,...x);
4207
- const sinc=(...x) => mapfun$1(Fixed.sinc,...x);
4208
- const csc=(...x) => mapfun$1(Fixed.csc,...x);
4209
- const cot=(...x) => mapfun$1(Fixed.cot,...x);
4210
- const acos$1=(...x) => mapfun$1(Fixed.acos,...x);
4211
- const asin=(...x) => mapfun$1(Fixed.asin,...x);
4212
- const atan=(...x) => mapfun$1(Fixed.atan,...x);
4213
- const acot=(...x) => mapfun$1(Fixed.acot,...x);
4214
- const cosh$1=(...x) => mapfun$1(Fixed.cosh,...x);
4215
- const sinh$1=(...x) => mapfun$1(Fixed.sinh,...x);
4216
- const tanh=(...x) => mapfun$1(Fixed.tanh,...x);
4217
- const coth=(...x) => mapfun$1(Fixed.coth,...x);
4218
- const acosh=(...x) => mapfun$1(Fixed.acosh,...x);
4219
- const asinh=(...x) => mapfun$1(Fixed.asinh,...x);
4220
- const atanh=(...x) => mapfun$1(Fixed.atanh,...x);
4221
- const ceil=(...x) => mapfun$1(Math.ceil,...x);
4222
- const floor=(...x) => mapfun$1(Math.floor,...x);
4223
- const round=(...x) => mapfun$1(Math.round,...x);
4224
- const atan2=(x,y,rad=true)=>{
4225
- if(typeof x === "number"){
4226
- if(typeof y === "number")return rad?Math.atan2(x,y):Math.atan2(x,y)*180/Math.PI;
4227
- else return mapfun$1(a=>atan2(x,a,rad),...y);
4228
- }
4229
- // else if(x instanceof Complex){
4230
- // if(typeof n === "number")return Complex.fromExpo(x.z**n,x.phi*n);
4231
- // else return mapfun(a=>pow(x,a),...n);
4232
- // }
4233
- else if(x instanceof Array){
4234
- if(typeof y === "number") return mapfun$1(a=>atan2(a,y,rad),...x);
4235
- else if(y instanceof Array){
4236
- const Y=[];
4237
- for(let i=0;i<x.length;i++){
4238
- Y.push(mapfun$1(a=>pow$1(x[i],a),...y));
4239
- }
4240
- return Y;
4255
+ else if(type==="alpha"){
4256
+ if(order==="asc")obj[n].sort((a,b)=>(""+a.x).localeCompare(""+b.x));
4257
+ else if(order==="desc")obj[n].sort((a,b)=>(""+b.x).localeCompare(""+a.x));
4258
+ }
4259
+ //var order=obj[n].map(n=>n.y);
4260
+ order=obj[n].map(n=>n.y);
4261
+ for(let i=0;i<obj.length;i++){
4262
+ if(i!==n)obj[i].map((n,j)=>n.y=order[j]);
4241
4263
  }
4264
+ for(let i=0;i<obj.length;i++){
4265
+ if(i!==n)newObj[i].map((n,j)=>n.x=obj[i][order[j]].x);
4266
+ }
4267
+ newObj[n]=obj[n];
4268
+ var newArr=newObj.map(n=>n.map(m=>m.x));
4269
+ return new Matrix(newArr).T;
4242
4270
  }
4243
- };
4244
- const fact=(...x)=>mapfun$1(n=> {
4245
- let i,
4246
- y = 1;
4247
- if (n == 0) y = 1;
4248
- else if (n > 0) for (i = 1; i <= n; i++) y *= i;
4249
- else y = NaN;
4250
- return y;
4251
- },...x);
4252
- const sign=(...x)=>mapfun$1(Math.sign,...x);
4271
+ }
4253
4272
 
4254
- const sig=(...x)=>mapfun$1(n=>1/(1+e(-n)),...x);
4255
4273
 
4256
- const hypot=(...x)=>{
4257
- if(x.every(n=>typeof n === "number"))return Math.hypot(...x);
4258
- if(x.every(n=>n instanceof Array))return mapfun$1(
4259
- Math.hypot,
4260
- ...x
4261
- )
4262
- };
4274
+ /**
4275
+ * @returns {Matrix}
4276
+ */
4277
+ const matrix=(r, c, element)=>new Matrix(r, c, element);
4278
+ const matrix2=(...element)=>new Matrix(2, 2, element);
4279
+ const matrix3=(...element)=>new Matrix(3, 3, element);
4280
+ const matrix4=(...element)=>new Matrix(4, 4, element);
4263
4281
 
4264
4282
  const { PI, sqrt: sqrt$1, cos: cos$1, sin: sin$1, acos, pow } = Math;
4265
4283