-
Notifications
You must be signed in to change notification settings - Fork 30
Element Selection Syntax: Position #1183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ax3l
wants to merge
3
commits into
BLAST-ImpactX:development
Choose a base branch
from
ax3l:topic-lattice-select-position
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| def _kahan_babushka_core(values, return_cumulative=False): | ||
| """Core implementation of the second-order iterative Kahan-Babuska algorithm. | ||
| This is the unified core that implements Klein (2006) algorithm for both | ||
| regular summation and cumulative summation. | ||
| - https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements | ||
| - Klein (2006). "A generalized Kahan–Babuška-Summation-Algorithm". in | ||
| Computing. 76 (3–4). Springer-Verlag: 279–293. doi:10.1007/s00607-005-0139-x | ||
| Args: | ||
| values: Iterable of numeric values to sum | ||
| return_cumulative: If True, returns list of cumulative sums; if False, returns final sum | ||
| Returns: | ||
| float or list: Final sum if return_cumulative=False, list of cumulative sums if True | ||
| """ | ||
| sum_val = 0.0 | ||
| cs = 0.0 # first-order compensation for lost low-order bits | ||
| ccs = 0.0 # second-order compensation for further lost bits | ||
| c = 0.0 # temporary variable for first-order compensation | ||
| cc = 0.0 # temporary variable for second-order compensation | ||
Check warningCode scanning / CodeQL Variable defined multiple times Warning
This assignment to 'cc' is unnecessary as it is
redefined Error loading related location Loading This assignment to 'cc' is unnecessary as it is redefined Error loading related location Loading |
||
|
|
||
| if return_cumulative: | ||
| cumulative_sums = [0.0] # Start with 0.0 | ||
|
|
||
| for val in values: | ||
| # First-order Kahan-Babuška step | ||
| t = sum_val + val | ||
| if abs(sum_val) >= abs(val): | ||
| c = (sum_val - t) + val | ||
| else: | ||
| c = (val - t) + sum_val | ||
| sum_val = t | ||
|
|
||
| # Second-order compensation step | ||
| t = cs + c | ||
| if abs(cs) >= abs(c): | ||
| cc = (cs - t) + c | ||
| else: | ||
| cc = (c - t) + cs | ||
| cs = t | ||
| ccs += cc | ||
|
|
||
| if return_cumulative: | ||
| # Store the accurate cumulative sum | ||
| cumulative_sums.append(sum_val + cs + ccs) | ||
|
|
||
| if return_cumulative: | ||
| return cumulative_sums | ||
| else: | ||
| return sum_val + cs + ccs | ||
|
|
||
|
|
||
| def kahan_babushka_sum(values): | ||
| """Calculate an accurate sum using the second-order iterative Kahan-Babuška algorithm. | ||
| This implementation follows Klein (2006) to provide high-precision summation | ||
| that avoids floating-point precision errors when summing many small values. | ||
| - https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements | ||
| - Klein (2006). "A generalized Kahan–Babuška-Summation-Algorithm". in | ||
| Computing. 76 (3–4). Springer-Verlag: 279–293. doi:10.1007/s00607-005-0139-x | ||
| The algorithm uses second-order compensation for lost low-order bits during | ||
| floating-point addition, providing significantly better accuracy than naive | ||
| summation when dealing with large numbers of small values (e.g., many ds | ||
| values in a long lattice). | ||
| Args: | ||
| values: Iterable of numeric values to sum | ||
| Returns: | ||
| float: Accurate sum of all values | ||
| """ | ||
| return _kahan_babushka_core(values, return_cumulative=False) | ||
|
|
||
|
|
||
| def kahan_babushka_cumsum(values): | ||
| """Calculate an accurate cumulative sum using the second-order iterative Kahan-Babuska algorithm. | ||
| This implementation follows Klein (2006) to provide high-precision summation | ||
| that avoids floating-point precision errors when summing many small values. | ||
| - https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements | ||
| - Klein (2006). "A generalized Kahan–Babuška-Summation-Algorithm". in | ||
| Computing. 76 (3–4). Springer-Verlag: 279–293. doi:10.1007/s00607-005-0139-x | ||
| The algorithm uses second-order compensation for lost low-order bits during | ||
| floating-point addition, providing significantly better accuracy than naive | ||
| summation when dealing with large numbers of small values (e.g., many ds | ||
| values in a long lattice). | ||
| Args: | ||
| values: Iterable of numeric values to cumulatively sum | ||
| Returns: | ||
| list: List of cumulative sums with initial 0.0 prepended | ||
| """ | ||
| return _kahan_babushka_core(values, return_cumulative=True) | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Check warning
Code scanning / CodeQL
Variable defined multiple times Warning