pinets 0.5.0 → 0.6.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/README.md +47 -3
- package/dist/pinets.dev.browser.js +3019 -391
- package/dist/pinets.dev.cjs +3106 -365
- package/dist/pinets.dev.cjs.map +1 -1
- package/dist/pinets.dev.es.js +3110 -360
- package/dist/pinets.dev.es.js.map +1 -1
- package/dist/pinets.min.browser.js +15 -14
- package/dist/pinets.min.cjs +15 -14
- package/dist/pinets.min.es.js +5 -4
- package/dist/types/Context.class.d.ts +19 -3
- package/dist/types/PineTS.class.d.ts +2 -0
- package/dist/types/marketData/Binance/BinanceProvider.class.d.ts +2 -1
- package/dist/types/marketData/IProvider.d.ts +43 -0
- package/dist/types/marketData/Mock/MockProvider.class.d.ts +13 -1
- package/dist/types/namespaces/Barstate.d.ts +13 -0
- package/dist/types/namespaces/Core.d.ts +15 -0
- package/dist/types/namespaces/Log.d.ts +10 -0
- package/dist/types/namespaces/Str.d.ts +23 -0
- package/dist/types/namespaces/Timeframe.d.ts +17 -0
- package/dist/types/namespaces/Types.d.ts +86 -0
- package/dist/types/namespaces/array/PineArrayObject.d.ts +102 -1
- package/dist/types/namespaces/array/array.index.d.ts +1 -129
- package/dist/types/namespaces/array/methods/avg.d.ts +2 -1
- package/dist/types/namespaces/array/methods/binary_search.d.ts +2 -0
- package/dist/types/namespaces/array/methods/binary_search_leftmost.d.ts +2 -0
- package/dist/types/namespaces/array/methods/binary_search_rightmost.d.ts +2 -0
- package/dist/types/namespaces/array/methods/covariance.d.ts +2 -1
- package/dist/types/namespaces/array/methods/every.d.ts +1 -1
- package/dist/types/namespaces/array/methods/fill.d.ts +2 -1
- package/dist/types/namespaces/array/methods/join.d.ts +5 -0
- package/dist/types/namespaces/array/methods/median.d.ts +2 -0
- package/dist/types/namespaces/array/methods/mode.d.ts +2 -0
- package/dist/types/namespaces/array/methods/new.d.ts +2 -1
- package/dist/types/namespaces/array/methods/new_float.d.ts +2 -1
- package/dist/types/namespaces/array/methods/new_int.d.ts +2 -1
- package/dist/types/namespaces/array/methods/percentile_linear_interpolation.d.ts +3 -0
- package/dist/types/namespaces/array/methods/percentile_nearest_rank.d.ts +2 -0
- package/dist/types/namespaces/array/methods/percentrank.d.ts +2 -0
- package/dist/types/namespaces/array/methods/push.d.ts +2 -1
- package/dist/types/namespaces/array/methods/range.d.ts +2 -1
- package/dist/types/namespaces/array/methods/set.d.ts +2 -1
- package/dist/types/namespaces/array/methods/some.d.ts +1 -1
- package/dist/types/namespaces/array/methods/sort.d.ts +2 -1
- package/dist/types/namespaces/array/methods/sort_indices.d.ts +2 -1
- package/dist/types/namespaces/array/methods/stdev.d.ts +2 -1
- package/dist/types/namespaces/array/methods/sum.d.ts +2 -1
- package/dist/types/namespaces/array/methods/unshift.d.ts +2 -1
- package/dist/types/namespaces/array/methods/variance.d.ts +2 -1
- package/dist/types/namespaces/array/utils.d.ts +5 -0
- package/dist/types/namespaces/input/methods/any.d.ts +1 -2
- package/dist/types/namespaces/input/methods/bool.d.ts +1 -2
- package/dist/types/namespaces/input/methods/color.d.ts +1 -2
- package/dist/types/namespaces/input/methods/enum.d.ts +1 -2
- package/dist/types/namespaces/input/methods/float.d.ts +1 -2
- package/dist/types/namespaces/input/methods/int.d.ts +1 -2
- package/dist/types/namespaces/input/methods/param.d.ts +1 -1
- package/dist/types/namespaces/input/methods/price.d.ts +1 -2
- package/dist/types/namespaces/input/methods/session.d.ts +1 -2
- package/dist/types/namespaces/input/methods/source.d.ts +1 -2
- package/dist/types/namespaces/input/methods/string.d.ts +1 -2
- package/dist/types/namespaces/input/methods/symbol.d.ts +1 -2
- package/dist/types/namespaces/input/methods/text_area.d.ts +1 -2
- package/dist/types/namespaces/input/methods/time.d.ts +1 -2
- package/dist/types/namespaces/input/methods/timeframe.d.ts +1 -2
- package/dist/types/namespaces/input/types.d.ts +12 -1
- package/dist/types/namespaces/input/utils.d.ts +14 -0
- package/dist/types/namespaces/map/PineMapObject.d.ts +26 -0
- package/dist/types/namespaces/map/map.index.d.ts +7 -0
- package/dist/types/namespaces/map/methods/clear.d.ts +3 -0
- package/dist/types/namespaces/map/methods/contains.d.ts +3 -0
- package/dist/types/namespaces/map/methods/copy.d.ts +3 -0
- package/dist/types/namespaces/map/methods/get.d.ts +3 -0
- package/dist/types/namespaces/map/methods/keys.d.ts +4 -0
- package/dist/types/namespaces/map/methods/new.d.ts +3 -0
- package/dist/types/namespaces/map/methods/param.d.ts +1 -0
- package/dist/types/namespaces/map/methods/put.d.ts +3 -0
- package/dist/types/namespaces/map/methods/put_all.d.ts +3 -0
- package/dist/types/namespaces/map/methods/remove.d.ts +3 -0
- package/dist/types/namespaces/map/methods/size.d.ts +3 -0
- package/dist/types/namespaces/map/methods/values.d.ts +4 -0
- package/dist/types/namespaces/math/math.index.d.ts +18 -0
- package/dist/types/namespaces/math/methods/abs.d.ts +2 -1
- package/dist/types/namespaces/math/methods/e.d.ts +5 -0
- package/dist/types/namespaces/math/methods/phi.d.ts +5 -0
- package/dist/types/namespaces/math/methods/pi.d.ts +5 -0
- package/dist/types/namespaces/math/methods/round_to_mintick.d.ts +2 -0
- package/dist/types/namespaces/math/methods/rphi.d.ts +5 -0
- package/dist/types/namespaces/math/methods/sign.d.ts +2 -0
- package/dist/types/namespaces/matrix/PineMatrixObject.d.ts +102 -0
- package/dist/types/namespaces/matrix/matrix.index.d.ts +7 -0
- package/dist/types/namespaces/matrix/methods/add_col.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/add_row.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/avg.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/col.d.ts +4 -0
- package/dist/types/namespaces/matrix/methods/columns.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/concat.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/copy.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/det.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/diff.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/eigenvalues.d.ts +4 -0
- package/dist/types/namespaces/matrix/methods/eigenvectors.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/elements_count.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/fill.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/get.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/inv.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_antidiagonal.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_antisymmetric.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_binary.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_diagonal.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_identity.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_square.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_stochastic.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_symmetric.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_triangular.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/is_zero.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/kron.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/max.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/median.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/min.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/mode.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/mult.d.ts +4 -0
- package/dist/types/namespaces/matrix/methods/new.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/param.d.ts +1 -0
- package/dist/types/namespaces/matrix/methods/pinv.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/pow.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/rank.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/remove_col.d.ts +4 -0
- package/dist/types/namespaces/matrix/methods/remove_row.d.ts +4 -0
- package/dist/types/namespaces/matrix/methods/reshape.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/reverse.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/row.d.ts +4 -0
- package/dist/types/namespaces/matrix/methods/rows.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/set.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/sort.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/submatrix.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/sum.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/swap_columns.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/swap_rows.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/trace.d.ts +3 -0
- package/dist/types/namespaces/matrix/methods/transpose.d.ts +3 -0
- package/dist/types/namespaces/request/methods/security_lower_tf.d.ts +9 -0
- package/dist/types/namespaces/request/request.index.d.ts +3 -0
- package/dist/types/namespaces/ta/methods/atr.d.ts +2 -1
- package/dist/types/namespaces/ta/methods/tr.d.ts +2 -1
- package/dist/types/transpiler/settings.d.ts +4 -0
- package/dist/types/types/PineTypes.d.ts +11 -0
- package/package.json +6 -4
package/dist/pinets.dev.es.js
CHANGED
|
@@ -22,9 +22,9 @@ import * as fs from 'fs';
|
|
|
22
22
|
import * as path from 'path';
|
|
23
23
|
import { fileURLToPath } from 'url';
|
|
24
24
|
|
|
25
|
-
var __defProp$
|
|
26
|
-
var __defNormalProp$
|
|
27
|
-
var __publicField$
|
|
25
|
+
var __defProp$d = Object.defineProperty;
|
|
26
|
+
var __defNormalProp$d = (obj, key, value) => key in obj ? __defProp$d(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
27
|
+
var __publicField$d = (obj, key, value) => __defNormalProp$d(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
28
28
|
const JS_GLOBAL_LITERALS = /* @__PURE__ */ new Set([
|
|
29
29
|
"Infinity",
|
|
30
30
|
"NaN",
|
|
@@ -65,23 +65,23 @@ const JS_GLOBAL_OBJECTS = /* @__PURE__ */ new Set([
|
|
|
65
65
|
]);
|
|
66
66
|
class ScopeManager {
|
|
67
67
|
constructor() {
|
|
68
|
-
__publicField$
|
|
69
|
-
__publicField$
|
|
70
|
-
__publicField$
|
|
71
|
-
__publicField$
|
|
72
|
-
__publicField$
|
|
73
|
-
__publicField$
|
|
74
|
-
__publicField$
|
|
75
|
-
__publicField$
|
|
76
|
-
__publicField$
|
|
77
|
-
__publicField$
|
|
68
|
+
__publicField$d(this, "scopes", []);
|
|
69
|
+
__publicField$d(this, "scopeTypes", []);
|
|
70
|
+
__publicField$d(this, "scopeCounts", /* @__PURE__ */ new Map());
|
|
71
|
+
__publicField$d(this, "contextBoundVars", /* @__PURE__ */ new Set());
|
|
72
|
+
__publicField$d(this, "arrayPatternElements", /* @__PURE__ */ new Set());
|
|
73
|
+
__publicField$d(this, "rootParams", /* @__PURE__ */ new Set());
|
|
74
|
+
__publicField$d(this, "localSeriesVars", /* @__PURE__ */ new Set());
|
|
75
|
+
__publicField$d(this, "varKinds", /* @__PURE__ */ new Map());
|
|
76
|
+
__publicField$d(this, "loopVars", /* @__PURE__ */ new Set());
|
|
77
|
+
__publicField$d(this, "loopVarNames", /* @__PURE__ */ new Map());
|
|
78
78
|
// Map original names to transformed names
|
|
79
|
-
__publicField$
|
|
80
|
-
__publicField$
|
|
81
|
-
__publicField$
|
|
82
|
-
__publicField$
|
|
83
|
-
__publicField$
|
|
84
|
-
__publicField$
|
|
79
|
+
__publicField$d(this, "paramIdCounter", 0);
|
|
80
|
+
__publicField$d(this, "cacheIdCounter", 0);
|
|
81
|
+
__publicField$d(this, "tempVarCounter", 0);
|
|
82
|
+
__publicField$d(this, "taCallIdCounter", 0);
|
|
83
|
+
__publicField$d(this, "hoistingStack", []);
|
|
84
|
+
__publicField$d(this, "suppressHoisting", false);
|
|
85
85
|
this.pushScope("glb");
|
|
86
86
|
}
|
|
87
87
|
get nextParamIdArg() {
|
|
@@ -344,6 +344,45 @@ const ASTFactory = {
|
|
|
344
344
|
}
|
|
345
345
|
};
|
|
346
346
|
|
|
347
|
+
const CONTEXT_DATA_VARS = ["open", "high", "low", "close", "volume", "hl2", "hlc3", "ohlc4", "openTime", "closeTime"];
|
|
348
|
+
const CONTEXT_PINE_VARS = [
|
|
349
|
+
"input",
|
|
350
|
+
"ta",
|
|
351
|
+
"math",
|
|
352
|
+
"request",
|
|
353
|
+
"array",
|
|
354
|
+
"na",
|
|
355
|
+
"plotchar",
|
|
356
|
+
"color",
|
|
357
|
+
"plot",
|
|
358
|
+
"nz",
|
|
359
|
+
"strategy",
|
|
360
|
+
"library",
|
|
361
|
+
"str",
|
|
362
|
+
"box",
|
|
363
|
+
"line",
|
|
364
|
+
"label",
|
|
365
|
+
"table",
|
|
366
|
+
"map",
|
|
367
|
+
"matrix",
|
|
368
|
+
"log",
|
|
369
|
+
"map",
|
|
370
|
+
//market info
|
|
371
|
+
"timeframe",
|
|
372
|
+
"syminfo",
|
|
373
|
+
"barstate",
|
|
374
|
+
//builtin variables
|
|
375
|
+
"bar_index",
|
|
376
|
+
"last_bar_index",
|
|
377
|
+
"last_bar_time",
|
|
378
|
+
// Pine Script enum types
|
|
379
|
+
"order",
|
|
380
|
+
"currency",
|
|
381
|
+
"display",
|
|
382
|
+
"dayofweek"
|
|
383
|
+
];
|
|
384
|
+
const CONTEXT_CORE_VARS = ["na", "nz", "plot", "plotchar", "color"];
|
|
385
|
+
|
|
347
386
|
function injectImplicitImports(ast) {
|
|
348
387
|
let mainBody = null;
|
|
349
388
|
let contextParamName = CONTEXT_NAME;
|
|
@@ -409,28 +448,8 @@ function injectImplicitImports(ast) {
|
|
|
409
448
|
addDeclared(stmt.id);
|
|
410
449
|
}
|
|
411
450
|
});
|
|
412
|
-
const contextDataVars =
|
|
413
|
-
const contextPineVars =
|
|
414
|
-
"input",
|
|
415
|
-
"ta",
|
|
416
|
-
"math",
|
|
417
|
-
"request",
|
|
418
|
-
"array",
|
|
419
|
-
"na",
|
|
420
|
-
"plotchar",
|
|
421
|
-
"color",
|
|
422
|
-
"plot",
|
|
423
|
-
"nz",
|
|
424
|
-
"strategy",
|
|
425
|
-
"library",
|
|
426
|
-
"str",
|
|
427
|
-
"box",
|
|
428
|
-
"line",
|
|
429
|
-
"label",
|
|
430
|
-
"table",
|
|
431
|
-
"map",
|
|
432
|
-
"matrix"
|
|
433
|
-
];
|
|
451
|
+
const contextDataVars = CONTEXT_DATA_VARS;
|
|
452
|
+
const contextPineVars = CONTEXT_PINE_VARS;
|
|
434
453
|
const missingDataVars = contextDataVars.filter((v) => !declaredVars.has(v));
|
|
435
454
|
const missingPineVars = contextPineVars.filter((v) => !declaredVars.has(v));
|
|
436
455
|
const neededDataVars = missingDataVars.filter((v) => usedIdentifiers.has(v));
|
|
@@ -511,29 +530,9 @@ function normalizeNativeImports(ast) {
|
|
|
511
530
|
}
|
|
512
531
|
}
|
|
513
532
|
if (!mainBody) return;
|
|
514
|
-
const contextDataVars =
|
|
515
|
-
const contextPineVars =
|
|
516
|
-
|
|
517
|
-
"ta",
|
|
518
|
-
"math",
|
|
519
|
-
"request",
|
|
520
|
-
"array",
|
|
521
|
-
"na",
|
|
522
|
-
"plotchar",
|
|
523
|
-
"color",
|
|
524
|
-
"plot",
|
|
525
|
-
"nz",
|
|
526
|
-
"strategy",
|
|
527
|
-
"library",
|
|
528
|
-
"str",
|
|
529
|
-
"box",
|
|
530
|
-
"line",
|
|
531
|
-
"label",
|
|
532
|
-
"table",
|
|
533
|
-
"map",
|
|
534
|
-
"matrix"
|
|
535
|
-
]);
|
|
536
|
-
const contextCoreVars = /* @__PURE__ */ new Set(["na", "nz", "plot", "plotchar", "color"]);
|
|
533
|
+
const contextDataVars = new Set(CONTEXT_DATA_VARS);
|
|
534
|
+
const contextPineVars = new Set(CONTEXT_PINE_VARS);
|
|
535
|
+
const contextCoreVars = new Set(CONTEXT_CORE_VARS);
|
|
537
536
|
const renames = /* @__PURE__ */ new Map();
|
|
538
537
|
mainBody.forEach((stmt) => {
|
|
539
538
|
if (stmt.type === "VariableDeclaration") {
|
|
@@ -785,6 +784,7 @@ const UNDEFINED_ARG = {
|
|
|
785
784
|
type: "Identifier",
|
|
786
785
|
name: "undefined"
|
|
787
786
|
};
|
|
787
|
+
const KNOWN_NAMESPACES = ["ta", "math", "request", "array", "input"];
|
|
788
788
|
function transformArrayIndex(node, scopeManager) {
|
|
789
789
|
if (node.computed && node.property.type === "Identifier") {
|
|
790
790
|
if (scopeManager.isLoopVariable(node.property.name)) {
|
|
@@ -844,12 +844,6 @@ function transformIdentifier(node, scopeManager) {
|
|
|
844
844
|
if (scopeManager.isLoopVariable(node.name)) {
|
|
845
845
|
return;
|
|
846
846
|
}
|
|
847
|
-
if (scopeManager.isContextBound(node.name) && !scopeManager.isRootParam(node.name)) {
|
|
848
|
-
return;
|
|
849
|
-
}
|
|
850
|
-
const isNamespaceMember = node.parent && node.parent.type === "MemberExpression" && node.parent.object === node && scopeManager.isContextBound(node.name);
|
|
851
|
-
const isParamCall = node.parent && node.parent.type === "CallExpression" && node.parent.callee && node.parent.callee.type === "MemberExpression" && node.parent.callee.property.name === "param";
|
|
852
|
-
node.parent && node.parent.type === "AssignmentExpression" && node.parent.left === node;
|
|
853
847
|
let isSeriesFunctionArg = false;
|
|
854
848
|
if (node.parent && node.parent.type === "CallExpression" && node.parent.arguments.includes(node)) {
|
|
855
849
|
const callee = node.parent.callee;
|
|
@@ -860,13 +854,20 @@ function transformIdentifier(node, scopeManager) {
|
|
|
860
854
|
isSeriesFunctionArg = true;
|
|
861
855
|
}
|
|
862
856
|
} else {
|
|
863
|
-
|
|
857
|
+
const isNamespaceCall = callee.type === "MemberExpression" && callee.object && callee.object.type === "Identifier" && KNOWN_NAMESPACES.includes(callee.object.name);
|
|
858
|
+
if (callee.type === "MemberExpression" && !isNamespaceCall) {
|
|
859
|
+
isSeriesFunctionArg = false;
|
|
860
|
+
} else {
|
|
861
|
+
isSeriesFunctionArg = true;
|
|
862
|
+
}
|
|
864
863
|
}
|
|
865
864
|
}
|
|
866
|
-
const
|
|
867
|
-
const
|
|
865
|
+
const isNamespaceMember = node.parent && node.parent.type === "MemberExpression" && node.parent.object === node && scopeManager.isContextBound(node.name);
|
|
866
|
+
const isParamCall = node.parent && node.parent.type === "CallExpression" && node.parent.callee && node.parent.callee.type === "MemberExpression" && node.parent.callee.property.name === "param";
|
|
867
|
+
node.parent && node.parent.type === "AssignmentExpression" && node.parent.left === node;
|
|
868
868
|
const isFunctionCall = node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
|
|
869
869
|
const hasArrayAccess = node.parent && node.parent.type === "MemberExpression" && node.parent.computed && node.parent.object === node;
|
|
870
|
+
const isArrayIndexInNamespaceCall = node.parent && node.parent.type === "MemberExpression" && node.parent.computed && node.parent.property === node && node.parent.parent && node.parent.parent.type === "CallExpression" && node.parent.parent.callee && node.parent.parent.callee.type === "MemberExpression" && scopeManager.isContextBound(node.parent.parent.callee.object.name);
|
|
870
871
|
if (isNamespaceMember || isParamCall || isSeriesFunctionArg || isArrayIndexInNamespaceCall || isFunctionCall) {
|
|
871
872
|
if (isFunctionCall) {
|
|
872
873
|
return;
|
|
@@ -874,13 +875,23 @@ function transformIdentifier(node, scopeManager) {
|
|
|
874
875
|
if (scopeManager.isLocalSeriesVar(node.name)) {
|
|
875
876
|
return;
|
|
876
877
|
}
|
|
878
|
+
if (scopeManager.isContextBound(node.name) && !scopeManager.isRootParam(node.name)) {
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
877
881
|
const [scopedName2, kind2] = scopeManager.getVariable(node.name);
|
|
878
882
|
const memberExpr2 = ASTFactory.createContextVariableReference(kind2, scopedName2);
|
|
879
883
|
Object.assign(node, memberExpr2);
|
|
880
884
|
return;
|
|
881
885
|
}
|
|
886
|
+
const isContextBoundVar = scopeManager.isContextBound(node.name) && !scopeManager.isRootParam(node.name);
|
|
887
|
+
if (isContextBoundVar) {
|
|
888
|
+
const isFunctionArg = node.parent && node.parent.type === "CallExpression" && node.parent.arguments.includes(node);
|
|
889
|
+
if (!isFunctionArg) {
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
882
893
|
if (scopeManager.isLocalSeriesVar(node.name)) {
|
|
883
|
-
if (!hasArrayAccess
|
|
894
|
+
if (!hasArrayAccess) {
|
|
884
895
|
const memberExpr2 = ASTFactory.createIdentifier(node.name);
|
|
885
896
|
const accessExpr = ASTFactory.createGetCall(memberExpr2, 0);
|
|
886
897
|
Object.assign(node, accessExpr);
|
|
@@ -888,8 +899,16 @@ function transformIdentifier(node, scopeManager) {
|
|
|
888
899
|
return;
|
|
889
900
|
}
|
|
890
901
|
const [scopedName, kind] = scopeManager.getVariable(node.name);
|
|
891
|
-
|
|
892
|
-
if (
|
|
902
|
+
let memberExpr;
|
|
903
|
+
if (isContextBoundVar) {
|
|
904
|
+
memberExpr = ASTFactory.createIdentifier(node.name);
|
|
905
|
+
} else {
|
|
906
|
+
if (scopedName === node.name && !scopeManager.isContextBound(node.name)) {
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
memberExpr = ASTFactory.createContextVariableReference(kind, scopedName);
|
|
910
|
+
}
|
|
911
|
+
if (!hasArrayAccess) {
|
|
893
912
|
const accessExpr = ASTFactory.createGetCall(memberExpr, 0);
|
|
894
913
|
Object.assign(node, accessExpr);
|
|
895
914
|
} else {
|
|
@@ -901,7 +920,6 @@ function transformMemberExpression(memberNode, originalParamName, scopeManager)
|
|
|
901
920
|
if (memberNode.object && memberNode.object.type === "Identifier" && memberNode.object.name === "Math") {
|
|
902
921
|
return;
|
|
903
922
|
}
|
|
904
|
-
const KNOWN_NAMESPACES = ["ta", "math", "request", "array", "input"];
|
|
905
923
|
const isDirectNamespaceMemberAccess = memberNode.object && memberNode.object.type === "Identifier" && KNOWN_NAMESPACES.includes(memberNode.object.name) && scopeManager.isContextBound(memberNode.object.name) && !memberNode.computed;
|
|
906
924
|
if (isDirectNamespaceMemberAccess) {
|
|
907
925
|
const isAlreadyBeingCalled = memberNode.parent && memberNode.parent.type === "CallExpression" && memberNode.parent.callee === memberNode;
|
|
@@ -945,6 +963,9 @@ function transformMemberExpression(memberNode, originalParamName, scopeManager)
|
|
|
945
963
|
if (memberNode.start) getCall.start = memberNode.start;
|
|
946
964
|
if (memberNode.end) getCall.end = memberNode.end;
|
|
947
965
|
Object.assign(memberNode, getCall);
|
|
966
|
+
delete memberNode.object;
|
|
967
|
+
delete memberNode.property;
|
|
968
|
+
delete memberNode.computed;
|
|
948
969
|
}
|
|
949
970
|
}
|
|
950
971
|
function transformIdentifierForParam(node, scopeManager) {
|
|
@@ -1097,16 +1118,31 @@ function getParamFromConditionalExpression(node, scopeManager, namespace) {
|
|
|
1097
1118
|
}
|
|
1098
1119
|
},
|
|
1099
1120
|
ConditionalExpression(node2, state, c) {
|
|
1121
|
+
const newState = { ...state, parent: node2 };
|
|
1100
1122
|
if (node2.test) {
|
|
1101
|
-
c(node2.test,
|
|
1123
|
+
c(node2.test, newState);
|
|
1102
1124
|
}
|
|
1103
1125
|
if (node2.consequent) {
|
|
1104
|
-
c(node2.consequent,
|
|
1126
|
+
c(node2.consequent, newState);
|
|
1105
1127
|
}
|
|
1106
1128
|
if (node2.alternate) {
|
|
1107
|
-
c(node2.alternate,
|
|
1129
|
+
c(node2.alternate, newState);
|
|
1108
1130
|
}
|
|
1109
1131
|
},
|
|
1132
|
+
BinaryExpression(node2, state, c) {
|
|
1133
|
+
const newState = { ...state, parent: node2 };
|
|
1134
|
+
c(node2.left, newState);
|
|
1135
|
+
c(node2.right, newState);
|
|
1136
|
+
},
|
|
1137
|
+
LogicalExpression(node2, state, c) {
|
|
1138
|
+
const newState = { ...state, parent: node2 };
|
|
1139
|
+
c(node2.left, newState);
|
|
1140
|
+
c(node2.right, newState);
|
|
1141
|
+
},
|
|
1142
|
+
UnaryExpression(node2, state, c) {
|
|
1143
|
+
const newState = { ...state, parent: node2 };
|
|
1144
|
+
c(node2.argument, newState);
|
|
1145
|
+
},
|
|
1110
1146
|
CallExpression(node2, state, c) {
|
|
1111
1147
|
const isNamespaceCall = node2.callee && node2.callee.type === "MemberExpression" && node2.callee.object && node2.callee.object.type === "Identifier" && scopeManager.isContextBound(node2.callee.object.name);
|
|
1112
1148
|
transformCallExpression(node2, scopeManager);
|
|
@@ -1195,6 +1231,20 @@ function transformFunctionArgument(arg, namespace, scopeManager) {
|
|
|
1195
1231
|
if (arg.type === "ObjectExpression") {
|
|
1196
1232
|
arg.properties = arg.properties.map((prop) => {
|
|
1197
1233
|
if (prop.value.name) {
|
|
1234
|
+
if (scopeManager.isContextBound(prop.value.name) && !scopeManager.isRootParam(prop.value.name)) {
|
|
1235
|
+
return {
|
|
1236
|
+
type: "Property",
|
|
1237
|
+
key: {
|
|
1238
|
+
type: "Identifier",
|
|
1239
|
+
name: prop.key.name
|
|
1240
|
+
},
|
|
1241
|
+
value: ASTFactory.createIdentifier(prop.value.name),
|
|
1242
|
+
kind: "init",
|
|
1243
|
+
method: false,
|
|
1244
|
+
shorthand: false,
|
|
1245
|
+
computed: false
|
|
1246
|
+
};
|
|
1247
|
+
}
|
|
1198
1248
|
const [scopedName, kind] = scopeManager.getVariable(prop.value.name);
|
|
1199
1249
|
return {
|
|
1200
1250
|
type: "Property",
|
|
@@ -1263,6 +1313,12 @@ function transformCallExpression(node, scopeManager, namespace) {
|
|
|
1263
1313
|
if (node._transformed) {
|
|
1264
1314
|
return;
|
|
1265
1315
|
}
|
|
1316
|
+
if (node.callee && node.callee.type === "Identifier" && KNOWN_NAMESPACES.includes(node.callee.name) && scopeManager.isContextBound(node.callee.name)) {
|
|
1317
|
+
node.callee = ASTFactory.createMemberExpression(
|
|
1318
|
+
node.callee,
|
|
1319
|
+
ASTFactory.createIdentifier("any")
|
|
1320
|
+
);
|
|
1321
|
+
}
|
|
1266
1322
|
const isNamespaceCall = node.callee && node.callee.type === "MemberExpression" && node.callee.object && node.callee.object.type === "Identifier" && (scopeManager.isContextBound(node.callee.object.name) || node.callee.object.name === "math" || node.callee.object.name === "ta");
|
|
1267
1323
|
if (isNamespaceCall) {
|
|
1268
1324
|
if (node.callee.object.name === CONTEXT_NAME && ["get", "init", "param"].includes(node.callee.property.name)) {
|
|
@@ -1299,6 +1355,11 @@ function transformCallExpression(node, scopeManager, namespace) {
|
|
|
1299
1355
|
});
|
|
1300
1356
|
node._transformed = true;
|
|
1301
1357
|
}
|
|
1358
|
+
if (!isNamespaceCall && node.callee && node.callee.type === "MemberExpression") {
|
|
1359
|
+
if (node.callee.object.type === "Identifier") {
|
|
1360
|
+
transformIdentifier(node.callee.object, scopeManager);
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1302
1363
|
node.arguments.forEach((arg) => {
|
|
1303
1364
|
walk.recursive(
|
|
1304
1365
|
arg,
|
|
@@ -1320,6 +1381,20 @@ function transformCallExpression(node, scopeManager, namespace) {
|
|
|
1320
1381
|
}
|
|
1321
1382
|
}
|
|
1322
1383
|
},
|
|
1384
|
+
BinaryExpression(node2, state, c) {
|
|
1385
|
+
const newState = { ...state, parent: node2 };
|
|
1386
|
+
c(node2.left, newState);
|
|
1387
|
+
c(node2.right, newState);
|
|
1388
|
+
},
|
|
1389
|
+
LogicalExpression(node2, state, c) {
|
|
1390
|
+
const newState = { ...state, parent: node2 };
|
|
1391
|
+
c(node2.left, newState);
|
|
1392
|
+
c(node2.right, newState);
|
|
1393
|
+
},
|
|
1394
|
+
UnaryExpression(node2, state, c) {
|
|
1395
|
+
const newState = { ...state, parent: node2 };
|
|
1396
|
+
c(node2.argument, newState);
|
|
1397
|
+
},
|
|
1323
1398
|
CallExpression(node2, state, c) {
|
|
1324
1399
|
if (!node2._transformed) {
|
|
1325
1400
|
transformCallExpression(node2, scopeManager);
|
|
@@ -1461,7 +1536,8 @@ function transformVariableDeclaration(varNode, scopeManager) {
|
|
|
1461
1536
|
}
|
|
1462
1537
|
const newName = scopeManager.addVariable(decl.id.name, varNode.kind);
|
|
1463
1538
|
const kind = varNode.kind;
|
|
1464
|
-
|
|
1539
|
+
const isArrayPatternVar = scopeManager.isArrayPatternElement(decl.id.name);
|
|
1540
|
+
if (decl.init && !isArrowFunction && !isArrayPatternVar) {
|
|
1465
1541
|
if (decl.init.type === "CallExpression" && decl.init.callee.type === "MemberExpression" && decl.init.callee.object && decl.init.callee.object.type === "Identifier" && scopeManager.isContextBound(decl.init.callee.object.name)) {
|
|
1466
1542
|
transformCallExpression(decl.init, scopeManager);
|
|
1467
1543
|
} else {
|
|
@@ -1522,7 +1598,6 @@ function transformVariableDeclaration(varNode, scopeManager) {
|
|
|
1522
1598
|
}
|
|
1523
1599
|
}
|
|
1524
1600
|
const targetVarRef = ASTFactory.createContextVariableReference(kind, newName);
|
|
1525
|
-
const isArrayPatternVar = scopeManager.isArrayPatternElement(decl.id.name);
|
|
1526
1601
|
const isArrayInit = !isArrayPatternVar && decl.init && decl.init.type === "MemberExpression" && decl.init.computed && decl.init.property && (decl.init.property.type === "Literal" || decl.init.property.type === "MemberExpression");
|
|
1527
1602
|
if (decl.init?.property?.type === "MemberExpression") {
|
|
1528
1603
|
if (!decl.init.property._indexTransformed) {
|
|
@@ -1535,10 +1610,7 @@ function transformVariableDeclaration(varNode, scopeManager) {
|
|
|
1535
1610
|
if (isArrowFunction || isArrayPatternVar) {
|
|
1536
1611
|
rightSide = decl.init;
|
|
1537
1612
|
} else if (kind === "var") {
|
|
1538
|
-
rightSide = ASTFactory.createInitVarCall(
|
|
1539
|
-
targetVarRef,
|
|
1540
|
-
decl.init
|
|
1541
|
-
);
|
|
1613
|
+
rightSide = ASTFactory.createInitVarCall(targetVarRef, decl.init);
|
|
1542
1614
|
} else {
|
|
1543
1615
|
rightSide = ASTFactory.createInitCall(
|
|
1544
1616
|
targetVarRef,
|
|
@@ -1551,7 +1623,9 @@ function transformVariableDeclaration(varNode, scopeManager) {
|
|
|
1551
1623
|
}
|
|
1552
1624
|
const assignmentExpr = ASTFactory.createExpressionStatement(ASTFactory.createAssignmentExpression(targetVarRef, rightSide));
|
|
1553
1625
|
if (isArrayPatternVar) {
|
|
1554
|
-
const
|
|
1626
|
+
const tempVarName = decl.init.object.name;
|
|
1627
|
+
const [scopedTempName, tempKind] = scopeManager.getVariable(tempVarName);
|
|
1628
|
+
const tempVarRef = ASTFactory.createContextVariableReference(tempKind, scopedTempName);
|
|
1555
1629
|
const arrayIndex = decl.init.property.value;
|
|
1556
1630
|
const getCall = ASTFactory.createGetCall(tempVarRef, 0);
|
|
1557
1631
|
const arrayAccess = {
|
|
@@ -1748,6 +1822,9 @@ function transformReturnStatement(node, scopeManager) {
|
|
|
1748
1822
|
} else if (node.argument.type === "ObjectExpression") {
|
|
1749
1823
|
node.argument.properties = node.argument.properties.map((prop) => {
|
|
1750
1824
|
if (prop.shorthand) {
|
|
1825
|
+
if (scopeManager.isContextBound(prop.value.name)) {
|
|
1826
|
+
return prop;
|
|
1827
|
+
}
|
|
1751
1828
|
const [scopedName, kind] = scopeManager.getVariable(prop.value.name);
|
|
1752
1829
|
return {
|
|
1753
1830
|
type: "Property",
|
|
@@ -1760,9 +1837,7 @@ function transformReturnStatement(node, scopeManager) {
|
|
|
1760
1837
|
};
|
|
1761
1838
|
}
|
|
1762
1839
|
if (prop.value && prop.value.type === "Identifier") {
|
|
1763
|
-
if (scopeManager.isContextBound(prop.value.name) && !scopeManager.isRootParam(prop.value.name)) {
|
|
1764
|
-
prop.value = ASTFactory.createGetCall(prop.value, 0);
|
|
1765
|
-
} else if (!scopeManager.isContextBound(prop.value.name)) {
|
|
1840
|
+
if (scopeManager.isContextBound(prop.value.name) && !scopeManager.isRootParam(prop.value.name)) ; else if (!scopeManager.isContextBound(prop.value.name)) {
|
|
1766
1841
|
const [scopedName, kind] = scopeManager.getVariable(prop.value.name);
|
|
1767
1842
|
prop.value = ASTFactory.createContextVariableReference(kind, scopedName);
|
|
1768
1843
|
}
|
|
@@ -1947,72 +2022,207 @@ function transpile(fn, options = { debug: false, ln: false }) {
|
|
|
1947
2022
|
return _wraperFunction(this);
|
|
1948
2023
|
}
|
|
1949
2024
|
|
|
1950
|
-
class PineArrayObject {
|
|
1951
|
-
constructor(array) {
|
|
1952
|
-
this.array = array;
|
|
1953
|
-
}
|
|
1954
|
-
toString() {
|
|
1955
|
-
return "PineArrayObject:" + this.array.toString();
|
|
1956
|
-
}
|
|
1957
|
-
}
|
|
1958
|
-
|
|
1959
2025
|
function abs$1(context) {
|
|
1960
2026
|
return (id) => {
|
|
1961
|
-
return new PineArrayObject(
|
|
2027
|
+
return new PineArrayObject(
|
|
2028
|
+
id.array.map((val) => Math.abs(val)),
|
|
2029
|
+
id.type,
|
|
2030
|
+
context
|
|
2031
|
+
);
|
|
1962
2032
|
};
|
|
1963
2033
|
}
|
|
1964
2034
|
|
|
1965
|
-
function avg$
|
|
2035
|
+
function avg$2(context) {
|
|
1966
2036
|
return (id) => {
|
|
1967
|
-
|
|
2037
|
+
let mean = 0;
|
|
2038
|
+
let count = 0;
|
|
2039
|
+
for (const item of id.array) {
|
|
2040
|
+
const val = Number(item);
|
|
2041
|
+
if (!isNaN(val)) {
|
|
2042
|
+
count++;
|
|
2043
|
+
mean += (val - mean) / count;
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
if (count === 0) return NaN;
|
|
2047
|
+
return context.precision(mean);
|
|
1968
2048
|
};
|
|
1969
2049
|
}
|
|
1970
2050
|
|
|
1971
|
-
function
|
|
2051
|
+
function binary_search(context) {
|
|
2052
|
+
return (id, value) => {
|
|
2053
|
+
const array = id.array;
|
|
2054
|
+
let low = 0;
|
|
2055
|
+
let high = array.length - 1;
|
|
2056
|
+
while (low <= high) {
|
|
2057
|
+
const mid = Math.floor((low + high) / 2);
|
|
2058
|
+
const midVal = array[mid];
|
|
2059
|
+
if (midVal === value) {
|
|
2060
|
+
return mid;
|
|
2061
|
+
}
|
|
2062
|
+
if (midVal < value) {
|
|
2063
|
+
low = mid + 1;
|
|
2064
|
+
} else {
|
|
2065
|
+
high = mid - 1;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
return -1;
|
|
2069
|
+
};
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
function binary_search_leftmost(context) {
|
|
2073
|
+
return (id, value) => {
|
|
2074
|
+
const array = id.array;
|
|
2075
|
+
let low = 0;
|
|
2076
|
+
let high = array.length;
|
|
2077
|
+
while (low < high) {
|
|
2078
|
+
const mid = Math.floor((low + high) / 2);
|
|
2079
|
+
if (array[mid] < value) {
|
|
2080
|
+
low = mid + 1;
|
|
2081
|
+
} else {
|
|
2082
|
+
high = mid;
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
if (low < array.length && array[low] === value) {
|
|
2086
|
+
return low;
|
|
2087
|
+
}
|
|
2088
|
+
return low - 1;
|
|
2089
|
+
};
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
function binary_search_rightmost(context) {
|
|
2093
|
+
return (id, value) => {
|
|
2094
|
+
const array = id.array;
|
|
2095
|
+
let low = 0;
|
|
2096
|
+
let high = array.length;
|
|
2097
|
+
while (low < high) {
|
|
2098
|
+
const mid = Math.floor((low + high) / 2);
|
|
2099
|
+
if (array[mid] <= value) {
|
|
2100
|
+
low = mid + 1;
|
|
2101
|
+
} else {
|
|
2102
|
+
high = mid;
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
if (low > 0 && array[low - 1] === value) {
|
|
2106
|
+
return low - 1;
|
|
2107
|
+
}
|
|
2108
|
+
return low;
|
|
2109
|
+
};
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
function clear$1(context) {
|
|
1972
2113
|
return (id) => {
|
|
1973
2114
|
id.array.length = 0;
|
|
1974
2115
|
};
|
|
1975
2116
|
}
|
|
1976
2117
|
|
|
1977
|
-
function concat(context) {
|
|
2118
|
+
function concat$1(context) {
|
|
1978
2119
|
return (id, other) => {
|
|
1979
2120
|
id.array.push(...other.array);
|
|
1980
2121
|
return id;
|
|
1981
2122
|
};
|
|
1982
2123
|
}
|
|
1983
2124
|
|
|
1984
|
-
function copy(context) {
|
|
2125
|
+
function copy$2(context) {
|
|
1985
2126
|
return (id) => {
|
|
1986
|
-
return new PineArrayObject([...id.array]);
|
|
2127
|
+
return new PineArrayObject([...id.array], id.type, context);
|
|
1987
2128
|
};
|
|
1988
2129
|
}
|
|
1989
2130
|
|
|
1990
2131
|
function covariance(context) {
|
|
1991
2132
|
return (arr1, arr2, biased = true) => {
|
|
1992
|
-
|
|
1993
|
-
const
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
let
|
|
1997
|
-
|
|
1998
|
-
|
|
2133
|
+
const a1 = arr1.array;
|
|
2134
|
+
const a2 = arr2.array;
|
|
2135
|
+
if (a1.length !== a2.length) return NaN;
|
|
2136
|
+
let sum1 = 0;
|
|
2137
|
+
let sum2 = 0;
|
|
2138
|
+
let count = 0;
|
|
2139
|
+
const validIndices = [];
|
|
2140
|
+
for (let i = 0; i < a1.length; i++) {
|
|
2141
|
+
const v1 = Number(a1[i]);
|
|
2142
|
+
const v2 = Number(a2[i]);
|
|
2143
|
+
if (!isNaN(v1) && v1 !== null && v1 !== void 0 && !isNaN(v2) && v2 !== null && v2 !== void 0) {
|
|
2144
|
+
sum1 += v1;
|
|
2145
|
+
sum2 += v2;
|
|
2146
|
+
count++;
|
|
2147
|
+
validIndices.push(i);
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
if (count === 0) return NaN;
|
|
2151
|
+
const mean1 = sum1 / count;
|
|
2152
|
+
const mean2 = sum2 / count;
|
|
2153
|
+
let sumProd = 0;
|
|
2154
|
+
for (const i of validIndices) {
|
|
2155
|
+
const v1 = Number(a1[i]);
|
|
2156
|
+
const v2 = Number(a2[i]);
|
|
2157
|
+
sumProd += (v1 - mean1) * (v2 - mean2);
|
|
1999
2158
|
}
|
|
2000
|
-
|
|
2159
|
+
const divisor = biased ? count : count - 1;
|
|
2160
|
+
if (divisor <= 0) return NaN;
|
|
2161
|
+
return context.precision(sumProd / divisor);
|
|
2001
2162
|
};
|
|
2002
2163
|
}
|
|
2003
2164
|
|
|
2004
2165
|
function every(context) {
|
|
2005
|
-
return (id
|
|
2006
|
-
return id.array.every(
|
|
2166
|
+
return (id) => {
|
|
2167
|
+
return id.array.every((value) => !isNaN(value) && value);
|
|
2007
2168
|
};
|
|
2008
2169
|
}
|
|
2009
2170
|
|
|
2010
|
-
function
|
|
2171
|
+
function inferArrayType(values) {
|
|
2172
|
+
if (values.every((value) => typeof value === "number")) {
|
|
2173
|
+
if (values.every((value) => (value | 0) === value)) {
|
|
2174
|
+
return PineArrayType.int;
|
|
2175
|
+
} else {
|
|
2176
|
+
return PineArrayType.float;
|
|
2177
|
+
}
|
|
2178
|
+
} else if (values.every((value) => typeof value === "string")) {
|
|
2179
|
+
return PineArrayType.string;
|
|
2180
|
+
} else if (values.every((value) => typeof value === "boolean")) {
|
|
2181
|
+
return PineArrayType.bool;
|
|
2182
|
+
} else {
|
|
2183
|
+
throw new Error("Cannot infer type from values");
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
function inferValueType(value) {
|
|
2187
|
+
if (typeof value === "number") {
|
|
2188
|
+
if ((value | 0) === value) {
|
|
2189
|
+
return PineArrayType.int;
|
|
2190
|
+
} else {
|
|
2191
|
+
return PineArrayType.float;
|
|
2192
|
+
}
|
|
2193
|
+
} else if (typeof value === "string") {
|
|
2194
|
+
return PineArrayType.string;
|
|
2195
|
+
} else if (typeof value === "boolean") {
|
|
2196
|
+
return PineArrayType.bool;
|
|
2197
|
+
} else {
|
|
2198
|
+
throw new Error("Cannot infer type from value");
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
function isValueOfType(value, type) {
|
|
2202
|
+
switch (type) {
|
|
2203
|
+
case PineArrayType.int:
|
|
2204
|
+
return typeof value === "number" && (value | 0) === value || isNaN(value);
|
|
2205
|
+
case PineArrayType.float:
|
|
2206
|
+
return typeof value === "number" || isNaN(value);
|
|
2207
|
+
case PineArrayType.string:
|
|
2208
|
+
return typeof value === "string";
|
|
2209
|
+
case PineArrayType.bool:
|
|
2210
|
+
return typeof value === "boolean";
|
|
2211
|
+
}
|
|
2212
|
+
return false;
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
function fill$1(context) {
|
|
2011
2216
|
return (id, value, start = 0, end) => {
|
|
2012
2217
|
const length = id.array.length;
|
|
2013
2218
|
const adjustedEnd = end !== void 0 ? Math.min(end, length) : length;
|
|
2014
2219
|
for (let i = start; i < adjustedEnd; i++) {
|
|
2015
|
-
id.
|
|
2220
|
+
if (!isValueOfType(value, id.type)) {
|
|
2221
|
+
throw new Error(
|
|
2222
|
+
`Cannot call 'array.fill' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
|
|
2223
|
+
);
|
|
2224
|
+
}
|
|
2225
|
+
id.array[i] = context.precision(value);
|
|
2016
2226
|
}
|
|
2017
2227
|
};
|
|
2018
2228
|
}
|
|
@@ -2023,13 +2233,7 @@ function first(context) {
|
|
|
2023
2233
|
};
|
|
2024
2234
|
}
|
|
2025
2235
|
|
|
2026
|
-
function
|
|
2027
|
-
return (...values) => {
|
|
2028
|
-
return new PineArrayObject([...values]);
|
|
2029
|
-
};
|
|
2030
|
-
}
|
|
2031
|
-
|
|
2032
|
-
function get(context) {
|
|
2236
|
+
function get$2(context) {
|
|
2033
2237
|
return (id, index) => {
|
|
2034
2238
|
return id.array[index];
|
|
2035
2239
|
};
|
|
@@ -2049,6 +2253,11 @@ function indexof(context) {
|
|
|
2049
2253
|
|
|
2050
2254
|
function insert(context) {
|
|
2051
2255
|
return (id, index, value) => {
|
|
2256
|
+
if (!isValueOfType(value, id.type)) {
|
|
2257
|
+
throw new Error(
|
|
2258
|
+
`Cannot call 'array.insert' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
|
|
2259
|
+
);
|
|
2260
|
+
}
|
|
2052
2261
|
id.array.splice(index, 0, value);
|
|
2053
2262
|
};
|
|
2054
2263
|
}
|
|
@@ -2071,84 +2280,136 @@ function lastindexof(context) {
|
|
|
2071
2280
|
};
|
|
2072
2281
|
}
|
|
2073
2282
|
|
|
2074
|
-
function max$
|
|
2283
|
+
function max$2(context) {
|
|
2075
2284
|
return (id, nth = 0) => {
|
|
2076
2285
|
const sorted = [...id.array].sort((a, b) => b - a);
|
|
2077
2286
|
return sorted[nth] ?? context.NA;
|
|
2078
2287
|
};
|
|
2079
2288
|
}
|
|
2080
2289
|
|
|
2081
|
-
function
|
|
2082
|
-
return (id
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
return (size, initial_value = false) => {
|
|
2096
|
-
return new PineArrayObject(Array(size).fill(initial_value));
|
|
2290
|
+
function median$2(context) {
|
|
2291
|
+
return (id) => {
|
|
2292
|
+
if (id.array.length === 0) return NaN;
|
|
2293
|
+
const sorted = [...id.array].sort((a, b) => {
|
|
2294
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
2295
|
+
return a - b;
|
|
2296
|
+
}
|
|
2297
|
+
return 0;
|
|
2298
|
+
});
|
|
2299
|
+
const mid = Math.floor(sorted.length / 2);
|
|
2300
|
+
if (sorted.length % 2 !== 0) {
|
|
2301
|
+
return sorted[mid];
|
|
2302
|
+
}
|
|
2303
|
+
return (sorted[mid - 1] + sorted[mid]) / 2;
|
|
2097
2304
|
};
|
|
2098
2305
|
}
|
|
2099
2306
|
|
|
2100
|
-
function
|
|
2101
|
-
return (
|
|
2102
|
-
|
|
2307
|
+
function min$2(context) {
|
|
2308
|
+
return (id, nth = 0) => {
|
|
2309
|
+
const sorted = [...id.array].sort((a, b) => a - b);
|
|
2310
|
+
return sorted[nth] ?? context.NA;
|
|
2103
2311
|
};
|
|
2104
2312
|
}
|
|
2105
2313
|
|
|
2106
|
-
function
|
|
2107
|
-
return (
|
|
2108
|
-
|
|
2314
|
+
function mode$2(context) {
|
|
2315
|
+
return (id) => {
|
|
2316
|
+
if (id.array.length === 0) return NaN;
|
|
2317
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2318
|
+
let maxFreq = 0;
|
|
2319
|
+
for (const val of id.array) {
|
|
2320
|
+
const count = (counts.get(val) || 0) + 1;
|
|
2321
|
+
counts.set(val, count);
|
|
2322
|
+
if (count > maxFreq) {
|
|
2323
|
+
maxFreq = count;
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
const modes = [];
|
|
2327
|
+
for (const [val, count] of counts) {
|
|
2328
|
+
if (count === maxFreq) {
|
|
2329
|
+
modes.push(val);
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
modes.sort((a, b) => {
|
|
2333
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
2334
|
+
return a - b;
|
|
2335
|
+
}
|
|
2336
|
+
if (typeof a === "string" && typeof b === "string") {
|
|
2337
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
2338
|
+
}
|
|
2339
|
+
return 0;
|
|
2340
|
+
});
|
|
2341
|
+
return modes[0];
|
|
2109
2342
|
};
|
|
2110
2343
|
}
|
|
2111
2344
|
|
|
2112
|
-
function
|
|
2113
|
-
return (
|
|
2114
|
-
|
|
2345
|
+
function percentile_linear_interpolation$1(context) {
|
|
2346
|
+
return (id, percentage) => {
|
|
2347
|
+
const array = id.array;
|
|
2348
|
+
if (array.length === 0) return NaN;
|
|
2349
|
+
const validValues = [];
|
|
2350
|
+
for (const item of array) {
|
|
2351
|
+
const val = Number(item);
|
|
2352
|
+
if (isNaN(val) || val === null || val === void 0) {
|
|
2353
|
+
return NaN;
|
|
2354
|
+
}
|
|
2355
|
+
validValues.push(val);
|
|
2356
|
+
}
|
|
2357
|
+
validValues.sort((a, b) => a - b);
|
|
2358
|
+
if (percentage < 0) percentage = 0;
|
|
2359
|
+
if (percentage > 100) percentage = 100;
|
|
2360
|
+
const k = percentage / 100 * validValues.length - 0.5;
|
|
2361
|
+
if (k <= 0) return context.precision(validValues[0]);
|
|
2362
|
+
if (k >= validValues.length - 1) return context.precision(validValues[validValues.length - 1]);
|
|
2363
|
+
const i = Math.floor(k);
|
|
2364
|
+
const f = k - i;
|
|
2365
|
+
return context.precision(validValues[i] * (1 - f) + validValues[i + 1] * f);
|
|
2115
2366
|
};
|
|
2116
2367
|
}
|
|
2117
2368
|
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2369
|
+
function percentile_nearest_rank$1(context) {
|
|
2370
|
+
return (id, percentage) => {
|
|
2371
|
+
const array = id.array;
|
|
2372
|
+
if (array.length === 0) return NaN;
|
|
2373
|
+
const validValues = [];
|
|
2374
|
+
for (const item of array) {
|
|
2375
|
+
const val = Number(item);
|
|
2376
|
+
if (!isNaN(val) && val !== null && val !== void 0) {
|
|
2377
|
+
validValues.push(val);
|
|
2378
|
+
}
|
|
2127
2379
|
}
|
|
2128
|
-
return
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2380
|
+
if (validValues.length === 0) return NaN;
|
|
2381
|
+
validValues.sort((a, b) => a - b);
|
|
2382
|
+
if (percentage < 0) percentage = 0;
|
|
2383
|
+
if (percentage > 100) percentage = 100;
|
|
2384
|
+
const totalCount = array.length;
|
|
2385
|
+
const rank = Math.ceil(percentage / 100 * totalCount);
|
|
2386
|
+
if (rank <= 0) {
|
|
2387
|
+
return validValues[0];
|
|
2134
2388
|
}
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
}
|
|
2139
|
-
toArray() {
|
|
2140
|
-
return this.data;
|
|
2141
|
-
}
|
|
2142
|
-
static from(source) {
|
|
2143
|
-
if (source instanceof Series) return source;
|
|
2144
|
-
if (Array.isArray(source)) return new Series(source);
|
|
2145
|
-
return new Series([source]);
|
|
2146
|
-
}
|
|
2389
|
+
if (rank > validValues.length) return NaN;
|
|
2390
|
+
return validValues[rank - 1];
|
|
2391
|
+
};
|
|
2147
2392
|
}
|
|
2148
2393
|
|
|
2149
|
-
function
|
|
2150
|
-
return (
|
|
2151
|
-
|
|
2394
|
+
function percentrank$1(context) {
|
|
2395
|
+
return (id, index) => {
|
|
2396
|
+
if (id.array.length === 0) return NaN;
|
|
2397
|
+
const idx = Math.floor(index);
|
|
2398
|
+
if (idx < 0 || idx >= id.array.length) return NaN;
|
|
2399
|
+
const value = Number(id.array[idx]);
|
|
2400
|
+
if (isNaN(value) || value === null || value === void 0) return NaN;
|
|
2401
|
+
let lessThan = 0;
|
|
2402
|
+
for (const item of id.array) {
|
|
2403
|
+
const val = Number(item);
|
|
2404
|
+
if (!isNaN(val) && val !== null && val !== void 0) {
|
|
2405
|
+
if (val < value) {
|
|
2406
|
+
lessThan++;
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
const divisor = id.array.length - 1;
|
|
2411
|
+
if (divisor <= 0) return NaN;
|
|
2412
|
+
return lessThan / divisor * 100;
|
|
2152
2413
|
};
|
|
2153
2414
|
}
|
|
2154
2415
|
|
|
@@ -2160,17 +2421,22 @@ function pop(context) {
|
|
|
2160
2421
|
|
|
2161
2422
|
function push(context) {
|
|
2162
2423
|
return (id, value) => {
|
|
2163
|
-
id.
|
|
2424
|
+
if (!isValueOfType(value, id.type)) {
|
|
2425
|
+
throw new Error(
|
|
2426
|
+
`Cannot call 'array.push' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
|
|
2427
|
+
);
|
|
2428
|
+
}
|
|
2429
|
+
id.array.push(context.precision(value));
|
|
2164
2430
|
};
|
|
2165
2431
|
}
|
|
2166
2432
|
|
|
2167
2433
|
function range$1(context) {
|
|
2168
2434
|
return (id) => {
|
|
2169
|
-
return context.array.max(id) - context.array.min(id);
|
|
2435
|
+
return context.precision(context.pine.array.max(id) - context.pine.array.min(id));
|
|
2170
2436
|
};
|
|
2171
2437
|
}
|
|
2172
2438
|
|
|
2173
|
-
function remove(context) {
|
|
2439
|
+
function remove$1(context) {
|
|
2174
2440
|
return (id, index) => {
|
|
2175
2441
|
if (index >= 0 && index < id.array.length) {
|
|
2176
2442
|
return id.array.splice(index, 1)[0];
|
|
@@ -2179,15 +2445,20 @@ function remove(context) {
|
|
|
2179
2445
|
};
|
|
2180
2446
|
}
|
|
2181
2447
|
|
|
2182
|
-
function reverse(context) {
|
|
2448
|
+
function reverse$1(context) {
|
|
2183
2449
|
return (id) => {
|
|
2184
2450
|
id.array.reverse();
|
|
2185
2451
|
};
|
|
2186
2452
|
}
|
|
2187
2453
|
|
|
2188
|
-
function set(context) {
|
|
2454
|
+
function set$1(context) {
|
|
2189
2455
|
return (id, index, value) => {
|
|
2190
|
-
id.
|
|
2456
|
+
if (!isValueOfType(value, id.type)) {
|
|
2457
|
+
throw new Error(
|
|
2458
|
+
`Cannot call 'array.set' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
|
|
2459
|
+
);
|
|
2460
|
+
}
|
|
2461
|
+
id.array[index] = context.precision(value);
|
|
2191
2462
|
};
|
|
2192
2463
|
}
|
|
2193
2464
|
|
|
@@ -2197,7 +2468,7 @@ function shift(context) {
|
|
|
2197
2468
|
};
|
|
2198
2469
|
}
|
|
2199
2470
|
|
|
2200
|
-
function size(context) {
|
|
2471
|
+
function size$1(context) {
|
|
2201
2472
|
return (id) => {
|
|
2202
2473
|
return id.array.length;
|
|
2203
2474
|
};
|
|
@@ -2205,32 +2476,116 @@ function size(context) {
|
|
|
2205
2476
|
|
|
2206
2477
|
function slice(context) {
|
|
2207
2478
|
return (id, start, end) => {
|
|
2208
|
-
const adjustedEnd = end !== void 0 ? end
|
|
2209
|
-
return new PineArrayObject(id.array.slice(start, adjustedEnd));
|
|
2479
|
+
const adjustedEnd = end !== void 0 ? end : void 0;
|
|
2480
|
+
return new PineArrayObject(id.array.slice(start, adjustedEnd), id.type, context);
|
|
2210
2481
|
};
|
|
2211
2482
|
}
|
|
2212
2483
|
|
|
2213
2484
|
function some(context) {
|
|
2214
|
-
return (id
|
|
2215
|
-
return id.array.some(
|
|
2216
|
-
};
|
|
2217
|
-
}
|
|
2485
|
+
return (id) => {
|
|
2486
|
+
return id.array.some((value) => !isNaN(value) && value);
|
|
2487
|
+
};
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
var order = /* @__PURE__ */ ((order2) => {
|
|
2491
|
+
order2[order2["ascending"] = 1] = "ascending";
|
|
2492
|
+
order2[order2["descending"] = 0] = "descending";
|
|
2493
|
+
return order2;
|
|
2494
|
+
})(order || {});
|
|
2495
|
+
var currency = /* @__PURE__ */ ((currency2) => {
|
|
2496
|
+
currency2["AED"] = "AED";
|
|
2497
|
+
currency2["ARS"] = "ARS";
|
|
2498
|
+
currency2["AUD"] = "AUD";
|
|
2499
|
+
currency2["BDT"] = "BDT";
|
|
2500
|
+
currency2["BHD"] = "BHD";
|
|
2501
|
+
currency2["BRL"] = "BRL";
|
|
2502
|
+
currency2["BTC"] = "BTC";
|
|
2503
|
+
currency2["CAD"] = "CAD";
|
|
2504
|
+
currency2["CHF"] = "CHF";
|
|
2505
|
+
currency2["CLP"] = "CLP";
|
|
2506
|
+
currency2["CNY"] = "CNY";
|
|
2507
|
+
currency2["COP"] = "COP";
|
|
2508
|
+
currency2["CZK"] = "CZK";
|
|
2509
|
+
currency2["DKK"] = "DKK";
|
|
2510
|
+
currency2["EGP"] = "EGP";
|
|
2511
|
+
currency2["ETH"] = "ETH";
|
|
2512
|
+
currency2["EUR"] = "EUR";
|
|
2513
|
+
currency2["GBP"] = "GBP";
|
|
2514
|
+
currency2["HKD"] = "HKD";
|
|
2515
|
+
currency2["HUF"] = "HUF";
|
|
2516
|
+
currency2["IDR"] = "IDR";
|
|
2517
|
+
currency2["ILS"] = "ILS";
|
|
2518
|
+
currency2["INR"] = "INR";
|
|
2519
|
+
currency2["ISK"] = "ISK";
|
|
2520
|
+
currency2["JPY"] = "JPY";
|
|
2521
|
+
currency2["KES"] = "KES";
|
|
2522
|
+
currency2["KRW"] = "KRW";
|
|
2523
|
+
currency2["KWD"] = "KWD";
|
|
2524
|
+
currency2["LKR"] = "LKR";
|
|
2525
|
+
currency2["MAD"] = "MAD";
|
|
2526
|
+
currency2["MXN"] = "MXN";
|
|
2527
|
+
currency2["MYR"] = "MYR";
|
|
2528
|
+
currency2["NGN"] = "NGN";
|
|
2529
|
+
currency2["NOK"] = "NOK";
|
|
2530
|
+
currency2["NONE"] = "NONE";
|
|
2531
|
+
currency2["NZD"] = "NZD";
|
|
2532
|
+
currency2["PEN"] = "PEN";
|
|
2533
|
+
currency2["PHP"] = "PHP";
|
|
2534
|
+
currency2["PKR"] = "PKR";
|
|
2535
|
+
currency2["PLN"] = "PLN";
|
|
2536
|
+
currency2["QAR"] = "QAR";
|
|
2537
|
+
currency2["RON"] = "RON";
|
|
2538
|
+
currency2["RSD"] = "RSD";
|
|
2539
|
+
currency2["RUB"] = "RUB";
|
|
2540
|
+
currency2["SAR"] = "SAR";
|
|
2541
|
+
currency2["SEK"] = "SEK";
|
|
2542
|
+
currency2["SGD"] = "SGD";
|
|
2543
|
+
currency2["THB"] = "THB";
|
|
2544
|
+
currency2["TND"] = "TND";
|
|
2545
|
+
currency2["TRY"] = "TRY";
|
|
2546
|
+
currency2["TWD"] = "TWD";
|
|
2547
|
+
currency2["USD"] = "USD";
|
|
2548
|
+
currency2["USDT"] = "USDT";
|
|
2549
|
+
currency2["VES"] = "VES";
|
|
2550
|
+
currency2["VND"] = "VND";
|
|
2551
|
+
currency2["ZAR"] = "ZAR";
|
|
2552
|
+
return currency2;
|
|
2553
|
+
})(currency || {});
|
|
2554
|
+
var dayofweek = /* @__PURE__ */ ((dayofweek2) => {
|
|
2555
|
+
dayofweek2[dayofweek2["sunday"] = 1] = "sunday";
|
|
2556
|
+
dayofweek2[dayofweek2["monday"] = 2] = "monday";
|
|
2557
|
+
dayofweek2[dayofweek2["tuesday"] = 3] = "tuesday";
|
|
2558
|
+
dayofweek2[dayofweek2["wednesday"] = 4] = "wednesday";
|
|
2559
|
+
dayofweek2[dayofweek2["thursday"] = 5] = "thursday";
|
|
2560
|
+
dayofweek2[dayofweek2["friday"] = 6] = "friday";
|
|
2561
|
+
dayofweek2[dayofweek2["saturday"] = 7] = "saturday";
|
|
2562
|
+
return dayofweek2;
|
|
2563
|
+
})(dayofweek || {});
|
|
2564
|
+
const types = {
|
|
2565
|
+
order,
|
|
2566
|
+
currency,
|
|
2567
|
+
dayofweek
|
|
2568
|
+
};
|
|
2218
2569
|
|
|
2219
|
-
function sort(context) {
|
|
2220
|
-
return (id,
|
|
2221
|
-
id.array.sort((a, b) =>
|
|
2570
|
+
function sort$1(context) {
|
|
2571
|
+
return (id, _order = order.ascending) => {
|
|
2572
|
+
id.array.sort((a, b) => {
|
|
2573
|
+
let _a = isNaN(a) ? Infinity : a;
|
|
2574
|
+
let _b = isNaN(b) ? Infinity : b;
|
|
2575
|
+
return _order === order.ascending ? _a - _b : _b - _a;
|
|
2576
|
+
});
|
|
2222
2577
|
};
|
|
2223
2578
|
}
|
|
2224
2579
|
|
|
2225
2580
|
function sort_indices(context) {
|
|
2226
|
-
return (id,
|
|
2581
|
+
return (id, _order = order.ascending) => {
|
|
2227
2582
|
const indices = id.array.map((_, index) => index);
|
|
2228
2583
|
indices.sort((a, b) => {
|
|
2229
|
-
const valA = id.array[a];
|
|
2230
|
-
const valB = id.array[b];
|
|
2231
|
-
return
|
|
2584
|
+
const valA = isNaN(id.array[a]) ? Infinity : id.array[a];
|
|
2585
|
+
const valB = isNaN(id.array[b]) ? Infinity : id.array[b];
|
|
2586
|
+
return _order === order.ascending ? valA - valB : valB - valA;
|
|
2232
2587
|
});
|
|
2233
|
-
return new PineArrayObject(indices);
|
|
2588
|
+
return new PineArrayObject(indices, PineArrayType.int, context);
|
|
2234
2589
|
};
|
|
2235
2590
|
}
|
|
2236
2591
|
|
|
@@ -2238,139 +2593,1896 @@ function standardize(context) {
|
|
|
2238
2593
|
return (id) => {
|
|
2239
2594
|
const mean = context.array.avg(id);
|
|
2240
2595
|
const stdev = context.array.stdev(id);
|
|
2596
|
+
if (isNaN(stdev)) {
|
|
2597
|
+
return new PineArrayObject(
|
|
2598
|
+
id.array.map(() => NaN),
|
|
2599
|
+
PineArrayType.int,
|
|
2600
|
+
context
|
|
2601
|
+
);
|
|
2602
|
+
}
|
|
2241
2603
|
if (stdev === 0) {
|
|
2242
|
-
return new PineArrayObject(
|
|
2604
|
+
return new PineArrayObject(
|
|
2605
|
+
id.array.map(() => 1),
|
|
2606
|
+
PineArrayType.int,
|
|
2607
|
+
context
|
|
2608
|
+
);
|
|
2243
2609
|
}
|
|
2244
|
-
return new PineArrayObject(
|
|
2610
|
+
return new PineArrayObject(
|
|
2611
|
+
id.array.map((x) => (x - mean) / stdev),
|
|
2612
|
+
PineArrayType.int,
|
|
2613
|
+
context
|
|
2614
|
+
);
|
|
2245
2615
|
};
|
|
2246
2616
|
}
|
|
2247
2617
|
|
|
2248
2618
|
function stdev$1(context) {
|
|
2249
2619
|
return (id, biased = true) => {
|
|
2250
|
-
const
|
|
2251
|
-
const
|
|
2252
|
-
|
|
2253
|
-
|
|
2620
|
+
const array = id.array;
|
|
2621
|
+
const n_total = array.length;
|
|
2622
|
+
if (n_total === 0) return NaN;
|
|
2623
|
+
let sum = 0;
|
|
2624
|
+
let sumSq = 0;
|
|
2625
|
+
let count = 0;
|
|
2626
|
+
for (let i = 0; i < n_total; i++) {
|
|
2627
|
+
const val = Number(array[i]);
|
|
2628
|
+
if (!isNaN(val) && val !== null && val !== void 0) {
|
|
2629
|
+
sum += val;
|
|
2630
|
+
sumSq += val * val;
|
|
2631
|
+
count++;
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
if (count === 0) return NaN;
|
|
2635
|
+
const mean = sum / count;
|
|
2636
|
+
const meanSq = sumSq / count;
|
|
2637
|
+
let variance = meanSq - mean * mean;
|
|
2638
|
+
if (variance < 0) return NaN;
|
|
2639
|
+
if (!biased && count > 1) {
|
|
2640
|
+
variance = variance * count / (count - 1);
|
|
2641
|
+
}
|
|
2642
|
+
if (!biased && count === 1) return 0;
|
|
2643
|
+
return context.precision(Math.sqrt(variance));
|
|
2644
|
+
};
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
function sum$2(context) {
|
|
2648
|
+
return (id) => {
|
|
2649
|
+
return context.precision(
|
|
2650
|
+
id.array.reduce((a, b) => {
|
|
2651
|
+
const val = Number(b);
|
|
2652
|
+
return isNaN(val) ? a : a + val;
|
|
2653
|
+
}, 0)
|
|
2654
|
+
);
|
|
2655
|
+
};
|
|
2656
|
+
}
|
|
2657
|
+
|
|
2658
|
+
function unshift(context) {
|
|
2659
|
+
return (id, value) => {
|
|
2660
|
+
if (!isValueOfType(value, id.type)) {
|
|
2661
|
+
throw new Error(
|
|
2662
|
+
`Cannot call 'array.unshift' with argument 'value'='${value}'. An argument of 'literal ${typeof value}' type was used but a '${id.type}' is expected.`
|
|
2663
|
+
);
|
|
2664
|
+
}
|
|
2665
|
+
id.array.unshift(context.precision(value));
|
|
2666
|
+
};
|
|
2667
|
+
}
|
|
2668
|
+
|
|
2669
|
+
function variance$1(context) {
|
|
2670
|
+
return (id, biased = true) => {
|
|
2671
|
+
let sum = 0;
|
|
2672
|
+
let count = 0;
|
|
2673
|
+
for (const item of id.array) {
|
|
2674
|
+
const val = Number(item);
|
|
2675
|
+
if (!isNaN(val) && val !== null && val !== void 0) {
|
|
2676
|
+
sum += val;
|
|
2677
|
+
count++;
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
if (count === 0) return NaN;
|
|
2681
|
+
const mean = sum / count;
|
|
2682
|
+
let sumSqDiff = 0;
|
|
2683
|
+
for (const item of id.array) {
|
|
2684
|
+
const val = Number(item);
|
|
2685
|
+
if (!isNaN(val) && val !== null && val !== void 0) {
|
|
2686
|
+
sumSqDiff += (val - mean) * (val - mean);
|
|
2687
|
+
}
|
|
2688
|
+
}
|
|
2689
|
+
const divisor = biased ? count : count - 1;
|
|
2690
|
+
if (divisor <= 0) return NaN;
|
|
2691
|
+
return context.precision(sumSqDiff / divisor);
|
|
2692
|
+
};
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
var __defProp$c = Object.defineProperty;
|
|
2696
|
+
var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2697
|
+
var __publicField$c = (obj, key, value) => __defNormalProp$c(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2698
|
+
var PineArrayType = /* @__PURE__ */ ((PineArrayType2) => {
|
|
2699
|
+
PineArrayType2["any"] = "";
|
|
2700
|
+
PineArrayType2["box"] = "box";
|
|
2701
|
+
PineArrayType2["bool"] = "bool";
|
|
2702
|
+
PineArrayType2["color"] = "color";
|
|
2703
|
+
PineArrayType2["float"] = "float";
|
|
2704
|
+
PineArrayType2["int"] = "int";
|
|
2705
|
+
PineArrayType2["label"] = "label";
|
|
2706
|
+
PineArrayType2["line"] = "line";
|
|
2707
|
+
PineArrayType2["linefill"] = "linefill";
|
|
2708
|
+
PineArrayType2["string"] = "string";
|
|
2709
|
+
PineArrayType2["table"] = "table";
|
|
2710
|
+
return PineArrayType2;
|
|
2711
|
+
})(PineArrayType || {});
|
|
2712
|
+
class PineArrayObject {
|
|
2713
|
+
constructor(array, type, context) {
|
|
2714
|
+
this.array = array;
|
|
2715
|
+
this.type = type;
|
|
2716
|
+
this.context = context;
|
|
2717
|
+
__publicField$c(this, "_abs");
|
|
2718
|
+
__publicField$c(this, "_avg");
|
|
2719
|
+
__publicField$c(this, "_binary_search");
|
|
2720
|
+
__publicField$c(this, "_binary_search_leftmost");
|
|
2721
|
+
__publicField$c(this, "_binary_search_rightmost");
|
|
2722
|
+
__publicField$c(this, "_clear");
|
|
2723
|
+
__publicField$c(this, "_concat");
|
|
2724
|
+
__publicField$c(this, "_copy");
|
|
2725
|
+
__publicField$c(this, "_covariance");
|
|
2726
|
+
__publicField$c(this, "_every");
|
|
2727
|
+
__publicField$c(this, "_fill");
|
|
2728
|
+
__publicField$c(this, "_first");
|
|
2729
|
+
__publicField$c(this, "_get");
|
|
2730
|
+
__publicField$c(this, "_includes");
|
|
2731
|
+
__publicField$c(this, "_indexof");
|
|
2732
|
+
__publicField$c(this, "_insert");
|
|
2733
|
+
__publicField$c(this, "_join");
|
|
2734
|
+
__publicField$c(this, "_last");
|
|
2735
|
+
__publicField$c(this, "_lastindexof");
|
|
2736
|
+
__publicField$c(this, "_max");
|
|
2737
|
+
__publicField$c(this, "_median");
|
|
2738
|
+
__publicField$c(this, "_min");
|
|
2739
|
+
__publicField$c(this, "_mode");
|
|
2740
|
+
__publicField$c(this, "_percentile_linear_interpolation");
|
|
2741
|
+
__publicField$c(this, "_percentile_nearest_rank");
|
|
2742
|
+
__publicField$c(this, "_percentrank");
|
|
2743
|
+
__publicField$c(this, "_pop");
|
|
2744
|
+
__publicField$c(this, "_push");
|
|
2745
|
+
__publicField$c(this, "_range");
|
|
2746
|
+
__publicField$c(this, "_remove");
|
|
2747
|
+
__publicField$c(this, "_reverse");
|
|
2748
|
+
__publicField$c(this, "_set");
|
|
2749
|
+
__publicField$c(this, "_shift");
|
|
2750
|
+
__publicField$c(this, "_size");
|
|
2751
|
+
__publicField$c(this, "_slice");
|
|
2752
|
+
__publicField$c(this, "_some");
|
|
2753
|
+
__publicField$c(this, "_sort");
|
|
2754
|
+
__publicField$c(this, "_sort_indices");
|
|
2755
|
+
__publicField$c(this, "_standardize");
|
|
2756
|
+
__publicField$c(this, "_stdev");
|
|
2757
|
+
__publicField$c(this, "_sum");
|
|
2758
|
+
__publicField$c(this, "_unshift");
|
|
2759
|
+
__publicField$c(this, "_variance");
|
|
2760
|
+
this._abs = abs$1(this.context);
|
|
2761
|
+
this._avg = avg$2(this.context);
|
|
2762
|
+
this._binary_search = binary_search(this.context);
|
|
2763
|
+
this._binary_search_leftmost = binary_search_leftmost(this.context);
|
|
2764
|
+
this._binary_search_rightmost = binary_search_rightmost(this.context);
|
|
2765
|
+
this._clear = clear$1(this.context);
|
|
2766
|
+
this._concat = concat$1(this.context);
|
|
2767
|
+
this._copy = copy$2(this.context);
|
|
2768
|
+
this._covariance = covariance(this.context);
|
|
2769
|
+
this._every = every(this.context);
|
|
2770
|
+
this._fill = fill$1(this.context);
|
|
2771
|
+
this._first = first(this.context);
|
|
2772
|
+
this._get = get$2(this.context);
|
|
2773
|
+
this._includes = includes(this.context);
|
|
2774
|
+
this._indexof = indexof(this.context);
|
|
2775
|
+
this._insert = insert(this.context);
|
|
2776
|
+
this._join = join(this.context);
|
|
2777
|
+
this._last = last(this.context);
|
|
2778
|
+
this._lastindexof = lastindexof(this.context);
|
|
2779
|
+
this._max = max$2(this.context);
|
|
2780
|
+
this._median = median$2(this.context);
|
|
2781
|
+
this._min = min$2(this.context);
|
|
2782
|
+
this._mode = mode$2(this.context);
|
|
2783
|
+
this._percentile_linear_interpolation = percentile_linear_interpolation$1(this.context);
|
|
2784
|
+
this._percentile_nearest_rank = percentile_nearest_rank$1(this.context);
|
|
2785
|
+
this._percentrank = percentrank$1(this.context);
|
|
2786
|
+
this._pop = pop(this.context);
|
|
2787
|
+
this._push = push(this.context);
|
|
2788
|
+
this._range = range$1(this.context);
|
|
2789
|
+
this._remove = remove$1(this.context);
|
|
2790
|
+
this._reverse = reverse$1(this.context);
|
|
2791
|
+
this._set = set$1(this.context);
|
|
2792
|
+
this._shift = shift(this.context);
|
|
2793
|
+
this._size = size$1(this.context);
|
|
2794
|
+
this._slice = slice(this.context);
|
|
2795
|
+
this._some = some(this.context);
|
|
2796
|
+
this._sort = sort$1(this.context);
|
|
2797
|
+
this._sort_indices = sort_indices(this.context);
|
|
2798
|
+
this._standardize = standardize(this.context);
|
|
2799
|
+
this._stdev = stdev$1(this.context);
|
|
2800
|
+
this._sum = sum$2(this.context);
|
|
2801
|
+
this._unshift = unshift(this.context);
|
|
2802
|
+
this._variance = variance$1(this.context);
|
|
2803
|
+
}
|
|
2804
|
+
toString() {
|
|
2805
|
+
return "[" + this.array.toString().replace(/,/g, ", ") + "]";
|
|
2806
|
+
}
|
|
2807
|
+
abs(...args) {
|
|
2808
|
+
return this._abs(this, ...args);
|
|
2809
|
+
}
|
|
2810
|
+
avg(...args) {
|
|
2811
|
+
return this._avg(this, ...args);
|
|
2812
|
+
}
|
|
2813
|
+
binary_search(...args) {
|
|
2814
|
+
return this._binary_search(this, ...args);
|
|
2815
|
+
}
|
|
2816
|
+
binary_search_leftmost(...args) {
|
|
2817
|
+
return this._binary_search_leftmost(this, ...args);
|
|
2818
|
+
}
|
|
2819
|
+
binary_search_rightmost(...args) {
|
|
2820
|
+
return this._binary_search_rightmost(this, ...args);
|
|
2821
|
+
}
|
|
2822
|
+
clear(...args) {
|
|
2823
|
+
return this._clear(this, ...args);
|
|
2824
|
+
}
|
|
2825
|
+
concat(...args) {
|
|
2826
|
+
return this._concat(this, ...args);
|
|
2827
|
+
}
|
|
2828
|
+
copy(...args) {
|
|
2829
|
+
return this._copy(this, ...args);
|
|
2830
|
+
}
|
|
2831
|
+
covariance(...args) {
|
|
2832
|
+
return this._covariance(this, ...args);
|
|
2833
|
+
}
|
|
2834
|
+
every(...args) {
|
|
2835
|
+
return this._every(this, ...args);
|
|
2836
|
+
}
|
|
2837
|
+
fill(...args) {
|
|
2838
|
+
return this._fill(this, ...args);
|
|
2839
|
+
}
|
|
2840
|
+
first(...args) {
|
|
2841
|
+
return this._first(this, ...args);
|
|
2842
|
+
}
|
|
2843
|
+
get(...args) {
|
|
2844
|
+
return this._get(this, ...args);
|
|
2845
|
+
}
|
|
2846
|
+
includes(...args) {
|
|
2847
|
+
return this._includes(this, ...args);
|
|
2848
|
+
}
|
|
2849
|
+
indexof(...args) {
|
|
2850
|
+
return this._indexof(this, ...args);
|
|
2851
|
+
}
|
|
2852
|
+
insert(...args) {
|
|
2853
|
+
return this._insert(this, ...args);
|
|
2854
|
+
}
|
|
2855
|
+
join(...args) {
|
|
2856
|
+
return this._join(this, ...args);
|
|
2857
|
+
}
|
|
2858
|
+
last(...args) {
|
|
2859
|
+
return this._last(this, ...args);
|
|
2860
|
+
}
|
|
2861
|
+
lastindexof(...args) {
|
|
2862
|
+
return this._lastindexof(this, ...args);
|
|
2863
|
+
}
|
|
2864
|
+
max(...args) {
|
|
2865
|
+
return this._max(this, ...args);
|
|
2866
|
+
}
|
|
2867
|
+
median(...args) {
|
|
2868
|
+
return this._median(this, ...args);
|
|
2869
|
+
}
|
|
2870
|
+
min(...args) {
|
|
2871
|
+
return this._min(this, ...args);
|
|
2872
|
+
}
|
|
2873
|
+
mode(...args) {
|
|
2874
|
+
return this._mode(this, ...args);
|
|
2875
|
+
}
|
|
2876
|
+
percentile_linear_interpolation(...args) {
|
|
2877
|
+
return this._percentile_linear_interpolation(this, ...args);
|
|
2878
|
+
}
|
|
2879
|
+
percentile_nearest_rank(...args) {
|
|
2880
|
+
return this._percentile_nearest_rank(this, ...args);
|
|
2881
|
+
}
|
|
2882
|
+
percentrank(...args) {
|
|
2883
|
+
return this._percentrank(this, ...args);
|
|
2884
|
+
}
|
|
2885
|
+
pop(...args) {
|
|
2886
|
+
return this._pop(this, ...args);
|
|
2887
|
+
}
|
|
2888
|
+
push(...args) {
|
|
2889
|
+
return this._push(this, ...args);
|
|
2890
|
+
}
|
|
2891
|
+
range(...args) {
|
|
2892
|
+
return this._range(this, ...args);
|
|
2893
|
+
}
|
|
2894
|
+
remove(...args) {
|
|
2895
|
+
return this._remove(this, ...args);
|
|
2896
|
+
}
|
|
2897
|
+
reverse(...args) {
|
|
2898
|
+
return this._reverse(this, ...args);
|
|
2899
|
+
}
|
|
2900
|
+
set(...args) {
|
|
2901
|
+
return this._set(this, ...args);
|
|
2902
|
+
}
|
|
2903
|
+
shift(...args) {
|
|
2904
|
+
return this._shift(this, ...args);
|
|
2905
|
+
}
|
|
2906
|
+
size(...args) {
|
|
2907
|
+
return this._size(this, ...args);
|
|
2908
|
+
}
|
|
2909
|
+
slice(...args) {
|
|
2910
|
+
return this._slice(this, ...args);
|
|
2911
|
+
}
|
|
2912
|
+
some(...args) {
|
|
2913
|
+
return this._some(this, ...args);
|
|
2914
|
+
}
|
|
2915
|
+
sort(...args) {
|
|
2916
|
+
return this._sort(this, ...args);
|
|
2917
|
+
}
|
|
2918
|
+
sort_indices(...args) {
|
|
2919
|
+
return this._sort_indices(this, ...args);
|
|
2920
|
+
}
|
|
2921
|
+
standardize(...args) {
|
|
2922
|
+
return this._standardize(this, ...args);
|
|
2923
|
+
}
|
|
2924
|
+
stdev(...args) {
|
|
2925
|
+
return this._stdev(this, ...args);
|
|
2926
|
+
}
|
|
2927
|
+
sum(...args) {
|
|
2928
|
+
return this._sum(this, ...args);
|
|
2929
|
+
}
|
|
2930
|
+
unshift(...args) {
|
|
2931
|
+
return this._unshift(this, ...args);
|
|
2932
|
+
}
|
|
2933
|
+
variance(...args) {
|
|
2934
|
+
return this._variance(this, ...args);
|
|
2935
|
+
}
|
|
2936
|
+
}
|
|
2937
|
+
|
|
2938
|
+
function from(context) {
|
|
2939
|
+
return (...values) => {
|
|
2940
|
+
return new PineArrayObject([...values], inferArrayType(values), context);
|
|
2941
|
+
};
|
|
2942
|
+
}
|
|
2943
|
+
|
|
2944
|
+
function new_fn$2(context) {
|
|
2945
|
+
return (size, initial_value) => {
|
|
2946
|
+
return new PineArrayObject(
|
|
2947
|
+
Array(size).fill(context.precision(initial_value || 0)),
|
|
2948
|
+
inferValueType(initial_value || 0),
|
|
2949
|
+
context
|
|
2950
|
+
);
|
|
2951
|
+
};
|
|
2952
|
+
}
|
|
2953
|
+
|
|
2954
|
+
function new_bool(context) {
|
|
2955
|
+
return (size, initial_value = false) => {
|
|
2956
|
+
return new PineArrayObject(Array(size).fill(initial_value), PineArrayType.bool, context);
|
|
2957
|
+
};
|
|
2958
|
+
}
|
|
2959
|
+
|
|
2960
|
+
function new_float(context) {
|
|
2961
|
+
return (size, initial_value = NaN) => {
|
|
2962
|
+
return new PineArrayObject(Array(size).fill(context.precision(initial_value)), PineArrayType.float, context);
|
|
2963
|
+
};
|
|
2964
|
+
}
|
|
2965
|
+
|
|
2966
|
+
function new_int(context) {
|
|
2967
|
+
return (size, initial_value = 0) => {
|
|
2968
|
+
return new PineArrayObject(Array(size).fill(context.precision(initial_value)), PineArrayType.int, context);
|
|
2969
|
+
};
|
|
2970
|
+
}
|
|
2971
|
+
|
|
2972
|
+
function new_string(context) {
|
|
2973
|
+
return (size, initial_value = "") => {
|
|
2974
|
+
return new PineArrayObject(Array(size).fill(initial_value), PineArrayType.string, context);
|
|
2975
|
+
};
|
|
2976
|
+
}
|
|
2977
|
+
|
|
2978
|
+
class Series {
|
|
2979
|
+
constructor(data, offset = 0) {
|
|
2980
|
+
this.data = data;
|
|
2981
|
+
this.offset = offset;
|
|
2982
|
+
}
|
|
2983
|
+
get(index) {
|
|
2984
|
+
const realIndex = this.data.length - 1 - (this.offset + index);
|
|
2985
|
+
if (realIndex < 0 || realIndex >= this.data.length) {
|
|
2986
|
+
return NaN;
|
|
2987
|
+
}
|
|
2988
|
+
return this.data[realIndex];
|
|
2989
|
+
}
|
|
2990
|
+
set(index, value) {
|
|
2991
|
+
const realIndex = this.data.length - 1 - (this.offset + index);
|
|
2992
|
+
if (realIndex >= 0 && realIndex < this.data.length) {
|
|
2993
|
+
this.data[realIndex] = value;
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
get length() {
|
|
2997
|
+
return this.data.length;
|
|
2998
|
+
}
|
|
2999
|
+
toArray() {
|
|
3000
|
+
return this.data;
|
|
3001
|
+
}
|
|
3002
|
+
static from(source) {
|
|
3003
|
+
if (source instanceof Series) return source;
|
|
3004
|
+
if (Array.isArray(source)) return new Series(source);
|
|
3005
|
+
return new Series([source]);
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
|
|
3009
|
+
function param$6(context) {
|
|
3010
|
+
return (source, index = 0) => {
|
|
3011
|
+
return Series.from(source).get(index);
|
|
3012
|
+
};
|
|
3013
|
+
}
|
|
3014
|
+
|
|
3015
|
+
class PineArray {
|
|
3016
|
+
constructor(context) {
|
|
3017
|
+
this.context = context;
|
|
3018
|
+
this.abs = (id, ...args) => id.abs(...args);
|
|
3019
|
+
this.avg = (id, ...args) => id.avg(...args);
|
|
3020
|
+
this.binary_search = (id, ...args) => id.binary_search(...args);
|
|
3021
|
+
this.binary_search_leftmost = (id, ...args) => id.binary_search_leftmost(...args);
|
|
3022
|
+
this.binary_search_rightmost = (id, ...args) => id.binary_search_rightmost(...args);
|
|
3023
|
+
this.clear = (id, ...args) => id.clear(...args);
|
|
3024
|
+
this.concat = (id, ...args) => id.concat(...args);
|
|
3025
|
+
this.copy = (id, ...args) => id.copy(...args);
|
|
3026
|
+
this.covariance = (id, ...args) => id.covariance(...args);
|
|
3027
|
+
this.every = (id, ...args) => id.every(...args);
|
|
3028
|
+
this.fill = (id, ...args) => id.fill(...args);
|
|
3029
|
+
this.first = (id, ...args) => id.first(...args);
|
|
3030
|
+
this.from = from(context);
|
|
3031
|
+
this.get = (id, ...args) => id.get(...args);
|
|
3032
|
+
this.includes = (id, ...args) => id.includes(...args);
|
|
3033
|
+
this.indexof = (id, ...args) => id.indexof(...args);
|
|
3034
|
+
this.insert = (id, ...args) => id.insert(...args);
|
|
3035
|
+
this.join = (id, ...args) => id.join(...args);
|
|
3036
|
+
this.last = (id, ...args) => id.last(...args);
|
|
3037
|
+
this.lastindexof = (id, ...args) => id.lastindexof(...args);
|
|
3038
|
+
this.max = (id, ...args) => id.max(...args);
|
|
3039
|
+
this.median = (id, ...args) => id.median(...args);
|
|
3040
|
+
this.min = (id, ...args) => id.min(...args);
|
|
3041
|
+
this.mode = (id, ...args) => id.mode(...args);
|
|
3042
|
+
this.new = new_fn$2(context);
|
|
3043
|
+
this.new_bool = new_bool(context);
|
|
3044
|
+
this.new_float = new_float(context);
|
|
3045
|
+
this.new_int = new_int(context);
|
|
3046
|
+
this.new_string = new_string(context);
|
|
3047
|
+
this.param = param$6();
|
|
3048
|
+
this.percentile_linear_interpolation = (id, ...args) => id.percentile_linear_interpolation(...args);
|
|
3049
|
+
this.percentile_nearest_rank = (id, ...args) => id.percentile_nearest_rank(...args);
|
|
3050
|
+
this.percentrank = (id, ...args) => id.percentrank(...args);
|
|
3051
|
+
this.pop = (id, ...args) => id.pop(...args);
|
|
3052
|
+
this.push = (id, ...args) => id.push(...args);
|
|
3053
|
+
this.range = (id, ...args) => id.range(...args);
|
|
3054
|
+
this.remove = (id, ...args) => id.remove(...args);
|
|
3055
|
+
this.reverse = (id, ...args) => id.reverse(...args);
|
|
3056
|
+
this.set = (id, ...args) => id.set(...args);
|
|
3057
|
+
this.shift = (id, ...args) => id.shift(...args);
|
|
3058
|
+
this.size = (id, ...args) => id.size(...args);
|
|
3059
|
+
this.slice = (id, ...args) => id.slice(...args);
|
|
3060
|
+
this.some = (id, ...args) => id.some(...args);
|
|
3061
|
+
this.sort = (id, ...args) => id.sort(...args);
|
|
3062
|
+
this.sort_indices = (id, ...args) => id.sort_indices(...args);
|
|
3063
|
+
this.standardize = (id, ...args) => id.standardize(...args);
|
|
3064
|
+
this.stdev = (id, ...args) => id.stdev(...args);
|
|
3065
|
+
this.sum = (id, ...args) => id.sum(...args);
|
|
3066
|
+
this.unshift = (id, ...args) => id.unshift(...args);
|
|
3067
|
+
this.variance = (id, ...args) => id.variance(...args);
|
|
3068
|
+
}
|
|
3069
|
+
}
|
|
3070
|
+
|
|
3071
|
+
function clear(context) {
|
|
3072
|
+
return (id) => {
|
|
3073
|
+
id.map.clear();
|
|
3074
|
+
};
|
|
3075
|
+
}
|
|
3076
|
+
|
|
3077
|
+
function contains(context) {
|
|
3078
|
+
return (id, key) => {
|
|
3079
|
+
return id.map.has(key);
|
|
3080
|
+
};
|
|
3081
|
+
}
|
|
3082
|
+
|
|
3083
|
+
function copy$1(context) {
|
|
3084
|
+
return (id) => {
|
|
3085
|
+
const newMap = new PineMapObject(id.keyType, id.valueType, context);
|
|
3086
|
+
newMap.map = new Map(id.map);
|
|
3087
|
+
return newMap;
|
|
3088
|
+
};
|
|
3089
|
+
}
|
|
3090
|
+
|
|
3091
|
+
function get$1(context) {
|
|
3092
|
+
return (id, key) => {
|
|
3093
|
+
const val = id.map.get(key);
|
|
3094
|
+
return val === void 0 ? NaN : val;
|
|
3095
|
+
};
|
|
3096
|
+
}
|
|
3097
|
+
|
|
3098
|
+
function keys(context) {
|
|
3099
|
+
return (id) => {
|
|
3100
|
+
const keysArray = Array.from(id.map.keys());
|
|
3101
|
+
return new PineArrayObject(keysArray, id.keyType, context);
|
|
3102
|
+
};
|
|
3103
|
+
}
|
|
3104
|
+
|
|
3105
|
+
function put(context) {
|
|
3106
|
+
return (id, key, value) => {
|
|
3107
|
+
const prev = id.map.get(key);
|
|
3108
|
+
id.map.set(key, value);
|
|
3109
|
+
return prev === void 0 ? NaN : prev;
|
|
3110
|
+
};
|
|
3111
|
+
}
|
|
3112
|
+
|
|
3113
|
+
function put_all(context) {
|
|
3114
|
+
return (id, id2) => {
|
|
3115
|
+
for (const [key, value] of id2.map) {
|
|
3116
|
+
id.map.set(key, value);
|
|
3117
|
+
}
|
|
3118
|
+
};
|
|
3119
|
+
}
|
|
3120
|
+
|
|
3121
|
+
function remove(context) {
|
|
3122
|
+
return (id, key) => {
|
|
3123
|
+
const val = id.map.get(key);
|
|
3124
|
+
const existed = id.map.delete(key);
|
|
3125
|
+
return existed ? val : NaN;
|
|
3126
|
+
};
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
function size(context) {
|
|
3130
|
+
return (id) => {
|
|
3131
|
+
return id.map.size;
|
|
3132
|
+
};
|
|
3133
|
+
}
|
|
3134
|
+
|
|
3135
|
+
function values(context) {
|
|
3136
|
+
return (id) => {
|
|
3137
|
+
const valuesArray = Array.from(id.map.values());
|
|
3138
|
+
return new PineArrayObject(valuesArray, id.valueType, context);
|
|
3139
|
+
};
|
|
3140
|
+
}
|
|
3141
|
+
|
|
3142
|
+
var __defProp$b = Object.defineProperty;
|
|
3143
|
+
var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3144
|
+
var __publicField$b = (obj, key, value) => __defNormalProp$b(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
3145
|
+
class PineMapObject {
|
|
3146
|
+
constructor(keyType, valueType, context) {
|
|
3147
|
+
this.keyType = keyType;
|
|
3148
|
+
this.valueType = valueType;
|
|
3149
|
+
this.context = context;
|
|
3150
|
+
__publicField$b(this, "map");
|
|
3151
|
+
__publicField$b(this, "_clear");
|
|
3152
|
+
__publicField$b(this, "_contains");
|
|
3153
|
+
__publicField$b(this, "_copy");
|
|
3154
|
+
__publicField$b(this, "_get");
|
|
3155
|
+
__publicField$b(this, "_keys");
|
|
3156
|
+
__publicField$b(this, "_put");
|
|
3157
|
+
__publicField$b(this, "_put_all");
|
|
3158
|
+
__publicField$b(this, "_remove");
|
|
3159
|
+
__publicField$b(this, "_size");
|
|
3160
|
+
__publicField$b(this, "_values");
|
|
3161
|
+
this.map = /* @__PURE__ */ new Map();
|
|
3162
|
+
this._clear = clear(this.context);
|
|
3163
|
+
this._contains = contains(this.context);
|
|
3164
|
+
this._copy = copy$1(this.context);
|
|
3165
|
+
this._get = get$1(this.context);
|
|
3166
|
+
this._keys = keys(this.context);
|
|
3167
|
+
this._put = put(this.context);
|
|
3168
|
+
this._put_all = put_all(this.context);
|
|
3169
|
+
this._remove = remove(this.context);
|
|
3170
|
+
this._size = size(this.context);
|
|
3171
|
+
this._values = values(this.context);
|
|
3172
|
+
}
|
|
3173
|
+
toString() {
|
|
3174
|
+
return `PineMapObject<${this.keyType}, ${this.valueType}>(${this.map.size})`;
|
|
3175
|
+
}
|
|
3176
|
+
clear(...args) {
|
|
3177
|
+
return this._clear(this, ...args);
|
|
3178
|
+
}
|
|
3179
|
+
contains(...args) {
|
|
3180
|
+
return this._contains(this, ...args);
|
|
3181
|
+
}
|
|
3182
|
+
copy(...args) {
|
|
3183
|
+
return this._copy(this, ...args);
|
|
3184
|
+
}
|
|
3185
|
+
get(...args) {
|
|
3186
|
+
return this._get(this, ...args);
|
|
3187
|
+
}
|
|
3188
|
+
keys(...args) {
|
|
3189
|
+
return this._keys(this, ...args);
|
|
3190
|
+
}
|
|
3191
|
+
put(...args) {
|
|
3192
|
+
return this._put(this, ...args);
|
|
3193
|
+
}
|
|
3194
|
+
put_all(...args) {
|
|
3195
|
+
return this._put_all(this, ...args);
|
|
3196
|
+
}
|
|
3197
|
+
remove(...args) {
|
|
3198
|
+
return this._remove(this, ...args);
|
|
3199
|
+
}
|
|
3200
|
+
size(...args) {
|
|
3201
|
+
return this._size(this, ...args);
|
|
3202
|
+
}
|
|
3203
|
+
values(...args) {
|
|
3204
|
+
return this._values(this, ...args);
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
|
|
3208
|
+
function new_fn$1(context) {
|
|
3209
|
+
return (keyType, valueType) => {
|
|
3210
|
+
return new PineMapObject(keyType, valueType, context);
|
|
3211
|
+
};
|
|
3212
|
+
}
|
|
3213
|
+
|
|
3214
|
+
function param$5(context) {
|
|
3215
|
+
return (source, index = 0) => {
|
|
3216
|
+
return Series.from(source).get(index);
|
|
3217
|
+
};
|
|
3218
|
+
}
|
|
3219
|
+
|
|
3220
|
+
class PineMap {
|
|
3221
|
+
constructor(context) {
|
|
3222
|
+
this.context = context;
|
|
3223
|
+
this.clear = (id, ...args) => id.clear(...args);
|
|
3224
|
+
this.contains = (id, ...args) => id.contains(...args);
|
|
3225
|
+
this.copy = (id, ...args) => id.copy(...args);
|
|
3226
|
+
this.get = (id, ...args) => id.get(...args);
|
|
3227
|
+
this.keys = (id, ...args) => id.keys(...args);
|
|
3228
|
+
this.new = new_fn$1(context);
|
|
3229
|
+
this.param = param$5();
|
|
3230
|
+
this.put = (id, ...args) => id.put(...args);
|
|
3231
|
+
this.put_all = (id, ...args) => id.put_all(...args);
|
|
3232
|
+
this.remove = (id, ...args) => id.remove(...args);
|
|
3233
|
+
this.size = (id, ...args) => id.size(...args);
|
|
3234
|
+
this.values = (id, ...args) => id.values(...args);
|
|
3235
|
+
}
|
|
3236
|
+
}
|
|
3237
|
+
|
|
3238
|
+
function add_col(context) {
|
|
3239
|
+
return (id, column_index, values) => {
|
|
3240
|
+
const rows = id.matrix.length;
|
|
3241
|
+
let colValues = [];
|
|
3242
|
+
if (values) {
|
|
3243
|
+
if (values instanceof PineArrayObject) {
|
|
3244
|
+
colValues = values.array;
|
|
3245
|
+
} else if (Array.isArray(values)) {
|
|
3246
|
+
colValues = values;
|
|
3247
|
+
} else {
|
|
3248
|
+
colValues = [values];
|
|
3249
|
+
}
|
|
3250
|
+
}
|
|
3251
|
+
if (rows === 0) {
|
|
3252
|
+
for (let i = 0; i < colValues.length; i++) {
|
|
3253
|
+
let val = colValues[i];
|
|
3254
|
+
if (val instanceof Series) val = val.get(0);
|
|
3255
|
+
id.matrix.push([val]);
|
|
3256
|
+
}
|
|
3257
|
+
return;
|
|
3258
|
+
}
|
|
3259
|
+
const cols = id.matrix[0].length;
|
|
3260
|
+
const index = column_index !== void 0 ? column_index : cols;
|
|
3261
|
+
for (let i = 0; i < rows; i++) {
|
|
3262
|
+
let val = i < colValues.length ? colValues[i] : NaN;
|
|
3263
|
+
if (val instanceof Series) val = val.get(0);
|
|
3264
|
+
id.matrix[i].splice(index, 0, val);
|
|
3265
|
+
}
|
|
3266
|
+
};
|
|
3267
|
+
}
|
|
3268
|
+
|
|
3269
|
+
function add_row(context) {
|
|
3270
|
+
return (id, row_index, values) => {
|
|
3271
|
+
const rows = id.matrix.length;
|
|
3272
|
+
let rowValues = [];
|
|
3273
|
+
if (values) {
|
|
3274
|
+
if (values instanceof PineArrayObject) {
|
|
3275
|
+
rowValues = values.array;
|
|
3276
|
+
} else if (Array.isArray(values)) {
|
|
3277
|
+
rowValues = values;
|
|
3278
|
+
} else {
|
|
3279
|
+
if (values instanceof Series) {
|
|
3280
|
+
const val = values.get(0);
|
|
3281
|
+
if (Array.isArray(val)) {
|
|
3282
|
+
rowValues = val;
|
|
3283
|
+
} else {
|
|
3284
|
+
rowValues = [val];
|
|
3285
|
+
}
|
|
3286
|
+
} else {
|
|
3287
|
+
rowValues = [values];
|
|
3288
|
+
}
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
3291
|
+
const cols = rows > 0 ? id.matrix[0].length : rowValues.length;
|
|
3292
|
+
const index = row_index !== void 0 ? row_index : rows;
|
|
3293
|
+
const newRow = [];
|
|
3294
|
+
for (let i = 0; i < cols; i++) {
|
|
3295
|
+
if (i < rowValues.length) {
|
|
3296
|
+
let val = rowValues[i];
|
|
3297
|
+
if (val instanceof Series) {
|
|
3298
|
+
val = val.get(0);
|
|
3299
|
+
}
|
|
3300
|
+
newRow.push(val);
|
|
3301
|
+
} else {
|
|
3302
|
+
newRow.push(NaN);
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
id.matrix.splice(index, 0, newRow);
|
|
3306
|
+
};
|
|
3307
|
+
}
|
|
3308
|
+
|
|
3309
|
+
function avg$1(context) {
|
|
3310
|
+
return (id) => {
|
|
3311
|
+
const rows = id.matrix.length;
|
|
3312
|
+
if (rows === 0) return NaN;
|
|
3313
|
+
const cols = id.matrix[0].length;
|
|
3314
|
+
if (cols === 0) return NaN;
|
|
3315
|
+
let sum = 0;
|
|
3316
|
+
let count = 0;
|
|
3317
|
+
for (let i = 0; i < rows; i++) {
|
|
3318
|
+
for (let j = 0; j < cols; j++) {
|
|
3319
|
+
const val = id.matrix[i][j];
|
|
3320
|
+
if (!isNaN(val)) {
|
|
3321
|
+
sum += val;
|
|
3322
|
+
count++;
|
|
3323
|
+
}
|
|
3324
|
+
}
|
|
3325
|
+
}
|
|
3326
|
+
if (count === 0) return NaN;
|
|
3327
|
+
return sum / count;
|
|
3328
|
+
};
|
|
3329
|
+
}
|
|
3330
|
+
|
|
3331
|
+
function col(context) {
|
|
3332
|
+
return (id, column) => {
|
|
3333
|
+
const rows = id.matrix.length;
|
|
3334
|
+
const result = [];
|
|
3335
|
+
for (let i = 0; i < rows; i++) {
|
|
3336
|
+
result.push(id.matrix[i][column]);
|
|
3337
|
+
}
|
|
3338
|
+
return new PineArrayObject(result, id.type, context);
|
|
3339
|
+
};
|
|
3340
|
+
}
|
|
3341
|
+
|
|
3342
|
+
function columns(context) {
|
|
3343
|
+
return (id) => {
|
|
3344
|
+
if (id.matrix.length === 0) return 0;
|
|
3345
|
+
return id.matrix[0].length;
|
|
3346
|
+
};
|
|
3347
|
+
}
|
|
3348
|
+
|
|
3349
|
+
function concat(context) {
|
|
3350
|
+
return (id, id2) => {
|
|
3351
|
+
const rows1 = id.matrix.length;
|
|
3352
|
+
const rows2 = id2.matrix.length;
|
|
3353
|
+
if (rows1 === 0) {
|
|
3354
|
+
for (let i = 0; i < rows2; i++) {
|
|
3355
|
+
id.matrix.push([...id2.matrix[i]]);
|
|
3356
|
+
}
|
|
3357
|
+
return;
|
|
3358
|
+
}
|
|
3359
|
+
const cols1 = id.matrix[0].length;
|
|
3360
|
+
const cols2 = rows2 > 0 ? id2.matrix[0].length : 0;
|
|
3361
|
+
if (cols1 !== cols2 && rows2 > 0) {
|
|
3362
|
+
console.error(`matrix.concat: Column count mismatch ${cols1} vs ${cols2}`);
|
|
3363
|
+
return;
|
|
3364
|
+
}
|
|
3365
|
+
for (let i = 0; i < rows2; i++) {
|
|
3366
|
+
id.matrix.push([...id2.matrix[i]]);
|
|
3367
|
+
}
|
|
3368
|
+
};
|
|
3369
|
+
}
|
|
3370
|
+
|
|
3371
|
+
function copy(context) {
|
|
3372
|
+
return (id) => {
|
|
3373
|
+
const rows = id.matrix.length;
|
|
3374
|
+
const cols = rows > 0 ? id.matrix[0].length : 0;
|
|
3375
|
+
const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3376
|
+
newMatrix.matrix = id.matrix.map((row) => [...row]);
|
|
3377
|
+
return newMatrix;
|
|
3378
|
+
};
|
|
3379
|
+
}
|
|
3380
|
+
|
|
3381
|
+
function determinant(matrix) {
|
|
3382
|
+
const n = matrix.length;
|
|
3383
|
+
if (n === 0) return 0;
|
|
3384
|
+
if (n === 1) return matrix[0][0];
|
|
3385
|
+
if (n === 2) return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
|
|
3386
|
+
const mat = matrix.map((row) => [...row]);
|
|
3387
|
+
let det2 = 1;
|
|
3388
|
+
for (let i = 0; i < n; i++) {
|
|
3389
|
+
let pivot = i;
|
|
3390
|
+
while (pivot < n && mat[pivot][i] === 0) pivot++;
|
|
3391
|
+
if (pivot === n) return 0;
|
|
3392
|
+
if (pivot !== i) {
|
|
3393
|
+
[mat[i], mat[pivot]] = [mat[pivot], mat[i]];
|
|
3394
|
+
det2 *= -1;
|
|
3395
|
+
}
|
|
3396
|
+
det2 *= mat[i][i];
|
|
3397
|
+
for (let j = i + 1; j < n; j++) {
|
|
3398
|
+
const factor = mat[j][i] / mat[i][i];
|
|
3399
|
+
for (let k = i; k < n; k++) {
|
|
3400
|
+
mat[j][k] -= factor * mat[i][k];
|
|
3401
|
+
}
|
|
3402
|
+
}
|
|
3403
|
+
}
|
|
3404
|
+
return det2;
|
|
3405
|
+
}
|
|
3406
|
+
function det(context) {
|
|
3407
|
+
return (id) => {
|
|
3408
|
+
const rows = id.matrix.length;
|
|
3409
|
+
const cols = rows > 0 ? id.matrix[0].length : 0;
|
|
3410
|
+
if (rows !== cols) return NaN;
|
|
3411
|
+
return determinant(id.matrix);
|
|
3412
|
+
};
|
|
3413
|
+
}
|
|
3414
|
+
|
|
3415
|
+
function diff(context) {
|
|
3416
|
+
return (id, id2) => {
|
|
3417
|
+
const rows = id.matrix.length;
|
|
3418
|
+
if (rows === 0) return new PineMatrixObject(id.type, 0, 0, NaN, context);
|
|
3419
|
+
const cols = id.matrix[0].length;
|
|
3420
|
+
const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3421
|
+
if (id2 instanceof PineMatrixObject) {
|
|
3422
|
+
for (let i = 0; i < rows; i++) {
|
|
3423
|
+
for (let j = 0; j < cols; j++) {
|
|
3424
|
+
const v1 = id.matrix[i][j];
|
|
3425
|
+
const v2 = id2.matrix[i] && id2.matrix[i][j] !== void 0 ? id2.matrix[i][j] : NaN;
|
|
3426
|
+
newMatrix.matrix[i][j] = v1 - v2;
|
|
3427
|
+
}
|
|
3428
|
+
}
|
|
3429
|
+
} else {
|
|
3430
|
+
const scalar = id2;
|
|
3431
|
+
for (let i = 0; i < rows; i++) {
|
|
3432
|
+
for (let j = 0; j < cols; j++) {
|
|
3433
|
+
newMatrix.matrix[i][j] = id.matrix[i][j] - scalar;
|
|
3434
|
+
}
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
return newMatrix;
|
|
3438
|
+
};
|
|
3439
|
+
}
|
|
3440
|
+
|
|
3441
|
+
function calculateEigenvalues(matrix) {
|
|
3442
|
+
const n = matrix.length;
|
|
3443
|
+
if (n !== 2) return Array(n).fill(NaN);
|
|
3444
|
+
const a = matrix[0][0];
|
|
3445
|
+
const b = matrix[0][1];
|
|
3446
|
+
const c = matrix[1][0];
|
|
3447
|
+
const d = matrix[1][1];
|
|
3448
|
+
const trace = a + d;
|
|
3449
|
+
const det = a * d - b * c;
|
|
3450
|
+
const delta = trace * trace - 4 * det;
|
|
3451
|
+
if (delta < 0) return [NaN, NaN];
|
|
3452
|
+
const l1 = (trace + Math.sqrt(delta)) / 2;
|
|
3453
|
+
const l2 = (trace - Math.sqrt(delta)) / 2;
|
|
3454
|
+
return [l1, l2];
|
|
3455
|
+
}
|
|
3456
|
+
function eigenvalues(context) {
|
|
3457
|
+
return (id) => {
|
|
3458
|
+
const rows = id.matrix.length;
|
|
3459
|
+
const cols = rows > 0 ? id.matrix[0].length : 0;
|
|
3460
|
+
if (rows !== cols) return new PineArrayObject([], id.type, context);
|
|
3461
|
+
const vals = calculateEigenvalues(id.matrix);
|
|
3462
|
+
return new PineArrayObject(vals, id.type, context);
|
|
3463
|
+
};
|
|
3464
|
+
}
|
|
3465
|
+
|
|
3466
|
+
function eigenvectors(context) {
|
|
3467
|
+
return (id) => {
|
|
3468
|
+
const rows = id.matrix.length;
|
|
3469
|
+
const cols = rows > 0 ? id.matrix[0].length : 0;
|
|
3470
|
+
if (rows !== cols) return new PineMatrixObject(id.type, 0, 0, NaN, context);
|
|
3471
|
+
const newMatrix = new PineMatrixObject(id.type, rows, cols, 0, context);
|
|
3472
|
+
for (let i = 0; i < rows; i++) newMatrix.matrix[i][i] = 1;
|
|
3473
|
+
return newMatrix;
|
|
3474
|
+
};
|
|
3475
|
+
}
|
|
3476
|
+
|
|
3477
|
+
function elements_count(context) {
|
|
3478
|
+
return (id) => {
|
|
3479
|
+
const rows = id.matrix.length;
|
|
3480
|
+
if (rows === 0) return 0;
|
|
3481
|
+
return rows * id.matrix[0].length;
|
|
3482
|
+
};
|
|
3483
|
+
}
|
|
3484
|
+
|
|
3485
|
+
function fill(context) {
|
|
3486
|
+
return (id, value, from_row, to_row, from_col, to_col) => {
|
|
3487
|
+
const rows = id.matrix.length;
|
|
3488
|
+
if (rows === 0) return;
|
|
3489
|
+
const cols = id.matrix[0].length;
|
|
3490
|
+
const r1 = from_row !== void 0 ? from_row : 0;
|
|
3491
|
+
const r2 = to_row !== void 0 ? to_row : rows;
|
|
3492
|
+
const c1 = from_col !== void 0 ? from_col : 0;
|
|
3493
|
+
const c2 = to_col !== void 0 ? to_col : cols;
|
|
3494
|
+
for (let i = r1; i < r2; i++) {
|
|
3495
|
+
if (i >= rows) break;
|
|
3496
|
+
for (let j = c1; j < c2; j++) {
|
|
3497
|
+
if (j >= cols) break;
|
|
3498
|
+
id.matrix[i][j] = value;
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
};
|
|
3502
|
+
}
|
|
3503
|
+
|
|
3504
|
+
function get(context) {
|
|
3505
|
+
return (id, row, col) => {
|
|
3506
|
+
if (!id.matrix[row]) return NaN;
|
|
3507
|
+
const val = id.matrix[row][col];
|
|
3508
|
+
return val === void 0 ? NaN : val;
|
|
3509
|
+
};
|
|
3510
|
+
}
|
|
3511
|
+
|
|
3512
|
+
function inverse(matrix) {
|
|
3513
|
+
const n = matrix.length;
|
|
3514
|
+
if (n !== 2) return matrix.map((r) => r.map(() => NaN));
|
|
3515
|
+
const det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
|
|
3516
|
+
if (det === 0)
|
|
3517
|
+
return [
|
|
3518
|
+
[NaN, NaN],
|
|
3519
|
+
[NaN, NaN]
|
|
3520
|
+
];
|
|
3521
|
+
return [
|
|
3522
|
+
[matrix[1][1] / det, -matrix[0][1] / det],
|
|
3523
|
+
[-matrix[1][0] / det, matrix[0][0] / det]
|
|
3524
|
+
];
|
|
3525
|
+
}
|
|
3526
|
+
function inv(context) {
|
|
3527
|
+
return (id) => {
|
|
3528
|
+
const rows = id.matrix.length;
|
|
3529
|
+
const cols = rows > 0 ? id.matrix[0].length : 0;
|
|
3530
|
+
if (rows !== cols) return new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3531
|
+
const invMat = inverse(id.matrix);
|
|
3532
|
+
const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3533
|
+
newMatrix.matrix = invMat;
|
|
3534
|
+
return newMatrix;
|
|
3535
|
+
};
|
|
3536
|
+
}
|
|
3537
|
+
|
|
3538
|
+
function is_antidiagonal(context) {
|
|
3539
|
+
return (id) => {
|
|
3540
|
+
const rows = id.matrix.length;
|
|
3541
|
+
if (rows === 0) return false;
|
|
3542
|
+
const cols = id.matrix[0].length;
|
|
3543
|
+
if (rows !== cols) return false;
|
|
3544
|
+
for (let i = 0; i < rows; i++) {
|
|
3545
|
+
for (let j = 0; j < cols; j++) {
|
|
3546
|
+
if (i + j !== rows - 1) {
|
|
3547
|
+
if (id.matrix[i][j] !== 0) return false;
|
|
3548
|
+
}
|
|
3549
|
+
}
|
|
3550
|
+
}
|
|
3551
|
+
return true;
|
|
3552
|
+
};
|
|
3553
|
+
}
|
|
3554
|
+
|
|
3555
|
+
function is_antisymmetric(context) {
|
|
3556
|
+
return (id) => {
|
|
3557
|
+
const rows = id.matrix.length;
|
|
3558
|
+
if (rows === 0) return false;
|
|
3559
|
+
const cols = id.matrix[0].length;
|
|
3560
|
+
if (rows !== cols) return false;
|
|
3561
|
+
for (let i = 0; i < rows; i++) {
|
|
3562
|
+
for (let j = 0; j < cols; j++) {
|
|
3563
|
+
if (id.matrix[j][i] !== -id.matrix[i][j]) return false;
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
return true;
|
|
3567
|
+
};
|
|
3568
|
+
}
|
|
3569
|
+
|
|
3570
|
+
function is_binary(context) {
|
|
3571
|
+
return (id) => {
|
|
3572
|
+
const rows = id.matrix.length;
|
|
3573
|
+
if (rows === 0) return false;
|
|
3574
|
+
const cols = id.matrix[0].length;
|
|
3575
|
+
for (let i = 0; i < rows; i++) {
|
|
3576
|
+
for (let j = 0; j < cols; j++) {
|
|
3577
|
+
const val = id.matrix[i][j];
|
|
3578
|
+
if (val !== 0 && val !== 1) return false;
|
|
3579
|
+
}
|
|
3580
|
+
}
|
|
3581
|
+
return true;
|
|
3582
|
+
};
|
|
3583
|
+
}
|
|
3584
|
+
|
|
3585
|
+
function is_diagonal(context) {
|
|
3586
|
+
return (id) => {
|
|
3587
|
+
const rows = id.matrix.length;
|
|
3588
|
+
if (rows === 0) return false;
|
|
3589
|
+
const cols = id.matrix[0].length;
|
|
3590
|
+
if (rows !== cols) return false;
|
|
3591
|
+
for (let i = 0; i < rows; i++) {
|
|
3592
|
+
for (let j = 0; j < cols; j++) {
|
|
3593
|
+
if (i !== j) {
|
|
3594
|
+
if (id.matrix[i][j] !== 0) return false;
|
|
3595
|
+
}
|
|
3596
|
+
}
|
|
3597
|
+
}
|
|
3598
|
+
return true;
|
|
3599
|
+
};
|
|
3600
|
+
}
|
|
3601
|
+
|
|
3602
|
+
function is_identity(context) {
|
|
3603
|
+
return (id) => {
|
|
3604
|
+
const rows = id.matrix.length;
|
|
3605
|
+
if (rows === 0) return false;
|
|
3606
|
+
const cols = id.matrix[0].length;
|
|
3607
|
+
if (rows !== cols) return false;
|
|
3608
|
+
for (let i = 0; i < rows; i++) {
|
|
3609
|
+
for (let j = 0; j < cols; j++) {
|
|
3610
|
+
if (i === j) {
|
|
3611
|
+
if (id.matrix[i][j] !== 1) return false;
|
|
3612
|
+
} else {
|
|
3613
|
+
if (id.matrix[i][j] !== 0) return false;
|
|
3614
|
+
}
|
|
3615
|
+
}
|
|
3616
|
+
}
|
|
3617
|
+
return true;
|
|
3618
|
+
};
|
|
3619
|
+
}
|
|
3620
|
+
|
|
3621
|
+
function is_square(context) {
|
|
3622
|
+
return (id) => {
|
|
3623
|
+
const rows = id.matrix.length;
|
|
3624
|
+
if (rows === 0) return false;
|
|
3625
|
+
const cols = id.matrix[0].length;
|
|
3626
|
+
return rows === cols;
|
|
3627
|
+
};
|
|
3628
|
+
}
|
|
3629
|
+
|
|
3630
|
+
function is_stochastic(context) {
|
|
3631
|
+
return (id) => {
|
|
3632
|
+
const rows = id.matrix.length;
|
|
3633
|
+
if (rows === 0) return false;
|
|
3634
|
+
const cols = id.matrix[0].length;
|
|
3635
|
+
for (let i = 0; i < rows; i++) {
|
|
3636
|
+
let sum = 0;
|
|
3637
|
+
for (let j = 0; j < cols; j++) {
|
|
3638
|
+
const val = id.matrix[i][j];
|
|
3639
|
+
if (val < 0) return false;
|
|
3640
|
+
sum += val;
|
|
3641
|
+
}
|
|
3642
|
+
if (Math.abs(sum - 1) > 1e-10) return false;
|
|
3643
|
+
}
|
|
3644
|
+
return true;
|
|
3645
|
+
};
|
|
3646
|
+
}
|
|
3647
|
+
|
|
3648
|
+
function is_symmetric(context) {
|
|
3649
|
+
return (id) => {
|
|
3650
|
+
const rows = id.matrix.length;
|
|
3651
|
+
if (rows === 0) return false;
|
|
3652
|
+
const cols = id.matrix[0].length;
|
|
3653
|
+
if (rows !== cols) return false;
|
|
3654
|
+
for (let i = 0; i < rows; i++) {
|
|
3655
|
+
for (let j = 0; j < i; j++) {
|
|
3656
|
+
if (id.matrix[i][j] !== id.matrix[j][i]) return false;
|
|
3657
|
+
}
|
|
3658
|
+
}
|
|
3659
|
+
return true;
|
|
3660
|
+
};
|
|
3661
|
+
}
|
|
3662
|
+
|
|
3663
|
+
function is_triangular(context) {
|
|
3664
|
+
return (id) => {
|
|
3665
|
+
const rows = id.matrix.length;
|
|
3666
|
+
if (rows === 0) return false;
|
|
3667
|
+
const cols = id.matrix[0].length;
|
|
3668
|
+
if (rows !== cols) return false;
|
|
3669
|
+
let isUpper = true;
|
|
3670
|
+
let isLower = true;
|
|
3671
|
+
for (let i = 0; i < rows; i++) {
|
|
3672
|
+
for (let j = 0; j < cols; j++) {
|
|
3673
|
+
if (i > j && id.matrix[i][j] !== 0) isUpper = false;
|
|
3674
|
+
if (i < j && id.matrix[i][j] !== 0) isLower = false;
|
|
3675
|
+
}
|
|
3676
|
+
}
|
|
3677
|
+
return isUpper || isLower;
|
|
3678
|
+
};
|
|
3679
|
+
}
|
|
3680
|
+
|
|
3681
|
+
function is_zero(context) {
|
|
3682
|
+
return (id) => {
|
|
3683
|
+
const rows = id.matrix.length;
|
|
3684
|
+
if (rows === 0) return true;
|
|
3685
|
+
const cols = id.matrix[0].length;
|
|
3686
|
+
for (let i = 0; i < rows; i++) {
|
|
3687
|
+
for (let j = 0; j < cols; j++) {
|
|
3688
|
+
if (id.matrix[i][j] !== 0) return false;
|
|
3689
|
+
}
|
|
3690
|
+
}
|
|
3691
|
+
return true;
|
|
3692
|
+
};
|
|
3693
|
+
}
|
|
3694
|
+
|
|
3695
|
+
function kron(context) {
|
|
3696
|
+
return (id, id2) => {
|
|
3697
|
+
const r1 = id.matrix.length;
|
|
3698
|
+
const c1 = r1 > 0 ? id.matrix[0].length : 0;
|
|
3699
|
+
const r2 = id2.matrix.length;
|
|
3700
|
+
const c2 = r2 > 0 ? id2.matrix[0].length : 0;
|
|
3701
|
+
const rows = r1 * r2;
|
|
3702
|
+
const cols = c1 * c2;
|
|
3703
|
+
const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3704
|
+
for (let i = 0; i < r1; i++) {
|
|
3705
|
+
for (let j = 0; j < c1; j++) {
|
|
3706
|
+
const val1 = id.matrix[i][j];
|
|
3707
|
+
for (let k = 0; k < r2; k++) {
|
|
3708
|
+
for (let l = 0; l < c2; l++) {
|
|
3709
|
+
const val2 = id2.matrix[k][l];
|
|
3710
|
+
newMatrix.matrix[i * r2 + k][j * c2 + l] = val1 * val2;
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
}
|
|
3714
|
+
}
|
|
3715
|
+
return newMatrix;
|
|
3716
|
+
};
|
|
3717
|
+
}
|
|
3718
|
+
|
|
3719
|
+
function max$1(context) {
|
|
3720
|
+
return (id) => {
|
|
3721
|
+
const rows = id.matrix.length;
|
|
3722
|
+
if (rows === 0) return NaN;
|
|
3723
|
+
const cols = id.matrix[0].length;
|
|
3724
|
+
let maxVal = -Infinity;
|
|
3725
|
+
let found = false;
|
|
3726
|
+
for (let i = 0; i < rows; i++) {
|
|
3727
|
+
for (let j = 0; j < cols; j++) {
|
|
3728
|
+
const val = id.matrix[i][j];
|
|
3729
|
+
if (!isNaN(val)) {
|
|
3730
|
+
if (val > maxVal) maxVal = val;
|
|
3731
|
+
found = true;
|
|
3732
|
+
}
|
|
3733
|
+
}
|
|
3734
|
+
}
|
|
3735
|
+
return found ? maxVal : NaN;
|
|
3736
|
+
};
|
|
3737
|
+
}
|
|
3738
|
+
|
|
3739
|
+
function median$1(context) {
|
|
3740
|
+
return (id) => {
|
|
3741
|
+
const rows = id.matrix.length;
|
|
3742
|
+
if (rows === 0) return NaN;
|
|
3743
|
+
const cols = id.matrix[0].length;
|
|
3744
|
+
const values = [];
|
|
3745
|
+
for (let i = 0; i < rows; i++) {
|
|
3746
|
+
for (let j = 0; j < cols; j++) {
|
|
3747
|
+
const val = id.matrix[i][j];
|
|
3748
|
+
if (!isNaN(val)) {
|
|
3749
|
+
values.push(val);
|
|
3750
|
+
}
|
|
3751
|
+
}
|
|
3752
|
+
}
|
|
3753
|
+
if (values.length === 0) return NaN;
|
|
3754
|
+
values.sort((a, b) => a - b);
|
|
3755
|
+
const mid = Math.floor(values.length / 2);
|
|
3756
|
+
if (values.length % 2 !== 0) {
|
|
3757
|
+
return values[mid];
|
|
3758
|
+
} else {
|
|
3759
|
+
return (values[mid - 1] + values[mid]) / 2;
|
|
3760
|
+
}
|
|
3761
|
+
};
|
|
3762
|
+
}
|
|
3763
|
+
|
|
3764
|
+
function min$1(context) {
|
|
3765
|
+
return (id) => {
|
|
3766
|
+
const rows = id.matrix.length;
|
|
3767
|
+
if (rows === 0) return NaN;
|
|
3768
|
+
const cols = id.matrix[0].length;
|
|
3769
|
+
let minVal = Infinity;
|
|
3770
|
+
let found = false;
|
|
3771
|
+
for (let i = 0; i < rows; i++) {
|
|
3772
|
+
for (let j = 0; j < cols; j++) {
|
|
3773
|
+
const val = id.matrix[i][j];
|
|
3774
|
+
if (!isNaN(val)) {
|
|
3775
|
+
if (val < minVal) minVal = val;
|
|
3776
|
+
found = true;
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
return found ? minVal : NaN;
|
|
3781
|
+
};
|
|
3782
|
+
}
|
|
3783
|
+
|
|
3784
|
+
function mode$1(context) {
|
|
3785
|
+
return (id) => {
|
|
3786
|
+
const rows = id.matrix.length;
|
|
3787
|
+
if (rows === 0) return NaN;
|
|
3788
|
+
const cols = id.matrix[0].length;
|
|
3789
|
+
const counts = /* @__PURE__ */ new Map();
|
|
3790
|
+
let maxCount = 0;
|
|
3791
|
+
let modeVal = NaN;
|
|
3792
|
+
for (let i = 0; i < rows; i++) {
|
|
3793
|
+
for (let j = 0; j < cols; j++) {
|
|
3794
|
+
const val = id.matrix[i][j];
|
|
3795
|
+
if (!isNaN(val)) {
|
|
3796
|
+
const count = (counts.get(val) || 0) + 1;
|
|
3797
|
+
counts.set(val, count);
|
|
3798
|
+
if (count > maxCount) {
|
|
3799
|
+
maxCount = count;
|
|
3800
|
+
modeVal = val;
|
|
3801
|
+
} else if (count === maxCount) {
|
|
3802
|
+
if (val < modeVal) modeVal = val;
|
|
3803
|
+
}
|
|
3804
|
+
}
|
|
3805
|
+
}
|
|
3806
|
+
}
|
|
3807
|
+
return modeVal;
|
|
3808
|
+
};
|
|
3809
|
+
}
|
|
3810
|
+
|
|
3811
|
+
function mult(context) {
|
|
3812
|
+
return (id, id2) => {
|
|
3813
|
+
const rows1 = id.matrix.length;
|
|
3814
|
+
const cols1 = rows1 > 0 ? id.matrix[0].length : 0;
|
|
3815
|
+
if (id2 instanceof PineMatrixObject) {
|
|
3816
|
+
const rows2 = id2.matrix.length;
|
|
3817
|
+
const cols2 = rows2 > 0 ? id2.matrix[0].length : 0;
|
|
3818
|
+
if (cols1 !== rows2) {
|
|
3819
|
+
return new PineMatrixObject(id.type, 0, 0, NaN, context);
|
|
3820
|
+
}
|
|
3821
|
+
const newMatrix = new PineMatrixObject(id.type, rows1, cols2, 0, context);
|
|
3822
|
+
for (let i = 0; i < rows1; i++) {
|
|
3823
|
+
for (let j = 0; j < cols2; j++) {
|
|
3824
|
+
let sum = 0;
|
|
3825
|
+
for (let k = 0; k < cols1; k++) {
|
|
3826
|
+
sum += id.matrix[i][k] * id2.matrix[k][j];
|
|
3827
|
+
}
|
|
3828
|
+
newMatrix.matrix[i][j] = sum;
|
|
3829
|
+
}
|
|
3830
|
+
}
|
|
3831
|
+
return newMatrix;
|
|
3832
|
+
} else if (id2 instanceof PineArrayObject || Array.isArray(id2.array || id2)) {
|
|
3833
|
+
const vec = id2.array || id2;
|
|
3834
|
+
if (cols1 !== vec.length) {
|
|
3835
|
+
return new PineMatrixObject(id.type, 0, 0, NaN, context);
|
|
3836
|
+
}
|
|
3837
|
+
const newMatrix = new PineMatrixObject(id.type, rows1, 1, 0, context);
|
|
3838
|
+
for (let i = 0; i < rows1; i++) {
|
|
3839
|
+
let sum = 0;
|
|
3840
|
+
for (let j = 0; j < cols1; j++) {
|
|
3841
|
+
sum += id.matrix[i][j] * vec[j];
|
|
3842
|
+
}
|
|
3843
|
+
newMatrix.matrix[i][0] = sum;
|
|
3844
|
+
}
|
|
3845
|
+
return newMatrix;
|
|
3846
|
+
} else {
|
|
3847
|
+
const scalar = id2;
|
|
3848
|
+
const newMatrix = new PineMatrixObject(id.type, rows1, cols1, NaN, context);
|
|
3849
|
+
for (let i = 0; i < rows1; i++) {
|
|
3850
|
+
for (let j = 0; j < cols1; j++) {
|
|
3851
|
+
newMatrix.matrix[i][j] = id.matrix[i][j] * scalar;
|
|
3852
|
+
}
|
|
3853
|
+
}
|
|
3854
|
+
return newMatrix;
|
|
3855
|
+
}
|
|
3856
|
+
};
|
|
3857
|
+
}
|
|
3858
|
+
|
|
3859
|
+
function pinv(context) {
|
|
3860
|
+
return (id) => {
|
|
3861
|
+
const rows = id.matrix.length;
|
|
3862
|
+
const cols = rows > 0 ? id.matrix[0].length : 0;
|
|
3863
|
+
if (rows === cols) {
|
|
3864
|
+
return new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3865
|
+
}
|
|
3866
|
+
return new PineMatrixObject(id.type, cols, rows, NaN, context);
|
|
3867
|
+
};
|
|
3868
|
+
}
|
|
3869
|
+
|
|
3870
|
+
function pow$1(context) {
|
|
3871
|
+
return (id, power) => {
|
|
3872
|
+
const rows = id.matrix.length;
|
|
3873
|
+
const cols = rows > 0 ? id.matrix[0].length : 0;
|
|
3874
|
+
if (rows !== cols) {
|
|
3875
|
+
return new PineMatrixObject(id.type, 0, 0, NaN, context);
|
|
3876
|
+
}
|
|
3877
|
+
let result = new PineMatrixObject(id.type, rows, cols, 0, context);
|
|
3878
|
+
for (let i = 0; i < rows; i++) result.matrix[i][i] = 1;
|
|
3879
|
+
let base = new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3880
|
+
for (let i = 0; i < rows; i++) base.matrix[i] = [...id.matrix[i]];
|
|
3881
|
+
let p = Math.floor(power);
|
|
3882
|
+
if (p < 0) return new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
3883
|
+
while (p > 0) {
|
|
3884
|
+
if (p % 2 === 1) {
|
|
3885
|
+
const temp = new PineMatrixObject(id.type, rows, cols, 0, context);
|
|
3886
|
+
for (let i = 0; i < rows; i++) {
|
|
3887
|
+
for (let j = 0; j < cols; j++) {
|
|
3888
|
+
let sum = 0;
|
|
3889
|
+
for (let k = 0; k < rows; k++) {
|
|
3890
|
+
sum += result.matrix[i][k] * base.matrix[k][j];
|
|
3891
|
+
}
|
|
3892
|
+
temp.matrix[i][j] = sum;
|
|
3893
|
+
}
|
|
3894
|
+
}
|
|
3895
|
+
result = temp;
|
|
3896
|
+
}
|
|
3897
|
+
const tempBase = new PineMatrixObject(id.type, rows, cols, 0, context);
|
|
3898
|
+
for (let i = 0; i < rows; i++) {
|
|
3899
|
+
for (let j = 0; j < cols; j++) {
|
|
3900
|
+
let sum = 0;
|
|
3901
|
+
for (let k = 0; k < rows; k++) {
|
|
3902
|
+
sum += base.matrix[i][k] * base.matrix[k][j];
|
|
3903
|
+
}
|
|
3904
|
+
tempBase.matrix[i][j] = sum;
|
|
3905
|
+
}
|
|
3906
|
+
}
|
|
3907
|
+
base = tempBase;
|
|
3908
|
+
p = Math.floor(p / 2);
|
|
3909
|
+
}
|
|
3910
|
+
return result;
|
|
3911
|
+
};
|
|
3912
|
+
}
|
|
3913
|
+
|
|
3914
|
+
function rank(context) {
|
|
3915
|
+
return (id) => {
|
|
3916
|
+
const rows = id.matrix.length;
|
|
3917
|
+
if (rows === 0) return 0;
|
|
3918
|
+
const cols = id.matrix[0].length;
|
|
3919
|
+
const mat = id.matrix.map((r2) => [...r2]);
|
|
3920
|
+
let r = 0;
|
|
3921
|
+
for (let c = 0; c < cols && r < rows; c++) {
|
|
3922
|
+
let pivot = r;
|
|
3923
|
+
while (pivot < rows && Math.abs(mat[pivot][c]) < 1e-10) pivot++;
|
|
3924
|
+
if (pivot < rows) {
|
|
3925
|
+
[mat[r], mat[pivot]] = [mat[pivot], mat[r]];
|
|
3926
|
+
const val = mat[r][c];
|
|
3927
|
+
for (let j = c; j < cols; j++) mat[r][j] /= val;
|
|
3928
|
+
for (let i = 0; i < rows; i++) {
|
|
3929
|
+
if (i !== r) {
|
|
3930
|
+
const factor = mat[i][c];
|
|
3931
|
+
for (let j = c; j < cols; j++) mat[i][j] -= factor * mat[r][j];
|
|
3932
|
+
}
|
|
3933
|
+
}
|
|
3934
|
+
r++;
|
|
3935
|
+
}
|
|
3936
|
+
}
|
|
3937
|
+
return r;
|
|
3938
|
+
};
|
|
3939
|
+
}
|
|
3940
|
+
|
|
3941
|
+
function remove_col(context) {
|
|
3942
|
+
return (id, column_index) => {
|
|
3943
|
+
const rows = id.matrix.length;
|
|
3944
|
+
if (rows === 0) return new PineArrayObject([], id.type, context);
|
|
3945
|
+
const removedValues = [];
|
|
3946
|
+
for (let i = 0; i < rows; i++) {
|
|
3947
|
+
const removed = id.matrix[i].splice(column_index, 1);
|
|
3948
|
+
removedValues.push(removed[0]);
|
|
3949
|
+
}
|
|
3950
|
+
return new PineArrayObject(removedValues, id.type, context);
|
|
3951
|
+
};
|
|
3952
|
+
}
|
|
3953
|
+
|
|
3954
|
+
function remove_row(context) {
|
|
3955
|
+
return (id, row_index) => {
|
|
3956
|
+
const removed = id.matrix.splice(row_index, 1);
|
|
3957
|
+
return new PineArrayObject(removed[0] || [], id.type, context);
|
|
3958
|
+
};
|
|
3959
|
+
}
|
|
3960
|
+
|
|
3961
|
+
function reshape(context) {
|
|
3962
|
+
return (id, rows, cols) => {
|
|
3963
|
+
const currentRows = id.matrix.length;
|
|
3964
|
+
const currentCols = currentRows > 0 ? id.matrix[0].length : 0;
|
|
3965
|
+
const newSize = rows * cols;
|
|
3966
|
+
const elements = [];
|
|
3967
|
+
for (let i = 0; i < currentRows; i++) {
|
|
3968
|
+
for (let j = 0; j < currentCols; j++) {
|
|
3969
|
+
elements.push(id.matrix[i][j]);
|
|
3970
|
+
}
|
|
3971
|
+
}
|
|
3972
|
+
while (elements.length < newSize) elements.push(NaN);
|
|
3973
|
+
if (elements.length > newSize) elements.length = newSize;
|
|
3974
|
+
const newMatrix = [];
|
|
3975
|
+
let k = 0;
|
|
3976
|
+
for (let i = 0; i < rows; i++) {
|
|
3977
|
+
const row = [];
|
|
3978
|
+
for (let j = 0; j < cols; j++) {
|
|
3979
|
+
row.push(elements[k++]);
|
|
3980
|
+
}
|
|
3981
|
+
newMatrix.push(row);
|
|
3982
|
+
}
|
|
3983
|
+
id.matrix = newMatrix;
|
|
3984
|
+
};
|
|
3985
|
+
}
|
|
3986
|
+
|
|
3987
|
+
function reverse(context) {
|
|
3988
|
+
return (id) => {
|
|
3989
|
+
id.matrix.reverse();
|
|
3990
|
+
for (const row of id.matrix) {
|
|
3991
|
+
row.reverse();
|
|
3992
|
+
}
|
|
3993
|
+
};
|
|
3994
|
+
}
|
|
3995
|
+
|
|
3996
|
+
function row(context) {
|
|
3997
|
+
return (id, row2) => {
|
|
3998
|
+
if (!id.matrix[row2]) return new PineArrayObject([], id.type, context);
|
|
3999
|
+
return new PineArrayObject([...id.matrix[row2]], id.type, context);
|
|
4000
|
+
};
|
|
4001
|
+
}
|
|
4002
|
+
|
|
4003
|
+
function rows(context) {
|
|
4004
|
+
return (id) => {
|
|
4005
|
+
return id.matrix.length;
|
|
4006
|
+
};
|
|
4007
|
+
}
|
|
4008
|
+
|
|
4009
|
+
function set(context) {
|
|
4010
|
+
return (id, row, col, value) => {
|
|
4011
|
+
if (!id.matrix[row]) return;
|
|
4012
|
+
id.matrix[row][col] = value;
|
|
4013
|
+
};
|
|
4014
|
+
}
|
|
4015
|
+
|
|
4016
|
+
function sort(context) {
|
|
4017
|
+
return (id, column = 0, order = "asc") => {
|
|
4018
|
+
const rows = id.matrix.length;
|
|
4019
|
+
if (rows === 0) return;
|
|
4020
|
+
id.matrix.sort((a, b) => {
|
|
4021
|
+
const valA = a[column];
|
|
4022
|
+
const valB = b[column];
|
|
4023
|
+
if (valA < valB) return order === "asc" ? -1 : 1;
|
|
4024
|
+
if (valA > valB) return order === "asc" ? 1 : -1;
|
|
4025
|
+
return 0;
|
|
4026
|
+
});
|
|
4027
|
+
};
|
|
4028
|
+
}
|
|
4029
|
+
|
|
4030
|
+
function submatrix(context) {
|
|
4031
|
+
return (id, from_row, to_row, from_col, to_col) => {
|
|
4032
|
+
const rows = to_row - from_row;
|
|
4033
|
+
const cols = to_col - from_col;
|
|
4034
|
+
const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
4035
|
+
for (let i = 0; i < rows; i++) {
|
|
4036
|
+
const sourceRow = from_row + i;
|
|
4037
|
+
if (sourceRow >= id.matrix.length) break;
|
|
4038
|
+
for (let j = 0; j < cols; j++) {
|
|
4039
|
+
const sourceCol = from_col + j;
|
|
4040
|
+
if (sourceCol >= id.matrix[sourceRow].length) break;
|
|
4041
|
+
newMatrix.matrix[i][j] = id.matrix[sourceRow][sourceCol];
|
|
4042
|
+
}
|
|
4043
|
+
}
|
|
4044
|
+
return newMatrix;
|
|
4045
|
+
};
|
|
4046
|
+
}
|
|
4047
|
+
|
|
4048
|
+
function sum$1(context) {
|
|
4049
|
+
return (id, id2) => {
|
|
4050
|
+
const rows = id.matrix.length;
|
|
4051
|
+
if (rows === 0) return new PineMatrixObject(id.type, 0, 0, NaN, context);
|
|
4052
|
+
const cols = id.matrix[0].length;
|
|
4053
|
+
const newMatrix = new PineMatrixObject(id.type, rows, cols, NaN, context);
|
|
4054
|
+
if (id2 instanceof PineMatrixObject) {
|
|
4055
|
+
for (let i = 0; i < rows; i++) {
|
|
4056
|
+
for (let j = 0; j < cols; j++) {
|
|
4057
|
+
const v1 = id.matrix[i][j];
|
|
4058
|
+
const v2 = id2.matrix[i] && id2.matrix[i][j] !== void 0 ? id2.matrix[i][j] : NaN;
|
|
4059
|
+
newMatrix.matrix[i][j] = v1 + v2;
|
|
4060
|
+
}
|
|
4061
|
+
}
|
|
4062
|
+
} else {
|
|
4063
|
+
const scalar = id2;
|
|
4064
|
+
for (let i = 0; i < rows; i++) {
|
|
4065
|
+
for (let j = 0; j < cols; j++) {
|
|
4066
|
+
newMatrix.matrix[i][j] = id.matrix[i][j] + scalar;
|
|
4067
|
+
}
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
return newMatrix;
|
|
4071
|
+
};
|
|
4072
|
+
}
|
|
4073
|
+
|
|
4074
|
+
function swap_columns(context) {
|
|
4075
|
+
return (id, col1, col2) => {
|
|
4076
|
+
const rows = id.matrix.length;
|
|
4077
|
+
for (let i = 0; i < rows; i++) {
|
|
4078
|
+
const temp = id.matrix[i][col1];
|
|
4079
|
+
id.matrix[i][col1] = id.matrix[i][col2];
|
|
4080
|
+
id.matrix[i][col2] = temp;
|
|
4081
|
+
}
|
|
4082
|
+
};
|
|
4083
|
+
}
|
|
4084
|
+
|
|
4085
|
+
function swap_rows(context) {
|
|
4086
|
+
return (id, row1, row2) => {
|
|
4087
|
+
const temp = id.matrix[row1];
|
|
4088
|
+
id.matrix[row1] = id.matrix[row2];
|
|
4089
|
+
id.matrix[row2] = temp;
|
|
4090
|
+
};
|
|
4091
|
+
}
|
|
4092
|
+
|
|
4093
|
+
function trace(context) {
|
|
4094
|
+
return (id) => {
|
|
4095
|
+
const rows = id.matrix.length;
|
|
4096
|
+
if (rows === 0) return 0;
|
|
4097
|
+
const cols = id.matrix[0].length;
|
|
4098
|
+
const n = Math.min(rows, cols);
|
|
4099
|
+
let sum = 0;
|
|
4100
|
+
for (let i = 0; i < n; i++) {
|
|
4101
|
+
sum += id.matrix[i][i];
|
|
4102
|
+
}
|
|
4103
|
+
return sum;
|
|
4104
|
+
};
|
|
4105
|
+
}
|
|
4106
|
+
|
|
4107
|
+
function transpose(context) {
|
|
4108
|
+
return (id) => {
|
|
4109
|
+
const rows = id.matrix.length;
|
|
4110
|
+
if (rows === 0) return new PineMatrixObject(id.type, 0, 0, NaN, context);
|
|
4111
|
+
const cols = id.matrix[0].length;
|
|
4112
|
+
const newMatrix = new PineMatrixObject(id.type, cols, rows, NaN, context);
|
|
4113
|
+
for (let i = 0; i < rows; i++) {
|
|
4114
|
+
for (let j = 0; j < cols; j++) {
|
|
4115
|
+
newMatrix.matrix[j][i] = id.matrix[i][j];
|
|
4116
|
+
}
|
|
4117
|
+
}
|
|
4118
|
+
return newMatrix;
|
|
2254
4119
|
};
|
|
2255
4120
|
}
|
|
2256
4121
|
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
4122
|
+
var __defProp$a = Object.defineProperty;
|
|
4123
|
+
var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4124
|
+
var __publicField$a = (obj, key, value) => __defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4125
|
+
class PineMatrixObject {
|
|
4126
|
+
constructor(type, rows$1 = 0, cols = 0, initialValue = NaN, context) {
|
|
4127
|
+
this.type = type;
|
|
4128
|
+
this.context = context;
|
|
4129
|
+
__publicField$a(this, "matrix");
|
|
4130
|
+
__publicField$a(this, "_add_col");
|
|
4131
|
+
__publicField$a(this, "_add_row");
|
|
4132
|
+
__publicField$a(this, "_avg");
|
|
4133
|
+
__publicField$a(this, "_col");
|
|
4134
|
+
__publicField$a(this, "_columns");
|
|
4135
|
+
__publicField$a(this, "_concat");
|
|
4136
|
+
__publicField$a(this, "_copy");
|
|
4137
|
+
__publicField$a(this, "_det");
|
|
4138
|
+
__publicField$a(this, "_diff");
|
|
4139
|
+
__publicField$a(this, "_eigenvalues");
|
|
4140
|
+
__publicField$a(this, "_eigenvectors");
|
|
4141
|
+
__publicField$a(this, "_elements_count");
|
|
4142
|
+
__publicField$a(this, "_fill");
|
|
4143
|
+
__publicField$a(this, "_get");
|
|
4144
|
+
__publicField$a(this, "_inv");
|
|
4145
|
+
__publicField$a(this, "_is_antidiagonal");
|
|
4146
|
+
__publicField$a(this, "_is_antisymmetric");
|
|
4147
|
+
__publicField$a(this, "_is_binary");
|
|
4148
|
+
__publicField$a(this, "_is_diagonal");
|
|
4149
|
+
__publicField$a(this, "_is_identity");
|
|
4150
|
+
__publicField$a(this, "_is_square");
|
|
4151
|
+
__publicField$a(this, "_is_stochastic");
|
|
4152
|
+
__publicField$a(this, "_is_symmetric");
|
|
4153
|
+
__publicField$a(this, "_is_triangular");
|
|
4154
|
+
__publicField$a(this, "_is_zero");
|
|
4155
|
+
__publicField$a(this, "_kron");
|
|
4156
|
+
__publicField$a(this, "_max");
|
|
4157
|
+
__publicField$a(this, "_median");
|
|
4158
|
+
__publicField$a(this, "_min");
|
|
4159
|
+
__publicField$a(this, "_mode");
|
|
4160
|
+
__publicField$a(this, "_mult");
|
|
4161
|
+
__publicField$a(this, "_pinv");
|
|
4162
|
+
__publicField$a(this, "_pow");
|
|
4163
|
+
__publicField$a(this, "_rank");
|
|
4164
|
+
__publicField$a(this, "_remove_col");
|
|
4165
|
+
__publicField$a(this, "_remove_row");
|
|
4166
|
+
__publicField$a(this, "_reshape");
|
|
4167
|
+
__publicField$a(this, "_reverse");
|
|
4168
|
+
__publicField$a(this, "_row");
|
|
4169
|
+
__publicField$a(this, "_rows");
|
|
4170
|
+
__publicField$a(this, "_set");
|
|
4171
|
+
__publicField$a(this, "_sort");
|
|
4172
|
+
__publicField$a(this, "_submatrix");
|
|
4173
|
+
__publicField$a(this, "_sum");
|
|
4174
|
+
__publicField$a(this, "_swap_columns");
|
|
4175
|
+
__publicField$a(this, "_swap_rows");
|
|
4176
|
+
__publicField$a(this, "_trace");
|
|
4177
|
+
__publicField$a(this, "_transpose");
|
|
4178
|
+
this.matrix = [];
|
|
4179
|
+
if (rows$1 > 0 && cols > 0) {
|
|
4180
|
+
for (let i = 0; i < rows$1; i++) {
|
|
4181
|
+
this.matrix.push(Array(cols).fill(initialValue));
|
|
4182
|
+
}
|
|
4183
|
+
}
|
|
4184
|
+
this._add_col = add_col(this.context);
|
|
4185
|
+
this._add_row = add_row(this.context);
|
|
4186
|
+
this._avg = avg$1(this.context);
|
|
4187
|
+
this._col = col(this.context);
|
|
4188
|
+
this._columns = columns(this.context);
|
|
4189
|
+
this._concat = concat(this.context);
|
|
4190
|
+
this._copy = copy(this.context);
|
|
4191
|
+
this._det = det(this.context);
|
|
4192
|
+
this._diff = diff(this.context);
|
|
4193
|
+
this._eigenvalues = eigenvalues(this.context);
|
|
4194
|
+
this._eigenvectors = eigenvectors(this.context);
|
|
4195
|
+
this._elements_count = elements_count(this.context);
|
|
4196
|
+
this._fill = fill(this.context);
|
|
4197
|
+
this._get = get(this.context);
|
|
4198
|
+
this._inv = inv(this.context);
|
|
4199
|
+
this._is_antidiagonal = is_antidiagonal(this.context);
|
|
4200
|
+
this._is_antisymmetric = is_antisymmetric(this.context);
|
|
4201
|
+
this._is_binary = is_binary(this.context);
|
|
4202
|
+
this._is_diagonal = is_diagonal(this.context);
|
|
4203
|
+
this._is_identity = is_identity(this.context);
|
|
4204
|
+
this._is_square = is_square(this.context);
|
|
4205
|
+
this._is_stochastic = is_stochastic(this.context);
|
|
4206
|
+
this._is_symmetric = is_symmetric(this.context);
|
|
4207
|
+
this._is_triangular = is_triangular(this.context);
|
|
4208
|
+
this._is_zero = is_zero(this.context);
|
|
4209
|
+
this._kron = kron(this.context);
|
|
4210
|
+
this._max = max$1(this.context);
|
|
4211
|
+
this._median = median$1(this.context);
|
|
4212
|
+
this._min = min$1(this.context);
|
|
4213
|
+
this._mode = mode$1(this.context);
|
|
4214
|
+
this._mult = mult(this.context);
|
|
4215
|
+
this._pinv = pinv(this.context);
|
|
4216
|
+
this._pow = pow$1(this.context);
|
|
4217
|
+
this._rank = rank(this.context);
|
|
4218
|
+
this._remove_col = remove_col(this.context);
|
|
4219
|
+
this._remove_row = remove_row(this.context);
|
|
4220
|
+
this._reshape = reshape(this.context);
|
|
4221
|
+
this._reverse = reverse(this.context);
|
|
4222
|
+
this._row = row(this.context);
|
|
4223
|
+
this._rows = rows(this.context);
|
|
4224
|
+
this._set = set(this.context);
|
|
4225
|
+
this._sort = sort(this.context);
|
|
4226
|
+
this._submatrix = submatrix(this.context);
|
|
4227
|
+
this._sum = sum$1(this.context);
|
|
4228
|
+
this._swap_columns = swap_columns(this.context);
|
|
4229
|
+
this._swap_rows = swap_rows(this.context);
|
|
4230
|
+
this._trace = trace(this.context);
|
|
4231
|
+
this._transpose = transpose(this.context);
|
|
4232
|
+
}
|
|
4233
|
+
toString() {
|
|
4234
|
+
let result = "";
|
|
4235
|
+
for (let i = 0; i < this.matrix.length; i++) {
|
|
4236
|
+
result += result === "" ? "" : "\n";
|
|
4237
|
+
result += "[" + this.matrix[i].join(", ") + "]";
|
|
4238
|
+
}
|
|
4239
|
+
return result;
|
|
4240
|
+
}
|
|
4241
|
+
add_col(...args) {
|
|
4242
|
+
return this._add_col(this, ...args);
|
|
4243
|
+
}
|
|
4244
|
+
add_row(...args) {
|
|
4245
|
+
return this._add_row(this, ...args);
|
|
4246
|
+
}
|
|
4247
|
+
avg(...args) {
|
|
4248
|
+
return this._avg(this, ...args);
|
|
4249
|
+
}
|
|
4250
|
+
col(...args) {
|
|
4251
|
+
return this._col(this, ...args);
|
|
4252
|
+
}
|
|
4253
|
+
columns(...args) {
|
|
4254
|
+
return this._columns(this, ...args);
|
|
4255
|
+
}
|
|
4256
|
+
concat(...args) {
|
|
4257
|
+
return this._concat(this, ...args);
|
|
4258
|
+
}
|
|
4259
|
+
copy(...args) {
|
|
4260
|
+
return this._copy(this, ...args);
|
|
4261
|
+
}
|
|
4262
|
+
det(...args) {
|
|
4263
|
+
return this._det(this, ...args);
|
|
4264
|
+
}
|
|
4265
|
+
diff(...args) {
|
|
4266
|
+
return this._diff(this, ...args);
|
|
4267
|
+
}
|
|
4268
|
+
eigenvalues(...args) {
|
|
4269
|
+
return this._eigenvalues(this, ...args);
|
|
4270
|
+
}
|
|
4271
|
+
eigenvectors(...args) {
|
|
4272
|
+
return this._eigenvectors(this, ...args);
|
|
4273
|
+
}
|
|
4274
|
+
elements_count(...args) {
|
|
4275
|
+
return this._elements_count(this, ...args);
|
|
4276
|
+
}
|
|
4277
|
+
fill(...args) {
|
|
4278
|
+
return this._fill(this, ...args);
|
|
4279
|
+
}
|
|
4280
|
+
get(...args) {
|
|
4281
|
+
return this._get(this, ...args);
|
|
4282
|
+
}
|
|
4283
|
+
inv(...args) {
|
|
4284
|
+
return this._inv(this, ...args);
|
|
4285
|
+
}
|
|
4286
|
+
is_antidiagonal(...args) {
|
|
4287
|
+
return this._is_antidiagonal(this, ...args);
|
|
4288
|
+
}
|
|
4289
|
+
is_antisymmetric(...args) {
|
|
4290
|
+
return this._is_antisymmetric(this, ...args);
|
|
4291
|
+
}
|
|
4292
|
+
is_binary(...args) {
|
|
4293
|
+
return this._is_binary(this, ...args);
|
|
4294
|
+
}
|
|
4295
|
+
is_diagonal(...args) {
|
|
4296
|
+
return this._is_diagonal(this, ...args);
|
|
4297
|
+
}
|
|
4298
|
+
is_identity(...args) {
|
|
4299
|
+
return this._is_identity(this, ...args);
|
|
4300
|
+
}
|
|
4301
|
+
is_square(...args) {
|
|
4302
|
+
return this._is_square(this, ...args);
|
|
4303
|
+
}
|
|
4304
|
+
is_stochastic(...args) {
|
|
4305
|
+
return this._is_stochastic(this, ...args);
|
|
4306
|
+
}
|
|
4307
|
+
is_symmetric(...args) {
|
|
4308
|
+
return this._is_symmetric(this, ...args);
|
|
4309
|
+
}
|
|
4310
|
+
is_triangular(...args) {
|
|
4311
|
+
return this._is_triangular(this, ...args);
|
|
4312
|
+
}
|
|
4313
|
+
is_zero(...args) {
|
|
4314
|
+
return this._is_zero(this, ...args);
|
|
4315
|
+
}
|
|
4316
|
+
kron(...args) {
|
|
4317
|
+
return this._kron(this, ...args);
|
|
4318
|
+
}
|
|
4319
|
+
max(...args) {
|
|
4320
|
+
return this._max(this, ...args);
|
|
4321
|
+
}
|
|
4322
|
+
median(...args) {
|
|
4323
|
+
return this._median(this, ...args);
|
|
4324
|
+
}
|
|
4325
|
+
min(...args) {
|
|
4326
|
+
return this._min(this, ...args);
|
|
4327
|
+
}
|
|
4328
|
+
mode(...args) {
|
|
4329
|
+
return this._mode(this, ...args);
|
|
4330
|
+
}
|
|
4331
|
+
mult(...args) {
|
|
4332
|
+
return this._mult(this, ...args);
|
|
4333
|
+
}
|
|
4334
|
+
pinv(...args) {
|
|
4335
|
+
return this._pinv(this, ...args);
|
|
4336
|
+
}
|
|
4337
|
+
pow(...args) {
|
|
4338
|
+
return this._pow(this, ...args);
|
|
4339
|
+
}
|
|
4340
|
+
rank(...args) {
|
|
4341
|
+
return this._rank(this, ...args);
|
|
4342
|
+
}
|
|
4343
|
+
remove_col(...args) {
|
|
4344
|
+
return this._remove_col(this, ...args);
|
|
4345
|
+
}
|
|
4346
|
+
remove_row(...args) {
|
|
4347
|
+
return this._remove_row(this, ...args);
|
|
4348
|
+
}
|
|
4349
|
+
reshape(...args) {
|
|
4350
|
+
return this._reshape(this, ...args);
|
|
4351
|
+
}
|
|
4352
|
+
reverse(...args) {
|
|
4353
|
+
return this._reverse(this, ...args);
|
|
4354
|
+
}
|
|
4355
|
+
row(...args) {
|
|
4356
|
+
return this._row(this, ...args);
|
|
4357
|
+
}
|
|
4358
|
+
rows(...args) {
|
|
4359
|
+
return this._rows(this, ...args);
|
|
4360
|
+
}
|
|
4361
|
+
set(...args) {
|
|
4362
|
+
return this._set(this, ...args);
|
|
4363
|
+
}
|
|
4364
|
+
sort(...args) {
|
|
4365
|
+
return this._sort(this, ...args);
|
|
4366
|
+
}
|
|
4367
|
+
submatrix(...args) {
|
|
4368
|
+
return this._submatrix(this, ...args);
|
|
4369
|
+
}
|
|
4370
|
+
sum(...args) {
|
|
4371
|
+
return this._sum(this, ...args);
|
|
4372
|
+
}
|
|
4373
|
+
swap_columns(...args) {
|
|
4374
|
+
return this._swap_columns(this, ...args);
|
|
4375
|
+
}
|
|
4376
|
+
swap_rows(...args) {
|
|
4377
|
+
return this._swap_rows(this, ...args);
|
|
4378
|
+
}
|
|
4379
|
+
trace(...args) {
|
|
4380
|
+
return this._trace(this, ...args);
|
|
4381
|
+
}
|
|
4382
|
+
transpose(...args) {
|
|
4383
|
+
return this._transpose(this, ...args);
|
|
4384
|
+
}
|
|
2261
4385
|
}
|
|
2262
4386
|
|
|
2263
|
-
function
|
|
2264
|
-
return (
|
|
2265
|
-
|
|
4387
|
+
function new_fn(context) {
|
|
4388
|
+
return (type, rows, cols, initial_value) => {
|
|
4389
|
+
return new PineMatrixObject(type, rows, cols, initial_value, context);
|
|
2266
4390
|
};
|
|
2267
4391
|
}
|
|
2268
4392
|
|
|
2269
|
-
function
|
|
2270
|
-
return (
|
|
2271
|
-
|
|
2272
|
-
const deviations = id.array.map((x) => Math.pow(x - mean, 2));
|
|
2273
|
-
const divisor = biased ? id.array.length : id.array.length - 1;
|
|
2274
|
-
return context.array.sum(new PineArrayObject(deviations)) / divisor;
|
|
4393
|
+
function param$4(context) {
|
|
4394
|
+
return (source, index = 0) => {
|
|
4395
|
+
return Series.from(source).get(index);
|
|
2275
4396
|
};
|
|
2276
4397
|
}
|
|
2277
4398
|
|
|
4399
|
+
class PineMatrix {
|
|
4400
|
+
constructor(context) {
|
|
4401
|
+
this.context = context;
|
|
4402
|
+
this.add_col = (id, ...args) => id.add_col(...args);
|
|
4403
|
+
this.add_row = (id, ...args) => id.add_row(...args);
|
|
4404
|
+
this.avg = (id, ...args) => id.avg(...args);
|
|
4405
|
+
this.col = (id, ...args) => id.col(...args);
|
|
4406
|
+
this.columns = (id, ...args) => id.columns(...args);
|
|
4407
|
+
this.concat = (id, ...args) => id.concat(...args);
|
|
4408
|
+
this.copy = (id, ...args) => id.copy(...args);
|
|
4409
|
+
this.det = (id, ...args) => id.det(...args);
|
|
4410
|
+
this.diff = (id, ...args) => id.diff(...args);
|
|
4411
|
+
this.eigenvalues = (id, ...args) => id.eigenvalues(...args);
|
|
4412
|
+
this.eigenvectors = (id, ...args) => id.eigenvectors(...args);
|
|
4413
|
+
this.elements_count = (id, ...args) => id.elements_count(...args);
|
|
4414
|
+
this.fill = (id, ...args) => id.fill(...args);
|
|
4415
|
+
this.get = (id, ...args) => id.get(...args);
|
|
4416
|
+
this.inv = (id, ...args) => id.inv(...args);
|
|
4417
|
+
this.is_antidiagonal = (id, ...args) => id.is_antidiagonal(...args);
|
|
4418
|
+
this.is_antisymmetric = (id, ...args) => id.is_antisymmetric(...args);
|
|
4419
|
+
this.is_binary = (id, ...args) => id.is_binary(...args);
|
|
4420
|
+
this.is_diagonal = (id, ...args) => id.is_diagonal(...args);
|
|
4421
|
+
this.is_identity = (id, ...args) => id.is_identity(...args);
|
|
4422
|
+
this.is_square = (id, ...args) => id.is_square(...args);
|
|
4423
|
+
this.is_stochastic = (id, ...args) => id.is_stochastic(...args);
|
|
4424
|
+
this.is_symmetric = (id, ...args) => id.is_symmetric(...args);
|
|
4425
|
+
this.is_triangular = (id, ...args) => id.is_triangular(...args);
|
|
4426
|
+
this.is_zero = (id, ...args) => id.is_zero(...args);
|
|
4427
|
+
this.kron = (id, ...args) => id.kron(...args);
|
|
4428
|
+
this.max = (id, ...args) => id.max(...args);
|
|
4429
|
+
this.median = (id, ...args) => id.median(...args);
|
|
4430
|
+
this.min = (id, ...args) => id.min(...args);
|
|
4431
|
+
this.mode = (id, ...args) => id.mode(...args);
|
|
4432
|
+
this.mult = (id, ...args) => id.mult(...args);
|
|
4433
|
+
this.new = new_fn(context);
|
|
4434
|
+
this.param = param$4();
|
|
4435
|
+
this.pinv = (id, ...args) => id.pinv(...args);
|
|
4436
|
+
this.pow = (id, ...args) => id.pow(...args);
|
|
4437
|
+
this.rank = (id, ...args) => id.rank(...args);
|
|
4438
|
+
this.remove_col = (id, ...args) => id.remove_col(...args);
|
|
4439
|
+
this.remove_row = (id, ...args) => id.remove_row(...args);
|
|
4440
|
+
this.reshape = (id, ...args) => id.reshape(...args);
|
|
4441
|
+
this.reverse = (id, ...args) => id.reverse(...args);
|
|
4442
|
+
this.row = (id, ...args) => id.row(...args);
|
|
4443
|
+
this.rows = (id, ...args) => id.rows(...args);
|
|
4444
|
+
this.set = (id, ...args) => id.set(...args);
|
|
4445
|
+
this.sort = (id, ...args) => id.sort(...args);
|
|
4446
|
+
this.submatrix = (id, ...args) => id.submatrix(...args);
|
|
4447
|
+
this.sum = (id, ...args) => id.sum(...args);
|
|
4448
|
+
this.swap_columns = (id, ...args) => id.swap_columns(...args);
|
|
4449
|
+
this.swap_rows = (id, ...args) => id.swap_rows(...args);
|
|
4450
|
+
this.trace = (id, ...args) => id.trace(...args);
|
|
4451
|
+
this.transpose = (id, ...args) => id.transpose(...args);
|
|
4452
|
+
}
|
|
4453
|
+
}
|
|
4454
|
+
|
|
2278
4455
|
var __defProp$9 = Object.defineProperty;
|
|
2279
4456
|
var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2280
4457
|
var __publicField$9 = (obj, key, value) => __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2281
|
-
|
|
2282
|
-
abs: abs$1,
|
|
2283
|
-
avg: avg$1,
|
|
2284
|
-
clear,
|
|
2285
|
-
concat,
|
|
2286
|
-
copy,
|
|
2287
|
-
covariance,
|
|
2288
|
-
every,
|
|
2289
|
-
fill,
|
|
2290
|
-
first,
|
|
2291
|
-
from,
|
|
2292
|
-
get,
|
|
2293
|
-
includes,
|
|
2294
|
-
indexof,
|
|
2295
|
-
insert,
|
|
2296
|
-
join,
|
|
2297
|
-
last,
|
|
2298
|
-
lastindexof,
|
|
2299
|
-
max: max$1,
|
|
2300
|
-
min: min$1,
|
|
2301
|
-
new: new_fn,
|
|
2302
|
-
new_bool,
|
|
2303
|
-
new_float,
|
|
2304
|
-
new_int,
|
|
2305
|
-
new_string,
|
|
2306
|
-
param: param$4,
|
|
2307
|
-
pop,
|
|
2308
|
-
push,
|
|
2309
|
-
range: range$1,
|
|
2310
|
-
remove,
|
|
2311
|
-
reverse,
|
|
2312
|
-
set,
|
|
2313
|
-
shift,
|
|
2314
|
-
size,
|
|
2315
|
-
slice,
|
|
2316
|
-
some,
|
|
2317
|
-
sort,
|
|
2318
|
-
sort_indices,
|
|
2319
|
-
standardize,
|
|
2320
|
-
stdev: stdev$1,
|
|
2321
|
-
sum: sum$1,
|
|
2322
|
-
unshift,
|
|
2323
|
-
variance: variance$1
|
|
2324
|
-
};
|
|
2325
|
-
class PineArray {
|
|
4458
|
+
class Barstate {
|
|
2326
4459
|
constructor(context) {
|
|
2327
4460
|
this.context = context;
|
|
2328
|
-
__publicField$9(this, "
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
__publicField$9(this, "param");
|
|
2354
|
-
__publicField$9(this, "pop");
|
|
2355
|
-
__publicField$9(this, "push");
|
|
2356
|
-
__publicField$9(this, "range");
|
|
2357
|
-
__publicField$9(this, "remove");
|
|
2358
|
-
__publicField$9(this, "reverse");
|
|
2359
|
-
__publicField$9(this, "set");
|
|
2360
|
-
__publicField$9(this, "shift");
|
|
2361
|
-
__publicField$9(this, "size");
|
|
2362
|
-
__publicField$9(this, "slice");
|
|
2363
|
-
__publicField$9(this, "some");
|
|
2364
|
-
__publicField$9(this, "sort");
|
|
2365
|
-
__publicField$9(this, "sort_indices");
|
|
2366
|
-
__publicField$9(this, "standardize");
|
|
2367
|
-
__publicField$9(this, "stdev");
|
|
2368
|
-
__publicField$9(this, "sum");
|
|
2369
|
-
__publicField$9(this, "unshift");
|
|
2370
|
-
__publicField$9(this, "variance");
|
|
2371
|
-
Object.entries(methods$4).forEach(([name, factory]) => {
|
|
2372
|
-
this[name] = factory(context);
|
|
2373
|
-
});
|
|
4461
|
+
__publicField$9(this, "_live", false);
|
|
4462
|
+
}
|
|
4463
|
+
setLive() {
|
|
4464
|
+
this._live = true;
|
|
4465
|
+
}
|
|
4466
|
+
get isnew() {
|
|
4467
|
+
return !this._live;
|
|
4468
|
+
}
|
|
4469
|
+
get islast() {
|
|
4470
|
+
return this.context.idx === this.context.data.close.data.length - 1;
|
|
4471
|
+
}
|
|
4472
|
+
get isfirst() {
|
|
4473
|
+
return this.context.idx === 0;
|
|
4474
|
+
}
|
|
4475
|
+
get ishistory() {
|
|
4476
|
+
return this.context.idx < this.context.data.close.data.length - 1;
|
|
4477
|
+
}
|
|
4478
|
+
get isrealtime() {
|
|
4479
|
+
return this.context.idx === this.context.data.close.data.length - 1;
|
|
4480
|
+
}
|
|
4481
|
+
get isconfirmed() {
|
|
4482
|
+
return this.context.data.closeTime[this.context.data.closeTime.length - 1] <= (/* @__PURE__ */ new Date()).getTime();
|
|
4483
|
+
}
|
|
4484
|
+
get islastconfirmedhistory() {
|
|
4485
|
+
return this.context.data.closeTime[this.context.data.closeTime.length - 1] <= (/* @__PURE__ */ new Date()).getTime();
|
|
2374
4486
|
}
|
|
2375
4487
|
}
|
|
2376
4488
|
|
|
@@ -2402,7 +4514,22 @@ class Core {
|
|
|
2402
4514
|
maroon: "maroon",
|
|
2403
4515
|
black: "black",
|
|
2404
4516
|
gray: "gray",
|
|
2405
|
-
blue: "blue"
|
|
4517
|
+
blue: "blue",
|
|
4518
|
+
yellow: "yellow",
|
|
4519
|
+
orange: "orange",
|
|
4520
|
+
purple: "purple",
|
|
4521
|
+
pink: "pink",
|
|
4522
|
+
brown: "brown",
|
|
4523
|
+
teal: "teal",
|
|
4524
|
+
cyan: "cyan",
|
|
4525
|
+
navy: "navy",
|
|
4526
|
+
indigo: "indigo",
|
|
4527
|
+
violet: "violet",
|
|
4528
|
+
magenta: "magenta",
|
|
4529
|
+
rose: "rose",
|
|
4530
|
+
gold: "gold",
|
|
4531
|
+
silver: "silver",
|
|
4532
|
+
bronze: "bronze"
|
|
2406
4533
|
});
|
|
2407
4534
|
}
|
|
2408
4535
|
extractPlotOptions(options) {
|
|
@@ -2450,94 +4577,155 @@ class Core {
|
|
|
2450
4577
|
}
|
|
2451
4578
|
}
|
|
2452
4579
|
|
|
4580
|
+
const INPUT_ARGS_OPTIONS = [
|
|
4581
|
+
["defval", "title", "tooltip", "inline", "group", "display"],
|
|
4582
|
+
["defval", "title", "tooltip", "group", "confirm", "display"],
|
|
4583
|
+
["defval", "title", "tooltip", "inline", "group", "confirm", "display"],
|
|
4584
|
+
["defval", "title", "options", "tooltip", "inline", "group", "confirm", "display"],
|
|
4585
|
+
["defval", "title", "minval", "maxval", "step", "tooltip", "inline", "group", "confirm", "display"]
|
|
4586
|
+
];
|
|
4587
|
+
const INPUT_ARGS_TYPE_CHECK = {
|
|
4588
|
+
defval: (arg) => typeof arg !== "object",
|
|
4589
|
+
title: (arg) => typeof arg === "string",
|
|
4590
|
+
tooltip: (arg) => typeof arg === "string",
|
|
4591
|
+
inline: (arg) => typeof arg === "string",
|
|
4592
|
+
group: (arg) => typeof arg === "string",
|
|
4593
|
+
display: (arg) => typeof arg === "string",
|
|
4594
|
+
confirm: (arg) => typeof arg === "boolean",
|
|
4595
|
+
options: (arg) => Array.isArray(arg),
|
|
4596
|
+
minval: (arg) => typeof arg === "number",
|
|
4597
|
+
maxval: (arg) => typeof arg === "number",
|
|
4598
|
+
step: (arg) => typeof arg === "number"
|
|
4599
|
+
};
|
|
4600
|
+
function parseInputOptions(args) {
|
|
4601
|
+
const options = {};
|
|
4602
|
+
let options_arg = {};
|
|
4603
|
+
const valid = new Array(INPUT_ARGS_OPTIONS.length).fill(true);
|
|
4604
|
+
for (let i = 0; i < args.length; i++) {
|
|
4605
|
+
const arg = args[i];
|
|
4606
|
+
if (typeof arg === "object") {
|
|
4607
|
+
options_arg = arg;
|
|
4608
|
+
break;
|
|
4609
|
+
}
|
|
4610
|
+
const curOptions = INPUT_ARGS_OPTIONS.map((e, idx) => valid[idx] ? e[i] : void 0);
|
|
4611
|
+
for (let o = 0; o < curOptions.length; o++) {
|
|
4612
|
+
const optionName = curOptions[o];
|
|
4613
|
+
if (optionName === void 0) {
|
|
4614
|
+
valid[o] = false;
|
|
4615
|
+
continue;
|
|
4616
|
+
}
|
|
4617
|
+
if (INPUT_ARGS_TYPE_CHECK[optionName](arg)) {
|
|
4618
|
+
options[optionName] = arg;
|
|
4619
|
+
} else {
|
|
4620
|
+
valid[o] = false;
|
|
4621
|
+
}
|
|
4622
|
+
}
|
|
4623
|
+
}
|
|
4624
|
+
return { ...options_arg, ...options };
|
|
4625
|
+
}
|
|
4626
|
+
|
|
2453
4627
|
function any(context) {
|
|
2454
|
-
return (
|
|
2455
|
-
|
|
4628
|
+
return (...args) => {
|
|
4629
|
+
const options = parseInputOptions(args);
|
|
4630
|
+
return options.defval;
|
|
2456
4631
|
};
|
|
2457
4632
|
}
|
|
2458
4633
|
|
|
2459
4634
|
function bool(context) {
|
|
2460
|
-
return (
|
|
2461
|
-
|
|
4635
|
+
return (...args) => {
|
|
4636
|
+
const options = parseInputOptions(args);
|
|
4637
|
+
return options.defval;
|
|
2462
4638
|
};
|
|
2463
4639
|
}
|
|
2464
4640
|
|
|
2465
4641
|
function color(context) {
|
|
2466
|
-
return (
|
|
2467
|
-
|
|
4642
|
+
return (...args) => {
|
|
4643
|
+
const options = parseInputOptions(args);
|
|
4644
|
+
return options.defval;
|
|
2468
4645
|
};
|
|
2469
4646
|
}
|
|
2470
4647
|
|
|
2471
4648
|
function enum_fn(context) {
|
|
2472
|
-
return (
|
|
2473
|
-
|
|
4649
|
+
return (...args) => {
|
|
4650
|
+
const options = parseInputOptions(args);
|
|
4651
|
+
return options.defval;
|
|
2474
4652
|
};
|
|
2475
4653
|
}
|
|
2476
4654
|
|
|
2477
4655
|
function float(context) {
|
|
2478
|
-
return (
|
|
2479
|
-
|
|
4656
|
+
return (...args) => {
|
|
4657
|
+
const options = parseInputOptions(args);
|
|
4658
|
+
return options.defval;
|
|
2480
4659
|
};
|
|
2481
4660
|
}
|
|
2482
4661
|
|
|
2483
4662
|
function int(context) {
|
|
2484
|
-
return (
|
|
2485
|
-
|
|
4663
|
+
return (...args) => {
|
|
4664
|
+
const options = parseInputOptions(args);
|
|
4665
|
+
return options.defval;
|
|
2486
4666
|
};
|
|
2487
4667
|
}
|
|
2488
4668
|
|
|
2489
4669
|
function param$3(context) {
|
|
2490
4670
|
return (source, index = 0) => {
|
|
2491
4671
|
const val = Series.from(source).get(index);
|
|
2492
|
-
return
|
|
4672
|
+
return val;
|
|
2493
4673
|
};
|
|
2494
4674
|
}
|
|
2495
4675
|
|
|
2496
4676
|
function price(context) {
|
|
2497
|
-
return (
|
|
2498
|
-
|
|
4677
|
+
return (...args) => {
|
|
4678
|
+
const options = parseInputOptions(args);
|
|
4679
|
+
return options.defval;
|
|
2499
4680
|
};
|
|
2500
4681
|
}
|
|
2501
4682
|
|
|
2502
4683
|
function session(context) {
|
|
2503
|
-
return (
|
|
2504
|
-
|
|
4684
|
+
return (...args) => {
|
|
4685
|
+
const options = parseInputOptions(args);
|
|
4686
|
+
return options.defval;
|
|
2505
4687
|
};
|
|
2506
4688
|
}
|
|
2507
4689
|
|
|
2508
4690
|
function source(context) {
|
|
2509
|
-
return (
|
|
2510
|
-
|
|
4691
|
+
return (...args) => {
|
|
4692
|
+
const options = parseInputOptions(args);
|
|
4693
|
+
return options.defval;
|
|
2511
4694
|
};
|
|
2512
4695
|
}
|
|
2513
4696
|
|
|
2514
4697
|
function string(context) {
|
|
2515
|
-
return (
|
|
2516
|
-
|
|
4698
|
+
return (...args) => {
|
|
4699
|
+
const options = parseInputOptions(args);
|
|
4700
|
+
return options.defval;
|
|
2517
4701
|
};
|
|
2518
4702
|
}
|
|
2519
4703
|
|
|
2520
4704
|
function symbol(context) {
|
|
2521
|
-
return (
|
|
2522
|
-
|
|
4705
|
+
return (...args) => {
|
|
4706
|
+
const options = parseInputOptions(args);
|
|
4707
|
+
return options.defval;
|
|
2523
4708
|
};
|
|
2524
4709
|
}
|
|
2525
4710
|
|
|
2526
4711
|
function text_area(context) {
|
|
2527
|
-
return (
|
|
2528
|
-
|
|
4712
|
+
return (...args) => {
|
|
4713
|
+
const options = parseInputOptions(args);
|
|
4714
|
+
return options.defval;
|
|
2529
4715
|
};
|
|
2530
4716
|
}
|
|
2531
4717
|
|
|
2532
4718
|
function time(context) {
|
|
2533
|
-
return (
|
|
2534
|
-
|
|
4719
|
+
return (...args) => {
|
|
4720
|
+
const options = parseInputOptions(args);
|
|
4721
|
+
return options.defval;
|
|
2535
4722
|
};
|
|
2536
4723
|
}
|
|
2537
4724
|
|
|
2538
4725
|
function timeframe(context) {
|
|
2539
|
-
return (
|
|
2540
|
-
|
|
4726
|
+
return (...args) => {
|
|
4727
|
+
const options = parseInputOptions(args);
|
|
4728
|
+
return options.defval;
|
|
2541
4729
|
};
|
|
2542
4730
|
}
|
|
2543
4731
|
|
|
@@ -2631,6 +4819,12 @@ function cos(context) {
|
|
|
2631
4819
|
};
|
|
2632
4820
|
}
|
|
2633
4821
|
|
|
4822
|
+
function e(context) {
|
|
4823
|
+
return () => {
|
|
4824
|
+
return Math.E;
|
|
4825
|
+
};
|
|
4826
|
+
}
|
|
4827
|
+
|
|
2634
4828
|
function exp(context) {
|
|
2635
4829
|
return (source) => {
|
|
2636
4830
|
return Math.exp(Series.from(source).get(0));
|
|
@@ -2699,6 +4893,18 @@ function param$2(context) {
|
|
|
2699
4893
|
};
|
|
2700
4894
|
}
|
|
2701
4895
|
|
|
4896
|
+
function phi(context) {
|
|
4897
|
+
return () => {
|
|
4898
|
+
return 1.618033988749895;
|
|
4899
|
+
};
|
|
4900
|
+
}
|
|
4901
|
+
|
|
4902
|
+
function pi(context) {
|
|
4903
|
+
return () => {
|
|
4904
|
+
return Math.PI;
|
|
4905
|
+
};
|
|
4906
|
+
}
|
|
4907
|
+
|
|
2702
4908
|
function pow(context) {
|
|
2703
4909
|
return (source, power) => {
|
|
2704
4910
|
return Math.pow(Series.from(source).get(0), Series.from(power).get(0));
|
|
@@ -2717,6 +4923,24 @@ function round(context) {
|
|
|
2717
4923
|
};
|
|
2718
4924
|
}
|
|
2719
4925
|
|
|
4926
|
+
function round_to_mintick(context) {
|
|
4927
|
+
return (source) => {
|
|
4928
|
+
return context.precision(Math.round(Series.from(source).get(0) / context.pine.syminfo.mintick) * context.pine.syminfo.mintick);
|
|
4929
|
+
};
|
|
4930
|
+
}
|
|
4931
|
+
|
|
4932
|
+
function rphi(context) {
|
|
4933
|
+
return () => {
|
|
4934
|
+
return 0.6180339887498948;
|
|
4935
|
+
};
|
|
4936
|
+
}
|
|
4937
|
+
|
|
4938
|
+
function sign(context) {
|
|
4939
|
+
return (source) => {
|
|
4940
|
+
return Math.sign(Series.from(source).get(0));
|
|
4941
|
+
};
|
|
4942
|
+
}
|
|
4943
|
+
|
|
2720
4944
|
function sin(context) {
|
|
2721
4945
|
return (source) => {
|
|
2722
4946
|
return Math.sin(Series.from(source).get(0));
|
|
@@ -2769,6 +4993,7 @@ const methods$2 = {
|
|
|
2769
4993
|
avg,
|
|
2770
4994
|
ceil,
|
|
2771
4995
|
cos,
|
|
4996
|
+
e,
|
|
2772
4997
|
exp,
|
|
2773
4998
|
floor,
|
|
2774
4999
|
ln,
|
|
@@ -2777,9 +5002,14 @@ const methods$2 = {
|
|
|
2777
5002
|
max,
|
|
2778
5003
|
min,
|
|
2779
5004
|
param: param$2,
|
|
5005
|
+
phi,
|
|
5006
|
+
pi,
|
|
2780
5007
|
pow,
|
|
2781
5008
|
random,
|
|
2782
5009
|
round,
|
|
5010
|
+
round_to_mintick,
|
|
5011
|
+
rphi,
|
|
5012
|
+
sign,
|
|
2783
5013
|
sin,
|
|
2784
5014
|
sqrt,
|
|
2785
5015
|
sum,
|
|
@@ -2797,6 +5027,7 @@ class PineMath {
|
|
|
2797
5027
|
__publicField$6(this, "avg");
|
|
2798
5028
|
__publicField$6(this, "ceil");
|
|
2799
5029
|
__publicField$6(this, "cos");
|
|
5030
|
+
__publicField$6(this, "e");
|
|
2800
5031
|
__publicField$6(this, "exp");
|
|
2801
5032
|
__publicField$6(this, "floor");
|
|
2802
5033
|
__publicField$6(this, "ln");
|
|
@@ -2805,9 +5036,14 @@ class PineMath {
|
|
|
2805
5036
|
__publicField$6(this, "max");
|
|
2806
5037
|
__publicField$6(this, "min");
|
|
2807
5038
|
__publicField$6(this, "param");
|
|
5039
|
+
__publicField$6(this, "phi");
|
|
5040
|
+
__publicField$6(this, "pi");
|
|
2808
5041
|
__publicField$6(this, "pow");
|
|
2809
5042
|
__publicField$6(this, "random");
|
|
2810
5043
|
__publicField$6(this, "round");
|
|
5044
|
+
__publicField$6(this, "round_to_mintick");
|
|
5045
|
+
__publicField$6(this, "rphi");
|
|
5046
|
+
__publicField$6(this, "sign");
|
|
2811
5047
|
__publicField$6(this, "sin");
|
|
2812
5048
|
__publicField$6(this, "sqrt");
|
|
2813
5049
|
__publicField$6(this, "sum");
|
|
@@ -2974,12 +5210,68 @@ function security(context) {
|
|
|
2974
5210
|
};
|
|
2975
5211
|
}
|
|
2976
5212
|
|
|
5213
|
+
function security_lower_tf(context) {
|
|
5214
|
+
return async (symbol, timeframe, expression, ignore_invalid_symbol = false, currency = null, ignore_invalid_timeframe = false, calc_bars_count = 0) => {
|
|
5215
|
+
const _symbol = symbol[0];
|
|
5216
|
+
const _timeframe = timeframe[0];
|
|
5217
|
+
const _expression = expression[0];
|
|
5218
|
+
const _expression_name = expression[1];
|
|
5219
|
+
Array.isArray(ignore_invalid_symbol) ? ignore_invalid_symbol[0] : ignore_invalid_symbol;
|
|
5220
|
+
const _ignore_invalid_timeframe = Array.isArray(ignore_invalid_timeframe) ? ignore_invalid_timeframe[0] : ignore_invalid_timeframe;
|
|
5221
|
+
if (context.isSecondaryContext) {
|
|
5222
|
+
return Array.isArray(_expression) ? [_expression] : _expression;
|
|
5223
|
+
}
|
|
5224
|
+
const ctxTimeframeIdx = TIMEFRAMES.indexOf(context.timeframe);
|
|
5225
|
+
const reqTimeframeIdx = TIMEFRAMES.indexOf(_timeframe);
|
|
5226
|
+
if (ctxTimeframeIdx === -1 || reqTimeframeIdx === -1) {
|
|
5227
|
+
if (_ignore_invalid_timeframe) return NaN;
|
|
5228
|
+
throw new Error("Invalid timeframe");
|
|
5229
|
+
}
|
|
5230
|
+
if (reqTimeframeIdx > ctxTimeframeIdx) {
|
|
5231
|
+
if (_ignore_invalid_timeframe) return NaN;
|
|
5232
|
+
throw new Error(`Timeframe ${_timeframe} is not lower than or equal to chart timeframe ${context.timeframe}`);
|
|
5233
|
+
}
|
|
5234
|
+
if (reqTimeframeIdx === ctxTimeframeIdx) {
|
|
5235
|
+
return [[_expression]];
|
|
5236
|
+
}
|
|
5237
|
+
const cacheKey = `${_symbol}_${_timeframe}_${_expression_name}_lower`;
|
|
5238
|
+
if (!context.cache[cacheKey]) {
|
|
5239
|
+
const buffer = 1e3 * 60 * 60 * 24 * 30;
|
|
5240
|
+
const adjustedSDate = context.sDate ? context.sDate - buffer : void 0;
|
|
5241
|
+
const limit = context.sDate && context.eDate ? void 0 : context.limit || 1e3;
|
|
5242
|
+
const pineTS = new PineTS(context.source, _symbol, _timeframe, limit, adjustedSDate, context.eDate);
|
|
5243
|
+
pineTS.markAsSecondary();
|
|
5244
|
+
const secContext2 = await pineTS.run(context.pineTSCode);
|
|
5245
|
+
context.cache[cacheKey] = secContext2;
|
|
5246
|
+
}
|
|
5247
|
+
const secContext = context.cache[cacheKey];
|
|
5248
|
+
const myOpenTime = Series.from(context.data.openTime).get(0);
|
|
5249
|
+
const myCloseTime = Series.from(context.data.closeTime).get(0);
|
|
5250
|
+
const secOpenTimes = secContext.data.openTime.data;
|
|
5251
|
+
const secCloseTimes = secContext.data.closeTime.data;
|
|
5252
|
+
const secValues = secContext.params[_expression_name];
|
|
5253
|
+
if (!secValues) return [];
|
|
5254
|
+
const result = [];
|
|
5255
|
+
for (let i = 0; i < secOpenTimes.length; i++) {
|
|
5256
|
+
const sOpen = secOpenTimes[i];
|
|
5257
|
+
const sClose = secCloseTimes[i];
|
|
5258
|
+
if (sClose <= myOpenTime) continue;
|
|
5259
|
+
if (sOpen >= myCloseTime) break;
|
|
5260
|
+
if (sOpen >= myOpenTime && sOpen < myCloseTime) {
|
|
5261
|
+
result.push(secValues[i]);
|
|
5262
|
+
}
|
|
5263
|
+
}
|
|
5264
|
+
return [result];
|
|
5265
|
+
};
|
|
5266
|
+
}
|
|
5267
|
+
|
|
2977
5268
|
var __defProp$5 = Object.defineProperty;
|
|
2978
5269
|
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2979
5270
|
var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2980
5271
|
const methods$1 = {
|
|
2981
5272
|
param: param$1,
|
|
2982
|
-
security
|
|
5273
|
+
security,
|
|
5274
|
+
security_lower_tf
|
|
2983
5275
|
};
|
|
2984
5276
|
class PineRequest {
|
|
2985
5277
|
constructor(context) {
|
|
@@ -2987,6 +5279,7 @@ class PineRequest {
|
|
|
2987
5279
|
__publicField$5(this, "_cache", {});
|
|
2988
5280
|
__publicField$5(this, "param");
|
|
2989
5281
|
__publicField$5(this, "security");
|
|
5282
|
+
__publicField$5(this, "security_lower_tf");
|
|
2990
5283
|
Object.entries(methods$1).forEach(([name, factory]) => {
|
|
2991
5284
|
this[name] = factory(context);
|
|
2992
5285
|
});
|
|
@@ -4789,10 +7082,10 @@ function tr(context) {
|
|
|
4789
7082
|
const low0 = context.get(context.data.low, 0);
|
|
4790
7083
|
const close1 = context.get(context.data.close, 1);
|
|
4791
7084
|
if (isNaN(close1)) {
|
|
4792
|
-
return handleNa ? high0 - low0 : NaN;
|
|
7085
|
+
return handleNa ? context.precision(high0 - low0) : NaN;
|
|
4793
7086
|
}
|
|
4794
7087
|
const val = Math.max(high0 - low0, Math.abs(high0 - close1), Math.abs(low0 - close1));
|
|
4795
|
-
return val;
|
|
7088
|
+
return context.precision(val);
|
|
4796
7089
|
};
|
|
4797
7090
|
}
|
|
4798
7091
|
|
|
@@ -5284,6 +7577,189 @@ class TechnicalAnalysis {
|
|
|
5284
7577
|
}
|
|
5285
7578
|
}
|
|
5286
7579
|
|
|
7580
|
+
class Log {
|
|
7581
|
+
constructor(context) {
|
|
7582
|
+
this.context = context;
|
|
7583
|
+
}
|
|
7584
|
+
logFormat(message, ...args) {
|
|
7585
|
+
return message.replace(/{(\d+)}/g, (match, index) => args[index]);
|
|
7586
|
+
}
|
|
7587
|
+
param(source, index = 0, name) {
|
|
7588
|
+
return Series.from(source).get(index);
|
|
7589
|
+
}
|
|
7590
|
+
warning(message, ...args) {
|
|
7591
|
+
console.warn(this.logFormat(message, ...args));
|
|
7592
|
+
}
|
|
7593
|
+
error(message, ...args) {
|
|
7594
|
+
console.error(this.logFormat(message, ...args));
|
|
7595
|
+
}
|
|
7596
|
+
info(message, ...args) {
|
|
7597
|
+
console.log(this.logFormat(message, ...args));
|
|
7598
|
+
}
|
|
7599
|
+
}
|
|
7600
|
+
|
|
7601
|
+
class Str {
|
|
7602
|
+
constructor(context) {
|
|
7603
|
+
this.context = context;
|
|
7604
|
+
}
|
|
7605
|
+
param(source, index = 0, name) {
|
|
7606
|
+
return Series.from(source).get(index);
|
|
7607
|
+
}
|
|
7608
|
+
tostring(value) {
|
|
7609
|
+
return String(value);
|
|
7610
|
+
}
|
|
7611
|
+
tonumber(value) {
|
|
7612
|
+
return Number(value);
|
|
7613
|
+
}
|
|
7614
|
+
lower(value) {
|
|
7615
|
+
return String(value).toLowerCase();
|
|
7616
|
+
}
|
|
7617
|
+
upper(value) {
|
|
7618
|
+
return String(value).toUpperCase();
|
|
7619
|
+
}
|
|
7620
|
+
trim(value) {
|
|
7621
|
+
return String(value).trim();
|
|
7622
|
+
}
|
|
7623
|
+
repeat(source, repeat, separator = "") {
|
|
7624
|
+
return Array(repeat).fill(source).join(separator || "");
|
|
7625
|
+
}
|
|
7626
|
+
replace_all(source, target, replacement) {
|
|
7627
|
+
return String(source).replaceAll(target, replacement);
|
|
7628
|
+
}
|
|
7629
|
+
//occurense is the nth occurrence to replace
|
|
7630
|
+
replace(source, target, replacement, occurrence = 0) {
|
|
7631
|
+
const str = String(source);
|
|
7632
|
+
const tgt = String(target);
|
|
7633
|
+
const repl = String(replacement);
|
|
7634
|
+
const occ = Math.floor(Number(occurrence)) || 0;
|
|
7635
|
+
if (tgt === "") return str;
|
|
7636
|
+
let pos = 0;
|
|
7637
|
+
let found = 0;
|
|
7638
|
+
while (true) {
|
|
7639
|
+
const idx = str.indexOf(tgt, pos);
|
|
7640
|
+
if (idx === -1) return str;
|
|
7641
|
+
if (found === occ) {
|
|
7642
|
+
return str.substring(0, idx) + repl + str.substring(idx + tgt.length);
|
|
7643
|
+
}
|
|
7644
|
+
found++;
|
|
7645
|
+
pos = idx + tgt.length;
|
|
7646
|
+
}
|
|
7647
|
+
}
|
|
7648
|
+
contains(source, target) {
|
|
7649
|
+
return String(source).includes(target);
|
|
7650
|
+
}
|
|
7651
|
+
endswith(source, target) {
|
|
7652
|
+
return String(source).endsWith(target);
|
|
7653
|
+
}
|
|
7654
|
+
startswith(source, target) {
|
|
7655
|
+
return String(source).startsWith(target);
|
|
7656
|
+
}
|
|
7657
|
+
pos(source, target) {
|
|
7658
|
+
const idx = String(source).indexOf(target);
|
|
7659
|
+
return idx === -1 ? NaN : idx;
|
|
7660
|
+
}
|
|
7661
|
+
length(source) {
|
|
7662
|
+
return String(source).length;
|
|
7663
|
+
}
|
|
7664
|
+
match(source, pattern) {
|
|
7665
|
+
return String(source).match(new RegExp(pattern));
|
|
7666
|
+
}
|
|
7667
|
+
split(source, separator) {
|
|
7668
|
+
return [String(source).split(separator)];
|
|
7669
|
+
}
|
|
7670
|
+
substring(source, begin_pos, end_pos) {
|
|
7671
|
+
return String(source).substring(begin_pos, end_pos);
|
|
7672
|
+
}
|
|
7673
|
+
format(message, ...args) {
|
|
7674
|
+
return message.replace(/{(\d+)}/g, (match, index) => args[index]);
|
|
7675
|
+
}
|
|
7676
|
+
}
|
|
7677
|
+
|
|
7678
|
+
class Timeframe {
|
|
7679
|
+
constructor(context) {
|
|
7680
|
+
this.context = context;
|
|
7681
|
+
}
|
|
7682
|
+
param(source, index = 0, name) {
|
|
7683
|
+
return Series.from(source).get(index);
|
|
7684
|
+
}
|
|
7685
|
+
//Note : current PineTS implementation does not differentiate between main_period and period because the timeframe is always taken from the main execution context.
|
|
7686
|
+
//once we implement indicator() function, the main_period can be overridden by the indicator's timeframe.
|
|
7687
|
+
get main_period() {
|
|
7688
|
+
return this.context.timeframe;
|
|
7689
|
+
}
|
|
7690
|
+
get period() {
|
|
7691
|
+
return this.context.timeframe;
|
|
7692
|
+
}
|
|
7693
|
+
get multiplier() {
|
|
7694
|
+
const val = parseInt(this.context.timeframe);
|
|
7695
|
+
return isNaN(val) ? 1 : val;
|
|
7696
|
+
}
|
|
7697
|
+
get isdwm() {
|
|
7698
|
+
return ["D", "W", "M"].includes(this.context.timeframe.slice(-1));
|
|
7699
|
+
}
|
|
7700
|
+
get isdaily() {
|
|
7701
|
+
return this.context.timeframe.slice(-1) === "D";
|
|
7702
|
+
}
|
|
7703
|
+
get isweekly() {
|
|
7704
|
+
return this.context.timeframe.slice(-1) === "W";
|
|
7705
|
+
}
|
|
7706
|
+
get ismonthly() {
|
|
7707
|
+
return this.context.timeframe.slice(-1) === "M";
|
|
7708
|
+
}
|
|
7709
|
+
get isseconds() {
|
|
7710
|
+
return this.context.timeframe.slice(-1) === "S";
|
|
7711
|
+
}
|
|
7712
|
+
get isminutes() {
|
|
7713
|
+
return parseInt(this.context.timeframe).toString() == this.context.timeframe.trim();
|
|
7714
|
+
}
|
|
7715
|
+
get isintraday() {
|
|
7716
|
+
return !this.isdwm;
|
|
7717
|
+
}
|
|
7718
|
+
// public change(timeframe: string) {
|
|
7719
|
+
// const prevOpenTime = this.context.data.openTime.get(this.context.data.openTime.length - 2);
|
|
7720
|
+
// const currentOpenTime = this.context.data.openTime.get(this.context.data.openTime.length - 1);
|
|
7721
|
+
// }
|
|
7722
|
+
from_seconds(seconds) {
|
|
7723
|
+
if (seconds < 60) {
|
|
7724
|
+
const roundedSeconds = Math.ceil(seconds / 5) * 5;
|
|
7725
|
+
return roundedSeconds + "S";
|
|
7726
|
+
}
|
|
7727
|
+
if (seconds < 60 * 60 * 24) {
|
|
7728
|
+
const roundedMinutes = Math.ceil(seconds / 60);
|
|
7729
|
+
return roundedMinutes;
|
|
7730
|
+
}
|
|
7731
|
+
if (seconds <= 60 * 60 * 24 * 7 * 52) {
|
|
7732
|
+
if (seconds % (60 * 60 * 24 * 7) === 0) {
|
|
7733
|
+
const roundedWeeks = Math.ceil(seconds / (60 * 60 * 24 * 7));
|
|
7734
|
+
return roundedWeeks + "W";
|
|
7735
|
+
}
|
|
7736
|
+
const roundedHours = Math.ceil(seconds / (60 * 60 * 24));
|
|
7737
|
+
return roundedHours + "D";
|
|
7738
|
+
}
|
|
7739
|
+
return "12M";
|
|
7740
|
+
}
|
|
7741
|
+
in_seconds(timeframe) {
|
|
7742
|
+
const multiplier = parseInt(timeframe);
|
|
7743
|
+
const unit = timeframe.slice(-1);
|
|
7744
|
+
if (unit === "S") {
|
|
7745
|
+
return multiplier;
|
|
7746
|
+
}
|
|
7747
|
+
if (unit === "D") {
|
|
7748
|
+
return multiplier * 60 * 60 * 24;
|
|
7749
|
+
}
|
|
7750
|
+
if (unit === "W") {
|
|
7751
|
+
return multiplier * 60 * 60 * 24 * 7;
|
|
7752
|
+
}
|
|
7753
|
+
if (unit === "M") {
|
|
7754
|
+
return multiplier * 60 * 60 * 24 * 30;
|
|
7755
|
+
}
|
|
7756
|
+
if (!isNaN(multiplier)) {
|
|
7757
|
+
return multiplier * 60;
|
|
7758
|
+
}
|
|
7759
|
+
return 0;
|
|
7760
|
+
}
|
|
7761
|
+
}
|
|
7762
|
+
|
|
5287
7763
|
var __defProp$3 = Object.defineProperty;
|
|
5288
7764
|
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5289
7765
|
var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
@@ -5295,7 +7771,8 @@ const _Context = class _Context {
|
|
|
5295
7771
|
timeframe,
|
|
5296
7772
|
limit,
|
|
5297
7773
|
sDate,
|
|
5298
|
-
eDate
|
|
7774
|
+
eDate,
|
|
7775
|
+
fullContext
|
|
5299
7776
|
}) {
|
|
5300
7777
|
__publicField$3(this, "data", {
|
|
5301
7778
|
open: new Series([]),
|
|
@@ -5305,7 +7782,8 @@ const _Context = class _Context {
|
|
|
5305
7782
|
volume: new Series([]),
|
|
5306
7783
|
hl2: new Series([]),
|
|
5307
7784
|
hlc3: new Series([]),
|
|
5308
|
-
ohlc4: new Series([])
|
|
7785
|
+
ohlc4: new Series([]),
|
|
7786
|
+
hlcc4: new Series([])
|
|
5309
7787
|
});
|
|
5310
7788
|
__publicField$3(this, "cache", {});
|
|
5311
7789
|
__publicField$3(this, "taState", {});
|
|
@@ -5330,6 +7808,7 @@ const _Context = class _Context {
|
|
|
5330
7808
|
__publicField$3(this, "limit");
|
|
5331
7809
|
__publicField$3(this, "sDate");
|
|
5332
7810
|
__publicField$3(this, "eDate");
|
|
7811
|
+
__publicField$3(this, "fullContext");
|
|
5333
7812
|
__publicField$3(this, "pineTSCode");
|
|
5334
7813
|
this.marketData = marketData;
|
|
5335
7814
|
this.source = source;
|
|
@@ -5338,6 +7817,7 @@ const _Context = class _Context {
|
|
|
5338
7817
|
this.limit = limit;
|
|
5339
7818
|
this.sDate = sDate;
|
|
5340
7819
|
this.eDate = eDate;
|
|
7820
|
+
this.fullContext = fullContext || this;
|
|
5341
7821
|
const core = new Core(this);
|
|
5342
7822
|
const coreFunctions = {
|
|
5343
7823
|
plotchar: core.plotchar.bind(core),
|
|
@@ -5353,14 +7833,33 @@ const _Context = class _Context {
|
|
|
5353
7833
|
math: new PineMath(this),
|
|
5354
7834
|
request: new PineRequest(this),
|
|
5355
7835
|
array: new PineArray(this),
|
|
7836
|
+
map: new PineMap(this),
|
|
7837
|
+
matrix: new PineMatrix(this),
|
|
5356
7838
|
na: coreFunctions.na,
|
|
5357
7839
|
plotchar: coreFunctions.plotchar,
|
|
5358
7840
|
color: coreFunctions.color,
|
|
5359
7841
|
plot: coreFunctions.plot,
|
|
5360
7842
|
nz: coreFunctions.nz,
|
|
7843
|
+
syminfo: null,
|
|
7844
|
+
timeframe: new Timeframe(this),
|
|
7845
|
+
//FIXME : this is a temporary solution to get the barstate values,
|
|
7846
|
+
//we need to implement a better way to handle realtime states
|
|
7847
|
+
barstate: new Barstate(this),
|
|
5361
7848
|
get bar_index() {
|
|
5362
7849
|
return _this.idx;
|
|
5363
|
-
}
|
|
7850
|
+
},
|
|
7851
|
+
get last_bar_index() {
|
|
7852
|
+
return _this.data.close.length - 1;
|
|
7853
|
+
},
|
|
7854
|
+
get last_bar_time() {
|
|
7855
|
+
return _this.data.openTime.get(_this.data.openTime.length - 1);
|
|
7856
|
+
},
|
|
7857
|
+
get timenow() {
|
|
7858
|
+
return (/* @__PURE__ */ new Date()).getTime();
|
|
7859
|
+
},
|
|
7860
|
+
log: new Log(this),
|
|
7861
|
+
str: new Str(this),
|
|
7862
|
+
...types
|
|
5364
7863
|
};
|
|
5365
7864
|
}
|
|
5366
7865
|
//#region [Runtime functions] ===========================
|
|
@@ -5380,10 +7879,10 @@ const _Context = class _Context {
|
|
|
5380
7879
|
if (Array.isArray(src[0])) {
|
|
5381
7880
|
value = src[0];
|
|
5382
7881
|
} else {
|
|
5383
|
-
value =
|
|
7882
|
+
value = src[src.length - 1 + idx];
|
|
5384
7883
|
}
|
|
5385
7884
|
} else {
|
|
5386
|
-
value =
|
|
7885
|
+
value = src;
|
|
5387
7886
|
}
|
|
5388
7887
|
if (!trg) {
|
|
5389
7888
|
return new Series([value]);
|
|
@@ -5431,9 +7930,9 @@ const _Context = class _Context {
|
|
|
5431
7930
|
* @param decimals - the number of decimals to precision to
|
|
5432
7931
|
* @returns the precision number
|
|
5433
7932
|
*/
|
|
5434
|
-
precision(
|
|
5435
|
-
|
|
5436
|
-
return
|
|
7933
|
+
precision(value, decimals = 10) {
|
|
7934
|
+
const epsilon = 10 ** decimals;
|
|
7935
|
+
return typeof value === "number" ? Math.round(value * epsilon) / epsilon : value;
|
|
5437
7936
|
}
|
|
5438
7937
|
/**
|
|
5439
7938
|
* This function is used to apply special transformation to internal PineTS parameters and handle them as time-series
|
|
@@ -5598,6 +8097,7 @@ class PineTS {
|
|
|
5598
8097
|
__publicField$2(this, "hl2", []);
|
|
5599
8098
|
__publicField$2(this, "hlc3", []);
|
|
5600
8099
|
__publicField$2(this, "ohlc4", []);
|
|
8100
|
+
__publicField$2(this, "hlcc4", []);
|
|
5601
8101
|
__publicField$2(this, "openTime", []);
|
|
5602
8102
|
__publicField$2(this, "closeTime", []);
|
|
5603
8103
|
//#endregion
|
|
@@ -5616,6 +8116,7 @@ class PineTS {
|
|
|
5616
8116
|
});
|
|
5617
8117
|
__publicField$2(this, "_transpiledCode", null);
|
|
5618
8118
|
__publicField$2(this, "_isSecondaryContext", false);
|
|
8119
|
+
__publicField$2(this, "_syminfo");
|
|
5619
8120
|
this._readyPromise = new Promise((resolve) => {
|
|
5620
8121
|
this.loadMarketData(source, tickerId, timeframe, limit, sDate, eDate).then((data) => {
|
|
5621
8122
|
const marketData = data;
|
|
@@ -5628,6 +8129,7 @@ class PineTS {
|
|
|
5628
8129
|
const _hlc3 = marketData.map((d) => (d.high + d.low + d.close) / 3);
|
|
5629
8130
|
const _hl2 = marketData.map((d) => (d.high + d.low) / 2);
|
|
5630
8131
|
const _ohlc4 = marketData.map((d) => (d.high + d.low + d.open + d.close) / 4);
|
|
8132
|
+
const _hlcc4 = marketData.map((d) => (d.high + d.low + d.close + d.close) / 4);
|
|
5631
8133
|
const _openTime = marketData.map((d) => d.openTime);
|
|
5632
8134
|
const _closeTime = marketData.map((d) => d.closeTime);
|
|
5633
8135
|
this.open = _open;
|
|
@@ -5638,10 +8140,23 @@ class PineTS {
|
|
|
5638
8140
|
this.hl2 = _hl2;
|
|
5639
8141
|
this.hlc3 = _hlc3;
|
|
5640
8142
|
this.ohlc4 = _ohlc4;
|
|
8143
|
+
this.hlcc4 = _hlcc4;
|
|
5641
8144
|
this.openTime = _openTime;
|
|
5642
8145
|
this.closeTime = _closeTime;
|
|
5643
|
-
|
|
5644
|
-
|
|
8146
|
+
if (source && source.getSymbolInfo) {
|
|
8147
|
+
source.getSymbolInfo(tickerId).then((symbolInfo2) => {
|
|
8148
|
+
this._syminfo = symbolInfo2;
|
|
8149
|
+
this._ready = true;
|
|
8150
|
+
resolve(true);
|
|
8151
|
+
}).catch((error) => {
|
|
8152
|
+
console.warn("Failed to get symbol info, using default values:", error);
|
|
8153
|
+
this._ready = true;
|
|
8154
|
+
resolve(true);
|
|
8155
|
+
});
|
|
8156
|
+
} else {
|
|
8157
|
+
this._ready = true;
|
|
8158
|
+
resolve(true);
|
|
8159
|
+
}
|
|
5645
8160
|
});
|
|
5646
8161
|
});
|
|
5647
8162
|
}
|
|
@@ -5731,6 +8246,7 @@ class PineTS {
|
|
|
5731
8246
|
}
|
|
5732
8247
|
this._removeLastResult(context);
|
|
5733
8248
|
processedUpToIdx = this.data.length - (newCandles + 1);
|
|
8249
|
+
context.pine.barstate.setLive();
|
|
5734
8250
|
}
|
|
5735
8251
|
}
|
|
5736
8252
|
/**
|
|
@@ -5760,7 +8276,8 @@ class PineTS {
|
|
|
5760
8276
|
timeframe: this.timeframe,
|
|
5761
8277
|
limit: this.limit,
|
|
5762
8278
|
sDate: this.sDate,
|
|
5763
|
-
eDate: this.eDate
|
|
8279
|
+
eDate: this.eDate,
|
|
8280
|
+
fullContext
|
|
5764
8281
|
});
|
|
5765
8282
|
pageContext.pineTSCode = fullContext.pineTSCode;
|
|
5766
8283
|
pageContext.idx = fullContext.idx;
|
|
@@ -5833,6 +8350,7 @@ class PineTS {
|
|
|
5833
8350
|
this.hl2[index] = (candle.high + candle.low) / 2;
|
|
5834
8351
|
this.hlc3[index] = (candle.high + candle.low + candle.close) / 3;
|
|
5835
8352
|
this.ohlc4[index] = (candle.high + candle.low + candle.open + candle.close) / 4;
|
|
8353
|
+
this.hlcc4[index] = (candle.high + candle.low + candle.close + candle.close) / 4;
|
|
5836
8354
|
this.openTime[index] = candle.openTime;
|
|
5837
8355
|
this.closeTime[index] = candle.closeTime;
|
|
5838
8356
|
}
|
|
@@ -5850,6 +8368,7 @@ class PineTS {
|
|
|
5850
8368
|
this.hl2.push((candle.high + candle.low) / 2);
|
|
5851
8369
|
this.hlc3.push((candle.high + candle.low + candle.close) / 3);
|
|
5852
8370
|
this.ohlc4.push((candle.high + candle.low + candle.open + candle.close) / 4);
|
|
8371
|
+
this.hlcc4.push((candle.high + candle.low + candle.close + candle.close) / 4);
|
|
5853
8372
|
this.openTime.push(candle.openTime);
|
|
5854
8373
|
this.closeTime.push(candle.closeTime);
|
|
5855
8374
|
}
|
|
@@ -5875,6 +8394,7 @@ class PineTS {
|
|
|
5875
8394
|
context.data.hl2.data.pop();
|
|
5876
8395
|
context.data.hlc3.data.pop();
|
|
5877
8396
|
context.data.ohlc4.data.pop();
|
|
8397
|
+
context.data.hlcc4.data.pop();
|
|
5878
8398
|
context.data.openTime.data.pop();
|
|
5879
8399
|
if (context.data.closeTime) {
|
|
5880
8400
|
context.data.closeTime.data.pop();
|
|
@@ -5894,6 +8414,7 @@ class PineTS {
|
|
|
5894
8414
|
sDate: this.sDate,
|
|
5895
8415
|
eDate: this.eDate
|
|
5896
8416
|
});
|
|
8417
|
+
context.pine.syminfo = this._syminfo;
|
|
5897
8418
|
context.pineTSCode = pineTSCode;
|
|
5898
8419
|
context.isSecondaryContext = isSecondary;
|
|
5899
8420
|
context.data.close = new Series([]);
|
|
@@ -5904,6 +8425,7 @@ class PineTS {
|
|
|
5904
8425
|
context.data.hl2 = new Series([]);
|
|
5905
8426
|
context.data.hlc3 = new Series([]);
|
|
5906
8427
|
context.data.ohlc4 = new Series([]);
|
|
8428
|
+
context.data.hlcc4 = new Series([]);
|
|
5907
8429
|
context.data.openTime = new Series([]);
|
|
5908
8430
|
context.data.closeTime = new Series([]);
|
|
5909
8431
|
return context;
|
|
@@ -5932,6 +8454,7 @@ class PineTS {
|
|
|
5932
8454
|
context.data.hl2.data.push(this.hl2[i]);
|
|
5933
8455
|
context.data.hlc3.data.push(this.hlc3[i]);
|
|
5934
8456
|
context.data.ohlc4.data.push(this.ohlc4[i]);
|
|
8457
|
+
context.data.hlcc4.data.push(this.hlcc4[i]);
|
|
5935
8458
|
context.data.openTime.data.push(this.openTime[i]);
|
|
5936
8459
|
context.data.closeTime.data.push(this.closeTime[i]);
|
|
5937
8460
|
const result = await transpiledFn(context);
|
|
@@ -6110,10 +8633,13 @@ class BinanceProvider {
|
|
|
6110
8633
|
}
|
|
6111
8634
|
async getMarketData(tickerId, timeframe, limit, sDate, eDate) {
|
|
6112
8635
|
try {
|
|
8636
|
+
const shouldCache = eDate !== void 0;
|
|
6113
8637
|
const cacheParams = { tickerId, timeframe, limit, sDate, eDate };
|
|
6114
|
-
|
|
6115
|
-
|
|
6116
|
-
|
|
8638
|
+
if (shouldCache) {
|
|
8639
|
+
const cachedData = this.cacheManager.get(cacheParams);
|
|
8640
|
+
if (cachedData) {
|
|
8641
|
+
return cachedData;
|
|
8642
|
+
}
|
|
6117
8643
|
}
|
|
6118
8644
|
const interval = timeframe_to_binance[timeframe.toUpperCase()];
|
|
6119
8645
|
if (!interval) {
|
|
@@ -6158,7 +8684,9 @@ class BinanceProvider {
|
|
|
6158
8684
|
ignore: item[11]
|
|
6159
8685
|
};
|
|
6160
8686
|
});
|
|
6161
|
-
|
|
8687
|
+
if (shouldCache) {
|
|
8688
|
+
this.cacheManager.set(cacheParams, data);
|
|
8689
|
+
}
|
|
6162
8690
|
return data;
|
|
6163
8691
|
} catch (error) {
|
|
6164
8692
|
console.error("Error in binance.klines:", error);
|
|
@@ -6195,6 +8723,106 @@ class BinanceProvider {
|
|
|
6195
8723
|
}
|
|
6196
8724
|
return false;
|
|
6197
8725
|
}
|
|
8726
|
+
async getSymbolInfo(tickerId) {
|
|
8727
|
+
try {
|
|
8728
|
+
let marketType = "crypto";
|
|
8729
|
+
let apiUrl = BINANCE_API_URL;
|
|
8730
|
+
let apiSymbol = tickerId;
|
|
8731
|
+
let contractType = "";
|
|
8732
|
+
if (tickerId.endsWith(".P")) {
|
|
8733
|
+
marketType = "futures";
|
|
8734
|
+
apiSymbol = tickerId.replace(".P", "");
|
|
8735
|
+
apiUrl = "https://fapi.binance.com/fapi/v1";
|
|
8736
|
+
contractType = "Perpetual";
|
|
8737
|
+
} else if (tickerId.includes("_")) {
|
|
8738
|
+
marketType = "futures";
|
|
8739
|
+
apiSymbol = tickerId;
|
|
8740
|
+
apiUrl = "https://dapi.binance.com/dapi/v1";
|
|
8741
|
+
contractType = "Delivery";
|
|
8742
|
+
}
|
|
8743
|
+
const url = marketType === "crypto" ? `${apiUrl}/exchangeInfo?symbol=${apiSymbol}` : `${apiUrl}/exchangeInfo`;
|
|
8744
|
+
const response = await fetch(url);
|
|
8745
|
+
if (!response.ok) {
|
|
8746
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
8747
|
+
}
|
|
8748
|
+
const result = await response.json();
|
|
8749
|
+
const symbols = result.symbols;
|
|
8750
|
+
if (!symbols || symbols.length === 0) {
|
|
8751
|
+
console.error(`Symbol ${tickerId} not found`);
|
|
8752
|
+
return null;
|
|
8753
|
+
}
|
|
8754
|
+
const symbolData = marketType === "futures" ? symbols.find((s) => s.symbol === apiSymbol) : symbols[0];
|
|
8755
|
+
if (!symbolData) {
|
|
8756
|
+
console.error(`Symbol ${apiSymbol} not found in exchange info`);
|
|
8757
|
+
return null;
|
|
8758
|
+
}
|
|
8759
|
+
const priceFilter = symbolData.filters?.find((f) => f.filterType === "PRICE_FILTER");
|
|
8760
|
+
const lotSizeFilter = symbolData.filters?.find((f) => f.filterType === "LOT_SIZE");
|
|
8761
|
+
const tickSize = priceFilter ? parseFloat(priceFilter.tickSize) : 0.01;
|
|
8762
|
+
const minQty = lotSizeFilter ? parseFloat(lotSizeFilter.minQty) : 0;
|
|
8763
|
+
const pricescale = Math.round(1 / tickSize);
|
|
8764
|
+
const baseAsset = symbolData.baseAsset;
|
|
8765
|
+
const quoteAsset = symbolData.quoteAsset;
|
|
8766
|
+
const typeLabel = contractType ? ` ${contractType}` : "";
|
|
8767
|
+
const description = `${baseAsset} / ${quoteAsset}${typeLabel}`;
|
|
8768
|
+
const symbolInfo = {
|
|
8769
|
+
// Symbol Identification
|
|
8770
|
+
ticker: tickerId,
|
|
8771
|
+
// KEEP ORIGINAL including .P if present!
|
|
8772
|
+
tickerid: `BINANCE:${tickerId}`,
|
|
8773
|
+
// Also keep .P here
|
|
8774
|
+
prefix: "BINANCE",
|
|
8775
|
+
root: baseAsset,
|
|
8776
|
+
// Just the base asset: "BTC"
|
|
8777
|
+
description,
|
|
8778
|
+
type: marketType,
|
|
8779
|
+
main_tickerid: `BINANCE:${tickerId}`,
|
|
8780
|
+
current_contract: contractType,
|
|
8781
|
+
isin: "",
|
|
8782
|
+
// Currency & Location
|
|
8783
|
+
basecurrency: baseAsset,
|
|
8784
|
+
currency: quoteAsset,
|
|
8785
|
+
timezone: "Etc/UTC",
|
|
8786
|
+
country: "",
|
|
8787
|
+
// Price & Contract Info
|
|
8788
|
+
mintick: tickSize,
|
|
8789
|
+
pricescale,
|
|
8790
|
+
minmove: 1,
|
|
8791
|
+
pointvalue: symbolData.contractSize || 1,
|
|
8792
|
+
mincontract: minQty,
|
|
8793
|
+
// Session & Market
|
|
8794
|
+
session: "24x7",
|
|
8795
|
+
volumetype: "base",
|
|
8796
|
+
expiration_date: symbolData.deliveryDate || 0,
|
|
8797
|
+
// Company Data (N/A for crypto)
|
|
8798
|
+
employees: 0,
|
|
8799
|
+
industry: "",
|
|
8800
|
+
sector: "",
|
|
8801
|
+
shareholders: 0,
|
|
8802
|
+
shares_outstanding_float: 0,
|
|
8803
|
+
shares_outstanding_total: 0,
|
|
8804
|
+
// Analyst Ratings (N/A for crypto)
|
|
8805
|
+
recommendations_buy: 0,
|
|
8806
|
+
recommendations_buy_strong: 0,
|
|
8807
|
+
recommendations_date: 0,
|
|
8808
|
+
recommendations_hold: 0,
|
|
8809
|
+
recommendations_sell: 0,
|
|
8810
|
+
recommendations_sell_strong: 0,
|
|
8811
|
+
recommendations_total: 0,
|
|
8812
|
+
// Price Targets (N/A for crypto)
|
|
8813
|
+
target_price_average: 0,
|
|
8814
|
+
target_price_date: 0,
|
|
8815
|
+
target_price_estimates: 0,
|
|
8816
|
+
target_price_high: 0,
|
|
8817
|
+
target_price_low: 0,
|
|
8818
|
+
target_price_median: 0
|
|
8819
|
+
};
|
|
8820
|
+
return symbolInfo;
|
|
8821
|
+
} catch (error) {
|
|
8822
|
+
console.error("Error in binance.exchangeInfo:", error);
|
|
8823
|
+
return null;
|
|
8824
|
+
}
|
|
8825
|
+
}
|
|
6198
8826
|
}
|
|
6199
8827
|
|
|
6200
8828
|
var __defProp = Object.defineProperty;
|
|
@@ -6205,6 +8833,7 @@ const __dirname = path.dirname(__filename);
|
|
|
6205
8833
|
class MockProvider {
|
|
6206
8834
|
constructor(dataDirectory) {
|
|
6207
8835
|
__publicField(this, "dataCache", /* @__PURE__ */ new Map());
|
|
8836
|
+
__publicField(this, "exchangeInfoCache", {});
|
|
6208
8837
|
__publicField(this, "dataDirectory");
|
|
6209
8838
|
if (dataDirectory) {
|
|
6210
8839
|
this.dataDirectory = dataDirectory;
|
|
@@ -6340,11 +8969,132 @@ class MockProvider {
|
|
|
6340
8969
|
return [];
|
|
6341
8970
|
}
|
|
6342
8971
|
}
|
|
8972
|
+
/**
|
|
8973
|
+
* Loads exchange info from JSON file
|
|
8974
|
+
*/
|
|
8975
|
+
loadExchangeInfo(type) {
|
|
8976
|
+
if (this.exchangeInfoCache[type]) {
|
|
8977
|
+
return this.exchangeInfoCache[type];
|
|
8978
|
+
}
|
|
8979
|
+
const fileName = type === "spot" ? "api-exchangeInfo.json" : "fapi-exchangeInfo.json";
|
|
8980
|
+
const filePath = path.join(this.dataDirectory, fileName);
|
|
8981
|
+
if (!fs.existsSync(filePath)) {
|
|
8982
|
+
console.warn(`Exchange info file not found: ${filePath}`);
|
|
8983
|
+
return null;
|
|
8984
|
+
}
|
|
8985
|
+
try {
|
|
8986
|
+
const fileContent = fs.readFileSync(filePath, "utf-8");
|
|
8987
|
+
const data = JSON.parse(fileContent);
|
|
8988
|
+
this.exchangeInfoCache[type] = data;
|
|
8989
|
+
return data;
|
|
8990
|
+
} catch (error) {
|
|
8991
|
+
console.error(`Error loading exchange info from ${fileName}:`, error);
|
|
8992
|
+
return null;
|
|
8993
|
+
}
|
|
8994
|
+
}
|
|
8995
|
+
/**
|
|
8996
|
+
* Implements IProvider.getSymbolInfo
|
|
8997
|
+
*
|
|
8998
|
+
* @param tickerId - Symbol name (e.g., 'BTCUSDT' or 'BTCUSDT.P')
|
|
8999
|
+
* @returns Promise<ISymbolInfo> - Symbol information
|
|
9000
|
+
*/
|
|
9001
|
+
async getSymbolInfo(tickerId) {
|
|
9002
|
+
try {
|
|
9003
|
+
let marketType = "crypto";
|
|
9004
|
+
let apiSymbol = tickerId;
|
|
9005
|
+
let contractType = "";
|
|
9006
|
+
if (tickerId.endsWith(".P")) {
|
|
9007
|
+
marketType = "futures";
|
|
9008
|
+
apiSymbol = tickerId.replace(".P", "");
|
|
9009
|
+
contractType = "Perpetual";
|
|
9010
|
+
} else if (tickerId.includes("_")) {
|
|
9011
|
+
marketType = "futures";
|
|
9012
|
+
apiSymbol = tickerId;
|
|
9013
|
+
contractType = "Delivery";
|
|
9014
|
+
}
|
|
9015
|
+
const exchangeInfo = this.loadExchangeInfo(marketType === "futures" ? "futures" : "spot");
|
|
9016
|
+
if (!exchangeInfo || !exchangeInfo.symbols) {
|
|
9017
|
+
console.error(`Exchange info not available for ${marketType}`);
|
|
9018
|
+
return null;
|
|
9019
|
+
}
|
|
9020
|
+
const symbolData = exchangeInfo.symbols.find((s) => s.symbol === apiSymbol);
|
|
9021
|
+
if (!symbolData) {
|
|
9022
|
+
console.error(`Symbol ${apiSymbol} not found in ${marketType} exchange info`);
|
|
9023
|
+
return null;
|
|
9024
|
+
}
|
|
9025
|
+
const priceFilter = symbolData.filters?.find((f) => f.filterType === "PRICE_FILTER");
|
|
9026
|
+
const lotSizeFilter = symbolData.filters?.find((f) => f.filterType === "LOT_SIZE");
|
|
9027
|
+
const tickSize = priceFilter ? parseFloat(priceFilter.tickSize) : 0.01;
|
|
9028
|
+
const minQty = lotSizeFilter ? parseFloat(lotSizeFilter.minQty) : 0;
|
|
9029
|
+
const pricescale = Math.round(1 / tickSize);
|
|
9030
|
+
const baseAsset = symbolData.baseAsset;
|
|
9031
|
+
const quoteAsset = symbolData.quoteAsset;
|
|
9032
|
+
const typeLabel = contractType ? ` ${contractType}` : "";
|
|
9033
|
+
const description = `${baseAsset} / ${quoteAsset}${typeLabel}`;
|
|
9034
|
+
const symbolInfo = {
|
|
9035
|
+
// Symbol Identification
|
|
9036
|
+
ticker: tickerId,
|
|
9037
|
+
// KEEP ORIGINAL including .P if present!
|
|
9038
|
+
tickerid: `BINANCE:${tickerId}`,
|
|
9039
|
+
// Also keep .P here
|
|
9040
|
+
prefix: "BINANCE",
|
|
9041
|
+
root: baseAsset,
|
|
9042
|
+
// Just the base asset: "BTC"
|
|
9043
|
+
description,
|
|
9044
|
+
type: marketType,
|
|
9045
|
+
main_tickerid: `BINANCE:${tickerId}`,
|
|
9046
|
+
current_contract: contractType,
|
|
9047
|
+
isin: "",
|
|
9048
|
+
// Currency & Location
|
|
9049
|
+
basecurrency: baseAsset,
|
|
9050
|
+
currency: quoteAsset,
|
|
9051
|
+
timezone: "Etc/UTC",
|
|
9052
|
+
country: "",
|
|
9053
|
+
// Price & Contract Info
|
|
9054
|
+
mintick: tickSize,
|
|
9055
|
+
pricescale,
|
|
9056
|
+
minmove: 1,
|
|
9057
|
+
pointvalue: symbolData.contractSize || 1,
|
|
9058
|
+
mincontract: minQty,
|
|
9059
|
+
// Session & Market
|
|
9060
|
+
session: "24x7",
|
|
9061
|
+
volumetype: "base",
|
|
9062
|
+
expiration_date: symbolData.deliveryDate || 0,
|
|
9063
|
+
// Company Data (N/A for crypto)
|
|
9064
|
+
employees: 0,
|
|
9065
|
+
industry: "",
|
|
9066
|
+
sector: "",
|
|
9067
|
+
shareholders: 0,
|
|
9068
|
+
shares_outstanding_float: 0,
|
|
9069
|
+
shares_outstanding_total: 0,
|
|
9070
|
+
// Analyst Ratings (N/A for crypto)
|
|
9071
|
+
recommendations_buy: 0,
|
|
9072
|
+
recommendations_buy_strong: 0,
|
|
9073
|
+
recommendations_date: 0,
|
|
9074
|
+
recommendations_hold: 0,
|
|
9075
|
+
recommendations_sell: 0,
|
|
9076
|
+
recommendations_sell_strong: 0,
|
|
9077
|
+
recommendations_total: 0,
|
|
9078
|
+
// Price Targets (N/A for crypto)
|
|
9079
|
+
target_price_average: 0,
|
|
9080
|
+
target_price_date: 0,
|
|
9081
|
+
target_price_estimates: 0,
|
|
9082
|
+
target_price_high: 0,
|
|
9083
|
+
target_price_low: 0,
|
|
9084
|
+
target_price_median: 0
|
|
9085
|
+
};
|
|
9086
|
+
return symbolInfo;
|
|
9087
|
+
} catch (error) {
|
|
9088
|
+
console.error("Error in MockProvider.getSymbolInfo:", error);
|
|
9089
|
+
return null;
|
|
9090
|
+
}
|
|
9091
|
+
}
|
|
6343
9092
|
/**
|
|
6344
9093
|
* Clears the data cache
|
|
6345
9094
|
*/
|
|
6346
9095
|
clearCache() {
|
|
6347
9096
|
this.dataCache.clear();
|
|
9097
|
+
this.exchangeInfoCache = {};
|
|
6348
9098
|
}
|
|
6349
9099
|
/**
|
|
6350
9100
|
* Sets a custom data directory
|