ziko 0.51.0 → 0.52.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.cjs +1638 -1456
- package/dist/ziko.js +1446 -1385
- package/dist/ziko.min.js +2 -2
- package/dist/ziko.mjs +1446 -1385
- package/package.json +1 -1
- package/src/data/converter/csv.js +1 -1
- package/src/{__helpers__ → helpers}/register/index.js +1 -1
- package/src/hooks/use-ipc.js +1 -3
- package/src/math/adapted/index.js +7 -0
- package/src/math/complex/index.js +7 -5
- package/src/math/discret/Conversion/index.js +1 -1
- package/src/math/discret/Logic/index.js +1 -1
- package/src/math/functions/helper.js +68 -17
- package/src/math/functions/index.js +11 -11
- package/src/math/functions/primitives/index.js +176 -0
- package/src/math/mapfun/index.js +19 -0
- 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/index.js +1 -1
- package/src/math/utils/mapfun.js +31 -35
- package/src/ui/constructors/UIElement.js +9 -9
- package/src/ui/constructors/UIElementCore.js +2 -2
- package/types/math/adapted/index.d.ts +4 -0
- package/types/math/complex/index.d.ts +1 -1
- package/types/math/mapfun/index.d.ts +87 -0
- package/src/math/matrix/Matrix.js +0 -675
- /package/src/{__helpers__ → helpers}/checkers/index.js +0 -0
- /package/src/{__helpers__ → helpers}/index.js +0 -0
- /package/src/{__helpers__ → helpers}/register/register-to-class.js +0 -0
- /package/src/{__helpers__ → helpers}/register/register-to-instance.js +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ziko",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.0",
|
|
4
4
|
"description": "A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"front-end",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Matrix } from "../../math/matrix/
|
|
1
|
+
import { Matrix } from "../../math/matrix/matrix.js"
|
|
2
2
|
const csv2arr = (csv, delimiter = ",")=>csv.trim().trimEnd().split("\n").map(n=>n.split(delimiter));
|
|
3
3
|
const csv2matrix = (csv, delimiter = ",")=>new Matrix(csv2arr(csv,delimiter));
|
|
4
4
|
const csv2object = (csv, delimiter = ",") => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { register_to_class } from "./register-to-class.js";
|
|
2
|
-
import { register_to_instance } from "./register-to-instance"; // Not Overridable
|
|
2
|
+
import { register_to_instance } from "./register-to-instance.js"; // Not Overridable
|
|
3
3
|
export const register = (target, ...mixins) => {
|
|
4
4
|
console.log(target)
|
|
5
5
|
// return register_to_class(target, ...mixins)
|
package/src/hooks/use-ipc.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Random } from "../math/random/index.js";
|
|
2
|
-
|
|
3
1
|
class UseIPC {
|
|
4
2
|
#channel;
|
|
5
3
|
#eventData;
|
|
@@ -11,7 +9,7 @@ class UseIPC {
|
|
|
11
9
|
this.#channel = new BroadcastChannel(name);
|
|
12
10
|
this.#eventData = new Map();
|
|
13
11
|
this.#handlers = new Map(); // Map<event, Array<{fn, rooms}>>
|
|
14
|
-
this.#uuid = "ziko-channel:" +
|
|
12
|
+
this.#uuid = "ziko-channel:" + (Math.random()*10e16); // To Be Replaced by UUID
|
|
15
13
|
this.#subscribers = new Set([this.#uuid]);
|
|
16
14
|
this.#currentRooms = new Set();
|
|
17
15
|
this.#channel.addEventListener("message", (e) => {
|
|
@@ -12,7 +12,6 @@ import{
|
|
|
12
12
|
sqrt,
|
|
13
13
|
ln
|
|
14
14
|
}from "../functions/index.js"
|
|
15
|
-
import { Matrix } from "../matrix/index.js";
|
|
16
15
|
import {sum,prod,deg2rad} from "../utils/index.js";
|
|
17
16
|
class Complex{
|
|
18
17
|
constructor(a = 0, b = 0) {
|
|
@@ -42,8 +41,8 @@ class Complex{
|
|
|
42
41
|
this.a=a.b/tan(a.phi);
|
|
43
42
|
}
|
|
44
43
|
else if(("z" in a && "phi" in a)){
|
|
45
|
-
this.a=a.z*cos(a.phi);
|
|
46
|
-
this.
|
|
44
|
+
this.a = +a.z*cos(a.phi).toFixed(15);
|
|
45
|
+
this.b = +a.z*sin(a.phi).toFixed(15);
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
else if(typeof(a)==="number"&&typeof(b)==="number"){
|
|
@@ -51,6 +50,9 @@ class Complex{
|
|
|
51
50
|
this.b = +b.toFixed(32);
|
|
52
51
|
}
|
|
53
52
|
}
|
|
53
|
+
get __mapfun__(){
|
|
54
|
+
return true
|
|
55
|
+
}
|
|
54
56
|
isComplex(){
|
|
55
57
|
return true
|
|
56
58
|
}
|
|
@@ -183,10 +185,10 @@ class Complex{
|
|
|
183
185
|
}
|
|
184
186
|
const complex=(a,b)=>{
|
|
185
187
|
if((a instanceof Array||ArrayBuffer.isView(a)) && (b instanceof Array||ArrayBuffer.isView(a)))return a.map((n,i)=>complex(a[i],b[i]));
|
|
186
|
-
if(a
|
|
188
|
+
if(a.isMatrix?.() && b.isMatrix?.()){
|
|
187
189
|
if((a.shape[0]!==b.shape[0])||(a.shape[1]!==b.shape[1]))return Error(0)
|
|
188
190
|
const arr=a.arr.map((n,i)=>complex(a.arr[i],b.arr[i]))
|
|
189
|
-
return new
|
|
191
|
+
return new a.constructor(a.rows,a.cols,...arr)
|
|
190
192
|
}
|
|
191
193
|
return new Complex(a,b)
|
|
192
194
|
}
|
|
@@ -1,30 +1,81 @@
|
|
|
1
1
|
const {PI, cos, sin, tan, acos, asin, atan, cosh, sinh, tanh, acosh, asinh, atanh, log} = Math
|
|
2
2
|
export let Fixed={
|
|
3
|
-
cos
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
cos : x=> {
|
|
4
|
+
if(x.isComplex?.()) return new x.constructor(
|
|
5
|
+
cos(x.a)*cosh(x.b),
|
|
6
|
+
-(sin(x.a)*sinh(x.b))
|
|
7
|
+
);
|
|
8
|
+
return cos(x)
|
|
9
|
+
},
|
|
10
|
+
sin : x=>{
|
|
11
|
+
if(x?.isComplex) return new x.constructor(
|
|
12
|
+
sin(x.a)*cosh(x.b),
|
|
13
|
+
cos(x.a)*sinh(x.b)
|
|
14
|
+
);
|
|
15
|
+
return sin(x)
|
|
16
|
+
},
|
|
17
|
+
tan : x=>{
|
|
18
|
+
if(x?.isComplex){
|
|
19
|
+
const DEN = cos(2*x.a)+cosh(2*x.b);
|
|
20
|
+
return new x.constructor(
|
|
21
|
+
sin(2*x.a)/DEN,
|
|
22
|
+
sinh(2*x.b)/DEN
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
return tan(x)
|
|
26
|
+
},
|
|
6
27
|
sinc: x => sin(PI*x)/(PI*x),
|
|
7
28
|
sec: x => 1/cos(x),
|
|
8
29
|
csc: x => 1/sin(x),
|
|
9
30
|
cot: x => 1/tan(x),
|
|
10
|
-
acos
|
|
11
|
-
|
|
12
|
-
|
|
31
|
+
acos: x=>{
|
|
32
|
+
if(x?.isComplex) return
|
|
33
|
+
return sin(x)
|
|
34
|
+
},
|
|
35
|
+
asin: x=>{
|
|
36
|
+
if(x?.isComplex) return
|
|
37
|
+
return sin(x)
|
|
38
|
+
},
|
|
39
|
+
atan: x=>{
|
|
40
|
+
if(x?.isComplex) return
|
|
41
|
+
return sin(x)
|
|
42
|
+
},
|
|
13
43
|
acot: x => PI/2-atan(x),
|
|
14
|
-
cosh
|
|
15
|
-
|
|
16
|
-
|
|
44
|
+
cosh: x=>{
|
|
45
|
+
if(x?.isComplex) return new x.constructor(
|
|
46
|
+
cosh(x.a)*cos(x.b),
|
|
47
|
+
sinh(x.a)*sin(x.b)
|
|
48
|
+
);
|
|
49
|
+
return cosh(x)
|
|
50
|
+
},
|
|
51
|
+
sinh: x=>{
|
|
52
|
+
if(x?.isComplex) return new x.constructor(
|
|
53
|
+
sinh(x.a)*cos(x.b),
|
|
54
|
+
cosh(x.a)*sin(x.b)
|
|
55
|
+
);
|
|
56
|
+
return sinh(x)
|
|
57
|
+
},
|
|
58
|
+
tanh: x=>{
|
|
59
|
+
if(x?.isComplex){
|
|
60
|
+
const DEN=cosh(2*a)+cos(2*b);
|
|
61
|
+
return new x.constructor(
|
|
62
|
+
sinh(2*a)/DEN,
|
|
63
|
+
sin(2*b)/DEN
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
return tanh(x)
|
|
67
|
+
},
|
|
17
68
|
coth: n => (1/2*log((1+n)/(1-n))),
|
|
18
69
|
acosh,
|
|
19
70
|
asinh,
|
|
20
71
|
atanh,
|
|
21
72
|
}
|
|
22
73
|
|
|
23
|
-
Fixed = new Proxy(Fixed, {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
})
|
|
74
|
+
// Fixed = new Proxy(Fixed, {
|
|
75
|
+
// get(target, prop) {
|
|
76
|
+
// if(prop in target){
|
|
77
|
+
// return x => + target[prop](x).toFixed(15);
|
|
78
|
+
// }
|
|
79
|
+
// return undefined;
|
|
80
|
+
// }
|
|
81
|
+
// })
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { Fixed } from "./helper.js";
|
|
2
|
-
import {
|
|
3
|
-
import { mapfun } from "../utils/mapfun.js";
|
|
2
|
+
import { mapfun } from "../mapfun/index.js";
|
|
4
3
|
import {
|
|
5
4
|
min,
|
|
6
5
|
max
|
|
7
|
-
}
|
|
6
|
+
}
|
|
7
|
+
from "../statistics/index.js";
|
|
8
8
|
|
|
9
9
|
const abs=(...x)=>mapfun(Math.abs,...x);
|
|
10
10
|
const sqrt=(...x)=>mapfun(Math.sqrt,...x);
|
|
11
11
|
const pow=(x,n)=>{
|
|
12
12
|
if(typeof x === "number"){
|
|
13
13
|
if(typeof n === "number")return Math.pow(x,n);
|
|
14
|
-
else if(n
|
|
14
|
+
else if(n?.isComplex?.())return n.constructor.fromExpo(x**n.a,n.b*ln(x))
|
|
15
15
|
else return mapfun(a=>pow(x,a),...n);
|
|
16
16
|
}
|
|
17
|
-
else if(x
|
|
18
|
-
if(typeof n === "number")return
|
|
19
|
-
else if(n
|
|
17
|
+
else if(x.isComplex?.()){
|
|
18
|
+
if(typeof n === "number")return x.constructor.fromExpo(x.z**n,x.phi*n);
|
|
19
|
+
else if(n.isComplex?.())return x.constructor.fromExpo(
|
|
20
20
|
x.z**n.a*e(-x.phi*n.b),
|
|
21
21
|
ln(x.z)*n.b+n.a*x.phi
|
|
22
22
|
)
|
|
@@ -38,8 +38,8 @@ const sqrtn=(x,n)=>{
|
|
|
38
38
|
if(typeof n === "number")return Math.pow(x,1/n);
|
|
39
39
|
else return mapfun(a=>sqrtn(x,a),...n);
|
|
40
40
|
}
|
|
41
|
-
else if(x
|
|
42
|
-
if(typeof n === "number")return
|
|
41
|
+
else if(x.isComplex?.()){
|
|
42
|
+
if(typeof n === "number")return x.constructor.fromExpo(sqrtn(x.z,n),x.phi/n);
|
|
43
43
|
else return mapfun(a=>sqrtn(x,a),...n);
|
|
44
44
|
}
|
|
45
45
|
else if(x instanceof Array){
|
|
@@ -81,8 +81,8 @@ const atan2=(x,y,rad=true)=>{
|
|
|
81
81
|
if(typeof y === "number")return rad?Math.atan2(x,y):Math.atan2(x,y)*180/Math.PI;
|
|
82
82
|
else return mapfun(a=>atan2(x,a,rad),...y);
|
|
83
83
|
}
|
|
84
|
-
// else if(x
|
|
85
|
-
// if(typeof n === "number")return
|
|
84
|
+
// else if(x.isComplex?.()){
|
|
85
|
+
// if(typeof n === "number")return x.constructor.fromExpo(x.z**n,x.phi*n);
|
|
86
86
|
// else return mapfun(a=>pow(x,a),...n);
|
|
87
87
|
// }
|
|
88
88
|
else if(x instanceof Array){
|
|
@@ -0,0 +1,176 @@
|
|
|
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
|
+
)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { is_primitive } from "../../helpers/checkers/index.js";
|
|
2
|
+
export const mapfun=(fun,...X)=>{
|
|
3
|
+
const Y=X.map(x=>{
|
|
4
|
+
if(is_primitive(x) || x?.__mapfun__) return fun(x)
|
|
5
|
+
if(x instanceof Array) return x.map(n=>mapfun(fun,n));
|
|
6
|
+
if(ArrayBuffer.isView(x)) return x.map(n=>fun(n));
|
|
7
|
+
if(x instanceof Set) return new Set(mapfun(fun,...[...x]));
|
|
8
|
+
if(x instanceof Map) return new Map([...x].map(n=>[n[0],mapfun(fun,n[1])]));
|
|
9
|
+
if(x.isMatrix?.()) return new x.constructor(x.rows, x.cols, mapfun(x.arr.flat(1)))
|
|
10
|
+
else if(x instanceof Object){
|
|
11
|
+
return Object.fromEntries(
|
|
12
|
+
Object.entries(x).map(
|
|
13
|
+
n=>n=[n[0],mapfun(fun,n[1])]
|
|
14
|
+
)
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return Y.length==1? Y[0]: Y;
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { add, sub, mul } from "../../utils/index.js";
|
|
2
|
+
import { pow } from "../../functions/index.js";
|
|
3
|
+
export function matrix_det(M) {
|
|
4
|
+
if (!M.isSquare) return new Error("is not square matrix");
|
|
5
|
+
if (M.rows == 1) return M.arr[0][0];
|
|
6
|
+
function determinat(M) {
|
|
7
|
+
if (M.length == 2) {
|
|
8
|
+
if (M.flat(1).some((n) => n?.isMatrix?.())) {
|
|
9
|
+
console.warn("Tensors are not completely supported yet ...");
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
return sub(mul(M[0][0],M[1][1]),mul(M[0][1],M[1][0]))
|
|
13
|
+
}
|
|
14
|
+
var answer = 0;
|
|
15
|
+
for (var i = 0; i < M.length; i++) {
|
|
16
|
+
//console.log(M[0][i]);
|
|
17
|
+
/*answer = answer.add(
|
|
18
|
+
pow(-1, i)
|
|
19
|
+
.mul(M[0][i])
|
|
20
|
+
.mul(determinat(deleteRowAndColumn(M, i)))
|
|
21
|
+
);*/
|
|
22
|
+
//const to_be_added=add(mul(pow(-1, i),mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
|
|
23
|
+
const to_be_added=add(mul(pow(-1, i),mul(M[0][i],determinat(deleteRowAndColumn(M, i)))));
|
|
24
|
+
answer=add(answer,to_be_added)
|
|
25
|
+
}
|
|
26
|
+
return answer;
|
|
27
|
+
}
|
|
28
|
+
return determinat(M.arr);
|
|
29
|
+
}
|
|
30
|
+
function deleteRowAndColumn(M, index) {
|
|
31
|
+
var temp = [];
|
|
32
|
+
for (let i = 0; i < M.length; i++) temp.push(M[i].slice(0));
|
|
33
|
+
temp.splice(0, 1);
|
|
34
|
+
for (let i = 0; i < temp.length; i++) temp[i].splice(index, 1);
|
|
35
|
+
return temp;
|
|
36
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export function matrix_inverse(M) {
|
|
2
|
+
if(M.row !== M.cols) throw Error('is not a square matrix"')
|
|
3
|
+
if (M.det === 0) throw Error("determinant should not equal 0");
|
|
4
|
+
const { arr } = M
|
|
5
|
+
if (arr.length !== arr[0].length) return;
|
|
6
|
+
var i = 0, ii = 0, j = 0, dim = arr.length, e = 0;
|
|
7
|
+
var I = [], C = [];
|
|
8
|
+
for (i = 0; i < dim; i += 1) {
|
|
9
|
+
I[I.length] = [];
|
|
10
|
+
C[C.length] = [];
|
|
11
|
+
for (j = 0; j < dim; j += 1) {
|
|
12
|
+
if (i == j) I[i][j] = 1;
|
|
13
|
+
else I[i][j] = 0;
|
|
14
|
+
C[i][j] = arr[i][j];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
for (i = 0; i < dim; i += 1) {
|
|
18
|
+
e = C[i][i];
|
|
19
|
+
if (e == 0) {
|
|
20
|
+
for (ii = i + 1; ii < dim; ii += 1) {
|
|
21
|
+
if (C[ii][i] != 0) {
|
|
22
|
+
for (j = 0; j < dim; j++) {
|
|
23
|
+
e = C[i][j];
|
|
24
|
+
C[i][j] = C[ii][j];
|
|
25
|
+
C[ii][j] = e;
|
|
26
|
+
e = I[i][j];
|
|
27
|
+
I[i][j] = I[ii][j];
|
|
28
|
+
I[ii][j] = e;
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
e = C[i][i];
|
|
34
|
+
if (e == 0) return;
|
|
35
|
+
}
|
|
36
|
+
for (j = 0; j < dim; j++) {
|
|
37
|
+
C[i][j] = C[i][j] / e;
|
|
38
|
+
I[i][j] = I[i][j] / e;
|
|
39
|
+
}
|
|
40
|
+
for (ii = 0; ii < dim; ii++) {
|
|
41
|
+
if (ii == i) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
e = C[ii][i];
|
|
45
|
+
for (j = 0; j < dim; j++) {
|
|
46
|
+
C[ii][j] -= e * C[i][j];
|
|
47
|
+
I[ii][j] -= e * I[i][j];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return new M.constructor(I);
|
|
52
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function hstack(M1, M2){
|
|
2
|
+
M1 = M1.clone()
|
|
3
|
+
M2 = M2.clone()
|
|
4
|
+
if (M1.rows !== M2.rows) return;
|
|
5
|
+
let newArr = M1.arr;
|
|
6
|
+
for (let i = 0; i < M1.rows; i++)
|
|
7
|
+
for (let j = M1.cols; j < M1.cols + M2.cols; j++)
|
|
8
|
+
newArr[i][j] = M2.arr[i][j - M1.cols];
|
|
9
|
+
M1.cols += M2.cols;
|
|
10
|
+
return new M1.constructor(M1.rows, M1.cols, newArr.flat(1));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function vstack(M1, M2){
|
|
14
|
+
M1 = M1.clone()
|
|
15
|
+
M2 = M2.clone()
|
|
16
|
+
if (M1.cols !== M2.cols) return;
|
|
17
|
+
let newArr = M1.arr;
|
|
18
|
+
for (let i = M1.rows; i < M1.rows + M2.rows; i++) {
|
|
19
|
+
newArr[i] = [];
|
|
20
|
+
for (let j = 0; j < M1.cols; j++) newArr[i][j] = M2.arr[i - M1.rows][j];
|
|
21
|
+
}
|
|
22
|
+
M1.rows += M2.rows;
|
|
23
|
+
return new M1.constructor(M1.rows, M1.cols, newArr.flat(1));
|
|
24
|
+
}
|
package/src/math/matrix/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./matrix.js"
|