ziko 0.51.0 → 0.51.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,6 @@
1
1
 
2
- import{Complex}from"../complex/index.js"
3
2
  import { Utils } from "../utils/index.js";
4
- import{Base}from"../discret/index.js"
5
- import { matrix } from "../matrix/index.js";
3
+ import{ Base } from "../discret/index.js"
6
4
  class Random {
7
5
  static float(a = 1, b) {
8
6
  return b ? Math.random() * (b - a) + a : a * Math.random();
@@ -45,10 +43,6 @@ class Random {
45
43
  static shuffleArr(arr){
46
44
  return arr.sort(()=>0.5-Math.random())
47
45
  }
48
- static shuffleMatrix(M){
49
- const {rows,cols,arr}=M;
50
- return matrix(rows,cols,arr.flat().sort(()=>0.5-Math.random()))
51
- }
52
46
  static floats(n, a, b) {
53
47
  return new Array(n).fill(0).map(() => this.float(a, b));
54
48
  }
@@ -83,91 +77,93 @@ class Random {
83
77
  static colors(n) {
84
78
  return new Array(n).fill(null).map(()=>this.color());
85
79
  }
86
- static complex(a = [0,1], b = [0,1]) {
87
- return a instanceof Array?
88
- new Complex(
89
- this.float(a[0], a[1]),
90
- this.float(b[0], b[1])
91
- ):
92
- new Complex(
93
- ...this.floats(2,a,b)
94
- )
80
+
81
+ // Should be Moved to Matrix and Complex to avoid Circular dependencies
82
+ // static complex(a = [0,1], b = [0,1]) {
83
+ // return a instanceof Array?
84
+ // new Complex(
85
+ // this.float(a[0], a[1]),
86
+ // this.float(b[0], b[1])
87
+ // ):
88
+ // new Complex(
89
+ // ...this.floats(2,a,b)
90
+ // )
95
91
 
96
- }
97
- static complexInt(a = [0,1], b = [0,1]) {
98
- return new Complex(
99
- this.int(a[0], a[1]),
100
- this.int(b[0], b[1])
101
- );
102
- }
103
- static complexBin() {
104
- return new Complex(...this.bins(2));
105
- }
106
- static complexOct() {
107
- return new Complex(...this.octs(2));
108
- }
109
- static complexDec() {
110
- return new Complex(...this.decs(10));
111
- }
112
- static complexHex() {
113
- return new Complex(...this.octs(2));
114
- }
115
- static complexes(n, a = 0, b = 1) {
116
- return new Array(n).fill(0).map(() => this.complex(a, b));
117
- }
118
- static complexesInt(n, a = 0, b = 1) {
119
- return new Array(n).fill(0).map(() => this.complexInt(a, b));
120
- }
121
- static complexesBin(n) {
122
- return new Array(n).fill(0).map(() => this.complexBin());
123
- }
124
- static complexesOct(n) {
125
- return new Array(n).fill(0).map(() => this.complexOct());
126
- }
127
- static complexesDec(n) {
128
- return new Array(n).fill(0).map(() => this.complexDec());
129
- }
130
- static complexesHex(n) {
131
- return new Array(n).fill(0).map(() => this.complexHex());
132
- }
133
- static matrix(r,c,min,max){
134
- return matrix(r,c,this.floats(r*c,min,max))
135
- }
136
- static matrixInt(r,c,min,max){
137
- return matrix(r,c,this.ints(r*c,min,max))
138
- }
139
- static matrixBin(r,c){
140
- return matrix(r,c,this.bins(r*c))
141
- }
142
- static matrixOct(r,c){
143
- return matrix(r,c,this.octs(r*c))
144
- }
145
- static matrixDec(r,c){
146
- return matrix(r,c,this.decs(r*c))
147
- }
148
- static matrixHex(r,c){
149
- return matrix(r,c,this.hex(r*c))
150
- }
151
- static matrixColor(r,c){
152
- return matrix(r,c,this.colors(r*c))
153
- }
154
- static matrixComplex(r,c,a,b){
155
- return matrix(r,c,this.complexes(r*c,a,b))
156
- }
157
- static matrixComplexInt(r,c,a,b){
158
- return matrix(r,c,this.complexesInt(r*c,a,b))
159
- }
160
- static matrixComplexBin(r,c){
161
- return matrix(r,c,this.complexesBin(r*c))
162
- }
163
- static matrixComplexOct(r,c){
164
- return matrix(r,c,this.complexesBin(r*c))
165
- }
166
- static matrixComplexDec(r,c){
167
- return matrix(r,c,this.complexesBin(r*c))
168
- }
169
- static matrixComplexHex(r,c){
170
- return matrix(r,c,this.complexesBin(r*c))
171
- }
92
+ // }
93
+ // static complexInt(a = [0,1], b = [0,1]) {
94
+ // return new Complex(
95
+ // this.int(a[0], a[1]),
96
+ // this.int(b[0], b[1])
97
+ // );
98
+ // }
99
+ // static complexBin() {
100
+ // return new Complex(...this.bins(2));
101
+ // }
102
+ // static complexOct() {
103
+ // return new Complex(...this.octs(2));
104
+ // }
105
+ // static complexDec() {
106
+ // return new Complex(...this.decs(10));
107
+ // }
108
+ // static complexHex() {
109
+ // return new Complex(...this.octs(2));
110
+ // }
111
+ // static complexes(n, a = 0, b = 1) {
112
+ // return new Array(n).fill(0).map(() => this.complex(a, b));
113
+ // }
114
+ // static complexesInt(n, a = 0, b = 1) {
115
+ // return new Array(n).fill(0).map(() => this.complexInt(a, b));
116
+ // }
117
+ // static complexesBin(n) {
118
+ // return new Array(n).fill(0).map(() => this.complexBin());
119
+ // }
120
+ // static complexesOct(n) {
121
+ // return new Array(n).fill(0).map(() => this.complexOct());
122
+ // }
123
+ // static complexesDec(n) {
124
+ // return new Array(n).fill(0).map(() => this.complexDec());
125
+ // }
126
+ // static complexesHex(n) {
127
+ // return new Array(n).fill(0).map(() => this.complexHex());
128
+ // }
129
+ // static matrix(r,c,min,max){
130
+ // return matrix(r,c,this.floats(r*c,min,max))
131
+ // }
132
+ // static matrixInt(r,c,min,max){
133
+ // return matrix(r,c,this.ints(r*c,min,max))
134
+ // }
135
+ // static matrixBin(r,c){
136
+ // return matrix(r,c,this.bins(r*c))
137
+ // }
138
+ // static matrixOct(r,c){
139
+ // return matrix(r,c,this.octs(r*c))
140
+ // }
141
+ // static matrixDec(r,c){
142
+ // return matrix(r,c,this.decs(r*c))
143
+ // }
144
+ // static matrixHex(r,c){
145
+ // return matrix(r,c,this.hex(r*c))
146
+ // }
147
+ // static matrixColor(r,c){
148
+ // return matrix(r,c,this.colors(r*c))
149
+ // }
150
+ // static matrixComplex(r,c,a,b){
151
+ // return matrix(r,c,this.complexes(r*c,a,b))
152
+ // }
153
+ // static matrixComplexInt(r,c,a,b){
154
+ // return matrix(r,c,this.complexesInt(r*c,a,b))
155
+ // }
156
+ // static matrixComplexBin(r,c){
157
+ // return matrix(r,c,this.complexesBin(r*c))
158
+ // }
159
+ // static matrixComplexOct(r,c){
160
+ // return matrix(r,c,this.complexesBin(r*c))
161
+ // }
162
+ // static matrixComplexDec(r,c){
163
+ // return matrix(r,c,this.complexesBin(r*c))
164
+ // }
165
+ // static matrixComplexHex(r,c){
166
+ // return matrix(r,c,this.complexesBin(r*c))
167
+ // }
172
168
  }
173
169
  export{Random}
@@ -1,15 +1,13 @@
1
- import { Complex , complex } from "../complex/index.js";
2
1
  import { abs , pow , sqrtn , max , min} from "../functions/index.js";
3
2
  import { mul } from "../utils/index.js";
4
3
  import { E } from "../const.js";
5
- import { Matrix } from "../matrix/Matrix.js";
6
4
  const zeros=(n)=>new Array(n).fill(0);
7
5
  const ones=(n)=>new Array(n).fill(1);
8
6
  const nums=(num,n)=>new Array(n).fill(num);
9
7
  const norm=(value, min, max)=>{
10
8
  if (typeof value === "number") return min !== max ? (value - min) / (max - min) : 0;
11
- else if (value instanceof Matrix) return new Matrix(value.rows, value.cols, norm(value.arr.flat(1), min, max));
12
- else if (value instanceof Complex) return new Complex(norm(value.a, min, max), norm(value.b, min, max));
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));
13
11
  else if (value instanceof Array) {
14
12
  if (value.every((n) => typeof (n === "number"))) {
15
13
  return value.map((n) => norm(n, min, max));
@@ -23,8 +21,8 @@ const norm=(value, min, max)=>{
23
21
  }
24
22
  const lerp=(value, min, max)=>{
25
23
  if (typeof value === "number") return (max - min) * value + min;
26
- else if (value instanceof Matrix) return new Matrix(value.rows, value.cols, lerp(value.arr.flat(1), min, max));
27
- else if (value instanceof Complex) return new Complex(lerp(value.a, min, max), lerp(value.b, min, max));
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));
28
26
  else if (value instanceof Array) {
29
27
  if (value.every((n) => typeof (n === "number"))) {
30
28
  return value.map((n) => lerp(n, min, max));
@@ -36,17 +34,17 @@ const lerp=(value, min, max)=>{
36
34
  }
37
35
  }
38
36
  }
39
- const map=(value, a, b, c, d)=>{
40
- if (typeof value === "number") return lerp(norm(value, a, b), c, d);
41
- else if (value instanceof Matrix) return new Matrix(value.rows, value.cols, map(value.arr.flat(1), a, b, c, d));
42
- else if (value instanceof Complex) return new Complex(map(value.a, b, c, d), map(value.b, a, b, c, d));
43
- else if (value instanceof Array) {
44
- if (value.every((n) => typeof (n === "number"))) {
45
- return value.map((n) => map(n, a, b, c, d));
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));
46
44
  } else {
47
- let y = new Array(value.length);
48
- for (let i = 0; i < value.length; i++) {
49
- y[i] = map(value[i], a, b, c, d);
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);
50
48
  }
51
49
  }
52
50
  }
@@ -54,8 +52,8 @@ const map=(value, a, b, c, d)=>{
54
52
  const clamp=(x, a , b)=>{
55
53
  const [min_value,max_value]=[min(a,b),max(a,b)]
56
54
  if (typeof x === "number") return min(max(x, min_value), max_value);
57
- else if (x instanceof Matrix) return new Matrix(x.rows, x.cols, clamp(x.arr.flat(1), min_value, max_value));
58
- else if (x instanceof Complex) return new Complex(clamp(x.a, min_value, max_value), clamp(x.b, 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));
59
57
  else if (x instanceof Array) {
60
58
  if (x.every((n) => typeof (n === "number"))) {
61
59
  return x.map((n) => clamp(n, min_value, max_value));
@@ -90,14 +88,14 @@ const linspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
90
88
  return Y
91
89
  }
92
90
 
93
- if([a,b].some(n=>n instanceof Complex)){
94
- const z1=complex(a)
95
- const z2=complex(b)
91
+ if([a,b].some(n=>n.isComplex?.())){
92
+ const z1 = new n.constructor(a)
93
+ const z2 = new n.constructor(b)
96
94
  n=n||Math.abs(z1.a-z2.a)+1;
97
95
  const X=linspace(z1.a,z2.a,n,endpoint);
98
96
  const Y=linspace(z1.b,z2.b,n,endpoint);
99
97
  let Z=new Array(n).fill(null);
100
- Z=Z.map((n,i)=>complex(X[i],Y[i]));
98
+ Z=Z.map((n,i)=> new n.constructor(X[i],Y[i]));
101
99
  return Z;
102
100
  }
103
101
  }
@@ -117,9 +115,9 @@ const geomspace=(a,b,n=abs(b-a)+1,endpoint=true)=>{
117
115
  return a<b?Y:Y.reverse()
118
116
  }
119
117
 
120
- if([a,b].some(n=>n instanceof Complex)){
121
- const z1=complex(a)
122
- const z2=complex(b)
118
+ if([a,b].some(n=>n.isComplex?.())){
119
+ const z1 = new n.constructor(a)
120
+ const z2 = new n.constructor(b)
123
121
  n=n||Math.abs(z1.a-z2.a)+1;
124
122
  let base;
125
123
  endpoint ? base = sqrtn(z2.div(z1),n-1) : base = sqrtn(z2.div(z1),n) ;
@@ -1,13 +1,11 @@
1
- import { Complex,complex} from "../complex/index.js";
2
- import { Matrix } from "../matrix/index.js";
3
1
  const _add=(a,b)=>{
4
2
  if(typeof(a)==="number"){
5
3
  if (typeof b == "number") return a + b;
6
- else if (b instanceof Complex)return complex(a + b.a, b.b);
7
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).add(b);
4
+ else if (b.isComplex?.())return new b.constructor(a + b.a, b.b);
5
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).add(b);
8
6
  else if (b instanceof Array)return b.map(n=>add(n,a));
9
7
  }
10
- else if(a instanceof Complex||a instanceof Matrix){
8
+ else if(a.isComplex?.()||a.isMatrix?.()){
11
9
  if(b instanceof Array)return b.map(n=>a.clone.add(n));
12
10
  return a.clone.add(b);
13
11
  }
@@ -23,11 +21,11 @@ const _add=(a,b)=>{
23
21
  const _sub=(a,b)=>{
24
22
  if(typeof(a)==="number"){
25
23
  if (typeof b == "number") return a - b;
26
- else if (b instanceof Complex)return complex(a - b.a, -b.b);
27
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).sub(b);
24
+ else if (b.isComplex?.())return new b.constructor(a - b.a, -b.b);
25
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).sub(b);
28
26
  else if (b instanceof Array)return b.map(n=>sub(n,a));
29
27
  }
30
- else if(a instanceof Complex||a instanceof Matrix){
28
+ else if(a.isComplex?.()||a.isMatrix?.()){
31
29
  if(b instanceof Array)return b.map(n=>a.clone.sub(n));
32
30
  return a.clone.sub(b);
33
31
  }
@@ -45,11 +43,11 @@ const _sub=(a,b)=>{
45
43
  const _mul=(a,b)=>{
46
44
  if(typeof(a)==="number"){
47
45
  if (typeof b == "number") return a * b;
48
- else if (b instanceof Complex)return complex(a * b.a,a * b.b);
49
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).mul(b);
46
+ else if (b.isComplex?.())return new b.constructor(a * b.a,a * b.b);
47
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).mul(b);
50
48
  else if (b instanceof Array)return b.map(n=>mul(a,n));
51
49
  }
52
- else if(a instanceof Complex||a instanceof Matrix){
50
+ else if(a.isComplex?.()||a.isMatrix?.()){
53
51
  if(b instanceof Array)return b.map(n=>a.clone.mul(n));
54
52
  return a.clone.mul(b);
55
53
  }
@@ -67,11 +65,11 @@ const _mul=(a,b)=>{
67
65
  const _div=(a,b)=>{
68
66
  if(typeof(a)==="number"){
69
67
  if (typeof b == "number") return a / b;
70
- else if (b instanceof Complex)return complex(a / b.a,a / b.b);
71
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).div(b);
68
+ else if (b.isComplex?.())return new b.constructor(a / b.a,a / b.b);
69
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).div(b);
72
70
  else if (b instanceof Array)return b.map(n=>div(a,n));
73
71
  }
74
- else if(a instanceof Complex||a instanceof Matrix){
72
+ else if(a.isComplex?.()||a.isMatrix?.()){
75
73
  if(b instanceof Array)return b.map(n=>a.clone.div(n));
76
74
  return a.clone.div(b);
77
75
  }
@@ -89,11 +87,11 @@ const _div=(a,b)=>{
89
87
  const _modulo=(a,b)=>{
90
88
  if(typeof(a)==="number"){
91
89
  if (typeof b == "number") return a % b;
92
- else if (b instanceof Complex)return complex(a % b.a,a % b.b);
93
- else if (b instanceof Matrix) return Matrix.nums(b.rows, b.cols, a).modulo(b);
90
+ else if (b.isComplex?.())return new b.constructor(a % b.a,a % b.b);
91
+ else if (b.isMatrix?.()) return b.constructor.nums(b.rows, b.cols, a).modulo(b);
94
92
  else if (b instanceof Array)return b.map(n=>div(a,n));
95
93
  }
96
- else if(a instanceof Complex||a instanceof Matrix){
94
+ else if(a.isComplex?.()||a.isMatrix?.()){
97
95
  if(b instanceof Array)return b.map(n=>a.clone.div(n));
98
96
  return a.clone.div(b);
99
97
  }
@@ -1,9 +1,6 @@
1
- import { Matrix } from "../matrix/index.js";
2
- import { Complex , complex } from "../complex/index.js";
3
1
  import {ln,e,cos,sin,sqrt,cosh,sinh} from "../functions/index.js";
4
2
  import { Fixed } from "../functions/helper.js";
5
3
  // To generalise
6
-
7
4
  const mapfun=(fun,...X)=>{
8
5
  const Y=X.map(x=>{
9
6
  if(x===null) return fun(null);
@@ -12,10 +9,9 @@ const mapfun=(fun,...X)=>{
12
9
  if(ArrayBuffer.isView(x)) return x.map(n=>fun(n));
13
10
  if(x instanceof Set) return new Set(mapfun(fun,...[...x]));
14
11
  if(x instanceof Map) return new Map([...x].map(n=>[n[0],mapfun(fun,n[1])]));
15
- if(x instanceof Matrix){
16
- return new Matrix(x.rows,x.cols, mapfun(x.arr.flat(1)))
17
- }
18
- if(x instanceof Complex){
12
+ if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, mapfun(x.arr.flat(1)))
13
+ if(x.isComplex?.()){
14
+ const complex = (...args) => new x.constructor(...args)
19
15
  const [a,b,z,phi]=[x.a,x.b,x.z,x.phi];
20
16
  switch(fun){
21
17
  case Math.log: return complex(ln(z),phi); // Done