exprify 1.0.1 → 1.0.3

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Exprify Social Banner](https://raw.githubusercontent.com/code-hemu/Exprify/refs/heads/main/src/assets/capture.jpg)](https://github.com/code-hemu/Exprify)
4
4
 
5
- Exprify is a JavaScript math expression parser and evaluator with runtime type checking. It supports arithmetic, variables, custom functions, unit conversion, matrices, complex numbers, and a small set of algebra helpers.
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.
6
6
 
7
7
  ## Installation
8
8
 
@@ -19,6 +19,10 @@ const expr = new Exprify();
19
19
 
20
20
  console.log(expr.evaluate("5 + 7 * 2"));
21
21
  // 19
22
+
23
+ expr.setVariable("x", 10);
24
+ console.log(expr.evaluate("x + 5"));
25
+ // 15
22
26
  ```
23
27
 
24
28
  ## Browser Usage
@@ -31,11 +35,18 @@ console.log(expr.evaluate("5 + 7 * 2"));
31
35
  </script>
32
36
  ```
33
37
 
38
+ `unpkg` resolves to the browser bundle from `dist/exprify.min.js`.
39
+
34
40
  ## API
35
41
 
36
42
  ### `new Exprify()`
37
43
 
38
- Creates a new evaluator instance with isolated variable, function, unit, and compile-cache state.
44
+ Creates a new evaluator instance with isolated state for:
45
+
46
+ - variables
47
+ - functions
48
+ - units
49
+ - compiled-expression cache
39
50
 
40
51
  ### `expr.evaluate(expression)`
41
52
 
@@ -48,7 +59,7 @@ expr.evaluate("10 + 5 * 2");
48
59
 
49
60
  ### `expr.parse(expression)`
50
61
 
51
- Returns the token list and AST for an expression.
62
+ Returns `{ tokens, ast }`.
52
63
 
53
64
  ```js
54
65
  const parsed = expr.parse("2 inch to cm");
@@ -58,7 +69,7 @@ console.log(parsed.ast);
58
69
 
59
70
  ### `expr.compile(expression)`
60
71
 
61
- Compiles an expression once and returns a reusable function. You can pass a temporary scope object when calling it.
72
+ Compiles an expression once and returns a reusable function.
62
73
 
63
74
  ```js
64
75
  const area = expr.compile("width * height");
@@ -69,7 +80,7 @@ console.log(area({ width: 6, height: 4 }));
69
80
 
70
81
  ### `expr.setVariable(name, value)` / `expr.getVariable(name)`
71
82
 
72
- Stores values on the instance for reuse across evaluations.
83
+ Stores and reuses values across evaluations.
73
84
 
74
85
  ```js
75
86
  expr.setVariable("x", 10);
@@ -81,7 +92,7 @@ console.log(expr.evaluate("x + y * 2"));
81
92
 
82
93
  ### `expr.addFunction(name, fn)`
83
94
 
84
- Registers a custom function.
95
+ Registers a custom JavaScript function.
85
96
 
86
97
  ```js
87
98
  expr.addFunction("double", (n) => n * 2);
@@ -90,9 +101,20 @@ console.log(expr.evaluate("double(5) + 3"));
90
101
  // 13
91
102
  ```
92
103
 
93
- ## Supported Features
104
+ ### Inline Function Definitions
105
+
106
+ You can define functions inside expressions.
107
+
108
+ ```js
109
+ expr.evaluate("hyp(a, b) = sqrt(a ^ 2 + b ^ 2)");
110
+
111
+ console.log(expr.evaluate("hyp(3, 4)"));
112
+ // 5
113
+ ```
114
+
115
+ ## Features
94
116
 
95
- ### Arithmetic and precedence
117
+ ### Arithmetic
96
118
 
97
119
  ```js
98
120
  expr.evaluate("2 + 3 * 4");
@@ -105,7 +127,7 @@ expr.evaluate("11n ^ 2n");
105
127
  // 121n
106
128
  ```
107
129
 
108
- ### Strings and booleans
130
+ ### Strings, Booleans, Complex Numbers
109
131
 
110
132
  ```js
111
133
  expr.evaluate('"Hello " + "World"');
@@ -113,24 +135,12 @@ expr.evaluate('"Hello " + "World"');
113
135
 
114
136
  expr.evaluate("true && false");
115
137
  // false
116
- ```
117
-
118
- ### Built-in functions
119
138
 
120
- ```js
121
- expr.evaluate("max(10, 25, 7)");
122
- // 25
123
-
124
- expr.evaluate("min(10, 25, 7)");
125
- // 7
126
-
127
- expr.evaluate("sqrt(81)");
128
- // 9
139
+ expr.evaluate("9 / 3 + 2i");
140
+ // "3 + 2i"
129
141
  ```
130
142
 
131
- Common built-ins include `max`, `min`, `abs`, `round`, `floor`, `ceil`, `sqrt`, `pow`, `sin`, `cos`, `tan`, `log`, `log10`, `exp`, `clamp`, `if`, `length`, `typeof`, `det`, `simplify`, and `derivative`.
132
-
133
- ### Unit conversion
143
+ ### Unit Conversion
134
144
 
135
145
  ```js
136
146
  expr.evaluate("2 inch to cm");
@@ -141,30 +151,52 @@ expr.evaluate("5 cm + 2 inch");
141
151
 
142
152
  expr.evaluate("5cm + 0.2 m in inch");
143
153
  // "9.84251968503937 inch"
154
+
155
+ expr.evaluate("a = 5.08 cm + 2 inch");
156
+ expr.evaluate("a to inch");
157
+ // "4 inch"
144
158
  ```
145
159
 
146
160
  ### Matrices
147
161
 
148
- ```js
149
- expr.evaluate("det([-1, 2; 3, 1])");
150
- // -7
162
+ Exprify supports matrix literals with `;` as row separators.
151
163
 
164
+ ```js
152
165
  expr.evaluate("a = [1, 2, 3; 4, 5, 6]");
166
+ // {"exprify":"DenseMatrix","data":[[1,2,3],[4,5,6]],"size":[2,3]}
167
+
153
168
  expr.evaluate("a[2, 3]");
154
169
  // 6
155
170
 
156
171
  expr.evaluate("a[1:2, 2]");
157
172
  // "2\n5"
173
+
174
+ expr.evaluate("a[3, 1:3] = [7, 8, 9]");
175
+ // "7\t8\t9"
176
+
177
+ expr.evaluate("det([-1, 2; 3, 1])");
178
+ // -7
158
179
  ```
159
180
 
160
- ### Complex numbers
181
+ ### Linear Algebra Helpers
161
182
 
162
183
  ```js
163
- expr.evaluate("9 / 3 + 2i");
164
- // "3 + 2i"
184
+ expr.evaluate("lup([[2, 1], [1, 4]])");
185
+ // {"L":{"exprify":"DenseMatrix",...},"U":{"exprify":"DenseMatrix",...},"p":[0,1]}
186
+
187
+ expr.evaluate("lyap([[-2, 0], [1, -4]], [[3, 1], [1, 3]])");
188
+ // {"exprify":"DenseMatrix","data":[[0.75,0.2916666666666667],[0.2916666666666667,0.44791666666666663]],"size":[2,2]}
189
+
190
+ expr.evaluate("qr([[1, -1, 4], [1, 4, -2], [1, 4, 2], [1, -1, 0]])");
191
+ // {"Q":{"exprify":"DenseMatrix",...},"R":{"exprify":"DenseMatrix",...}}
192
+
193
+ expr.evaluate("polynomialRoot(-6, 11, -6, 1)");
194
+ // [1,3,2]
165
195
  ```
166
196
 
167
- ### Algebra helpers
197
+ Available helpers currently include `det`, `lsolve`, `lup`, `lyap`, `qr`, and `polynomialRoot`.
198
+
199
+ ### Algebra Helpers
168
200
 
169
201
  ```js
170
202
  expr.evaluate('simplify("2x + x")');
@@ -172,8 +204,43 @@ expr.evaluate('simplify("2x + x")');
172
204
 
173
205
  expr.evaluate('derivative("2x^2 + 3x + 4", "x")');
174
206
  // "4 * x + 3"
207
+
208
+ expr.evaluate('rationalize("2x/y - y/(x+1)", true)');
209
+ // {"numerator":"2 * x ^ 2 + 2 * x - y ^ 2","denominator":"x * y + y","coefficients":[],"variables":["x","y"],"expression":"(2 * x ^ 2 + 2 * x - y ^ 2) / (x * y + y)"}
175
210
  ```
176
211
 
212
+ ### Parse and AST Utilities
213
+
214
+ ```js
215
+ expr.evaluate('leafCount("e^(i*pi)-1")');
216
+ // 4
217
+
218
+ expr.evaluate('leafCount(parse("{a: 22/7, b: 10^(1/2)}"))');
219
+ // 5
220
+ ```
221
+
222
+ ### Built-in Functions
223
+
224
+ Common built-ins include:
225
+
226
+ - `max`, `min`, `abs`, `round`, `floor`, `ceil`, `sqrt`, `pow`
227
+ - `sin`, `cos`, `tan`, `asin`, `acos`, `atan`
228
+ - `log`, `log10`, `exp`, `random`
229
+ - `clamp`, `if`, `length`, `typeof`
230
+ - `det`, `lsolve`, `lup`, `lyap`, `qr`, `polynomialRoot`
231
+ - `simplify`, `derivative`, `rationalize`, `leafCount`, `parse`
232
+
233
+ ## Return Types
234
+
235
+ Depending on the expression, `evaluate()` may return:
236
+
237
+ - numbers / bigint / booleans
238
+ - strings
239
+ - formatted unit strings like `"5.08 cm"`
240
+ - formatted complex strings like `"3 + 2i"`
241
+ - matrix wrapper JSON strings such as `{"exprify":"DenseMatrix",...}`
242
+ - JSON strings for structured helper outputs like `lup()` or `rationalize(..., true)`
243
+
177
244
  ## Manual Build
178
245
 
179
246
  ```bash
@@ -183,7 +250,7 @@ npm install
183
250
  npm run build
184
251
  ```
185
252
 
186
- Build output is generated in `dist/`.
253
+ Build output is written to `dist/`.
187
254
 
188
255
  ## Testing
189
256
 
@@ -200,4 +267,4 @@ Exprify is licensed under GPL-3.0. Copyright (c) [Nirmal Paul](https://github.co
200
267
  1. Fork the repository.
201
268
  2. Create a branch: `git checkout -b feature/your-feature`
202
269
  3. Commit your changes: `git commit -m "Add your feature"`
203
- 4. Push the branch and open a pull request.
270
+ 4. Push your branch and open a pull request.