[Interactive Graph] Add logarithm math utilities to kmath#3421
[Interactive Graph] Add logarithm math utilities to kmath#3421ivyolamit wants to merge 2 commits intoLEMS-3953/pr1-add-logarithm-type-definitionsfrom
Conversation
…th utilities to kmath for supporting Logarithm graph in Interactive Graph
…h utilities to kmath
🗄️ Schema Change: No Changes ✅ |
🛠️ Item Splitting: No Changes ✅ |
|
Size Change: +134 B (+0.03%) Total Size: 495 kB
ℹ️ View Unchanged
|
npm Snapshot: PublishedGood news!! We've packaged up the latest commit from this PR (48b121b) and published it to npm. You Example: pnpm add @khanacademy/perseus@PR3421If you are working in Khan Academy's frontend, you can run the below command. ./dev/tools/bump_perseus_version.ts -t PR3421If you are working in Khan Academy's webapp, you can run the below command. ./dev/tools/bump_perseus_version.js -t PR3421 |
|
@claude review |
There was a problem hiding this comment.
LGTM — clean, well-tested math utility following the existing exponential pattern.
Extended reasoning...
Overview
Adds LogarithmCoefficient type and getLogarithmCoefficients() to packages/kmath/src/coefficients.ts, exports the type in index.ts, and adds a changeset file. 4 files changed, all additive.
Mathematical correctness
The inverse-exponential derivation is sound. For f(x) = a·ln(b·x + c), flipping coordinates transforms the problem into fitting an exponential x = aExp·e^(bExp·y) + cExp where cExp = asymptote. Inverting gives a = 1/bExp, b = 1/aExp, c = -cExp/aExp — all verified algebraically. The validation guards (same-y, point on asymptote, opposite sides, non-finite/zero intermediates) correctly cover degenerate cases, including the extra bExp === 0 guard needed because a = 1/bExp.
Security risks
None. This is a pure math utility with no I/O or user-input handling.
Level of scrutiny
Low. The change is additive-only, follows an established pattern (getExponentialCoefficients), and all checks passed (tsc, lint, prettier, knip). The 6 new tests exercise both happy paths and all four error/invalid-input cases.
Other factors
No CODEOWNERS concerns for this kmath utility file. Bundle impact is +134 B, as expected.
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.
Tip: disable this comment in your organization's Code Review settings.
SonicScrewdriver
left a comment
There was a problem hiding this comment.
Looks great to me! The graph appears to work well when I manually test as well
Summary:
PR series to add logarithm graph support to the Interactive Graph widget:
Add the logarithm math utilities to kmath for supporting Logarithm graph in Interactive Graph
LogarithmCoefficienttype andgetLogarithmCoefficients()to kmath, following the exponential pattern{a, b, c}forf(x) = a·ln(b·x + c)using the inverse exponential approachDetails
This adds the shared math utility for logarithm coefficient computation to
@khanacademy/kmath, following the same pattern asgetExponentialCoefficients(). Both the rendering component (PR 4) and scoring (PR 5) will consume this function.Mathematical approach (inverse exponential):
(x, y) → (y, x)— treating the logarithm as the inverse of an exponentialccoefficientaExp,bExpfrom the flipped pointsa = 1/bExp,b = 1/aExp,c = -cExp/aExpValidation guards (returns
undefinedfor invalid inputs):bExpundefined)This matches the reference implementation in
packages/perseus-core/src/utils/grapher-util.ts(the Grapher widget'sLogarithmobject, lines 449–558).Co-Authored by Claude Code (Opus)
Issue: LEMS-3953
Test plan
pnpm tscpassespnpm knippassespnpm lintpassespnpm prettier . --checkpasses[-4,-3],[-5,-7], asymptote-6reproduces correct y-values[1,0],[e,1], asymptote0→a≈1, b≈1, c≈0y = ln(-x)) →b≈-1undefinedundefinedundefined