ziko 0.32.0 → 0.34.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 +2440 -3447
- package/dist/ziko.js +8192 -9199
- package/dist/ziko.min.js +2 -2
- package/dist/ziko.mjs +2423 -3424
- package/package.json +4 -6
- package/src/__helpers__/composition/compose-class.js +28 -0
- package/src/__helpers__/composition/compose-instance.js +15 -0
- package/src/__helpers__/composition/compose.js +11 -0
- package/src/__helpers__/composition/index.js +3 -0
- package/src/__helpers__/index.js +4 -2
- package/src/app/index.js +0 -3
- package/src/events/__Events__.js +18 -8
- package/src/events/pointer.js +1 -1
- package/src/index.js +9 -3
- package/src/math/functions/index.js +6 -9
- package/src/math/index.js +0 -7
- package/src/math/matrix/Matrix.js +1 -50
- package/src/math/matrix/index.js +1 -3
- package/src/math/signal/index.js +1 -110
- package/src/math/statistics/index.js +2 -2
- package/src/reactivity/events/index.js +8 -8
- package/src/reactivity/hooks/Head/useFavIcon.js +2 -2
- package/src/reactivity/hooks/Head/useTitle.js +1 -1
- package/src/reactivity/hooks/index.js +3 -3
- package/src/ui/elements/ZikoUIElement.js +45 -285
- package/src/ui/elements/semantic/index.js +33 -73
- package/src/ui/methods/dom.js +99 -0
- package/src/ui/methods/events.js +39 -0
- package/src/ui/methods/indexing.js +15 -0
- package/src/use/decorators/index.js +2 -0
- package/src/use/decorators/use-debounce.js +4 -0
- package/src/use/decorators/use-throttle.js +9 -0
- package/src/use/index.js +3 -0
- package/src/use/interactions/index.js +3 -0
- package/src/{reactivity/hooks/Interactions/useChannel.js → use/interactions/use-channel.js} +8 -8
- package/src/{reactivity/hooks/Storage/useStorage.js → use/storage/index.js} +2 -2
- package/src/app/json-style-sheet.js +0 -64
- package/src/math/calculus/derivation/index.js +0 -7
- package/src/math/calculus/index.js +0 -2
- package/src/math/calculus/special-functions/bessel.js +0 -31
- package/src/math/calculus/special-functions/beta.js +0 -38
- package/src/math/calculus/special-functions/gamma.js +0 -30
- package/src/math/calculus/special-functions/index.js +0 -4
- package/src/math/matrix/Decomposition.js +0 -90
- package/src/math/matrix/LinearSystem.js +0 -10
- package/src/math/signal/__np.py.txt +0 -40
- package/src/math/signal/conv.js +0 -175
- package/src/math/signal/fft.js +0 -55
- package/src/math/signal/filter.js +0 -39
- package/src/reactivity/events/Pointer.js +0 -232
- package/src/reactivity/events/click.js +0 -102
- package/src/reactivity/events/clipboard.js +0 -88
- package/src/reactivity/events/drag.js +0 -137
- package/src/reactivity/events/focus.js +0 -59
- package/src/reactivity/events/key.js +0 -102
- package/src/reactivity/events/wheel.js +0 -47
- package/src/reactivity/hooks/Decorators/index.js +0 -6
- package/src/reactivity/hooks/Decorators/time.js +0 -17
- package/src/reactivity/hooks/Decorators/type.js +0 -88
- package/src/reactivity/hooks/Interactions/_useBluetooth.js +0 -48
- package/src/reactivity/hooks/Interactions/index.js +0 -4
- package/src/reactivity/hooks/Sensors ( To Be Moved )/index.js +0 -2
- package/src/reactivity/hooks/Sensors ( To Be Moved )/useBattery.js +0 -36
- package/src/reactivity/hooks/Sensors ( To Be Moved )/useGeolocation.js +0 -17
- package/src/reactivity/hooks/Storage/index.js +0 -1
- /package/src/events/{index.d.ts → index.d.ts.txt} +0 -0
- /package/src/math/statistics/{Functions → functions}/index.js +0 -0
- /package/src/{math/numeric/index.js → ui/methods/observer.js} +0 -0
- /package/src/{reactivity/hooks/Interactions/useUsb.js → ui/methods/style.js} +0 -0
- /package/src/{reactivity/hooks/Sensors ( To Be Moved )/useCamera.js → use/app/index.js} +0 -0
- /package/src/{reactivity/hooks/Sensors ( To Be Moved )/useMicro.js → use/app/use-favicon.js} +0 -0
- /package/src/{reactivity/hooks/Sensors ( To Be Moved )/useOrientation.js → use/app/use-link.js} +0 -0
- /package/src/{reactivity/hooks/Storage/useCookie.js → use/app/use-meta.js} +0 -0
- /package/src/{reactivity/hooks/Storage/useIndexedDb.js → use/app/use-title.js} +0 -0
- /package/src/{reactivity/hooks/Interactions/useEventEmmiter.js → use/interactions/use-event-emmiter.js} +0 -0
- /package/src/{reactivity/hooks/Interactions/useThread.js → use/interactions/use-thread.js} +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Random } from "
|
|
1
|
+
import { Random } from "../../math/random/index.js";
|
|
2
2
|
class ZikoUseChannel{
|
|
3
|
-
constructor(name=""){
|
|
4
|
-
this.channel=new BroadcastChannel(name);
|
|
5
|
-
this.EVENTS_DATAS_PAIRS=new Map();
|
|
6
|
-
this.EVENTS_HANDLERS_PAIRS=new Map();
|
|
7
|
-
this.LAST_RECEIVED_EVENT="";
|
|
3
|
+
constructor(name = ""){
|
|
4
|
+
this.channel = new BroadcastChannel(name);
|
|
5
|
+
this.EVENTS_DATAS_PAIRS = new Map();
|
|
6
|
+
this.EVENTS_HANDLERS_PAIRS = new Map();
|
|
7
|
+
this.LAST_RECEIVED_EVENT = "";
|
|
8
8
|
this.UUID="ziko-channel"+Random.string(10);
|
|
9
|
-
this.SUBSCRIBERS=new Set([this.UUID]);
|
|
9
|
+
this.SUBSCRIBERS = new Set([this.UUID]);
|
|
10
10
|
}
|
|
11
11
|
get broadcast(){
|
|
12
12
|
// update receiver
|
|
@@ -46,5 +46,5 @@ class ZikoUseChannel{
|
|
|
46
46
|
return this;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
const useChannel=name=>new ZikoUseChannel(name);
|
|
49
|
+
const useChannel = name => new ZikoUseChannel(name);
|
|
50
50
|
export{ useChannel }
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// To do : remove old items
|
|
2
|
-
import { useChannel } from "../
|
|
2
|
+
import { useChannel } from "../interactions/use-channel.js";
|
|
3
3
|
class ZikoUseStorage{
|
|
4
|
-
constructor(storage,globalKey,initialValue){
|
|
4
|
+
constructor(storage, globalKey, initialValue){
|
|
5
5
|
this.cache={
|
|
6
6
|
storage,
|
|
7
7
|
globalKey,
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { json2css } from "../data/index.js";
|
|
2
|
-
class ZikoJsonStyleSheet{
|
|
3
|
-
constructor(JsonStyle={}){
|
|
4
|
-
this.cache={
|
|
5
|
-
JsonStyle,
|
|
6
|
-
isRenderd : false
|
|
7
|
-
}
|
|
8
|
-
this.target = globalThis?.document?.head;
|
|
9
|
-
this.element = document.createElement("style");
|
|
10
|
-
this.element.setAttribute("data-generator","zikojs");
|
|
11
|
-
this.write();
|
|
12
|
-
this.render()
|
|
13
|
-
}
|
|
14
|
-
get CssText(){
|
|
15
|
-
return json2css(this.cache.JsonStyle);
|
|
16
|
-
}
|
|
17
|
-
render(){
|
|
18
|
-
this.target.append(this.element);
|
|
19
|
-
this.cache.isRenderd = true;
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
unrender(){
|
|
23
|
-
if(this.target?.children?.length && [...this.target?.children].includes(this.element)) this.target.removeChild(this.element);
|
|
24
|
-
this.cache.isRenderd = false;
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
write(){
|
|
28
|
-
this.element.textContent = this.CssText;
|
|
29
|
-
return this;
|
|
30
|
-
}
|
|
31
|
-
style(styles, overwrite = false){
|
|
32
|
-
if(overwrite) this.clear();
|
|
33
|
-
Object.assign(this.cache.JsonStyle,styles);
|
|
34
|
-
this.write();
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
clear(){
|
|
38
|
-
this.cache.JsonStyle = {}
|
|
39
|
-
this.write();
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
|
-
// overrideGlobal(style){
|
|
43
|
-
// if(this.cache.JsonStyle["*"]){
|
|
44
|
-
// Object.assign(this.cache.JsonStyle["*"],style);
|
|
45
|
-
// }
|
|
46
|
-
// else Object.assign();
|
|
47
|
-
// this.write();
|
|
48
|
-
// return this;
|
|
49
|
-
// }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const jsonStyleSheet = JsonStyle =>new ZikoJsonStyleSheet(JsonStyle);
|
|
53
|
-
export {
|
|
54
|
-
jsonStyleSheet,
|
|
55
|
-
ZikoJsonStyleSheet
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// p().style({color:"red"})
|
|
60
|
-
// jsonStyleSheet({
|
|
61
|
-
// p:{
|
|
62
|
-
// color : "green !important"
|
|
63
|
-
// }
|
|
64
|
-
// })
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { mapfun } from "../../utils/index.js";
|
|
2
|
-
import { fact } from "../../functions/index.js";
|
|
3
|
-
const _bessel=(n, x)=>{
|
|
4
|
-
const maxTerms = 100;
|
|
5
|
-
let result = 0;
|
|
6
|
-
for (let k = 0; k < maxTerms; k++) {
|
|
7
|
-
const numerator = Math.pow(-1, k) * Math.pow(x / 2, n + 2 * k);
|
|
8
|
-
const denominator = fact(k) * fact(n + k);
|
|
9
|
-
result += numerator / denominator;
|
|
10
|
-
}
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
13
|
-
const bessel=(n,x)=>{
|
|
14
|
-
if(typeof n === "number"){
|
|
15
|
-
if(typeof n === "number")return _bessel(n,x);
|
|
16
|
-
else console.warn("Not supported yet")
|
|
17
|
-
}
|
|
18
|
-
else if(n instanceof Array){
|
|
19
|
-
if(typeof x === "number") return mapfun(a=>_bessel(a,x),...n);
|
|
20
|
-
else if(x instanceof Array){
|
|
21
|
-
const Y=[];
|
|
22
|
-
for(let i=0;i<n.length;i++){
|
|
23
|
-
Y.push(mapfun(a=>_bessel(n[i],a),...x))
|
|
24
|
-
}
|
|
25
|
-
return Y;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
export{
|
|
30
|
-
bessel
|
|
31
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { mapfun } from "../../utils/index.js";
|
|
2
|
-
const _beta=(x, y)=>{
|
|
3
|
-
const lowerLimit = 0;
|
|
4
|
-
const upperLimit = 1;
|
|
5
|
-
const intervals = 1000;
|
|
6
|
-
let result = 0;
|
|
7
|
-
|
|
8
|
-
const f = (t) => Math.pow(t, x - 1) * Math.pow(1 - t, y - 1);
|
|
9
|
-
const h = (upperLimit - lowerLimit) / intervals;
|
|
10
|
-
|
|
11
|
-
result += 0.5 * (f(lowerLimit) + f(upperLimit));
|
|
12
|
-
|
|
13
|
-
for (let i = 1; i < intervals; i++) {
|
|
14
|
-
const xi = lowerLimit + i * h;
|
|
15
|
-
result += f(xi);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return result * h;
|
|
19
|
-
}
|
|
20
|
-
const beta=(x,y)=>{
|
|
21
|
-
if(typeof x === "number"){
|
|
22
|
-
if(typeof x === "number")return _beta(x,y);
|
|
23
|
-
else console.warn("Not supported yet")
|
|
24
|
-
}
|
|
25
|
-
else if(x instanceof Array){
|
|
26
|
-
if(typeof y === "number") return mapfun(a=>_beta(a,y),...x);
|
|
27
|
-
else if(y instanceof Array){
|
|
28
|
-
const Z=[];
|
|
29
|
-
for(let i=0;i<x.length;i++){
|
|
30
|
-
Z.push(mapfun(a=>_beta(x[i],a),...y))
|
|
31
|
-
}
|
|
32
|
-
return Z;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
export{
|
|
37
|
-
beta
|
|
38
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { mapfun } from "../../utils/mapfun.js";
|
|
2
|
-
const _gamma=x=>{
|
|
3
|
-
// Coefficients for the Lanczos approximation
|
|
4
|
-
const g = 7;
|
|
5
|
-
const p = [
|
|
6
|
-
0.99999999999980993,
|
|
7
|
-
676.5203681218851,
|
|
8
|
-
-1259.1392167224028,
|
|
9
|
-
771.32342877765313,
|
|
10
|
-
-176.61502916214059,
|
|
11
|
-
12.507343278686905,
|
|
12
|
-
-0.13857109526572012,
|
|
13
|
-
9.9843695780195716e-6,
|
|
14
|
-
1.5056327351493116e-7
|
|
15
|
-
];
|
|
16
|
-
if (x < 0.5) {
|
|
17
|
-
return +(Math.PI / (Math.sin(Math.PI * x) * _gamma(1 - x))).toFixed(10);
|
|
18
|
-
}
|
|
19
|
-
x -= 1;
|
|
20
|
-
let a = p[0];
|
|
21
|
-
for (let i = 1; i < g + 2; i++) {
|
|
22
|
-
a += p[i] / (x + i);
|
|
23
|
-
}
|
|
24
|
-
const t = x + g + 0.5;
|
|
25
|
-
return +(Math.sqrt(2 * Math.PI) * Math.pow(t, (x + 0.5)) * Math.exp(-t) * a).toFixed(10);
|
|
26
|
-
}
|
|
27
|
-
const gamma=(...x)=>mapfun(_gamma,...x);
|
|
28
|
-
export{
|
|
29
|
-
gamma
|
|
30
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { Matrix } from "./Matrix.js";
|
|
2
|
-
const luDecomposition=matrix=>{
|
|
3
|
-
if(matrix instanceof Matrix)matrix=matrix.arr;
|
|
4
|
-
const n = matrix.length;
|
|
5
|
-
const L = new Array(n).fill(0).map(() => new Array(n).fill(0));
|
|
6
|
-
const U = new Array(n).fill(0).map(() => new Array(n).fill(0));
|
|
7
|
-
for (let i = 0; i < n; i++) {
|
|
8
|
-
// Upper Triangular
|
|
9
|
-
for (let k = i; k < n; k++) {
|
|
10
|
-
// Summation of L(i, j) * U(j, k)
|
|
11
|
-
let sum = 0;
|
|
12
|
-
for (let j = 0; j < i; j++) {
|
|
13
|
-
sum += (L[i][j] * U[j][k]);
|
|
14
|
-
}
|
|
15
|
-
U[i][k] = matrix[i][k] - sum;
|
|
16
|
-
}
|
|
17
|
-
// Lower Triangular
|
|
18
|
-
for (let k = i; k < n; k++) {
|
|
19
|
-
if (i == k) {
|
|
20
|
-
L[i][i] = 1; // Diagonal as 1
|
|
21
|
-
} else {
|
|
22
|
-
// Summation of L(k, j) * U(j, i)
|
|
23
|
-
let sum = 0;
|
|
24
|
-
for (let j = 0; j < i; j++) {
|
|
25
|
-
sum += (L[k][j] * U[j][i]);
|
|
26
|
-
}
|
|
27
|
-
// Evaluate L(k, i)
|
|
28
|
-
L[k][i] = (matrix[k][i] - sum) / U[i][i];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return [L, U].map(n=>new Matrix(n));
|
|
33
|
-
}
|
|
34
|
-
const dotProduct=(v1, v2)=>v1.reduce((sum, el, i) => sum + el * v2[i], 0);
|
|
35
|
-
const magnitude=vector=>Math.sqrt(vector.reduce((sum, el) => sum + el * el, 0));
|
|
36
|
-
const normalize=vector=>vector.map(el => el / magnitude(vector));
|
|
37
|
-
const qrDecomposition=matrix=>{
|
|
38
|
-
if(matrix instanceof Matrix)matrix=matrix.arr;
|
|
39
|
-
const m = matrix.length;
|
|
40
|
-
const n = matrix[0].length;
|
|
41
|
-
const Q = [];
|
|
42
|
-
const R = [];
|
|
43
|
-
// Initialize R as an m x n matrix of zeroes
|
|
44
|
-
for (let i = 0; i < m; i++) {
|
|
45
|
-
R.push(new Array(n).fill(0));
|
|
46
|
-
}
|
|
47
|
-
for (let j = 0; j < n; j++) {
|
|
48
|
-
let v = matrix.map(row => row[j]);
|
|
49
|
-
for (let i = 0; i < j; i++) {
|
|
50
|
-
const q = Q[i];
|
|
51
|
-
const r_ij = dotProduct(q, matrix.map(row => row[j]));
|
|
52
|
-
for (let k = 0; k < m; k++) {
|
|
53
|
-
v[k] -= r_ij * q[k];
|
|
54
|
-
}
|
|
55
|
-
R[i][j] = r_ij;
|
|
56
|
-
}
|
|
57
|
-
const v_mag = magnitude(v);
|
|
58
|
-
Q.push(normalize(v));
|
|
59
|
-
R[j][j] = v_mag;
|
|
60
|
-
}
|
|
61
|
-
return [Q, R].map(n=>new Matrix(n));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const choleskyDecomposition=matrix=>{
|
|
65
|
-
if(matrix instanceof Matrix)matrix=matrix.arr;
|
|
66
|
-
const n = matrix.length;
|
|
67
|
-
const L = new Array(n).fill(0).map(() => new Array(n).fill(0));
|
|
68
|
-
for (let i = 0; i < n; i++) {
|
|
69
|
-
for (let j = 0; j <= i; j++) {
|
|
70
|
-
let sum = 0;
|
|
71
|
-
for (let k = 0; k < j; k++) {
|
|
72
|
-
sum += L[i][k] * L[j][k];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (i === j) {
|
|
76
|
-
L[i][j] = Math.sqrt(matrix[i][i] - sum);
|
|
77
|
-
} else {
|
|
78
|
-
L[i][j] = (1.0 / L[j][j] * (matrix[i][j] - sum));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return new Matrix(L);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
export {
|
|
87
|
-
luDecomposition,
|
|
88
|
-
qrDecomposition,
|
|
89
|
-
choleskyDecomposition
|
|
90
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
lin_R=np.linspace(1,10,10,)
|
|
3
|
-
lin_C=np.linspace(10j+1,10+1j,10)
|
|
4
|
-
print("Linspace")
|
|
5
|
-
print(lin_R)
|
|
6
|
-
print("-"*70)
|
|
7
|
-
print(lin_C)
|
|
8
|
-
log_R=np.logspace(1,10,10, base = 2)
|
|
9
|
-
log_C=np.logspace(10j+1,10+1j,10 , base = 2)
|
|
10
|
-
print("-"*70)
|
|
11
|
-
print("Logspace")
|
|
12
|
-
print(log_R)
|
|
13
|
-
print("-"*70)
|
|
14
|
-
print(log_C)
|
|
15
|
-
geo_R=np.geomspace(1,512,10)
|
|
16
|
-
geo_C=np.geomspace(1+1j,512+512j,10)
|
|
17
|
-
print("-"*70)
|
|
18
|
-
print("geomspace")
|
|
19
|
-
print(geo_R)
|
|
20
|
-
print("-"*70)
|
|
21
|
-
print(geo_C)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
# // const logspace=(a,b,n=b-a+1,base=E,endpoint=true)=>{
|
|
25
|
-
# // if(a instanceof Complex||b instanceof Complex){
|
|
26
|
-
# // a=complex(a);
|
|
27
|
-
# // b=complex(b);
|
|
28
|
-
# // n=n??abs(b.a-a.a)
|
|
29
|
-
# // const X=linspace(a.a,b.a,n,base,endpoint);
|
|
30
|
-
# // const Y=linspace(a.b,b.b,n,base,endpoint);
|
|
31
|
-
# // const Z=new Array(X.length).fill(0)
|
|
32
|
-
# // const ZZ=Z.map((n,i) => pow(base,complex(X[i],Y[i])));
|
|
33
|
-
# // return ZZ;
|
|
34
|
-
# // }
|
|
35
|
-
# // const start=base**min(a,b);
|
|
36
|
-
# // const stop=base**max(a,b);
|
|
37
|
-
# // const y = linspace(ln(start) / ln(base), ln(stop) / ln(base), n, endpoint);
|
|
38
|
-
# // const result=y.map(n => pow(base, n));
|
|
39
|
-
# // return a<b?result:result.reverse();
|
|
40
|
-
# // }
|
package/src/math/signal/conv.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { Matrix , matrix } from "../matrix/index.js";
|
|
2
|
-
import { sqrt } from "../functions/index.js";
|
|
3
|
-
const conv1d=(input, kernel , circular = true)=>{
|
|
4
|
-
const INPUT_LENGTH = input.length;
|
|
5
|
-
const KERNEL_LENGTH = kernel.length;
|
|
6
|
-
const output = [];
|
|
7
|
-
const LENGTH_OUT = circular ? Math.max(INPUT_LENGTH,KERNEL_LENGTH) : INPUT_LENGTH + KERNEL_LENGTH - 1;
|
|
8
|
-
for (let i = 0; i < LENGTH_OUT; i++) {
|
|
9
|
-
let sum = 0;
|
|
10
|
-
for (let j = 0; j < KERNEL_LENGTH; j++) {
|
|
11
|
-
const inputIndex = i + j - Math.floor(KERNEL_LENGTH / 2);
|
|
12
|
-
// Apply zero-padding for out-of-bounds indices
|
|
13
|
-
const inputValue = inputIndex >= 0 && inputIndex < INPUT_LENGTH
|
|
14
|
-
? input[inputIndex]
|
|
15
|
-
: 0;
|
|
16
|
-
sum += inputValue * kernel[j];
|
|
17
|
-
}
|
|
18
|
-
output.push(sum);
|
|
19
|
-
}
|
|
20
|
-
return output;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const conv2d = (input, kernel, circular = true) => {
|
|
24
|
-
if(!(input instanceof Matrix)) input = matrix(input);
|
|
25
|
-
if(!(kernel instanceof Matrix)) kernel = matrix(kernel);
|
|
26
|
-
const INPUT_ROWS=input.rows;
|
|
27
|
-
const INPUT_COLS=input.cols;
|
|
28
|
-
const OUTPUT_ROWS = circular ? Math.max(input.rows,kernel.rows) : input.rows + kernel.rows-1;
|
|
29
|
-
const OUTPUT_COLS = circular ? Math.max(input.cols,kernel.cols) : input.cols + kernel.cols-1;
|
|
30
|
-
const KERNEL_SIZE = kernel.rows;
|
|
31
|
-
const output = [];
|
|
32
|
-
for (let i = 0; i < OUTPUT_ROWS ; i++) {
|
|
33
|
-
const row = [];
|
|
34
|
-
for (let j = 0; j < OUTPUT_COLS ; j++) {
|
|
35
|
-
let sum = 0;
|
|
36
|
-
for (let k = 0; k < KERNEL_SIZE; k++) {
|
|
37
|
-
for (let l = 0; l < KERNEL_SIZE; l++) {
|
|
38
|
-
const rowIndex = i + k - Math.floor(KERNEL_SIZE / 2);
|
|
39
|
-
const colIndex = j + l - Math.floor(KERNEL_SIZE / 2);
|
|
40
|
-
// Apply zero-padding for out-of-bounds indices
|
|
41
|
-
const inputValue = (rowIndex >= 0 && rowIndex < INPUT_ROWS &&
|
|
42
|
-
colIndex >= 0 && colIndex < INPUT_COLS)
|
|
43
|
-
? input[rowIndex][colIndex]
|
|
44
|
-
: 0;
|
|
45
|
-
sum += inputValue * kernel[k][l];
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
row.push(sum);
|
|
49
|
-
}
|
|
50
|
-
output.push(row);
|
|
51
|
-
}
|
|
52
|
-
return output;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
var convolute=(parent,kernel = [0, -1, 0, -1, 5, -1, 0, -1, 0], x1 = 0, y1 = 0, x2 = parent.element.width, y2 = parent.element.height)=>{
|
|
56
|
-
if(kernel instanceof Matrix)kernel=kernel.arr.flat(1)
|
|
57
|
-
var opaque = 1;
|
|
58
|
-
var pixels = parent.ctx.getImageData(x1, y1, x2, y2);
|
|
59
|
-
var side = Math.round(sqrt(kernel.length));
|
|
60
|
-
var halfSide = Math.floor(side / 2);
|
|
61
|
-
var src = pixels.data;
|
|
62
|
-
var sw = pixels.width;
|
|
63
|
-
var sh = pixels.height;
|
|
64
|
-
// pad output by the convolution matrix
|
|
65
|
-
var w = sw;
|
|
66
|
-
var h = sh;
|
|
67
|
-
var output = parent.ctx.createImageData(w, h);
|
|
68
|
-
var dst = output.data;
|
|
69
|
-
// go through the destination image pixels
|
|
70
|
-
var alphaFac = opaque ? 1 : 0;
|
|
71
|
-
for (var y = 0; y < h; y++) {
|
|
72
|
-
for (var x = 0; x < w; x++) {
|
|
73
|
-
var sy = y;
|
|
74
|
-
var sx = x;
|
|
75
|
-
var dstOff = (y * w + x) * 4;
|
|
76
|
-
// calculate the weighed sum of the source image pixels that
|
|
77
|
-
// fall under the convolution matrix
|
|
78
|
-
var r = 0,
|
|
79
|
-
g = 0,
|
|
80
|
-
b = 0,
|
|
81
|
-
a = 0;
|
|
82
|
-
for (var cy = 0; cy < side; cy++) {
|
|
83
|
-
for (var cx = 0; cx < side; cx++) {
|
|
84
|
-
var scy = sy + cy - halfSide;
|
|
85
|
-
var scx = sx + cx - halfSide;
|
|
86
|
-
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
|
|
87
|
-
var srcOff = (scy * sw + scx) * 4;
|
|
88
|
-
var wt = kernel[cy * side + cx];
|
|
89
|
-
r += src[srcOff] * wt;
|
|
90
|
-
g += src[srcOff + 1] * wt;
|
|
91
|
-
b += src[srcOff + 2] * wt;
|
|
92
|
-
a += src[srcOff + 3] * wt;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
dst[dstOff] = r;
|
|
97
|
-
dst[dstOff + 1] = g;
|
|
98
|
-
dst[dstOff + 2] = b;
|
|
99
|
-
dst[dstOff + 3] = a + alphaFac * (255 - a);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return output;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
convolute=(parent,kernel = [0, -1, 0, -1, 5, -1, 0, -1, 0], x1 = 0, y1 = 0, x2 = parent.element.width, y2 = parent.element.height)=>{
|
|
106
|
-
if(kernel instanceof Matrix)kernel=kernel.arr.flat(1)
|
|
107
|
-
var opaque = 1;
|
|
108
|
-
var pixels = parent.ctx.getImageData(x1, y1, x2, y2);
|
|
109
|
-
var side = Math.round(sqrt(kernel.length));
|
|
110
|
-
var halfSide = Math.floor(side / 2);
|
|
111
|
-
var src = pixels.data;
|
|
112
|
-
var sw = pixels.width;
|
|
113
|
-
var sh = pixels.height;
|
|
114
|
-
// pad output by the convolution matrix
|
|
115
|
-
var w = sw;
|
|
116
|
-
var h = sh;
|
|
117
|
-
var output = parent.ctx.createImageData(w, h);
|
|
118
|
-
var dst = output.data;
|
|
119
|
-
// go through the destination image pixels
|
|
120
|
-
var alphaFac = opaque ? 1 : 0;
|
|
121
|
-
for (var y = 0; y < h; y++) {
|
|
122
|
-
for (var x = 0; x < w; x++) {
|
|
123
|
-
var sy = y;
|
|
124
|
-
var sx = x;
|
|
125
|
-
var dstOff = (y * w + x) * 4;
|
|
126
|
-
// calculate the weighed sum of the source image pixels that
|
|
127
|
-
// fall under the convolution matrix
|
|
128
|
-
var r = 0,
|
|
129
|
-
g = 0,
|
|
130
|
-
b = 0,
|
|
131
|
-
a = 0;
|
|
132
|
-
for (var cy = 0; cy < side; cy++) {
|
|
133
|
-
for (var cx = 0; cx < side; cx++) {
|
|
134
|
-
var scy = sy + cy - halfSide;
|
|
135
|
-
var scx = sx + cx - halfSide;
|
|
136
|
-
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
|
|
137
|
-
var srcOff = (scy * sw + scx) * 4;
|
|
138
|
-
var wt = kernel[cy * side + cx];
|
|
139
|
-
r += src[srcOff] * wt;
|
|
140
|
-
g += src[srcOff + 1] * wt;
|
|
141
|
-
b += src[srcOff + 2] * wt;
|
|
142
|
-
a += src[srcOff + 3] * wt;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
dst[dstOff] = r;
|
|
147
|
-
dst[dstOff + 1] = g;
|
|
148
|
-
dst[dstOff + 2] = b;
|
|
149
|
-
dst[dstOff + 3] = a + alphaFac * (255 - a);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return output;
|
|
153
|
-
}
|
|
154
|
-
const conv=(input,kernel,circular)=>{
|
|
155
|
-
if(input instanceof Matrix || (input instanceof Array && input[0][0]))return conv2d(input,kernel,circular);
|
|
156
|
-
return conv1d(input,kernel,circular)
|
|
157
|
-
}
|
|
158
|
-
const circularConv=(input,kernel)=>conv(input,kernel,true);
|
|
159
|
-
const linearConv=(input,kernel)=>conv(input,kernel,false);
|
|
160
|
-
const circularConv1d=(input,kernel)=>conv1d(input,kernel,true);
|
|
161
|
-
const circularConv2d=(input,kernel)=>conv2d(input,kernel,true);
|
|
162
|
-
const linearConv1d=(input,kernel)=>conv1d(input,kernel,false);
|
|
163
|
-
const linearConv2d=(input,kernel)=>conv2d(input,kernel,false);
|
|
164
|
-
export{
|
|
165
|
-
conv1d,
|
|
166
|
-
conv2d,
|
|
167
|
-
conv,
|
|
168
|
-
circularConv,
|
|
169
|
-
linearConv,
|
|
170
|
-
circularConv1d,
|
|
171
|
-
linearConv1d,
|
|
172
|
-
circularConv2d,
|
|
173
|
-
linearConv2d,
|
|
174
|
-
convolute
|
|
175
|
-
}
|
package/src/math/signal/fft.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Complex , complex } from "../complex";
|
|
2
|
-
import { PI } from "../const";
|
|
3
|
-
import { cos , sin } from "../functions";
|
|
4
|
-
const fft=x=>{
|
|
5
|
-
const output = [];
|
|
6
|
-
const N = x.length;
|
|
7
|
-
if(!(x[0]instanceof Complex))x=x.map((n)=>complex(n,0));
|
|
8
|
-
for (let k = 0; k < N; k++) {
|
|
9
|
-
let re = 0,im=0;
|
|
10
|
-
for (let n = 0; n < N; n++) {
|
|
11
|
-
const phi = (2*PI * k * n) / N;
|
|
12
|
-
re += x[n].a*cos(phi)+x[n].b*sin(phi);
|
|
13
|
-
im += -x[n].a*sin(phi)+x[n].b*cos(phi);
|
|
14
|
-
}
|
|
15
|
-
re = re / N;
|
|
16
|
-
im = im / N;
|
|
17
|
-
output[k] = complex(re,im);
|
|
18
|
-
}
|
|
19
|
-
return {
|
|
20
|
-
output,
|
|
21
|
-
re:output.map(n=>n.a),
|
|
22
|
-
im:output.map(n=>n.b),
|
|
23
|
-
z:output.map(n=>n.z),
|
|
24
|
-
phi:output.map(n=>n.phi)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
const ifft=x=>{
|
|
28
|
-
const output = [];
|
|
29
|
-
const N = x.length;
|
|
30
|
-
if(!(x[0]instanceof Complex))x=x.map((n)=>complex(n,0));
|
|
31
|
-
for (let k = 0; k < N; k++) {
|
|
32
|
-
let re=0,im=0;
|
|
33
|
-
for (let n = 0; n < N; n++) {
|
|
34
|
-
const phi = (2*PI * k * n) / N;
|
|
35
|
-
re += x[n].a*cos(phi)+x[n].b*sin(phi);
|
|
36
|
-
im += x[n].a*sin(phi)+x[n].b*cos(phi);
|
|
37
|
-
}
|
|
38
|
-
re = re / N;
|
|
39
|
-
im = im / N;
|
|
40
|
-
|
|
41
|
-
output[k] = complex(re,im);
|
|
42
|
-
}
|
|
43
|
-
return {
|
|
44
|
-
output,
|
|
45
|
-
re:output.map(n=>n.a),
|
|
46
|
-
im:output.map(n=>n.b),
|
|
47
|
-
z:output.map(n=>n.z),
|
|
48
|
-
phi:output.map(n=>n.phi)
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export{
|
|
53
|
-
fft,
|
|
54
|
-
ifft
|
|
55
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { complex } from "../complex/index.js";
|
|
2
|
-
import {fft,ifft} from "./fft.js";
|
|
3
|
-
// should be processed in other thread
|
|
4
|
-
class Filter{
|
|
5
|
-
constructor(input){
|
|
6
|
-
this.input=input;
|
|
7
|
-
this.input_fft=fft(this.input);
|
|
8
|
-
this.output_fft=[]
|
|
9
|
-
}
|
|
10
|
-
// get length(){
|
|
11
|
-
// return this.input.length;
|
|
12
|
-
// }
|
|
13
|
-
lowPass(fc){
|
|
14
|
-
this.input_fft.output.forEach((n,i)=>{
|
|
15
|
-
n=n.z<fc
|
|
16
|
-
? this.output_fft[i]=this.input_fft.output[i]
|
|
17
|
-
: this.output_fft[i]=complex(0,0)
|
|
18
|
-
})
|
|
19
|
-
return ifft(this.output_fft).re;
|
|
20
|
-
}
|
|
21
|
-
highPass(fc){
|
|
22
|
-
this.input_fft.output.forEach((n,i)=>{
|
|
23
|
-
n=n.z>fc
|
|
24
|
-
? this.output_fft[i]=this.input_fft.output[i]
|
|
25
|
-
: this.output_fft[i]=complex(0,0)
|
|
26
|
-
})
|
|
27
|
-
return ifft(this.output_fft).re;
|
|
28
|
-
}
|
|
29
|
-
bandePass(){
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
bandeCoupe(){
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
const filter=input=>new Filter(input);
|
|
37
|
-
export{
|
|
38
|
-
filter
|
|
39
|
-
}
|