ziko 0.54.0 → 0.54.2

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.
@@ -1,13 +1,10 @@
1
- export * from './position/index.js';
1
+ export * from './percentile/index.js';
2
2
  export * from './average/index.js';
3
3
  export * from './variability/index.js'
4
4
  export * from './rolling/index.js'
5
5
  export * from './accum/index.js'
6
6
 
7
7
 
8
- // min
9
- // max
10
-
11
8
  // mean
12
9
  // variance
13
10
  // std
@@ -1,6 +1,3 @@
1
- export const min = (...x) => Math.min(...x);
2
- export const max = (...x) => Math.max(...x);
3
-
4
1
  export const percentile = (X, p) => {
5
2
  if (X.length === 0)
6
3
  return NaN;
@@ -38,6 +38,7 @@ export declare class Complex {
38
38
  nrth(n?: number): Complex;
39
39
 
40
40
  static Zero(): Complex;
41
+ static Twuddle(N : number, K : number): Complex;
41
42
  static fromExpo(z: number, phi: number): Complex;
42
43
  static add(c: Complex, ...z: (number | Complex)[]): Complex;
43
44
  static sub(c: Complex, ...z: (number | Complex)[]): Complex;
@@ -1,596 +0,0 @@
1
- import {
2
- add,
3
- sub,
4
- mul,
5
- div,
6
- modulo
7
- } from '../functions/arithmetic/index.js'
8
- import {
9
- map,
10
- lerp,
11
- clamp,
12
- norm
13
- } from '../functions/utils/index.js'
14
- import { Complex } from "../complex/index.js";
15
- import { arr2str } from "../../data/index.js";
16
- import {
17
- matrix_inverse,
18
- matrix_det,
19
- hstack,
20
- vstack
21
- } from "./helpers/index.js";
22
- class Matrix{
23
- constructor(rows, cols, element = [] ) {
24
- if(rows instanceof Matrix){
25
- this.arr=rows.arr;
26
- this.rows=rows.rows;
27
- this.cols=rows.cols;
28
- }
29
- else {
30
- let arr = [], i, j;
31
- if (arguments[0] instanceof Array) {
32
- rows = arguments[0].length;
33
- cols = arguments[0][0].length;
34
- arr = arguments[0];
35
- } else {
36
- for (i = 0; i < rows; i++) {
37
- arr.push([]);
38
- arr[i].push(new Array(cols));
39
- for (j = 0; j < cols; j++) {
40
- arr[i][j] = element[i * cols + j];
41
- if (element[i * cols + j] == undefined) arr[i][j] = 0;
42
- }
43
- }
44
- }
45
- this.rows = rows;
46
- this.cols = cols;
47
- this.arr = arr;
48
- }
49
- this.#maintain();
50
- }
51
- isMatrix(){
52
- return true
53
- }
54
- clone() {
55
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
56
- }
57
- [Symbol.iterator]() {
58
- return this.arr[Symbol.iterator]();
59
- }
60
- #maintain() {
61
- for (let i = 0; i < this.arr.length; i++) {
62
- Object.defineProperty(this, i, {
63
- value: this.arr[i],
64
- writable: true,
65
- configurable: true,
66
- enumerable: false
67
- });
68
- }
69
- }
70
- get size() {
71
- return this.rows * this.cols;
72
- }
73
- get shape() {
74
- return [this.rows, this.cols];
75
- }
76
- toString(){
77
- return arr2str(this.arr,false);
78
- }
79
- at(i = 0, j = undefined) {
80
- if (i < 0) i += this.rows;
81
- if (i < 0 || i >= this.rows) throw new Error('Row index out of bounds');
82
- if (j === undefined) return this.arr[i];
83
- if (j < 0) j += this.cols;
84
- if (j < 0 || j >= this.cols) throw new Error('Column index out of bounds');
85
- return this.arr[i][j];
86
- }
87
- slice(r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
88
- let newRow = r1 - r0,
89
- newCol = c1 - c0;
90
- let newArr = new Array(newCol);
91
- for (let i = 0; i < newRow; i++) {
92
- newArr[i] = [];
93
- for (let j = 0; j < newCol; j++) newArr[i][j] = this.arr[i + r0][j + c0];
94
- }
95
- return new Matrix(newRow, newCol, newArr.flat(1));
96
- }
97
- static slice(m1,r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
98
- return m1.slice(r0, c0, r1, c1);
99
- }
100
- reshape(newRows, newCols) {
101
- if(!(newRows * newCols === this.rows * this.cols)) throw Error('size not matched')
102
- return new Matrix(newRows, newCols, this.arr.flat(1));
103
- }
104
- get T() {
105
- let transpose = [];
106
- for (let i = 0; i < this.arr[0].length; i++) {
107
- transpose[i] = [];
108
- for (let j = 0; j < this.arr.length; j++)
109
- transpose[i][j] = this.arr[j][i];
110
- }
111
- return new Matrix(this.cols, this.rows, transpose.flat(1));
112
- }
113
- get det() {
114
- return matrix_det(this)
115
- }
116
- get inv() {
117
- return matrix_inverse(this)
118
- }
119
- static eye(size) {
120
- let result = new Matrix(size, size);
121
- for (let i = 0; i < size; i++)
122
- for (let j = 0; j < size; j++) i === j ? (result.arr[i][j] = 1) : (result.arr[i][j] = 0);
123
- return result;
124
- }
125
- static zeros(rows, cols) {
126
- let result = new Matrix(rows, cols);
127
- for (let i = 0; i < rows; i++)
128
- for (var j = 0; j < cols; j++) result.arr[i][j] = 0;
129
- return result;
130
- }
131
- static ones(rows, cols) {
132
- let result = new Matrix(rows, cols);
133
- for (let i = 0; i < rows; i++)
134
- for (let j = 0; j < cols; j++) result.arr[i][j] = 1;
135
- return result;
136
- }
137
- static nums(rows, cols, number) {
138
- let result = new Matrix(rows, cols);
139
- for (let i = 0; i < rows; i++)
140
- for (let j = 0; j < cols; j++) result.arr[i][j] = number;
141
- return result;
142
- }
143
- hstack(...matrices) {
144
- const M=[this, ...matrices].reduce((a,b)=>hstack(a, b));
145
- Object.assign(this, M);
146
- this.#maintain();
147
- return this;
148
- }
149
- static hstack(matrix,...matrices) {
150
- return matrix.clone().hstack(...matrices);
151
- }
152
- vstack(...matrices){
153
- const M=[this, ...matrices].reduce((a,b)=>vstack(a, b));
154
- Object.assign(this, M);
155
- this.#maintain();
156
- return this;
157
- }
158
- static vstack(matrix,...matrices) {
159
- return matrix.clone().vstack(...matrices);
160
- }
161
- hqueue(...matrices){
162
- const M=[this, ...matrices].reverse().reduce((a,b)=>hstack(a, b));
163
- Object.assign(this, M)
164
- return this;
165
- }
166
- static hqueue(matrix,...matrices) {
167
- return matrix.clone().hqueue(...matrices);
168
- }
169
- vqueue(...matrices){
170
- const M=[this,...matrices].reverse().reduce((a, b)=>vstack(a, b));
171
- Object.assign(this, M)
172
- return this;
173
- }
174
- static vqueue(matrix,...matrices) {
175
- return matrix.clone().vqueue(...matrices);
176
- }
177
- shuffle(){
178
- this.arr = this.arr.sort(()=>0.5-Math.random());
179
- return this;
180
- }
181
- static shuffle(M){
182
- return M.clone().shuffle()
183
- }
184
- // get reel() {
185
- // return new Matrix(this.cols, this.rows, this.arr.flat(1).reel);
186
- // }
187
- // get imag() {
188
- // return new Matrix(this.cols, this.rows, this.arr.flat(1).imag);
189
- // }
190
-
191
- // Checkers
192
- get isSquare() {
193
- return this.rows === this.cols;
194
- }
195
- get isSym() {
196
- if (!this.isSquare) return false;
197
- for (let i = 0; i < this.rows; i++) {
198
- for (let j = i + 1; j < this.cols; j++) {
199
- if (this.arr[i][j] !== this.arr[j][i]) return false;
200
- }
201
- }
202
- return true;
203
- }
204
- get isAntiSym() {
205
- if (!this.isSquare) return false;
206
- const n = this.rows;
207
- for (let i = 0; i < n; i++) {
208
- if (this.arr[i][i] !== 0) return false;
209
- for (let j = i + 1; j < n; j++) {
210
- if (this.arr[i][j] !== -this.arr[j][i]) return false;
211
- }
212
- }
213
- return true;
214
- }
215
- get isDiag() {
216
- if (!this.isSquare) return false;
217
- const n = this.rows;
218
- for (let i = 0; i < n; i++) {
219
- for (let j = i + 1; j < n; j++) {
220
- if (this.arr[i][j] !== 0 || this.arr[j][i] !== 0) return false;
221
- }
222
- }
223
- return true;
224
- }
225
- get isOrtho() {
226
- if (!this.isSquare) return false;
227
- return this.isDiag && (this.det == 1 || this.det == -1);
228
- }
229
- get isIdemp() {
230
- if (!this.isSquare) return false;
231
- const n = this.rows;
232
- const A = this.arr;
233
- // Compute A * A
234
- const MM = [];
235
- for (let i = 0; i < n; i++) {
236
- MM[i] = [];
237
- for (let j = 0; j < n; j++) {
238
- let sum = 0;
239
- for (let k = 0; k < n; k++) {
240
- sum += A[i][k] * A[k][j];
241
- }
242
- MM[i][j] = sum;
243
- }
244
- }
245
- // Check if A * A == A
246
- for (let i = 0; i < n; i++) {
247
- for (let j = 0; j < n; j++) {
248
- if (MM[i][j] !== A[i][j]) return false;
249
- }
250
- }
251
- return true;
252
- }
253
-
254
- get isUpperTri() {
255
- if (!this.isSquare) return false;
256
- const n = this.rows;
257
- for (let i = 1; i < n; i++) {
258
- for (let j = 0; j < i; j++) {
259
- if (this.arr[i][j] !== 0) return false;
260
- }
261
- }
262
- return true;
263
- }
264
- get isLowerTri() {
265
- if (!this.isSquare) return false;
266
- const n = this.rows;
267
- for (let i = 0; i < n - 1; i++) {
268
- for (let j = i + 1; j < n; j++) {
269
- if (this.arr[i][j] !== 0) return false;
270
- }
271
- }
272
- return true;
273
- }
274
-
275
- // static get rand(){
276
- // return {
277
- // int:(rows, cols, a, b)=>{
278
- // let result = new Matrix(rows, cols);
279
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randInt(a, b);
280
- // return result;
281
- // },
282
- // bin:(rows,cols)=>{
283
- // let result = new Matrix(rows, cols);
284
- // for (let i = 0; i < rows; i++) {
285
- // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randBin;
286
- // }
287
- // return result;
288
- // },
289
- // hex:(rows,cols)=>{
290
- // let result = new Matrix(rows, cols);
291
- // for (let i = 0; i < rows; i++) {
292
- // for (let j = 0; j < cols; j++) result.arr[i][j] = Random.randHex;
293
- // }
294
- // return result;
295
- // },
296
- // choices:(rows, cols, choices, p)=>{
297
- // let result = new Matrix(rows, cols);
298
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.choice(choices, p);
299
- // return result
300
- // },
301
- // permutation:(rows,cols,arr)=>{
302
- // //return new Matrix(rows, cols, Random.permutation(...arr))
303
- // }
304
- // }
305
- // }
306
- // static rands(rows, cols, a = 1, b) {
307
- // let result = new Matrix(rows, cols);
308
- // for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) result.arr[i][j] = Random.rand(a, b);
309
- // return result;
310
- // }
311
- map(Imin, Imax, Fmin, Fmax) {
312
- this.arr = map(this.arr, Imin, Imax, Fmin, Fmax)
313
- return this;
314
- }
315
- lerp(min, max) {
316
- this.arr = lerp(this.arr, min, max)
317
- return this;
318
- }
319
- norm(min, max) {
320
- this.arr = norm(this.arr, min, max)
321
- return this;
322
- }
323
- clamp(min, max) {
324
- this.arr = clamp(this.arr, min, max)
325
- return this;
326
- }
327
- static map(M, Imin, Imax, Fmin, Fmax) {
328
- return M.clone().map(Imin, Imax, Fmin, Fmax)
329
- }
330
- static lerp(M, min, max) {
331
- return M.clone().lerp(min, max)
332
- }
333
- static norm(M, min, max) {
334
- return M.clone().norm(min, max)
335
- }
336
- static clamp(M, min, max) {
337
- return M.clone().clamp(min, max)
338
- }
339
- toPrecision(p) {
340
- for (let i = 0; i < this.cols; i++)
341
- for (let j = 0; j < this.rows; j++)
342
- this.arr[i][j] = +this.arr[i][j].toPrecision(p);
343
- return this;
344
- }
345
- // get toBin() {
346
- // let newArr = this.arr.flat(1).toBin;
347
- // return new Matrix(this.rows, this.cols, newArr);
348
- // }
349
- // get toOct() {
350
- // let newArr = this.arr.flat(1).toOct;
351
- // return new Matrix(this.rows, this.cols, newArr);
352
- // }
353
- // get toHex() {
354
- // let newArr = this.arr.flat(1).toHex;
355
- // return new Matrix(this.rows, this.cols, newArr);
356
- // }
357
- /*get isOdd() {
358
- let newArr = this.arr.flat(1).isOdd;
359
- return new Matrix(this.rows, this.cols, newArr);
360
- }*/
361
- max2min() {
362
- let newArr = this.arr.flat(1).max2min;
363
- return new Matrix(this.rows, this.cols, newArr);
364
- }
365
- min2max() {
366
- let newArr = this.arr.flat(1).min2max;
367
- return new Matrix(this.rows, this.cols, newArr);
368
- }
369
- sortRows(calback=undefined){
370
- let newArr=this.arr.map(n=>n.sort(calback)).flat(1);
371
- return new Matrix(this.rows, this.cols, newArr);
372
- }
373
- sortCols(calback=undefined){
374
- let m=this.T;
375
- let newArr=m.arr.map(n=>n.sort(calback)).flat(1);
376
- return new Matrix(this.rows, this.cols, newArr).T;
377
- }
378
- filterByRows(item){
379
- var truth=this.arr.map(n=>n.map(m=>+(""+m).includes(item)))
380
- var mask=truth.map(n=>!!Logic.or(...n))
381
- var filtredArray=this.arr.filter((n,i)=>mask[i]===true)
382
- if(filtredArray.length===0)filtredArray.push([])
383
- console.log(filtredArray)
384
- return new Matrix(filtredArray)
385
- }
386
- filterByCols(item){
387
- return new Matrix(this.T.arr.filter(n=>n.includes(item)))
388
- }
389
- sortAll(calback=undefined){
390
- let newArr=this.arr.flat(1).sort(calback);
391
- return new Matrix(this.rows, this.cols, newArr);
392
- }
393
- count(n) {
394
- return this.arr.flat(1).count(n);
395
- }
396
- // toBase(n) {
397
- // let newArr = this.arr.flat(1).toBase(n);
398
- // return new Matrix(this.rows, this.cols, newArr);
399
- // }
400
-
401
- splice(r0,c0,deleteCount,...items){
402
-
403
- }
404
- getRows(ri, rf = ri + 1) {
405
- return this.slice(ri, 0, rf, this.cols);
406
- }
407
- getCols(ci, cf = ci + 1) {
408
- return this.slice(0, ci, this.rows, cf);
409
- }
410
- static getRows(m, ri, rf = ri + 1) {
411
- return m.slice(ri, 0, rf, m.cols);
412
- }
413
- static getCols(m, ci, cf = ci + 1) {
414
- return m.slice(0, ci, m.rows, cf);
415
- }
416
- add(...matr) {
417
- for (let k = 0; k < matr.length; k++) {
418
- if (typeof matr[k] == "number"||matr[k] instanceof Complex) matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
419
- 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]);
420
- }
421
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
422
- }
423
- sub(...matr) {
424
- for (let k = 0; k < matr.length; k++) {
425
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
426
- 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]);
427
- }
428
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
429
- }
430
- static add(m1, ...m2) {
431
- return m1.clone().add(...m2);
432
- }
433
- static sub(m1, ...m2) {
434
- return m1.clone().sub(...m2);
435
- }
436
- mul(...matr) {
437
- for (let k = 0; k < matr.length; k++) {
438
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
439
- 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]);
440
- }
441
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
442
- }
443
- div(...matr) {
444
- for (let k = 0; k < matr.length; k++) {
445
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
446
- 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]);
447
- }
448
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
449
- }
450
- static div(m1, ...m2) {
451
- return m1.clone().div(...m2);
452
- }
453
- static mul(m1, ...m2) {
454
- return m1.clone().mul(...m2);
455
- }
456
- modulo(...matr) {
457
- for (let k = 0; k < matr.length; k++) {
458
- if (typeof matr[k] == "number") matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
459
- for (let i = 0; i < this.rows; i++)
460
- for (var j = 0; j < this.cols; j++)
461
- this.arr[i][j]=modulo(this.arr[i][j],matr[k].arr[i][j]);
462
- }
463
- return new Matrix(this.rows, this.cols, this.arr.flat(1));
464
- }
465
- static modulo(m1, ...m2) {
466
- return m1.clone().modulo(...m2);
467
- }
468
- dot(matrix) {
469
- var res = [];
470
- for (var i = 0; i < this.arr.length; i++) {
471
- res[i] = [];
472
- for (var j = 0; j < matrix.arr[0].length; j++) {
473
- res[i][j] = 0;
474
- for (var k = 0; k < this.arr[0].length; k++) {
475
- res[i][j] = add(
476
- res[i][j],
477
- mul(this.arr[i][k],matrix.arr[k][j])
478
- )
479
- }
480
- }
481
- }
482
- return new Matrix(this.arr.length, matrix.arr[0].length, res.flat(1));
483
- }
484
- static dot(matrix1, matrix2) {
485
- return matrix1.dot(matrix2);
486
- }
487
- pow(n) {
488
- let a = this.clone(),
489
- p = this.clone();
490
- for (let i = 0; i < n - 1; i++) p = p.dot(a);
491
- return p;
492
- }
493
- static pow(m, n) {
494
- return m.clone().pow(n);
495
- }
496
- get somme() {
497
- let S = 0;
498
- for (let i = 0; i < this.rows; i++) for (let j = 0; j < this.cols; j++) S += this.arr[i][j];
499
- return S;
500
- }
501
- get hasComplex() {
502
- return this.arr.flat(Infinity).some((n) => n instanceof Complex);
503
- }
504
- get min() {
505
- if (this.hasComplex) console.error("Complex numbers are not comparable");
506
- let minRow = [];
507
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
508
- return min(...minRow);
509
- }
510
- get max() {
511
- if (this.hasComplex) console.error("Complex numbers are not comparable");
512
- let maxRow = [];
513
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
514
- return max(...maxRow);
515
- }
516
- get minRows() {
517
- if (this.hasComplex) console.error("Complex numbers are not comparable");
518
- let minRow = [];
519
- for (let i = 0; i < this.rows; i++) minRow.push(min(...this.arr[i]));
520
- return minRow;
521
- }
522
- get maxRows() {
523
- if (this.hasComplex) console.error("Complex numbers are not comparable");
524
- let maxRow = [];
525
- for (let i = 0; i < this.rows; i++) maxRow.push(max(...this.arr[i]));
526
- return maxRow;
527
- }
528
- get minCols() {
529
- if (this.hasComplex) console.error("Complex numbers are not comparable");
530
- return this.T.minRows;
531
- }
532
- get maxCols() {
533
- if (this.hasComplex) console.error("Complex numbers are not comparable");
534
- return this.T.maxRows;
535
- }
536
- static fromVector(v) {
537
- return new Matrix(v.length, 1, v);
538
- }
539
- get toArray() {
540
- let arr = [];
541
- for (let i = 0; i < this.rows; i++) {
542
- for (let j = 0; j < this.cols; j++) {
543
- arr.push(this.arr[i][j]);
544
- }
545
- }
546
- return arr;
547
- }
548
- get serialize() {
549
- return JSON.stringify(this);
550
- }
551
- static deserialize(data) {
552
- if (typeof data == "string") data = JSON.parse(data);
553
- let matrix = new Matrix(data.rows, data.cols);
554
- matrix.arr = data.arr;
555
- return matrix;
556
- }
557
- sortTable(n=0,{type="num",order="asc"}={}) {
558
- var obj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
559
- var newObj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
560
- if(type==="num"){
561
- if(order==="asc")obj[n].sort((a,b)=>a.x-b.x);
562
- else if(order==="desc")obj[n].sort((a,b)=>b.x-a.x);
563
- else if(order==="toggle"){
564
- // console.log(obj[n][0])
565
- //console.log(obj[n][1])
566
- if(obj[n][0].x>obj[n][1].x)obj[n].sort((a,b)=>b.x-a.x);
567
- else obj[n].sort((a,b)=>a.x-b.x);
568
- }
569
- }
570
- else if(type==="alpha"){
571
- if(order==="asc")obj[n].sort((a,b)=>(""+a.x).localeCompare(""+b.x));
572
- else if(order==="desc")obj[n].sort((a,b)=>(""+b.x).localeCompare(""+a.x));
573
- }
574
- //var order=obj[n].map(n=>n.y);
575
- order=obj[n].map(n=>n.y);
576
- for(let i=0;i<obj.length;i++){
577
- if(i!==n)obj[i].map((n,j)=>n.y=order[j]);
578
- }
579
- for(let i=0;i<obj.length;i++){
580
- if(i!==n)newObj[i].map((n,j)=>n.x=obj[i][order[j]].x)
581
- }
582
- newObj[n]=obj[n];
583
- var newArr=newObj.map(n=>n.map(m=>m.x));
584
- return new Matrix(newArr).T;
585
- }
586
- }
587
-
588
-
589
- /**
590
- * @returns {Matrix}
591
- */
592
- const matrix=(r, c, element)=>new Matrix(r, c, element);
593
- const matrix2=(...element)=>new Matrix(2, 2, element);
594
- const matrix3=(...element)=>new Matrix(3, 3, element);
595
- const matrix4=(...element)=>new Matrix(4, 4, element);
596
- export{Matrix,matrix,matrix2,matrix3,matrix4}