This repository was archived by the owner on Jun 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 69
New sicmutils.algebra.fold namespace, fold improvements all around #451
Merged
Conversation
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
Codecov Report
@@ Coverage Diff @@
## main #451 +/- ##
==========================================
+ Coverage 85.54% 85.66% +0.11%
==========================================
Files 97 98 +1
Lines 13071 13204 +133
Branches 709 712 +3
==========================================
+ Hits 11182 11311 +129
- Misses 1180 1181 +1
- Partials 709 712 +3
Continue to review full report at Codecov.
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
This PR started as a simple PR to implement kahan-babushka-neumaier summation, described here: https://en.wikipedia.org/wiki/Kahan_summation_algorithm
But of course I had some realizations about how useful folds could be, and went a little bonkers.
From the CHANGELOG:
new
sicmutils.algebra.fold
namespace:New folds:
kahan-babushka-neumaier
(aliased askbn
),kahan-babushka-klein
and andkbk-n
macro for generating higher-orderkahan-babushka-klein
variants.generic-sum-fold
folds usingsicmutils.generic/+
.sicmutils.util.aggregate/kahan-fold
now lives here, namedkahan
.fold->sum-fn
andfold->scan-fn
generate functions likesicmutils.util.aggregate.{sum,scan}
specialized to the supplied fold.See the docstrings for the multiple arities supported
fold primitives:
count
,constant
,min
,max
.fold combinator
join
allows compound folds to be built out of primitivefolds.
Upgrades to
sicmutils.util.aggregate
:scanning-sum
renamed toscan
halt-at
deleted in favor of the built-inhalt-when
that I didn't knowabout!
scan
andsum
now both use a dynamic binding,*fold*
, to set the foldthey use for aggregation. By default, this is set to the new
kahan-babushka-neumaier-fold
.The three-arity version of
sum
now uses transducers, saving a pass overthe input range.
pairwise-sum
implements pairwise summation, an error-limiting techniquefor summing vectors. Use the dynamic binding
*cutoff*
to set wherepairwise-sum
bails out to normal summation.Upgrades to
sicmutils.rational-function.polynomial
:The folds in this namespace now follow the fold contract laid out in
sicmutils.algebra.fold
, implementing all three arities correctly.I realized that the fold implementation here should /not/ return a full
row every time it processes a previous row; a far better
present
implementation would return the best estimate so far. Then you could build
a
scan
from that fold to see the estimates evolve lazily as new pointsare added. This has better performance, it turns out, than the original
method!
added a bunch to the exposition to make the advantages clear.
Upgrades to
sicmutils.rational-function.interpolate
:fold
interface upgraded, similar to the polynomial interpolation notes.New
bulirsch-stoer-fold
,bulirsch-stoer-sum
andbulirsch-stoer-scan
functions. These are similar to the
modified-**
versions but use thebulirsch-stoer
algorithm, instead ofmodified-bulirsch-stoer
.modified-bulirsch-stoer-fold-fn
renamed tomodified-bulirsch-stoer-fold
, to match the naming scheme of other"folds" in the library.
modified-bulirsch-stoer-fold
renamed tomodified-bulirsch-stoer-sum
,to match the convention that "reducing a sequence with a fold" is called
"summing" the sequence. I can see this changing down the road...
See
context-opts
for instructions on how to enablesicmutils.algebra.fold/kbk-n
in the SCI environment (you'll need to turnon access to
js/Math
orjava.lang.Math
).Fixed a type inference warning in Clojurescript in
sicmutils.complex
.Added support for
sicmutils.util.def
and itsfork
macro to the defaultSCI environment provided by SICMUtils. Helpful for macro-writing!
sicmutils.numerical.quadrature.adaptive
now uses the dynamically boundsicmutils.util.aggregate/*fold*
to accumulate its numerical integralpieces, instead of a hardcoded
kahan-sum
.sicmutils.numerical.quadrature.bulirsch-stoer
now uses the functional scanversions of polynomial and rational function interpolation, as these are a
bit faster than the originals!
sicmutils.util.stream/scan
deleted in favor ofsicmutils.util.aggregate/scan
with a dynamic binding for*fold*
tocustomize.