mathjs 11.3.3 → 11.5.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/HISTORY.md +25 -0
- package/lib/browser/math.js +1 -1
- package/lib/browser/math.js.LICENSE.txt +2 -2
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesLyap.generated.js +26 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesPolynomialRoot.generated.js +42 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSchur.generated.js +30 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSylvester.generated.js +46 -0
- package/lib/cjs/entry/dependenciesAny.generated.js +28 -0
- package/lib/cjs/entry/impureFunctionsAny.generated.js +21 -17
- package/lib/cjs/entry/pureFunctionsAny.generated.js +100 -48
- package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +8 -0
- package/lib/cjs/expression/embeddedDocs/function/algebra/polynomialRoot.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/lyap.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/reshape.js +1 -1
- package/lib/cjs/expression/embeddedDocs/function/matrix/schur.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/sylvester.js +15 -0
- package/lib/cjs/factoriesAny.js +28 -0
- package/lib/cjs/function/algebra/decomposition/schur.js +75 -0
- package/lib/cjs/function/algebra/lyap.js +57 -0
- package/lib/cjs/function/algebra/polynomialRoot.js +139 -0
- package/lib/cjs/function/algebra/simplify/wildcards.js +38 -0
- package/lib/cjs/function/algebra/simplify.js +161 -48
- package/lib/cjs/function/algebra/simplifyConstant.js +29 -12
- package/lib/cjs/function/algebra/sylvester.js +127 -0
- package/lib/cjs/function/matrix/forEach.js +1 -1
- package/lib/cjs/function/matrix/reshape.js +1 -1
- package/lib/cjs/function/string/format.js +24 -24
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/version.js +1 -1
- package/lib/esm/entry/dependenciesAny/dependenciesLyap.generated.js +18 -0
- package/lib/esm/entry/dependenciesAny/dependenciesPolynomialRoot.generated.js +34 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSchur.generated.js +22 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSylvester.generated.js +38 -0
- package/lib/esm/entry/dependenciesAny.generated.js +4 -0
- package/lib/esm/entry/impureFunctionsAny.generated.js +22 -18
- package/lib/esm/entry/pureFunctionsAny.generated.js +87 -39
- package/lib/esm/expression/embeddedDocs/embeddedDocs.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/algebra/polynomialRoot.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/lyap.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/reshape.js +1 -1
- package/lib/esm/expression/embeddedDocs/function/matrix/schur.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/sylvester.js +8 -0
- package/lib/esm/factoriesAny.js +4 -0
- package/lib/esm/function/algebra/decomposition/schur.js +70 -0
- package/lib/esm/function/algebra/lyap.js +52 -0
- package/lib/esm/function/algebra/polynomialRoot.js +122 -0
- package/lib/esm/function/algebra/simplify/wildcards.js +20 -0
- package/lib/esm/function/algebra/simplify.js +162 -49
- package/lib/esm/function/algebra/simplifyConstant.js +29 -12
- package/lib/esm/function/algebra/sylvester.js +118 -0
- package/lib/esm/function/matrix/forEach.js +1 -1
- package/lib/esm/function/matrix/reshape.js +1 -1
- package/lib/esm/function/string/format.js +24 -24
- package/lib/esm/version.js +1 -1
- package/package.json +14 -14
- package/types/index.d.ts +88 -6
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
7
7
|
exports.createSimplify = void 0;
|
8
8
|
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
9
9
|
var _is = require("../../utils/is.js");
|
10
|
+
var _wildcards = require("./simplify/wildcards.js");
|
10
11
|
var _factory = require("../../utils/factory.js");
|
11
12
|
var _util = require("./simplify/util.js");
|
12
13
|
var _object = require("../../utils/object.js");
|
@@ -80,9 +81,15 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
80
81
|
* expression is that variables starting with the following characters are
|
81
82
|
* interpreted as wildcards:
|
82
83
|
*
|
83
|
-
* - 'n' -
|
84
|
-
* - 'c' -
|
85
|
-
* - '
|
84
|
+
* - 'n' - Matches any node [Node]
|
85
|
+
* - 'c' - Matches a constant literal (5 or 3.2) [ConstantNode]
|
86
|
+
* - 'cl' - Matches a constant literal; same as c [ConstantNode]
|
87
|
+
* - 'cd' - Matches a decimal literal (5 or -3.2) [ConstantNode or unaryMinus wrapping a ConstantNode]
|
88
|
+
* - 'ce' - Matches a constant expression (-5 or √3) [Expressions consisting of only ConstantNodes, functions, and operators]
|
89
|
+
* - 'v' - Matches a variable; anything not matched by c (-5 or x) [Node that is not a ConstantNode]
|
90
|
+
* - 'vl' - Matches a variable literal (x or y) [SymbolNode]
|
91
|
+
* - 'vd' - Matches a non-decimal expression; anything not matched by cd (x or √3) [Node that is not a ConstantNode or unaryMinus that is wrapping a ConstantNode]
|
92
|
+
* - 've' - Matches a variable expression; anything not matched by ce (x or 2x) [Expressions that contain a SymbolNode or other non-constant term]
|
86
93
|
*
|
87
94
|
* The default list of rules is exposed on the function as `simplify.rules`
|
88
95
|
* and can be used as a basis to built a set of custom rules. Note that since
|
@@ -259,7 +266,7 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
259
266
|
}
|
260
267
|
}
|
261
268
|
}, {
|
262
|
-
s: '-(
|
269
|
+
s: '-(cl*v) -> v * (-cl)',
|
263
270
|
// make non-constant terms positive
|
264
271
|
assuming: {
|
265
272
|
multiply: {
|
@@ -270,7 +277,7 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
270
277
|
}
|
271
278
|
}
|
272
279
|
}, {
|
273
|
-
s: '-(
|
280
|
+
s: '-(cl*v) -> (-cl) * v',
|
274
281
|
// non-commutative version, part 1
|
275
282
|
assuming: {
|
276
283
|
multiply: {
|
@@ -281,7 +288,7 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
281
288
|
}
|
282
289
|
}
|
283
290
|
}, {
|
284
|
-
s: '-(v*
|
291
|
+
s: '-(v*cl) -> v * (-cl)',
|
285
292
|
// non-commutative version, part 2
|
286
293
|
assuming: {
|
287
294
|
multiply: {
|
@@ -339,24 +346,24 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
339
346
|
},
|
340
347
|
// collect like factors; into a sum, only do this for nonconstants
|
341
348
|
{
|
342
|
-
l: '
|
343
|
-
r: '
|
349
|
+
l: ' vd * ( vd * n1 + n2)',
|
350
|
+
r: 'vd^2 * n1 + vd * n2'
|
344
351
|
}, {
|
345
|
-
s: '
|
352
|
+
s: ' vd * (vd^n4 * n1 + n2) -> vd^(1+n4) * n1 + vd * n2',
|
346
353
|
assuming: {
|
347
354
|
divide: {
|
348
355
|
total: true
|
349
356
|
}
|
350
357
|
} // v*1/v = v^(1+-1) needs 1/v
|
351
358
|
}, {
|
352
|
-
s: '
|
359
|
+
s: 'vd^n3 * ( vd * n1 + n2) -> vd^(n3+1) * n1 + vd^n3 * n2',
|
353
360
|
assuming: {
|
354
361
|
divide: {
|
355
362
|
total: true
|
356
363
|
}
|
357
364
|
}
|
358
365
|
}, {
|
359
|
-
s: '
|
366
|
+
s: 'vd^n3 * (vd^n4 * n1 + n2) -> vd^(n3+n4) * n1 + vd^n3 * n2',
|
360
367
|
assuming: {
|
361
368
|
divide: {
|
362
369
|
total: true
|
@@ -399,8 +406,8 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
399
406
|
l: 'n+-n',
|
400
407
|
r: '0'
|
401
408
|
}, {
|
402
|
-
l: '
|
403
|
-
r: '
|
409
|
+
l: 'vd*n + vd',
|
410
|
+
r: 'vd*(n+1)'
|
404
411
|
},
|
405
412
|
// NOTE: leftmost position is special:
|
406
413
|
{
|
@@ -414,9 +421,17 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
414
421
|
}, {
|
415
422
|
l: 'n3^(-n4)*n1 + n3^n5 * n2',
|
416
423
|
r: 'n3^(-n4)*(n1 + n3^(n4+n5)*n2)'
|
424
|
+
},
|
425
|
+
// noncommutative additional cases (term collection & factoring)
|
426
|
+
{
|
427
|
+
s: 'n*vd + vd -> (n+1)*vd',
|
428
|
+
assuming: {
|
429
|
+
multiply: {
|
430
|
+
commutative: false
|
431
|
+
}
|
432
|
+
}
|
417
433
|
}, {
|
418
|
-
s: '
|
419
|
-
// noncommutative additional cases
|
434
|
+
s: 'vd + n*vd -> (1+n)*vd',
|
420
435
|
assuming: {
|
421
436
|
multiply: {
|
422
437
|
commutative: false
|
@@ -429,6 +444,16 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
429
444
|
commutative: false
|
430
445
|
}
|
431
446
|
}
|
447
|
+
}, {
|
448
|
+
s: 'n^n1 * n -> n^(n1+1)',
|
449
|
+
assuming: {
|
450
|
+
divide: {
|
451
|
+
total: true
|
452
|
+
},
|
453
|
+
multiply: {
|
454
|
+
commutative: false
|
455
|
+
}
|
456
|
+
}
|
432
457
|
}, {
|
433
458
|
s: 'n1*n3^(-n4) + n2 * n3 -> (n1 + n2*n3^(n4 + 1))*n3^(-n4)',
|
434
459
|
assuming: {
|
@@ -444,10 +469,17 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
444
469
|
}
|
445
470
|
}
|
446
471
|
}, {
|
447
|
-
l: 'n*
|
448
|
-
r: '(n+1)*
|
472
|
+
l: 'n*cd + cd',
|
473
|
+
r: '(n+1)*cd'
|
449
474
|
}, {
|
450
|
-
s: '
|
475
|
+
s: 'cd*n + cd -> cd*(n+1)',
|
476
|
+
assuming: {
|
477
|
+
multiply: {
|
478
|
+
commutative: false
|
479
|
+
}
|
480
|
+
}
|
481
|
+
}, {
|
482
|
+
s: 'cd + cd*n -> cd*(1+n)',
|
451
483
|
assuming: {
|
452
484
|
multiply: {
|
453
485
|
commutative: false
|
@@ -478,7 +510,7 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
478
510
|
},
|
479
511
|
// final ordering of constants
|
480
512
|
{
|
481
|
-
s: '
|
513
|
+
s: 'ce+ve -> ve+ce',
|
482
514
|
assuming: {
|
483
515
|
add: {
|
484
516
|
commutative: true
|
@@ -490,7 +522,7 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
490
522
|
}
|
491
523
|
}
|
492
524
|
}, {
|
493
|
-
s: '
|
525
|
+
s: 'vd*cd -> cd*vd',
|
494
526
|
assuming: {
|
495
527
|
multiply: {
|
496
528
|
commutative: true
|
@@ -609,15 +641,33 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
609
641
|
newRule.evaluate = parse(ruleObject.evaluate);
|
610
642
|
}
|
611
643
|
if (isAssociative(newRule.l, context)) {
|
644
|
+
var nonCommutative = !isCommutative(newRule.l, context);
|
645
|
+
var leftExpandsym;
|
646
|
+
// Gen. the LHS placeholder used in this NC-context specific expansion rules
|
647
|
+
if (nonCommutative) leftExpandsym = _getExpandPlaceholderSymbol();
|
612
648
|
var makeNode = createMakeNodeFunction(newRule.l);
|
613
649
|
var expandsym = _getExpandPlaceholderSymbol();
|
614
650
|
newRule.expanded = {};
|
615
|
-
newRule.expanded.l = makeNode([newRule.l
|
651
|
+
newRule.expanded.l = makeNode([newRule.l, expandsym]);
|
616
652
|
// Push the expandsym into the deepest possible branch.
|
617
653
|
// This helps to match the newRule against nodes returned from getSplits() later on.
|
618
654
|
flatten(newRule.expanded.l, context);
|
619
655
|
unflattenr(newRule.expanded.l, context);
|
620
656
|
newRule.expanded.r = makeNode([newRule.r, expandsym]);
|
657
|
+
|
658
|
+
// In and for a non-commutative context, attempting with yet additional expansion rules makes
|
659
|
+
// way for more matches cases of multi-arg expressions; such that associative rules (such as
|
660
|
+
// 'n*n -> n^2') can be applied to exprs. such as 'a * b * b' and 'a * b * b * a'.
|
661
|
+
if (nonCommutative) {
|
662
|
+
// 'Non-commutative' 1: LHS (placeholder) only
|
663
|
+
newRule.expandedNC1 = {};
|
664
|
+
newRule.expandedNC1.l = makeNode([leftExpandsym, newRule.l]);
|
665
|
+
newRule.expandedNC1.r = makeNode([leftExpandsym, newRule.r]);
|
666
|
+
// 'Non-commutative' 2: farmost LHS and RHS placeholders
|
667
|
+
newRule.expandedNC2 = {};
|
668
|
+
newRule.expandedNC2.l = makeNode([leftExpandsym, newRule.expanded.l]);
|
669
|
+
newRule.expandedNC2.r = makeNode([leftExpandsym, newRule.expanded.r]);
|
670
|
+
}
|
621
671
|
}
|
622
672
|
return newRule;
|
623
673
|
}
|
@@ -823,6 +873,16 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
823
873
|
repl = rule.expanded.r;
|
824
874
|
matches = _ruleMatch(rule.expanded.l, res, mergedContext)[0];
|
825
875
|
}
|
876
|
+
// Additional, non-commutative context expansion-rules
|
877
|
+
if (!matches && rule.expandedNC1) {
|
878
|
+
repl = rule.expandedNC1.r;
|
879
|
+
matches = _ruleMatch(rule.expandedNC1.l, res, mergedContext)[0];
|
880
|
+
if (!matches) {
|
881
|
+
// Existence of NC1 implies NC2
|
882
|
+
repl = rule.expandedNC2.r;
|
883
|
+
matches = _ruleMatch(rule.expandedNC2.l, res, mergedContext)[0];
|
884
|
+
}
|
885
|
+
}
|
826
886
|
if (matches) {
|
827
887
|
// const before = res.toString({parenthesis: 'all'})
|
828
888
|
|
@@ -1040,8 +1100,8 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
1040
1100
|
res = mergeChildMatches(childMatches);
|
1041
1101
|
} else if (node.args.length >= 2 && rule.args.length === 2) {
|
1042
1102
|
// node is flattened, rule is not
|
1043
|
-
// Associative operators/functions can be split in different ways so we check if the rule
|
1044
|
-
// them and return their union.
|
1103
|
+
// Associative operators/functions can be split in different ways so we check if the rule
|
1104
|
+
// matches for each of them and return their union.
|
1045
1105
|
var splits = getSplits(node, context);
|
1046
1106
|
var splitMatches = [];
|
1047
1107
|
for (var _i3 = 0; _i3 < splits.length; _i3++) {
|
@@ -1057,9 +1117,8 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
1057
1117
|
}
|
1058
1118
|
} else if (rule instanceof SymbolNode) {
|
1059
1119
|
// If the rule is a SymbolNode, then it carries a special meaning
|
1060
|
-
// according to the first
|
1061
|
-
//
|
1062
|
-
// n.* matches any node
|
1120
|
+
// according to the first one or two characters of the symbol node name.
|
1121
|
+
// These meanings are expalined in the documentation for simplify()
|
1063
1122
|
if (rule.name.length === 0) {
|
1064
1123
|
throw new Error('Symbol in rule has 0 length...!?');
|
1065
1124
|
}
|
@@ -1068,29 +1127,83 @@ var createSimplify = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fu
|
|
1068
1127
|
if (rule.name !== node.name) {
|
1069
1128
|
return [];
|
1070
1129
|
}
|
1071
|
-
} else if (rule.name[0] === 'n' || rule.name.substring(0, 2) === '_p') {
|
1072
|
-
// rule matches _anything_, so assign this node to the rule.name placeholder
|
1073
|
-
// Assign node to the rule.name placeholder.
|
1074
|
-
// Our parent will check for matches among placeholders.
|
1075
|
-
res[0].placeholders[rule.name] = node;
|
1076
|
-
} else if (rule.name[0] === 'v') {
|
1077
|
-
// rule matches any variable thing (not a ConstantNode)
|
1078
|
-
if (!(0, _is.isConstantNode)(node)) {
|
1079
|
-
res[0].placeholders[rule.name] = node;
|
1080
|
-
} else {
|
1081
|
-
// Mis-match: rule was expecting something other than a ConstantNode
|
1082
|
-
return [];
|
1083
|
-
}
|
1084
|
-
} else if (rule.name[0] === 'c') {
|
1085
|
-
// rule matches any ConstantNode
|
1086
|
-
if (node instanceof ConstantNode) {
|
1087
|
-
res[0].placeholders[rule.name] = node;
|
1088
|
-
} else {
|
1089
|
-
// Mis-match: rule was expecting a ConstantNode
|
1090
|
-
return [];
|
1091
|
-
}
|
1092
1130
|
} else {
|
1093
|
-
|
1131
|
+
// wildcards are composed of up to two alphabetic or underscore characters
|
1132
|
+
switch (rule.name[1] >= 'a' && rule.name[1] <= 'z' ? rule.name.substring(0, 2) : rule.name[0]) {
|
1133
|
+
case 'n':
|
1134
|
+
case '_p':
|
1135
|
+
// rule matches _anything_, so assign this node to the rule.name placeholder
|
1136
|
+
// Assign node to the rule.name placeholder.
|
1137
|
+
// Our parent will check for matches among placeholders.
|
1138
|
+
res[0].placeholders[rule.name] = node;
|
1139
|
+
break;
|
1140
|
+
case 'c':
|
1141
|
+
case 'cl':
|
1142
|
+
// rule matches a ConstantNode
|
1143
|
+
if ((0, _wildcards.isConstantNode)(node)) {
|
1144
|
+
res[0].placeholders[rule.name] = node;
|
1145
|
+
} else {
|
1146
|
+
// mis-match: rule does not encompass current node
|
1147
|
+
return [];
|
1148
|
+
}
|
1149
|
+
break;
|
1150
|
+
case 'v':
|
1151
|
+
// rule matches anything other than a ConstantNode
|
1152
|
+
if (!(0, _wildcards.isConstantNode)(node)) {
|
1153
|
+
res[0].placeholders[rule.name] = node;
|
1154
|
+
} else {
|
1155
|
+
// mis-match: rule does not encompass current node
|
1156
|
+
return [];
|
1157
|
+
}
|
1158
|
+
break;
|
1159
|
+
case 'vl':
|
1160
|
+
// rule matches VariableNode
|
1161
|
+
if ((0, _wildcards.isVariableNode)(node)) {
|
1162
|
+
res[0].placeholders[rule.name] = node;
|
1163
|
+
} else {
|
1164
|
+
// mis-match: rule does not encompass current node
|
1165
|
+
return [];
|
1166
|
+
}
|
1167
|
+
break;
|
1168
|
+
case 'cd':
|
1169
|
+
// rule matches a ConstantNode or unaryMinus-wrapped ConstantNode
|
1170
|
+
if ((0, _wildcards.isNumericNode)(node)) {
|
1171
|
+
res[0].placeholders[rule.name] = node;
|
1172
|
+
} else {
|
1173
|
+
// mis-match: rule does not encompass current node
|
1174
|
+
return [];
|
1175
|
+
}
|
1176
|
+
break;
|
1177
|
+
case 'vd':
|
1178
|
+
// rule matches anything other than a ConstantNode or unaryMinus-wrapped ConstantNode
|
1179
|
+
if (!(0, _wildcards.isNumericNode)(node)) {
|
1180
|
+
res[0].placeholders[rule.name] = node;
|
1181
|
+
} else {
|
1182
|
+
// mis-match: rule does not encompass current node
|
1183
|
+
return [];
|
1184
|
+
}
|
1185
|
+
break;
|
1186
|
+
case 'ce':
|
1187
|
+
// rule matches expressions that have a constant value
|
1188
|
+
if ((0, _wildcards.isConstantExpression)(node)) {
|
1189
|
+
res[0].placeholders[rule.name] = node;
|
1190
|
+
} else {
|
1191
|
+
// mis-match: rule does not encompass current node
|
1192
|
+
return [];
|
1193
|
+
}
|
1194
|
+
break;
|
1195
|
+
case 've':
|
1196
|
+
// rule matches expressions that do not have a constant value
|
1197
|
+
if (!(0, _wildcards.isConstantExpression)(node)) {
|
1198
|
+
res[0].placeholders[rule.name] = node;
|
1199
|
+
} else {
|
1200
|
+
// mis-match: rule does not encompass current node
|
1201
|
+
return [];
|
1202
|
+
}
|
1203
|
+
break;
|
1204
|
+
default:
|
1205
|
+
throw new Error('Invalid symbol in rule: ' + rule.name);
|
1206
|
+
}
|
1094
1207
|
}
|
1095
1208
|
} else if (rule instanceof ConstantNode) {
|
1096
1209
|
// Literal constant must match exactly
|
@@ -307,20 +307,37 @@ var createSimplifyConstant = /* #__PURE__ */(0, _factory.factory)(name, dependen
|
|
307
307
|
* @return - Either a Node representing a binary expression or Fraction
|
308
308
|
*/
|
309
309
|
function foldOp(fn, args, makeNode, options) {
|
310
|
-
|
311
|
-
|
310
|
+
var first = args.shift();
|
311
|
+
|
312
|
+
// In the following reduction, sofar always has one of the three following
|
313
|
+
// forms: [NODE], [CONSTANT], or [NODE, CONSTANT]
|
314
|
+
var reduction = args.reduce(function (sofar, next) {
|
315
|
+
if (!(0, _is.isNode)(next)) {
|
316
|
+
var last = sofar.pop();
|
317
|
+
if ((0, _is.isNode)(last)) {
|
318
|
+
return [last, next];
|
319
|
+
}
|
320
|
+
// Two constants in a row, try to fold them into one
|
312
321
|
try {
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
} else if (!(0, _is.isNode)(b)) {
|
320
|
-
b = _toNode(b);
|
322
|
+
sofar.push(_eval(fn, [last, next], options));
|
323
|
+
return sofar;
|
324
|
+
} catch (ignoreandcontinue) {
|
325
|
+
sofar.push(last);
|
326
|
+
// fall through to Node case
|
327
|
+
}
|
321
328
|
}
|
322
|
-
|
323
|
-
|
329
|
+
|
330
|
+
// Encountered a Node, or failed folding --
|
331
|
+
// collapse everything so far into a single tree:
|
332
|
+
sofar.push(_ensureNode(sofar.pop()));
|
333
|
+
var newtree = sofar.length === 1 ? sofar[0] : makeNode(sofar);
|
334
|
+
return [makeNode([newtree, _ensureNode(next)])];
|
335
|
+
}, [first]);
|
336
|
+
if (reduction.length === 1) {
|
337
|
+
return reduction[0];
|
338
|
+
}
|
339
|
+
// Might end up with a tree and a constant at the end:
|
340
|
+
return makeNode([reduction[0], _toNode(reduction[1])]);
|
324
341
|
}
|
325
342
|
|
326
343
|
// destroys the original node and returns a folded one
|
@@ -0,0 +1,127 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.createSylvester = void 0;
|
7
|
+
var _factory = require("../../utils/factory.js");
|
8
|
+
var name = 'sylvester';
|
9
|
+
var dependencies = ['typed', 'schur', 'matrixFromColumns', 'matrix', 'multiply', 'range', 'concat', 'transpose', 'index', 'subset', 'add', 'subtract', 'identity', 'lusolve', 'abs'];
|
10
|
+
var createSylvester = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
|
11
|
+
var typed = _ref.typed,
|
12
|
+
schur = _ref.schur,
|
13
|
+
matrixFromColumns = _ref.matrixFromColumns,
|
14
|
+
matrix = _ref.matrix,
|
15
|
+
multiply = _ref.multiply,
|
16
|
+
range = _ref.range,
|
17
|
+
concat = _ref.concat,
|
18
|
+
transpose = _ref.transpose,
|
19
|
+
index = _ref.index,
|
20
|
+
subset = _ref.subset,
|
21
|
+
add = _ref.add,
|
22
|
+
subtract = _ref.subtract,
|
23
|
+
identity = _ref.identity,
|
24
|
+
lusolve = _ref.lusolve,
|
25
|
+
abs = _ref.abs;
|
26
|
+
/**
|
27
|
+
*
|
28
|
+
* Solves the real-valued Sylvester equation AX+XB=C for X, where A, B and C are
|
29
|
+
* matrices of appropriate dimensions, being A and B squared. Notice that other
|
30
|
+
* equivalent definitions for the Sylvester equation exist and this function
|
31
|
+
* assumes the one presented in the original publication of the the Bartels-
|
32
|
+
* Stewart algorithm, which is implemented by this function.
|
33
|
+
* https://en.wikipedia.org/wiki/Sylvester_equation
|
34
|
+
*
|
35
|
+
* Syntax:
|
36
|
+
*
|
37
|
+
* math.sylvester(A, B, C)
|
38
|
+
*
|
39
|
+
* Examples:
|
40
|
+
*
|
41
|
+
* const A = [[-1, -2], [1, 1]]
|
42
|
+
* const B = [[2, -1], [1, -2]]
|
43
|
+
* const C = [[-3, 2], [3, 0]]
|
44
|
+
* math.sylvester(A, B, C) // returns DenseMatrix [[-0.25, 0.25], [1.5, -1.25]]
|
45
|
+
*
|
46
|
+
* See also:
|
47
|
+
*
|
48
|
+
* schur, lyap
|
49
|
+
*
|
50
|
+
* @param {Matrix | Array} A Matrix A
|
51
|
+
* @param {Matrix | Array} B Matrix B
|
52
|
+
* @param {Matrix | Array} C Matrix C
|
53
|
+
* @return {Matrix | Array} Matrix X, solving the Sylvester equation
|
54
|
+
*/
|
55
|
+
return typed(name, {
|
56
|
+
'Matrix, Matrix, Matrix': _sylvester,
|
57
|
+
'Array, Matrix, Matrix': function ArrayMatrixMatrix(A, B, C) {
|
58
|
+
return _sylvester(matrix(A), B, C);
|
59
|
+
},
|
60
|
+
'Array, Array, Matrix': function ArrayArrayMatrix(A, B, C) {
|
61
|
+
return _sylvester(matrix(A), matrix(B), C);
|
62
|
+
},
|
63
|
+
'Array, Matrix, Array': function ArrayMatrixArray(A, B, C) {
|
64
|
+
return _sylvester(matrix(A), B, matrix(C));
|
65
|
+
},
|
66
|
+
'Matrix, Array, Matrix': function MatrixArrayMatrix(A, B, C) {
|
67
|
+
return _sylvester(A, matrix(B), C);
|
68
|
+
},
|
69
|
+
'Matrix, Array, Array': function MatrixArrayArray(A, B, C) {
|
70
|
+
return _sylvester(A, matrix(B), matrix(C));
|
71
|
+
},
|
72
|
+
'Matrix, Matrix, Array': function MatrixMatrixArray(A, B, C) {
|
73
|
+
return _sylvester(A, B, matrix(C));
|
74
|
+
},
|
75
|
+
'Array, Array, Array': function ArrayArrayArray(A, B, C) {
|
76
|
+
return _sylvester(matrix(A), matrix(B), matrix(C)).toArray();
|
77
|
+
}
|
78
|
+
});
|
79
|
+
function _sylvester(A, B, C) {
|
80
|
+
var n = B.size()[0];
|
81
|
+
var m = A.size()[0];
|
82
|
+
var sA = schur(A);
|
83
|
+
var F = sA.T;
|
84
|
+
var U = sA.U;
|
85
|
+
var sB = schur(multiply(-1, B));
|
86
|
+
var G = sB.T;
|
87
|
+
var V = sB.U;
|
88
|
+
var D = multiply(multiply(transpose(U), C), V);
|
89
|
+
var all = range(0, m);
|
90
|
+
var y = [];
|
91
|
+
var hc = function hc(a, b) {
|
92
|
+
return concat(a, b, 1);
|
93
|
+
};
|
94
|
+
var vc = function vc(a, b) {
|
95
|
+
return concat(a, b, 0);
|
96
|
+
};
|
97
|
+
for (var k = 0; k < n; k++) {
|
98
|
+
if (k < n - 1 && abs(subset(G, index(k + 1, k))) > 1e-5) {
|
99
|
+
var RHS = vc(subset(D, index(all, k)), subset(D, index(all, k + 1)));
|
100
|
+
for (var j = 0; j < k; j++) {
|
101
|
+
RHS = add(RHS, vc(multiply(y[j], subset(G, index(j, k))), multiply(y[j], subset(G, index(j, k + 1)))));
|
102
|
+
}
|
103
|
+
var gkk = multiply(identity(m), multiply(-1, subset(G, index(k, k))));
|
104
|
+
var gmk = multiply(identity(m), multiply(-1, subset(G, index(k + 1, k))));
|
105
|
+
var gkm = multiply(identity(m), multiply(-1, subset(G, index(k, k + 1))));
|
106
|
+
var gmm = multiply(identity(m), multiply(-1, subset(G, index(k + 1, k + 1))));
|
107
|
+
var LHS = vc(hc(add(F, gkk), gmk), hc(gkm, add(F, gmm)));
|
108
|
+
var yAux = lusolve(LHS, RHS);
|
109
|
+
y[k] = yAux.subset(index(range(0, m), 0));
|
110
|
+
y[k + 1] = yAux.subset(index(range(m, 2 * m), 0));
|
111
|
+
k++;
|
112
|
+
} else {
|
113
|
+
var _RHS = subset(D, index(all, k));
|
114
|
+
for (var _j = 0; _j < k; _j++) {
|
115
|
+
_RHS = add(_RHS, multiply(y[_j], subset(G, index(_j, k))));
|
116
|
+
}
|
117
|
+
var _gkk = subset(G, index(k, k));
|
118
|
+
var _LHS = subtract(F, multiply(_gkk, identity(m)));
|
119
|
+
y[k] = lusolve(_LHS, _RHS);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
var Y = matrix(matrixFromColumns.apply(void 0, y));
|
123
|
+
var X = multiply(U, multiply(Y, transpose(V)));
|
124
|
+
return X;
|
125
|
+
}
|
126
|
+
});
|
127
|
+
exports.createSylvester = createSylvester;
|
@@ -37,7 +37,7 @@ var createForEach = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fun
|
|
37
37
|
return typed(name, {
|
38
38
|
'Array, function': _forEach,
|
39
39
|
'Matrix, function': function MatrixFunction(x, callback) {
|
40
|
-
|
40
|
+
x.forEach(callback);
|
41
41
|
}
|
42
42
|
});
|
43
43
|
});
|
@@ -53,7 +53,7 @@ var createReshape = /* #__PURE__ */(0, _factory.factory)(name, dependencies, fun
|
|
53
53
|
*/
|
54
54
|
return typed(name, {
|
55
55
|
'Matrix, Array': function MatrixArray(x, sizes) {
|
56
|
-
return x.reshape(sizes);
|
56
|
+
return x.reshape(sizes, true);
|
57
57
|
},
|
58
58
|
'Array, Array': function ArrayArray(x, sizes) {
|
59
59
|
sizes.forEach(function (size) {
|
@@ -28,53 +28,53 @@ var createFormat = /* #__PURE__ */(0, _factory.factory)(name, dependencies, func
|
|
28
28
|
* An object with formatting options. Available options:
|
29
29
|
* - `notation: string`
|
30
30
|
* Number notation. Choose from:
|
31
|
-
* - 'fixed'
|
31
|
+
* - `'fixed'`
|
32
32
|
* Always use regular number notation.
|
33
|
-
* For example '123.40' and '14000000'
|
34
|
-
* - 'exponential'
|
33
|
+
* For example `'123.40'` and `'14000000'`
|
34
|
+
* - `'exponential'`
|
35
35
|
* Always use exponential notation.
|
36
|
-
* For example '1.234e+2' and '1.4e+7'
|
37
|
-
* - 'engineering'
|
36
|
+
* For example `'1.234e+2'` and `'1.4e+7'`
|
37
|
+
* - `'engineering'`
|
38
38
|
* Always use engineering notation: always have exponential notation,
|
39
|
-
* and select the exponent to be a multiple of 3
|
40
|
-
* For example '123.4e+0' and '14.0e+6'
|
41
|
-
* - 'auto' (default)
|
39
|
+
* and select the exponent to be a multiple of `3`.
|
40
|
+
* For example `'123.4e+0'` and `'14.0e+6'`
|
41
|
+
* - `'auto'` (default)
|
42
42
|
* Regular number notation for numbers having an absolute value between
|
43
43
|
* `lower` and `upper` bounds, and uses exponential notation elsewhere.
|
44
44
|
* Lower bound is included, upper bound is excluded.
|
45
|
-
* For example '123.4' and '1.4e7'
|
46
|
-
* - 'bin'
|
45
|
+
* For example `'123.4'` and `'1.4e7'`.
|
46
|
+
* - `'bin'`, `'oct'`, or `'hex'`
|
47
47
|
* Format the number using binary, octal, or hexadecimal notation.
|
48
|
-
* For example '0b1101' and '0x10fe'
|
48
|
+
* For example `'0b1101'` and `'0x10fe'`.
|
49
49
|
* - `wordSize: number`
|
50
50
|
* The word size in bits to use for formatting in binary, octal, or
|
51
|
-
* hexadecimal notation. To be used only with 'bin'
|
52
|
-
* values for
|
51
|
+
* hexadecimal notation. To be used only with `'bin'`, `'oct'`, or `'hex'`
|
52
|
+
* values for `notation` option. When this option is defined the value
|
53
53
|
* is formatted as a signed twos complement integer of the given word
|
54
54
|
* size and the size suffix is appended to the output.
|
55
|
-
* For example format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'
|
55
|
+
* For example `format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'`.
|
56
56
|
* Default value is undefined.
|
57
57
|
* - `precision: number`
|
58
58
|
* Limit the number of digits of the formatted value.
|
59
|
-
* For regular numbers, must be a number between 0 and 16
|
59
|
+
* For regular numbers, must be a number between `0` and `16`.
|
60
60
|
* For bignumbers, the maximum depends on the configured precision,
|
61
61
|
* see function `config()`.
|
62
|
-
* In case of notations 'exponential'
|
63
|
-
* defines the total number of significant digits returned.
|
64
|
-
* In case of notation 'fixed'
|
62
|
+
* In case of notations `'exponential'`, `'engineering'`, and `'auto'`,
|
63
|
+
* `precision` defines the total number of significant digits returned.
|
64
|
+
* In case of notation `'fixed'`, `precision` defines the number of
|
65
65
|
* significant digits after the decimal point.
|
66
66
|
* `precision` is undefined by default.
|
67
67
|
* - `lowerExp: number`
|
68
68
|
* Exponent determining the lower boundary for formatting a value with
|
69
|
-
* an exponent when `notation='auto`. Default value is `-3`.
|
69
|
+
* an exponent when `notation='auto'`. Default value is `-3`.
|
70
70
|
* - `upperExp: number`
|
71
71
|
* Exponent determining the upper boundary for formatting a value with
|
72
|
-
* an exponent when `notation='auto`. Default value is `5`.
|
73
|
-
* - `fraction: string`. Available values: 'ratio' (default) or 'decimal'
|
74
|
-
* For example `format(fraction(1, 3))` will output '1/3' when 'ratio'
|
75
|
-
* configured, and will output `0.(3)` when 'decimal' is configured.
|
72
|
+
* an exponent when `notation='auto'`. Default value is `5`.
|
73
|
+
* - `fraction: string`. Available values: `'ratio'` (default) or `'decimal'`.
|
74
|
+
* For example `format(fraction(1, 3))` will output `'1/3'` when `'ratio'`
|
75
|
+
* is configured, and will output `'0.(3)'` when `'decimal'` is configured.
|
76
76
|
* - `truncate: number`. Specifies the maximum allowed length of the
|
77
|
-
* returned string. If it
|
77
|
+
* returned string. If it had been longer, the excess characters
|
78
78
|
* are deleted and replaced with `'...'`.
|
79
79
|
* - `callback: function`
|
80
80
|
* A custom formatting function, invoked for all numeric elements in `value`,
|
package/lib/cjs/header.js
CHANGED
@@ -6,8 +6,8 @@
|
|
6
6
|
* It features real and complex numbers, units, matrices, a large set of
|
7
7
|
* mathematical functions, and a flexible expression parser.
|
8
8
|
*
|
9
|
-
* @version 11.
|
10
|
-
* @date 2022-
|
9
|
+
* @version 11.5.0
|
10
|
+
* @date 2022-12-05
|
11
11
|
*
|
12
12
|
* @license
|
13
13
|
* Copyright (C) 2013-2022 Jos de Jong <wjosdejong@gmail.com>
|
package/lib/cjs/version.js
CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.version = void 0;
|
7
|
-
var version = '11.
|
7
|
+
var version = '11.5.0';
|
8
8
|
// Note: This file is automatically generated when building math.js.
|
9
9
|
// Changes made in this file will be overwritten.
|
10
10
|
exports.version = version;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
/**
|
2
|
+
* THIS FILE IS AUTO-GENERATED
|
3
|
+
* DON'T MAKE CHANGES HERE
|
4
|
+
*/
|
5
|
+
import { matrixDependencies } from './dependenciesMatrix.generated.js';
|
6
|
+
import { multiplyDependencies } from './dependenciesMultiply.generated.js';
|
7
|
+
import { sylvesterDependencies } from './dependenciesSylvester.generated.js';
|
8
|
+
import { transposeDependencies } from './dependenciesTranspose.generated.js';
|
9
|
+
import { typedDependencies } from './dependenciesTyped.generated.js';
|
10
|
+
import { createLyap } from '../../factoriesAny.js';
|
11
|
+
export var lyapDependencies = {
|
12
|
+
matrixDependencies,
|
13
|
+
multiplyDependencies,
|
14
|
+
sylvesterDependencies,
|
15
|
+
transposeDependencies,
|
16
|
+
typedDependencies,
|
17
|
+
createLyap
|
18
|
+
};
|