Skip to content

Commit 23af33d

Browse files
committed
Refactor unit conversion and matrix utility functions; add extensive linear algebra tests
- Updated globalUnits.js to improve structure and comments for length and radiation units. - Enhanced matrix.js with JSDoc annotations for better type checking and clarity. - Refined store.js and variables/store.js with improved documentation and type annotations. - Removed outdated notepad.html file to streamline project structure. - Introduced a new lint configuration file for consistent code formatting and linting. - Added comprehensive tests for linear algebra operations including matrix arithmetic, vector operations, and advanced decompositions.
1 parent 8e57177 commit 23af33d

29 files changed

Lines changed: 6241 additions & 2042 deletions

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
npx lint-staged
1+
npx lint-staged --config lint.config.js

.npmignore

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
.github/
2+
.husky/
3+
24
docs/
3-
node_modules/
45
test/
6+
node_modules/
57

6-
.gitattributes
78
.gitignore
9+
.npmignore
10+
.prettierrc
11+
.gitattributes
12+
.prettierignore
13+
.editorconfig
14+
815
CHANGELOG.md
16+
17+
tsconfig.json
918
package-lock.json
10-
rollup.config.js
1119

12-
.prettierrc
13-
.prettierignore
20+
lint.config.js
21+
rollup.config.js
1422
eslint.config.js
15-
tsconfig.json
16-
.husky/
23+

README.md

Lines changed: 196 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# Exprify
2-
31
[![Exprify Social Banner](https://raw.githubusercontent.com/code-hemu/Exprify/refs/heads/main/docs/assets/capture.jpg)](https://github.com/code-hemu/Exprify)
42

5-
Exprify is a JavaScript expression parser and evaluator for math-heavy apps. It supports arithmetic, variables, custom functions, unit conversion, matrices, complex numbers, symbolic helpers, and a growing set of linear algebra utilities.
3+
Exprify is a JavaScript expression parser and evaluator. It is made for math applications, scientific computing, and complex workflows in the browser and Node.js. It supports basic arithmetic, variables, and defined functions. Exprify also covers advanced mathematical areas like unit conversion, matrix operations, complex number arithmetic, and symbolic manipulation.
64

75
[![Version](https://img.shields.io/npm/v/exprify)](https://www.npmjs.com/package/exprify)
86
[![Downloads](https://img.shields.io/npm/dt/exprify)](https://www.npmjs.com/package/exprify)
@@ -18,6 +16,17 @@ Exprify is a JavaScript expression parser and evaluator for math-heavy apps. It
1816
[![Github Sponsor](https://img.shields.io/github/sponsors/code-hemu)](https://github.com/sponsors/code-hemu)
1917

2018

19+
## Features
20+
21+
- Math expression parsing and evaluation
22+
- Variables, functions, and lambdas
23+
- Unit conversion support
24+
- Matrix and linear algebra operations
25+
- Complex numbers
26+
- Symbolic math utilities
27+
- Calculus and statistics tools
28+
29+
2130
## Installation
2231

2332
```bash
@@ -196,9 +205,142 @@ expr.evaluate("a to inch");
196205
// "4 inch"
197206
```
198207

208+
### Constants
209+
210+
```js
211+
expr.evaluate("PHI");
212+
// 1.6180...
213+
214+
expr.evaluate("TAU");
215+
// 6.2832...
216+
217+
expr.evaluate("INFINITY > 1e308");
218+
// true
219+
220+
expr.evaluate("isNaN(NaN)");
221+
// true
222+
```
223+
224+
### Range Operator
225+
226+
```js
227+
expr.evaluate("1:5");
228+
// [1,2,3,4,5]
229+
230+
expr.evaluate("r = 3:7");
231+
// [3,4,5,6,7]
232+
233+
expr.evaluate("sum(1:10)");
234+
// 55
235+
```
236+
237+
### Lambda Expressions
238+
239+
```js
240+
expr.evaluate("sq = x -> x^2");
241+
// function
242+
243+
expr.evaluate('map([1, 2, 3], x -> x^2)');
244+
// [1,4,9]
245+
```
246+
247+
### Compound Assignment
248+
249+
```js
250+
expr.evaluate("a = 10");
251+
expr.evaluate("a += 5");
252+
// 15
253+
254+
expr.evaluate("a *= 2");
255+
// 30
256+
```
257+
258+
### Spread Operator
259+
260+
```js
261+
expr.evaluate("max(...[1, 5, 3])");
262+
// 5
263+
264+
expr.evaluate("max(10, ...[1, 5, 3], 7)");
265+
// 10
266+
```
267+
268+
### String Utilities
269+
270+
```js
271+
expr.evaluate('split("a,b,c", ",")');
272+
// ["a","b","c"]
273+
274+
expr.evaluate('join(["a", "b", "c"], ", ")');
275+
// "a, b, c"
276+
277+
expr.evaluate('upper("hello")');
278+
// "HELLO"
279+
280+
expr.evaluate('lower("HELLO")');
281+
// "hello"
282+
283+
expr.evaluate('trim(" hi ")');
284+
// "hi"
285+
286+
expr.evaluate('replace("hello world", "world", "there")');
287+
// "hello there"
288+
289+
expr.evaluate('substring("hello", 1, 4)');
290+
// "ell"
291+
```
292+
293+
### Array Utilities
294+
295+
```js
296+
expr.evaluate('map([1, 4, 9], "sqrt")');
297+
// [1,2,3]
298+
299+
expr.evaluate('filter([1, 2, 3, 4, 5], "isPrime")');
300+
// [2,3,5]
301+
```
302+
303+
### Calculus
304+
305+
```js
306+
expr.evaluate('integral("x^2", 0, 1)');
307+
// 0.333... (Simpson's rule)
308+
309+
expr.evaluate('sigma("n", 1, 10, "n")');
310+
// 55
311+
312+
expr.evaluate('sigma("n", 1, 5, "n^2")');
313+
// 55
314+
315+
expr.evaluate('pi("n", 1, 5, "n")');
316+
// 120
317+
318+
expr.evaluate('substitute("x + 1", "x", 5)');
319+
// 6
320+
321+
expr.evaluate('limit("1/x", "x", 1000000, "right")');
322+
// ~0
323+
```
324+
325+
### Symbolic Helpers
326+
327+
```js
328+
expr.evaluate('expand("(x+1)^2")');
329+
// "x^2 + 2x + 1"
330+
331+
expr.evaluate('factor("x^2 - 5x + 6")');
332+
// "(x - 2)(x - 3)"
333+
334+
expr.evaluate('solve("x^2 - 4 = 0")');
335+
// [-2,2]
336+
337+
expr.evaluate('solve("2x - 8 = 0")');
338+
// [4]
339+
```
340+
199341
### Matrices
200342

201-
Exprify supports matrix literals with `;` as row separators.
343+
Exprify supports matrix literals with `;` as row separators, and matrix arithmetic (addition, subtraction, multiplication, scalar multiplication, integer power).
202344

203345
```js
204346
expr.evaluate("a = [1, 2, 3; 4, 5, 6]");
@@ -215,6 +357,18 @@ expr.evaluate("a[3, 1:3] = [7, 8, 9]");
215357

216358
expr.evaluate("det([-1, 2; 3, 1])");
217359
// -7
360+
361+
expr.evaluate("[[1, 2], [3, 4]] + [[5, 6], [7, 8]]");
362+
// "6\t8\n10\t12"
363+
364+
expr.evaluate("[[1, 2], [3, 4]] * [[5, 6], [7, 8]]");
365+
// "19\t22\n43\t50"
366+
367+
expr.evaluate("3 * [[1, 2], [3, 4]]");
368+
// "3\t6\n9\t12"
369+
370+
expr.evaluate("[[1, 1], [1, 0]] ^ 3");
371+
// "3\t2\n2\t1"
218372
```
219373

220374
### Linear Algebra Helpers
@@ -315,6 +469,25 @@ expr.evaluate('leafCount(parse("{a: 22/7, b: 10^(1/2)}"))');
315469
| `lyap` | Solve Lyapunov equation | `lyap([[-2,0];[1,-4]], [[3,1];[1,3]])` | matrix JSON |
316470
| `qr` | QR decomposition | `qr([[1,-1,4];[1,4,-2];[1,4,2];[1,-1,0]])` | matrix JSON |
317471
| `polynomialRoot` | Find polynomial roots | `polynomialRoot(-6, 11, -6, 1)` | `[1,3,2]` |
472+
| `transpose` | Matrix transpose | `transpose([[1,2];[3,4]])` | matrix JSON |
473+
| `inverse` | Matrix inverse | `inverse([[1,2];[3,4]])` | matrix JSON |
474+
| `trace` | Sum of diagonal elements | `trace([[1,2];[3,4]])` | `5` |
475+
| `rank` | Matrix rank | `rank([[1,2];[3,4]])` | `2` |
476+
| `rref` | Reduced row echelon form | `rref([[1,2,3];[4,5,6]])` | matrix JSON |
477+
| `minor` | Matrix minor (submatrix det) | `minor([[1,2];[3,4]], 0, 0)` | `4` |
478+
| `cofactor` | Matrix cofactor | `cofactor([[1,2];[3,4]], 0, 1)` | `-3` |
479+
| `cross` | 3D cross product | `cross([1,0,0], [0,1,0])` | `[0,0,1]` |
480+
| `normalize` | Unit vector | `normalize([3,4])` | `[0.6,0.8]` |
481+
| `angle` | Angle between vectors | `angle([1,0], [0,1])` | `1.5708` |
482+
| `projection` | Scalar projection | `projection([3,4], [1,0])` | `3` |
483+
| `identity` | Identity matrix (n×n) | `identity(3)` | matrix JSON |
484+
| `eye` | Identity matrix alias | `eye(2)` | matrix JSON |
485+
| `zeros` | Zero matrix (n×m) | `zeros(2, 3)` | matrix JSON |
486+
| `ones` | Ones matrix (n×m) | `ones(2, 2)` | matrix JSON |
487+
| `diag` | Diagonal matrix from array | `diag([1,2,3])` | matrix JSON |
488+
| `cholesky` | Cholesky decomposition | `cholesky([[4,2];[2,3]])` | matrix JSON |
489+
| `eig` | Eigenvalues & vectors (2×2) | `eig([[1,2];[2,1]])` | JSON |
490+
| `svd` | SVD (2×2) | `svd([[1,0];[0,2]])` | JSON |
318491
| `simplify` | Simplify polynomial | `simplify("x^2 + 2x^2")` | `3 * x^2` |
319492
| `derivative` | Differentiate polynomial | `derivative("x^3")` | `3 * x^2` |
320493
| `rationalize` | Rationalize expression | `rationalize("1/x + 1/(x+1)")` | `(2x + 1)/(x^2 + x)` |
@@ -324,6 +497,23 @@ expr.evaluate('leafCount(parse("{a: 22/7, b: 10^(1/2)}"))');
324497
| `random` | Random number (0, 1) | `random()` | e.g. `0.374` |
325498
| `leafCount` | Count expression leaves | `leafCount("e^(i*pi)-1")` | `4` |
326499
| `parse` | Return expression string | `parse("a + b")` | `"a + b"` |
500+
| `split` | Split string by separator | `split("a,b,c", ",")` | `["a","b","c"]` |
501+
| `join` | Join array with separator | `join(["a","b"], ",")` | `"a,b"` |
502+
| `upper` | Uppercase string | `upper("hi")` | `"HI"` |
503+
| `lower` | Lowercase string | `lower("HI")` | `"hi"` |
504+
| `trim` | Trim whitespace | `trim(" hi ")` | `"hi"` |
505+
| `replace` | Replace substring | `replace("hi","i","o")` | `"ho"` |
506+
| `substring` | Extract substring | `substring("hello",1,4)` | `"ell"` |
507+
| `map` | Apply function to array | `map([1,4,9],"sqrt")` | `[1,2,3]` |
508+
| `filter` | Filter array by predicate | `filter([1,2,3,4,5],"isPrime")` | `[2,3,5]` |
509+
| `integral` | Numeric integral (Simpson) | `integral("x^2",0,1)` | `~0.333` |
510+
| `sigma` | Summation notation | `sigma("n",1,10,"n")` | `55` |
511+
| `pi` | Product notation | `pi("n",1,5,"n")` | `120` |
512+
| `limit` | Numeric limit | `limit("1/x","x",1e6,"right")` | `~0` |
513+
| `substitute` | Replace variable with value | `substitute("x+1","x",5)` | `6` |
514+
| `expand` | Expand polynomial expression | `expand("(x+1)^2")` | `"x^2 + 2x + 1"` |
515+
| `factor` | Factor polynomial | `factor("x^2-5x+6")` | `"(x-2)(x-3)"` |
516+
| `solve` | Solve polynomial equation | `solve("x^2-4=0")` | `[-2,2]` |
327517

328518
## Return Types
329519

@@ -335,6 +525,8 @@ Depending on the expression, `evaluate()` may return:
335525
- formatted complex strings like `"3 + 2i"`
336526
- matrix wrapper JSON strings such as `{"exprify":"DenseMatrix",...}`
337527
- JSON strings for structured helper outputs like `lup()` or `rationalize(..., true)`
528+
- native JavaScript functions for lambda expressions (`evaluate("x -> x^2")` returns a `function`)
529+
- arrays for range expressions like `[1,2,3,4,5]`
338530

339531
## Manual Build
340532

0 commit comments

Comments
 (0)