Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge branch 'u/tscrim/dense_lls-31897' of git://trac.sagemath.org/sa…
Browse files Browse the repository at this point in the history
…ge into t/32324/lazy_taylor_series
  • Loading branch information
mantepse committed Aug 12, 2021
2 parents b332728 + c38900b commit 8317764
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 118 deletions.
64 changes: 36 additions & 28 deletions src/sage/data_structures/coefficient_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
sage: type(f._coeff_stream)
<class 'sage.data_structures.coefficient_stream.CoefficientStream_coefficient_function'>
There are basic unary and binary operators available for the coefficient streams.
For example, we can add two streams together::
There are basic unary and binary operators available for the coefficient
streams. For example, we can add two streams together::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_coefficient_function
sage: from sage.data_structures.coefficient_stream import CoefficientStream_add
sage: from sage.data_structures.coefficient_stream import *
sage: f = CoefficientStream_coefficient_function(lambda n: n, QQ, True, 0)
sage: [f[i] for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Expand All @@ -34,58 +33,52 @@
Coefficient streams can be subtracted::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_sub
sage: h = CoefficientStream_sub(f, g)
sage: [h[i] for i in range(10)]
[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
Coefficient streams can be multiplied::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_product
sage: h = CoefficientStream_cauchy_product(f, g)
sage: [h[i] for i in range(10)]
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
Coefficient streams can be divided::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_inverse
sage: ginv = CoefficientStream_cauchy_inverse(g)
sage: h = CoefficientStream_cauchy_product(f, ginv)
sage: [h[i] for i in range(10)]
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Two coefficient streams can be composed (depending on whether it exists)::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_composition
sage: CS_prod = CoefficientStream_cauchy_product
sage: CS_inv = CoefficientStream_cauchy_inverse
sage: g = CoefficientStream_coefficient_function(lambda n: n, QQ, True, 1)
sage: h = CoefficientStream_composition(f, g)
sage: h = CoefficientStream_composition(f, g, CS_prod, CS_inv)
sage: [h[i] for i in range(10)]
[0, 1, 4, 14, 46, 145, 444, 1331, 3926, 11434]
We can also use the unary negation operator on a coefficient stream::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_neg
sage: h = CoefficientStream_neg(f)
sage: [h[i] for i in range(10)]
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
Coefficient streams can be multiplied by a scalar::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_lmul
sage: h = CoefficientStream_lmul(f, 2)
sage: [h[i] for i in range(10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
The multiplicative inverse of a series can also be obtained::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_inverse
sage: h = CoefficientStream_cauchy_inverse(g)
sage: [h[i] for i in range(10)]
[-2, 1, 0, 0, 0, 0, 0, 0, 0, 0]
Functions can also be applied to a coefficient stream::
sage: from sage.data_structures.coefficient_stream import CoefficientStream_map_coefficients
sage: h = CoefficientStream_map_coefficients(f, lambda n: n^2, QQ)
sage: [h[i] for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Expand Down Expand Up @@ -295,10 +288,12 @@ def iterate_coefficients(self):
EXAMPLES::
sage: from sage.data_structures.coefficient_stream import (CoefficientStream_coefficient_function, CoefficientStream_composition)
sage: from sage.data_structures.coefficient_stream import CoefficientStream_coefficient_function, CoefficientStream_composition
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_product as CS_prod
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_inverse as CS_inv
sage: f = CoefficientStream_coefficient_function(lambda n: 1, ZZ, False, 1)
sage: g = CoefficientStream_coefficient_function(lambda n: n^3, ZZ, False, 1)
sage: h = CoefficientStream_composition(f, g)
sage: h = CoefficientStream_composition(f, g, CS_prod, CS_inv)
sage: n = h.iterate_coefficients()
sage: [next(n) for i in range(10)]
[1, 9, 44, 207, 991, 4752, 22769, 109089, 522676, 2504295]
Expand Down Expand Up @@ -1323,37 +1318,42 @@ class CoefficientStream_composition(CoefficientStream_binary):
EXAMPLES::
sage: from sage.data_structures.coefficient_stream import (CoefficientStream_composition, CoefficientStream_coefficient_function)
sage: from sage.data_structures.coefficient_stream import CoefficientStream_composition, CoefficientStream_coefficient_function
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_product as CS_prod
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_inverse as CS_inv
sage: f = CoefficientStream_coefficient_function(lambda n: n, ZZ, True, 1)
sage: g = CoefficientStream_coefficient_function(lambda n: 1, ZZ, True, 1)
sage: h = CoefficientStream_composition(f, g)
sage: h = CoefficientStream_composition(f, g, CS_prod, CS_inv)
sage: [h[i] for i in range(10)]
[0, 1, 3, 8, 20, 48, 112, 256, 576, 1280]
sage: u = CoefficientStream_composition(g, f)
sage: u = CoefficientStream_composition(g, f, CS_prod, CS_inv)
sage: [u[i] for i in range(10)]
[0, 1, 3, 8, 21, 55, 144, 377, 987, 2584]
"""
def __init__(self, f, g):
def __init__(self, f, g, prod_stream, inv_stream):
"""
Initialize ``self``.
TESTS::
sage: from sage.data_structures.coefficient_stream import (CoefficientStream_coefficient_function, CoefficientStream_composition)
sage: from sage.data_structures.coefficient_stream import CoefficientStream_coefficient_function, CoefficientStream_composition
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_product as CS_prod
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_inverse as CS_inv
sage: f = CoefficientStream_coefficient_function(lambda n: 1, ZZ, True, 1)
sage: g = CoefficientStream_coefficient_function(lambda n: n^2, ZZ, True, 1)
sage: h = CoefficientStream_composition(f, g)
sage: h = CoefficientStream_composition(f, g, CS_prod, CS_inv)
"""
assert g._approximate_order > 0
self._fv = f._approximate_order
self._gv = g._approximate_order
self._prod_stream = prod_stream
if self._fv < 0:
ginv = CoefficientStream_cauchy_inverse(g)
ginv = inv_stream(g)
# the constant part makes no contribution to the negative
# we need this for the case so self._neg_powers[0][n] => 0
self._neg_powers = [CoefficientStream_zero(f._is_sparse), ginv]
for i in range(1, -self._fv):
self._neg_powers.append(CoefficientStream_cauchy_product(self._neg_powers[-1], ginv))
self._neg_powers.append(self._prod_stream(self._neg_powers[-1], ginv))
# Placeholder None to make this 1-based
self._pos_powers = [None, g]
val = self._fv * self._gv
Expand All @@ -1369,10 +1369,12 @@ def get_coefficient(self, n):
EXAMPLES::
sage: from sage.data_structures.coefficient_stream import (CoefficientStream_coefficient_function, CoefficientStream_composition)
sage: from sage.data_structures.coefficient_stream import CoefficientStream_coefficient_function, CoefficientStream_composition
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_product as CS_prod
sage: from sage.data_structures.coefficient_stream import CoefficientStream_cauchy_inverse as CS_inv
sage: f = CoefficientStream_coefficient_function(lambda n: n, ZZ, True, 1)
sage: g = CoefficientStream_coefficient_function(lambda n: n^2, ZZ, True, 1)
sage: h = CoefficientStream_composition(f, g)
sage: h = CoefficientStream_composition(f, g, CS_prod, CS_inv)
sage: h.get_coefficient(5)
527
sage: [h.get_coefficient(i) for i in range(10)]
Expand All @@ -1382,7 +1384,7 @@ def get_coefficient(self, n):
return sum(self._left[i] * self._neg_powers[-i][n] for i in range(self._fv, n // self._gv + 1))
# n > 0
while len(self._pos_powers) <= n // self._gv:
self._pos_powers.append(CoefficientStream_cauchy_product(self._pos_powers[-1], self._right))
self._pos_powers.append(self._prod_stream(self._pos_powers[-1], self._right))
ret = sum(self._left[i] * self._neg_powers[-i][n] for i in range(self._fv, 0))
if n == 0:
ret += self._left[0]
Expand Down Expand Up @@ -1699,8 +1701,14 @@ def get_coefficient(self, n):
sage: g = CoefficientStream_map_coefficients(f, lambda n: n.degree() + 1, R)
sage: [g.get_coefficient(i) for i in range(-1, 3)]
[1, 0, 1, 1]
sage: f = CoefficientStream_coefficient_function(lambda n: n, ZZ, True, 0)
sage: g = CoefficientStream_map_coefficients(f, lambda n: 5, GF(3))
sage: [g.get_coefficient(i) for i in range(10)]
[0, 5, 5, 0, 5, 5, 0, 5, 5, 0]
"""
c = self._series[n]
c = self._ring(self._series[n])
if c:
return self._function(self._ring(c))
return self._function(c)
return c

Loading

0 comments on commit 8317764

Please sign in to comment.