-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslang-basic.js
More file actions
766 lines (649 loc) · 20.2 KB
/
slang-basic.js
File metadata and controls
766 lines (649 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/**
* SLaNg (Saad Language for Analytical Numerics and Geometry) - Math Library
* Enhanced with full denominator support and improved calculus operations
*
*/
// ============================================================================
// DEEP COPY UTILITIES
// ============================================================================
/**
* Deep clone an equation to avoid mutation
*/
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
// ============================================================================
// TERM CREATION UTILITIES
// ============================================================================
/**
* Create a term with coefficient and optional variables
* @param {number} coeff - Coefficient
* @param {Object} vars - Variables object like {x: 2, y: 1}
*/
function createTerm(coeff, vars = {}) {
const term = { coeff };
if (Object.keys(vars).length > 0) {
term.var = { ...vars };
}
return term;
}
/**
* Create a fraction with polynomial numerator and denominator
* @param {Array} numiTerms - Array of terms for numerator
* @param {Array|number} denoTerms - Array of terms for denominator OR simple number
*
*: Full support for polynomial denominators
*/
function createFraction(numiTerms, denoTerms = 1) {
const fraction = {
numi: { terms: deepClone(numiTerms) }
};
// Support both old (number) and new (polynomial) denominators
if (typeof denoTerms === 'number') {
fraction.deno = denoTerms;
} else if (Array.isArray(denoTerms)) {
fraction.deno = { terms: deepClone(denoTerms) };
} else if (denoTerms.terms) {
fraction.deno = { terms: deepClone(denoTerms.terms) };
}
return fraction;
}
/**
* Check if denominator is a simple number
*/
function hasSimpleDenominator(fraction) {
return typeof fraction.deno === 'number';
}
/**
* Get GCD of two numbers for simplification
*/
function gcd(a, b) {
a = Math.abs(a);
b = Math.abs(b);
while (b) {
[a, b] = [b, a % b];
}
return a || 1;
}
// ============================================================================
// EVALUATION FUNCTIONS
// ============================================================================
/**
* Evaluate a term at given variable values
* @param {Object} term - A single term
* @param {Object} values - Variable values like {x: 2, y: 3}
*/
function evaluateTerm(term, values) {
let result = term.coeff;
if (term.var) {
for (let [variable, power] of Object.entries(term.var)) {
if (values[variable] === undefined) {
throw new Error(`Variable ${variable} not provided`);
}
result *= Math.pow(values[variable], power);
}
}
return result;
}
/**
* Evaluate polynomial (array of terms)
*/
function evaluatePolynomial(polynomial, values) {
let sum = 0;
for (let term of polynomial.terms) {
sum += evaluateTerm(term, values);
}
return sum;
}
/**
* Evaluate a fraction (numerator / denominator)
*: Supports polynomial denominators
*/
function evaluateFraction(fraction, values) {
const numeratorSum = evaluatePolynomial(fraction.numi, values);
if (hasSimpleDenominator(fraction)) {
return numeratorSum / fraction.deno;
} else {
const denominatorSum = evaluatePolynomial(fraction.deno, values);
return numeratorSum / denominatorSum;
}
}
/**
* Evaluate a product (array of fractions multiplied together)
*/
function evaluateProduct(product, values) {
let result = 1;
for (let fraction of product) {
result *= evaluateFraction(fraction, values);
}
return result;
}
/**
* Evaluate entire equation (sum of products)
*/
function evaluateEquation(equation, values) {
let result = 0;
for (let product of equation) {
result += evaluateProduct(product, values);
}
return result;
}
// ============================================================================
// INTEGRATION FUNCTIONS - ENHANCED
// ============================================================================
/**
* Integrate a single term with respect to a variable
* Power rule: ∫ c*x^n dx = c/(n+1) * x^(n+1)
*/
function integrateTerm(term, indvar) {
const newTerm = deepClone(term);
// Get current power of the variable (0 if not present)
const power = newTerm.var?.[indvar] ?? 0;
// Check for special case: x^(-1) -> ln|x| (not handled symbolically yet)
if (power === -1) {
throw new Error('Integration of 1/x requires logarithm (not yet implemented)');
}
// Apply power rule
newTerm.coeff = newTerm.coeff / (power + 1);
// Increment power
if (!newTerm.var) {
newTerm.var = {};
}
newTerm.var[indvar] = power + 1;
return newTerm;
}
/**
* Integrate a polynomial
*/
function integratePolynomial(polynomial, indvar) {
return {
terms: polynomial.terms.map(term => integrateTerm(term, indvar))
};
}
/**
* Integrate a fraction
*: Improved handling of different denominator types
*/
function integrateFraction(fraction, indvar) {
if (hasSimpleDenominator(fraction)) {
// Simple case: polynomial / constant
return {
numi: integratePolynomial(fraction.numi, indvar),
deno: fraction.deno
};
} else {
// Complex case: polynomial / polynomial
// This requires more advanced techniques
// Check if it's a simple substitution case
if (isSimpleSubstitutionCase(fraction, indvar)) {
return integrateBySubstitution(fraction, indvar);
}
// Otherwise, try partial fractions or numerical methods
throw new Error(
'Integration of complex rational functions requires partial fractions or numerical methods. ' +
'Use numericalIntegrateFraction() instead.'
);
}
}
/**
* Check if fraction can be integrated by simple substitution
* e.g., ∫ 2x/(x²+1) dx where numerator is derivative of denominator
*/
function isSimpleSubstitutionCase(fraction, indvar) {
if (hasSimpleDenominator(fraction)) return false;
// Differentiate denominator
const denoDeriv = differentiatePolynomial(fraction.deno, indvar);
// Check if numerator is a constant multiple of denominator derivative
if (fraction.numi.terms.length !== denoDeriv.terms.length) return false;
// More sophisticated check would go here
return false; // Conservative for now
}
/**
* Definite integration of a term
* Evaluates ∫[lower to upper] term dx
*/
function definiteIntegrateTerm(term, lower, upper, indvar) {
const integratedTerm = integrateTerm(term, indvar);
// Get power of integration variable in the integrated term
const intPower = integratedTerm.var?.[indvar] ?? 0;
// Calculate the coefficient multiplier from bounds
const upperValue = Math.pow(upper, intPower);
const lowerValue = Math.pow(lower, intPower);
const boundsDiff = upperValue - lowerValue;
// Create result term
const resultTerm = deepClone(integratedTerm);
resultTerm.coeff = resultTerm.coeff * boundsDiff;
// Remove the integration variable
if (resultTerm.var) {
delete resultTerm.var[indvar];
if (Object.keys(resultTerm.var).length === 0) {
delete resultTerm.var;
}
}
return resultTerm;
}
/**
* Definite integration of a polynomial
*/
function definiteIntegratePolynomial(polynomial, lower, upper, indvar) {
return {
terms: polynomial.terms.map(term =>
definiteIntegrateTerm(term, lower, upper, indvar)
)
};
}
/**
* Definite integration of a fraction
*: Better handling for different denominator types
*/
function definiteIntegrateFraction(fraction, lower, upper, indvar) {
if (hasSimpleDenominator(fraction)) {
// Simple case
return {
numi: definiteIntegratePolynomial(fraction.numi, lower, upper, indvar),
deno: fraction.deno
};
} else {
// For polynomial denominators, use numerical integration
return numericalIntegrateFraction(fraction, lower, upper, indvar);
}
}
/**
* Numerical integration using Simpson's rule (more accurate than rectangles)
*: Enhanced numerical integration
*/
function numericalIntegrateFraction(fraction, lower, upper, indvar, numSteps = 1000) {
// Simpson's rule requires even number of steps
if (numSteps % 2 !== 0) numSteps++;
const h = (upper - lower) / numSteps;
let sum = 0;
// Simpson's rule: h/3 * [f(x0) + 4*f(x1) + 2*f(x2) + 4*f(x3) + ... + f(xn)]
for (let i = 0; i <= numSteps; i++) {
const x = lower + i * h;
const point = { [indvar]: x };
const value = evaluateFraction(fraction, point);
if (i === 0 || i === numSteps) {
sum += value;
} else if (i % 2 === 1) {
sum += 4 * value;
} else {
sum += 2 * value;
}
}
const result = (h / 3) * sum;
// Return as a constant term
return {
numi: { terms: [createTerm(result)] },
deno: 1
};
}
// ============================================================================
// DIFFERENTIATION FUNCTIONS - ENHANCED
// ============================================================================
/**
* Differentiate a term with respect to a variable
* Power rule: d/dx(c*x^n) = c*n*x^(n-1)
*/
function differentiateTerm(term, indvar) {
const newTerm = deepClone(term);
// Get current power
const power = newTerm.var?.[indvar];
// If variable not present, derivative is 0
if (power === undefined) {
return createTerm(0);
}
// Apply power rule
newTerm.coeff = newTerm.coeff * power;
// Decrease power
if (power === 1) {
delete newTerm.var[indvar];
if (Object.keys(newTerm.var).length === 0) {
delete newTerm.var;
}
} else {
newTerm.var[indvar] = power - 1;
}
return newTerm;
}
/**
* Differentiate a polynomial
*/
function differentiatePolynomial(polynomial, indvar) {
return {
terms: polynomial.terms
.map(term => differentiateTerm(term, indvar))
.filter(term => term.coeff !== 0)
};
}
/**
* Differentiate a fraction using quotient rule
*: Full quotient rule for polynomial denominators
*
* d/dx[f/g] = (f'g - fg') / g²
*/
function differentiateFraction(fraction, indvar) {
if (hasSimpleDenominator(fraction)) {
// Simple case: just differentiate numerator, keep constant denominator
return {
numi: differentiatePolynomial(fraction.numi, indvar),
deno: fraction.deno
};
} else {
// Quotient rule for polynomial denominator
const f = fraction.numi;
const g = fraction.deno;
const fPrime = differentiatePolynomial(f, indvar);
const gPrime = differentiatePolynomial(g, indvar);
// f' * g
const fPrimeTimesG = multiplyPolynomials(fPrime, g);
// f * g'
const fTimesGPrime = multiplyPolynomials(f, gPrime);
// f'g - fg'
const numerator = subtractPolynomials(fPrimeTimesG, fTimesGPrime);
// g²
const denominator = multiplyPolynomials(g, g);
return {
numi: numerator,
deno: denominator
};
}
}
// ============================================================================
// POLYNOMIAL ARITHMETIC -
// ============================================================================
/**
* Add two polynomials
*/
function addPolynomials(poly1, poly2) {
return {
terms: [...poly1.terms, ...poly2.terms]
};
}
/**
* Subtract two polynomials
*/
function subtractPolynomials(poly1, poly2) {
const negatedPoly2 = {
terms: poly2.terms.map(t => ({ ...deepClone(t), coeff: -t.coeff }))
};
return addPolynomials(poly1, negatedPoly2);
}
/**
* Multiply two terms together
*/
function multiplyTerms(term1, term2) {
const result = {
coeff: term1.coeff * term2.coeff
};
// Combine variables (add exponents for same variable)
const vars = {};
if (term1.var) {
for (let [v, pow] of Object.entries(term1.var)) {
vars[v] = pow;
}
}
if (term2.var) {
for (let [v, pow] of Object.entries(term2.var)) {
vars[v] = (vars[v] || 0) + pow;
}
}
if (Object.keys(vars).length > 0) {
result.var = vars;
}
return result;
}
/**
* Multiply two polynomials
*: Essential for quotient rule
*/
function multiplyPolynomials(poly1, poly2) {
const resultTerms = [];
for (let term1 of poly1.terms) {
for (let term2 of poly2.terms) {
resultTerms.push(multiplyTerms(term1, term2));
}
}
return { terms: resultTerms };
}
// ============================================================================
// SIMPLIFICATION FUNCTIONS - ENHANCED
// ============================================================================
/**
* Combine like terms in a polynomial
*/
function simplifyPolynomial(polynomial) {
const termMap = new Map();
for (let term of polynomial.terms) {
const varKey = term.var ? JSON.stringify(term.var) : 'constant';
if (termMap.has(varKey)) {
termMap.get(varKey).coeff += term.coeff;
} else {
termMap.set(varKey, deepClone(term));
}
}
// Filter out zero terms and sort for consistency
const simplifiedTerms = Array.from(termMap.values())
.filter(term => Math.abs(term.coeff) > 1e-10)
.sort((a, b) => {
// Sort by total degree (descending)
const degreeA = a.var ? Object.values(a.var).reduce((sum, p) => sum + p, 0) : 0;
const degreeB = b.var ? Object.values(b.var).reduce((sum, p) => sum + p, 0) : 0;
return degreeB - degreeA;
});
return { terms: simplifiedTerms };
}
/**
* Simplify a fraction
*: Enhanced to handle polynomial denominators
*/
function simplifyFraction(fraction) {
const simplifiedNumi = simplifyPolynomial(fraction.numi);
if (hasSimpleDenominator(fraction)) {
// Try to simplify constant denominator with GCD
if (simplifiedNumi.terms.length > 0) {
const coeffs = simplifiedNumi.terms.map(t => t.coeff);
const numGCD = coeffs.reduce((a, b) => gcd(a, b));
const denoGCD = gcd(numGCD, fraction.deno);
if (denoGCD > 1) {
return {
numi: {
terms: simplifiedNumi.terms.map(t => ({
...deepClone(t),
coeff: t.coeff / denoGCD
}))
},
deno: fraction.deno / denoGCD
};
}
}
return {
numi: simplifiedNumi,
deno: fraction.deno
};
} else {
// Simplify both numerator and denominator
const simplifiedDeno = simplifyPolynomial(fraction.deno);
// TODO: Factor and cancel common factors
return {
numi: simplifiedNumi,
deno: simplifiedDeno
};
}
}
/**
* Simplify an entire product
*/
function simplifyProduct(product) {
return product.map(fraction => simplifyFraction(fraction));
}
/**
* Simplify entire equation
*/
function simplifyEquation(equation) {
return equation.map(product => simplifyProduct(product));
}
// ============================================================================
// EXPANSION FUNCTIONS
// ============================================================================
/**
* Expand product of two fractions (both with simple denominators)
* (a + b)(c + d) = ac + ad + bc + bd
*/
function expandFractions(frac1, frac2) {
const numi1 = frac1.numi;
const numi2 = frac2.numi;
const expandedNumi = multiplyPolynomials(numi1, numi2);
// Handle denominators
let newDeno;
if (hasSimpleDenominator(frac1) && hasSimpleDenominator(frac2)) {
newDeno = frac1.deno * frac2.deno;
} else if (hasSimpleDenominator(frac1) && !hasSimpleDenominator(frac2)) {
newDeno = multiplyPolynomialByConstant(frac2.deno, frac1.deno);
} else if (!hasSimpleDenominator(frac1) && hasSimpleDenominator(frac2)) {
newDeno = multiplyPolynomialByConstant(frac1.deno, frac2.deno);
} else {
newDeno = multiplyPolynomials(frac1.deno, frac2.deno);
}
return simplifyFraction({
numi: expandedNumi,
deno: newDeno
});
}
/**
* Multiply polynomial by constant
*/
function multiplyPolynomialByConstant(polynomial, constant) {
return {
terms: polynomial.terms.map(t => ({ ...deepClone(t), coeff: t.coeff * constant }))
};
}
/**
* Expand a product into a single fraction
*/
function expandProduct(product) {
if (product.length === 0) {
return createFraction([createTerm(1)]);
}
let result = product[0];
for (let i = 1; i < product.length; i++) {
result = expandFractions(result, product[i]);
}
return result;
}
// ============================================================================
// DISPLAY FUNCTIONS
// ============================================================================
/**
* Convert term to readable string
*/
function termToString(term) {
let str = '';
// Handle coefficient
if (term.coeff === 0) return '0';
if (Math.abs(term.coeff - 1) < 1e-10 && term.var) {
str = '';
} else if (Math.abs(term.coeff + 1) < 1e-10 && term.var) {
str = '-';
} else {
str = term.coeff.toString();
}
// Handle variables
if (term.var) {
const varStr = Object.entries(term.var)
.map(([variable, power]) => {
if (power === 1) return variable;
return `${variable}^${power}`;
})
.join('*');
str += (str && str !== '-' ? '*' : '') + varStr;
}
return str || term.coeff.toString();
}
/**
* Convert polynomial to readable string
*/
function polynomialToString(polynomial) {
if (!polynomial.terms || polynomial.terms.length === 0) return '0';
return polynomial.terms.map((term, i) => {
const termStr = termToString(term);
if (i === 0) return termStr;
if (term.coeff >= 0) return '+ ' + termStr;
return termStr; // Negative sign already in termStr
}).join(' ');
}
/**
* Convert fraction to readable string
*/
function fractionToString(fraction) {
const numi = polynomialToString(fraction.numi);
if (hasSimpleDenominator(fraction)) {
if (fraction.deno === 1) {
return `(${numi})`;
}
return `(${numi})/${fraction.deno}`;
} else {
const deno = polynomialToString(fraction.deno);
return `(${numi})/(${deno})`;
}
}
/**
* Convert product to readable string
*/
function productToString(product) {
return product.map(fractionToString).join(' * ');
}
/**
* Convert equation to readable string
*/
function equationToString(equation) {
return equation.map(productToString).join(' + ');
}
// ============================================================================
// EXPORTS
// ============================================================================
export {
// Utilities
deepClone,
createTerm,
createFraction,
hasSimpleDenominator,
gcd,
// Evaluation
evaluateTerm,
evaluatePolynomial,
evaluateFraction,
evaluateProduct,
evaluateEquation,
// Integration
integrateTerm,
integratePolynomial,
integrateFraction,
definiteIntegrateTerm,
definiteIntegratePolynomial,
definiteIntegrateFraction,
numericalIntegrateFraction,
// Differentiation
differentiateTerm,
differentiatePolynomial,
differentiateFraction,
// Polynomial Arithmetic
addPolynomials,
subtractPolynomials,
multiplyTerms,
multiplyPolynomials,
multiplyPolynomialByConstant,
// Simplification
simplifyPolynomial,
simplifyFraction,
simplifyProduct,
simplifyEquation,
// Expansion
expandFractions,
expandProduct,
// Display
termToString,
polynomialToString,
fractionToString,
productToString,
equationToString
};