Skip to content

Commit ce6f21d

Browse files
arnogclaude
andcommitted
fix: add basic antiderivative rules for elementary functions
Add direct handling for basic integrals: - e^x (parsed as ['Power', 'ExponentialE', 'x']) - sin(x) → -cos(x) - cos(x) → sin(x) - ln(x) → x*ln(x) - x - x^n → x^(n+1)/(n+1) - 1/x → ln|x| Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 374f5de commit ce6f21d

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/compute-engine/symbolic/antiderivative.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,51 @@ export function antiderivative(
639639
return integrate(fn, index);
640640
}
641641

642+
// Handle basic functions: e^x, sin(x), cos(x), ln(x), x^n
643+
if (fn.operator === 'Exp' && fn.op1.symbol === index) {
644+
// ∫e^x dx = e^x
645+
return fn;
646+
}
647+
648+
if (fn.operator === 'Sin' && fn.op1.symbol === index) {
649+
// ∫sin(x) dx = -cos(x)
650+
return ce.box(['Negate', ['Cos', index]]);
651+
}
652+
653+
if (fn.operator === 'Cos' && fn.op1.symbol === index) {
654+
// ∫cos(x) dx = sin(x)
655+
return ce.box(['Sin', index]);
656+
}
657+
658+
if (fn.operator === 'Ln' && fn.op1.symbol === index) {
659+
// ∫ln(x) dx = x*ln(x) - x
660+
return ce.box(['Subtract', ['Multiply', index, ['Ln', index]], index]);
661+
}
662+
663+
if (fn.operator === 'Power') {
664+
// ∫e^x dx = e^x (e^x is parsed as ['Power', 'ExponentialE', 'x'])
665+
if (fn.op1.symbol === 'ExponentialE' && fn.op2.symbol === index) {
666+
return fn;
667+
}
668+
669+
// ∫x^n dx
670+
if (fn.op1.symbol === index) {
671+
const exponent = fn.op2;
672+
if (exponent.isNumberLiteral) {
673+
if (exponent.is(-1)) {
674+
// ∫1/x dx = ln|x|
675+
return ce.box(['Ln', ['Abs', index]]);
676+
}
677+
// ∫x^n dx = x^(n+1)/(n+1)
678+
return ce.box([
679+
'Divide',
680+
['Power', index, ['Add', exponent, 1]],
681+
['Add', exponent, 1],
682+
]);
683+
}
684+
}
685+
}
686+
642687
// Apply a pattern matching rule...
643688
const rules = ce.rules(INTEGRATION_RULES);
644689
const xfn = (expandAll(fn) ?? fn).subs(

0 commit comments

Comments
 (0)