-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Conversation
669ff95
to
06c85d2
Compare
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
1c236f6
to
55054e2
Compare
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
f3b6683
to
df7b942
Compare
a13f185
to
3e53e15
Compare
654c2c3
to
d2998de
Compare
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 thatthey are now a good bit faster than the original scmutils implementation.
sicmutils.polynomial
andsicmutils.rational-function
are now solid datastructures 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 forsorted-map
instances.GCD in Clojurescript is now fast and efficient between all combinations of
js/BigInt
andjs/Number
, and in Clojure between all combinations ofclojure.lang.BigInt
,BigInteger
,Long
andInteger
.on the JVM, GCD now works properly with rational numbers. Previously
anything non-integral would return
1
; now(gcd 1/2 1/3)
properly returns1/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 a0, rather than attempting to multiply all remaining items by 0.
The default function for
sicmutils.generic/lcm
protects against overflowby dividing only a single one of its arguments
a
andb
by(gcd a b)
.(g/lcm 0 0)
now properly returns 0.New
sicmutils.util.aggregate/{monoid,group}
functions let you buildmulti-arity aggregations out of binary combination functions, with an option
to bail early at "annihilator" values, like 0 for multiplication.
New multi-arity
lcm
andgcd
implementations for symbolic expressionsappropriately handle
0
and1
on either side, as well as the case whereboth arguments are equal.
In the
sicmutils.numsymb
namespace, thanks tomonoid
andgroup
, the'*
,'/
,'-
,'+
,'or
,'and
,'gcd
,'lcm
and'=
operationsnow have efficient multi-arity implementations that stop computing when they
receive an annihilator, like
0
for multiplication ortrue
foror
.Access these via
(sicmutils.numsymb/symbolic-operator <symbol>)
.sicmutils.series/PowerSeries
gainsarg-scale
andarg-shift
functions;these are identical to
sicmutils.function/arg-{scale,shift}
, but preservethe
PowerSeries
type. (#367 proposes making these functions generic.)New
sicmutils.ratio/IRational
protocol, withnumerator
anddenominator
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 nowtrue
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 onboth bare, unwrapped expressions (raw lists etc) and on
sicmutils.expression.Literal
instances. This means that you can now callsicmutils.simplify/{*rf-simplify*,*poly-simplify*}
as functions andcanonicalize 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 fewfunctions like
poly->factored-expression
,factor-expression
andfactor
.factor
is tremendously useful! Callfactor
(it's aliased intosicmutils.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 containedexpression via
sicmutils.value/=
.sicmutils.rules/constant-elimination
can now eliminate constants fromexpressions with any arity, not just binary forms.
Now, the three big namespaces...
sicmutils.polynomial
,sicmutils.rational-function
andsicmutils.polynomial.gcd
all got a bigoverhaul.
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. Theycan hold metadata; they can be evaluated as functions of their
indeterminates, and
seq
now returns a sequence of terms.Polynomial
extendssicmutils.function/IArity
anddifferential/IPerturbed
, so you can usesicmutils.function/arity
, andtake derivatives of functions that return polynomials.
In their arithmetic,
Polynomial
instances will drop down to barecoefficients 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
, andnew-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
andextend
alongsidecontractible?
,lower-arity
,raise-arity
,with-lower-arity
,arg-scale
,arg-shift
arithmetic:
negate
,abs
,add
,sub
,mul
,square
,cube
,expt
,divide
along withdivisible?
,evenly-divide
,pseudo-remainder
, and lots of functions installed into the genericarithmetic system.
different ways to evaluate polynomials:
evaluate
,horner-with-error
calculus!
partial-derivative
andpartial-derivatives
are alive andwell, 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 readnow, 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 ofnumerator
,denominator
.RationalFunction
extendssicmutils.function/IArity
andsicmutils.ratio/IRational
, so our genericarity
,numerator
anddenominator
work on these instances.Here are some new functions from the
RationalFunction
namespace:constructor:
make
, drops to polynomial or coefficient where neededjust like
Polynomial
functionsfunctions 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 thegeneric arithmetic system.
evaluation via
evaluate
calculus!
partial-derivative
andpartial-derivatives
are alive andwell, and work with the
D
operator.Functions to get in and out of rational functions from symbolic
expressions:
expression->
,->expression
.