ziko 0.52.0 → 0.54.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ziko.js +5582 -5450
- package/dist/ziko.mjs +939 -807
- package/package.json +1 -1
- package/src/math/complex/index.js +81 -87
- package/src/math/functions/arithmetic/index.js +82 -0
- package/src/math/functions/index.js +39 -153
- package/src/math/{mapfun → functions/mapfun}/index.js +1 -1
- package/src/math/functions/nested/index.js +371 -0
- package/src/math/functions/utils/index.js +65 -0
- package/src/math/matrix/helpers/det.js +1 -1
- package/src/math/matrix/matrix.js +9 -14
- package/src/math/signal/functions.js +9 -69
- package/src/math/stats/accum/index.js +39 -0
- package/src/math/stats/average/index.js +75 -0
- package/src/math/stats/index.js +17 -0
- package/src/math/stats/position/index.js +21 -0
- package/src/math/stats/rolling/index.js +34 -0
- package/src/math/stats/variability/index.js +37 -0
- package/src/math/utils/arithmetic.js +1 -1
- package/src/math/utils/index.js +39 -39
- package/src/time/animation/index.js +1 -1
- package/types/math/complex/index.d.ts +2 -1
- package/src/math/functions/primitives/index.js +0 -176
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const min = (...x) => Math.min(...x);
|
|
2
|
+
export const max = (...x) => Math.max(...x);
|
|
3
|
+
|
|
4
|
+
export const percentile = (X, p) => {
|
|
5
|
+
if (X.length === 0)
|
|
6
|
+
return NaN;
|
|
7
|
+
let a = X.sort((x, y) => x - y);
|
|
8
|
+
let index = (p / 100) * (a.length - 1);
|
|
9
|
+
let i = Math.floor(index);
|
|
10
|
+
let f = index - i;
|
|
11
|
+
if (i === a.length - 1)
|
|
12
|
+
return a[i];
|
|
13
|
+
return a[i] * (1 - f) + a[i + 1] * f;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const q1 = X => percentile(X, 25);
|
|
17
|
+
export const median = X => percentile(X, 50);
|
|
18
|
+
export const q3 = X => percentile(X, 75);
|
|
19
|
+
|
|
20
|
+
// Interquartile Range
|
|
21
|
+
export const iqr = X => q3(X) - q1(X)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Simple Moving Average
|
|
2
|
+
export const sma = (X, w) =>{
|
|
3
|
+
let r = [];
|
|
4
|
+
for (let i = 0; i <= X.length - w; i++) {
|
|
5
|
+
let s = 0;
|
|
6
|
+
for (let j = 0; j < w; j++) s += X[i + j];
|
|
7
|
+
r.push(s / w);
|
|
8
|
+
}
|
|
9
|
+
return r;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// exponential Moving Average
|
|
13
|
+
export const ema = (X, alpha) =>{
|
|
14
|
+
let r = [], prev = X[0];
|
|
15
|
+
r.push(prev);
|
|
16
|
+
for (let i = 1; i < X.length; i++) {
|
|
17
|
+
prev = alpha * X[i] + (1 - alpha) * prev;
|
|
18
|
+
r.push(prev);
|
|
19
|
+
}
|
|
20
|
+
return r;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// weightedMovingAverage
|
|
24
|
+
export const wma = (X, weights) =>{
|
|
25
|
+
let k = weights.length;
|
|
26
|
+
let sw = weights.reduce((a,b)=>a+b, 0);
|
|
27
|
+
let r = [];
|
|
28
|
+
for (let i = 0; i <= X.length - k; i++) {
|
|
29
|
+
let s = 0;
|
|
30
|
+
for (let j = 0; j < k; j++) s += X[i+j] * weights[j];
|
|
31
|
+
r.push(s / sw);
|
|
32
|
+
}
|
|
33
|
+
return r;
|
|
34
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Population Variance
|
|
2
|
+
import { mean } from "../average/index.js";
|
|
3
|
+
export const variance = (...x) => {
|
|
4
|
+
const n = x.length;
|
|
5
|
+
if (n === 0) return NaN;
|
|
6
|
+
const x_mean = mean(...x);
|
|
7
|
+
return x.reduce((sum, xi) => sum + (xi - x_mean) ** 2, 0) / n;
|
|
8
|
+
};
|
|
9
|
+
export const std = (...x) => Math.sqrt(variance(...x));
|
|
10
|
+
|
|
11
|
+
export const sample_variance = (...x) => {
|
|
12
|
+
const n = x.length;
|
|
13
|
+
if (n < 2) return NaN;
|
|
14
|
+
const x_mean = mean(...x);
|
|
15
|
+
return x.reduce((sum, xi) => sum + (xi - x_mean) ** 2, 0) / (n - 1);
|
|
16
|
+
};
|
|
17
|
+
export const sample_std = (...x) => Math.sqrt(sample_variance(...x));
|
|
18
|
+
|
|
19
|
+
export const weighted_variance = (X, weights) => {
|
|
20
|
+
const n = X.length;
|
|
21
|
+
if (n === 0 || weights.length !== n) return NaN;
|
|
22
|
+
const sw = weights.reduce((sum, w) => sum + w, 0);
|
|
23
|
+
const mean = X.reduce((sum, x, i) => sum + x * weights[i], 0) / sw;
|
|
24
|
+
return X.reduce((sum, x, i) => sum + weights[i] * (x - mean) ** 2, 0) / sw;
|
|
25
|
+
};
|
|
26
|
+
export const weighted_std = (X, weights) => Math.sqrt(weighted_variance(X, weights));
|
|
27
|
+
|
|
28
|
+
export const rolling_variance = (X, windowSize) => {
|
|
29
|
+
if (windowSize < 1 || X.length < windowSize) return [];
|
|
30
|
+
let result = [];
|
|
31
|
+
for (let i = 0; i <= X.length - windowSize; i++) {
|
|
32
|
+
const w = X.slice(i, i + windowSize);
|
|
33
|
+
result.push(sample_variance(w)); // usually sample variance for rolling
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
export const rolling_std = (X, windowSize) => Math.sqrt(rolling_variance(X, windowSize));
|
|
@@ -43,7 +43,7 @@ const _sub=(a,b)=>{
|
|
|
43
43
|
const _mul=(a,b)=>{
|
|
44
44
|
if(typeof(a)==="number"){
|
|
45
45
|
if (typeof b == "number") return a * b;
|
|
46
|
-
else if (b.isComplex?.())return new b.constructor(a * b.a,a * b.b);
|
|
46
|
+
else if (b.isComplex?.())return new b.constructor(a * b.a, a * b.b);
|
|
47
47
|
else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).mul(b);
|
|
48
48
|
else if (b instanceof Array)return b.map(n=>mul(a,n));
|
|
49
49
|
}
|
package/src/math/utils/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { mapfun } from "../mapfun/index.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// import { mapfun } from "../mapfun/index.js";
|
|
2
|
+
// import {
|
|
3
|
+
// add,
|
|
4
|
+
// sub,
|
|
5
|
+
// mul,
|
|
6
|
+
// div,
|
|
7
|
+
// modulo
|
|
8
|
+
// } from "./arithmetic.js";
|
|
9
9
|
import {
|
|
10
10
|
zeros,
|
|
11
11
|
ones,
|
|
12
12
|
nums,
|
|
13
|
-
norm,
|
|
14
|
-
lerp,
|
|
15
|
-
map,
|
|
16
|
-
clamp,
|
|
13
|
+
// norm,
|
|
14
|
+
// lerp,
|
|
15
|
+
// map,
|
|
16
|
+
// clamp,
|
|
17
17
|
arange,
|
|
18
18
|
linspace,
|
|
19
19
|
logspace,
|
|
20
20
|
geomspace
|
|
21
21
|
} from "../signal/functions.js"
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
} from "./conversions.js"
|
|
22
|
+
// import {
|
|
23
|
+
// deg2rad,
|
|
24
|
+
// rad2deg
|
|
25
|
+
// } from "./conversions.js"
|
|
26
26
|
import{
|
|
27
27
|
sum,
|
|
28
28
|
prod,
|
|
@@ -38,19 +38,19 @@ import{
|
|
|
38
38
|
pgcd
|
|
39
39
|
} from "./discret.js"
|
|
40
40
|
const Utils={
|
|
41
|
-
add,
|
|
42
|
-
sub,
|
|
43
|
-
mul,
|
|
44
|
-
div,
|
|
45
|
-
modulo,
|
|
41
|
+
// add,
|
|
42
|
+
// sub,
|
|
43
|
+
// mul,
|
|
44
|
+
// div,
|
|
45
|
+
// modulo,
|
|
46
46
|
|
|
47
47
|
zeros,
|
|
48
48
|
ones,
|
|
49
49
|
nums,
|
|
50
|
-
norm,
|
|
51
|
-
lerp,
|
|
52
|
-
map,
|
|
53
|
-
clamp,
|
|
50
|
+
// norm,
|
|
51
|
+
// lerp,
|
|
52
|
+
// map,
|
|
53
|
+
// clamp,
|
|
54
54
|
arange,
|
|
55
55
|
linspace,
|
|
56
56
|
logspace,
|
|
@@ -64,35 +64,35 @@ const Utils={
|
|
|
64
64
|
ppcm,
|
|
65
65
|
pgcd,
|
|
66
66
|
|
|
67
|
-
deg2rad,
|
|
68
|
-
rad2deg,
|
|
67
|
+
// deg2rad,
|
|
68
|
+
// rad2deg,
|
|
69
69
|
|
|
70
70
|
inRange,
|
|
71
71
|
isApproximatlyEqual
|
|
72
72
|
}
|
|
73
73
|
export {
|
|
74
|
-
mapfun,
|
|
74
|
+
// mapfun,
|
|
75
75
|
Utils,
|
|
76
76
|
zeros,
|
|
77
77
|
ones,
|
|
78
78
|
nums,
|
|
79
79
|
sum,
|
|
80
80
|
prod,
|
|
81
|
-
add,
|
|
82
|
-
mul,
|
|
83
|
-
sub,
|
|
84
|
-
div,
|
|
85
|
-
modulo,
|
|
86
|
-
rad2deg,
|
|
87
|
-
deg2rad,
|
|
81
|
+
// add,
|
|
82
|
+
// mul,
|
|
83
|
+
// sub,
|
|
84
|
+
// div,
|
|
85
|
+
// modulo,
|
|
86
|
+
// rad2deg,
|
|
87
|
+
// deg2rad,
|
|
88
88
|
arange,
|
|
89
89
|
linspace,
|
|
90
90
|
logspace,
|
|
91
91
|
geomspace,
|
|
92
|
-
norm,
|
|
93
|
-
lerp,
|
|
94
|
-
map,
|
|
95
|
-
clamp,
|
|
92
|
+
// norm,
|
|
93
|
+
// lerp,
|
|
94
|
+
// map,
|
|
95
|
+
// clamp,
|
|
96
96
|
pgcd,
|
|
97
97
|
ppcm,
|
|
98
98
|
isApproximatlyEqual,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { linear } from "../ease/index.js";
|
|
2
|
-
import { map } from "../../math/utils/index.js";
|
|
2
|
+
import { map } from "../../math/functions/utils/index.js";
|
|
3
3
|
|
|
4
4
|
class TimeAnimation {
|
|
5
5
|
constructor(callback, { ease = linear, step = 50, t0 = 0, start = true, duration = 3000 } = {}) {
|
|
@@ -23,6 +23,7 @@ export declare class Complex {
|
|
|
23
23
|
readonly conj: Complex;
|
|
24
24
|
readonly inv: Complex;
|
|
25
25
|
readonly sqrt: Complex;
|
|
26
|
+
readonly cbrt: Complex;
|
|
26
27
|
readonly log: Complex;
|
|
27
28
|
readonly cos: Complex;
|
|
28
29
|
readonly sin: Complex;
|
|
@@ -34,7 +35,7 @@ export declare class Complex {
|
|
|
34
35
|
mul(...z: (number | Complex)[]): this;
|
|
35
36
|
div(...z: (number | Complex)[]): this;
|
|
36
37
|
pow(n: number): this;
|
|
37
|
-
|
|
38
|
+
nrth(n?: number): Complex;
|
|
38
39
|
|
|
39
40
|
static Zero(): Complex;
|
|
40
41
|
static fromExpo(z: number, phi: number): Complex;
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { mapfun } from '../../mapfun/index.js';
|
|
2
|
-
|
|
3
|
-
export const abs = (...x) => mapfun(
|
|
4
|
-
x =>{
|
|
5
|
-
if(x.isComplex?.()) return x.z;
|
|
6
|
-
return Math.abs(x)
|
|
7
|
-
},
|
|
8
|
-
...x
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
export const sqrt = (...x) => mapfun(
|
|
12
|
-
x=>{
|
|
13
|
-
if(x.isComplex?.()){
|
|
14
|
-
const {z, phi} = x
|
|
15
|
-
return new x.constructor(
|
|
16
|
-
Math.sqrt(z) * Math.cos(phi/2),
|
|
17
|
-
Math.sqrt(z) * Math.sin(phi/2)
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
return Math.sqrt(x);
|
|
21
|
-
},
|
|
22
|
-
...x
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
export const e = (...x) => mapfun(
|
|
26
|
-
x => {
|
|
27
|
-
if(x.isComplex?.()) return new x.constructor(
|
|
28
|
-
Math.exp(x.a) * Math.cos(x.b),
|
|
29
|
-
Math.exp(x.a) * Math.sin(x.b)
|
|
30
|
-
);
|
|
31
|
-
return Math.ln(x)
|
|
32
|
-
}
|
|
33
|
-
,...x
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
export const ln = (...x) => mapfun(
|
|
37
|
-
x => {
|
|
38
|
-
if(x.isComplex?.()) return new x.constructor(
|
|
39
|
-
Math.log(x.z),
|
|
40
|
-
x.phi
|
|
41
|
-
);
|
|
42
|
-
return Math.ln(x)
|
|
43
|
-
}
|
|
44
|
-
,...x
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
export const sign = (...x) => mapfun(
|
|
48
|
-
x => {
|
|
49
|
-
if(x.isComplex?.()){
|
|
50
|
-
const {z, phi} = x;
|
|
51
|
-
if(z===0) return new x.constructor(0, 0);
|
|
52
|
-
return new x.constructor({z:1, phi})
|
|
53
|
-
}
|
|
54
|
-
return Math.sign(x)
|
|
55
|
-
}
|
|
56
|
-
,...x
|
|
57
|
-
);
|
|
58
|
-
export const cos = (...x) => mapfun(
|
|
59
|
-
x => {
|
|
60
|
-
if(x.isComplex?.()) return new x.constructor(
|
|
61
|
-
Math.cos(x.a) * Math.cosh(x.b),
|
|
62
|
-
-Math.sin(x.a) * Math.sinh(x.b)
|
|
63
|
-
);
|
|
64
|
-
return Math.cos(x)
|
|
65
|
-
}
|
|
66
|
-
,...x
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
export const sin = (...x) => mapfun(
|
|
70
|
-
x =>{
|
|
71
|
-
if(x?.isComplex) return new x.constructor(
|
|
72
|
-
Math.sin(x.a) * Math.cosh(x.b),
|
|
73
|
-
Math.cos(x.a) * Math.sinh(x.b)
|
|
74
|
-
);
|
|
75
|
-
return Math.sin(x)
|
|
76
|
-
}
|
|
77
|
-
, ...x
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
export const tan = (...x) => mapfun(
|
|
81
|
-
x =>{
|
|
82
|
-
if(x?.isComplex){
|
|
83
|
-
const DEN = Math.cos(2*x.a) + Math.cosh(2*x.b);
|
|
84
|
-
return new x.constructor(
|
|
85
|
-
Math.sin(2*x.a) / DEN,
|
|
86
|
-
Math.sinh(2*x.b) / DEN
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
return Math.tan(x)
|
|
90
|
-
},
|
|
91
|
-
...x
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
export const acos = (...x) => mapfun(
|
|
95
|
-
x =>{
|
|
96
|
-
if(x?.isComplex){
|
|
97
|
-
const { a, b } = x;
|
|
98
|
-
const Rp = Math.hypot(a + 1, b);
|
|
99
|
-
const Rm = Math.hypot(a - 1, b);
|
|
100
|
-
globalThis.Rp = Rp
|
|
101
|
-
globalThis.Rm = Rm
|
|
102
|
-
console.log({a, b, Rp, Rm})
|
|
103
|
-
return new x.constructor(
|
|
104
|
-
Math.acos((Rp - Rm) / 2),
|
|
105
|
-
-Math.acosh((Rp + Rm) / 2),
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
return Math.acos(x)
|
|
109
|
-
},
|
|
110
|
-
...x
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
export const asin = (...x) => mapfun(
|
|
114
|
-
x => {
|
|
115
|
-
if(x?.isComplex){
|
|
116
|
-
const { a, b } = x;
|
|
117
|
-
const Rp = Math.hypot(a + 1, b);
|
|
118
|
-
const Rm = Math.hypot(a - 1, b);
|
|
119
|
-
return new x.constructor(
|
|
120
|
-
Math.asin((Rp - Rm) / 2),
|
|
121
|
-
Math.acosh((Rp + Rm) / 2)
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
return Math.asin(x);
|
|
125
|
-
},
|
|
126
|
-
...x
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
export const atan = (...x) => mapfun(
|
|
130
|
-
x => {
|
|
131
|
-
if(x?.isComplex){
|
|
132
|
-
const { a, b } = x;
|
|
133
|
-
return new x.constructor(
|
|
134
|
-
Math.atan((a*2/(1-a**2-b**2)))/2,
|
|
135
|
-
Math.log((a**2 + (1+b)**2)/(a**2 + (1-b)**2))/4
|
|
136
|
-
)
|
|
137
|
-
}
|
|
138
|
-
return Math.atan(x);
|
|
139
|
-
},
|
|
140
|
-
...x
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
export const cosh = (...x) => mapfun(
|
|
145
|
-
x =>{
|
|
146
|
-
if(x?.isComplex) return new x.constructor(
|
|
147
|
-
cosh(x.a)*cos(x.b),
|
|
148
|
-
sinh(x.a)*sin(x.b)
|
|
149
|
-
);
|
|
150
|
-
return cosh(x)
|
|
151
|
-
},
|
|
152
|
-
...x
|
|
153
|
-
)
|
|
154
|
-
export const sinh = (...x) => mapfun(
|
|
155
|
-
x =>{
|
|
156
|
-
if(x?.isComplex) return new x.constructor(
|
|
157
|
-
sinh(x.a)*cos(x.b),
|
|
158
|
-
cosh(x.a)*sin(x.b)
|
|
159
|
-
);
|
|
160
|
-
return sinh(x)
|
|
161
|
-
},
|
|
162
|
-
...x
|
|
163
|
-
)
|
|
164
|
-
export const tanh = (...x) => mapfun(
|
|
165
|
-
x =>{
|
|
166
|
-
if(x?.isComplex){
|
|
167
|
-
const DEN=cosh(2*a)+cos(2*b);
|
|
168
|
-
return new x.constructor(
|
|
169
|
-
sinh(2*a)/DEN,
|
|
170
|
-
sin(2*b)/DEN
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
return tanh(x)
|
|
174
|
-
},
|
|
175
|
-
...x
|
|
176
|
-
)
|