ziko 0.56.0 → 0.56.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.
- package/package.json +1 -1
- package/src/math/complex/helpers/constructor.js +38 -0
- package/src/math/complex/helpers/index.js +1 -0
- package/src/math/complex/index.js +18 -47
- package/src/math/functions/arithmetic/index.js +27 -53
- package/src/math/matrix/helpers/constructor.js +2 -2
- package/src/math/matrix/index.js +75 -146
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ziko",
|
|
3
|
-
"version": "0.56.
|
|
3
|
+
"version": "0.56.1",
|
|
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",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export const complex_constructor = (Complex, a, b) => {
|
|
2
|
+
let _a, _b;
|
|
3
|
+
if (a instanceof Complex) {
|
|
4
|
+
_a = a.a;
|
|
5
|
+
_b = a.b;
|
|
6
|
+
}
|
|
7
|
+
else if (typeof a === "object") {
|
|
8
|
+
if ("a" in a && "b" in a) {
|
|
9
|
+
_a = a.a;
|
|
10
|
+
_b = a.b;
|
|
11
|
+
}
|
|
12
|
+
else if ("a" in a && "z" in a) {
|
|
13
|
+
_a = a.a;
|
|
14
|
+
_b = Math.sqrt(a.z ** 2 - a.a ** 2);
|
|
15
|
+
}
|
|
16
|
+
else if ("a" in a && "phi" in a) {
|
|
17
|
+
_a = a.a;
|
|
18
|
+
_b = a.a * Math.tan(a.phi);
|
|
19
|
+
}
|
|
20
|
+
else if ("b" in a && "z" in a) {
|
|
21
|
+
_b = a.b;
|
|
22
|
+
_a = Math.sqrt(a.z ** 2 - a.b ** 2);
|
|
23
|
+
}
|
|
24
|
+
else if ("b" in a && "phi" in a) {
|
|
25
|
+
_b = b;
|
|
26
|
+
_a = a.b / Math.tan(a.phi);
|
|
27
|
+
}
|
|
28
|
+
else if ("z" in a && "phi" in a) {
|
|
29
|
+
_a = +a.z * Math.cos(a.phi).toFixed(15);
|
|
30
|
+
_b = +a.z * Math.sin(a.phi).toFixed(15);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else if (typeof a === "number" && typeof b === "number") {
|
|
34
|
+
_a = +a.toFixed(32);
|
|
35
|
+
_b = +b.toFixed(32);
|
|
36
|
+
}
|
|
37
|
+
return [_a, _b]
|
|
38
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './constructor.js'
|
|
@@ -1,40 +1,11 @@
|
|
|
1
1
|
import { Random } from "../random/index.js";
|
|
2
|
+
import { complex_constructor } from "./helpers/index.js";
|
|
2
3
|
class Complex{
|
|
3
4
|
constructor(a = 0, b = 0) {
|
|
4
|
-
|
|
5
|
-
this.a
|
|
6
|
-
this.b
|
|
7
|
-
|
|
8
|
-
else if(typeof(a)==="object"){
|
|
9
|
-
if(("a" in a && "b" in a)){
|
|
10
|
-
this.a = a.a;
|
|
11
|
-
this.b = a.b;
|
|
12
|
-
}
|
|
13
|
-
else if(("a" in a && "z" in a)){
|
|
14
|
-
this.a = a.a;
|
|
15
|
-
this.b = Math.sqrt((a.z**2)-(a.a**2));
|
|
16
|
-
}
|
|
17
|
-
else if(("a" in a && "phi" in a)){
|
|
18
|
-
this.a = a.a;
|
|
19
|
-
this.b = a.a * Math.tan(a.phi);
|
|
20
|
-
}
|
|
21
|
-
else if(("b" in a && "z" in a)){
|
|
22
|
-
this.b = a.b;
|
|
23
|
-
this.a = Math.sqrt((a.z**2)-(a.b**2));
|
|
24
|
-
}
|
|
25
|
-
else if(("b" in a && "phi" in a)){
|
|
26
|
-
this.b = b;
|
|
27
|
-
this.a = a.b / Math.tan(a.phi);
|
|
28
|
-
}
|
|
29
|
-
else if(("z" in a && "phi" in a)){
|
|
30
|
-
this.a = + a.z * Math.cos(a.phi).toFixed(15);
|
|
31
|
-
this.b = + a.z * Math.sin(a.phi).toFixed(15);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
else if(typeof(a)==="number" && typeof(b)==="number"){
|
|
35
|
-
this.a = + a.toFixed(32);
|
|
36
|
-
this.b = + b.toFixed(32);
|
|
37
|
-
}
|
|
5
|
+
[
|
|
6
|
+
this.a,
|
|
7
|
+
this.b
|
|
8
|
+
] = complex_constructor(Complex, a, b)
|
|
38
9
|
}
|
|
39
10
|
get __mapfun__(){
|
|
40
11
|
return true
|
|
@@ -54,6 +25,19 @@ class Complex{
|
|
|
54
25
|
: (str = `-${Math.abs(this.b)}*i`);
|
|
55
26
|
return str;
|
|
56
27
|
}
|
|
28
|
+
serialize() {
|
|
29
|
+
return JSON.stringify({
|
|
30
|
+
type : 'complex',
|
|
31
|
+
data : this
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
static deserialize(json){
|
|
35
|
+
if(typeof json === 'string') json = JSON.parse(json);
|
|
36
|
+
let {data, type} = json;
|
|
37
|
+
return (type === 'complex' && ('a' in data) && ('b' in data))
|
|
38
|
+
? new Complex(data.a, data.b)
|
|
39
|
+
: TypeError('Not a valid complex')
|
|
40
|
+
}
|
|
57
41
|
toFixed(n){
|
|
58
42
|
this.a = + this.a.toFixed(n);
|
|
59
43
|
this.b = + this.b.toFixed(n);
|
|
@@ -165,19 +149,6 @@ class Complex{
|
|
|
165
149
|
get expo() {
|
|
166
150
|
return [this.z, this.phi];
|
|
167
151
|
}
|
|
168
|
-
static add(c,...z) {
|
|
169
|
-
return c.clone().add(...z);
|
|
170
|
-
}
|
|
171
|
-
static sub(c,...z) {
|
|
172
|
-
return c.clone().sub(...z);
|
|
173
|
-
}
|
|
174
|
-
static mul(c,...z) {
|
|
175
|
-
return c.clone().mul(...z);
|
|
176
|
-
}
|
|
177
|
-
static div(c,...z) {
|
|
178
|
-
return c.clone().div(...z);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
152
|
nthr(n=2){
|
|
182
153
|
return complex({z: this.z ** (1/n), phi: this.phi / n});
|
|
183
154
|
}
|
|
@@ -1,82 +1,56 @@
|
|
|
1
|
-
const
|
|
1
|
+
const arithmetic_helper=(op, x, y)=>{
|
|
2
2
|
if(typeof x === 'number'){
|
|
3
|
-
if(typeof y === 'number')
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
if(typeof y === 'number'){
|
|
4
|
+
switch(op){
|
|
5
|
+
case 'add' : return x + y;
|
|
6
|
+
case 'sub' : return x - y;
|
|
7
|
+
case 'mul' : return x * y;
|
|
8
|
+
case 'div' : return x / y;
|
|
9
|
+
case 'modulo' : return x % y;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
if(y?.isComplex?.()) x = new y.constructor(x, 0);
|
|
13
|
+
if(y?.isMatrix?.()) x = y.constructor.nums(y.rows, y.cols, x);
|
|
14
|
+
return x[op](y)
|
|
15
|
+
}
|
|
16
|
+
if(x?.isComplex?.()){
|
|
17
|
+
if(typeof y === 'number' || y?.isComplex?.()) return x.clone()[op](y);
|
|
18
|
+
if(y?.isMatrix?.()){
|
|
19
|
+
x = y.constructor.nums(y.rows, y.cols, x);
|
|
20
|
+
return x.clone()[op](y)
|
|
6
21
|
}
|
|
7
22
|
}
|
|
8
|
-
if(x
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const _sub = (x, y) =>{
|
|
14
|
-
if(typeof x === 'number'){
|
|
15
|
-
if(typeof y === 'number') return x - y;
|
|
16
|
-
if(y.isComplex?.()) return new y.constructor(x - y.a, y.b);
|
|
17
|
-
}
|
|
18
|
-
if(x.isComplex?.()){
|
|
19
|
-
if(typeof y === 'number' || y.isComplex?.()) return x.clone().sub(y);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const _mul = (x, y) =>{
|
|
24
|
-
if(typeof x === 'number'){
|
|
25
|
-
if(typeof y === 'number') return x * y;
|
|
26
|
-
if(y.isComplex?.()) return y.clone().mul(x);
|
|
27
|
-
}
|
|
28
|
-
if(x.isComplex?.()){
|
|
29
|
-
if(typeof y === 'number' || y.isComplex?.()) return x.clone().mul(y);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const _div = (x, y) =>{
|
|
34
|
-
if(typeof x === 'number'){
|
|
35
|
-
if(typeof y === 'number') return x / y;
|
|
36
|
-
if(y.isComplex?.()) return new y.constructor(x, 0).div(y)
|
|
37
|
-
}
|
|
38
|
-
if(x.isComplex?.()){
|
|
39
|
-
if(typeof y === 'number' || y.isComplex?.()) return x.clone().mul(y);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const _modulo = (x, y) =>{
|
|
44
|
-
if(typeof x === 'number'){
|
|
45
|
-
if(typeof y === 'number') return x % y;
|
|
46
|
-
if(y.isComplex?.()) return new y.constructor(x, 0).modulo(y)
|
|
47
|
-
}
|
|
48
|
-
if(x.isComplex?.()){
|
|
49
|
-
if(typeof y === 'number' || y.isComplex?.()) return x.clone().modulo(y);
|
|
50
|
-
}
|
|
23
|
+
if(x?.isMatrix?.()){
|
|
24
|
+
return x.clone()[op](y)
|
|
25
|
+
}
|
|
51
26
|
}
|
|
52
|
-
|
|
53
27
|
export const add=(a,...b)=>{
|
|
54
28
|
let res = a;
|
|
55
29
|
for(let i=0; i<b.length; i++)
|
|
56
|
-
res =
|
|
30
|
+
res = arithmetic_helper('add', res, b[i])
|
|
57
31
|
return res;
|
|
58
32
|
}
|
|
59
33
|
export const sub=(a,...b)=>{
|
|
60
34
|
let res = a;
|
|
61
35
|
for(let i=0; i<b.length; i++)
|
|
62
|
-
res =
|
|
36
|
+
res = arithmetic_helper('sub', res, b[i])
|
|
63
37
|
return res;
|
|
64
38
|
}
|
|
65
39
|
export const mul=(a,...b)=>{
|
|
66
40
|
let res = a;
|
|
67
41
|
for(let i=0; i<b.length; i++)
|
|
68
|
-
res =
|
|
42
|
+
res = arithmetic_helper('mul', res, b[i])
|
|
69
43
|
return res;
|
|
70
44
|
}
|
|
71
45
|
export const div=(a,...b)=>{
|
|
72
46
|
let res = a;
|
|
73
47
|
for(let i=0; i<b.length; i++)
|
|
74
|
-
res =
|
|
48
|
+
res = arithmetic_helper('div', res, b[i])
|
|
75
49
|
return res;
|
|
76
50
|
}
|
|
77
51
|
export const modulo=(a,...b)=>{
|
|
78
52
|
let res = a;
|
|
79
53
|
for(let i=0; i<b.length; i++)
|
|
80
|
-
res =
|
|
54
|
+
res = arithmetic_helper('modulo', res, b[i])
|
|
81
55
|
return res;
|
|
82
56
|
}
|
package/src/math/matrix/index.js
CHANGED
|
@@ -28,7 +28,7 @@ class Matrix{
|
|
|
28
28
|
this.rows,
|
|
29
29
|
this.cols,
|
|
30
30
|
this.arr
|
|
31
|
-
] = matrix_constructor(rows, cols, element);
|
|
31
|
+
] = matrix_constructor(Matrix, rows, cols, element);
|
|
32
32
|
this.#maintain();
|
|
33
33
|
}
|
|
34
34
|
isMatrix(){
|
|
@@ -88,9 +88,6 @@ class Matrix{
|
|
|
88
88
|
}
|
|
89
89
|
return new Matrix(newRow, newCol, newArr.flat(1));
|
|
90
90
|
}
|
|
91
|
-
static slice(m1,r0=0, c0=0, r1=this.rows-1, c1=this.cols-1) {
|
|
92
|
-
return m1.slice(r0, c0, r1, c1);
|
|
93
|
-
}
|
|
94
91
|
reshape(newRows, newCols) {
|
|
95
92
|
if(!(newRows * newCols === this.rows * this.cols)) throw Error('size not matched')
|
|
96
93
|
return new Matrix(newRows, newCols, this.arr.flat(1));
|
|
@@ -155,35 +152,34 @@ class Matrix{
|
|
|
155
152
|
this.#maintain();
|
|
156
153
|
return this;
|
|
157
154
|
}
|
|
158
|
-
static hstack(matrix,...matrices) {
|
|
159
|
-
return matrix.clone().hstack(...matrices);
|
|
160
|
-
}
|
|
161
155
|
vstack(...matrices){
|
|
162
156
|
const M=[this, ...matrices].reduce((a,b)=>vstack(a, b));
|
|
163
157
|
Object.assign(this, M);
|
|
164
158
|
this.#maintain();
|
|
165
159
|
return this;
|
|
166
160
|
}
|
|
167
|
-
static vstack(matrix,...matrices) {
|
|
168
|
-
return matrix.clone().vstack(...matrices);
|
|
169
|
-
}
|
|
170
161
|
hqueue(...matrices){
|
|
171
162
|
const M=[this, ...matrices].reverse().reduce((a,b)=>hstack(a, b));
|
|
172
163
|
Object.assign(this, M)
|
|
173
164
|
return this;
|
|
174
165
|
}
|
|
175
|
-
static hqueue(matrix,...matrices) {
|
|
176
|
-
return matrix.clone().hqueue(...matrices);
|
|
177
|
-
}
|
|
178
166
|
vqueue(...matrices){
|
|
179
167
|
const M=[this,...matrices].reverse().reduce((a, b)=>vstack(a, b));
|
|
180
168
|
Object.assign(this, M)
|
|
181
169
|
return this;
|
|
182
170
|
}
|
|
183
|
-
|
|
184
|
-
|
|
171
|
+
forEach(fn){
|
|
172
|
+
this.arr.flat(1).forEach(fn);
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
forEachRow(fn){
|
|
176
|
+
this.arr.forEach(fn);
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
forEachCol(fn){
|
|
180
|
+
this.clone().T.forEachRow(fn);
|
|
181
|
+
return this
|
|
185
182
|
}
|
|
186
|
-
|
|
187
183
|
apply(fn){
|
|
188
184
|
const arr = this.arr.flat(1).map(fn)
|
|
189
185
|
return new Matrix(
|
|
@@ -192,22 +188,13 @@ class Matrix{
|
|
|
192
188
|
arr
|
|
193
189
|
)
|
|
194
190
|
}
|
|
195
|
-
static apply(M, fn){
|
|
196
|
-
return M.clone().apply(fn)
|
|
197
|
-
}
|
|
198
191
|
applyRows(fn = ()=>{}){
|
|
199
192
|
this.arr = this.arr.map(fn)
|
|
200
193
|
return this;
|
|
201
194
|
}
|
|
202
|
-
static applyRows(M, fn){
|
|
203
|
-
return M.clone().applyRows(fn)
|
|
204
|
-
}
|
|
205
195
|
applyCols(fn){
|
|
206
196
|
return this.clone().T.applyRows(fn).T;
|
|
207
197
|
}
|
|
208
|
-
static applyCols(M, fn){
|
|
209
|
-
return M.clone().applyCols(fn);
|
|
210
|
-
}
|
|
211
198
|
sort(fn = ()=>{}){
|
|
212
199
|
const arr = this.arr.flat(1).sort(fn)
|
|
213
200
|
return new Matrix(
|
|
@@ -216,66 +203,38 @@ class Matrix{
|
|
|
216
203
|
arr
|
|
217
204
|
)
|
|
218
205
|
}
|
|
219
|
-
static sort(M, fn){
|
|
220
|
-
return M.clone().sort(fn)
|
|
221
|
-
}
|
|
222
206
|
shuffle(){
|
|
223
207
|
return this.sort(() => 0.5-Math.random())
|
|
224
208
|
}
|
|
225
|
-
static shuffle(M){
|
|
226
|
-
return M.clone().shuffle()
|
|
227
|
-
}
|
|
228
209
|
sortRows(fn = ()=>{}){
|
|
229
210
|
this.arr = this.arr.map(row => row.sort(fn))
|
|
230
211
|
return this;
|
|
231
212
|
}
|
|
232
|
-
static sortRows(M, fn){
|
|
233
|
-
return M.clone().sortRows(fn)
|
|
234
|
-
}
|
|
235
213
|
shuffleRows(){
|
|
236
214
|
return this.sortRows(() => 0.5-Math.random())
|
|
237
215
|
}
|
|
238
|
-
static shuffleRows(M){
|
|
239
|
-
return M.clone().shuffleRows()
|
|
240
|
-
}
|
|
241
216
|
sortCols(fn){
|
|
242
217
|
return this.clone().T.sortRows(fn).T;
|
|
243
218
|
}
|
|
244
|
-
static sortCols(M, fn){
|
|
245
|
-
return M.clone().sortCols(fn);
|
|
246
|
-
}
|
|
247
219
|
shuffleCols(){
|
|
248
220
|
return this.sortCols(() => 0.5-Math.random())
|
|
249
221
|
}
|
|
250
|
-
static shuffleCols(M){
|
|
251
|
-
return M.clone().shuffleCols()
|
|
252
|
-
}
|
|
253
|
-
|
|
254
222
|
reduce(fn, initialValue){
|
|
255
223
|
const value = initialValue
|
|
256
224
|
? this.arr.flat(1).reduce(fn, initialValue)
|
|
257
225
|
: this.arr.flat(1).reduce(fn);
|
|
258
226
|
return new Matrix([[value]])
|
|
259
227
|
}
|
|
260
|
-
static reduce(M, fn, initialValue){
|
|
261
|
-
return M.clone().reduce(fn, initialValue)
|
|
262
|
-
}
|
|
263
228
|
reduceRows(fn, initialValue){
|
|
264
229
|
const values = initialValue
|
|
265
230
|
? this.arr.map(row => row.reduce(fn, initialValue))
|
|
266
231
|
: this.arr.map(row => row.reduce(fn))
|
|
267
232
|
return new Matrix(1, this.cols, values)
|
|
268
233
|
}
|
|
269
|
-
static reduceRows(M, fn, initialValue){
|
|
270
|
-
return M.clone().reduceRows(fn, initialValue)
|
|
271
|
-
}
|
|
272
234
|
reduceCols(fn, initialValue){
|
|
273
235
|
return this.T.reduceRows(fn, initialValue).T
|
|
274
236
|
|
|
275
237
|
}
|
|
276
|
-
static reduceCols(M, fn, initialValue){
|
|
277
|
-
return M.clone().reduceCols(fn, initialValue)
|
|
278
|
-
}
|
|
279
238
|
filterRows(fn){
|
|
280
239
|
const mask = this.arr.map(n => n.some(m => fn(m)));
|
|
281
240
|
const arr = [];
|
|
@@ -284,16 +243,10 @@ class Matrix{
|
|
|
284
243
|
if(mask[i]) arr.push(this.arr[i])
|
|
285
244
|
return new Matrix(arr)
|
|
286
245
|
}
|
|
287
|
-
static filterCols(M, fn){
|
|
288
|
-
return M.clone().filterRows(fn)
|
|
289
|
-
}
|
|
290
246
|
filterCols(fn){
|
|
291
247
|
const arr = this.T.filterRows(fn);
|
|
292
248
|
return new Matrix(arr).T
|
|
293
249
|
}
|
|
294
|
-
static filterCols(M, fn){
|
|
295
|
-
return M.clone().filterCols(fn);
|
|
296
|
-
}
|
|
297
250
|
every(fn){
|
|
298
251
|
return this.arr.flat(1).every(fn)
|
|
299
252
|
}
|
|
@@ -395,7 +348,6 @@ class Matrix{
|
|
|
395
348
|
}
|
|
396
349
|
return true;
|
|
397
350
|
}
|
|
398
|
-
|
|
399
351
|
map(Imin, Imax, Fmin, Fmax) {
|
|
400
352
|
this.arr = map(this.arr, Imin, Imax, Fmin, Fmax)
|
|
401
353
|
return this;
|
|
@@ -412,18 +364,6 @@ class Matrix{
|
|
|
412
364
|
this.arr = clamp(this.arr, min, max)
|
|
413
365
|
return this;
|
|
414
366
|
}
|
|
415
|
-
static map(M, Imin, Imax, Fmin, Fmax) {
|
|
416
|
-
return M.clone().map(Imin, Imax, Fmin, Fmax)
|
|
417
|
-
}
|
|
418
|
-
static lerp(M, min, max) {
|
|
419
|
-
return M.clone().lerp(min, max)
|
|
420
|
-
}
|
|
421
|
-
static norm(M, min, max) {
|
|
422
|
-
return M.clone().norm(min, max)
|
|
423
|
-
}
|
|
424
|
-
static clamp(M, min, max) {
|
|
425
|
-
return M.clone().clamp(min, max)
|
|
426
|
-
}
|
|
427
367
|
toPrecision(p) {
|
|
428
368
|
for (let i = 0; i < this.cols; i++)
|
|
429
369
|
for (let j = 0; j < this.rows; j++)
|
|
@@ -456,15 +396,9 @@ class Matrix{
|
|
|
456
396
|
getCols(ci, cf = ci + 1) {
|
|
457
397
|
return this.slice(0, ci, this.rows, cf);
|
|
458
398
|
}
|
|
459
|
-
static getRows(m, ri, rf = ri + 1) {
|
|
460
|
-
return m.slice(ri, 0, rf, m.cols);
|
|
461
|
-
}
|
|
462
|
-
static getCols(m, ci, cf = ci + 1) {
|
|
463
|
-
return m.slice(0, ci, m.rows, cf);
|
|
464
|
-
}
|
|
465
399
|
#arithmetic(fn, ...matr){
|
|
466
400
|
for (let k = 0; k < matr.length; k++) {
|
|
467
|
-
if (typeof matr[k] == "number"||matr[k]
|
|
401
|
+
if (typeof matr[k] == "number" || matr[k]?.isComplex?.()) matr[k] = Matrix.nums(this.rows, this.cols, matr[k]);
|
|
468
402
|
for (let i = 0; i < this.rows; i++)
|
|
469
403
|
for (var j = 0; j < this.cols; j++)
|
|
470
404
|
this.arr[i][j] = fn(this.arr[i][j], matr[k].arr[i][j]);
|
|
@@ -474,33 +408,18 @@ class Matrix{
|
|
|
474
408
|
add(...matr) {
|
|
475
409
|
return this.#arithmetic(add, ...matr)
|
|
476
410
|
}
|
|
477
|
-
static add(m1, ...m2) {
|
|
478
|
-
return m1.clone().add(...m2);
|
|
479
|
-
}
|
|
480
411
|
sub(...matr) {
|
|
481
412
|
return this.#arithmetic(sub, ...matr)
|
|
482
413
|
}
|
|
483
|
-
static sub(m1, ...m2) {
|
|
484
|
-
return m1.clone().sub(...m2);
|
|
485
|
-
}
|
|
486
414
|
mul(...matr) {
|
|
487
415
|
return this.#arithmetic(mul, ...matr)
|
|
488
416
|
}
|
|
489
|
-
static mul(m1, ...m2) {
|
|
490
|
-
return m1.clone().mul(...m2);
|
|
491
|
-
}
|
|
492
417
|
div(...matr) {
|
|
493
418
|
return this.#arithmetic(div, ...matr)
|
|
494
419
|
}
|
|
495
|
-
static div(m1, ...m2) {
|
|
496
|
-
return m1.clone().div(...m2);
|
|
497
|
-
}
|
|
498
420
|
modulo(...matr) {
|
|
499
421
|
return this.#arithmetic(modulo, ...matr)
|
|
500
422
|
}
|
|
501
|
-
static modulo(m1, ...m2) {
|
|
502
|
-
return m1.clone().modulo(...m2);
|
|
503
|
-
}
|
|
504
423
|
dot(matrix) {
|
|
505
424
|
var res = [];
|
|
506
425
|
for (var i = 0; i < this.arr.length; i++) {
|
|
@@ -517,23 +436,24 @@ class Matrix{
|
|
|
517
436
|
}
|
|
518
437
|
return new Matrix(this.arr.length, matrix.arr[0].length, res.flat(1));
|
|
519
438
|
}
|
|
520
|
-
static dot(matrix1, matrix2) {
|
|
521
|
-
return matrix1.dot(matrix2);
|
|
522
|
-
}
|
|
523
439
|
pow(n) {
|
|
524
440
|
let a = this.clone(),
|
|
525
441
|
p = this.clone();
|
|
526
442
|
for (let i = 0; i < n - 1; i++) p = p.dot(a);
|
|
527
443
|
return p;
|
|
528
444
|
}
|
|
529
|
-
|
|
530
|
-
return m.clone().pow(n);
|
|
531
|
-
}
|
|
532
|
-
get somme() {
|
|
445
|
+
sum(){
|
|
533
446
|
let S = 0;
|
|
534
447
|
for (let i = 0; i < this.rows; i++)
|
|
535
448
|
for (let j = 0; j < this.cols; j++)
|
|
536
|
-
S
|
|
449
|
+
S = add(S, this.arr[i][j]);
|
|
450
|
+
return S;
|
|
451
|
+
}
|
|
452
|
+
prod(){
|
|
453
|
+
let S = 1;
|
|
454
|
+
for (let i = 0; i < this.rows; i++)
|
|
455
|
+
for (let j = 0; j < this.cols; j++)
|
|
456
|
+
S = mul(S, this.arr[i][j]);
|
|
537
457
|
return S;
|
|
538
458
|
}
|
|
539
459
|
hasComplex(){
|
|
@@ -578,53 +498,62 @@ class Matrix{
|
|
|
578
498
|
static fromVector(v) {
|
|
579
499
|
return new Matrix(v.length, 1, v);
|
|
580
500
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
501
|
+
serialize() {
|
|
502
|
+
const arr = mapfun(x => x.serialize?.() || x, ...this.arr)
|
|
503
|
+
return JSON.stringify({
|
|
504
|
+
type : 'matrix',
|
|
505
|
+
data : {
|
|
506
|
+
rows : this.rows,
|
|
507
|
+
cols : this.cols,
|
|
508
|
+
arr,
|
|
586
509
|
}
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
sortTable(n=0,{type="num",order="asc"}={}) {
|
|
600
|
-
var obj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
|
|
601
|
-
var newObj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
|
|
602
|
-
if(type==="num"){
|
|
603
|
-
if(order==="asc")obj[n].sort((a,b)=>a.x-b.x);
|
|
604
|
-
else if(order==="desc")obj[n].sort((a,b)=>b.x-a.x);
|
|
605
|
-
else if(order==="toggle"){
|
|
606
|
-
// console.log(obj[n][0])
|
|
607
|
-
//console.log(obj[n][1])
|
|
608
|
-
if(obj[n][0].x>obj[n][1].x)obj[n].sort((a,b)=>b.x-a.x);
|
|
609
|
-
else obj[n].sort((a,b)=>a.x-b.x);
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
static deserialize(json) {
|
|
513
|
+
if (typeof json == "string") json = JSON.parse(json);
|
|
514
|
+
const {type, data} = json;
|
|
515
|
+
if(type !== 'matrix') return TypeError('Not a valid Matrix')
|
|
516
|
+
let {arr} = data;
|
|
517
|
+
arr = mapfun(x => {
|
|
518
|
+
if(typeof x === 'string') {
|
|
519
|
+
const x_obj = JSON.parse(x);
|
|
520
|
+
const {type} = x_obj
|
|
521
|
+
if(type === 'complex') return Complex.deserialize(x_obj)
|
|
610
522
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
else if(order==="desc")obj[n].sort((a,b)=>(""+b.x).localeCompare(""+a.x));
|
|
615
|
-
}
|
|
616
|
-
//var order=obj[n].map(n=>n.y);
|
|
617
|
-
order=obj[n].map(n=>n.y);
|
|
618
|
-
for(let i=0;i<obj.length;i++){
|
|
619
|
-
if(i!==n)obj[i].map((n,j)=>n.y=order[j]);
|
|
620
|
-
}
|
|
621
|
-
for(let i=0;i<obj.length;i++){
|
|
622
|
-
if(i!==n)newObj[i].map((n,j)=>n.x=obj[i][order[j]].x)
|
|
623
|
-
}
|
|
624
|
-
newObj[n]=obj[n];
|
|
625
|
-
var newArr=newObj.map(n=>n.map(m=>m.x));
|
|
626
|
-
return new Matrix(newArr).T;
|
|
523
|
+
return x
|
|
524
|
+
}, ...arr)
|
|
525
|
+
return new Matrix(arr)
|
|
627
526
|
}
|
|
527
|
+
// To Be Moved to Table or GridView
|
|
528
|
+
// sortTable(n=0,{type="num",order="asc"}={}) {
|
|
529
|
+
// var obj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
|
|
530
|
+
// var newObj=this.T.arr.map(n=>n.map((n,i)=>Object.assign({},{x:n,y:i})));
|
|
531
|
+
// if(type==="num"){
|
|
532
|
+
// if(order==="asc")obj[n].sort((a,b)=>a.x-b.x);
|
|
533
|
+
// else if(order==="desc")obj[n].sort((a,b)=>b.x-a.x);
|
|
534
|
+
// else if(order==="toggle"){
|
|
535
|
+
// // console.log(obj[n][0])
|
|
536
|
+
// //console.log(obj[n][1])
|
|
537
|
+
// if(obj[n][0].x>obj[n][1].x)obj[n].sort((a,b)=>b.x-a.x);
|
|
538
|
+
// else obj[n].sort((a,b)=>a.x-b.x);
|
|
539
|
+
// }
|
|
540
|
+
// }
|
|
541
|
+
// else if(type==="alpha"){
|
|
542
|
+
// if(order==="asc")obj[n].sort((a,b)=>(""+a.x).localeCompare(""+b.x));
|
|
543
|
+
// else if(order==="desc")obj[n].sort((a,b)=>(""+b.x).localeCompare(""+a.x));
|
|
544
|
+
// }
|
|
545
|
+
// //var order=obj[n].map(n=>n.y);
|
|
546
|
+
// order=obj[n].map(n=>n.y);
|
|
547
|
+
// for(let i=0;i<obj.length;i++){
|
|
548
|
+
// if(i!==n)obj[i].map((n,j)=>n.y=order[j]);
|
|
549
|
+
// }
|
|
550
|
+
// for(let i=0;i<obj.length;i++){
|
|
551
|
+
// if(i!==n)newObj[i].map((n,j)=>n.x=obj[i][order[j]].x)
|
|
552
|
+
// }
|
|
553
|
+
// newObj[n]=obj[n];
|
|
554
|
+
// var newArr=newObj.map(n=>n.map(m=>m.x));
|
|
555
|
+
// return new Matrix(newArr).T;
|
|
556
|
+
// }
|
|
628
557
|
}
|
|
629
558
|
|
|
630
559
|
|