mathjs 14.6.0 → 14.7.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.
@@ -23,7 +23,6 @@ function configFactory(config, emit) {
23
23
  *
24
24
  * Examples:
25
25
  *
26
- *
27
26
  * import { create, all } from 'mathjs'
28
27
  *
29
28
  * // create a mathjs instance
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createParserClass = void 0;
7
7
  var _factory = require("../utils/factory.js");
8
+ var _is = require("../utils/is.js");
8
9
  var _map = require("../utils/map.js");
9
10
  const name = 'Parser';
10
11
  const dependencies = ['evaluate', 'parse'];
@@ -160,7 +161,40 @@ const createParserClass = exports.createParserClass = /* #__PURE__ */(0, _factor
160
161
  Parser.prototype.clear = function () {
161
162
  this.scope.clear();
162
163
  };
164
+ Parser.prototype.toJSON = function () {
165
+ const json = {
166
+ mathjs: 'Parser',
167
+ variables: {},
168
+ functions: {}
169
+ };
170
+ for (const [name, value] of this.scope) {
171
+ if ((0, _is.isFunction)(value)) {
172
+ if (!isExpressionFunction(value)) {
173
+ throw new Error(`Cannot serialize external function ${name}`);
174
+ }
175
+ json.functions[name] = `${value.syntax} = ${value.expr}`;
176
+ } else {
177
+ json.variables[name] = value;
178
+ }
179
+ }
180
+ return json;
181
+ };
182
+ Parser.fromJSON = function (json) {
183
+ const parser = new Parser();
184
+ Object.entries(json.variables).forEach(_ref2 => {
185
+ let [name, value] = _ref2;
186
+ return parser.set(name, value);
187
+ });
188
+ Object.entries(json.functions).forEach(_ref3 => {
189
+ let [_name, fn] = _ref3;
190
+ return parser.evaluate(fn);
191
+ });
192
+ return parser;
193
+ };
163
194
  return Parser;
164
195
  }, {
165
196
  isClass: true
166
- });
197
+ });
198
+ function isExpressionFunction(value) {
199
+ return typeof value === 'function' && typeof value.syntax === 'string' && typeof value.expr === 'string';
200
+ }
@@ -105,7 +105,8 @@ const createFunctionAssignmentNode = exports.createFunctionAssignmentNode = /* #
105
105
  });
106
106
 
107
107
  // compile the function expression with the child args
108
- const evalExpr = this.expr._compile(math, childArgNames);
108
+ const expr = this.expr;
109
+ const evalExpr = expr._compile(math, childArgNames);
109
110
  const name = this.name;
110
111
  const params = this.params;
111
112
  const signature = (0, _array.join)(this.types, ',');
@@ -121,6 +122,7 @@ const createFunctionAssignmentNode = exports.createFunctionAssignmentNode = /* #
121
122
  };
122
123
  const fn = typed(name, signatures);
123
124
  fn.syntax = syntax;
125
+ fn.expr = expr.toString();
124
126
  scope.set(name, fn);
125
127
  return fn;
126
128
  };
@@ -54,7 +54,7 @@ const createParse = exports.createParse = /* #__PURE__ */(0, _factory.factory)(n
54
54
  * node1.compile().evaluate() // 5
55
55
  *
56
56
  * let scope = {a:3, b:4}
57
- * const node2 = math.parse('a * b') // 12
57
+ * const node2 = math.parse('a * b')
58
58
  * node2.evaluate(scope) // 12
59
59
  * const code2 = node2.compile()
60
60
  * code2.evaluate(scope) // 12
@@ -22,7 +22,7 @@ const createRange = exports.createRange = /* #__PURE__ */(0, _factory.factory)(n
22
22
  isPositive
23
23
  } = _ref;
24
24
  /**
25
- * Create an array from a range.
25
+ * Create a matrix or array containing a range of values.
26
26
  * By default, the range end is excluded. This can be customized by providing
27
27
  * an extra parameter `includeEnd`.
28
28
  *
@@ -50,10 +50,12 @@ const createRange = exports.createRange = /* #__PURE__ */(0, _factory.factory)(n
50
50
  * - `includeEnd: boolean`
51
51
  * Option to specify whether to include the end or not. False by default.
52
52
  *
53
- * Note that the return type of the range is taken from the type of
54
- * the start/end. If only one these is a built-in `number` type, it will
55
- * be promoted to the type of the other endpoint. However, in the case of
56
- * Unit values, both endpoints must have compatible units, and the return
53
+ * The function returns a `DenseMatrix` when the library is configured with
54
+ * `config = { matrix: 'Matrix' }, and returns an Array otherwise.
55
+ * Note that the type of the returned values is taken from the type of the
56
+ * provided start/end value. If only one of these is a built-in `number` type,
57
+ * it will be promoted to the type of the other endpoint. However, in the case
58
+ * of Unit values, both endpoints must have compatible units, and the return
57
59
  * value will have compatible units as well.
58
60
  *
59
61
  * Examples:
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 14.6.0
10
- * @date 2025-07-25
9
+ * @version 14.7.0
10
+ * @date 2025-09-05
11
11
  *
12
12
  * @license
13
13
  * Copyright (C) 2013-2025 Jos de Jong <wjosdejong@gmail.com>
@@ -679,24 +679,28 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
679
679
  }
680
680
  return;
681
681
  }
682
- const index = [];
683
- const recurse = function* (value, depth) {
684
- if (depth < maxDepth) {
685
- for (let i = 0; i < value.length; i++) {
686
- index[depth] = i;
687
- yield* recurse(value[i], depth + 1);
688
- }
689
- } else {
690
- for (let i = 0; i < value.length; i++) {
691
- index[depth] = i;
692
- yield {
693
- value: value[i],
694
- index: index.slice()
695
- };
696
- }
682
+
683
+ // Multi-dimensional matrix: iterate over all elements
684
+ const index = Array(maxDepth + 1).fill(0);
685
+ const totalElements = this._size.reduce((a, b) => a * b, 1);
686
+ for (let count = 0; count < totalElements; count++) {
687
+ // Traverse to the current element using indices
688
+ let current = this._data;
689
+ for (let d = 0; d < maxDepth; d++) {
690
+ current = current[index[d]];
697
691
  }
698
- };
699
- yield* recurse(this._data, 0);
692
+ yield {
693
+ value: current[index[maxDepth]],
694
+ index: index.slice()
695
+ };
696
+
697
+ // Increment indices for next element
698
+ for (let d = maxDepth; d >= 0; d--) {
699
+ index[d]++;
700
+ if (index[d] < this._size[d]) break;
701
+ index[d] = 0;
702
+ }
703
+ }
700
704
  };
701
705
 
702
706
  /**
@@ -23,13 +23,13 @@ const createString = exports.createString = /* #__PURE__ */(0, _factory.factory)
23
23
  *
24
24
  * Examples:
25
25
  *
26
- * math.string(4.2) // returns string '4.2'
27
- * math.string(math.complex(3, 2) // returns string '3 + 2i'
26
+ * math.string(4.2) // returns string '4.2'
27
+ * math.string(math.complex(3, 2)) // returns string '3 + 2i'
28
28
  *
29
29
  * const u = math.unit(5, 'km')
30
- * math.string(u.to('m')) // returns string '5000 m'
30
+ * math.string(u.to('m')) // returns string '5000 m'
31
31
  *
32
- * math.string([true, false]) // returns ['true', 'false']
32
+ * math.string([true, false]) // returns ['true', 'false']
33
33
  *
34
34
  * See also:
35
35
  *
@@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.version = void 0;
7
- const version = exports.version = '14.6.0';
7
+ const version = exports.version = '14.7.0';
8
8
  // Note: This file is automatically generated when building math.js.
9
9
  // Changes made in this file will be overwritten.
@@ -16,7 +16,6 @@ export function configFactory(config, emit) {
16
16
  *
17
17
  * Examples:
18
18
  *
19
- *
20
19
  * import { create, all } from 'mathjs'
21
20
  *
22
21
  * // create a mathjs instance
@@ -1,4 +1,5 @@
1
1
  import { factory } from '../utils/factory.js';
2
+ import { isFunction } from '../utils/is.js';
2
3
  import { createEmptyMap, toObject } from '../utils/map.js';
3
4
  var name = 'Parser';
4
5
  var dependencies = ['evaluate', 'parse'];
@@ -154,7 +155,40 @@ export var createParserClass = /* #__PURE__ */factory(name, dependencies, _ref =
154
155
  Parser.prototype.clear = function () {
155
156
  this.scope.clear();
156
157
  };
158
+ Parser.prototype.toJSON = function () {
159
+ var json = {
160
+ mathjs: 'Parser',
161
+ variables: {},
162
+ functions: {}
163
+ };
164
+ for (var [_name2, value] of this.scope) {
165
+ if (isFunction(value)) {
166
+ if (!isExpressionFunction(value)) {
167
+ throw new Error("Cannot serialize external function ".concat(_name2));
168
+ }
169
+ json.functions[_name2] = "".concat(value.syntax, " = ").concat(value.expr);
170
+ } else {
171
+ json.variables[_name2] = value;
172
+ }
173
+ }
174
+ return json;
175
+ };
176
+ Parser.fromJSON = function (json) {
177
+ var parser = new Parser();
178
+ Object.entries(json.variables).forEach(_ref2 => {
179
+ var [name, value] = _ref2;
180
+ return parser.set(name, value);
181
+ });
182
+ Object.entries(json.functions).forEach(_ref3 => {
183
+ var [_name, fn] = _ref3;
184
+ return parser.evaluate(fn);
185
+ });
186
+ return parser;
187
+ };
157
188
  return Parser;
158
189
  }, {
159
190
  isClass: true
160
- });
191
+ });
192
+ function isExpressionFunction(value) {
193
+ return typeof value === 'function' && typeof value.syntax === 'string' && typeof value.expr === 'string';
194
+ }
@@ -98,7 +98,8 @@ export var createFunctionAssignmentNode = /* #__PURE__ */factory(name, dependenc
98
98
  });
99
99
 
100
100
  // compile the function expression with the child args
101
- var evalExpr = this.expr._compile(math, childArgNames);
101
+ var expr = this.expr;
102
+ var evalExpr = expr._compile(math, childArgNames);
102
103
  var name = this.name;
103
104
  var params = this.params;
104
105
  var signature = join(this.types, ',');
@@ -114,6 +115,7 @@ export var createFunctionAssignmentNode = /* #__PURE__ */factory(name, dependenc
114
115
  };
115
116
  var fn = typed(name, signatures);
116
117
  fn.syntax = syntax;
118
+ fn.expr = expr.toString();
117
119
  scope.set(name, fn);
118
120
  return fn;
119
121
  };
@@ -47,7 +47,7 @@ export var createParse = /* #__PURE__ */factory(name, dependencies, _ref => {
47
47
  * node1.compile().evaluate() // 5
48
48
  *
49
49
  * let scope = {a:3, b:4}
50
- * const node2 = math.parse('a * b') // 12
50
+ * const node2 = math.parse('a * b')
51
51
  * node2.evaluate(scope) // 12
52
52
  * const code2 = node2.compile()
53
53
  * code2.evaluate(scope) // 12
@@ -16,7 +16,7 @@ export var createRange = /* #__PURE__ */factory(name, dependencies, _ref => {
16
16
  isPositive
17
17
  } = _ref;
18
18
  /**
19
- * Create an array from a range.
19
+ * Create a matrix or array containing a range of values.
20
20
  * By default, the range end is excluded. This can be customized by providing
21
21
  * an extra parameter `includeEnd`.
22
22
  *
@@ -44,10 +44,12 @@ export var createRange = /* #__PURE__ */factory(name, dependencies, _ref => {
44
44
  * - `includeEnd: boolean`
45
45
  * Option to specify whether to include the end or not. False by default.
46
46
  *
47
- * Note that the return type of the range is taken from the type of
48
- * the start/end. If only one these is a built-in `number` type, it will
49
- * be promoted to the type of the other endpoint. However, in the case of
50
- * Unit values, both endpoints must have compatible units, and the return
47
+ * The function returns a `DenseMatrix` when the library is configured with
48
+ * `config = { matrix: 'Matrix' }, and returns an Array otherwise.
49
+ * Note that the type of the returned values is taken from the type of the
50
+ * provided start/end value. If only one of these is a built-in `number` type,
51
+ * it will be promoted to the type of the other endpoint. However, in the case
52
+ * of Unit values, both endpoints must have compatible units, and the return
51
53
  * value will have compatible units as well.
52
54
  *
53
55
  * Examples:
@@ -672,24 +672,28 @@ export var createDenseMatrixClass = /* #__PURE__ */factory(name, dependencies, _
672
672
  }
673
673
  return;
674
674
  }
675
- var index = [];
676
- var _recurse = function* recurse(value, depth) {
677
- if (depth < maxDepth) {
678
- for (var _i9 = 0; _i9 < value.length; _i9++) {
679
- index[depth] = _i9;
680
- yield* _recurse(value[_i9], depth + 1);
681
- }
682
- } else {
683
- for (var _i0 = 0; _i0 < value.length; _i0++) {
684
- index[depth] = _i0;
685
- yield {
686
- value: value[_i0],
687
- index: index.slice()
688
- };
689
- }
675
+
676
+ // Multi-dimensional matrix: iterate over all elements
677
+ var index = Array(maxDepth + 1).fill(0);
678
+ var totalElements = this._size.reduce((a, b) => a * b, 1);
679
+ for (var count = 0; count < totalElements; count++) {
680
+ // Traverse to the current element using indices
681
+ var current = this._data;
682
+ for (var d = 0; d < maxDepth; d++) {
683
+ current = current[index[d]];
690
684
  }
691
- };
692
- yield* _recurse(this._data, 0);
685
+ yield {
686
+ value: current[index[maxDepth]],
687
+ index: index.slice()
688
+ };
689
+
690
+ // Increment indices for next element
691
+ for (var _d = maxDepth; _d >= 0; _d--) {
692
+ index[_d]++;
693
+ if (index[_d] < this._size[_d]) break;
694
+ index[_d] = 0;
695
+ }
696
+ }
693
697
  };
694
698
 
695
699
  /**
@@ -17,13 +17,13 @@ export var createString = /* #__PURE__ */factory(name, dependencies, _ref => {
17
17
  *
18
18
  * Examples:
19
19
  *
20
- * math.string(4.2) // returns string '4.2'
21
- * math.string(math.complex(3, 2) // returns string '3 + 2i'
20
+ * math.string(4.2) // returns string '4.2'
21
+ * math.string(math.complex(3, 2)) // returns string '3 + 2i'
22
22
  *
23
23
  * const u = math.unit(5, 'km')
24
- * math.string(u.to('m')) // returns string '5000 m'
24
+ * math.string(u.to('m')) // returns string '5000 m'
25
25
  *
26
- * math.string([true, false]) // returns ['true', 'false']
26
+ * math.string([true, false]) // returns ['true', 'false']
27
27
  *
28
28
  * See also:
29
29
  *
@@ -1,3 +1,3 @@
1
- export var version = '14.6.0';
1
+ export var version = '14.7.0';
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": "14.6.0",
3
+ "version": "14.7.0",
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",
@@ -36,31 +36,30 @@
36
36
  "typed-function": "^4.2.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@babel/core": "7.28.0",
39
+ "@babel/core": "7.28.3",
40
40
  "@babel/plugin-transform-object-assign": "7.27.1",
41
41
  "@babel/plugin-transform-optional-catch-binding": "7.27.1",
42
- "@babel/plugin-transform-runtime": "7.28.0",
43
- "@babel/preset-env": "7.28.0",
44
- "@babel/register": "7.27.1",
42
+ "@babel/plugin-transform-runtime": "7.28.3",
43
+ "@babel/preset-env": "7.28.3",
44
+ "@babel/register": "7.28.3",
45
45
  "@types/assert": "1.5.11",
46
46
  "@types/mocha": "10.0.10",
47
- "@typescript-eslint/eslint-plugin": "8.38.0",
48
- "@typescript-eslint/parser": "8.38.0",
47
+ "@typescript-eslint/eslint-plugin": "8.42.0",
48
+ "@typescript-eslint/parser": "8.42.0",
49
49
  "assert": "2.1.0",
50
50
  "babel-loader": "10.0.0",
51
51
  "c8": "10.1.3",
52
52
  "codecov": "3.8.3",
53
- "core-js": "3.44.0",
54
53
  "del": "8.0.0",
55
- "dtslint": "4.2.1",
56
54
  "eigen": "0.2.2",
55
+ "es-check": "9.3.1",
57
56
  "eslint": "8.57.1",
58
57
  "eslint-config-prettier": "9.1.0",
59
58
  "eslint-config-standard": "17.1.0",
60
59
  "eslint-plugin-import": "2.32.0",
61
60
  "eslint-plugin-mocha": "10.5.0",
62
61
  "eslint-plugin-n": "16.6.2",
63
- "eslint-plugin-prettier": "5.5.3",
62
+ "eslint-plugin-prettier": "5.5.4",
64
63
  "eslint-plugin-promise": "6.6.0",
65
64
  "expect-type": "1.2.2",
66
65
  "expr-eval": "2.0.2",
@@ -78,7 +77,7 @@
78
77
  "karma-webdriver-launcher": "1.0.8",
79
78
  "karma-webpack": "5.0.1",
80
79
  "mkdirp": "3.0.1",
81
- "mocha": "11.7.1",
80
+ "mocha": "11.7.2",
82
81
  "mocha-junit-reporter": "2.2.1",
83
82
  "ndarray": "1.0.19",
84
83
  "ndarray-determinant": "1.0.0",
@@ -90,10 +89,10 @@
90
89
  "process": "0.11.10",
91
90
  "sinon": "21.0.0",
92
91
  "sylvester": "0.0.21",
93
- "tinybench": "4.0.1",
92
+ "tinybench": "5.0.1",
94
93
  "ts-node": "10.9.2",
95
- "typescript": "5.8.3",
96
- "webpack": "5.100.2",
94
+ "typescript": "5.9.2",
95
+ "webpack": "5.101.3",
97
96
  "zeros": "1.0.0"
98
97
  },
99
98
  "type": "module",
@@ -149,7 +148,7 @@
149
148
  "test": "npm run test:src && npm run lint",
150
149
  "test:src": "mocha test/unit-tests",
151
150
  "test:generated": "mocha test/generated-code-tests",
152
- "test:node": "mocha test/node-tests/*.test.js test/node-tests/**/*.test.js",
151
+ "test:node": "mocha test/node-tests/*.test.{js,cjs,mjs} test/node-tests/**/*.test.{js,cjs,mjs}",
153
152
  "test:all": "npm run test:src && npm run test:generated && npm run test:node && npm run test:types",
154
153
  "test:browser": "karma start test/browser-test-config/local-karma.js",
155
154
  "test:lambdatest": "karma start test/browser-test-config/lambdatest-karma.js",
package/types/index.d.ts CHANGED
@@ -1904,6 +1904,14 @@ export interface MathJsInstance extends MathJsFactory {
1904
1904
  */
1905
1905
  det(x: MathCollection): number
1906
1906
 
1907
+ /**
1908
+ * Calculate the difference between adjacent elements of a matrix or array.
1909
+ * @param x A matrix or array
1910
+ * @param dim The dimension to apply the difference on.
1911
+ * @returns A matrix or array containing the differences
1912
+ */
1913
+ diff<T extends MathCollection>(x: T, dim?: number | BigNumber): T
1914
+
1907
1915
  /**
1908
1916
  * Create a diagonal matrix or retrieve the diagonal of a matrix. When x
1909
1917
  * is a vector, a matrix with vector x on the diagonal will be returned.
@@ -4079,7 +4087,8 @@ export const {
4079
4087
  concatTransformDependencies,
4080
4088
  stdTransformDependencies,
4081
4089
  sumTransformDependencies,
4082
- varianceTransformDependencies
4090
+ varianceTransformDependencies,
4091
+ printTransformDependencies
4083
4092
  }: Record<string, FactoryFunctionMap>
4084
4093
 
4085
4094
  export interface Matrix<T = MathGeneric> {
@@ -5805,6 +5814,15 @@ export interface MathJsChain<TValue> {
5805
5814
  y: MathCollection
5806
5815
  ): MathJsChain<MathCollection>
5807
5816
 
5817
+ /**
5818
+ * Calculate the difference between adjacent elements of the chained matrix or array.
5819
+ * @param dim The dimension to apply the difference on.
5820
+ */
5821
+ diff<T extends MathCollection>(
5822
+ this: MathJsChain<T>,
5823
+ dim?: number | BigNumber
5824
+ ): MathJsChain<T>
5825
+
5808
5826
  /**
5809
5827
  * Calculate the determinant of a matrix.
5810
5828
  */
package/types/tslint.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "extends": "dtslint/dtslint.json",
3
- "rules": {
4
- "no-redundant-jsdoc": false
5
- }
6
- }