ziko 0.53.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.
@@ -0,0 +1,65 @@
1
+ export const norm = (x, min, max) => {
2
+ if(x.isComplex?.()) return new x.constructor(
3
+ norm(x.a, min, max),
4
+ norm(x.b, min, max)
5
+ )
6
+ if(x.isMatrix?.()) return new x.constructor(
7
+ x.rows,
8
+ x.cols,
9
+ norm(x.arr.flat(1), min, max)
10
+ );
11
+ return min !== max ? (value - min) / (max - min) : 0;
12
+ }
13
+
14
+ export const lerp = (x, min, max) => {
15
+ if(x.isComplex?.()) return new x.constructor(
16
+ lerp(x.a, min, max),
17
+ lerp(x.b, min, max)
18
+ )
19
+ if(x.isMatrix?.()) return new x.constructor(
20
+ x.rows,
21
+ x.cols,
22
+ lerp(x.arr.flat(1), min, max)
23
+ );
24
+ return (max - min) * value + min;
25
+ }
26
+
27
+ export const map = (x, min, max) => {
28
+ if(x.isComplex?.()) return new x.constructor(
29
+ map(x.a, min, max),
30
+ map(x.b, min, max)
31
+ )
32
+ if(x.isMatrix?.()) return new x.constructor(
33
+ x.rows,
34
+ x.cols,
35
+ map(x.arr.flat(1), min, max)
36
+ );
37
+ return lerp(norm(x, a, b), c, d);
38
+ }
39
+
40
+ export const clamp = (x, min, max) => {
41
+ if(x.isComplex?.()) return new x.constructor(
42
+ clamp(x.a, min, max),
43
+ clamp(x.b, min, max)
44
+ )
45
+ if(x.isMatrix?.()) return new x.constructor(
46
+ x.rows,
47
+ x.cols,
48
+ clamp(x.arr.flat(1), min, max)
49
+ );
50
+ return Math.min(Math.max(c, min), max)
51
+ }
52
+
53
+ export const hypot = (...x) => {
54
+ const c0 = x.find(a => a.isComplex?.());
55
+ if (c0) {
56
+ const W = x.map(n => n.isComplex?.() ? n : new c0.constructor(n, 0));
57
+ return Math.hypot(...W.map(c => c.z));
58
+ }
59
+ return Math.hypot(...x);
60
+ };
61
+
62
+
63
+ export const atan2 = (x, y, rad = true) =>{
64
+
65
+ }
@@ -1,4 +1,4 @@
1
- import { add, sub, mul } from "../../utils/index.js";
1
+ import { add, sub, mul } from "../../functions/arithmetic/index.js";
2
2
  import { pow } from "../../functions/index.js";
3
3
  export function matrix_det(M) {
4
4
  if (!M.isSquare) return new Error("is not square matrix");
@@ -1,22 +1,17 @@
1
- import{
2
- min,
3
- max,
4
- } from "../functions/index.js"
5
- import {
6
- Utils,
1
+ import {
7
2
  add,
8
3
  sub,
9
4
  mul,
10
- div,
11
- modulo,
5
+ div,
6
+ modulo
7
+ } from '../functions/arithmetic/index.js'
8
+ import {
12
9
  map,
13
- lerp,
14
- norm,
15
- clamp
16
-
17
- } from "../utils/index.js";
10
+ lerp,
11
+ clamp,
12
+ norm
13
+ } from '../functions/utils/index.js'
18
14
  import { Complex } from "../complex/index.js";
19
- // import { Random } from "../random/index.js"
20
15
  import { arr2str } from "../../data/index.js";
21
16
  import {
22
17
  matrix_inverse,
@@ -1,70 +1,10 @@
1
- import { abs , pow , sqrtn , max , min} from "../functions/index.js";
2
- import { mul } from "../utils/index.js";
1
+ import { abs , pow , nthr } from "../functions/index.js";
2
+ import { mul } from "../functions/arithmetic/index.js";
3
3
  import { E } from "../const.js";
4
4
  const zeros=(n)=>new Array(n).fill(0);
5
5
  const ones=(n)=>new Array(n).fill(1);
6
6
  const nums=(num,n)=>new Array(n).fill(num);
7
- const norm=(value, min, max)=>{
8
- if (typeof value === "number") return min !== max ? (value - min) / (max - min) : 0;
9
- else if (value.isMatrix?.()) return new value.constructor(value.rows, value.cols, norm(value.arr.flat(1), min, max));
10
- else if (value.isComplex?.()) return new value.constructor(norm(value.a, min, max), norm(value.b, min, max));
11
- else if (value instanceof Array) {
12
- if (value.every((n) => typeof (n === "number"))) {
13
- return value.map((n) => norm(n, min, max));
14
- } else {
15
- let y = new Array(value.length);
16
- for (let i = 0; i < value.length; i++) {
17
- y[i] = norm(value[i]);
18
- }
19
- }
20
- }
21
- }
22
- const lerp=(value, min, max)=>{
23
- if (typeof value === "number") return (max - min) * value + min;
24
- else if (value.isMatrix?.()) return new value.constructor(value.rows, value.cols, lerp(value.arr.flat(1), min, max));
25
- else if (value.isComplex?.()) return new value.constructor(lerp(value.a, min, max), lerp(value.b, min, max));
26
- else if (value instanceof Array) {
27
- if (value.every((n) => typeof (n === "number"))) {
28
- return value.map((n) => lerp(n, min, max));
29
- } else {
30
- let y = new Array(value.length);
31
- for (let i = 0; i < value.length; i++) {
32
- y[i] = lerp(value[i]);
33
- }
34
- }
35
- }
36
- }
37
- const map=(x, a, b, c, d)=>{
38
- if (typeof x === "number") return lerp(norm(x, a, b), c, d);
39
- else if (x.isMatrix?.()) return new x.constructor(x.rows, x.cols, map(x.arr.flat(1), a, b, c, d));
40
- else if (x.isComplex?.()) return new x.constructor(map(x.a, b, c, d), map(x.b, a, b, c, d));
41
- else if (x instanceof Array) {
42
- if (x.every((n) => typeof (n === "number"))) {
43
- return x.map((n) => map(n, a, b, c, d));
44
- } else {
45
- let y = new Array(x.length);
46
- for (let i = 0; i < x.length; i++) {
47
- y[i] = map(x[i], a, b, c, d);
48
- }
49
- }
50
- }
51
- }
52
- const clamp=(x, a , b)=>{
53
- const [min_value,max_value]=[min(a,b),max(a,b)]
54
- if (typeof x === "number") return min(max(x, min_value), max_value);
55
- else if (x.isMatrix?.()) return new x.constructor(x.rows, x.cols, clamp(x.arr.flat(1), min_value, max_value));
56
- else if (x.isComplex?.()) return new x.constructor(clamp(x.a, min_value, max_value), clamp(x.b, min_value, max_value));
57
- else if (x instanceof Array) {
58
- if (x.every((n) => typeof (n === "number"))) {
59
- return x.map((n) => clamp(n, min_value, max_value));
60
- } else {
61
- let y = new Array(x.length);
62
- for (let i = 0; i < x.length; i++) {
63
- y[i] = clamp(x[i], min_value, max_value);
64
- }
65
- }
66
- }
67
- }
7
+
68
8
  const arange=(a, b, step , include = false)=>{
69
9
  let tab = [];
70
10
  if(a<b){
@@ -107,7 +47,7 @@ const geomspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
107
47
  if([a,b].every(n=>typeof n==="number")){
108
48
  const [max,min]=[a,b].sort((a,b)=>b-a);
109
49
  let base;
110
- endpoint ? base = sqrtn(max/min,n-1) : base = sqrtn(max/min,n) ;
50
+ endpoint ? base = nthr(max/min,n-1) : base = nthr(max/min,n) ;
111
51
  const Y = [min];
112
52
  for (let i = 1; i < n; i++) {
113
53
  Y.push(Y[i-1]*base)
@@ -120,7 +60,7 @@ const geomspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
120
60
  const z2 = new n.constructor(b)
121
61
  n=n||Math.abs(z1.a-z2.a)+1;
122
62
  let base;
123
- endpoint ? base = sqrtn(z2.div(z1),n-1) : base = sqrtn(z2.div(z1),n) ;
63
+ endpoint ? base = nthr(z2.div(z1),n-1) : base = nthr(z2.div(z1),n) ;
124
64
  const Y = [z1];
125
65
  for (let i = 1; i < n; i++) {
126
66
  Y.push(mul(Y[i-1],base))
@@ -132,10 +72,10 @@ export {
132
72
  zeros,
133
73
  ones,
134
74
  nums,
135
- norm,
136
- lerp,
137
- map,
138
- clamp,
75
+ // norm,
76
+ // lerp,
77
+ // map,
78
+ // clamp,
139
79
  arange,
140
80
  linspace,
141
81
  logspace,
@@ -0,0 +1,39 @@
1
+ export const accum_sum = (arr) => {
2
+ let result = [];
3
+ let total = 0;
4
+ for (let x of arr) {
5
+ total += x;
6
+ result.push(total);
7
+ }
8
+ return result;
9
+ };
10
+
11
+ export const accum_product = (arr) => {
12
+ let result = [];
13
+ let prod = 1;
14
+ for (let x of arr) {
15
+ prod *= x;
16
+ result.push(prod);
17
+ }
18
+ return result;
19
+ };
20
+
21
+ export const accum_max = (arr) => {
22
+ let result = [];
23
+ let m = -Infinity;
24
+ for (let x of arr) {
25
+ m = Math.max(m, x);
26
+ result.push(m);
27
+ }
28
+ return result;
29
+ };
30
+
31
+ export const accum_min = (arr) => {
32
+ let result = [];
33
+ let m = Infinity;
34
+ for (let x of arr) {
35
+ m = Math.min(m, x);
36
+ result.push(m);
37
+ }
38
+ return result;
39
+ };
@@ -0,0 +1,75 @@
1
+ // Mean
2
+ export const mean = (...x) => x.reduce((a, b) => a + b) / x.length;
3
+ export const geo_mean = (...x) => (x.reduce((a, b) => a * b)) ** (1/x.length);
4
+ // Quadratic Mean
5
+ export const rms = (...x) => {
6
+ const n = x.length;
7
+ return (Math.hypot(...x)/n)**(1/n)
8
+ }
9
+
10
+ export const weighted_mean=(values, weights)=>{
11
+ let sum = 0, sw = 0;
12
+ for (let i = 0; i < values.length; i++) {
13
+ sum += values[i] * weights[i];
14
+ sw += weights[i];
15
+ }
16
+ return sum / sw;
17
+ }
18
+
19
+ export const harmonic_mean = (...x) => {
20
+ let s = 0, i = 0;
21
+ for(i=0; i<x.length; i++)
22
+ s += 1/x[i]
23
+ return x.length / s;
24
+ }
25
+
26
+ export const power_mean = (X, p) =>{
27
+ let s = 0, i = 0, l = X.length;
28
+ for(i=0; i < l; i++)
29
+ s+= X[i]**p
30
+ return (s / l) ** (1 / p);
31
+ }
32
+
33
+ export const trimmed_mean = (X, k) =>{
34
+ let a = [...X].sort((a,b)=>a-b).slice(k, X.length - k);
35
+ return mean(...a);
36
+ }
37
+
38
+ export const winsorized_mean = (X, k) =>{
39
+ let a = [...X].sort((a,b)=>a-b);
40
+ let low = a[k], high = a[a.length - k - 1];
41
+ a = a.map(x => Math.max(low, Math.min(high, x)));
42
+ return mean(a);
43
+ }
44
+
45
+ export const midrange = (x) =>{
46
+ let min = Math.min(...x);
47
+ let max = Math.max(...x);
48
+ return (min + max) / 2;
49
+ }
50
+
51
+ export const midhinge = (...x) =>{
52
+ let a = x.sort((a,b)=>a-b);
53
+ let q1 = a[Math.floor((a.length - 1) * 0.25)];
54
+ let q3 = a[Math.floor((a.length - 1) * 0.75)];
55
+ return (q1 + q3) / 2;
56
+ }
57
+
58
+
59
+ export const interquartile_mean = (...x) =>{
60
+ let a = x.sort((a,b)=>a-b);
61
+ let q1 = a[Math.floor((a.length - 1) * 0.25)];
62
+ let q3 = a[Math.floor((a.length - 1) * 0.75)];
63
+ let m = a.filter(x => x >= q1 && x <= q3);
64
+ return mean(m);
65
+ }
66
+
67
+
68
+ export const contraharmonic_mean = (...x) =>{
69
+ let num = 0, den = 0, i, l = x.length;
70
+ for(i = 0; i < l; i++){
71
+ num += x[i]**2;
72
+ den += x[i]
73
+ }
74
+ return num / den;
75
+ }
@@ -0,0 +1,17 @@
1
+ export * from './position/index.js';
2
+ export * from './average/index.js';
3
+ export * from './variability/index.js'
4
+ export * from './rolling/index.js'
5
+ export * from './accum/index.js'
6
+
7
+
8
+ // min
9
+ // max
10
+
11
+ // mean
12
+ // variance
13
+ // std
14
+ // percentil
15
+ // accum
16
+
17
+ //
@@ -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
  }
@@ -1,28 +1,28 @@
1
- import { mapfun } from "../mapfun/index.js";
2
- import {
3
- add,
4
- sub,
5
- mul,
6
- div,
7
- modulo
8
- } from "./arithmetic.js";
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
- deg2rad,
24
- rad2deg
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
- sqrtn(n?: number): Complex;
38
+ nrth(n?: number): Complex;
38
39
 
39
40
  static Zero(): Complex;
40
41
  static fromExpo(z: number, phi: number): Complex;