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