@bian-womp/spark-graph 0.1.24 → 0.1.26
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/lib/cjs/index.cjs +147 -90
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/cjs/src/misc/base.d.ts.map +1 -1
- package/lib/esm/index.js +147 -90
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/src/misc/base.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../src/misc/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../src/misc/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,eAAe,CAAC;AA+IvB,wBAAgB,uBAAuB,IAAI,QAAQ,CAq3BlD;AAED,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,QA+BnD;AAqBD,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,QA6BvD;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAE,eAAe,6BAUrE"}
|
package/lib/esm/index.js
CHANGED
|
@@ -1778,6 +1778,22 @@ const broadcast = (a, b) => {
|
|
|
1778
1778
|
const len = Math.max(aa.length, bb.length);
|
|
1779
1779
|
return [new Array(len).fill(aa[0] ?? 0), new Array(len).fill(bb[0] ?? 0)];
|
|
1780
1780
|
};
|
|
1781
|
+
const asBoolArray = (v) => Array.isArray(v) ? v.map((x) => Boolean(x)) : [Boolean(v)];
|
|
1782
|
+
const broadcastBool = (a, b) => {
|
|
1783
|
+
const aa = asBoolArray(a);
|
|
1784
|
+
const bb = asBoolArray(b);
|
|
1785
|
+
if (aa.length === bb.length)
|
|
1786
|
+
return [aa, bb];
|
|
1787
|
+
if (aa.length === 1)
|
|
1788
|
+
return [new Array(bb.length).fill(aa[0]), bb];
|
|
1789
|
+
if (bb.length === 1)
|
|
1790
|
+
return [aa, new Array(aa.length).fill(bb[0])];
|
|
1791
|
+
const len = Math.max(aa.length, bb.length);
|
|
1792
|
+
return [
|
|
1793
|
+
new Array(len).fill(aa[0] ?? false),
|
|
1794
|
+
new Array(len).fill(bb[0] ?? false),
|
|
1795
|
+
];
|
|
1796
|
+
};
|
|
1781
1797
|
const clamp = (x, min, max) => Math.min(max, Math.max(min, x));
|
|
1782
1798
|
const lerp = (a, b, t) => a + (b - a) * t;
|
|
1783
1799
|
const lcg = (seed) => {
|
|
@@ -1969,40 +1985,70 @@ function setupBasicGraphRegistry() {
|
|
|
1969
1985
|
return undefined;
|
|
1970
1986
|
}
|
|
1971
1987
|
});
|
|
1988
|
+
const BaseMathOperation = {
|
|
1989
|
+
Add: 0,
|
|
1990
|
+
Subtract: 1,
|
|
1991
|
+
Multiply: 2,
|
|
1992
|
+
Divide: 3,
|
|
1993
|
+
Min: 4,
|
|
1994
|
+
Max: 5,
|
|
1995
|
+
Modulo: 6,
|
|
1996
|
+
Power: 7,
|
|
1997
|
+
Round: 8,
|
|
1998
|
+
Floor: 9,
|
|
1999
|
+
Ceil: 10,
|
|
2000
|
+
Abs: 11,
|
|
2001
|
+
Sum: 12,
|
|
2002
|
+
Avg: 13,
|
|
2003
|
+
MinAll: 14,
|
|
2004
|
+
MaxAll: 15,
|
|
2005
|
+
Sin: 16,
|
|
2006
|
+
Cos: 17,
|
|
2007
|
+
Tan: 18,
|
|
2008
|
+
Asin: 19,
|
|
2009
|
+
Acos: 20,
|
|
2010
|
+
Atan: 21,
|
|
2011
|
+
Sqrt: 22,
|
|
2012
|
+
Exp: 23,
|
|
2013
|
+
Log: 24,
|
|
2014
|
+
};
|
|
1972
2015
|
// Enums: Math Operation
|
|
1973
2016
|
registry.registerEnum({
|
|
1974
2017
|
id: "enum:base.math.operation",
|
|
1975
|
-
options: [
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
{ value: 3, label: "Divide" },
|
|
1980
|
-
{ value: 4, label: "Min" },
|
|
1981
|
-
{ value: 5, label: "Max" },
|
|
1982
|
-
{ value: 6, label: "Modulo" },
|
|
1983
|
-
{ value: 7, label: "Power" },
|
|
1984
|
-
// Unary / aggregate operations on A only
|
|
1985
|
-
{ value: 8, label: "Round" },
|
|
1986
|
-
{ value: 9, label: "Floor" },
|
|
1987
|
-
{ value: 10, label: "Ceil" },
|
|
1988
|
-
{ value: 11, label: "Abs" },
|
|
1989
|
-
{ value: 12, label: "Sum" },
|
|
1990
|
-
{ value: 13, label: "Avg" },
|
|
1991
|
-
{ value: 14, label: "MinAll" },
|
|
1992
|
-
{ value: 15, label: "MaxAll" },
|
|
1993
|
-
],
|
|
2018
|
+
options: Object.entries(BaseMathOperation).map(([label, value]) => ({
|
|
2019
|
+
value,
|
|
2020
|
+
label,
|
|
2021
|
+
})),
|
|
1994
2022
|
});
|
|
2023
|
+
const BaseCompareOperation = {
|
|
2024
|
+
LessThan: 0,
|
|
2025
|
+
LessThanOrEqual: 1,
|
|
2026
|
+
GreaterThan: 2,
|
|
2027
|
+
GreaterThanOrEqual: 3,
|
|
2028
|
+
Equal: 4,
|
|
2029
|
+
NotEqual: 5,
|
|
2030
|
+
};
|
|
1995
2031
|
// Enums: Compare Operation
|
|
1996
2032
|
registry.registerEnum({
|
|
1997
2033
|
id: "enum:base.compare.operation",
|
|
1998
|
-
options: [
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2034
|
+
options: Object.entries(BaseCompareOperation).map(([label, value]) => ({
|
|
2035
|
+
value,
|
|
2036
|
+
label,
|
|
2037
|
+
})),
|
|
2038
|
+
});
|
|
2039
|
+
const BaseLogicOperation = {
|
|
2040
|
+
Not: 0,
|
|
2041
|
+
And: 1,
|
|
2042
|
+
Or: 2,
|
|
2043
|
+
Xor: 3,
|
|
2044
|
+
};
|
|
2045
|
+
// Enums: Logic Operation
|
|
2046
|
+
registry.registerEnum({
|
|
2047
|
+
id: "enum:base.logic.operation",
|
|
2048
|
+
options: Object.entries(BaseLogicOperation).map(([label, value]) => ({
|
|
2049
|
+
value,
|
|
2050
|
+
label,
|
|
2051
|
+
})),
|
|
2006
2052
|
});
|
|
2007
2053
|
// Number
|
|
2008
2054
|
registry.registerNode({
|
|
@@ -2127,48 +2173,49 @@ function setupBasicGraphRegistry() {
|
|
|
2127
2173
|
// Registry-level defaults: Add by default, A=[1], B=[1]
|
|
2128
2174
|
inputDefaults: { Operation: 0, A: [1], B: [1] },
|
|
2129
2175
|
impl: (ins) => {
|
|
2130
|
-
|
|
2131
|
-
const
|
|
2132
|
-
const b = ins.B === undefined ? [] : asArray(ins.B);
|
|
2176
|
+
const a = asArray(ins.A ?? []);
|
|
2177
|
+
const b = asArray(ins.B ?? []);
|
|
2133
2178
|
const op = Number(ins.Operation ?? 0) | 0;
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2179
|
+
const unaryByOp = {
|
|
2180
|
+
[BaseMathOperation.Round]: (x) => Math.round(x),
|
|
2181
|
+
[BaseMathOperation.Floor]: (x) => Math.floor(x),
|
|
2182
|
+
[BaseMathOperation.Ceil]: (x) => Math.ceil(x),
|
|
2183
|
+
[BaseMathOperation.Abs]: (x) => Math.abs(x),
|
|
2184
|
+
[BaseMathOperation.Sin]: (x) => Math.sin(x),
|
|
2185
|
+
[BaseMathOperation.Cos]: (x) => Math.cos(x),
|
|
2186
|
+
[BaseMathOperation.Tan]: (x) => Math.tan(x),
|
|
2187
|
+
[BaseMathOperation.Asin]: (x) => Math.asin(x),
|
|
2188
|
+
[BaseMathOperation.Acos]: (x) => Math.acos(x),
|
|
2189
|
+
[BaseMathOperation.Atan]: (x) => Math.atan(x),
|
|
2190
|
+
[BaseMathOperation.Sqrt]: (x) => Math.sqrt(x),
|
|
2191
|
+
[BaseMathOperation.Exp]: (x) => Math.exp(x),
|
|
2192
|
+
[BaseMathOperation.Log]: (x) => Math.log(x),
|
|
2193
|
+
};
|
|
2194
|
+
if (unaryByOp[op])
|
|
2195
|
+
return { Result: a.map((x) => unaryByOp[op](Number(x))) };
|
|
2196
|
+
const aggregateByOp = {
|
|
2197
|
+
[BaseMathOperation.Sum]: (arr) => arr.reduce((s, x) => s + Number(x || 0), 0),
|
|
2198
|
+
[BaseMathOperation.Avg]: (arr) => {
|
|
2199
|
+
const sum = arr.reduce((s, x) => s + Number(x || 0), 0);
|
|
2200
|
+
return arr.length ? sum / arr.length : 0;
|
|
2201
|
+
},
|
|
2202
|
+
[BaseMathOperation.MinAll]: (arr) => arr.length ? Math.min(...arr.map((x) => Number(x))) : 0,
|
|
2203
|
+
[BaseMathOperation.MaxAll]: (arr) => arr.length ? Math.max(...arr.map((x) => Number(x))) : 0,
|
|
2204
|
+
};
|
|
2205
|
+
if (aggregateByOp[op] !== undefined)
|
|
2206
|
+
return { Result: [aggregateByOp[op](a)] };
|
|
2207
|
+
const binaryByOp = {
|
|
2208
|
+
[BaseMathOperation.Add]: (x, y) => x + y,
|
|
2209
|
+
[BaseMathOperation.Subtract]: (x, y) => x - y,
|
|
2210
|
+
[BaseMathOperation.Multiply]: (x, y) => x * y,
|
|
2211
|
+
[BaseMathOperation.Divide]: (x, y) => x / (y || 1),
|
|
2212
|
+
[BaseMathOperation.Min]: (x, y) => Math.min(x, y),
|
|
2213
|
+
[BaseMathOperation.Max]: (x, y) => Math.max(x, y),
|
|
2214
|
+
[BaseMathOperation.Modulo]: (x, y) => (y ? x % y : 0),
|
|
2215
|
+
[BaseMathOperation.Power]: (x, y) => Math.pow(x, y),
|
|
2216
|
+
};
|
|
2217
|
+
const fn = binaryByOp[op] || binaryByOp[BaseMathOperation.Add];
|
|
2160
2218
|
const len = Math.max(a.length, b.length);
|
|
2161
|
-
const ops = [
|
|
2162
|
-
(x, y) => x + y,
|
|
2163
|
-
(x, y) => x - y,
|
|
2164
|
-
(x, y) => x * y,
|
|
2165
|
-
(x, y) => x / (y || 1),
|
|
2166
|
-
(x, y) => Math.min(x, y),
|
|
2167
|
-
(x, y) => Math.max(x, y),
|
|
2168
|
-
(x, y) => (y ? x % y : 0),
|
|
2169
|
-
(x, y) => Math.pow(x, y),
|
|
2170
|
-
];
|
|
2171
|
-
const fn = ops[op] ?? ops[0];
|
|
2172
2219
|
const out = new Array(len).fill(0).map((_, i) => {
|
|
2173
2220
|
const ax = a.length === 1 && len > 1 ? a[0] : a[i] ?? 0;
|
|
2174
2221
|
const bx = b.length === 1 && len > 1 ? b[0] : b[i] ?? 0;
|
|
@@ -2189,16 +2236,16 @@ function setupBasicGraphRegistry() {
|
|
|
2189
2236
|
outputs: { Result: "base.bool[]" },
|
|
2190
2237
|
impl: (ins) => {
|
|
2191
2238
|
const [a, b] = broadcast(ins.A, ins.B);
|
|
2192
|
-
const op = Number(ins.Operation ??
|
|
2193
|
-
const
|
|
2194
|
-
(x, y) => x < y,
|
|
2195
|
-
(x, y) => x <= y,
|
|
2196
|
-
(x, y) => x > y,
|
|
2197
|
-
(x, y) => x >= y,
|
|
2198
|
-
(x, y) => x === y,
|
|
2199
|
-
(x, y) => x !== y,
|
|
2200
|
-
|
|
2201
|
-
const fn =
|
|
2239
|
+
const op = Number(ins.Operation ?? BaseCompareOperation.Equal) | 0;
|
|
2240
|
+
const compareByOp = {
|
|
2241
|
+
[BaseCompareOperation.LessThan]: (x, y) => x < y,
|
|
2242
|
+
[BaseCompareOperation.LessThanOrEqual]: (x, y) => x <= y,
|
|
2243
|
+
[BaseCompareOperation.GreaterThan]: (x, y) => x > y,
|
|
2244
|
+
[BaseCompareOperation.GreaterThanOrEqual]: (x, y) => x >= y,
|
|
2245
|
+
[BaseCompareOperation.Equal]: (x, y) => x === y,
|
|
2246
|
+
[BaseCompareOperation.NotEqual]: (x, y) => x !== y,
|
|
2247
|
+
};
|
|
2248
|
+
const fn = compareByOp[op] || compareByOp[BaseCompareOperation.Equal];
|
|
2202
2249
|
return { Result: a.map((x, i) => fn(Number(x), Number(b[i] ?? 0))) };
|
|
2203
2250
|
},
|
|
2204
2251
|
});
|
|
@@ -2206,19 +2253,29 @@ function setupBasicGraphRegistry() {
|
|
|
2206
2253
|
registry.registerNode({
|
|
2207
2254
|
id: "base.logic",
|
|
2208
2255
|
categoryId: "compute",
|
|
2209
|
-
inputs: {
|
|
2210
|
-
|
|
2256
|
+
inputs: {
|
|
2257
|
+
Operation: "enum:base.logic.operation",
|
|
2258
|
+
A: "base.bool[]",
|
|
2259
|
+
B: "base.bool[]",
|
|
2260
|
+
},
|
|
2261
|
+
outputs: { Result: "base.bool[]" },
|
|
2262
|
+
inputDefaults: { Operation: BaseLogicOperation.Not },
|
|
2211
2263
|
impl: (ins) => {
|
|
2212
|
-
const op =
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2264
|
+
const op = Number(ins.Operation ?? BaseLogicOperation.Not) | 0;
|
|
2265
|
+
if (op === BaseLogicOperation.Not) {
|
|
2266
|
+
const a = asBoolArray(ins.A ?? []);
|
|
2267
|
+
return { Result: a.map((x) => !x) };
|
|
2268
|
+
}
|
|
2269
|
+
const [a, b] = broadcastBool(ins.A ?? [], ins.B ?? []);
|
|
2270
|
+
const logicByOp = {
|
|
2271
|
+
[BaseLogicOperation.And]: (x, y) => x && y,
|
|
2272
|
+
[BaseLogicOperation.Or]: (x, y) => x || y,
|
|
2273
|
+
[BaseLogicOperation.Xor]: (x, y) => Boolean(x ? !y : y),
|
|
2274
|
+
};
|
|
2275
|
+
const fn = logicByOp[op] || logicByOp[BaseLogicOperation.And];
|
|
2276
|
+
return {
|
|
2277
|
+
Result: a.map((x, i) => fn(Boolean(x), Boolean(b[i] ?? false))),
|
|
2278
|
+
};
|
|
2222
2279
|
},
|
|
2223
2280
|
});
|
|
2224
2281
|
// Strings
|
|
@@ -2477,7 +2534,7 @@ function setupBasicGraphRegistry() {
|
|
|
2477
2534
|
inputs: {
|
|
2478
2535
|
Object: "base.object",
|
|
2479
2536
|
Pointers: "base.string[]",
|
|
2480
|
-
NewValues: "base.
|
|
2537
|
+
NewValues: "base.object",
|
|
2481
2538
|
},
|
|
2482
2539
|
outputs: { Result: "base.object" },
|
|
2483
2540
|
impl: (ins) => {
|