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

tejasvicsr1 commented 3 years ago

Branch: u/gh-tejasvicsr1/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series

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

Branch pushed to git repo; I updated commit sha1. Last 10 new commits:

651b372Revert "Merge branch 'u/gh-tejasvicsr1/special_functions_for_lazy_series' of git://trac.sagemath.org/sage into t/32324/lazy_taylor_series"
7c9a5ddfix invert
51357a0Small reworkings and fixes. Adding another composition test.
7501204Adding documentation to the user-facing class level documentation.
4723f6bRefactoring we discussed at the meeting today.
651c3edonly get coefficient if necessary
65e604dMerge branch 'u/mantepse/dense_lls-31897' of git://trac.sagemath.org/sage into t/32324/lazy_taylor_series
b332728fix and document series reversion
6a2391eMerge remote-tracking branch 'trac/u/mantepse/lazy_taylor_series' into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
0d56b02Merged new Taylor series and deleted new_gs.py
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Commit: 0d56b02

tscrim commented 3 years ago

Changed keywords from LazyPowerSeries, FormalSeries, GSoC21 to LazyPowerSeries, FormalSeries, gsoc2021

tscrim commented 3 years ago

Dependencies: #32324

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

Changed commit from 0d56b02 to 39993e3

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

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

39993e3Fixed the initialising issue.
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

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

73b9964Fixed naming issue.
db20680Changed _gs_iterator to callables
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 39993e3 to db20680

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

Changed commit from db20680 to 7d491f6

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

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

974fa4fChanged _list and _term methods.
6cfdf8dCreated restrict method in the series helper function.
eef15d8Changed the name of iterator to callable.
7d491f6Raised a not implemented error for all not corrected methods in the generating_series file.
mantepse commented 3 years ago

Changed branch from u/gh-tejasvicsr1/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series to u/mantepse/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series

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

Branch pushed to git repo; I updated commit sha1. Last 10 new commits:

661c3d4backport two fixes from 32324
8a7dcd3fix oversight in shift
78b8fffLast trivial fix for clean pyflakes.
96771f0Merge branch 'u/tscrim/dense_lls-31897' of git://trac.sagemath.org/sage into t/32324/lazy_taylor_series
86925afinitial version of lazy symmetric functions
aa8a250initial and insanely stupid plethysm implementation
1a36f59add (currently failing) doctest for plethysm
03198a0fix bug in plethysm
eb3e88fmore doctests for composition and plethysm
a9084c6Merge branch 'u/mantepse/lazy_taylor_series' of git://trac.sagemath.org/sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 7d491f6 to a9084c6

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

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

4b76e93switch to power sum by default
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from a9084c6 to 4b76e93

tejasvicsr1 commented 3 years ago

Changed branch from u/mantepse/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series to u/gh-tejasvicsr1/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series

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

Changed commit from 4b76e93 to 48e8e26

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

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

48e8e26Changed more methods.
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

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

78a85aeSome more work in generating series.
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 48e8e26 to 78a85ae

mantepse commented 2 years ago

Changed branch from u/gh-tejasvicsr1/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series to u/mantepse/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series

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

Branch pushed to git repo; I updated commit sha1. Last 10 new commits:

e4479edfix missing space
3da6b3dbetter docstring
8798139Adding a slightly more complicated doctest and other small tweaks.
75e3f3bMerge branch 'public/rings/laurent_quo_rem_bug-34330' into public/rings/lazy_talyor_series-32324
c74fd71improve documentation, move options to abstract base class
9d6579bimprove documentation, move zero, one, characteristic, etc. to ABC
feba6b8Working more on `__call__` for LazySymFunc.
3f3e0f2Merge branch 'public/rings/lazy_talyor_series-32324' of https://github.com/sagemath/sagetrac-mirror into public/rings/lazy_talyor_series-32324
6727228Merge branch 'public/rings/lazy_talyor_series-32324' of trac.sagemath.org:sage into t/32324/public/rings/lazy_talyor_series-32324
d4f2e5cMerge branch 'public/rings/lazy_talyor_series-32324' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 78a85ae to d4f2e5c

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

Changed commit from d4f2e5c to 1b34e62

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

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

028796dFixing numerous issues with `__call__` and expanding its functionality. Moving plethysm to a Stream_plethysm.
9fb155fRemoving unused code from previous version.
1b34e62Merge branch 'public/rings/lazy_talyor_series-32324' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 1b34e62 to 5ba18e8

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

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

7f9dbb1Some last doc fixes and tweaks.
4e03feeremove unused local variable
e780472Addressing the linter complaint.
5ba18e8Merge branch 'public/rings/lazy_talyor_series-32324' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
tscrim commented 2 years ago
comment:20

Is this ready for review?

mantepse commented 2 years ago
comment:21

No, not at all, I am working on it.

I am having slight difficulties with plethysm, but I think that I have mostly resolved these by now.

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

Branch pushed to git repo; I updated commit sha1. Last 10 new commits:

50ce450derivative for LazyTaylorSeries
10cfc11derivative for LazySymmetricFunctions
7be0b86functorial composition for LazySymmetricFunctions
c755d71implement arithmetic product for LazySymmetricFunction
c36796eMerge branch 'u/mantepse/implement_arithmetic_product_of_lazy_symmetric_functions' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
18429efdo not use change_ring, because it is brittle, and take care of zero coefficients which are not symmetric functions
481d78dMerge 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
b469431do not use change_ring, because it is brittle
56ee69eMerge branch 'u/mantepse/implement_arithmetic_product_of_lazy_symmetric_functions' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
1e8f69dremove LazyPowerSeries and Stream
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 5ba18e8 to 1e8f69d

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

Changed commit from 1e8f69d to d75ac13

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

Branch pushed to git repo; I updated commit sha1. Last 10 new commits:

e1e7f0dMerge branch 'u/mantepse/implement_derivatives_of_lazy_series' of trac.sagemath.org:sage into t/34473/remove_rings_from_streams
6010361remove ring argument from Stream_map_coefficient and Stream_function
aa15394Merge branch 'u/mantepse/remove_rings_from_streams' of trac.sagemath.org:sage into t/34422/implement_functorial_composition_of_lazy_symmetric_functiosn
760372aaddress reviewer's comments
8ca3624polish functorial composition
b2ef1e4allow SymmetricFunctions as arguments of functorial composition
83e7dd3Merge 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
d22f87aadapt code after merge
f2a3cafreimplement arithmetic_product by using the symmetric function version (which is more robust
d75ac13Merge branch 'u/mantepse/implement_arithmetic_product_of_lazy_symmetric_functions' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from d75ac13 to 293b439

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

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

293b439adapt doctests
mantepse commented 2 years ago
comment:25

A small step but a giant leap!

mantepse commented 2 years ago

Changed dependencies from #32324 to #34423

mantepse commented 2 years ago

Changed author from Tejasvi Chebrolu to Tejasvi Chebrolu, Martin Rubey, Travis Scrimshaw

tscrim commented 2 years ago
comment:28

From a first quick look, it seems like there are just a few small things to address:

These need at least one doctest each:

    def __init__(self, base_ring):
        super().__init__(base_ring, names="z")

This can be simplified:

-    return CIS(lambda n: _cl_term(n))
+    return CIS(_cl_term)

We can also use (the cached)

-base_ring(1)
+base_ring.one()

We no longer have a coefficients() method (a difference between the implementations), right? We should introduce a deprecation message for this method or add it to the lazy series. I am leaning towards adding it to the lazy series as a redirect of self[:n].

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

Changed commit from 293b439 to 89270eb

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

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

8b6a752no period at end of input description, break long doctest
f88a5c8fix arithmetic product with 0 and finite support
89270ebMerge branch 'u/mantepse/implement_arithmetic_product_of_lazy_symmetric_functions' of trac.sagemath.org:sage into t/32367/replace_lazy_power_series_in_species_directory_with_the_new_lazy_taylor_series
mantepse commented 2 years ago
comment:30

There are several failing doctests and a few hidden bugs elsewhere (found using grep LazyPowerSeries). Trying to fix.

mantepse commented 2 years ago
comment:31

There is at least one place, where we feed the LazyPowerSeriesRing with an infinite iterator, see below.

While this is surely easy to change, shouldn't we support this, too? Actually, I thought we did, but I cannot find it right now.

                # If we're here, we may not be a finite crystal.
                # In fact, we're probably infinite.
                from sage.rings.lazy_series_ring import LazyTaylorSeriesRing
                if q is None:
                    P = LazyTaylorSeriesRing(ZZ, names='q')
                else:
                    P = q.parent()
                if not isinstance(P, LazyTaylorSeriesRing):
                    raise TypeError("the parent of q must be a lazy power series ring")
                ret = P(iter_by_deg(mg))
                return ret
mantepse commented 2 years ago
comment:32

There is one doctest from a book which we cannot sensibly support anymore, see below.

In short:

  sage: L.<z> = LazyPowerSeriesRing(QQ)
  sage: C = L()
  sage: C._name = 'C'
  sage: C.define( z + C * C )

must become

  sage: L.<z> = LazyPowerSeriesRing(QQ)
  sage: C = L.undefined(valuation=1)
  sage: C.define( z + C * C )

Can I simply change this? (Note that we do not support this recursive definition with valuation=0 anymore.)

Sage example in ./combinat.tex, line 654::

  sage: L.<z> = LazyPowerSeriesRing(QQ)

Sage example in ./combinat.tex, line 661::

  sage: C = L()
  sage: C._name = 'C'
  sage: C.define( z + C * C )

Sage example in ./combinat.tex, line 666::

  sage: [C.coefficient(i) for i in range(11)]
  [0, 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]
mantepse commented 2 years ago
comment:33

Similarly, also in src/sage/tests/books/computational-mathematics-with-sagemath/, but polynomes.tex.

I think we can make exponential an alias, but compute_coefficients does not really make sense anymore.

  sage: L.<x> = LazyPowerSeriesRing(QQ)
  sage: lazy_exp = x.exponential(); lazy_exp
  O(1)

Sage example in ./polynomes.tex, line 2039::

  sage: lazy_exp[5]
  1/120
  sage: lazy_exp
  1 + x + 1/2*x^2 + 1/6*x^3 + 1/24*x^4 + 1/120*x^5 + O(x^6)

Sage example in ./polynomes.tex, line 2062::

  sage: f = L(1)  # the constant lazy series 1
  sage: for i in range(5):
  ....:     f = (x*f).exponential()
  ....:     f.compute_coefficients(5) # forces the computation
  ....:     print(f)                  # of the first coefficients
  1 + x + 1/2*x^2 + 1/6*x^3 + 1/24*x^4 + 1/120*x^5 + O(x^6)
  1 + x + 3/2*x^2 + 5/3*x^3 + 41/24*x^4 + 49/30*x^5 + O(x^6)
  1 + x + 3/2*x^2 + 8/3*x^3 + 101/24*x^4 + 63/10*x^5 + O(x^6)
  1 + x + 3/2*x^2 + 8/3*x^3 + 125/24*x^4 + 49/5*x^5 + O(x^6)
  1 + x + 3/2*x^2 + 8/3*x^3 + 125/24*x^4 + 54/5*x^5 + O(x^6)

Finally:

Sage example in ./polynomes.tex, line 2105::

  sage: from sage.combinat.species.series import LazyPowerSeries
  sage: f = LazyPowerSeries(L, name='f')
  sage: f.define((x*f).exponential())
  sage: f.coefficients(8)
  [1, 1, 3/2, 8/3, 125/24, 54/5, 16807/720, 16384/315]

(L is the lazy power series ring here)

tscrim commented 2 years ago
comment:35

I thought we also supported iterators too, but it is possible that we don’t. I don’t quite have any explicit memory of what we did other than have a discussion about how to handle them. It might have fallen through the cracks.

We can make these changes. The standard thing to do is to cc the book author(s) to let them know.

I guess we don’t have a good mechanism for finding out what is the smallest valuation for a define() series should make sense. I remember us talking about this for Laurent series and not knowing there is a unique solution, much less knowing what lower bound might work. The issue for the power series was we had to try and catch an infinite recursion error (which would be very bad practice because it would hide bugs). I don’t think we talked too much about other solutions. When we have one unknown function, we could take its _approximate_order to be a variable, e.g., n and take the ceiling of the smallest positive real root of f(n) - n, where f is the function that we get from the define() input _approximate_order.

Well, if we don’t want to try anything like this beforehand (or it becomes too much work), then we can simply change the test (and I would say it is better for the user to supply anyways). (That is also a very evil test. :p)

I think exponential() is a good alias to have. I would add a deprecation for compute_coefficients() and remove the line in the above doctest.

mantepse commented 2 years ago
comment:36

Important and somewhat urgent question:

Support for passing generators turns out to be easy, but there is a question of semantics. If the valuation is specified, should it refer to the first term yielded by the generator, or the first non-zero term yielded by the generator.

Both is easy to achieve.

By way of example: do we want

sage: L.<x> = LazyLaurentSeriesRing(QQ); L(iter(NN), valuation=-3)
x^-3 + 2*x^-2 + 3*x^-1 + 4 + 5*x + 6*x^2 + O(x^3)

or

sage: L.<x> = LazyLaurentSeriesRing(QQ); L(iter(NN), valuation=-3)
x^-2 + 2*x^-1 + 3 + 4*x + 5*x^2 + 6*x^3 + O(x^4)

Compare with our current decisions:

sage: L.<x> = LazyLaurentSeriesRing(QQ); L(range(5), valuation=-3)
x^-3 + 2*x^-2 + 3*x^-1 + 4
sage: L.<x> = LazyLaurentSeriesRing(QQ); L(lambda n: n+3, valuation=-3)
x^-2 + 2*x^-1 + 3 + 4*x + 5*x^2 + 6*x^3 + O(x^4)
tscrim commented 2 years ago
comment:37

The first nonzero term following our statement that passing the valuation means the series exactly has that valuation.

We might want to do something as a safety guard of running forever (say 1000 trials) if someone does something crazy like pass

def zero():
    while True: yield 0

(This might arise more naturally if someone passes something that is not known to be exactly 0 but ends up being 0 and doing some iteration over that.)

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

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

b76d8f4allow to define lazy series using iterators, adapt doctest