slangmath 1.0.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/ARCHITECTURE.MD +366 -0
- package/LICENSE +21 -0
- package/README.md +953 -0
- package/package.json +23 -0
- package/slang-advanced.js +559 -0
- package/slang-basic.js +766 -0
- package/slang-cache.js +519 -0
- package/slang-convertor.js +652 -0
- package/slang-errors.js +454 -0
- package/slang-extended.js +501 -0
- package/slang-helpers.js +284 -0
- package/slang-math.js +3 -0
package/ARCHITECTURE.MD
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
# SLaNg Math Library - Architecture & Flow
|
|
2
|
+
|
|
3
|
+
## π Library Architecture
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
7
|
+
β YOUR APPLICATION β
|
|
8
|
+
β (Your Exercise 15.1, etc.) β
|
|
9
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
10
|
+
β
|
|
11
|
+
β imports
|
|
12
|
+
βΌ
|
|
13
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
14
|
+
β HELPER LAYER β
|
|
15
|
+
β (slang-helpers.js) β
|
|
16
|
+
β β
|
|
17
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
18
|
+
β β polynomial β β sum β β integralValueβ β
|
|
19
|
+
β β Easy create β β Easy create β β One-liner β β
|
|
20
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
21
|
+
β β
|
|
22
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
23
|
+
β βvolumeUnder β β areaOfRegion β βverification β β
|
|
24
|
+
β β Surface β β β β tools β β
|
|
25
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
26
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
27
|
+
β
|
|
28
|
+
β uses
|
|
29
|
+
βΌ
|
|
30
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
31
|
+
β CORE LAYER β
|
|
32
|
+
β (slang-math.js) β
|
|
33
|
+
β β
|
|
34
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
35
|
+
β β createTerm β β integrate β β differentiateβ β
|
|
36
|
+
β β createFrac β β Fraction β β Fraction β β
|
|
37
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
38
|
+
β β
|
|
39
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
40
|
+
β β evaluate β β expand β β simplify β β
|
|
41
|
+
β β Equation β β Product β β Fraction β β
|
|
42
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
43
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
44
|
+
β
|
|
45
|
+
β uses
|
|
46
|
+
βΌ
|
|
47
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
48
|
+
β ADVANCED LAYER β
|
|
49
|
+
β (slang-advanced.js) β
|
|
50
|
+
β β
|
|
51
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
52
|
+
β β product β β quotient β β integration β β
|
|
53
|
+
β β Rule β β Rule β β by Parts β β
|
|
54
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
55
|
+
β β
|
|
56
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
57
|
+
β β Taylor β β critical β β gradient β β
|
|
58
|
+
β β Series β β Points β β & directionalβ β
|
|
59
|
+
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
|
|
60
|
+
β β
|
|
61
|
+
β ββββββββββββββββ ββββββββββββββββ β
|
|
62
|
+
β β arc β β surface β β
|
|
63
|
+
β β Length β β Area β β
|
|
64
|
+
β ββββββββββββββββ ββββββββββββββββ β
|
|
65
|
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## π Data Flow Example
|
|
69
|
+
|
|
70
|
+
### Example: Computing β«βΒ² β«βΒ³ xy dy dx
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
USER CODE:
|
|
74
|
+
integralValue(xy, {x: [0,2], y: [0,3]})
|
|
75
|
+
β
|
|
76
|
+
βΌ
|
|
77
|
+
HELPER LAYER (slang-helpers.js):
|
|
78
|
+
integralOverRegion(xy, bounds)
|
|
79
|
+
β
|
|
80
|
+
ββ Loop through variables
|
|
81
|
+
β β
|
|
82
|
+
β ββ For y: definiteIntegrateFraction(xy, 0, 3, 'y')
|
|
83
|
+
β β β
|
|
84
|
+
β β βΌ
|
|
85
|
+
β β CORE LAYER (slang-math.js):
|
|
86
|
+
β β definiteIntegrateTerm for each term
|
|
87
|
+
β β β
|
|
88
|
+
β β ββ integrateTerm(term, 'y') // xy β xyΒ²/2
|
|
89
|
+
β β β β
|
|
90
|
+
β β β βΌ
|
|
91
|
+
β β β Power rule: n β n+1, coeff/(n+1)
|
|
92
|
+
β β β
|
|
93
|
+
β β ββ Evaluate at bounds
|
|
94
|
+
β β β Upper: y=3 β coefficient * 3Β²
|
|
95
|
+
β β β Lower: y=0 β coefficient * 0Β²
|
|
96
|
+
β β β Difference: gets multiplied into coefficient
|
|
97
|
+
β β β
|
|
98
|
+
β β ββ Remove 'y' variable, keep 'x'
|
|
99
|
+
β β
|
|
100
|
+
β β Result: 4.5x
|
|
101
|
+
β β
|
|
102
|
+
β ββ For x: definiteIntegrateFraction(4.5x, 0, 2, 'x')
|
|
103
|
+
β β
|
|
104
|
+
β βΌ
|
|
105
|
+
β CORE LAYER:
|
|
106
|
+
β Similar process
|
|
107
|
+
β β
|
|
108
|
+
β ββ Result: 9
|
|
109
|
+
β
|
|
110
|
+
ββ simplifyFraction(result)
|
|
111
|
+
β
|
|
112
|
+
βΌ
|
|
113
|
+
evaluateEquation([[result]], {})
|
|
114
|
+
β
|
|
115
|
+
βΌ
|
|
116
|
+
RETURN: 9
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## ποΈ SLaNg Notation Structure
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
EQUATION (Top level - Array)
|
|
123
|
+
β
|
|
124
|
+
ββ PRODUCT 1 (Array) βββββββββββββββββ
|
|
125
|
+
β β β
|
|
126
|
+
β ββ FRACTION 1 β Products are summed
|
|
127
|
+
β β ββ numerator (numi) β (a)(b) + (c)(d) + ...
|
|
128
|
+
β β β ββ terms (Array) β
|
|
129
|
+
β β β ββ TERM 1 ββββ β
|
|
130
|
+
β β β β ββ coeff β β
|
|
131
|
+
β β β β ββ var {} β β
|
|
132
|
+
β β β β Terms β
|
|
133
|
+
β β β ββ TERM 2 are added β
|
|
134
|
+
β β β β ββ coeff β β
|
|
135
|
+
β β β β ββ var {} β β
|
|
136
|
+
β β β β βΌ β
|
|
137
|
+
β β β ββ TERM 3 Sum β
|
|
138
|
+
β β β ββ coeff β
|
|
139
|
+
β β β ββ var {} β
|
|
140
|
+
β β β β
|
|
141
|
+
β β ββ denominator (deno) β
|
|
142
|
+
β β ββ number β
|
|
143
|
+
β β β
|
|
144
|
+
β ββ FRACTION 2 βββ β
|
|
145
|
+
β ββ numi β Fractions β
|
|
146
|
+
β ββ deno β are multiplied β
|
|
147
|
+
β βΌ β
|
|
148
|
+
β Product β
|
|
149
|
+
β β
|
|
150
|
+
ββ PRODUCT 2 ββββββββββββββββββββββββββ€
|
|
151
|
+
β ββ ... β
|
|
152
|
+
β β
|
|
153
|
+
ββ PRODUCT N ββββββββββββββββββββββββββ
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## π― Operation Flow Diagrams
|
|
157
|
+
|
|
158
|
+
### Integration Flow
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
INPUT: Expression + bounds + variable
|
|
162
|
+
β
|
|
163
|
+
βΌ
|
|
164
|
+
βββββββββββββββββββ
|
|
165
|
+
β Deep Clone β (Prevent mutation)
|
|
166
|
+
βββββββββββββββββββ
|
|
167
|
+
β
|
|
168
|
+
βΌ
|
|
169
|
+
βββββββββββββββββββ
|
|
170
|
+
β For each term: β
|
|
171
|
+
β Apply power β
|
|
172
|
+
β rule β
|
|
173
|
+
βββββββββββββββββββ
|
|
174
|
+
β
|
|
175
|
+
βΌ
|
|
176
|
+
βββββββββββββββββββ
|
|
177
|
+
β Evaluate at β
|
|
178
|
+
β upper bound β
|
|
179
|
+
βββββββββββββββββββ
|
|
180
|
+
β
|
|
181
|
+
βΌ
|
|
182
|
+
βββββββββββββββββββ
|
|
183
|
+
β Evaluate at β
|
|
184
|
+
β lower bound β
|
|
185
|
+
βββββββββββββββββββ
|
|
186
|
+
β
|
|
187
|
+
βΌ
|
|
188
|
+
βββββββββββββββββββ
|
|
189
|
+
β Subtract: β
|
|
190
|
+
β upper - lower β
|
|
191
|
+
βββββββββββββββββββ
|
|
192
|
+
β
|
|
193
|
+
βΌ
|
|
194
|
+
βββββββββββββββββββ
|
|
195
|
+
β Remove variable β
|
|
196
|
+
β from term β
|
|
197
|
+
βββββββββββββββββββ
|
|
198
|
+
β
|
|
199
|
+
βΌ
|
|
200
|
+
OUTPUT: Result
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Differentiation Flow
|
|
204
|
+
|
|
205
|
+
```
|
|
206
|
+
INPUT: Expression + variable
|
|
207
|
+
β
|
|
208
|
+
βΌ
|
|
209
|
+
βββββββββββββββββββ
|
|
210
|
+
β Deep Clone β
|
|
211
|
+
βββββββββββββββββββ
|
|
212
|
+
β
|
|
213
|
+
βΌ
|
|
214
|
+
βββββββββββββββββββ
|
|
215
|
+
β For each term: β
|
|
216
|
+
β β
|
|
217
|
+
β If var absent: β
|
|
218
|
+
β return 0 β
|
|
219
|
+
β β
|
|
220
|
+
β If var present: β
|
|
221
|
+
β coeff *= pow β
|
|
222
|
+
β pow -= 1 β
|
|
223
|
+
βββββββββββββββββββ
|
|
224
|
+
β
|
|
225
|
+
βΌ
|
|
226
|
+
βββββββββββββββββββ
|
|
227
|
+
β Simplify: β
|
|
228
|
+
β Remove zeros β
|
|
229
|
+
β Combine terms β
|
|
230
|
+
βββββββββββββββββββ
|
|
231
|
+
β
|
|
232
|
+
βΌ
|
|
233
|
+
OUTPUT: Result
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Expansion Flow (FOIL)
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
INPUT: (a + b)(c + d)
|
|
240
|
+
β
|
|
241
|
+
βΌ
|
|
242
|
+
βββββββββββββββββββ
|
|
243
|
+
β Nested loop: β
|
|
244
|
+
β β
|
|
245
|
+
β For each term β
|
|
246
|
+
β in first () β
|
|
247
|
+
β For each β
|
|
248
|
+
β term in β
|
|
249
|
+
β second () β
|
|
250
|
+
β Multiply β
|
|
251
|
+
β terms β
|
|
252
|
+
βββββββββββββββββββ
|
|
253
|
+
β
|
|
254
|
+
βΌ
|
|
255
|
+
βββββββββββββββββββ
|
|
256
|
+
β Collect all β
|
|
257
|
+
β products: β
|
|
258
|
+
β a*c β
|
|
259
|
+
β a*d β
|
|
260
|
+
β b*c β
|
|
261
|
+
β b*d β
|
|
262
|
+
βββββββββββββββββββ
|
|
263
|
+
β
|
|
264
|
+
βΌ
|
|
265
|
+
βββββββββββββββββββ
|
|
266
|
+
β Combine like β
|
|
267
|
+
β terms β
|
|
268
|
+
βββββββββββββββββββ
|
|
269
|
+
β
|
|
270
|
+
βΌ
|
|
271
|
+
OUTPUT: ac + ad + bc + bd
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## π Complexity Analysis
|
|
275
|
+
|
|
276
|
+
### Time Complexity
|
|
277
|
+
|
|
278
|
+
| Operation | Complexity | Notes |
|
|
279
|
+
|-----------|------------|-------|
|
|
280
|
+
| Create term | O(1) | Just object creation |
|
|
281
|
+
| Evaluate term | O(v) | v = number of variables |
|
|
282
|
+
| Differentiate | O(n) | n = number of terms |
|
|
283
|
+
| Integrate | O(n) | Linear in terms |
|
|
284
|
+
| Expand (a+b)(c+d) | O(nm) | n, m = terms in each |
|
|
285
|
+
| Simplify | O(nΒ²) | Comparing all pairs |
|
|
286
|
+
| Definite integral | O(n) | Plus evaluation |
|
|
287
|
+
|
|
288
|
+
### Space Complexity
|
|
289
|
+
|
|
290
|
+
| Operation | Complexity | Notes |
|
|
291
|
+
|-----------|------------|-------|
|
|
292
|
+
| Store expression | O(n) | n = number of terms |
|
|
293
|
+
| Deep clone | O(n) | Duplicate all data |
|
|
294
|
+
| Expansion | O(nm) | New terms created |
|
|
295
|
+
|
|
296
|
+
## π Design Patterns Used
|
|
297
|
+
|
|
298
|
+
### 1. **Builder Pattern**
|
|
299
|
+
```javascript
|
|
300
|
+
polynomial([1, -2, 1], 'x') // Builds complex structure simply
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### 2. **Strategy Pattern**
|
|
304
|
+
```javascript
|
|
305
|
+
// Different integration strategies
|
|
306
|
+
definiteIntegrateFraction() // Numerical bounds
|
|
307
|
+
integrateFraction() // Symbolic
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### 3. **Visitor Pattern**
|
|
311
|
+
```javascript
|
|
312
|
+
// Operations "visit" each term
|
|
313
|
+
terms.map(term => differentiateTerm(term, var))
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### 4. **Immutability Pattern**
|
|
317
|
+
```javascript
|
|
318
|
+
deepClone(obj) // Always work on copies
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## π¨ Code Organization Principles
|
|
322
|
+
|
|
323
|
+
1. **Separation of Concerns**
|
|
324
|
+
- Core: Basic operations
|
|
325
|
+
- Helpers: Convenience functions
|
|
326
|
+
- Advanced: Complex algorithms
|
|
327
|
+
|
|
328
|
+
2. **Single Responsibility**
|
|
329
|
+
- Each function does ONE thing
|
|
330
|
+
- Clear, focused purpose
|
|
331
|
+
|
|
332
|
+
3. **DRY (Don't Repeat Yourself)**
|
|
333
|
+
- `deepClone` used everywhere
|
|
334
|
+
- `evaluateEquation` reused
|
|
335
|
+
- Helper builders reduce duplication
|
|
336
|
+
|
|
337
|
+
4. **Progressive Enhancement**
|
|
338
|
+
- Start with core
|
|
339
|
+
- Add helpers
|
|
340
|
+
- Layer advanced features
|
|
341
|
+
|
|
342
|
+
## π Performance Tips
|
|
343
|
+
|
|
344
|
+
1. **Use helpers**: They're optimized
|
|
345
|
+
2. **Simplify often**: Reduces term count
|
|
346
|
+
3. **Clone wisely**: Only when needed (but library does this for you)
|
|
347
|
+
4. **Cache results**: Don't recompute same integrals
|
|
348
|
+
5. **Increase steps carefully**: More steps = slower but more accurate
|
|
349
|
+
|
|
350
|
+
## π Scalability
|
|
351
|
+
|
|
352
|
+
The library scales well for:
|
|
353
|
+
- β Polynomials up to degree ~20
|
|
354
|
+
- β ~100 terms per expression
|
|
355
|
+
- β Double/triple integrals
|
|
356
|
+
- β Functions of 2-3 variables
|
|
357
|
+
|
|
358
|
+
For larger problems:
|
|
359
|
+
- Consider breaking into smaller parts
|
|
360
|
+
- Use numerical methods
|
|
361
|
+
- Cache intermediate results
|
|
362
|
+
|
|
363
|
+
---
|
|
364
|
+
|
|
365
|
+
This architecture provides a solid foundation for symbolic mathematics
|
|
366
|
+
while remaining simple enough to understand and extend!
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Muhammad Saad Amin - @SENODROOM
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|