Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/compute-engine/boxed-expression/simplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,12 @@ function simplifyOperands(
simplifiedOps.push(full(x, i));
// Simplify Ln/Log operands within Add/Multiply to enable term cancellation
// (e.g., ln(x^3) -> 3*ln(x) so that ln(x^3) - 3*ln(x) = 0)
// Only simplify Ln (natural log), not Log (which may lose base info)
else if (x.operator === 'Ln') simplifiedOps.push(full(x, i));
// Log with base e is safe to simplify: log_e(x) -> ln(x)
else if (
x.operator === 'Ln' ||
(isFunction(x, 'Log') && x.op2 && isSymbol(x.op2, 'ExponentialE'))
)
simplifiedOps.push(full(x, i));
// Simplify Abs operands to enable cancellation
// (e.g., |xy| -> |x||y| so that |xy| - |x||y| = 0)
// Also handle Negate(Abs(...)) which appears in subtraction expressions
Expand Down
52 changes: 37 additions & 15 deletions src/compute-engine/symbolic/simplify-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
import { toBigint } from '../boxed-expression/numerics.js';
import { logarithmAtExceptionalPoint } from '../boxed-expression/logarithm.js';

/** Whether `logBase` is Euler's number — `Log(x, e)` is `Ln(x)`. */
function isNaturalLogBase(base: Expression): boolean {
return sym(base) === 'ExponentialE';
}

/**
* Logarithm simplification rules consolidated from simplify-rules.ts.
* Handles ~30 patterns for simplifying Ln and Log expressions.
Expand Down Expand Up @@ -242,6 +247,11 @@ function simplifyLogCore(x: Expression): RuleStep | undefined {
return { value: special, because: 'log at an exceptional point' };
}

// log_e(x) -> ln(x): base-e logarithm is the natural logarithm
if (isNaturalLogBase(logBase)) {
return { value: ce._fn('Ln', [arg]), because: 'log_e(x) -> ln(x)' };
}

// log_c(c) -> 1 (an infinite base was answered above: `∞/∞` is NaN)
if (arg.isSame(logBase) && logBase.isInfinity !== true) {
return { value: ce.One, because: 'log_c(c) -> 1' };
Expand Down Expand Up @@ -659,27 +669,39 @@ function simplifyLogCore(x: Expression): RuleStep | undefined {
innerTerm.op1 &&
innerTerm.op2
) {
const baseKey = JSON.stringify(innerTerm.op2.json);
if (!logTerms.has(baseKey)) {
logTerms.set(baseKey, []);
if (isNaturalLogBase(innerTerm.op2)) {
lnTerms.push({
index: i,
arg: innerTerm.op1,
positive: false,
});
} else {
const baseKey = JSON.stringify(innerTerm.op2.json);
if (!logTerms.has(baseKey)) {
logTerms.set(baseKey, []);
}
logTerms.get(baseKey)!.push({
index: i,
arg: innerTerm.op1,
base: innerTerm.op2,
positive: false,
});
}
logTerms.get(baseKey)!.push({
index: i,
arg: innerTerm.op1,
base: innerTerm.op2,
positive: false,
});
}
}
// Direct Log term
else if (isFunction(term, 'Log') && term.op1 && term.op2) {
const baseKey = JSON.stringify(term.op2.json);
if (!logTerms.has(baseKey)) {
logTerms.set(baseKey, []);
if (isNaturalLogBase(term.op2)) {
lnTerms.push({ index: i, arg: term.op1, positive: true });
} else {
const baseKey = JSON.stringify(term.op2.json);
if (!logTerms.has(baseKey)) {
logTerms.set(baseKey, []);
}
logTerms
.get(baseKey)!
.push({ index: i, arg: term.op1, base: term.op2, positive: true });
}
logTerms
.get(baseKey)!
.push({ index: i, arg: term.op1, base: term.op2, positive: true });
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/compute-engine/simplify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,24 @@ describe('LOGARITHM COMBINATION RULES', () => {
expect(simplify('\\ln(x) + \\ln(y) + z')).toMatchInlineSnapshot(
`["Add", "z", ["Ln", ["Multiply", "x", "y"]]]`
));

test('log_e(x) -> ln(x)', () =>
expect(simplify('\\log_e(x)')).toMatchInlineSnapshot(`["Ln", "x"]`));

test('log_e(x) + log_e(x) = 2*ln(x)', () =>
expect(simplify('\\log_e(x) + \\log_e(x)')).toMatchInlineSnapshot(
`["Multiply", 2, ["Ln", "x"]]`
));

test('log_e(x) + log_e(y) = ln(xy)', () =>
expect(simplify('\\log_e(x) + \\log_e(y)')).toMatchInlineSnapshot(
`["Ln", ["Multiply", "x", "y"]]`
));

test('ln(x) + log_e(y) = ln(xy)', () =>
expect(simplify('\\ln(x) + \\log_e(y)')).toMatchInlineSnapshot(
`["Ln", ["Multiply", "x", "y"]]`
));
});

describe('INDETERMINATE FORMS', () => {
Expand Down
Loading