10000 Upgrade polynomial + rational function representation, gcd namespace by sritchie · Pull Request #341 · sicmutils/sicmutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Upgrade polynomial + rational function representation, gcd namespace #341

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

Merged
merged 110 commits into from
May 27, 2021

Conversation

sritchie
Copy link
Member
@sritchie sritchie commented Mar 30, 2021

From the CHANGELOG:

  • Upgrade polynomial + rational function representation, gcd namespace #341 takes on a large rewrite of the rational function and polynomial
    simplfiers. One goal of this project was to improve the performance of the
    Bianchi Identities in sicmutils.fdg.bianchi-test, and I'm happy to say that
    they are now a good bit faster than the original scmutils implementation.

    sicmutils.polynomial and sicmutils.rational-function are now solid data
    structures of their own, with many operations installed into the generic
    system. These are now valuable and useful outside of their role in the
    simplifier.

    This was a large project, and many small improvements and bugfixes snuck in.
    Here is the full list:

    • v/kind now works for sorted-map instances.

    • GCD in Clojurescript is now fast and efficient between all combinations of
      js/BigInt and js/Number, and in Clojure between all combinations of
      clojure.lang.BigInt, BigInteger, Long and Integer.

    • on the JVM, GCD now works properly with rational numbers. Previously
      anything non-integral would return 1; now (gcd 1/2 1/3) properly returns
      1/6.

    • g/exact-divide now succeeds for all non-exact ::v/scalar types (symbols,
      floats, etc) either if the denominator is zero, or if the two arguments are
      equal. Else, it throws, just like before.

    • A multi-arity call to sicmutils.generic/* now stops if it encounters a
      0, rather than attempting to multiply all remaining items by 0.

    • The default function for sicmutils.generic/lcm protects against overflow
      by dividing only a single one of its arguments a and b by (gcd a b).

    • (g/lcm 0 0) now properly returns 0.

    • New sicmutils.util.aggregate/{monoid,group} functions let you build
      multi-arity aggregations out of binary combination functions, with an option
      to bail early at "annihilator" values, like 0 for multiplication.

    • New multi-arity lcm and gcd implementations for symbolic expressions
      appropriately handle 0 and 1 on either side, as well as the case where
      both arguments are equal.

    • In the sicmutils.numsymb namespace, thanks to monoid and group, the
      '*, '/, '-, '+, 'or, 'and, 'gcd, 'lcm and '= operations
      now have efficient multi-arity implementations that stop computing when they
      receive an annihilator, like 0 for multiplication or true for or.
      Access these via (sicmutils.numsymb/symbolic-operator <symbol>).

    • sicmutils.series/PowerSeries gains arg-scale and arg-shift functions;
      these are identical to sicmutils.function/arg-{scale,shift}, but preserve
      the PowerSeries type. (#367 proposes making these functions generic.)

    • New sicmutils.ratio/IRational protocol, with numerator and denominator
      functions implemented for ratios and for the RationalFunction data type.
      These two are now exposed in sicmutils.env.

    • sicmutils.simplify.rules/*divide-numbers-through-simplify?* is now true
      by default; numbers in the denominator will now automatically pull up into
      the numerator. All tests now reflect this setting.

    • Any analyzer generated from sicmutils.expression.analyze can now act on
      both bare, unwrapped expressions (raw lists etc) and on
      sicmutils.expression.Literal instances. This means that you can now call
      sicmutils.simplify/{*rf-simplify*,*poly-simplify*} as functions and
      canonicalize some form with either simplifier without triggering a full
      simplification. A small win, but ice.

    • sicmutils.polynomial.factor got a major rewrite, and now exposes a few
      functions like poly->factored-expression, factor-expression and
      factor.

      • factor is tremendously useful! Call factor (it's aliased into
        sicmutils.env) on any expression to factor out all possible terms.
        This makes it much easier to see where there is some cancellation
        lurking, in, say, some expression you know should equal zero (a
        residual).
    • bugfix: sicmutils.expression.Literal instances now compare their contained
      expression via sicmutils.value/=.

    • sicmutils.rules/constant-elimination can now eliminate constants from
      expressions with any arity, not just binary forms.

    Now, the three big namespaces... sicmutils.polynomial,
    sicmutils.rational-function and sicmutils.polynomial.gcd all got a big
    overhaul.

    • sicmutils.polynomial notes:

      • Polynomial uses a new sparse representation for its "power product"
        term; this, plus an arithmetic rewrite, makes the whole system much faster
        for larger numbers of variables (for all #s, really).

      • Polynomial instances implement many more Clojure(script) protocols. They
        can hold metadata; they can be evaluated as functions of their
        indeterminates, and seq now returns a sequence of terms.

      • Polynomial extends sicmutils.function/IArity and
        differential/IPerturbed, so you can use sicmutils.function/arity, and
        take derivatives of functions that return polynomials.

      • In their arithmetic, Polynomial instances will drop down to bare
        coefficients whenever some multiplication or addition removes all
        indeterminates. All binary arithmetic exposed in the namespace can handle
        non-Polynomial instances on either or both sides, so this is fine.
        Coefficients are treated as constant polynomials.

      • The namespace holds many new functions. Some choice ones are:

        • constructors: make, constant, linear, c*xn, identity, and
          new-variables

        • accessor functions: arity, degree, coefficients, leading-term,
          leading-coefficient, leading-exponents, leading-base-coefficient,
          trailing-coefficient, lowest-degree

        • predicates: monomial?, monic?, univariate?, multivariate?,
          negative?

        • functions to generate new polynomials: map-coefficients,
          map-exponents, scale, scale-l, normalize, reciprocal,
          drop-leading-term, contract and extend alongside contractible?,
          lower-arity, raise-arity, with-lower-arity, arg-scale,
          arg-shift

        • arithmetic: negate, abs, add, sub, mul, square, cube,
          expt, divide along with divisible?, evenly-divide,
          pseudo-remainder, and lots of functions installed into the generic
          arithmetic system.

        • different ways to evaluate polynomials: evaluate, horner-with-error

        • calculus! partial-derivative and partial-derivatives are alive and
          well, and work with the D operator.

        • Functions to get in and out of polynomials from other types:
          univariate->dense, ->power-series, expression->, ->expression

    • sicmutils.polynomial.gcd also got a rewrite; it's fairly clear to read
      now, and prepared for the eventual addition of the sparse multivariate GCD
      routine that scmutils uses. There are some efficiency gains here too that
      let us turn a number of tests back on, or demote them from :long markers.

    • sicmutils.rational-function notes:

      • RationalFunction instances implement many more Clojure(script)
        protocols. They can hold metadata; they can be evaluated as functions of
        their indeterminates, and seq now returns a pair of numerator,
        denominator.

      • RationalFunction extends sicmutils.function/IArity and
        sicmutils.ratio/IRational, so our generic arity, numerator and
        denominator work on these instances.

      • Here are some new functions from the RationalFunction namespace:

        • constructor: make, drops to polynomial or coefficient where needed
          just like Polynomial functions

        • functions to generate new rational functions: arg-scale, arg-shift

        • predicates: negative?

        • arithmetic: negate, abs, add, sub, mul, square, cube,
          expt, invert, div, gcd, and many functions installed into the
          generic arithmetic system.

        • evaluation via evaluate

        • calculus! partial-derivative and partial-derivatives are alive and
          well, and work with the D operator.

        • Functions to get in and out of rational functions from symbolic
          expressions: expression->, ->expression.

@codecov-io
Copy link
codecov-io commented Apr 7, 2021

Codecov Report

Merging #341 (2c314e6) into master (da43ad1) will decrease coverage by 2.42%.
The diff coverage is 75.33%.

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

@@            Coverage Diff             @@
##           master     sicmutils/sicmutils#341      +/-   ##
==========================================
- Coverage   84.05%   81.62%   -2.43%     
==========================================
  Files          83       93      +10     
  Lines        9367    11279    +1912     
  Branches      496      557      +61     
==========================================
+ Hits         7873     9206    +1333     
- Misses        998     1516     +518     
- Partials      496      557      +61     
Impacted Files Coverage Δ
src/sicmutils/abstract/number.cljc 80.00% <0.00%> (-2.48%) ⬇️
src/sicmutils/simplify/rules.cljc 26.36% <0.00%> (-10.22%) ⬇️
src/sicmutils/calculus/curvature.cljc 48.00% <48.00%> (ø)
src/sicmutils/rational_function.cljc 70.07% <50.00%> (-15.10%) ⬇️
src/sicmutils/structure.cljc 86.30% <52.38%> (-0.07%) ⬇️
src/sicmutils/calculus/connection.cljc 53.74% <53.74%> (ø)
src/sicmutils/polynomial/gcd.cljc 54.85% <54.70%> (-31.42%) ⬇️
src/sicmutils/numsymb.cljc 90.14% <55.55%> (-0.13%) ⬇️
src/sicmutils/calculus/vector_calculus.cljc 63.33% <63.33%> (ø)
src/sicmutils/calculus/metric.cljc 65.76% <65.76%> (ø)
... and 56 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 dfe4eee...9e14091. Read the comment docs.

@sritchie sritchie force-pushed the sritchie/poly_upgrade branch from 1c236f6 to 55054e2 Compare May 3, 2021 13:50
@sritchie sritchie changed the title Upgrade polynomial gcd [in progress] Upgrade polynomial + rational function representation, prep for sparse gcd [in progress] May 3, 2021
@codecov-commenter
Copy link
codecov-commenter commented May 11, 2021

Codecov Report

Merging #341 (6ded9da) into master (a018123) will increase coverage by 0.01%.
The diff coverage is 85.86%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     sicmutils/sicmutils#341      +/-   ##
==========================================
+ Coverage   83.95%   83.97%   +0.01%     
==========================================
  Files          95       97       +2     
  Lines       12113    12872     +759     
  Branches      619      673      +54     
==========================================
+ Hits        10170    10809     +639     
- Misses       1324     1390      +66     
- Partials      619      673      +54     
Impacted Files Coverage Δ
src/sicmutils/value.cljc 83.08% <ø> (ø)
src/sicmutils/numbers.cljc 80.68% <22.22%> (-11.63%) ⬇️
src/sicmutils/polynomial/factor.cljc 66.27% <71.21%> (-12.48%) ⬇️
src/sicmutils/rational_function.cljc 79.26% <79.25%> (-8.30%) ⬇️
src/sicmutils/series.cljc 63.23% <80.00%> (+0.63%) ⬆️
src/sicmutils/polynomial.cljc 82.64% <82.16%> (-6.06%) ⬇️
src/sicmutils/ratio.cljc 85.52% <86.66%> (+4.49%) ⬆️
src/sicmutils/polynomial/gcd.cljc 93.83% <94.11%> (+5.31%) ⬆️
src/sicmutils/expression/analyze.cljc 96.39% <95.29%> (-1.65%) ⬇️
src/sicmutils/numsymb.cljc 93.20% <96.72%> (+2.31%) ⬆️
... and 21 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 a018123...6ded9da. Read the comment docs.

@sritchie sritchie force-pushed the sritchie/poly_upgrade branch from f3b6683 to df7b942 Compare May 26, 2021 19:25
@sritchie sritchie changed the title Upgrade polynomial + rational function representation, prep for sparse gcd [in progress] Upgrade polynomial + rational function representation, prep for sparse gcd May 26, 2021
@sritchie sritchie force-pushed the sritchie/poly_upgrade branch from a13f185 to 3e53e15 Compare May 27, 2021 12:00
@sritchie sritchie force-pushed the sritchie/poly_upgrade branch from 654c2c3 to d2998de Compare May 27, 2021 18:30
@sritchie sritchie changed the title Upgrade polynomial + rational function representation, prep for sparse gcd Upgrade polynomial + rational function representation, gcd namespace May 27, 2021
@sritchie sritchie merged commit 5385c8e into master May 27, 2021
@sritchie sritchie deleted the sritchie/poly_upgrade branch May 27, 2021 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0