Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2607: more efficient implementation of Add.as_numer_denom
By collecting denominators and processing Integer denominators, as_numer_denom is more efficient. When denominators are collected this produces less terms to process; when Integers are handled separately, their denominator can be computed very quickly with integer arithmetic. Because of collection of denominators, there will be a slight bit of nesting that will occur: >>> (1/x+a/x+b/y).as_numer_denom() (b*x + y*(a + 1), x*y) Processing Integer denominators will not result in nesting as a result of the automatic distribution of Rationals on Adds: >>> (a/1+b/2+c/3+d/4).as_numer_denom() (12*a + 6*b + 4*c + 3*d, 12) (Note, too, that the numerical gcd of numerator and denominator has been removed so (24*a + 12*b + 8*c + 6*d, 24) is not obtained. There are other possibilities for improvement: if one doesn't care about producing a nested numerator, a binary approach can be employed where pairs of terms are processed. If a mostly cancelled expression is desired, a symbolic lcm method (Vladimir's approach) can be used. One of the dilemmas is to know when to use one method over another; and testing to see which method to use can eliminate the savings that a technique might have over another. Testing for common denominators and integers are a fairly easy preprocessing step that can lead to large improvements over a more naive method. This work was a collaborative effort of Aaron Meurer, Vladimir Peric, and Christopher Smith.
- Loading branch information