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