mathjs 12.3.1 → 12.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,6 +13,7 @@ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits
13
13
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
14
  var _is = require("../../utils/is.js");
15
15
  var _array = require("../../utils/array.js");
16
+ var _scope = require("../../utils/scope.js");
16
17
  var _string = require("../../utils/string.js");
17
18
  var _customs = require("../../utils/customs.js");
18
19
  var _operators = require("../operators.js");
@@ -311,7 +312,7 @@ var createOperatorNode = exports.createOperatorNode = /* #__PURE__ */(0, _factor
311
312
  // "raw" evaluation
312
313
  var rawArgs = this.args;
313
314
  return function evalOperatorNode(scope, args, context) {
314
- return fn(rawArgs, math, scope);
315
+ return fn(rawArgs, math, (0, _scope.createSubScope)(scope, args));
315
316
  };
316
317
  } else if (evalArgs.length === 1) {
317
318
  var evalArg0 = evalArgs[0];
@@ -5,12 +5,12 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.compileInlineExpression = compileInlineExpression;
7
7
  var _is = require("../../../utils/is.js");
8
- var _scope = require("../../../utils/scope.js");
8
+ var _map = require("../../../utils/map.js");
9
9
  /**
10
10
  * Compile an inline expression like "x > 0"
11
11
  * @param {Node} expression
12
12
  * @param {Object} math
13
- * @param {Object} scope
13
+ * @param {Map} scope
14
14
  * @return {function} Returns a function with one argument which fills in the
15
15
  * undefined variable (like "x") and evaluates the expression
16
16
  */
@@ -25,10 +25,11 @@ function compileInlineExpression(expression, math, scope) {
25
25
 
26
26
  // create a test function for this equation
27
27
  var name = symbol.name; // variable name
28
- var subScope = (0, _scope.createSubScope)(scope);
28
+ var argsScope = new Map();
29
+ var subScope = new _map.PartitionedMap(scope, argsScope, new Set([name]));
29
30
  var eq = expression.compile();
30
31
  return function inlineExpression(x) {
31
- subScope.set(name, x);
32
+ argsScope.set(name, x);
32
33
  return eq.evaluate(subScope);
33
34
  };
34
35
  }
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 12.3.1
10
- * @date 2024-02-01
9
+ * @version 12.3.2
10
+ * @date 2024-02-08
11
11
  *
12
12
  * @license
13
13
  * Copyright (C) 2013-2024 Jos de Jong <wjosdejong@gmail.com>
@@ -4,12 +4,13 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.ObjectWrappingMap = void 0;
7
+ exports.PartitionedMap = exports.ObjectWrappingMap = void 0;
8
8
  exports.assign = assign;
9
9
  exports.createEmptyMap = createEmptyMap;
10
10
  exports.createMap = createMap;
11
11
  exports.isMap = isMap;
12
12
  exports.toObject = toObject;
13
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
14
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
14
15
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
16
  var _customs = require("./customs.js");
@@ -33,7 +34,7 @@ var ObjectWrappingMap = exports.ObjectWrappingMap = /*#__PURE__*/function () {
33
34
  (0, _createClass2["default"])(ObjectWrappingMap, [{
34
35
  key: "keys",
35
36
  value: function keys() {
36
- return Object.keys(this.wrappedObject);
37
+ return Object.keys(this.wrappedObject).values();
37
38
  }
38
39
  }, {
39
40
  key: "get",
@@ -51,9 +52,168 @@ var ObjectWrappingMap = exports.ObjectWrappingMap = /*#__PURE__*/function () {
51
52
  value: function has(key) {
52
53
  return (0, _customs.hasSafeProperty)(this.wrappedObject, key);
53
54
  }
55
+ }, {
56
+ key: "entries",
57
+ value: function entries() {
58
+ var _this = this;
59
+ return mapIterator(this.keys(), function (key) {
60
+ return [key, _this.get(key)];
61
+ });
62
+ }
63
+ }, {
64
+ key: "forEach",
65
+ value: function forEach(callback) {
66
+ var _iterator = _createForOfIteratorHelper(this.keys()),
67
+ _step;
68
+ try {
69
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
70
+ var key = _step.value;
71
+ callback(this.get(key), key, this);
72
+ }
73
+ } catch (err) {
74
+ _iterator.e(err);
75
+ } finally {
76
+ _iterator.f();
77
+ }
78
+ }
79
+ }, {
80
+ key: "delete",
81
+ value: function _delete(key) {
82
+ delete this.wrappedObject[key];
83
+ }
84
+ }, {
85
+ key: "clear",
86
+ value: function clear() {
87
+ var _iterator2 = _createForOfIteratorHelper(this.keys()),
88
+ _step2;
89
+ try {
90
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
91
+ var key = _step2.value;
92
+ this["delete"](key);
93
+ }
94
+ } catch (err) {
95
+ _iterator2.e(err);
96
+ } finally {
97
+ _iterator2.f();
98
+ }
99
+ }
100
+ }, {
101
+ key: "size",
102
+ get: function get() {
103
+ return Object.keys(this.wrappedObject).length;
104
+ }
54
105
  }]);
55
106
  return ObjectWrappingMap;
56
107
  }();
108
+ /**
109
+ * Create a map with two partitions: a and b.
110
+ * The set with bKeys determines which keys/values are read/written to map b,
111
+ * all other values are read/written to map a
112
+ *
113
+ * For example:
114
+ *
115
+ * const a = new Map()
116
+ * const b = new Map()
117
+ * const p = new PartitionedMap(a, b, new Set(['x', 'y']))
118
+ *
119
+ * In this case, values `x` and `y` are read/written to map `b`,
120
+ * all other values are read/written to map `a`.
121
+ */
122
+ var PartitionedMap = exports.PartitionedMap = /*#__PURE__*/function () {
123
+ /**
124
+ * @param {Map} a
125
+ * @param {Map} b
126
+ * @param {Set} bKeys
127
+ */
128
+ function PartitionedMap(a, b, bKeys) {
129
+ (0, _classCallCheck2["default"])(this, PartitionedMap);
130
+ this.a = a;
131
+ this.b = b;
132
+ this.bKeys = bKeys;
133
+ }
134
+ (0, _createClass2["default"])(PartitionedMap, [{
135
+ key: "get",
136
+ value: function get(key) {
137
+ return this.bKeys.has(key) ? this.b.get(key) : this.a.get(key);
138
+ }
139
+ }, {
140
+ key: "set",
141
+ value: function set(key, value) {
142
+ if (this.bKeys.has(key)) {
143
+ this.b.set(key, value);
144
+ } else {
145
+ this.a.set(key, value);
146
+ }
147
+ return this;
148
+ }
149
+ }, {
150
+ key: "has",
151
+ value: function has(key) {
152
+ return this.b.has(key) || this.a.has(key);
153
+ }
154
+ }, {
155
+ key: "keys",
156
+ value: function keys() {
157
+ return new Set([].concat((0, _toConsumableArray2["default"])(this.a.keys()), (0, _toConsumableArray2["default"])(this.b.keys())))[Symbol.iterator]();
158
+ }
159
+ }, {
160
+ key: "entries",
161
+ value: function entries() {
162
+ var _this2 = this;
163
+ return mapIterator(this.keys(), function (key) {
164
+ return [key, _this2.get(key)];
165
+ });
166
+ }
167
+ }, {
168
+ key: "forEach",
169
+ value: function forEach(callback) {
170
+ var _iterator3 = _createForOfIteratorHelper(this.keys()),
171
+ _step3;
172
+ try {
173
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
174
+ var key = _step3.value;
175
+ callback(this.get(key), key, this);
176
+ }
177
+ } catch (err) {
178
+ _iterator3.e(err);
179
+ } finally {
180
+ _iterator3.f();
181
+ }
182
+ }
183
+ }, {
184
+ key: "delete",
185
+ value: function _delete(key) {
186
+ return this.bKeys.has(key) ? this.b["delete"](key) : this.a["delete"](key);
187
+ }
188
+ }, {
189
+ key: "clear",
190
+ value: function clear() {
191
+ this.a.clear();
192
+ this.b.clear();
193
+ }
194
+ }, {
195
+ key: "size",
196
+ get: function get() {
197
+ return (0, _toConsumableArray2["default"])(this.keys()).length;
198
+ }
199
+ }]);
200
+ return PartitionedMap;
201
+ }();
202
+ /**
203
+ * Create a new iterator that maps over the provided iterator, applying a mapping function to each item
204
+ */
205
+ function mapIterator(it, callback) {
206
+ return {
207
+ next: function next() {
208
+ var n = it.next();
209
+ return n.done ? n : {
210
+ value: callback(n.value),
211
+ done: false
212
+ };
213
+ }
214
+ };
215
+ }
216
+
57
217
  /**
58
218
  * Creates an empty map, or whatever your platform's polyfill is.
59
219
  *
@@ -93,18 +253,18 @@ function toObject(map) {
93
253
  return map.wrappedObject;
94
254
  }
95
255
  var object = {};
96
- var _iterator = _createForOfIteratorHelper(map.keys()),
97
- _step;
256
+ var _iterator4 = _createForOfIteratorHelper(map.keys()),
257
+ _step4;
98
258
  try {
99
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
100
- var key = _step.value;
259
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
260
+ var key = _step4.value;
101
261
  var value = map.get(key);
102
262
  (0, _customs.setSafeProperty)(object, key, value);
103
263
  }
104
264
  } catch (err) {
105
- _iterator.e(err);
265
+ _iterator4.e(err);
106
266
  } finally {
107
- _iterator.f();
267
+ _iterator4.f();
108
268
  }
109
269
  return object;
110
270
  }
@@ -143,17 +303,17 @@ function assign(map) {
143
303
  continue;
144
304
  }
145
305
  if (isMap(args)) {
146
- var _iterator2 = _createForOfIteratorHelper(args.keys()),
147
- _step2;
306
+ var _iterator5 = _createForOfIteratorHelper(args.keys()),
307
+ _step5;
148
308
  try {
149
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
150
- var key = _step2.value;
309
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
310
+ var key = _step5.value;
151
311
  map.set(key, args.get(key));
152
312
  }
153
313
  } catch (err) {
154
- _iterator2.e(err);
314
+ _iterator5.e(err);
155
315
  } finally {
156
- _iterator2.f();
316
+ _iterator5.f();
157
317
  }
158
318
  } else if ((0, _is.isObject)(args)) {
159
319
  for (var _i2 = 0, _Object$keys = Object.keys(args); _i2 < _Object$keys.length; _i2++) {
@@ -15,15 +15,9 @@ var _map = require("./map.js");
15
15
  * the remaining `args`.
16
16
  *
17
17
  * @param {Map} parentScope
18
- * @param {...any} args
19
- * @returns {Map}
18
+ * @param {Object} args
19
+ * @returns {PartitionedMap}
20
20
  */
21
- function createSubScope(parentScope) {
22
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
23
- args[_key - 1] = arguments[_key];
24
- }
25
- if (typeof parentScope.createSubScope === 'function') {
26
- return _map.assign.apply(void 0, [parentScope.createSubScope()].concat(args));
27
- }
28
- return _map.assign.apply(void 0, [(0, _map.createEmptyMap)(), parentScope].concat(args));
21
+ function createSubScope(parentScope, args) {
22
+ return new _map.PartitionedMap(parentScope, new _map.ObjectWrappingMap(args), new Set(Object.keys(args)));
29
23
  }
@@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.version = void 0;
7
- var version = exports.version = '12.3.1';
7
+ var version = exports.version = '12.3.2';
8
8
  // Note: This file is automatically generated when building math.js.
9
9
  // Changes made in this file will be overwritten.
@@ -1,6 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import { isNode, isConstantNode, isOperatorNode, isParenthesisNode } from '../../utils/is.js';
3
3
  import { map } from '../../utils/array.js';
4
+ import { createSubScope } from '../../utils/scope.js';
4
5
  import { escape } from '../../utils/string.js';
5
6
  import { getSafeProperty, isSafeMethod } from '../../utils/customs.js';
6
7
  import { getAssociativity, getPrecedence, isAssociativeWith, properties } from '../operators.js';
@@ -289,7 +290,7 @@ export var createOperatorNode = /* #__PURE__ */factory(name, dependencies, _ref
289
290
  // "raw" evaluation
290
291
  var rawArgs = this.args;
291
292
  return function evalOperatorNode(scope, args, context) {
292
- return fn(rawArgs, math, scope);
293
+ return fn(rawArgs, math, createSubScope(scope, args));
293
294
  };
294
295
  } else if (evalArgs.length === 1) {
295
296
  var evalArg0 = evalArgs[0];
@@ -1,11 +1,11 @@
1
1
  import { isSymbolNode } from '../../../utils/is.js';
2
- import { createSubScope } from '../../../utils/scope.js';
2
+ import { PartitionedMap } from '../../../utils/map.js';
3
3
 
4
4
  /**
5
5
  * Compile an inline expression like "x > 0"
6
6
  * @param {Node} expression
7
7
  * @param {Object} math
8
- * @param {Object} scope
8
+ * @param {Map} scope
9
9
  * @return {function} Returns a function with one argument which fills in the
10
10
  * undefined variable (like "x") and evaluates the expression
11
11
  */
@@ -20,10 +20,11 @@ export function compileInlineExpression(expression, math, scope) {
20
20
 
21
21
  // create a test function for this equation
22
22
  var name = symbol.name; // variable name
23
- var subScope = createSubScope(scope);
23
+ var argsScope = new Map();
24
+ var subScope = new PartitionedMap(scope, argsScope, new Set([name]));
24
25
  var eq = expression.compile();
25
26
  return function inlineExpression(x) {
26
- subScope.set(name, x);
27
+ argsScope.set(name, x);
27
28
  return eq.evaluate(subScope);
28
29
  };
29
30
  }
@@ -14,7 +14,7 @@ export class ObjectWrappingMap {
14
14
  this.wrappedObject = object;
15
15
  }
16
16
  keys() {
17
- return Object.keys(this.wrappedObject);
17
+ return Object.keys(this.wrappedObject).values();
18
18
  }
19
19
  get(key) {
20
20
  return getSafeProperty(this.wrappedObject, key);
@@ -26,6 +26,102 @@ export class ObjectWrappingMap {
26
26
  has(key) {
27
27
  return hasSafeProperty(this.wrappedObject, key);
28
28
  }
29
+ entries() {
30
+ return mapIterator(this.keys(), key => [key, this.get(key)]);
31
+ }
32
+ forEach(callback) {
33
+ for (var key of this.keys()) {
34
+ callback(this.get(key), key, this);
35
+ }
36
+ }
37
+ delete(key) {
38
+ delete this.wrappedObject[key];
39
+ }
40
+ clear() {
41
+ for (var key of this.keys()) {
42
+ this.delete(key);
43
+ }
44
+ }
45
+ get size() {
46
+ return Object.keys(this.wrappedObject).length;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Create a map with two partitions: a and b.
52
+ * The set with bKeys determines which keys/values are read/written to map b,
53
+ * all other values are read/written to map a
54
+ *
55
+ * For example:
56
+ *
57
+ * const a = new Map()
58
+ * const b = new Map()
59
+ * const p = new PartitionedMap(a, b, new Set(['x', 'y']))
60
+ *
61
+ * In this case, values `x` and `y` are read/written to map `b`,
62
+ * all other values are read/written to map `a`.
63
+ */
64
+ export class PartitionedMap {
65
+ /**
66
+ * @param {Map} a
67
+ * @param {Map} b
68
+ * @param {Set} bKeys
69
+ */
70
+ constructor(a, b, bKeys) {
71
+ this.a = a;
72
+ this.b = b;
73
+ this.bKeys = bKeys;
74
+ }
75
+ get(key) {
76
+ return this.bKeys.has(key) ? this.b.get(key) : this.a.get(key);
77
+ }
78
+ set(key, value) {
79
+ if (this.bKeys.has(key)) {
80
+ this.b.set(key, value);
81
+ } else {
82
+ this.a.set(key, value);
83
+ }
84
+ return this;
85
+ }
86
+ has(key) {
87
+ return this.b.has(key) || this.a.has(key);
88
+ }
89
+ keys() {
90
+ return new Set([...this.a.keys(), ...this.b.keys()])[Symbol.iterator]();
91
+ }
92
+ entries() {
93
+ return mapIterator(this.keys(), key => [key, this.get(key)]);
94
+ }
95
+ forEach(callback) {
96
+ for (var key of this.keys()) {
97
+ callback(this.get(key), key, this);
98
+ }
99
+ }
100
+ delete(key) {
101
+ return this.bKeys.has(key) ? this.b.delete(key) : this.a.delete(key);
102
+ }
103
+ clear() {
104
+ this.a.clear();
105
+ this.b.clear();
106
+ }
107
+ get size() {
108
+ return [...this.keys()].length;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Create a new iterator that maps over the provided iterator, applying a mapping function to each item
114
+ */
115
+ function mapIterator(it, callback) {
116
+ return {
117
+ next: () => {
118
+ var n = it.next();
119
+ return n.done ? n : {
120
+ value: callback(n.value),
121
+ done: false
122
+ };
123
+ }
124
+ };
29
125
  }
30
126
 
31
127
  /**
@@ -1,4 +1,4 @@
1
- import { createEmptyMap, assign } from './map.js';
1
+ import { ObjectWrappingMap, PartitionedMap } from './map.js';
2
2
 
3
3
  /**
4
4
  * Create a new scope which can access the parent scope,
@@ -10,15 +10,9 @@ import { createEmptyMap, assign } from './map.js';
10
10
  * the remaining `args`.
11
11
  *
12
12
  * @param {Map} parentScope
13
- * @param {...any} args
14
- * @returns {Map}
13
+ * @param {Object} args
14
+ * @returns {PartitionedMap}
15
15
  */
16
- export function createSubScope(parentScope) {
17
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
18
- args[_key - 1] = arguments[_key];
19
- }
20
- if (typeof parentScope.createSubScope === 'function') {
21
- return assign(parentScope.createSubScope(), ...args);
22
- }
23
- return assign(createEmptyMap(), parentScope, ...args);
16
+ export function createSubScope(parentScope, args) {
17
+ return new PartitionedMap(parentScope, new ObjectWrappingMap(args), new Set(Object.keys(args)));
24
18
  }
@@ -1,3 +1,3 @@
1
- export var version = '12.3.1';
1
+ export var version = '12.3.2';
2
2
  // Note: This file is automatically generated when building math.js.
3
3
  // Changes made in this file will be overwritten.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mathjs",
3
- "version": "12.3.1",
3
+ "version": "12.3.2",
4
4
  "description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.",
5
5
  "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
6
6
  "homepage": "https://mathjs.org",
@@ -43,8 +43,8 @@
43
43
  "@babel/register": "7.23.7",
44
44
  "@types/assert": "1.5.10",
45
45
  "@types/mocha": "10.0.6",
46
- "@typescript-eslint/eslint-plugin": "6.20.0",
47
- "@typescript-eslint/parser": "6.20.0",
46
+ "@typescript-eslint/eslint-plugin": "6.21.0",
47
+ "@typescript-eslint/parser": "6.21.0",
48
48
  "assert": "2.1.0",
49
49
  "babel-loader": "9.1.3",
50
50
  "benchmark": "2.1.4",
@@ -74,7 +74,7 @@
74
74
  "karma-firefox-launcher": "2.1.2",
75
75
  "karma-mocha": "2.0.1",
76
76
  "karma-mocha-reporter": "2.2.5",
77
- "karma-webpack": "5.0.0",
77
+ "karma-webpack": "5.0.1",
78
78
  "mkdirp": "3.0.1",
79
79
  "mocha": "10.2.0",
80
80
  "mocha-junit-reporter": "2.2.1",
@@ -85,12 +85,12 @@
85
85
  "ndarray-pack": "1.2.1",
86
86
  "numericjs": "1.2.6",
87
87
  "pad-right": "0.2.2",
88
- "prettier": "3.2.4",
88
+ "prettier": "3.2.5",
89
89
  "process": "0.11.10",
90
90
  "sylvester": "0.0.21",
91
91
  "ts-node": "10.9.2",
92
92
  "typescript": "5.3.3",
93
- "webpack": "5.90.0",
93
+ "webpack": "5.90.1",
94
94
  "zeros": "1.0.0"
95
95
  },
96
96
  "type": "module",