@cortex-js/compute-engine 0.63.0 → 0.64.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compile.esm.js +8 -3
- package/dist/compile.min.esm.js +7 -7
- package/dist/compile.min.umd.cjs +7 -7
- package/dist/compile.umd.cjs +8 -3
- package/dist/compute-engine.esm.js +1097 -106
- package/dist/compute-engine.min.esm.js +80 -80
- package/dist/compute-engine.min.umd.cjs +80 -80
- package/dist/compute-engine.umd.cjs +1097 -106
- package/dist/core.esm.js +1096 -105
- package/dist/core.min.esm.js +78 -78
- package/dist/core.min.umd.cjs +78 -78
- package/dist/core.umd.cjs +1096 -105
- package/dist/identities.esm.js +2 -7
- package/dist/identities.min.esm.js +2 -2
- package/dist/identities.min.umd.cjs +2 -2
- package/dist/identities.umd.cjs +2 -7
- package/dist/integration-rules.esm.js +3 -2
- package/dist/integration-rules.min.esm.js +4 -4
- package/dist/integration-rules.min.umd.cjs +4 -4
- package/dist/integration-rules.umd.cjs +3 -2
- package/dist/interval.esm.js +3 -2
- package/dist/interval.min.esm.js +4 -4
- package/dist/interval.min.umd.cjs +4 -4
- package/dist/interval.umd.cjs +3 -2
- package/dist/latex-syntax.esm.js +3 -2
- package/dist/latex-syntax.min.esm.js +6 -6
- package/dist/latex-syntax.min.umd.cjs +6 -6
- package/dist/latex-syntax.umd.cjs +3 -2
- package/dist/math-json.esm.js +2 -2
- package/dist/math-json.min.esm.js +2 -2
- package/dist/math-json.min.umd.cjs +2 -2
- package/dist/math-json.umd.cjs +2 -2
- package/dist/numerics.esm.js +53 -91
- package/dist/numerics.min.esm.js +4 -4
- package/dist/numerics.min.umd.cjs +4 -4
- package/dist/numerics.umd.cjs +53 -91
- package/dist/types/big-decimal/big-decimal.d.ts +1 -1
- package/dist/types/big-decimal/index.d.ts +1 -1
- package/dist/types/big-decimal/transcendentals.d.ts +1 -1
- package/dist/types/big-decimal/utils.d.ts +1 -1
- package/dist/types/common/ansi-codes.d.ts +1 -1
- package/dist/types/common/configuration-change.d.ts +1 -1
- package/dist/types/common/fuzzy-string-match.d.ts +1 -1
- package/dist/types/common/grapheme-splitter.d.ts +1 -1
- package/dist/types/common/interruptible.d.ts +1 -1
- package/dist/types/common/one-of.d.ts +1 -1
- package/dist/types/common/signals.d.ts +1 -1
- package/dist/types/common/type/ast-nodes.d.ts +1 -1
- package/dist/types/common/type/boxed-type.d.ts +1 -1
- package/dist/types/common/type/lexer.d.ts +1 -1
- package/dist/types/common/type/parse.d.ts +1 -1
- package/dist/types/common/type/parser.d.ts +1 -1
- package/dist/types/common/type/primitive.d.ts +1 -1
- package/dist/types/common/type/reduce.d.ts +1 -1
- package/dist/types/common/type/serialize.d.ts +1 -1
- package/dist/types/common/type/subtype.d.ts +1 -1
- package/dist/types/common/type/type-builder.d.ts +1 -1
- package/dist/types/common/type/types.d.ts +1 -1
- package/dist/types/common/type/utils.d.ts +1 -1
- package/dist/types/common/utils.d.ts +1 -1
- package/dist/types/compile.d.ts +1 -1
- package/dist/types/compute-engine/assume.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-operator-definition.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/constants.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/constraint-subject.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/exp-to-trig.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/factor.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/hold.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/inequality-bounds.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/init-lazy-refs.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/invisible-operator.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/multivariate-gcd.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/multivariate-poly.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/numerics.d.ts +11 -5
- package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/pattern-utils.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/polynomial-degree.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/predicates.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/rule-index.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/solve-linear-system.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/stochastic-equal.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/type-guards.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
- package/dist/types/compute-engine/collection-utils.d.ts +1 -1
- package/dist/types/compute-engine/compilation/base-compiler.d.ts +1 -1
- package/dist/types/compute-engine/compilation/compile-expression.d.ts +1 -1
- package/dist/types/compute-engine/compilation/constant-folding.d.ts +1 -1
- package/dist/types/compute-engine/compilation/glsl-target.d.ts +1 -1
- package/dist/types/compute-engine/compilation/gpu-target.d.ts +1 -1
- package/dist/types/compute-engine/compilation/interval-glsl-target.d.ts +1 -1
- package/dist/types/compute-engine/compilation/interval-javascript-target.d.ts +1 -1
- package/dist/types/compute-engine/compilation/javascript-target.d.ts +1 -1
- package/dist/types/compute-engine/compilation/python-target.d.ts +1 -1
- package/dist/types/compute-engine/compilation/types.d.ts +1 -1
- package/dist/types/compute-engine/compilation/wgsl-target.d.ts +1 -1
- package/dist/types/compute-engine/cost-function.d.ts +1 -1
- package/dist/types/compute-engine/engine-assumptions.d.ts +1 -1
- package/dist/types/compute-engine/engine-cache.d.ts +1 -1
- package/dist/types/compute-engine/engine-common-symbols.d.ts +1 -1
- package/dist/types/compute-engine/engine-compilation-targets.d.ts +1 -1
- package/dist/types/compute-engine/engine-configuration-lifecycle.d.ts +1 -1
- package/dist/types/compute-engine/engine-declarations.d.ts +1 -1
- package/dist/types/compute-engine/engine-expression-entrypoints.d.ts +1 -1
- package/dist/types/compute-engine/engine-extension-contracts.d.ts +1 -1
- package/dist/types/compute-engine/engine-library-bootstrap.d.ts +1 -1
- package/dist/types/compute-engine/engine-numeric-configuration.d.ts +1 -1
- package/dist/types/compute-engine/engine-runtime-state.d.ts +1 -1
- package/dist/types/compute-engine/engine-scope.d.ts +1 -1
- package/dist/types/compute-engine/engine-sequences.d.ts +1 -1
- package/dist/types/compute-engine/engine-simplification-rules.d.ts +1 -1
- package/dist/types/compute-engine/engine-startup-coordinator.d.ts +1 -1
- package/dist/types/compute-engine/engine-type-resolver.d.ts +1 -1
- package/dist/types/compute-engine/engine-validation-entrypoints.d.ts +1 -1
- package/dist/types/compute-engine/free-functions.d.ts +1 -1
- package/dist/types/compute-engine/function-properties/index.d.ts +1 -1
- package/dist/types/compute-engine/function-utils.d.ts +1 -1
- package/dist/types/compute-engine/fungrim/loader.d.ts +1 -1
- package/dist/types/compute-engine/fungrim/types.d.ts +1 -1
- package/dist/types/compute-engine/global-types.d.ts +1 -1
- package/dist/types/compute-engine/index.d.ts +1 -1
- package/dist/types/compute-engine/interval/arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/interval/comparison.d.ts +1 -1
- package/dist/types/compute-engine/interval/elementary.d.ts +1 -1
- package/dist/types/compute-engine/interval/index.d.ts +1 -1
- package/dist/types/compute-engine/interval/trigonometric.d.ts +1 -1
- package/dist/types/compute-engine/interval/types.d.ts +1 -1
- package/dist/types/compute-engine/interval/util.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/default-dictionary.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-colors.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-units.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/indexed-types.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/parse-number.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/serialize-dms.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/types.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
- package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/library/calculus.d.ts +1 -1
- package/dist/types/compute-engine/library/collections.d.ts +1 -1
- package/dist/types/compute-engine/library/colors.d.ts +1 -1
- package/dist/types/compute-engine/library/combinatorics.d.ts +1 -1
- package/dist/types/compute-engine/library/complex.d.ts +1 -1
- package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
- package/dist/types/compute-engine/library/core.d.ts +1 -1
- package/dist/types/compute-engine/library/fractals.d.ts +1 -1
- package/dist/types/compute-engine/library/library.d.ts +1 -1
- package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/library/logic-analysis.d.ts +1 -1
- package/dist/types/compute-engine/library/logic.d.ts +1 -1
- package/dist/types/compute-engine/library/number-theory.d.ts +1 -1
- package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
- package/dist/types/compute-engine/library/quantity-arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
- package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
- package/dist/types/compute-engine/library/sets.d.ts +1 -1
- package/dist/types/compute-engine/library/special-functions.d.ts +1 -1
- package/dist/types/compute-engine/library/statistics.d.ts +1 -1
- package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
- package/dist/types/compute-engine/library/type-handlers.d.ts +1 -1
- package/dist/types/compute-engine/library/unit-data.d.ts +1 -1
- package/dist/types/compute-engine/library/units.d.ts +1 -1
- package/dist/types/compute-engine/library/utils.d.ts +1 -1
- package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
- package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
- package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
- package/dist/types/compute-engine/numeric-value/types.d.ts +1 -1
- package/dist/types/compute-engine/numerics/bernoulli.d.ts +1 -1
- package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
- package/dist/types/compute-engine/numerics/complex-esm-augment.d.ts +1 -1
- package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
- package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
- package/dist/types/compute-engine/numerics/linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
- package/dist/types/compute-engine/numerics/oscillatory-quadrature.d.ts +1 -1
- package/dist/types/compute-engine/numerics/polynomial-roots.d.ts +1 -1
- package/dist/types/compute-engine/numerics/primes.d.ts +5 -3
- package/dist/types/compute-engine/numerics/random.d.ts +1 -1
- package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
- package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
- package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
- package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
- package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
- package/dist/types/compute-engine/numerics/types.d.ts +1 -1
- package/dist/types/compute-engine/numerics/unit-data.d.ts +1 -1
- package/dist/types/compute-engine/oeis.d.ts +1 -1
- package/dist/types/compute-engine/rubi/compile.d.ts +1 -1
- package/dist/types/compute-engine/rubi/driver.d.ts +1 -1
- package/dist/types/compute-engine/rubi/loader.d.ts +1 -1
- package/dist/types/compute-engine/rubi/match.d.ts +1 -1
- package/dist/types/compute-engine/rubi/normal-form.d.ts +1 -1
- package/dist/types/compute-engine/rubi/rubi-utils.d.ts +1 -1
- package/dist/types/compute-engine/rubi/types.d.ts +1 -1
- package/dist/types/compute-engine/sequence.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/fu-cost.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/fu.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/limit.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/logic-utils.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/residue.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-factorial.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-log.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-logic.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-power.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +1 -1
- package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
- package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
- package/dist/types/compute-engine/types-definitions.d.ts +1 -1
- package/dist/types/compute-engine/types-engine.d.ts +1 -1
- package/dist/types/compute-engine/types-evaluation.d.ts +1 -1
- package/dist/types/compute-engine/types-expression.d.ts +1 -1
- package/dist/types/compute-engine/types-kernel-evaluation.d.ts +1 -1
- package/dist/types/compute-engine/types-kernel-serialization.d.ts +1 -1
- package/dist/types/compute-engine/types-serialization.d.ts +1 -1
- package/dist/types/compute-engine/types.d.ts +1 -1
- package/dist/types/compute-engine.d.ts +1 -1
- package/dist/types/core.d.ts +1 -1
- package/dist/types/identities.d.ts +1 -1
- package/dist/types/integration-rules.d.ts +1 -1
- package/dist/types/interval.d.ts +1 -1
- package/dist/types/latex-syntax.d.ts +2 -2
- package/dist/types/math-json/symbols.d.ts +1 -1
- package/dist/types/math-json/types.d.ts +1 -1
- package/dist/types/math-json/utils.d.ts +1 -1
- package/dist/types/math-json.d.ts +2 -2
- package/dist/types/numerics.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Compute Engine 0.
|
|
1
|
+
/** Compute Engine 0.64.0 */
|
|
2
2
|
|
|
3
3
|
// node_modules/complex-esm/dist/src/complex.js
|
|
4
4
|
var cosh = Math.cosh || function(x) {
|
|
@@ -5277,7 +5277,6 @@ function bigint(a) {
|
|
|
5277
5277
|
}
|
|
5278
5278
|
|
|
5279
5279
|
// src/compute-engine/numerics/primes.ts
|
|
5280
|
-
var LARGE_PRIME = 1125899906842597;
|
|
5281
5280
|
var SMALL_PRIMES = /* @__PURE__ */ new Set([
|
|
5282
5281
|
2,
|
|
5283
5282
|
3,
|
|
@@ -6324,102 +6323,60 @@ function primeFactors(n) {
|
|
|
6324
6323
|
else result[n] = 1;
|
|
6325
6324
|
return result;
|
|
6326
6325
|
}
|
|
6327
|
-
|
|
6328
|
-
|
|
6329
|
-
|
|
6330
|
-
|
|
6331
|
-
|
|
6332
|
-
|
|
6333
|
-
|
|
6334
|
-
|
|
6335
|
-
|
|
6336
|
-
|
|
6337
|
-
|
|
6338
|
-
|
|
6339
|
-
|
|
6340
|
-
|
|
6341
|
-
|
|
6342
|
-
if (n % 5 === 0) return 5;
|
|
6343
|
-
const m = Math.floor(Math.sqrt(n));
|
|
6344
|
-
let i = 7;
|
|
6345
|
-
while (i <= m) {
|
|
6346
|
-
if (n % i === 0) return i;
|
|
6347
|
-
if (n % (i + 4) === 0) return i + 4;
|
|
6348
|
-
if (n % (i + 6) === 0) return i + 6;
|
|
6349
|
-
if (n % (i + 10) === 0) return i + 10;
|
|
6350
|
-
if (n % (i + 12) === 0) return i + 12;
|
|
6351
|
-
if (n % (i + 16) === 0) return i + 16;
|
|
6352
|
-
if (n % (i + 22) === 0) return i + 22;
|
|
6353
|
-
if (n % (i + 24) === 0) return i + 24;
|
|
6354
|
-
i += 30;
|
|
6355
|
-
}
|
|
6356
|
-
return n;
|
|
6357
|
-
}
|
|
6326
|
+
var MILLER_RABIN_THRESHOLD = 1n << 32n;
|
|
6327
|
+
var MILLER_RABIN_BASES = [
|
|
6328
|
+
2n,
|
|
6329
|
+
3n,
|
|
6330
|
+
5n,
|
|
6331
|
+
7n,
|
|
6332
|
+
11n,
|
|
6333
|
+
13n,
|
|
6334
|
+
17n,
|
|
6335
|
+
19n,
|
|
6336
|
+
23n,
|
|
6337
|
+
29n,
|
|
6338
|
+
31n,
|
|
6339
|
+
37n
|
|
6340
|
+
];
|
|
6358
6341
|
function isPrimeBigint(n) {
|
|
6359
|
-
if (n
|
|
6360
|
-
if (n <= LARGEST_SMALL_PRIME) return
|
|
6361
|
-
|
|
6362
|
-
if (n %
|
|
6363
|
-
|
|
6364
|
-
|
|
6365
|
-
|
|
6366
|
-
}
|
|
6367
|
-
function leastBigFactor(n) {
|
|
6368
|
-
if (n === BigInt(1)) return BigInt(1);
|
|
6369
|
-
if (n % BigInt(2) === BigInt(0)) return BigInt(2);
|
|
6370
|
-
if (n % BigInt(3) === BigInt(0)) return BigInt(3);
|
|
6371
|
-
if (n % BigInt(5) === BigInt(0)) return BigInt(5);
|
|
6372
|
-
const m = BigInt(Math.floor(Math.sqrt(Number(n))));
|
|
6373
|
-
let i = BigInt(7);
|
|
6374
|
-
while (i <= m) {
|
|
6375
|
-
if (n % i === BigInt(0)) return i;
|
|
6376
|
-
if (n % (i + BigInt(4)) === BigInt(0)) return i + BigInt(4);
|
|
6377
|
-
if (n % (i + BigInt(6)) === BigInt(0)) return i + BigInt(6);
|
|
6378
|
-
if (n % (i + BigInt(10)) === BigInt(0)) return i + BigInt(10);
|
|
6379
|
-
if (n % (i + BigInt(12)) === BigInt(0)) return i + BigInt(12);
|
|
6380
|
-
if (n % (i + BigInt(16)) === BigInt(0)) return i + BigInt(16);
|
|
6381
|
-
if (n % (i + BigInt(22)) === BigInt(0)) return i + BigInt(22);
|
|
6382
|
-
if (n % (i + BigInt(24)) === BigInt(0)) return i + BigInt(24);
|
|
6383
|
-
i += BigInt(30);
|
|
6342
|
+
if (n < 2n) return false;
|
|
6343
|
+
if (n <= LARGEST_SMALL_PRIME) return SMALL_PRIMES.has(Number(n));
|
|
6344
|
+
if (n < MILLER_RABIN_THRESHOLD) {
|
|
6345
|
+
if (n % 2n === 0n || n % 3n === 0n) return false;
|
|
6346
|
+
for (let i = 5n; i * i <= n; i += 6n)
|
|
6347
|
+
if (n % i === 0n || n % (i + 2n) === 0n) return false;
|
|
6348
|
+
return true;
|
|
6384
6349
|
}
|
|
6385
|
-
return n;
|
|
6350
|
+
return millerRabin(n);
|
|
6386
6351
|
}
|
|
6387
|
-
function
|
|
6388
|
-
|
|
6389
|
-
let
|
|
6390
|
-
|
|
6391
|
-
|
|
6392
|
-
|
|
6393
|
-
|
|
6394
|
-
|
|
6395
|
-
|
|
6396
|
-
|
|
6397
|
-
for (let i = s - 1; i--; ) {
|
|
6398
|
-
x = x * x % n;
|
|
6399
|
-
if (x === 1) return false;
|
|
6400
|
-
if (x === n - 1) continue WitnessLoop;
|
|
6401
|
-
}
|
|
6402
|
-
return false;
|
|
6403
|
-
} while (--k);
|
|
6404
|
-
return true;
|
|
6352
|
+
function modPow(base, exp3, mod3) {
|
|
6353
|
+
if (mod3 === 1n) return 0n;
|
|
6354
|
+
let result = 1n;
|
|
6355
|
+
base = (base % mod3 + mod3) % mod3;
|
|
6356
|
+
while (exp3 > 0n) {
|
|
6357
|
+
if (exp3 & 1n) result = result * base % mod3;
|
|
6358
|
+
base = base * base % mod3;
|
|
6359
|
+
exp3 >>= 1n;
|
|
6360
|
+
}
|
|
6361
|
+
return result;
|
|
6405
6362
|
}
|
|
6406
|
-
function
|
|
6407
|
-
|
|
6408
|
-
let d = n -
|
|
6409
|
-
|
|
6410
|
-
|
|
6411
|
-
|
|
6412
|
-
|
|
6413
|
-
|
|
6414
|
-
|
|
6415
|
-
|
|
6416
|
-
|
|
6363
|
+
function millerRabin(n) {
|
|
6364
|
+
for (const p of MILLER_RABIN_BASES) if (n % p === 0n) return n === p;
|
|
6365
|
+
let d = n - 1n;
|
|
6366
|
+
let s = 0n;
|
|
6367
|
+
while (d % 2n === 0n) {
|
|
6368
|
+
d /= 2n;
|
|
6369
|
+
s += 1n;
|
|
6370
|
+
}
|
|
6371
|
+
WitnessLoop: for (const a of MILLER_RABIN_BASES) {
|
|
6372
|
+
let x = modPow(a, d, n);
|
|
6373
|
+
if (x === 1n || x === n - 1n) continue;
|
|
6374
|
+
for (let r = 1n; r < s; r++) {
|
|
6417
6375
|
x = x * x % n;
|
|
6418
|
-
if (x ===
|
|
6419
|
-
if (x === n - BigInt(1)) continue WitnessLoop;
|
|
6376
|
+
if (x === n - 1n) continue WitnessLoop;
|
|
6420
6377
|
}
|
|
6421
6378
|
return false;
|
|
6422
|
-
}
|
|
6379
|
+
}
|
|
6423
6380
|
return true;
|
|
6424
6381
|
}
|
|
6425
6382
|
var PRIME_WHEEL_INC = [
|
|
@@ -6432,6 +6389,63 @@ var PRIME_WHEEL_INC = [
|
|
|
6432
6389
|
BigInt(2),
|
|
6433
6390
|
BigInt(6)
|
|
6434
6391
|
];
|
|
6392
|
+
function bigPrimeFactors(d) {
|
|
6393
|
+
if (d < Number.MAX_SAFE_INTEGER) {
|
|
6394
|
+
const factors = primeFactors(Number(d));
|
|
6395
|
+
const result2 = /* @__PURE__ */ new Map();
|
|
6396
|
+
for (const f of Object.keys(factors))
|
|
6397
|
+
result2.set(bigint(f), factors[Number(f)]);
|
|
6398
|
+
return result2;
|
|
6399
|
+
}
|
|
6400
|
+
let n = d;
|
|
6401
|
+
const result = /* @__PURE__ */ new Map();
|
|
6402
|
+
let count2 = 0;
|
|
6403
|
+
let count3 = 0;
|
|
6404
|
+
let count5 = 0;
|
|
6405
|
+
let k = BigInt(10);
|
|
6406
|
+
while (n % k === BigInt(0)) {
|
|
6407
|
+
count2 += 1;
|
|
6408
|
+
count5 += 1;
|
|
6409
|
+
n = n / k;
|
|
6410
|
+
}
|
|
6411
|
+
k = BigInt(5);
|
|
6412
|
+
while (n % k === BigInt(0)) {
|
|
6413
|
+
count5 += 1;
|
|
6414
|
+
n = n / k;
|
|
6415
|
+
}
|
|
6416
|
+
k = BigInt(3);
|
|
6417
|
+
while (n % k === BigInt(0)) {
|
|
6418
|
+
count3 += 1;
|
|
6419
|
+
n = n / k;
|
|
6420
|
+
}
|
|
6421
|
+
k = BigInt(2);
|
|
6422
|
+
while (n % k === BigInt(0)) {
|
|
6423
|
+
count2 += 1;
|
|
6424
|
+
n = n / k;
|
|
6425
|
+
}
|
|
6426
|
+
if (count2 > 0) result.set("2", count2);
|
|
6427
|
+
if (count3 > 0) result.set("3", count3);
|
|
6428
|
+
if (count5 > 0) result.set("5", count5);
|
|
6429
|
+
k = BigInt(7);
|
|
6430
|
+
let kIndex = "";
|
|
6431
|
+
let i = 0;
|
|
6432
|
+
while (k * k < n) {
|
|
6433
|
+
if (n % k === BigInt(0)) {
|
|
6434
|
+
if (!kIndex) kIndex = k.toString();
|
|
6435
|
+
result.set(kIndex, (result.get(kIndex) ?? 0) + 1);
|
|
6436
|
+
n = n / k;
|
|
6437
|
+
} else {
|
|
6438
|
+
k = k + PRIME_WHEEL_INC[i];
|
|
6439
|
+
kIndex = "";
|
|
6440
|
+
i = i < 7 ? i + 1 : 0;
|
|
6441
|
+
}
|
|
6442
|
+
}
|
|
6443
|
+
if (n !== BigInt(1))
|
|
6444
|
+
result.set(n.toString(), (result.get(n.toString()) ?? 0) + 1);
|
|
6445
|
+
const r = /* @__PURE__ */ new Map();
|
|
6446
|
+
for (const [k2, v] of result) r.set(bigint(k2), v);
|
|
6447
|
+
return r;
|
|
6448
|
+
}
|
|
6435
6449
|
|
|
6436
6450
|
// src/compute-engine/numerics/numeric.ts
|
|
6437
6451
|
var DEFAULT_PRECISION = 21;
|
|
@@ -25273,7 +25287,11 @@ function asSmallInteger(expr2) {
|
|
|
25273
25287
|
function toInteger(expr2) {
|
|
25274
25288
|
if (!isNumber(expr2)) return null;
|
|
25275
25289
|
const num = expr2.numericValue;
|
|
25276
|
-
|
|
25290
|
+
const re = typeof num === "number" ? num : num.re;
|
|
25291
|
+
if (!Number.isFinite(re)) return null;
|
|
25292
|
+
const n = Math.round(re);
|
|
25293
|
+
if (!Number.isSafeInteger(n)) return null;
|
|
25294
|
+
return n;
|
|
25277
25295
|
}
|
|
25278
25296
|
function toBigint(expr2) {
|
|
25279
25297
|
if (expr2 === void 0 || expr2 === null) return null;
|
|
@@ -31892,12 +31910,10 @@ function bigHypergeometric1F1(ce, a, b, z) {
|
|
|
31892
31910
|
}
|
|
31893
31911
|
|
|
31894
31912
|
// src/compute-engine/boxed-expression/predicates.ts
|
|
31895
|
-
function
|
|
31913
|
+
function isPrime(expr2) {
|
|
31896
31914
|
if (!expr2.isInteger) return void 0;
|
|
31897
31915
|
if (expr2.isNegative) return void 0;
|
|
31898
31916
|
if (!isNumber(expr2)) return void 0;
|
|
31899
|
-
const n = toInteger(expr2);
|
|
31900
|
-
if (n !== null) return isPrime(n);
|
|
31901
31917
|
const b = asBigint(expr2);
|
|
31902
31918
|
if (b !== null) return isPrimeBigint(b);
|
|
31903
31919
|
return void 0;
|
|
@@ -37869,6 +37885,7 @@ var ARITHMETIC_LIBRARY = [
|
|
|
37869
37885
|
sgn: ([x]) => x.isNonNegative ? "positive" : x.isNegative || x.isReal === false ? "unsigned" : void 0,
|
|
37870
37886
|
evaluate: (ops) => {
|
|
37871
37887
|
const x = ops[0];
|
|
37888
|
+
if (x.isInteger !== true) return void 0;
|
|
37872
37889
|
const n = toInteger(x);
|
|
37873
37890
|
if (n === null) return void 0;
|
|
37874
37891
|
const ce = x.engine;
|
|
@@ -38822,7 +38839,7 @@ var ARITHMETIC_LIBRARY = [
|
|
|
38822
38839
|
broadcastable: true,
|
|
38823
38840
|
signature: "(number) -> boolean",
|
|
38824
38841
|
evaluate: ([n], { engine }) => {
|
|
38825
|
-
const result =
|
|
38842
|
+
const result = isPrime(n);
|
|
38826
38843
|
if (result === void 0) return void 0;
|
|
38827
38844
|
return engine.symbol(result ? "True" : "False");
|
|
38828
38845
|
}
|
|
@@ -39504,8 +39521,8 @@ var CONDITIONS = {
|
|
|
39504
39521
|
nonpositive: (x) => x.isNonPositive,
|
|
39505
39522
|
even: (x) => x.isEven,
|
|
39506
39523
|
odd: (x) => x.isOdd,
|
|
39507
|
-
prime: (x) =>
|
|
39508
|
-
composite: (x) =>
|
|
39524
|
+
prime: (x) => isPrime(x) === true,
|
|
39525
|
+
composite: (x) => isPrime(x) === false,
|
|
39509
39526
|
notzero: (x) => x.isSame(0) === false,
|
|
39510
39527
|
notone: (x) => x.isSame(1) === false,
|
|
39511
39528
|
finite: (x) => x.isFinite,
|
|
@@ -56524,6 +56541,7 @@ var COMBINATORICS_LIBRARY = [
|
|
|
56524
56541
|
signature: "(integer) -> integer",
|
|
56525
56542
|
type: () => "finite_integer",
|
|
56526
56543
|
evaluate: ([n], { engine: ce }) => {
|
|
56544
|
+
if (n.isInteger !== true) return void 0;
|
|
56527
56545
|
const k = toInteger(n);
|
|
56528
56546
|
if (k === null || k < 0) return void 0;
|
|
56529
56547
|
let result = 1n;
|
|
@@ -56541,6 +56559,7 @@ var COMBINATORICS_LIBRARY = [
|
|
|
56541
56559
|
signature: "(integer) -> integer",
|
|
56542
56560
|
type: () => "finite_integer",
|
|
56543
56561
|
evaluate: ([n], { engine: ce }) => {
|
|
56562
|
+
if (n.isInteger !== true) return void 0;
|
|
56544
56563
|
const k = toInteger(n);
|
|
56545
56564
|
if (k === null || k < 0) return void 0;
|
|
56546
56565
|
let row = [1n];
|
|
@@ -58120,17 +58139,35 @@ var CORE_LIBRARY = [
|
|
|
58120
58139
|
}
|
|
58121
58140
|
},
|
|
58122
58141
|
N: {
|
|
58123
|
-
description:
|
|
58142
|
+
description: [
|
|
58143
|
+
"N(expr): numerically evaluate an expression",
|
|
58144
|
+
"N(expr, precision): evaluate to `precision` significant digits"
|
|
58145
|
+
],
|
|
58124
58146
|
lazy: true,
|
|
58125
|
-
signature: "(any) -> unknown",
|
|
58147
|
+
signature: "(any, integer?) -> unknown",
|
|
58126
58148
|
type: ([x]) => x.type,
|
|
58127
58149
|
canonical: (ops, { engine: ce }) => {
|
|
58128
|
-
if (ops.length
|
|
58129
|
-
|
|
58130
|
-
if (
|
|
58150
|
+
if (ops.length === 0) return ce._fn("N", checkArity(ce, ops, 1));
|
|
58151
|
+
if (ops.length > 2) return ce._fn("N", checkArity(ce, ops, 2));
|
|
58152
|
+
if (ops.length === 1) {
|
|
58153
|
+
const h = ops[0].operator;
|
|
58154
|
+
if (h === "N" || h === "Evaluate") return ops[0].canonical;
|
|
58155
|
+
}
|
|
58131
58156
|
return ce._fn("N", ops);
|
|
58132
58157
|
},
|
|
58133
|
-
evaluate: (
|
|
58158
|
+
evaluate: (ops, { engine: ce }) => {
|
|
58159
|
+
const x = ops[0];
|
|
58160
|
+
if (ops.length < 2) return x.canonical.N();
|
|
58161
|
+
let p = ops[1].canonical.N().re;
|
|
58162
|
+
if (!Number.isFinite(p) || p < 1) return x.canonical.N();
|
|
58163
|
+
p = Math.min(Math.trunc(p), 1e3);
|
|
58164
|
+
const global = ce.precision;
|
|
58165
|
+
if (p > global) {
|
|
58166
|
+
ce.precision = p;
|
|
58167
|
+
return x.canonical.N();
|
|
58168
|
+
}
|
|
58169
|
+
return roundToSignificantDigits(x.canonical.N(), p);
|
|
58170
|
+
}
|
|
58134
58171
|
},
|
|
58135
58172
|
Random: {
|
|
58136
58173
|
description: [
|
|
@@ -58635,6 +58672,20 @@ var CORE_LIBRARY = [
|
|
|
58635
58672
|
}
|
|
58636
58673
|
}
|
|
58637
58674
|
];
|
|
58675
|
+
function roundToSignificantDigits(value, p) {
|
|
58676
|
+
const ce = value.engine;
|
|
58677
|
+
const re = value.re;
|
|
58678
|
+
const im = value.im;
|
|
58679
|
+
if (!Number.isFinite(re) || !Number.isFinite(im)) return value;
|
|
58680
|
+
if (im !== 0) {
|
|
58681
|
+
const clamp2 = Math.min(p, 100);
|
|
58682
|
+
return ce.number(
|
|
58683
|
+
ce.complex(Number(re.toPrecision(clamp2)), Number(im.toPrecision(clamp2)))
|
|
58684
|
+
);
|
|
58685
|
+
}
|
|
58686
|
+
const bd = value.bignumRe ?? ce.bignum(re);
|
|
58687
|
+
return ce.number(bd.toPrecision(p));
|
|
58688
|
+
}
|
|
58638
58689
|
|
|
58639
58690
|
// src/compute-engine/library/linear-algebra.ts
|
|
58640
58691
|
var LINEAR_ALGEBRA_LIBRARY = [
|
|
@@ -59093,6 +59144,165 @@ var LINEAR_ALGEBRA_LIBRARY = [
|
|
|
59093
59144
|
return void 0;
|
|
59094
59145
|
}
|
|
59095
59146
|
},
|
|
59147
|
+
Dot: {
|
|
59148
|
+
description: "Dot product (vector inner product) or matrix product.",
|
|
59149
|
+
complexity: 8300,
|
|
59150
|
+
signature: "(matrix|vector, matrix|vector) -> value",
|
|
59151
|
+
// `Dot` is Mathematica's `.`: it reduces to the inner product for two
|
|
59152
|
+
// vectors and to the matrix product otherwise — exactly what
|
|
59153
|
+
// `MatrixMultiply` already computes.
|
|
59154
|
+
evaluate: (ops, { engine: ce }) => ce.function("MatrixMultiply", ops).evaluate()
|
|
59155
|
+
},
|
|
59156
|
+
MatrixRank: {
|
|
59157
|
+
description: "Rank of a matrix (number of linearly independent rows/columns).",
|
|
59158
|
+
complexity: 8200,
|
|
59159
|
+
signature: "(value) -> integer",
|
|
59160
|
+
sgn: () => "non-negative",
|
|
59161
|
+
evaluate: ([map], { engine: ce }) => {
|
|
59162
|
+
const op = map.evaluate();
|
|
59163
|
+
if (op.isNumber) return ce.number(op.isSame(0) ? 0 : 1);
|
|
59164
|
+
if (!isTensor2(op)) return void 0;
|
|
59165
|
+
const shape = op.shape;
|
|
59166
|
+
if (shape.length > 2) return ce.error("expected-matrix", op.toString());
|
|
59167
|
+
const rowCount = shape.length === 1 ? 1 : shape[0];
|
|
59168
|
+
const columnCount = shape.length === 1 ? shape[0] : shape[1];
|
|
59169
|
+
const matrix = tensorToNumericMatrix(op, rowCount, columnCount);
|
|
59170
|
+
if (!matrix) return void 0;
|
|
59171
|
+
const nullity = computeNullSpaceBasis(matrix).length;
|
|
59172
|
+
return ce.number(columnCount - nullity);
|
|
59173
|
+
}
|
|
59174
|
+
},
|
|
59175
|
+
IsSquareMatrix: {
|
|
59176
|
+
description: "Whether the value is a square matrix.",
|
|
59177
|
+
complexity: 8200,
|
|
59178
|
+
signature: "(value) -> boolean",
|
|
59179
|
+
evaluate: ([m], { engine: ce }) => {
|
|
59180
|
+
const op = m.evaluate();
|
|
59181
|
+
if (!isTensor2(op)) return ce.False;
|
|
59182
|
+
return op.tensor.isSquare ? ce.True : ce.False;
|
|
59183
|
+
}
|
|
59184
|
+
},
|
|
59185
|
+
IsSymmetric: {
|
|
59186
|
+
description: "Whether the matrix is symmetric (A equals its transpose).",
|
|
59187
|
+
complexity: 8200,
|
|
59188
|
+
signature: "(value) -> boolean",
|
|
59189
|
+
evaluate: ([m], { engine: ce }) => {
|
|
59190
|
+
const op = m.evaluate();
|
|
59191
|
+
if (!isTensor2(op)) return ce.False;
|
|
59192
|
+
return op.tensor.isSymmetric ? ce.True : ce.False;
|
|
59193
|
+
}
|
|
59194
|
+
},
|
|
59195
|
+
IsDiagonal: {
|
|
59196
|
+
description: "Whether the matrix is diagonal (all off-diagonal entries are zero).",
|
|
59197
|
+
complexity: 8200,
|
|
59198
|
+
signature: "(value) -> boolean",
|
|
59199
|
+
evaluate: ([m], { engine: ce }) => {
|
|
59200
|
+
const op = m.evaluate();
|
|
59201
|
+
if (!isTensor2(op)) return ce.False;
|
|
59202
|
+
return op.tensor.isDiagonal ? ce.True : ce.False;
|
|
59203
|
+
}
|
|
59204
|
+
},
|
|
59205
|
+
Cross: {
|
|
59206
|
+
description: "Cross product of two 3-vectors.",
|
|
59207
|
+
complexity: 8300,
|
|
59208
|
+
signature: "(vector, vector) -> vector",
|
|
59209
|
+
evaluate: ([a, b], { engine: ce }) => {
|
|
59210
|
+
const A = a.evaluate();
|
|
59211
|
+
const B = b.evaluate();
|
|
59212
|
+
if (!isTensor2(A) || !isTensor2(B)) return void 0;
|
|
59213
|
+
if (A.shape.length !== 1 || A.shape[0] !== 3 || B.shape.length !== 1 || B.shape[0] !== 3)
|
|
59214
|
+
return ce.error(
|
|
59215
|
+
"incompatible-dimensions",
|
|
59216
|
+
"cross product requires two 3-vectors"
|
|
59217
|
+
);
|
|
59218
|
+
const a1 = ce.expr(A.tensor.at(1) ?? ce.Zero);
|
|
59219
|
+
const a2 = ce.expr(A.tensor.at(2) ?? ce.Zero);
|
|
59220
|
+
const a3 = ce.expr(A.tensor.at(3) ?? ce.Zero);
|
|
59221
|
+
const b1 = ce.expr(B.tensor.at(1) ?? ce.Zero);
|
|
59222
|
+
const b2 = ce.expr(B.tensor.at(2) ?? ce.Zero);
|
|
59223
|
+
const b3 = ce.expr(B.tensor.at(3) ?? ce.Zero);
|
|
59224
|
+
return ce.function("List", [
|
|
59225
|
+
a2.mul(b3).sub(a3.mul(b2)),
|
|
59226
|
+
a3.mul(b1).sub(a1.mul(b3)),
|
|
59227
|
+
a1.mul(b2).sub(a2.mul(b1))
|
|
59228
|
+
]).evaluate();
|
|
59229
|
+
}
|
|
59230
|
+
},
|
|
59231
|
+
MatrixPower: {
|
|
59232
|
+
description: "Square matrix raised to an integer power (repeated matrix product).",
|
|
59233
|
+
complexity: 8300,
|
|
59234
|
+
signature: "(matrix, integer) -> matrix",
|
|
59235
|
+
evaluate: ([mat, exponent], { engine: ce }) => {
|
|
59236
|
+
const A = mat.evaluate();
|
|
59237
|
+
if (!isTensor2(A)) return void 0;
|
|
59238
|
+
if (!A.tensor.isSquare)
|
|
59239
|
+
return ce.error("expected-square-matrix", A.toString());
|
|
59240
|
+
const n = exponent.re;
|
|
59241
|
+
if (n === void 0 || !Number.isInteger(n)) return void 0;
|
|
59242
|
+
const size = A.shape[0];
|
|
59243
|
+
if (n === 0)
|
|
59244
|
+
return ce.function("IdentityMatrix", [ce.number(size)]).evaluate();
|
|
59245
|
+
let base = A;
|
|
59246
|
+
let k = n;
|
|
59247
|
+
if (n < 0) {
|
|
59248
|
+
const inv = ce.function("Inverse", [A]).evaluate();
|
|
59249
|
+
if (!isTensor2(inv)) return inv;
|
|
59250
|
+
base = inv;
|
|
59251
|
+
k = -n;
|
|
59252
|
+
}
|
|
59253
|
+
let result = base;
|
|
59254
|
+
for (let i = 1; i < k; i++)
|
|
59255
|
+
result = ce.function("MatrixMultiply", [result, base]).evaluate();
|
|
59256
|
+
return result;
|
|
59257
|
+
}
|
|
59258
|
+
},
|
|
59259
|
+
CharacteristicPolynomial: {
|
|
59260
|
+
description: "Characteristic polynomial det(x\xB7I \u2212 A) of a square matrix (monic).",
|
|
59261
|
+
complexity: 8700,
|
|
59262
|
+
// The variable is accepted as `any` (not `symbol`): an undeclared symbol
|
|
59263
|
+
// is inferred to have a numeric type, which would fail a `symbol`
|
|
59264
|
+
// signature check. The evaluate handler validates it with `isSymbol`.
|
|
59265
|
+
signature: "(matrix, any?) -> expression",
|
|
59266
|
+
evaluate: ([mat, variable], { engine: ce }) => {
|
|
59267
|
+
const A = mat.evaluate();
|
|
59268
|
+
if (!isTensor2(A)) return void 0;
|
|
59269
|
+
if (!A.tensor.isSquare)
|
|
59270
|
+
return ce.error("expected-square-matrix", A.toString());
|
|
59271
|
+
const x = variable && isSymbol2(variable) ? variable : ce.symbol("x");
|
|
59272
|
+
const n = A.shape[0];
|
|
59273
|
+
const rows = [];
|
|
59274
|
+
for (let i = 0; i < n; i++) {
|
|
59275
|
+
const row = [];
|
|
59276
|
+
for (let j = 0; j < n; j++) {
|
|
59277
|
+
const entry = ce.expr(A.tensor.at(i + 1, j + 1) ?? ce.Zero);
|
|
59278
|
+
row.push(i === j ? x.sub(entry) : entry.neg());
|
|
59279
|
+
}
|
|
59280
|
+
rows.push(ce.function("List", row));
|
|
59281
|
+
}
|
|
59282
|
+
return ce.function("Determinant", [ce.function("List", rows)]).evaluate();
|
|
59283
|
+
}
|
|
59284
|
+
},
|
|
59285
|
+
RowReduce: {
|
|
59286
|
+
description: "Reduced row echelon form (RREF) of a matrix.",
|
|
59287
|
+
complexity: 8200,
|
|
59288
|
+
signature: "(matrix) -> matrix",
|
|
59289
|
+
evaluate: ([m], { engine: ce }) => {
|
|
59290
|
+
const op = m.evaluate();
|
|
59291
|
+
if (!isTensor2(op)) return void 0;
|
|
59292
|
+
const shape = op.shape;
|
|
59293
|
+
if (shape.length !== 2)
|
|
59294
|
+
return ce.error("expected-matrix", op.toString());
|
|
59295
|
+
const matrix = tensorToNumericMatrix(op, shape[0], shape[1]);
|
|
59296
|
+
if (!matrix) return void 0;
|
|
59297
|
+
const { matrix: reduced } = rref(matrix);
|
|
59298
|
+
return ce.expr([
|
|
59299
|
+
"List",
|
|
59300
|
+
...reduced.map(
|
|
59301
|
+
(row) => ce.expr(["List", ...row.map((value) => ce.number(ce.chop(value)))])
|
|
59302
|
+
)
|
|
59303
|
+
]);
|
|
59304
|
+
}
|
|
59305
|
+
},
|
|
59096
59306
|
// Diagonal can be used to:
|
|
59097
59307
|
// 1. Create a diagonal matrix from a vector
|
|
59098
59308
|
// 2. Extract the diagonal from a matrix as a vector
|
|
@@ -60703,6 +60913,628 @@ var LOGIC_FUNCTION_LIBRARY = {
|
|
|
60703
60913
|
// src/compute-engine/library/number-theory.ts
|
|
60704
60914
|
var NUMBER_THEORY_LIBRARY = [
|
|
60705
60915
|
{
|
|
60916
|
+
FactorInteger: {
|
|
60917
|
+
description: "Return the prime factorization of an integer `n` as a list of `[prime, exponent]` tuples, ordered by ascending prime. For a negative `n`, a leading `[-1, 1]` tuple carries the sign.",
|
|
60918
|
+
signature: "(integer) -> list<tuple<integer, integer>>",
|
|
60919
|
+
examples: ["FactorInteger(360) // [(2, 3), (3, 2), (5, 1)]"],
|
|
60920
|
+
evaluate: ([n], { engine: ce }) => {
|
|
60921
|
+
const k = toBigint(n);
|
|
60922
|
+
if (k === null) return void 0;
|
|
60923
|
+
const tuple = (prime, exponent) => ce._fn("Tuple", [ce.number(prime), ce.number(exponent)]);
|
|
60924
|
+
if (k === 0n) return ce.function("List", [tuple(0n, 1)]);
|
|
60925
|
+
if (k === 1n) return ce.function("List", [tuple(1n, 1)]);
|
|
60926
|
+
const result = [];
|
|
60927
|
+
let m = k;
|
|
60928
|
+
if (m < 0n) {
|
|
60929
|
+
result.push(tuple(-1n, 1));
|
|
60930
|
+
m = -m;
|
|
60931
|
+
}
|
|
60932
|
+
if (m > 1n) {
|
|
60933
|
+
const factors = bigPrimeFactors(m);
|
|
60934
|
+
const primes = [...factors.keys()].sort(
|
|
60935
|
+
(a, b) => a < b ? -1 : a > b ? 1 : 0
|
|
60936
|
+
);
|
|
60937
|
+
for (const p of primes) result.push(tuple(p, factors.get(p)));
|
|
60938
|
+
}
|
|
60939
|
+
return ce.function("List", result);
|
|
60940
|
+
}
|
|
60941
|
+
},
|
|
60942
|
+
Divisors: {
|
|
60943
|
+
description: "Return the sorted list of positive divisors of an integer `n`. The sign of `n` is ignored.",
|
|
60944
|
+
signature: "(integer) -> list<integer>",
|
|
60945
|
+
examples: ["Divisors(12) // [1, 2, 3, 4, 6, 12]"],
|
|
60946
|
+
evaluate: ([n], { engine: ce }) => {
|
|
60947
|
+
const k = toBigint(n);
|
|
60948
|
+
if (k === null) return void 0;
|
|
60949
|
+
const m = k < 0n ? -k : k;
|
|
60950
|
+
if (m === 0n) return void 0;
|
|
60951
|
+
return ce.function(
|
|
60952
|
+
"List",
|
|
60953
|
+
divisorsAscending(m, ce._deadline).map((d) => ce.number(d))
|
|
60954
|
+
);
|
|
60955
|
+
}
|
|
60956
|
+
},
|
|
60957
|
+
NthPrime: {
|
|
60958
|
+
description: "Return the nth prime number (1-based): `NthPrime(1)` is 2, `NthPrime(2)` is 3, \u2026",
|
|
60959
|
+
signature: "(integer) -> integer",
|
|
60960
|
+
type: () => "finite_integer",
|
|
60961
|
+
examples: ["NthPrime(10) // 29"],
|
|
60962
|
+
evaluate: ([n], { engine: ce }) => {
|
|
60963
|
+
const k = toBigint(n);
|
|
60964
|
+
if (k === null || k < 1n) return void 0;
|
|
60965
|
+
if (k === 1n) return ce.number(2);
|
|
60966
|
+
let count = 1n;
|
|
60967
|
+
let candidate = 1n;
|
|
60968
|
+
let steps = 0;
|
|
60969
|
+
while (count < k) {
|
|
60970
|
+
candidate += 2n;
|
|
60971
|
+
if ((++steps & 1023) === 0) checkDeadline(ce._deadline);
|
|
60972
|
+
if (isPrimeBigint(candidate)) count += 1n;
|
|
60973
|
+
}
|
|
60974
|
+
return ce.number(candidate);
|
|
60975
|
+
}
|
|
60976
|
+
},
|
|
60977
|
+
NextPrime: {
|
|
60978
|
+
description: "Return the smallest prime greater than `n`. With a second argument `k`, return the kth prime after `n` (`k < 0` returns the |k|th prime before `n`).",
|
|
60979
|
+
signature: "(integer, integer?) -> integer",
|
|
60980
|
+
type: () => "finite_integer",
|
|
60981
|
+
examples: ["NextPrime(10) // 11", "NextPrime(10, -1) // 7"],
|
|
60982
|
+
evaluate: ([n, kArg], { engine: ce }) => {
|
|
60983
|
+
const start = toBigint(n);
|
|
60984
|
+
if (start === null) return void 0;
|
|
60985
|
+
const k = kArg === void 0 ? 1n : toBigint(kArg);
|
|
60986
|
+
if (k === null || k === 0n) return void 0;
|
|
60987
|
+
let p = start;
|
|
60988
|
+
let steps = 0;
|
|
60989
|
+
if (k > 0n) {
|
|
60990
|
+
for (let i = 0n; i < k; i++) {
|
|
60991
|
+
do {
|
|
60992
|
+
p += 1n;
|
|
60993
|
+
if ((++steps & 1023) === 0) checkDeadline(ce._deadline);
|
|
60994
|
+
} while (!isPrimeBigint(p));
|
|
60995
|
+
}
|
|
60996
|
+
} else {
|
|
60997
|
+
for (let i = 0n; i > k; i--) {
|
|
60998
|
+
do {
|
|
60999
|
+
p -= 1n;
|
|
61000
|
+
if ((++steps & 1023) === 0) checkDeadline(ce._deadline);
|
|
61001
|
+
if (p < 2n) return void 0;
|
|
61002
|
+
} while (!isPrimeBigint(p));
|
|
61003
|
+
}
|
|
61004
|
+
}
|
|
61005
|
+
return ce.number(p);
|
|
61006
|
+
}
|
|
61007
|
+
},
|
|
61008
|
+
PrimeFactors: {
|
|
61009
|
+
description: "Return the sorted list of distinct prime factors of an integer `n`. The sign of `n` is ignored; `PrimeFactors(1)` is the empty list.",
|
|
61010
|
+
signature: "(integer) -> list<integer>",
|
|
61011
|
+
examples: ["PrimeFactors(360) // [2, 3, 5]"],
|
|
61012
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61013
|
+
const k = toBigint(n);
|
|
61014
|
+
if (k === null) return void 0;
|
|
61015
|
+
const m = k < 0n ? -k : k;
|
|
61016
|
+
if (m === 0n) return void 0;
|
|
61017
|
+
const primes = [...bigPrimeFactors(m).keys()].filter((p) => p !== 1n).sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
61018
|
+
return ce.function(
|
|
61019
|
+
"List",
|
|
61020
|
+
primes.map((p) => ce.number(p))
|
|
61021
|
+
);
|
|
61022
|
+
}
|
|
61023
|
+
},
|
|
61024
|
+
PrimeNu: {
|
|
61025
|
+
description: "Return \u03C9(n), the number of distinct prime factors of `n`. The sign of `n` is ignored; `PrimeNu(1)` is 0.",
|
|
61026
|
+
signature: "(integer) -> integer",
|
|
61027
|
+
type: () => "finite_integer",
|
|
61028
|
+
examples: ["PrimeNu(360) // 3"],
|
|
61029
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61030
|
+
const k = toBigint(n);
|
|
61031
|
+
if (k === null) return void 0;
|
|
61032
|
+
const m = k < 0n ? -k : k;
|
|
61033
|
+
if (m === 0n) return void 0;
|
|
61034
|
+
if (m === 1n) return ce.number(0);
|
|
61035
|
+
return ce.number(bigPrimeFactors(m).size);
|
|
61036
|
+
}
|
|
61037
|
+
},
|
|
61038
|
+
PrimeOmega: {
|
|
61039
|
+
description: "Return \u03A9(n), the number of prime factors of `n` counted with multiplicity. The sign of `n` is ignored; `PrimeOmega(1)` is 0.",
|
|
61040
|
+
signature: "(integer) -> integer",
|
|
61041
|
+
type: () => "finite_integer",
|
|
61042
|
+
examples: ["PrimeOmega(360) // 6"],
|
|
61043
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61044
|
+
const k = toBigint(n);
|
|
61045
|
+
if (k === null) return void 0;
|
|
61046
|
+
const m = k < 0n ? -k : k;
|
|
61047
|
+
if (m === 0n) return void 0;
|
|
61048
|
+
if (m === 1n) return ce.number(0);
|
|
61049
|
+
let total = 0;
|
|
61050
|
+
for (const e of bigPrimeFactors(m).values()) total += e;
|
|
61051
|
+
return ce.number(total);
|
|
61052
|
+
}
|
|
61053
|
+
},
|
|
61054
|
+
MoebiusMu: {
|
|
61055
|
+
description: "Return the M\xF6bius function \u03BC(n): 0 if `n` is divisible by a perfect square > 1, otherwise (-1) raised to the number of distinct prime factors. The sign of `n` is ignored.",
|
|
61056
|
+
signature: "(integer) -> integer",
|
|
61057
|
+
type: () => "finite_integer",
|
|
61058
|
+
examples: ["MoebiusMu(30) // -1"],
|
|
61059
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61060
|
+
const k = toBigint(n);
|
|
61061
|
+
if (k === null) return void 0;
|
|
61062
|
+
const m = k < 0n ? -k : k;
|
|
61063
|
+
if (m === 0n) return void 0;
|
|
61064
|
+
if (m === 1n) return ce.number(1);
|
|
61065
|
+
const factors = bigPrimeFactors(m);
|
|
61066
|
+
for (const e of factors.values()) if (e > 1) return ce.number(0);
|
|
61067
|
+
return ce.number(factors.size % 2 === 0 ? 1 : -1);
|
|
61068
|
+
}
|
|
61069
|
+
},
|
|
61070
|
+
IsSquareFree: {
|
|
61071
|
+
description: 'Return `"True"` if `n` is square-free (not divisible by any perfect square > 1). The sign of `n` is ignored.',
|
|
61072
|
+
signature: "(integer) -> boolean",
|
|
61073
|
+
examples: ['IsSquareFree(30) // "True"'],
|
|
61074
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61075
|
+
const k = toBigint(n);
|
|
61076
|
+
if (k === null) return void 0;
|
|
61077
|
+
const m = k < 0n ? -k : k;
|
|
61078
|
+
if (m === 0n) return ce.False;
|
|
61079
|
+
if (m === 1n) return ce.True;
|
|
61080
|
+
const factors = bigPrimeFactors(m);
|
|
61081
|
+
for (const e of factors.values()) if (e > 1) return ce.False;
|
|
61082
|
+
return ce.True;
|
|
61083
|
+
}
|
|
61084
|
+
},
|
|
61085
|
+
Radical: {
|
|
61086
|
+
description: "Return the radical of `n` (its square-free kernel): the product of its distinct prime factors. The sign of `n` is ignored; `Radical(1)` is 1.",
|
|
61087
|
+
signature: "(integer) -> integer",
|
|
61088
|
+
type: () => "finite_integer",
|
|
61089
|
+
examples: ["Radical(360) // 30"],
|
|
61090
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61091
|
+
const k = toBigint(n);
|
|
61092
|
+
if (k === null) return void 0;
|
|
61093
|
+
const m = k < 0n ? -k : k;
|
|
61094
|
+
if (m === 0n) return void 0;
|
|
61095
|
+
if (m === 1n) return ce.number(1);
|
|
61096
|
+
let product = 1n;
|
|
61097
|
+
for (const p of bigPrimeFactors(m).keys()) product *= p;
|
|
61098
|
+
return ce.number(product);
|
|
61099
|
+
}
|
|
61100
|
+
},
|
|
61101
|
+
PowerMod: {
|
|
61102
|
+
description: "Return `a^b mod m` (modular exponentiation). A negative `b` uses the modular inverse of `a`; the result is undefined when that inverse does not exist (i.e. when `a` and `m` are not coprime). The result is in the range [0, m).",
|
|
61103
|
+
signature: "(integer, integer, integer) -> integer",
|
|
61104
|
+
type: () => "finite_integer",
|
|
61105
|
+
examples: ["PowerMod(2, 10, 1000) // 24"],
|
|
61106
|
+
evaluate: ([aOp, bOp, mOp], { engine: ce }) => {
|
|
61107
|
+
const a = toBigint(aOp);
|
|
61108
|
+
const b = toBigint(bOp);
|
|
61109
|
+
const m = toBigint(mOp);
|
|
61110
|
+
if (a === null || b === null || m === null) return void 0;
|
|
61111
|
+
if (m <= 0n) return void 0;
|
|
61112
|
+
if (m === 1n) return ce.number(0);
|
|
61113
|
+
if (b >= 0n) return ce.number(modPow(a, b, m));
|
|
61114
|
+
const base = (a % m + m) % m;
|
|
61115
|
+
const [g, s] = extGcd(base, m);
|
|
61116
|
+
if (g !== 1n) return void 0;
|
|
61117
|
+
const inv = (s % m + m) % m;
|
|
61118
|
+
return ce.number(modPow(inv, -b, m));
|
|
61119
|
+
}
|
|
61120
|
+
},
|
|
61121
|
+
ExtendedGCD: {
|
|
61122
|
+
description: "Return the extended GCD of `a` and `b` as a tuple `(g, x, y)` where `g = gcd(a, b)` is non-negative and `a\xB7x + b\xB7y = g` (B\xE9zout coefficients).",
|
|
61123
|
+
signature: "(integer, integer) -> tuple<integer, integer, integer>",
|
|
61124
|
+
examples: ["ExtendedGCD(12, 18) // (6, -1, 1)"],
|
|
61125
|
+
evaluate: ([aOp, bOp], { engine: ce }) => {
|
|
61126
|
+
const a = toBigint(aOp);
|
|
61127
|
+
const b = toBigint(bOp);
|
|
61128
|
+
if (a === null || b === null) return void 0;
|
|
61129
|
+
let [g, x, y] = extGcd(a, b);
|
|
61130
|
+
if (g < 0n) {
|
|
61131
|
+
g = -g;
|
|
61132
|
+
x = -x;
|
|
61133
|
+
y = -y;
|
|
61134
|
+
}
|
|
61135
|
+
return ce._fn("Tuple", [ce.number(g), ce.number(x), ce.number(y)]);
|
|
61136
|
+
}
|
|
61137
|
+
},
|
|
61138
|
+
IntegerSqrt: {
|
|
61139
|
+
description: "Return the integer square root of `n`, i.e. the largest integer `m` such that `m\xB2 \u2264 n`. Undefined for negative `n`.",
|
|
61140
|
+
signature: "(integer) -> integer",
|
|
61141
|
+
type: () => "finite_integer",
|
|
61142
|
+
examples: ["IntegerSqrt(17) // 4"],
|
|
61143
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61144
|
+
const k = toBigint(n);
|
|
61145
|
+
if (k === null || k < 0n) return void 0;
|
|
61146
|
+
return ce.number(bigintSqrt2(k));
|
|
61147
|
+
}
|
|
61148
|
+
},
|
|
61149
|
+
ChineseRemainder: {
|
|
61150
|
+
description: "Solve a system of simultaneous congruences: return the smallest non-negative integer `x` such that `x \u2261 residues[i] (mod moduli[i])` for every `i`. Undefined if the system is inconsistent or the two lists differ in length.",
|
|
61151
|
+
signature: "(collection, collection) -> integer",
|
|
61152
|
+
examples: ["ChineseRemainder([2, 3, 2], [3, 5, 7]) // 23"],
|
|
61153
|
+
evaluate: ([residuesOp, moduliOp], { engine: ce }) => {
|
|
61154
|
+
const residues = Array.from(residuesOp?.each() ?? []).map(toBigint);
|
|
61155
|
+
const moduli = Array.from(moduliOp?.each() ?? []).map(toBigint);
|
|
61156
|
+
if (residues.length === 0 || residues.length !== moduli.length)
|
|
61157
|
+
return void 0;
|
|
61158
|
+
if (residues.includes(null) || moduli.includes(null)) return void 0;
|
|
61159
|
+
const x = chineseRemainder(residues, moduli);
|
|
61160
|
+
if (x === null) return void 0;
|
|
61161
|
+
return ce.number(x);
|
|
61162
|
+
}
|
|
61163
|
+
},
|
|
61164
|
+
CarmichaelLambda: {
|
|
61165
|
+
description: "Return the Carmichael function \u03BB(n) (the reduced totient): the smallest positive integer `m` such that `a^m \u2261 1 (mod n)` for every `a` coprime to `n`. Defined for `n \u2265 1`.",
|
|
61166
|
+
signature: "(integer) -> integer",
|
|
61167
|
+
type: () => "finite_integer",
|
|
61168
|
+
examples: ["CarmichaelLambda(15) // 4"],
|
|
61169
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61170
|
+
const k = toBigint(n);
|
|
61171
|
+
if (k === null || k < 1n) return void 0;
|
|
61172
|
+
return ce.number(carmichaelLambda(k));
|
|
61173
|
+
}
|
|
61174
|
+
},
|
|
61175
|
+
LucasL: {
|
|
61176
|
+
description: "Return the nth Lucas number: `LucasL(0)` is 2, `LucasL(1)` is 1, and `LucasL(n) = LucasL(n-1) + LucasL(n-2)`. Negative indices follow `LucasL(-n) = (-1)^n \xB7 LucasL(n)`.",
|
|
61177
|
+
signature: "(integer) -> integer",
|
|
61178
|
+
type: () => "finite_integer",
|
|
61179
|
+
examples: ["LucasL(10) // 123"],
|
|
61180
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61181
|
+
const k = toBigint(n);
|
|
61182
|
+
if (k === null) return void 0;
|
|
61183
|
+
const neg2 = k < 0n;
|
|
61184
|
+
const kk = neg2 ? -k : k;
|
|
61185
|
+
let a = 2n;
|
|
61186
|
+
let b = 1n;
|
|
61187
|
+
let steps = 0;
|
|
61188
|
+
for (let i = 0n; i < kk; i++) {
|
|
61189
|
+
if ((++steps & 4095) === 0) checkDeadline(ce._deadline);
|
|
61190
|
+
[a, b] = [b, a + b];
|
|
61191
|
+
}
|
|
61192
|
+
return ce.number(neg2 && kk % 2n === 1n ? -a : a);
|
|
61193
|
+
}
|
|
61194
|
+
},
|
|
61195
|
+
CatalanNumber: {
|
|
61196
|
+
description: "Return the nth Catalan number `C(n) = (2n)! / ((n+1)! \xB7 n!)`: 1, 1, 2, 5, 14, 42, \u2026 Defined for `n \u2265 0`.",
|
|
61197
|
+
signature: "(integer) -> integer",
|
|
61198
|
+
type: () => "finite_integer",
|
|
61199
|
+
examples: ["CatalanNumber(5) // 42"],
|
|
61200
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61201
|
+
const k = toBigint(n);
|
|
61202
|
+
if (k === null || k < 0n) return void 0;
|
|
61203
|
+
let c = 1n;
|
|
61204
|
+
let steps = 0;
|
|
61205
|
+
for (let i = 0n; i < k; i++) {
|
|
61206
|
+
if ((++steps & 4095) === 0) checkDeadline(ce._deadline);
|
|
61207
|
+
c = c * 2n * (2n * i + 1n) / (i + 2n);
|
|
61208
|
+
}
|
|
61209
|
+
return ce.number(c);
|
|
61210
|
+
}
|
|
61211
|
+
},
|
|
61212
|
+
IsPerfectPower: {
|
|
61213
|
+
description: 'Return `"True"` if `n` is a perfect power `a^b` for integers `a` and `b \u2265 2` (a negative `n` requires an odd exponent). The smallest perfect power is 4.',
|
|
61214
|
+
signature: "(integer) -> boolean",
|
|
61215
|
+
examples: ['IsPerfectPower(64) // "True"'],
|
|
61216
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61217
|
+
const k = toBigint(n);
|
|
61218
|
+
if (k === null) return void 0;
|
|
61219
|
+
const m = k < 0n ? -k : k;
|
|
61220
|
+
if (m < 4n) return ce.False;
|
|
61221
|
+
return ce.symbol(
|
|
61222
|
+
isPerfectPowerBigint(m, k < 0n, ce._deadline) ? "True" : "False"
|
|
61223
|
+
);
|
|
61224
|
+
}
|
|
61225
|
+
},
|
|
61226
|
+
ContinuedFraction: {
|
|
61227
|
+
description: "Return the continued-fraction expansion of `x` as a list of integer terms `[a0, a1, \u2026]`. An exact rational is expanded fully; for an inexact value the expansion is truncated to the optional `n` terms (default 20).",
|
|
61228
|
+
signature: "(number, integer?) -> list<integer>",
|
|
61229
|
+
examples: ["ContinuedFraction(43/19) // [2, 3, 1, 4]"],
|
|
61230
|
+
evaluate: ([xOp, nOp], { engine: ce }) => {
|
|
61231
|
+
if (xOp === void 0) return void 0;
|
|
61232
|
+
const maxTerms = nOp === void 0 ? void 0 : Number(toBigint(nOp) ?? 0n);
|
|
61233
|
+
if (maxTerms !== void 0 && maxTerms < 1) return void 0;
|
|
61234
|
+
const terms = [];
|
|
61235
|
+
if (xOp.isRational) {
|
|
61236
|
+
let a = toBigint(xOp.numerator);
|
|
61237
|
+
let b = toBigint(xOp.denominator);
|
|
61238
|
+
if (a === null || b === null || b === 0n) return void 0;
|
|
61239
|
+
if (b < 0n) {
|
|
61240
|
+
a = -a;
|
|
61241
|
+
b = -b;
|
|
61242
|
+
}
|
|
61243
|
+
while (b !== 0n) {
|
|
61244
|
+
let q = a / b;
|
|
61245
|
+
if (a % b !== 0n && a < 0n !== b < 0n) q -= 1n;
|
|
61246
|
+
terms.push(q);
|
|
61247
|
+
[a, b] = [b, a - q * b];
|
|
61248
|
+
if (maxTerms !== void 0 && terms.length >= maxTerms) break;
|
|
61249
|
+
}
|
|
61250
|
+
} else {
|
|
61251
|
+
let val = xOp.re;
|
|
61252
|
+
if (!Number.isFinite(val)) return void 0;
|
|
61253
|
+
const cap = maxTerms ?? 20;
|
|
61254
|
+
for (let i = 0; i < cap; i++) {
|
|
61255
|
+
const fl = Math.floor(val);
|
|
61256
|
+
terms.push(BigInt(fl));
|
|
61257
|
+
const frac = val - fl;
|
|
61258
|
+
if (frac < 1e-12) break;
|
|
61259
|
+
val = 1 / frac;
|
|
61260
|
+
if (!Number.isFinite(val)) break;
|
|
61261
|
+
}
|
|
61262
|
+
}
|
|
61263
|
+
return ce.function(
|
|
61264
|
+
"List",
|
|
61265
|
+
terms.map((t) => ce.number(t))
|
|
61266
|
+
);
|
|
61267
|
+
}
|
|
61268
|
+
},
|
|
61269
|
+
FromContinuedFraction: {
|
|
61270
|
+
description: "Reconstruct the (rational) value of a continued fraction given its list of integer terms `[a0, a1, \u2026]`.",
|
|
61271
|
+
signature: "(collection) -> number",
|
|
61272
|
+
examples: ["FromContinuedFraction([2, 3, 1, 4]) // 43/19"],
|
|
61273
|
+
evaluate: ([listOp], { engine: ce }) => {
|
|
61274
|
+
const terms = Array.from(listOp?.each() ?? []).map(toBigint);
|
|
61275
|
+
if (terms.length === 0 || terms.includes(null)) return void 0;
|
|
61276
|
+
let p = 1n;
|
|
61277
|
+
let q = 0n;
|
|
61278
|
+
for (let i = terms.length - 1; i >= 0; i--)
|
|
61279
|
+
[p, q] = [terms[i] * p + q, p];
|
|
61280
|
+
if (q === 0n) return void 0;
|
|
61281
|
+
return ce.number(p).div(ce.number(q));
|
|
61282
|
+
}
|
|
61283
|
+
},
|
|
61284
|
+
IntegerDigits: {
|
|
61285
|
+
description: "Return the digits of `n` in the given `base` (default 10), most-significant first. The sign of `n` is ignored. With a third argument `length`, the result is zero-padded on the left (or truncated to its least-significant digits) to that length.",
|
|
61286
|
+
signature: "(integer, integer?, integer?) -> list<integer>",
|
|
61287
|
+
examples: ["IntegerDigits(255, 16) // [15, 15]"],
|
|
61288
|
+
evaluate: ([nOp, baseOp, lenOp], { engine: ce }) => {
|
|
61289
|
+
const k = toBigint(nOp);
|
|
61290
|
+
if (k === null) return void 0;
|
|
61291
|
+
const base = baseOp === void 0 ? 10n : toBigint(baseOp);
|
|
61292
|
+
if (base === null || base < 2n) return void 0;
|
|
61293
|
+
let m = k < 0n ? -k : k;
|
|
61294
|
+
const digits = [];
|
|
61295
|
+
if (m === 0n) digits.push(0n);
|
|
61296
|
+
while (m > 0n) {
|
|
61297
|
+
digits.push(m % base);
|
|
61298
|
+
m = m / base;
|
|
61299
|
+
}
|
|
61300
|
+
digits.reverse();
|
|
61301
|
+
if (lenOp !== void 0) {
|
|
61302
|
+
const len = toBigint(lenOp);
|
|
61303
|
+
if (len === null || len < 0n) return void 0;
|
|
61304
|
+
const L = Number(len);
|
|
61305
|
+
while (digits.length < L) digits.unshift(0n);
|
|
61306
|
+
if (digits.length > L) digits.splice(0, digits.length - L);
|
|
61307
|
+
}
|
|
61308
|
+
return ce.function(
|
|
61309
|
+
"List",
|
|
61310
|
+
digits.map((d) => ce.number(d))
|
|
61311
|
+
);
|
|
61312
|
+
}
|
|
61313
|
+
},
|
|
61314
|
+
DigitCount: {
|
|
61315
|
+
description: "Count digits of `n` in the given `base` (default 10); the sign of `n` is ignored. With a third argument `digit`, return how many times that digit occurs. Otherwise return a list `[count of 1, count of 2, \u2026, count of base-1, count of 0]`.",
|
|
61316
|
+
signature: "(integer, integer?, integer?) -> integer | list<integer>",
|
|
61317
|
+
type: ([, , digit]) => digit !== void 0 ? "finite_integer" : "list",
|
|
61318
|
+
examples: ["DigitCount(122, 10, 2) // 2"],
|
|
61319
|
+
evaluate: ([nOp, baseOp, digitOp], { engine: ce }) => {
|
|
61320
|
+
const k = toBigint(nOp);
|
|
61321
|
+
if (k === null) return void 0;
|
|
61322
|
+
const base = baseOp === void 0 ? 10n : toBigint(baseOp);
|
|
61323
|
+
if (base === null || base < 2n) return void 0;
|
|
61324
|
+
const counts = /* @__PURE__ */ new Map();
|
|
61325
|
+
let m = k < 0n ? -k : k;
|
|
61326
|
+
if (m === 0n) counts.set(0n, 1);
|
|
61327
|
+
while (m > 0n) {
|
|
61328
|
+
const d = m % base;
|
|
61329
|
+
counts.set(d, (counts.get(d) ?? 0) + 1);
|
|
61330
|
+
m = m / base;
|
|
61331
|
+
}
|
|
61332
|
+
if (digitOp !== void 0) {
|
|
61333
|
+
const d = toBigint(digitOp);
|
|
61334
|
+
if (d === null || d < 0n || d >= base) return void 0;
|
|
61335
|
+
return ce.number(counts.get(d) ?? 0);
|
|
61336
|
+
}
|
|
61337
|
+
const list = [];
|
|
61338
|
+
for (let d = 1n; d < base; d++)
|
|
61339
|
+
list.push(ce.number(counts.get(d) ?? 0));
|
|
61340
|
+
list.push(ce.number(counts.get(0n) ?? 0));
|
|
61341
|
+
return ce.function("List", list);
|
|
61342
|
+
}
|
|
61343
|
+
},
|
|
61344
|
+
RandomPrime: {
|
|
61345
|
+
description: "Return a random prime. `RandomPrime(n)` draws a prime in [2, n]; `RandomPrime(m, n)` draws a prime in [m, n]. Undefined if the range contains no prime.",
|
|
61346
|
+
pure: false,
|
|
61347
|
+
signature: "(integer, integer?) -> integer",
|
|
61348
|
+
type: () => "finite_integer",
|
|
61349
|
+
examples: ["RandomPrime(100) // e.g. 47"],
|
|
61350
|
+
evaluate: ([aOp, bOp], { engine: ce }) => {
|
|
61351
|
+
let lo;
|
|
61352
|
+
let hi;
|
|
61353
|
+
if (bOp === void 0) {
|
|
61354
|
+
lo = 2n;
|
|
61355
|
+
hi = toBigint(aOp);
|
|
61356
|
+
} else {
|
|
61357
|
+
lo = toBigint(aOp);
|
|
61358
|
+
hi = toBigint(bOp);
|
|
61359
|
+
}
|
|
61360
|
+
if (lo === null || hi === null) return void 0;
|
|
61361
|
+
if (lo < 2n) lo = 2n;
|
|
61362
|
+
if (hi < lo) return void 0;
|
|
61363
|
+
const range2 = hi - lo + 1n;
|
|
61364
|
+
const attempts = 100 + 20 * hi.toString().length;
|
|
61365
|
+
for (let i = 0; i < attempts; i++) {
|
|
61366
|
+
const r = lo + randomBigintBelow(range2);
|
|
61367
|
+
if (isPrimeBigint(r)) return ce.number(r);
|
|
61368
|
+
}
|
|
61369
|
+
for (let p = lo; p <= hi; p++) {
|
|
61370
|
+
checkDeadline(ce._deadline);
|
|
61371
|
+
if (isPrimeBigint(p)) return ce.number(p);
|
|
61372
|
+
}
|
|
61373
|
+
return void 0;
|
|
61374
|
+
}
|
|
61375
|
+
},
|
|
61376
|
+
PrimePi: {
|
|
61377
|
+
description: "Return \u03C0(n), the prime-counting function: the number of primes less than or equal to `n`.",
|
|
61378
|
+
signature: "(real) -> integer",
|
|
61379
|
+
type: () => "finite_integer",
|
|
61380
|
+
examples: ["PrimePi(10) // 4"],
|
|
61381
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61382
|
+
const x = n?.re;
|
|
61383
|
+
if (x === void 0 || !Number.isFinite(x)) return void 0;
|
|
61384
|
+
const bound = BigInt(Math.floor(x));
|
|
61385
|
+
if (bound < 2n) return ce.number(0);
|
|
61386
|
+
let count = 1;
|
|
61387
|
+
let steps = 0;
|
|
61388
|
+
for (let k = 3n; k <= bound; k += 2n) {
|
|
61389
|
+
if ((++steps & 1023) === 0) checkDeadline(ce._deadline);
|
|
61390
|
+
if (isPrimeBigint(k)) count++;
|
|
61391
|
+
}
|
|
61392
|
+
return ce.number(count);
|
|
61393
|
+
}
|
|
61394
|
+
},
|
|
61395
|
+
BernoulliB: {
|
|
61396
|
+
description: "Return the nth Bernoulli number B\u2099 as an exact rational, using the convention B\u2081 = -1/2. Odd `n > 1` give 0.",
|
|
61397
|
+
signature: "(integer) -> finite_rational",
|
|
61398
|
+
type: () => "finite_rational",
|
|
61399
|
+
examples: ["BernoulliB(2) // 1/6"],
|
|
61400
|
+
evaluate: ([n], { engine: ce }) => {
|
|
61401
|
+
const k = toBigint(n);
|
|
61402
|
+
if (k === null || k < 0n) return void 0;
|
|
61403
|
+
if (k === 0n) return ce.number(1);
|
|
61404
|
+
if (k === 1n) return ce.number(-1).div(ce.number(2));
|
|
61405
|
+
if (k % 2n === 1n) return ce.number(0);
|
|
61406
|
+
const [num, den] = bernoulliNumber(Number(k), ce._deadline);
|
|
61407
|
+
return den === 1n ? ce.number(num) : ce.number(num).div(ce.number(den));
|
|
61408
|
+
}
|
|
61409
|
+
},
|
|
61410
|
+
FromDigits: {
|
|
61411
|
+
description: "Reconstruct an integer from its list of digits (most-significant first) in the given `base` (default 10). The inverse of `IntegerDigits`. Digits outside `[0, base)` are combined positionally (Horner evaluation).",
|
|
61412
|
+
signature: "(collection, integer?) -> integer",
|
|
61413
|
+
type: () => "finite_integer",
|
|
61414
|
+
examples: ["FromDigits([1, 2, 3, 4]) // 1234"],
|
|
61415
|
+
evaluate: ([digitsOp, baseOp], { engine: ce }) => {
|
|
61416
|
+
const digits = Array.from(digitsOp?.each() ?? []).map(toBigint);
|
|
61417
|
+
if (digits.length === 0 || digits.includes(null)) return void 0;
|
|
61418
|
+
const base = baseOp === void 0 ? 10n : toBigint(baseOp);
|
|
61419
|
+
if (base === null || base < 2n) return void 0;
|
|
61420
|
+
let result = 0n;
|
|
61421
|
+
for (const d of digits) result = result * base + d;
|
|
61422
|
+
return ce.number(result);
|
|
61423
|
+
}
|
|
61424
|
+
},
|
|
61425
|
+
DigitSum: {
|
|
61426
|
+
description: "Return the sum of the digits of `n` in the given `base` (default 10). The sign of `n` is ignored.",
|
|
61427
|
+
signature: "(integer, integer?) -> integer",
|
|
61428
|
+
type: () => "finite_integer",
|
|
61429
|
+
examples: ["DigitSum(1234) // 10"],
|
|
61430
|
+
evaluate: ([nOp, baseOp], { engine: ce }) => {
|
|
61431
|
+
const k = toBigint(nOp);
|
|
61432
|
+
if (k === null) return void 0;
|
|
61433
|
+
const base = baseOp === void 0 ? 10n : toBigint(baseOp);
|
|
61434
|
+
if (base === null || base < 2n) return void 0;
|
|
61435
|
+
let m = k < 0n ? -k : k;
|
|
61436
|
+
let sum = 0n;
|
|
61437
|
+
while (m > 0n) {
|
|
61438
|
+
sum += m % base;
|
|
61439
|
+
m /= base;
|
|
61440
|
+
}
|
|
61441
|
+
return ce.number(sum);
|
|
61442
|
+
}
|
|
61443
|
+
},
|
|
61444
|
+
DivisorSigma: {
|
|
61445
|
+
description: "The divisor function \u03C3_k(n) = \u03A3_{d | n} d\u1D4F over the positive divisors of `n`. \u03C3\u2080 counts divisors, \u03C3\u2081 sums them. Defined for `n \u2265 1`.",
|
|
61446
|
+
signature: "(integer, integer) -> integer",
|
|
61447
|
+
type: () => "finite_integer",
|
|
61448
|
+
examples: ["DivisorSigma(2, 6) // 50"],
|
|
61449
|
+
evaluate: ([kOp, nOp], { engine: ce }) => {
|
|
61450
|
+
const k = toBigint(kOp);
|
|
61451
|
+
const n = toBigint(nOp);
|
|
61452
|
+
if (k === null || n === null || k < 0n || n < 1n) return void 0;
|
|
61453
|
+
if (n === 1n) return ce.number(1);
|
|
61454
|
+
let result = 1n;
|
|
61455
|
+
for (const [p, e] of bigPrimeFactors(n)) {
|
|
61456
|
+
if (k === 0n) result *= BigInt(e + 1);
|
|
61457
|
+
else {
|
|
61458
|
+
const pk = p ** k;
|
|
61459
|
+
result *= (pk ** BigInt(e + 1) - 1n) / (pk - 1n);
|
|
61460
|
+
}
|
|
61461
|
+
}
|
|
61462
|
+
return ce.number(result);
|
|
61463
|
+
}
|
|
61464
|
+
},
|
|
61465
|
+
JacobiSymbol: {
|
|
61466
|
+
description: "The Jacobi symbol (a/n) for an odd `n > 0`. Returns -1, 0, or 1. Undefined when `n` is even or non-positive.",
|
|
61467
|
+
signature: "(integer, integer) -> integer",
|
|
61468
|
+
type: () => "finite_integer",
|
|
61469
|
+
examples: ["JacobiSymbol(5, 21) // 1"],
|
|
61470
|
+
evaluate: ([aOp, nOp], { engine: ce }) => {
|
|
61471
|
+
const a = toBigint(aOp);
|
|
61472
|
+
const n = toBigint(nOp);
|
|
61473
|
+
if (a === null || n === null) return void 0;
|
|
61474
|
+
if (n <= 0n || n % 2n === 0n) return void 0;
|
|
61475
|
+
return ce.number(jacobiSymbol(a, n));
|
|
61476
|
+
}
|
|
61477
|
+
},
|
|
61478
|
+
LegendreSymbol: {
|
|
61479
|
+
description: "The Legendre symbol (a/p) for an odd prime `p`. Returns -1, 0, or 1. Undefined when `p` is not an odd prime.",
|
|
61480
|
+
signature: "(integer, integer) -> integer",
|
|
61481
|
+
type: () => "finite_integer",
|
|
61482
|
+
examples: ["LegendreSymbol(3, 7) // -1"],
|
|
61483
|
+
evaluate: ([aOp, pOp], { engine: ce }) => {
|
|
61484
|
+
const a = toBigint(aOp);
|
|
61485
|
+
const p = toBigint(pOp);
|
|
61486
|
+
if (a === null || p === null) return void 0;
|
|
61487
|
+
if (p <= 2n || p % 2n === 0n || !isPrimeBigint(p)) return void 0;
|
|
61488
|
+
return ce.number(jacobiSymbol(a, p));
|
|
61489
|
+
}
|
|
61490
|
+
},
|
|
61491
|
+
MultiplicativeOrder: {
|
|
61492
|
+
description: "The multiplicative order of `a` modulo `n`: the smallest `k > 0` such that `a^k \u2261 1 (mod n)`. Undefined unless `a` and `n` are coprime.",
|
|
61493
|
+
signature: "(integer, integer) -> integer",
|
|
61494
|
+
type: () => "finite_integer",
|
|
61495
|
+
examples: ["MultiplicativeOrder(2, 7) // 3"],
|
|
61496
|
+
evaluate: ([aOp, nOp], { engine: ce }) => {
|
|
61497
|
+
const a0 = toBigint(aOp);
|
|
61498
|
+
const n = toBigint(nOp);
|
|
61499
|
+
if (a0 === null || n === null || n < 1n) return void 0;
|
|
61500
|
+
if (n === 1n) return ce.number(1);
|
|
61501
|
+
const a = (a0 % n + n) % n;
|
|
61502
|
+
if (gcd2(a, n) !== 1n) return void 0;
|
|
61503
|
+
for (const d of divisorsAscending(carmichaelLambda(n), ce._deadline)) {
|
|
61504
|
+
checkDeadline(ce._deadline);
|
|
61505
|
+
if (modPow(a, d, n) === 1n) return ce.number(d);
|
|
61506
|
+
}
|
|
61507
|
+
return void 0;
|
|
61508
|
+
}
|
|
61509
|
+
},
|
|
61510
|
+
PrimitiveRoot: {
|
|
61511
|
+
description: "The smallest primitive root modulo `n` (a generator of the multiplicative group of integers mod `n`), or undefined if none exists (which happens unless `n` is 1, 2, 4, p\u1D4F, or 2p\u1D4F for an odd prime p).",
|
|
61512
|
+
signature: "(integer) -> integer",
|
|
61513
|
+
type: () => "finite_integer",
|
|
61514
|
+
examples: ["PrimitiveRoot(7) // 3"],
|
|
61515
|
+
evaluate: ([nOp], { engine: ce }) => {
|
|
61516
|
+
const n = toBigint(nOp);
|
|
61517
|
+
if (n === null || n < 1n) return void 0;
|
|
61518
|
+
if (n === 1n) return ce.number(0);
|
|
61519
|
+
if (n === 2n) return ce.number(1);
|
|
61520
|
+
if (n === 4n) return ce.number(3);
|
|
61521
|
+
if (!hasPrimitiveRoot(n)) return void 0;
|
|
61522
|
+
const phi = eulerPhi(n);
|
|
61523
|
+
const phiFactors = [...bigPrimeFactors(phi).keys()];
|
|
61524
|
+
for (let a = 2n; a < n; a++) {
|
|
61525
|
+
checkDeadline(ce._deadline);
|
|
61526
|
+
if (gcd2(a, n) !== 1n) continue;
|
|
61527
|
+
if (phiFactors.every((q) => modPow(a, phi / q, n) !== 1n))
|
|
61528
|
+
return ce.number(a);
|
|
61529
|
+
}
|
|
61530
|
+
return void 0;
|
|
61531
|
+
}
|
|
61532
|
+
},
|
|
61533
|
+
PrimeNumber: {
|
|
61534
|
+
description: "The nth prime number. `PrimeNumber` is an alias for `NthPrime`, which is the preferred name.",
|
|
61535
|
+
signature: "(integer) -> integer",
|
|
61536
|
+
canonical: ([n], { engine }) => engine._fn("NthPrime", [n])
|
|
61537
|
+
},
|
|
60706
61538
|
Totient: {
|
|
60707
61539
|
wikidata: "Q190026",
|
|
60708
61540
|
description: "Euler's totient function \u03C6(n): count of positive integers \u2264 n that are coprime to n.",
|
|
@@ -60939,6 +61771,165 @@ var NUMBER_THEORY_LIBRARY = [
|
|
|
60939
61771
|
}
|
|
60940
61772
|
}
|
|
60941
61773
|
];
|
|
61774
|
+
function extGcd(a, b) {
|
|
61775
|
+
let [oldR, r] = [a, b];
|
|
61776
|
+
let [oldS, s] = [1n, 0n];
|
|
61777
|
+
let [oldT, t] = [0n, 1n];
|
|
61778
|
+
while (r !== 0n) {
|
|
61779
|
+
const q = oldR / r;
|
|
61780
|
+
[oldR, r] = [r, oldR - q * r];
|
|
61781
|
+
[oldS, s] = [s, oldS - q * s];
|
|
61782
|
+
[oldT, t] = [t, oldT - q * t];
|
|
61783
|
+
}
|
|
61784
|
+
return [oldR, oldS, oldT];
|
|
61785
|
+
}
|
|
61786
|
+
function bigintSqrt2(n) {
|
|
61787
|
+
if (n < 2n) return n;
|
|
61788
|
+
let x = 1n << (BigInt(n.toString(2).length) + 1n) / 2n;
|
|
61789
|
+
for (; ; ) {
|
|
61790
|
+
const y = x + n / x >> 1n;
|
|
61791
|
+
if (y >= x) break;
|
|
61792
|
+
x = y;
|
|
61793
|
+
}
|
|
61794
|
+
while (x * x > n) x -= 1n;
|
|
61795
|
+
return x;
|
|
61796
|
+
}
|
|
61797
|
+
function chineseRemainder(residues, moduli) {
|
|
61798
|
+
let x = 0n;
|
|
61799
|
+
let m = 1n;
|
|
61800
|
+
for (let i = 0; i < residues.length; i++) {
|
|
61801
|
+
const ni = moduli[i];
|
|
61802
|
+
if (ni <= 0n) return null;
|
|
61803
|
+
const ri = (residues[i] % ni + ni) % ni;
|
|
61804
|
+
const [g, p] = extGcd(m, ni);
|
|
61805
|
+
if ((ri - x) % g !== 0n) return null;
|
|
61806
|
+
const lcmMN = m / g * ni;
|
|
61807
|
+
const mod22 = ni / g;
|
|
61808
|
+
const lambda = ((ri - x) / g * p % mod22 + mod22) % mod22;
|
|
61809
|
+
x = ((x + m * lambda) % lcmMN + lcmMN) % lcmMN;
|
|
61810
|
+
m = lcmMN;
|
|
61811
|
+
}
|
|
61812
|
+
return x;
|
|
61813
|
+
}
|
|
61814
|
+
function iroot(n, b) {
|
|
61815
|
+
if (n < 2n) return n;
|
|
61816
|
+
const bb = BigInt(b);
|
|
61817
|
+
const approx = Math.pow(Number(n), 1 / b);
|
|
61818
|
+
let x = Number.isFinite(approx) ? BigInt(Math.round(approx)) : 1n << BigInt(n.toString(2).length) / bb + 1n;
|
|
61819
|
+
if (x < 1n) x = 1n;
|
|
61820
|
+
while (x ** bb > n) x -= 1n;
|
|
61821
|
+
while ((x + 1n) ** bb <= n) x += 1n;
|
|
61822
|
+
return x;
|
|
61823
|
+
}
|
|
61824
|
+
function isPerfectPowerBigint(m, oddOnly, deadline) {
|
|
61825
|
+
const bits = m.toString(2).length;
|
|
61826
|
+
for (let b = oddOnly ? 3 : 2; b <= bits; b++) {
|
|
61827
|
+
if (oddOnly && b % 2 === 0) continue;
|
|
61828
|
+
checkDeadline(deadline);
|
|
61829
|
+
const r = iroot(m, b);
|
|
61830
|
+
if (r >= 2n && r ** BigInt(b) === m) return true;
|
|
61831
|
+
}
|
|
61832
|
+
return false;
|
|
61833
|
+
}
|
|
61834
|
+
function randomBigintBelow(n) {
|
|
61835
|
+
if (n <= 1n) return 0n;
|
|
61836
|
+
if (n <= BigInt(Number.MAX_SAFE_INTEGER))
|
|
61837
|
+
return BigInt(Math.floor(Math.random() * Number(n)));
|
|
61838
|
+
const bits = n.toString(2).length;
|
|
61839
|
+
let r;
|
|
61840
|
+
do {
|
|
61841
|
+
r = 0n;
|
|
61842
|
+
for (let i = 0; i < bits; i += 30)
|
|
61843
|
+
r = r << 30n | BigInt(Math.floor(Math.random() * (1 << 30)));
|
|
61844
|
+
r &= (1n << BigInt(bits)) - 1n;
|
|
61845
|
+
} while (r >= n);
|
|
61846
|
+
return r;
|
|
61847
|
+
}
|
|
61848
|
+
function divisorsAscending(m, deadline) {
|
|
61849
|
+
const small = [];
|
|
61850
|
+
const large = [];
|
|
61851
|
+
let steps = 0;
|
|
61852
|
+
for (let i = 1n; i * i <= m; i++) {
|
|
61853
|
+
if ((++steps & 4095) === 0) checkDeadline(deadline);
|
|
61854
|
+
if (m % i === 0n) {
|
|
61855
|
+
small.push(i);
|
|
61856
|
+
const j = m / i;
|
|
61857
|
+
if (j !== i) large.push(j);
|
|
61858
|
+
}
|
|
61859
|
+
}
|
|
61860
|
+
large.reverse();
|
|
61861
|
+
return [...small, ...large];
|
|
61862
|
+
}
|
|
61863
|
+
function eulerPhi(n) {
|
|
61864
|
+
if (n <= 1n) return 1n;
|
|
61865
|
+
let result = n;
|
|
61866
|
+
for (const p of bigPrimeFactors(n).keys()) result = result / p * (p - 1n);
|
|
61867
|
+
return result;
|
|
61868
|
+
}
|
|
61869
|
+
function carmichaelLambda(n) {
|
|
61870
|
+
if (n <= 1n) return 1n;
|
|
61871
|
+
let result = 1n;
|
|
61872
|
+
for (const [p, e] of bigPrimeFactors(n)) {
|
|
61873
|
+
const lambda = p === 2n ? e === 1 ? 1n : e === 2 ? 2n : 1n << BigInt(e - 2) : p ** BigInt(e - 1) * (p - 1n);
|
|
61874
|
+
result = lcm2(result, lambda);
|
|
61875
|
+
}
|
|
61876
|
+
return result;
|
|
61877
|
+
}
|
|
61878
|
+
function jacobiSymbol(a, n) {
|
|
61879
|
+
a = (a % n + n) % n;
|
|
61880
|
+
let result = 1;
|
|
61881
|
+
while (a !== 0n) {
|
|
61882
|
+
while (a % 2n === 0n) {
|
|
61883
|
+
a /= 2n;
|
|
61884
|
+
const r = n % 8n;
|
|
61885
|
+
if (r === 3n || r === 5n) result = -result;
|
|
61886
|
+
}
|
|
61887
|
+
[a, n] = [n, a];
|
|
61888
|
+
if (a % 4n === 3n && n % 4n === 3n) result = -result;
|
|
61889
|
+
a %= n;
|
|
61890
|
+
}
|
|
61891
|
+
return n === 1n ? result : 0;
|
|
61892
|
+
}
|
|
61893
|
+
function hasPrimitiveRoot(n) {
|
|
61894
|
+
if (n === 1n || n === 2n || n === 4n) return true;
|
|
61895
|
+
let m = n;
|
|
61896
|
+
let twos = 0;
|
|
61897
|
+
while (m % 2n === 0n) {
|
|
61898
|
+
m /= 2n;
|
|
61899
|
+
twos++;
|
|
61900
|
+
}
|
|
61901
|
+
if (twos > 1) return false;
|
|
61902
|
+
if (m === 1n) return false;
|
|
61903
|
+
return bigPrimeFactors(m).size === 1;
|
|
61904
|
+
}
|
|
61905
|
+
function reduceRat(num, den) {
|
|
61906
|
+
if (den < 0n) {
|
|
61907
|
+
num = -num;
|
|
61908
|
+
den = -den;
|
|
61909
|
+
}
|
|
61910
|
+
const g = gcd2(num < 0n ? -num : num, den);
|
|
61911
|
+
return g > 1n ? [num / g, den / g] : [num, den];
|
|
61912
|
+
}
|
|
61913
|
+
function bernoulliNumber(n, deadline) {
|
|
61914
|
+
const B = [[1n, 1n]];
|
|
61915
|
+
for (let m = 1; m <= n; m++) {
|
|
61916
|
+
checkDeadline(deadline);
|
|
61917
|
+
let sNum = 0n;
|
|
61918
|
+
let sDen = 1n;
|
|
61919
|
+
let c = 1n;
|
|
61920
|
+
for (let k = 0; k < m; k++) {
|
|
61921
|
+
const tNum = c * B[k][0];
|
|
61922
|
+
const tDen = B[k][1];
|
|
61923
|
+
const g = gcd2(sDen, tDen);
|
|
61924
|
+
sNum = sNum * (tDen / g) + tNum * (sDen / g);
|
|
61925
|
+
sDen = sDen / g * tDen;
|
|
61926
|
+
[sNum, sDen] = reduceRat(sNum, sDen);
|
|
61927
|
+
c = c * BigInt(m + 1 - k) / BigInt(k + 1);
|
|
61928
|
+
}
|
|
61929
|
+
B.push(reduceRat(-sNum, sDen * BigInt(m + 1)));
|
|
61930
|
+
}
|
|
61931
|
+
return B[n];
|
|
61932
|
+
}
|
|
60942
61933
|
function sumSquareDigits(k) {
|
|
60943
61934
|
return k.toString().split("").map((d) => BigInt(d)).reduce((sum, d) => sum + d * d, 0n);
|
|
60944
61935
|
}
|
|
@@ -84314,14 +85305,14 @@ var ComputeEngine = class _ComputeEngine {
|
|
|
84314
85305
|
_setDefaultEngineFactory(() => new ComputeEngine());
|
|
84315
85306
|
|
|
84316
85307
|
// src/compute-engine.ts
|
|
84317
|
-
var version = "0.
|
|
85308
|
+
var version = "0.64.0";
|
|
84318
85309
|
ComputeEngine._latexSyntaxFactory = () => new LatexSyntax();
|
|
84319
85310
|
_setDefaultEngineFactory(
|
|
84320
85311
|
() => new ComputeEngine({ latexSyntax: new LatexSyntax() })
|
|
84321
85312
|
);
|
|
84322
85313
|
globalThis[/* @__PURE__ */ Symbol.for("io.cortexjs.compute-engine")] = {
|
|
84323
85314
|
ComputeEngine: ComputeEngine.prototype.constructor,
|
|
84324
|
-
version: "0.
|
|
85315
|
+
version: "0.64.0"
|
|
84325
85316
|
};
|
|
84326
85317
|
export {
|
|
84327
85318
|
DEFINITIONS_ALGEBRA as ALGEBRA_DICTIONARY,
|