mathjs 9.3.2 → 9.4.0
Sign up to get free protection for your applications and to get access to all the features.
- package/HISTORY.md +22 -0
- package/README.md +1 -0
- package/bin/cli.js +11 -11
- package/docs/expressions/parsing.md +21 -4
- package/docs/reference/functions/complex.md +5 -49
- package/docs/reference/functions/eigs.md +12 -12
- package/docs/reference/functions/flatten.md +1 -0
- package/docs/reference/functions/matrixFromColumns.md +1 -1
- package/docs/reference/functions/matrixFromFunction.md +12 -0
- package/docs/reference/functions/matrixFromRows.md +1 -1
- package/docs/reference/functions.md +4 -1
- package/examples/advanced/custom_scope_objects.js +115 -0
- package/lib/browser/math.js +10 -10
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/core/create.js +8 -7
- package/lib/cjs/core/function/import.js +9 -7
- package/lib/cjs/core/function/typed.js +6 -1
- package/lib/cjs/defaultInstance.js +3 -3
- package/lib/cjs/entry/allFactoriesAny.js +3 -3
- package/lib/cjs/entry/allFactoriesNumber.js +3 -3
- package/lib/cjs/entry/configReadonly.js +5 -4
- package/lib/cjs/entry/dependenciesAny/dependenciesEigs.generated.js +51 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesMatrixFromColumns.generated.js +29 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesMatrixFromFunction.generated.js +26 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesMatrixFromRows.generated.js +29 -0
- package/lib/cjs/entry/dependenciesAny.generated.js +24 -0
- package/lib/cjs/entry/impureFunctionsAny.generated.js +22 -17
- package/lib/cjs/entry/impureFunctionsNumber.generated.js +11 -9
- package/lib/cjs/entry/pureFunctionsAny.generated.js +78 -41
- package/lib/cjs/expression/Parser.js +22 -15
- package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +9 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/column.js +1 -1
- package/lib/cjs/expression/embeddedDocs/function/matrix/matrixFromColumns.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/matrixFromFunction.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/matrixFromRows.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/row.js +1 -1
- package/lib/cjs/expression/function/evaluate.js +6 -4
- package/lib/cjs/expression/keywords.js +1 -3
- package/lib/cjs/expression/node/AccessorNode.js +1 -0
- package/lib/cjs/expression/node/AssignmentNode.js +6 -3
- package/lib/cjs/expression/node/FunctionAssignmentNode.js +2 -4
- package/lib/cjs/expression/node/FunctionNode.js +76 -46
- package/lib/cjs/expression/node/IndexNode.js +5 -13
- package/lib/cjs/expression/node/Node.js +15 -9
- package/lib/cjs/expression/node/ObjectNode.js +5 -3
- package/lib/cjs/expression/node/SymbolNode.js +2 -2
- package/lib/cjs/expression/node/utils/access.js +5 -3
- package/lib/cjs/expression/node/utils/assign.js +5 -3
- package/lib/cjs/expression/parse.js +10 -14
- package/lib/cjs/expression/transform/utils/compileInlineExpression.js +5 -3
- package/lib/cjs/factoriesAny.js +24 -0
- package/lib/cjs/function/algebra/decomposition/qr.js +5 -3
- package/lib/cjs/function/algebra/simplify/resolve.js +7 -1
- package/lib/cjs/function/algebra/simplify.js +20 -14
- package/lib/cjs/function/algebra/solver/lsolveAll.js +6 -16
- package/lib/cjs/function/algebra/solver/usolveAll.js +6 -16
- package/lib/cjs/function/arithmetic/ceil.js +6 -16
- package/lib/cjs/function/arithmetic/floor.js +6 -16
- package/lib/cjs/function/arithmetic/norm.js +2 -2
- package/lib/cjs/function/arithmetic/pow.js +9 -7
- package/lib/cjs/function/arithmetic/round.js +7 -5
- package/lib/cjs/function/complex/im.js +3 -0
- package/lib/cjs/function/complex/re.js +3 -0
- package/lib/cjs/function/matrix/eigs/complexEigs.js +631 -0
- package/lib/cjs/function/matrix/eigs/realSymetric.js +341 -0
- package/lib/cjs/function/matrix/eigs.js +188 -334
- package/lib/cjs/function/matrix/flatten.js +1 -0
- package/lib/cjs/function/matrix/matrixFromColumns.js +118 -0
- package/lib/cjs/function/matrix/matrixFromFunction.js +85 -0
- package/lib/cjs/function/matrix/matrixFromRows.js +108 -0
- package/lib/cjs/function/probability/pickRandom.js +1 -1
- package/lib/cjs/function/probability/util/seededRNG.js +2 -2
- package/lib/cjs/function/relational/compareNatural.js +2 -2
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/plain/bignumber/index.js +2 -2
- package/lib/cjs/type/bignumber/BigNumber.js +2 -2
- package/lib/cjs/type/complex/Complex.js +5 -5
- package/lib/cjs/type/fraction/Fraction.js +2 -2
- package/lib/cjs/type/matrix/DenseMatrix.js +138 -0
- package/lib/cjs/type/matrix/Matrix.js +10 -0
- package/lib/cjs/type/matrix/SparseMatrix.js +77 -3
- package/lib/cjs/type/matrix/function/matrix.js +2 -1
- package/lib/cjs/type/unit/Unit.js +15 -13
- package/lib/cjs/utils/array.js +1 -1
- package/lib/cjs/utils/customs.js +18 -4
- package/lib/cjs/utils/emitter.js +2 -2
- package/lib/cjs/utils/function.js +6 -2
- package/lib/cjs/utils/is.js +7 -5
- package/lib/cjs/utils/latex.js +2 -2
- package/lib/cjs/utils/map.js +197 -0
- package/lib/cjs/utils/object.js +6 -5
- package/lib/cjs/utils/scope.js +33 -0
- package/lib/cjs/utils/snapshot.js +17 -16
- package/lib/cjs/utils/string.js +6 -4
- package/lib/cjs/version.js +1 -1
- package/lib/esm/constants.js +15 -15
- package/lib/esm/core/create.js +1 -2
- package/lib/esm/core/function/typed.js +5 -1
- package/lib/esm/entry/configReadonly.js +1 -2
- package/lib/esm/entry/dependenciesAny/dependenciesEigs.generated.js +34 -0
- package/lib/esm/entry/dependenciesAny/dependenciesMatrixFromColumns.generated.js +16 -0
- package/lib/esm/entry/dependenciesAny/dependenciesMatrixFromFunction.generated.js +14 -0
- package/lib/esm/entry/dependenciesAny/dependenciesMatrixFromRows.generated.js +16 -0
- package/lib/esm/entry/dependenciesAny.generated.js +3 -0
- package/lib/esm/entry/impureFunctionsAny.generated.js +13 -10
- package/lib/esm/entry/impureFunctionsNumber.generated.js +1 -1
- package/lib/esm/entry/pureFunctionsAny.generated.js +66 -32
- package/lib/esm/expression/Help.js +1 -1
- package/lib/esm/expression/Parser.js +23 -15
- package/lib/esm/expression/embeddedDocs/embeddedDocs.js +6 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/column.js +1 -1
- package/lib/esm/expression/embeddedDocs/function/matrix/matrixFromColumns.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/matrixFromFunction.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/matrixFromRows.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/row.js +1 -1
- package/lib/esm/expression/function/compile.js +1 -1
- package/lib/esm/expression/function/evaluate.js +6 -5
- package/lib/esm/expression/function/help.js +1 -1
- package/lib/esm/expression/function/parser.js +1 -1
- package/lib/esm/expression/keywords.js +1 -3
- package/lib/esm/expression/node/AccessorNode.js +2 -1
- package/lib/esm/expression/node/ArrayNode.js +1 -1
- package/lib/esm/expression/node/AssignmentNode.js +7 -4
- package/lib/esm/expression/node/BlockNode.js +1 -1
- package/lib/esm/expression/node/ConditionalNode.js +1 -1
- package/lib/esm/expression/node/ConstantNode.js +1 -1
- package/lib/esm/expression/node/FunctionAssignmentNode.js +3 -4
- package/lib/esm/expression/node/FunctionNode.js +57 -39
- package/lib/esm/expression/node/IndexNode.js +1 -1
- package/lib/esm/expression/node/Node.js +7 -8
- package/lib/esm/expression/node/ObjectNode.js +1 -1
- package/lib/esm/expression/node/OperatorNode.js +1 -1
- package/lib/esm/expression/node/ParenthesisNode.js +1 -1
- package/lib/esm/expression/node/RangeNode.js +1 -1
- package/lib/esm/expression/node/RelationalNode.js +1 -1
- package/lib/esm/expression/node/SymbolNode.js +3 -3
- package/lib/esm/expression/parse.js +2 -3
- package/lib/esm/expression/transform/apply.transform.js +1 -1
- package/lib/esm/expression/transform/column.transform.js +1 -1
- package/lib/esm/expression/transform/concat.transform.js +1 -1
- package/lib/esm/expression/transform/diff.transform.js +1 -1
- package/lib/esm/expression/transform/filter.transform.js +1 -1
- package/lib/esm/expression/transform/forEach.transform.js +1 -1
- package/lib/esm/expression/transform/index.transform.js +1 -1
- package/lib/esm/expression/transform/map.transform.js +1 -1
- package/lib/esm/expression/transform/max.transform.js +1 -1
- package/lib/esm/expression/transform/mean.transform.js +1 -1
- package/lib/esm/expression/transform/min.transform.js +1 -1
- package/lib/esm/expression/transform/range.transform.js +1 -1
- package/lib/esm/expression/transform/row.transform.js +1 -1
- package/lib/esm/expression/transform/std.transform.js +1 -1
- package/lib/esm/expression/transform/subset.transform.js +1 -1
- package/lib/esm/expression/transform/sum.transform.js +1 -1
- package/lib/esm/expression/transform/utils/compileInlineExpression.js +4 -3
- package/lib/esm/expression/transform/variance.transform.js +1 -1
- package/lib/esm/factoriesAny.js +3 -0
- package/lib/esm/factoriesNumber.js +1 -1
- package/lib/esm/function/algebra/decomposition/lup.js +1 -1
- package/lib/esm/function/algebra/decomposition/qr.js +2 -3
- package/lib/esm/function/algebra/decomposition/slu.js +1 -1
- package/lib/esm/function/algebra/derivative.js +1 -1
- package/lib/esm/function/algebra/rationalize.js +1 -1
- package/lib/esm/function/algebra/simplify/resolve.js +7 -2
- package/lib/esm/function/algebra/simplify/simplifyConstant.js +1 -1
- package/lib/esm/function/algebra/simplify/simplifyCore.js +1 -1
- package/lib/esm/function/algebra/simplify/util.js +1 -1
- package/lib/esm/function/algebra/simplify.js +16 -12
- package/lib/esm/function/algebra/solver/lsolve.js +1 -1
- package/lib/esm/function/algebra/solver/lsolveAll.js +1 -1
- package/lib/esm/function/algebra/solver/lusolve.js +1 -1
- package/lib/esm/function/algebra/solver/usolve.js +1 -1
- package/lib/esm/function/algebra/solver/usolveAll.js +1 -1
- package/lib/esm/function/algebra/sparse/csAmd.js +1 -1
- package/lib/esm/function/algebra/sparse/csChol.js +1 -1
- package/lib/esm/function/algebra/sparse/csCounts.js +1 -1
- package/lib/esm/function/algebra/sparse/csLu.js +1 -1
- package/lib/esm/function/algebra/sparse/csSpsolve.js +1 -1
- package/lib/esm/function/algebra/sparse/csSqr.js +1 -1
- package/lib/esm/function/algebra/sparse/csSymperm.js +1 -1
- package/lib/esm/function/arithmetic/abs.js +1 -1
- package/lib/esm/function/arithmetic/add.js +1 -1
- package/lib/esm/function/arithmetic/addScalar.js +1 -1
- package/lib/esm/function/arithmetic/cbrt.js +1 -1
- package/lib/esm/function/arithmetic/ceil.js +1 -1
- package/lib/esm/function/arithmetic/cube.js +1 -1
- package/lib/esm/function/arithmetic/divide.js +1 -1
- package/lib/esm/function/arithmetic/divideScalar.js +1 -1
- package/lib/esm/function/arithmetic/dotDivide.js +1 -1
- package/lib/esm/function/arithmetic/dotMultiply.js +1 -1
- package/lib/esm/function/arithmetic/dotPow.js +1 -1
- package/lib/esm/function/arithmetic/exp.js +1 -1
- package/lib/esm/function/arithmetic/expm1.js +1 -1
- package/lib/esm/function/arithmetic/fix.js +1 -1
- package/lib/esm/function/arithmetic/floor.js +1 -1
- package/lib/esm/function/arithmetic/gcd.js +1 -1
- package/lib/esm/function/arithmetic/hypot.js +1 -1
- package/lib/esm/function/arithmetic/lcm.js +1 -1
- package/lib/esm/function/arithmetic/log.js +1 -1
- package/lib/esm/function/arithmetic/log10.js +1 -1
- package/lib/esm/function/arithmetic/log1p.js +1 -1
- package/lib/esm/function/arithmetic/log2.js +1 -1
- package/lib/esm/function/arithmetic/mod.js +1 -1
- package/lib/esm/function/arithmetic/multiply.js +1 -1
- package/lib/esm/function/arithmetic/multiplyScalar.js +1 -1
- package/lib/esm/function/arithmetic/norm.js +3 -3
- package/lib/esm/function/arithmetic/nthRoot.js +2 -2
- package/lib/esm/function/arithmetic/nthRoots.js +1 -1
- package/lib/esm/function/arithmetic/pow.js +10 -8
- package/lib/esm/function/arithmetic/round.js +6 -6
- package/lib/esm/function/arithmetic/sign.js +1 -1
- package/lib/esm/function/arithmetic/sqrt.js +1 -1
- package/lib/esm/function/arithmetic/square.js +1 -1
- package/lib/esm/function/arithmetic/subtract.js +1 -1
- package/lib/esm/function/arithmetic/unaryMinus.js +1 -1
- package/lib/esm/function/arithmetic/unaryPlus.js +1 -1
- package/lib/esm/function/arithmetic/xgcd.js +1 -1
- package/lib/esm/function/bitwise/bitAnd.js +1 -1
- package/lib/esm/function/bitwise/bitNot.js +1 -1
- package/lib/esm/function/bitwise/bitOr.js +1 -1
- package/lib/esm/function/bitwise/bitXor.js +1 -1
- package/lib/esm/function/bitwise/leftShift.js +1 -1
- package/lib/esm/function/bitwise/rightArithShift.js +1 -1
- package/lib/esm/function/bitwise/rightLogShift.js +1 -1
- package/lib/esm/function/combinatorics/bellNumbers.js +1 -1
- package/lib/esm/function/combinatorics/catalan.js +1 -1
- package/lib/esm/function/combinatorics/composition.js +1 -1
- package/lib/esm/function/combinatorics/stirlingS2.js +1 -1
- package/lib/esm/function/complex/arg.js +1 -1
- package/lib/esm/function/complex/conj.js +1 -1
- package/lib/esm/function/complex/im.js +4 -1
- package/lib/esm/function/complex/re.js +4 -1
- package/lib/esm/function/geometry/distance.js +1 -1
- package/lib/esm/function/geometry/intersect.js +1 -1
- package/lib/esm/function/logical/and.js +1 -1
- package/lib/esm/function/logical/not.js +1 -1
- package/lib/esm/function/logical/or.js +1 -1
- package/lib/esm/function/logical/xor.js +1 -1
- package/lib/esm/function/matrix/apply.js +1 -1
- package/lib/esm/function/matrix/column.js +1 -1
- package/lib/esm/function/matrix/concat.js +1 -1
- package/lib/esm/function/matrix/count.js +1 -1
- package/lib/esm/function/matrix/cross.js +1 -1
- package/lib/esm/function/matrix/ctranspose.js +1 -1
- package/lib/esm/function/matrix/det.js +1 -1
- package/lib/esm/function/matrix/diag.js +1 -1
- package/lib/esm/function/matrix/diff.js +1 -1
- package/lib/esm/function/matrix/dot.js +1 -1
- package/lib/esm/function/matrix/eigs/complexEigs.js +586 -0
- package/lib/esm/function/matrix/eigs/realSymetric.js +335 -0
- package/lib/esm/function/matrix/eigs.js +187 -335
- package/lib/esm/function/matrix/expm.js +1 -1
- package/lib/esm/function/matrix/filter.js +1 -1
- package/lib/esm/function/matrix/flatten.js +2 -1
- package/lib/esm/function/matrix/forEach.js +1 -1
- package/lib/esm/function/matrix/getMatrixDataType.js +1 -1
- package/lib/esm/function/matrix/identity.js +1 -1
- package/lib/esm/function/matrix/inv.js +1 -1
- package/lib/esm/function/matrix/kron.js +1 -1
- package/lib/esm/function/matrix/map.js +1 -1
- package/lib/esm/function/matrix/matrixFromColumns.js +93 -0
- package/lib/esm/function/matrix/matrixFromFunction.js +78 -0
- package/lib/esm/function/matrix/matrixFromRows.js +83 -0
- package/lib/esm/function/matrix/ones.js +1 -1
- package/lib/esm/function/matrix/partitionSelect.js +1 -1
- package/lib/esm/function/matrix/range.js +1 -1
- package/lib/esm/function/matrix/reshape.js +1 -1
- package/lib/esm/function/matrix/resize.js +1 -1
- package/lib/esm/function/matrix/rotate.js +1 -1
- package/lib/esm/function/matrix/rotationMatrix.js +1 -1
- package/lib/esm/function/matrix/row.js +1 -1
- package/lib/esm/function/matrix/size.js +1 -1
- package/lib/esm/function/matrix/sort.js +1 -1
- package/lib/esm/function/matrix/sqrtm.js +1 -1
- package/lib/esm/function/matrix/squeeze.js +1 -1
- package/lib/esm/function/matrix/subset.js +1 -1
- package/lib/esm/function/matrix/trace.js +1 -1
- package/lib/esm/function/matrix/transpose.js +1 -1
- package/lib/esm/function/matrix/zeros.js +1 -1
- package/lib/esm/function/probability/combinations.js +1 -1
- package/lib/esm/function/probability/combinationsWithRep.js +1 -1
- package/lib/esm/function/probability/factorial.js +1 -1
- package/lib/esm/function/probability/gamma.js +1 -1
- package/lib/esm/function/probability/kldivergence.js +1 -1
- package/lib/esm/function/probability/multinomial.js +1 -1
- package/lib/esm/function/probability/permutations.js +1 -1
- package/lib/esm/function/probability/pickRandom.js +2 -2
- package/lib/esm/function/probability/random.js +2 -2
- package/lib/esm/function/probability/randomInt.js +1 -1
- package/lib/esm/function/relational/compare.js +2 -2
- package/lib/esm/function/relational/compareNatural.js +1 -1
- package/lib/esm/function/relational/compareText.js +2 -2
- package/lib/esm/function/relational/deepEqual.js +1 -1
- package/lib/esm/function/relational/equal.js +2 -2
- package/lib/esm/function/relational/equalScalar.js +2 -2
- package/lib/esm/function/relational/equalText.js +1 -1
- package/lib/esm/function/relational/larger.js +2 -2
- package/lib/esm/function/relational/largerEq.js +2 -2
- package/lib/esm/function/relational/smaller.js +2 -2
- package/lib/esm/function/relational/smallerEq.js +2 -2
- package/lib/esm/function/relational/unequal.js +2 -2
- package/lib/esm/function/set/setCartesian.js +1 -1
- package/lib/esm/function/set/setDifference.js +1 -1
- package/lib/esm/function/set/setDistinct.js +1 -1
- package/lib/esm/function/set/setIntersect.js +1 -1
- package/lib/esm/function/set/setIsSubset.js +1 -1
- package/lib/esm/function/set/setMultiplicity.js +1 -1
- package/lib/esm/function/set/setPowerset.js +1 -1
- package/lib/esm/function/set/setSize.js +1 -1
- package/lib/esm/function/set/setSymDifference.js +1 -1
- package/lib/esm/function/set/setUnion.js +1 -1
- package/lib/esm/function/special/erf.js +1 -1
- package/lib/esm/function/statistics/mad.js +1 -1
- package/lib/esm/function/statistics/max.js +1 -1
- package/lib/esm/function/statistics/mean.js +1 -1
- package/lib/esm/function/statistics/median.js +1 -1
- package/lib/esm/function/statistics/min.js +1 -1
- package/lib/esm/function/statistics/mode.js +1 -1
- package/lib/esm/function/statistics/prod.js +1 -1
- package/lib/esm/function/statistics/quantileSeq.js +1 -1
- package/lib/esm/function/statistics/std.js +1 -1
- package/lib/esm/function/statistics/sum.js +1 -1
- package/lib/esm/function/statistics/variance.js +1 -1
- package/lib/esm/function/string/bin.js +1 -1
- package/lib/esm/function/string/format.js +1 -1
- package/lib/esm/function/string/hex.js +1 -1
- package/lib/esm/function/string/oct.js +1 -1
- package/lib/esm/function/string/print.js +1 -1
- package/lib/esm/function/trigonometry/acos.js +1 -1
- package/lib/esm/function/trigonometry/acosh.js +1 -1
- package/lib/esm/function/trigonometry/acot.js +1 -1
- package/lib/esm/function/trigonometry/acoth.js +1 -1
- package/lib/esm/function/trigonometry/acsc.js +1 -1
- package/lib/esm/function/trigonometry/acsch.js +1 -1
- package/lib/esm/function/trigonometry/asec.js +1 -1
- package/lib/esm/function/trigonometry/asech.js +1 -1
- package/lib/esm/function/trigonometry/asin.js +1 -1
- package/lib/esm/function/trigonometry/asinh.js +1 -1
- package/lib/esm/function/trigonometry/atan.js +1 -1
- package/lib/esm/function/trigonometry/atan2.js +1 -1
- package/lib/esm/function/trigonometry/atanh.js +1 -1
- package/lib/esm/function/trigonometry/cos.js +1 -1
- package/lib/esm/function/trigonometry/cosh.js +1 -1
- package/lib/esm/function/trigonometry/cot.js +1 -1
- package/lib/esm/function/trigonometry/coth.js +1 -1
- package/lib/esm/function/trigonometry/csc.js +1 -1
- package/lib/esm/function/trigonometry/csch.js +1 -1
- package/lib/esm/function/trigonometry/sec.js +1 -1
- package/lib/esm/function/trigonometry/sech.js +1 -1
- package/lib/esm/function/trigonometry/sin.js +1 -1
- package/lib/esm/function/trigonometry/sinh.js +1 -1
- package/lib/esm/function/trigonometry/tan.js +1 -1
- package/lib/esm/function/trigonometry/tanh.js +1 -1
- package/lib/esm/function/unit/to.js +1 -1
- package/lib/esm/function/utils/clone.js +1 -1
- package/lib/esm/function/utils/hasNumericValue.js +1 -1
- package/lib/esm/function/utils/isInteger.js +1 -1
- package/lib/esm/function/utils/isNaN.js +1 -1
- package/lib/esm/function/utils/isNegative.js +1 -1
- package/lib/esm/function/utils/isNumeric.js +1 -1
- package/lib/esm/function/utils/isPositive.js +1 -1
- package/lib/esm/function/utils/isPrime.js +1 -1
- package/lib/esm/function/utils/isZero.js +1 -1
- package/lib/esm/function/utils/numeric.js +1 -1
- package/lib/esm/function/utils/typeOf.js +1 -1
- package/lib/esm/json/reviver.js +1 -1
- package/lib/esm/type/bignumber/BigNumber.js +1 -1
- package/lib/esm/type/bignumber/function/bignumber.js +1 -1
- package/lib/esm/type/boolean.js +1 -1
- package/lib/esm/type/chain/Chain.js +1 -1
- package/lib/esm/type/chain/function/chain.js +1 -1
- package/lib/esm/type/complex/function/complex.js +1 -1
- package/lib/esm/type/fraction/function/fraction.js +1 -1
- package/lib/esm/type/matrix/DenseMatrix.js +75 -1
- package/lib/esm/type/matrix/FibonacciHeap.js +1 -1
- package/lib/esm/type/matrix/ImmutableDenseMatrix.js +1 -1
- package/lib/esm/type/matrix/Matrix.js +10 -0
- package/lib/esm/type/matrix/MatrixIndex.js +1 -1
- package/lib/esm/type/matrix/Spa.js +1 -1
- package/lib/esm/type/matrix/SparseMatrix.js +37 -3
- package/lib/esm/type/matrix/function/index.js +1 -1
- package/lib/esm/type/matrix/function/matrix.js +3 -2
- package/lib/esm/type/matrix/function/sparse.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm01.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm02.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm03.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm04.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm05.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm06.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm07.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm08.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm09.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm10.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm11.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm12.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm13.js +1 -1
- package/lib/esm/type/matrix/utils/algorithm14.js +1 -1
- package/lib/esm/type/number.js +1 -1
- package/lib/esm/type/string.js +1 -1
- package/lib/esm/type/unit/Unit.js +4 -5
- package/lib/esm/type/unit/function/createUnit.js +1 -1
- package/lib/esm/type/unit/function/splitUnit.js +1 -1
- package/lib/esm/type/unit/function/unit.js +1 -1
- package/lib/esm/type/unit/physicalConstants.js +2 -2
- package/lib/esm/utils/array.js +1 -1
- package/lib/esm/utils/customs.js +10 -0
- package/lib/esm/utils/map.js +135 -0
- package/lib/esm/utils/scope.js +26 -0
- package/lib/esm/utils/snapshot.js +3 -4
- package/lib/esm/version.js +1 -1
- package/package.json +18 -9
@@ -0,0 +1,631 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
6
|
+
value: true
|
7
|
+
});
|
8
|
+
exports.createComplexEigs = createComplexEigs;
|
9
|
+
|
10
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
11
|
+
|
12
|
+
var _object = require("../../../utils/object.js");
|
13
|
+
|
14
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
15
|
+
|
16
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
17
|
+
|
18
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
19
|
+
|
20
|
+
function createComplexEigs(_ref) {
|
21
|
+
var addScalar = _ref.addScalar,
|
22
|
+
subtract = _ref.subtract,
|
23
|
+
flatten = _ref.flatten,
|
24
|
+
multiply = _ref.multiply,
|
25
|
+
multiplyScalar = _ref.multiplyScalar,
|
26
|
+
divideScalar = _ref.divideScalar,
|
27
|
+
sqrt = _ref.sqrt,
|
28
|
+
abs = _ref.abs,
|
29
|
+
bignumber = _ref.bignumber,
|
30
|
+
diag = _ref.diag,
|
31
|
+
inv = _ref.inv,
|
32
|
+
qr = _ref.qr,
|
33
|
+
usolveAll = _ref.usolveAll,
|
34
|
+
equal = _ref.equal,
|
35
|
+
complex = _ref.complex,
|
36
|
+
larger = _ref.larger,
|
37
|
+
smaller = _ref.smaller,
|
38
|
+
round = _ref.round,
|
39
|
+
log10 = _ref.log10,
|
40
|
+
transpose = _ref.transpose,
|
41
|
+
matrixFromColumns = _ref.matrixFromColumns;
|
42
|
+
|
43
|
+
/**
|
44
|
+
* @param {number[][]} arr the matrix to find eigenvalues of
|
45
|
+
* @param {number} N size of the matrix
|
46
|
+
* @param {number|BigNumber} prec precision, anything lower will be considered zero
|
47
|
+
* @param {'number'|'BigNumber'|'Complex'} type
|
48
|
+
* @param {boolean} findVectors should we find eigenvectors?
|
49
|
+
*
|
50
|
+
* @returns {{ values: number[], vectors: number[][] }}
|
51
|
+
*/
|
52
|
+
function complexEigs(arr, N, prec, type, findVectors) {
|
53
|
+
if (findVectors === undefined) {
|
54
|
+
findVectors = true;
|
55
|
+
} // TODO check if any row/col are zero except the diagonal
|
56
|
+
// make sure corresponding rows and columns have similar magnitude
|
57
|
+
// important because of numerical stability
|
58
|
+
|
59
|
+
|
60
|
+
var R = balance(arr, N, prec, type, findVectors); // R is the row transformation matrix
|
61
|
+
// A' = R A R⁻¹, A is the original matrix
|
62
|
+
// (if findVectors is false, R is undefined)
|
63
|
+
// TODO if magnitudes of elements vary over many orders,
|
64
|
+
// move greatest elements to the top left corner
|
65
|
+
// using similarity transformations, reduce the matrix
|
66
|
+
// to Hessenberg form (upper triangular plus one subdiagonal row)
|
67
|
+
// updates the transformation matrix R with new row operationsq
|
68
|
+
|
69
|
+
reduceToHessenberg(arr, N, prec, type, findVectors, R); // find eigenvalues
|
70
|
+
|
71
|
+
var _iterateUntilTriangul = iterateUntilTriangular(arr, N, prec, type, findVectors),
|
72
|
+
values = _iterateUntilTriangul.values,
|
73
|
+
C = _iterateUntilTriangul.C; // values is the list of eigenvalues, C is the column
|
74
|
+
// transformation matrix that transforms the hessenberg
|
75
|
+
// matrix to upper triangular
|
76
|
+
// compose transformations A → hess. and hess. → triang.
|
77
|
+
|
78
|
+
|
79
|
+
C = multiply(inv(R), C);
|
80
|
+
var vectors;
|
81
|
+
|
82
|
+
if (findVectors) {
|
83
|
+
vectors = findEigenvectors(arr, N, C, values, prec);
|
84
|
+
vectors = matrixFromColumns.apply(void 0, (0, _toConsumableArray2["default"])(vectors));
|
85
|
+
}
|
86
|
+
|
87
|
+
return {
|
88
|
+
values: values,
|
89
|
+
vectors: vectors
|
90
|
+
};
|
91
|
+
}
|
92
|
+
/**
|
93
|
+
* @param {number[][]} arr
|
94
|
+
* @param {number} N
|
95
|
+
* @param {number} prec
|
96
|
+
* @param {'number'|'BigNumber'|'Complex'} type
|
97
|
+
* @returns {number[][]}
|
98
|
+
*/
|
99
|
+
|
100
|
+
|
101
|
+
function balance(arr, N, prec, type, findVectors) {
|
102
|
+
var big = type === 'BigNumber';
|
103
|
+
var cplx = type === 'Complex';
|
104
|
+
var zero = big ? bignumber(0) : cplx ? complex(0) : 0;
|
105
|
+
var one = big ? bignumber(1) : cplx ? complex(1) : 1; // base of the floating-point arithmetic
|
106
|
+
|
107
|
+
var radix = big ? bignumber(10) : 2;
|
108
|
+
var radixSq = multiplyScalar(radix, radix); // the diagonal transformation matrix R
|
109
|
+
|
110
|
+
var Rdiag;
|
111
|
+
|
112
|
+
if (findVectors) {
|
113
|
+
Rdiag = Array(N).fill(one);
|
114
|
+
} // this isn't the only time we loop thru the matrix...
|
115
|
+
|
116
|
+
|
117
|
+
var last = false;
|
118
|
+
|
119
|
+
while (!last) {
|
120
|
+
// ...haha I'm joking! unless...
|
121
|
+
last = true;
|
122
|
+
|
123
|
+
for (var i = 0; i < N; i++) {
|
124
|
+
// compute the taxicab norm of i-th column and row
|
125
|
+
// TODO optimize for complex numbers
|
126
|
+
var colNorm = zero;
|
127
|
+
var rowNorm = zero;
|
128
|
+
|
129
|
+
for (var j = 0; j < N; j++) {
|
130
|
+
if (i === j) continue;
|
131
|
+
var c = abs(arr[i][j]);
|
132
|
+
colNorm = addScalar(colNorm, c);
|
133
|
+
rowNorm = addScalar(rowNorm, c);
|
134
|
+
}
|
135
|
+
|
136
|
+
if (!equal(colNorm, 0) && !equal(rowNorm, 0)) {
|
137
|
+
// find integer power closest to balancing the matrix
|
138
|
+
// (we want to scale only by integer powers of radix,
|
139
|
+
// so that we don't lose any precision due to round-off)
|
140
|
+
var f = one;
|
141
|
+
var _c = colNorm;
|
142
|
+
var rowDivRadix = divideScalar(rowNorm, radix);
|
143
|
+
var rowMulRadix = multiplyScalar(rowNorm, radix);
|
144
|
+
|
145
|
+
while (smaller(_c, rowDivRadix)) {
|
146
|
+
_c = multiplyScalar(_c, radixSq);
|
147
|
+
f = multiplyScalar(f, radix);
|
148
|
+
}
|
149
|
+
|
150
|
+
while (larger(_c, rowMulRadix)) {
|
151
|
+
_c = divideScalar(_c, radixSq);
|
152
|
+
f = divideScalar(f, radix);
|
153
|
+
} // check whether balancing is needed
|
154
|
+
// condition = (c + rowNorm) / f < 0.95 * (colNorm + rowNorm)
|
155
|
+
|
156
|
+
|
157
|
+
var condition = smaller(divideScalar(addScalar(_c, rowNorm), f), multiplyScalar(addScalar(colNorm, rowNorm), 0.95)); // apply balancing similarity transformation
|
158
|
+
|
159
|
+
if (condition) {
|
160
|
+
// we should loop once again to check whether
|
161
|
+
// another rebalancing is needed
|
162
|
+
last = false;
|
163
|
+
var g = divideScalar(1, f);
|
164
|
+
|
165
|
+
for (var _j = 0; _j < N; _j++) {
|
166
|
+
if (i === _j) {
|
167
|
+
continue;
|
168
|
+
}
|
169
|
+
|
170
|
+
arr[i][_j] = multiplyScalar(arr[i][_j], f);
|
171
|
+
arr[_j][i] = multiplyScalar(arr[_j][i], g);
|
172
|
+
} // keep track of transformations
|
173
|
+
|
174
|
+
|
175
|
+
if (findVectors) {
|
176
|
+
Rdiag[i] = multiplyScalar(Rdiag[i], f);
|
177
|
+
}
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}
|
181
|
+
} // return the diagonal row transformation matrix
|
182
|
+
|
183
|
+
|
184
|
+
return diag(Rdiag);
|
185
|
+
}
|
186
|
+
/**
|
187
|
+
* @param {number[][]} arr
|
188
|
+
* @param {number} N
|
189
|
+
* @param {number} prec
|
190
|
+
* @param {'number'|'BigNumber'|'Complex'} type
|
191
|
+
* @param {boolean} findVectors
|
192
|
+
* @param {number[][]} R the row transformation matrix that will be modified
|
193
|
+
*/
|
194
|
+
|
195
|
+
|
196
|
+
function reduceToHessenberg(arr, N, prec, type, findVectors, R) {
|
197
|
+
var big = type === 'BigNumber';
|
198
|
+
var cplx = type === 'Complex';
|
199
|
+
var zero = big ? bignumber(0) : cplx ? complex(0) : 0;
|
200
|
+
|
201
|
+
if (big) {
|
202
|
+
prec = bignumber(prec);
|
203
|
+
}
|
204
|
+
|
205
|
+
for (var i = 0; i < N - 2; i++) {
|
206
|
+
// Find the largest subdiag element in the i-th col
|
207
|
+
var maxIndex = 0;
|
208
|
+
var max = zero;
|
209
|
+
|
210
|
+
for (var j = i + 1; j < N; j++) {
|
211
|
+
var el = arr[j][i];
|
212
|
+
|
213
|
+
if (smaller(abs(max), abs(el))) {
|
214
|
+
max = el;
|
215
|
+
maxIndex = j;
|
216
|
+
}
|
217
|
+
} // This col is pivoted, no need to do anything
|
218
|
+
|
219
|
+
|
220
|
+
if (smaller(abs(max), prec)) {
|
221
|
+
continue;
|
222
|
+
}
|
223
|
+
|
224
|
+
if (maxIndex !== i + 1) {
|
225
|
+
// Interchange maxIndex-th and (i+1)-th row
|
226
|
+
var tmp1 = arr[maxIndex];
|
227
|
+
arr[maxIndex] = arr[i + 1];
|
228
|
+
arr[i + 1] = tmp1; // Interchange maxIndex-th and (i+1)-th column
|
229
|
+
|
230
|
+
for (var _j2 = 0; _j2 < N; _j2++) {
|
231
|
+
var tmp2 = arr[_j2][maxIndex];
|
232
|
+
arr[_j2][maxIndex] = arr[_j2][i + 1];
|
233
|
+
arr[_j2][i + 1] = tmp2;
|
234
|
+
} // keep track of transformations
|
235
|
+
|
236
|
+
|
237
|
+
if (findVectors) {
|
238
|
+
var tmp3 = R[maxIndex];
|
239
|
+
R[maxIndex] = R[i + 1];
|
240
|
+
R[i + 1] = tmp3;
|
241
|
+
}
|
242
|
+
} // Reduce following rows and columns
|
243
|
+
|
244
|
+
|
245
|
+
for (var _j3 = i + 2; _j3 < N; _j3++) {
|
246
|
+
var n = divideScalar(arr[_j3][i], max);
|
247
|
+
|
248
|
+
if (n === 0) {
|
249
|
+
continue;
|
250
|
+
} // from j-th row subtract n-times (i+1)th row
|
251
|
+
|
252
|
+
|
253
|
+
for (var k = 0; k < N; k++) {
|
254
|
+
arr[_j3][k] = subtract(arr[_j3][k], multiplyScalar(n, arr[i + 1][k]));
|
255
|
+
} // to (i+1)th column add n-times j-th column
|
256
|
+
|
257
|
+
|
258
|
+
for (var _k = 0; _k < N; _k++) {
|
259
|
+
arr[_k][i + 1] = addScalar(arr[_k][i + 1], multiplyScalar(n, arr[_k][_j3]));
|
260
|
+
} // keep track of transformations
|
261
|
+
|
262
|
+
|
263
|
+
if (findVectors) {
|
264
|
+
for (var _k2 = 0; _k2 < N; _k2++) {
|
265
|
+
R[_j3][_k2] = subtract(R[_j3][_k2], multiplyScalar(n, R[i + 1][_k2]));
|
266
|
+
}
|
267
|
+
}
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
return R;
|
272
|
+
}
|
273
|
+
/**
|
274
|
+
* @returns {{values: values, C: Matrix}}
|
275
|
+
* @see Press, Wiliams: Numerical recipes in Fortran 77
|
276
|
+
* @see https://en.wikipedia.org/wiki/QR_algorithm
|
277
|
+
*/
|
278
|
+
|
279
|
+
|
280
|
+
function iterateUntilTriangular(A, N, prec, type, findVectors) {
|
281
|
+
var big = type === 'BigNumber';
|
282
|
+
var cplx = type === 'Complex';
|
283
|
+
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
|
284
|
+
|
285
|
+
if (big) {
|
286
|
+
prec = bignumber(prec);
|
287
|
+
} // The Francis Algorithm
|
288
|
+
// The core idea of this algorithm is that doing successive
|
289
|
+
// A' = Q⁺AQ transformations will eventually converge to block-
|
290
|
+
// upper-triangular with diagonal blocks either 1x1 or 2x2.
|
291
|
+
// The Q here is the one from the QR decomposition, A = QR.
|
292
|
+
// Since the eigenvalues of a block-upper-triangular matrix are
|
293
|
+
// the eigenvalues of its diagonal blocks and we know how to find
|
294
|
+
// eigenvalues of a 2x2 matrix, we know the eigenvalues of A.
|
295
|
+
|
296
|
+
|
297
|
+
var arr = (0, _object.clone)(A); // the list of converged eigenvalues
|
298
|
+
|
299
|
+
var lambdas = []; // size of arr, which will get smaller as eigenvalues converge
|
300
|
+
|
301
|
+
var n = N; // the diagonal of the block-diagonal matrix that turns
|
302
|
+
// converged 2x2 matrices into upper triangular matrices
|
303
|
+
|
304
|
+
var Sdiag = []; // N×N matrix describing the overall transformation done during the QR algorithm
|
305
|
+
|
306
|
+
var Qtotal = findVectors ? diag(Array(N).fill(one)) : undefined; // n×n matrix describing the QR transformations done since last convergence
|
307
|
+
|
308
|
+
var Qpartial = findVectors ? diag(Array(n).fill(one)) : undefined; // last eigenvalue converged before this many steps
|
309
|
+
|
310
|
+
var lastConvergenceBefore = 0;
|
311
|
+
|
312
|
+
while (lastConvergenceBefore <= 100) {
|
313
|
+
lastConvergenceBefore += 1; // TODO if the convergence is slow, do something clever
|
314
|
+
// Perform the factorization
|
315
|
+
|
316
|
+
var k = 0; // TODO set close to an eigenvalue
|
317
|
+
|
318
|
+
for (var i = 0; i < n; i++) {
|
319
|
+
arr[i][i] = subtract(arr[i][i], k);
|
320
|
+
} // TODO do an implicit QR transformation
|
321
|
+
|
322
|
+
|
323
|
+
var _qr = qr(arr),
|
324
|
+
Q = _qr.Q,
|
325
|
+
R = _qr.R;
|
326
|
+
|
327
|
+
arr = multiply(R, Q);
|
328
|
+
|
329
|
+
for (var _i = 0; _i < n; _i++) {
|
330
|
+
arr[_i][_i] = addScalar(arr[_i][_i], k);
|
331
|
+
} // keep track of transformations
|
332
|
+
|
333
|
+
|
334
|
+
if (findVectors) {
|
335
|
+
Qpartial = multiply(Qpartial, Q);
|
336
|
+
} // The rightmost diagonal element converged to an eigenvalue
|
337
|
+
|
338
|
+
|
339
|
+
if (n === 1 || smaller(abs(arr[n - 1][n - 2]), prec)) {
|
340
|
+
lastConvergenceBefore = 0;
|
341
|
+
lambdas.push(arr[n - 1][n - 1]); // keep track of transformations
|
342
|
+
|
343
|
+
if (findVectors) {
|
344
|
+
Sdiag.unshift([[1]]);
|
345
|
+
inflateMatrix(Qpartial, N);
|
346
|
+
Qtotal = multiply(Qtotal, Qpartial);
|
347
|
+
|
348
|
+
if (n > 1) {
|
349
|
+
Qpartial = diag(Array(n - 1).fill(one));
|
350
|
+
}
|
351
|
+
} // reduce the matrix size
|
352
|
+
|
353
|
+
|
354
|
+
n -= 1;
|
355
|
+
arr.pop();
|
356
|
+
|
357
|
+
for (var _i2 = 0; _i2 < n; _i2++) {
|
358
|
+
arr[_i2].pop();
|
359
|
+
} // The rightmost diagonal 2x2 block converged
|
360
|
+
|
361
|
+
} else if (n === 2 || smaller(abs(arr[n - 2][n - 3]), prec)) {
|
362
|
+
lastConvergenceBefore = 0;
|
363
|
+
var ll = eigenvalues2x2(arr[n - 2][n - 2], arr[n - 2][n - 1], arr[n - 1][n - 2], arr[n - 1][n - 1]);
|
364
|
+
lambdas.push.apply(lambdas, (0, _toConsumableArray2["default"])(ll)); // keep track of transformations
|
365
|
+
|
366
|
+
if (findVectors) {
|
367
|
+
Sdiag.unshift(jordanBase2x2(arr[n - 2][n - 2], arr[n - 2][n - 1], arr[n - 1][n - 2], arr[n - 1][n - 1], ll[0], ll[1], prec, type));
|
368
|
+
inflateMatrix(Qpartial, N);
|
369
|
+
Qtotal = multiply(Qtotal, Qpartial);
|
370
|
+
|
371
|
+
if (n > 2) {
|
372
|
+
Qpartial = diag(Array(n - 2).fill(one));
|
373
|
+
}
|
374
|
+
} // reduce the matrix size
|
375
|
+
|
376
|
+
|
377
|
+
n -= 2;
|
378
|
+
arr.pop();
|
379
|
+
arr.pop();
|
380
|
+
|
381
|
+
for (var _i3 = 0; _i3 < n; _i3++) {
|
382
|
+
arr[_i3].pop();
|
383
|
+
|
384
|
+
arr[_i3].pop();
|
385
|
+
}
|
386
|
+
}
|
387
|
+
|
388
|
+
if (n === 0) {
|
389
|
+
break;
|
390
|
+
}
|
391
|
+
} // standard sorting
|
392
|
+
|
393
|
+
|
394
|
+
lambdas.sort(function (a, b) {
|
395
|
+
return +subtract(abs(a), abs(b));
|
396
|
+
}); // the algorithm didn't converge
|
397
|
+
|
398
|
+
if (lastConvergenceBefore > 100) {
|
399
|
+
var err = Error('The eigenvalues failed to converge. Only found these eigenvalues: ' + lambdas.join(', '));
|
400
|
+
err.values = lambdas;
|
401
|
+
err.vectors = [];
|
402
|
+
throw err;
|
403
|
+
} // combine the overall QR transformation Qtotal with the subsequent
|
404
|
+
// transformation S that turns the diagonal 2x2 blocks to upper triangular
|
405
|
+
|
406
|
+
|
407
|
+
var C = findVectors ? multiply(Qtotal, blockDiag(Sdiag, N)) : undefined;
|
408
|
+
return {
|
409
|
+
values: lambdas,
|
410
|
+
C: C
|
411
|
+
};
|
412
|
+
}
|
413
|
+
/**
|
414
|
+
* @param {Matrix} A original matrix
|
415
|
+
* @param {number} N size of A
|
416
|
+
* @param {Matrix} C column transformation matrix that turns A into upper triangular
|
417
|
+
* @param {number[]} values array of eigenvalues of A
|
418
|
+
* @returns {number[][]} eigenvalues
|
419
|
+
*/
|
420
|
+
|
421
|
+
|
422
|
+
function findEigenvectors(A, N, C, values, prec) {
|
423
|
+
var Cinv = inv(C);
|
424
|
+
var U = multiply(Cinv, A, C); // turn values into a kind of "multiset"
|
425
|
+
// this way it is easier to find eigenvectors
|
426
|
+
|
427
|
+
var uniqueValues = [];
|
428
|
+
var multiplicities = [];
|
429
|
+
|
430
|
+
var _iterator = _createForOfIteratorHelper(values),
|
431
|
+
_step;
|
432
|
+
|
433
|
+
try {
|
434
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
435
|
+
var _λ = _step.value;
|
436
|
+
|
437
|
+
var _i4 = indexOf(uniqueValues, _λ, equal);
|
438
|
+
|
439
|
+
if (_i4 === -1) {
|
440
|
+
// a dirty trick that helps us find more vectors
|
441
|
+
// TODO with iterative algorithm this can be removed
|
442
|
+
// Note: the round around log10 is needed to prevent rounding off errors in IE
|
443
|
+
var rounded = round(_λ, subtract(-1, round(log10(prec))));
|
444
|
+
uniqueValues.push(rounded);
|
445
|
+
multiplicities.push(1);
|
446
|
+
} else {
|
447
|
+
multiplicities[_i4] += 1;
|
448
|
+
}
|
449
|
+
} // find eigenvectors by solving U − λE = 0
|
450
|
+
// TODO replace with an iterative eigenvector algorithm
|
451
|
+
// (this one might fail for imprecise eigenvalues)
|
452
|
+
|
453
|
+
} catch (err) {
|
454
|
+
_iterator.e(err);
|
455
|
+
} finally {
|
456
|
+
_iterator.f();
|
457
|
+
}
|
458
|
+
|
459
|
+
var vectors = [];
|
460
|
+
var len = uniqueValues.length;
|
461
|
+
var b = Array(N).fill(0);
|
462
|
+
var E = diag(Array(N).fill(1)); // eigenvalues for which usolve failed (due to numerical error)
|
463
|
+
|
464
|
+
var failedLambdas = [];
|
465
|
+
|
466
|
+
for (var i = 0; i < len; i++) {
|
467
|
+
var λ = uniqueValues[i];
|
468
|
+
var solutions = usolveAll(subtract(U, multiply(λ, E)), b);
|
469
|
+
solutions = solutions.map(function (v) {
|
470
|
+
return multiply(C, v);
|
471
|
+
});
|
472
|
+
solutions.shift(); // ignore the null vector
|
473
|
+
// looks like we missed something
|
474
|
+
|
475
|
+
if (solutions.length < multiplicities[i]) {
|
476
|
+
failedLambdas.push(λ);
|
477
|
+
}
|
478
|
+
|
479
|
+
vectors.push.apply(vectors, (0, _toConsumableArray2["default"])(solutions.map(function (v) {
|
480
|
+
return flatten(v);
|
481
|
+
})));
|
482
|
+
}
|
483
|
+
|
484
|
+
if (failedLambdas.length !== 0) {
|
485
|
+
var err = new Error('Failed to find eigenvectors for the following eigenvalues: ' + failedLambdas.join(', '));
|
486
|
+
err.values = values;
|
487
|
+
err.vectors = vectors;
|
488
|
+
throw err;
|
489
|
+
}
|
490
|
+
|
491
|
+
return vectors;
|
492
|
+
}
|
493
|
+
/**
|
494
|
+
* Compute the eigenvalues of an 2x2 matrix
|
495
|
+
* @return {[number,number]}
|
496
|
+
*/
|
497
|
+
|
498
|
+
|
499
|
+
function eigenvalues2x2(a, b, c, d) {
|
500
|
+
// λ± = ½ trA ± ½ √( tr²A - 4 detA )
|
501
|
+
var trA = addScalar(a, d);
|
502
|
+
var detA = subtract(multiplyScalar(a, d), multiplyScalar(b, c));
|
503
|
+
var x = multiplyScalar(trA, 0.5);
|
504
|
+
var y = multiplyScalar(sqrt(subtract(multiplyScalar(trA, trA), multiplyScalar(4, detA))), 0.5);
|
505
|
+
return [addScalar(x, y), subtract(x, y)];
|
506
|
+
}
|
507
|
+
/**
|
508
|
+
* For an 2x2 matrix compute the transformation matrix S,
|
509
|
+
* so that SAS⁻¹ is an upper triangular matrix
|
510
|
+
* @return {[[number,number],[number,number]]}
|
511
|
+
* @see https://math.berkeley.edu/~ogus/old/Math_54-05/webfoils/jordan.pdf
|
512
|
+
* @see http://people.math.harvard.edu/~knill/teaching/math21b2004/exhibits/2dmatrices/index.html
|
513
|
+
*/
|
514
|
+
|
515
|
+
|
516
|
+
function jordanBase2x2(a, b, c, d, l1, l2, prec, type) {
|
517
|
+
var big = type === 'BigNumber';
|
518
|
+
var cplx = type === 'Complex';
|
519
|
+
var zero = big ? bignumber(0) : cplx ? complex(0) : 0;
|
520
|
+
var one = big ? bignumber(1) : cplx ? complex(1) : 1; // matrix is already upper triangular
|
521
|
+
// return an identity matrix
|
522
|
+
|
523
|
+
if (smaller(abs(c), prec)) {
|
524
|
+
return [[one, zero], [zero, one]];
|
525
|
+
} // matrix is diagonalizable
|
526
|
+
// return its eigenvectors as columns
|
527
|
+
|
528
|
+
|
529
|
+
if (larger(abs(subtract(l1, l2)), prec)) {
|
530
|
+
return [[subtract(l1, d), subtract(l2, d)], [c, c]];
|
531
|
+
} // matrix is not diagonalizable
|
532
|
+
// compute off-diagonal elements of N = A - λI
|
533
|
+
// N₁₂ = 0 ⇒ S = ( N⃗₁, I⃗₁ )
|
534
|
+
// N₁₂ ≠ 0 ⇒ S = ( N⃗₂, I⃗₂ )
|
535
|
+
|
536
|
+
|
537
|
+
var na = subtract(a, l1);
|
538
|
+
var nb = subtract(b, l1);
|
539
|
+
var nc = subtract(c, l1);
|
540
|
+
var nd = subtract(d, l1);
|
541
|
+
|
542
|
+
if (smaller(abs(nb), prec)) {
|
543
|
+
return [[na, one], [nc, zero]];
|
544
|
+
} else {
|
545
|
+
return [[nb, zero], [nd, one]];
|
546
|
+
}
|
547
|
+
}
|
548
|
+
/**
|
549
|
+
* Enlarge the matrix from n×n to N×N, setting the new
|
550
|
+
* elements to 1 on diagonal and 0 elsewhere
|
551
|
+
*/
|
552
|
+
|
553
|
+
|
554
|
+
function inflateMatrix(arr, N) {
|
555
|
+
// add columns
|
556
|
+
for (var i = 0; i < arr.length; i++) {
|
557
|
+
var _arr$i;
|
558
|
+
|
559
|
+
(_arr$i = arr[i]).push.apply(_arr$i, (0, _toConsumableArray2["default"])(Array(N - arr[i].length).fill(0)));
|
560
|
+
} // add rows
|
561
|
+
|
562
|
+
|
563
|
+
for (var _i5 = arr.length; _i5 < N; _i5++) {
|
564
|
+
arr.push(Array(N).fill(0));
|
565
|
+
arr[_i5][_i5] = 1;
|
566
|
+
}
|
567
|
+
|
568
|
+
return arr;
|
569
|
+
}
|
570
|
+
/**
|
571
|
+
* Create a block-diagonal matrix with the given square matrices on the diagonal
|
572
|
+
* @param {Matrix[] | number[][][]} arr array of matrices to be placed on the diagonal
|
573
|
+
* @param {number} N the size of the resulting matrix
|
574
|
+
*/
|
575
|
+
|
576
|
+
|
577
|
+
function blockDiag(arr, N) {
|
578
|
+
var M = [];
|
579
|
+
|
580
|
+
for (var i = 0; i < N; i++) {
|
581
|
+
M[i] = Array(N).fill(0);
|
582
|
+
}
|
583
|
+
|
584
|
+
var I = 0;
|
585
|
+
|
586
|
+
var _iterator2 = _createForOfIteratorHelper(arr),
|
587
|
+
_step2;
|
588
|
+
|
589
|
+
try {
|
590
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
591
|
+
var sub = _step2.value;
|
592
|
+
var n = sub.length;
|
593
|
+
|
594
|
+
for (var _i6 = 0; _i6 < n; _i6++) {
|
595
|
+
for (var j = 0; j < n; j++) {
|
596
|
+
M[I + _i6][I + j] = sub[_i6][j];
|
597
|
+
}
|
598
|
+
}
|
599
|
+
|
600
|
+
I += n;
|
601
|
+
}
|
602
|
+
} catch (err) {
|
603
|
+
_iterator2.e(err);
|
604
|
+
} finally {
|
605
|
+
_iterator2.f();
|
606
|
+
}
|
607
|
+
|
608
|
+
return M;
|
609
|
+
}
|
610
|
+
/**
|
611
|
+
* Finds the index of an element in an array using a custom equality function
|
612
|
+
* @template T
|
613
|
+
* @param {Array<T>} arr array in which to search
|
614
|
+
* @param {T} el the element to find
|
615
|
+
* @param {function(T, T): boolean} fn the equality function, first argument is an element of `arr`, the second is always `el`
|
616
|
+
* @returns {number} the index of `el`, or -1 when it's not in `arr`
|
617
|
+
*/
|
618
|
+
|
619
|
+
|
620
|
+
function indexOf(arr, el, fn) {
|
621
|
+
for (var i = 0; i < arr.length; i++) {
|
622
|
+
if (fn(arr[i], el)) {
|
623
|
+
return i;
|
624
|
+
}
|
625
|
+
}
|
626
|
+
|
627
|
+
return -1;
|
628
|
+
}
|
629
|
+
|
630
|
+
return complexEigs;
|
631
|
+
}
|