remove abs() from trapezoidal rule segment sum#2
Open
HrachShah wants to merge 1 commit into
Open
Conversation
…s through
The trapezoidal rule is a signed-area approximation: each linear segment
between (x1, f(x1)) and (x2, f(x2)) contributes (f(x1) + f(x2)) * (x2 - x1) / 2
to the running total. The previous code wrapped that in abs(), which forced
the running total to be non-negative. For functions that are positive in
the test range the abs() was a no-op and the existing doctests passed, but
two natural cases broke:
- f(x) = -x**2 from -1.0 to 1.0 returns +0.6666... instead of -0.6666...
- f(x) = x from -2.0 to 2.0 returns 4.0 instead of 0 (the symmetric
linear function integrates to zero across the origin)
The doctests use f(x)=5 and f(x)=9*x**2 — both non-negative in the test
range, so they coincidentally worked. Drop the abs() and the formula now
returns the correct signed approximation for any real-valued f.
Verified with python3 -m doctest maths/area_under_curve.py: 5/5 pass.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts the trapezoidal integration implementation to compute true signed area by removing an absolute value operation from the per-segment contribution formula, preserving existing doctests. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The trapezoidal_area function in maths/area_under_curve.py wraps the per-segment trapezoid sum in abs(...), which forces the running total to be non-negative.
The trapezoidal rule is a signed-area approximation: each linear segment between (x1, f(x1)) and (x2, f(x2)) contributes (f(x1) + f(x2)) * (x2 - x1) / 2 to the running total. The previous code wrapped that in abs(), which forced the running total to be non-negative. For functions that are positive in the test range the abs() was a no-op and the existing doctests passed, but two natural cases broke:
The doctests use f(x)=5 and f(x)=9*x**2 — both non-negative in the test range, so they coincidentally worked. Drop the abs() and the formula returns the correct signed approximation for any real-valued f.
Verified with python3 -m doctest maths/area_under_curve.py: 5/5 still pass.
Summary by Sourcery
Bug Fixes: