sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.4k stars 473 forks source link

Replace Lazy Power Series in species directory #32367

Closed tejasvicsr1 closed 2 years ago

tejasvicsr1 commented 3 years ago

In this ticket we finalize the implementation of lazy series, and replace the old lazy series framework.

Depends on #34423

CC: @mantepse @tscrim @pfili @zimmermann6 @sagetrac-tmonteil

Component: combinatorics

Keywords: LazyPowerSeries, FormalSeries, gsoc2021

Author: Tejasvi Chebrolu, Martin Rubey, Travis Scrimshaw

Branch/Commit: 4fc981b

Reviewer: Martin Rubey, Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/32367

tscrim commented 2 years ago
comment:113

Did we want to error out when doing the division by a positive valuation series? I don’t remember. Although these are integral domains, right (of course, assuming the base ring is)? So we could (and probably should) follow many other things in Sage and go to the formal fraction field and do the computation there, although that possibly needs the gcd to be implemented. I am not sure what the best way forward with division. In any case, we should end up with the second answer (and likely consistently in the fraction field).

mantepse commented 2 years ago
comment:114

Fraction field sounds great.

The problem i see with error: we can only check once a coefficient is requested. If we check already in div itself, implicit definitions might not work anymore.

tscrim commented 2 years ago
comment:115

Yea, I don’t see an easy way out of something that has an uninitialized series. Although I was thinking of erroring out whenever the approximate valuation was positive.

With fraction fields, we run into the problem of if we want to always return something in the fraction field or not. If we do, then we need to teach using more // instead of /. If we don’t, then we need to check somehow when to give something in the fraction field, which is basically the same problem.

Maybe we could get around this with using the same implementation for the fraction field with just a different parent. This might help with making things work more seamlessly.

For the short-term, I propose we raise an NotImplementedError whenever the denominator is known to have a positive valuation.

mantepse commented 2 years ago
comment:116

OK, any way you like. I admit that I like the idea of //. I remember that in FriCAS it was sometimes buggy to go from the fraction field to the ring, if mathematically possible.

tscrim commented 2 years ago
comment:117

A fraction field shouldn't be too hard to implement, but it shouldn't be something we should hold up this ticket for. I have a good idea for how to do it now. Well, for right now, it is GIGO, so we can leave it be other than explicitly telling the user when giving a positive valuation series (and promising we will implement it via the NotImplementedError, as we know those cases are bad). What do you think?

mantepse commented 2 years ago
comment:118

I keep saying to myself: let me just do this tiny thing, and then I'll stop and continue with my research (one week to go until term starts).

Now it's saturday evening, and I haven't even started what I promised to do...

It is fun, though.

mantepse commented 2 years ago
comment:119

It wasn't as easy as I thought to raise an error and be lazy at the same time, but its done.

Again, I pushed this to #34470.

I used the TestSuite.run() a lot to discover bugs, and I found many. Whenever I thought I finally did it right, the TestSuite.run() taught me otherwise. We should absolutely put more effort into these.

tscrim commented 2 years ago
comment:120

From a quick look and what I was thinking, it seems like an overly complicated solution. I was just thinking of putting if self._stream._approximate_order > 0: in the inverse/division methods, then in the division/inverse streams, adding a check attribute and, e.g., if check and not self.right[0]:. I will take a closer look today.

Indeed, that is the idea behind the TestSuite, and one of the reasons why I make sure to add it somewhere, typically for the __init__ doctests.

mantepse commented 2 years ago
comment:121

Well, part of the problem was that we were not lazy enough for certain recursive definitions. For example

            sage: P.<x> = LazyPowerSeriesRing(QQ)
            sage: f = P.undefined()
            sage: f.define(1 - ~f*x)
            sage: f
            1 - x - x^2 - 2*x^3 - 5*x^4 - 14*x^5 - 42*x^6 + O(x^7)

            sage: D = LazyDirichletSeriesRing(QQ, "s")
            sage: g = D([0, 1])
            sage: f = D.undefined()
            sage: f.define(1 + ~f*g)
            sage: f
            1 + 1/(2^s) - 1/(4^s) + O(1/(8^s))

did not work previously.

Also, equality testing should be clearer now. I don't know why we decided to check terms in the range

n = min(self._coeff_stream._approximate_order, other._coeff_stream._approximate_order)
m = max(self._coeff_stream._approximate_order, other._coeff_stream._approximate_order)

previously - it seems to me that this computes coefficients for no good reason. (it won't hurt, but it won't help either).

Other than that, the diff is not really large.

I believe that giving a name to the true order, if it is known, might be helpful for further improvements, but I did not check.

I also did not check whether we should update _approximate_order in Stream_inexact.__getitem__. I would like to first implement more _test_XXX methods (in particular for composition and reversion), to get a better picture of performance.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 69d44ab to 3d5b8cf

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

3d5b8cfLast little things from the patchbot.
tscrim commented 2 years ago
comment:123

Well, for now, let's bring this to a positive review and move onto the next ticket (assuming we get a green bot this time because I didn't do anything stupid). I will do stuff there starting from your changes.

I did a few little things from what I saw from the last patchbot report (the other failure seems unrelated). I made species library lazily imported to decrease the startup modules number.

mantepse commented 2 years ago
comment:125

The Returns in the doctring noticed by the patchbot is fixed in the follow-up ticket #34470.

The pyflakes warnings about imported but unused modules in species/library.py, are, as far as I know, unavoidable.

The ellipsis in the doctest in sfa.py is misinterpreted by the patchbot as a doctest continuation, but is correct, as far as I know.

I am therefore setting this to positive review.

mantepse commented 2 years ago

Changed branch from u/tscrim/replace_lazy_power_series-32367 to u/mantepse/replace_lazy_power_series-32367

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. Last 10 new commits:

5d76825Merge branch 'u/chapoton/34494' of https://github.com/sagemath/sagetrac-mirror into u/tscrim/derivatives_lazy_series-34413
97df300Updating sf/sfa.py doctest due to #34494.
1388b8aMerge branch 'u/chapoton/34494' of https://github.com/sagemath/sagetrac-mirror into public/rings/lazy_series_revert-34383
7bfe5f7Merge branch 'public/rings/lazy_series_revert-34383' of https://github.com/sagemath/sagetrac-mirror into public/rings/lazy_series_revert-34383
2039990Updating doctest due to changes from #34494.
cdea820Merge branch 'public/rings/lazy_series_revert-34383' of https://github.com/sagemath/sagetrac-mirror into u/tscrim/derivatives_lazy_series-34413
b7f04edMerge branch 'develop' of trac.sagemath.org:sage into t/34413/derivatives_lazy_series-34413
2325826Merge branch 'u/mantepse/derivatives_lazy_series-34413' of trac.sagemath.org:sage into t/34422/implement_functorial_composition_of_lazy_symmetric_functiosn
ebcf5c3Merge branch 'u/mantepse/implement_functorial_composition_of_lazy_symmetric_functiosn' of trac.sagemath.org:sage into t/34423/implement_arithmetic_product_of_lazy_symmetric_functions
4172688Merge branch 'u/mantepse/implement_arithmetic_product_of_lazy_symmetric_functions' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series-32367
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 3d5b8cf to 4172688

mantepse commented 2 years ago
comment:129

trivial (autopmatic) merge, necessary to make the patchbots happy.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. New commits:

67dff95Merge branch 'develop' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series-32367
4fc981bfix for pyflakes and blocks
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 4172688 to 4fc981b

mantepse commented 2 years ago
comment:131

trivial fixes to make patchbot happy.

vbraun commented 2 years ago

Changed branch from u/mantepse/replace_lazy_power_series-32367 to 4fc981b