Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Conversation

sritchie
Copy link
Member

@sritchie sritchie commented Jan 4, 2022

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 as kbn),
      kahan-babushka-klein and and kbk-n macro for generating higher-order
      kahan-babushka-klein variants. generic-sum-fold folds using
      sicmutils.generic/+.

    • sicmutils.util.aggregate/kahan-fold now lives here, named kahan.

    • fold->sum-fn and fold->scan-fn generate functions like
      sicmutils.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 primitive
      folds.

  • Upgrades to sicmutils.util.aggregate:

    • scanning-sum renamed to scan

    • halt-at deleted in favor of the built-in halt-when that I didn't know
      about!

    • scan and sum now both use a dynamic binding, *fold*, to set the fold
      they 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 over
      the input range.

    • pairwise-sum implements pairwise summation, an error-limiting technique
      for summing vectors. Use the dynamic binding *cutoff* to set where
      pairwise-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 points
      are 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 and bulirsch-stoer-scan
      functions. These are similar to the modified-** versions but use the
      bulirsch-stoer algorithm, instead of modified-bulirsch-stoer.

    • modified-bulirsch-stoer-fold-fn renamed to
      modified-bulirsch-stoer-fold, to match the naming scheme of other
      "folds" in the library.

    • modified-bulirsch-stoer-fold renamed to modified-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 enable
    sicmutils.algebra.fold/kbk-n in the SCI environment (you'll need to turn
    on access to js/Math or java.lang.Math).

  • Fixed a type inference warning in Clojurescript in sicmutils.complex.

  • Added support for sicmutils.util.def and its fork macro to the default
    SCI environment provided by SICMUtils. Helpful for macro-writing!

  • sicmutils.numerical.quadrature.adaptive now uses the dynamically bound
    sicmutils.util.aggregate/*fold* to accumulate its numerical integral
    pieces, instead of a hardcoded kahan-sum.

  • sicmutils.numerical.quadrature.bulirsch-stoer now uses the functional scan
    versions of polynomial and rational function interpolation, as these are a
    bit faster than the originals!

  • sicmutils.util.stream/scan deleted in favor of
    sicmutils.util.aggregate/scan with a dynamic binding for *fold* to
    customize.

@sritchie sritchie added this to the 0.21.0 milestone Jan 4, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 4, 2022

Codecov Report

Merging #451 (39155db) into main (e778cde) will increase coverage by 0.11%.
The diff coverage is 97.40%.

❗ Current head 39155db differs from pull request most recent head 2b2bce7. Consider uploading reports for the commit 2b2bce7 to get more accurate results
Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
src/sicmutils/env.cljc 93.33% <ø> (ø)
src/sicmutils/polynomial/richardson.cljc 97.36% <ø> (ø)
src/sicmutils/util/stream.cljc 89.58% <ø> (-1.16%) ⬇️
src/sicmutils/env/sci/macros.cljc 83.78% <78.57%> (-1.07%) ⬇️
src/sicmutils/algebra/fold.cljc 97.16% <97.16%> (ø)
src/sicmutils/complex.cljc 93.63% <100.00%> (ø)
src/sicmutils/env/sci.cljc 96.77% <100.00%> (+0.10%) ⬆️
src/sicmutils/numerical/quadrature/adaptive.cljc 94.73% <100.00%> (ø)
...sicmutils/numerical/quadrature/bulirsch_stoer.cljc 97.29% <100.00%> (ø)
src/sicmutils/polynomial/gcd.cljc 93.83% <100.00%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e778cde...2b2bce7. Read the comment docs.

@sritchie sritchie changed the title kahan-babushka-neumaier-sum from GJS [in progress] New sicmutils.algebra.fold namespace, fold improvements all around Jan 12, 2022
@sritchie sritchie merged commit 88bcd4c into main Jan 12, 2022
@sritchie sritchie deleted the sritchie/new_aggregate branch January 12, 2022 23:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants