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

Commit

Permalink
Remove dependence on sage.combinat.species.stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhansen committed Jan 13, 2014
1 parent fea9eb0 commit 6525200
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 73 deletions.
5 changes: 2 additions & 3 deletions src/sage/combinat/species/combinatorial_logarithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@
#*****************************************************************************

from sage.misc.cachefunc import cached_function
from sage.combinat.species.stream import _integers_from
from sage.combinat.sf.all import SymmetricFunctions
from sage.combinat.species.generating_series import CycleIndexSeriesRing
from sage.rings.all import RationalField, Integer, divisors
from sage.rings.all import RationalField, Integer, divisors, NN

@cached_function
def _cl_term(n, R = RationalField()):
Expand Down Expand Up @@ -75,7 +74,7 @@ def _cl_gen (R = RationalField()):
sage: [g.next() for i in range(4)]
[0, p[1], -1/2*p[1, 1] - 1/2*p[2], 1/3*p[1, 1, 1] - 1/3*p[3]]
"""
return (_cl_term(i, R) for i in _integers_from(0))
return (_cl_term(i, R) for i in NN)

@cached_function
def CombinatorialLogarithmSeries(R = RationalField()):
Expand Down
1 change: 0 additions & 1 deletion src/sage/combinat/species/cycle_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from species import GenericCombinatorialSpecies, SpeciesSeriesStream
from series import SeriesStreamFromList
from structure import GenericSpeciesStructure
from generating_series import _integers_from
from sage.structure.unique_representation import UniqueRepresentation
from sage.rings.all import ZZ, divisors, euler_phi
from sage.combinat.species.misc import accept_size
Expand Down
30 changes: 12 additions & 18 deletions src/sage/combinat/species/generating_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
TESTS::
sage: from sage.combinat.species.stream import Stream, _integers_from
sage: from sage.combinat.species.generating_series import CycleIndexSeriesRing
sage: p = SymmetricFunctions(QQ).power()
sage: CIS = CycleIndexSeriesRing(QQ)
::
sage: geo1 = CIS((p([1])^i for i in _integers_from(0)))
sage: geo2 = CIS((p([2])^i for i in _integers_from(0)))
sage: geo1 = CIS((p([1])^i for i in NN))
sage: geo2 = CIS((p([2])^i for i in NN))
sage: s = geo1 * geo2
sage: s[0]
p[]
Expand All @@ -41,10 +40,10 @@
::
sage: def g():
... for i in _integers_from(0):
... for i in NN:
... yield p([2])^i
... yield p(0)
sage: geo1 = CIS((p([1])^i for i in _integers_from(0)))
sage: geo1 = CIS((p([1])^i for i in NN))
sage: geo2 = CIS(g())
sage: s = geo1 * geo2
sage: s[0]
Expand Down Expand Up @@ -74,15 +73,13 @@
#*****************************************************************************
from series import (LazyPowerSeriesRing, LazyPowerSeries)
from series_stream import (SeriesStream, PowerStream, SumGeneratorStream,
ListSumStream, TermStream)
from stream import Stream, _integers_from
from sage.rings.all import Integer, moebius, lcm, divisors, gcd
ListSumStream, TermStream, SeriesStreamFromIterator)
from sage.rings.all import ZZ, Integer, moebius, lcm, divisors, gcd, NN
from sage.combinat.sf.sf import SymmetricFunctions
from sage.misc.cachefunc import cached_function
from sage.combinat.partition import Partition, Partitions
from sage.structure.element import coerce_binop


class OrdinaryGeneratingSeriesRing(LazyPowerSeriesRing):
def __init__(self, R):
"""
Expand Down Expand Up @@ -269,9 +266,7 @@ def factorial_gen():
yield z
n += 1

factorial_stream = Stream(factorial_gen())


factorial_stream = SeriesStreamFromIterator(iterator=factorial_gen(), base_ring=ZZ)

class CycleIndexSeriesRing(LazyPowerSeriesRing):
def __init__(self, R):
Expand Down Expand Up @@ -530,7 +525,7 @@ def expand_as_sf(self, n, alphabet='x'):
expanded_poly_ring = self.coefficient(0).expand(n, alphabet).parent()
LPSR = LazyPowerSeriesRing(expanded_poly_ring)

expander_gen = (LPSR.term(self.coefficient(i).expand(n, alphabet), i) for i in _integers_from(0))
expander_gen = (LPSR.term(self.coefficient(i).expand(n, alphabet), i) for i in NN)

return LPSR.sum_generator(expander_gen)

Expand Down Expand Up @@ -633,7 +628,7 @@ def __invert__(self):
def multinv_builder(i):
return self.coefficient(0)**(-i-1) * (self.coefficient(0) + (-1)*self)**i

return self.parent().sum_generator(multinv_builder(i) for i in _integers_from(0))
return self.parent().sum_generator(multinv_builder(i) for i in NN)

@coerce_binop
def __div__(self, y):
Expand Down Expand Up @@ -823,7 +818,7 @@ def arith_prod_coeff(n):

# Finally, we use the sum_generator method to assemble these results into a single
# LazyPowerSeries object.
return self.parent().sum_generator(arith_prod_coeff(n) for n in _integers_from(0))
return self.parent().sum_generator(arith_prod_coeff(n) for n in NN)

def _cycle_type(self, s):
"""
Expand Down Expand Up @@ -928,7 +923,7 @@ def recursive_stream(self):
[p[1], p[1, 1] + p[2], p[1, 1, 1] + p[2, 1] + p[3]]
"""
g = (self._compose_term(self._outer[i], self._y_powers)
for i in _integers_from(0))
for i in NN)
res = SumGeneratorStream(g, base_ring=self._base_ring)
return res

Expand All @@ -947,7 +942,6 @@ def _compose_term(self, p, y_powers):
EXAMPLES::
sage: from sage.combinat.species.stream import Stream
sage: E = species.SetSpecies(); C = species.CycleSpecies()
sage: E_cis = E.cycle_index_series()
sage: C_cis = C.cycle_index_series()
Expand Down Expand Up @@ -994,7 +988,7 @@ def __init__(self, outer, inner, inner_species, **kwds):

def recursive_stream(self):
g = (self._weighted_compose_term(self._outer[i], self._inner_species)
for i in _integers_from(0))
for i in NN)
return SumGeneratorStream(g, base_ring=self._base_ring)

def _weighted_compose_term(self, p, y_species):
Expand Down
10 changes: 5 additions & 5 deletions src/sage/combinat/species/new_stream.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sage.structure.sage_object import SageObject
from sage.misc.misc import is_iterator

# TODO:
# 1. __len__ / number_computed / max_computed??
Expand Down Expand Up @@ -299,14 +300,13 @@ def __init__(self, iterator=None, **kwds):
EXAMPLES::
sage: from sage.combinat.species.new_stream import StreamFromIterator
sage: from sage.combinat.species.stream import _integers_from
sage: s = StreamFromIterator(_integers_from(1))
sage: s = StreamFromIterator(iterator=NN)
sage: s[0]
1
0
sage: s[10]
11
10
"""
self._it = iterator
self._it = iterator if is_iterator(iterator) else iter(iterator)
super(StreamFromIterator, self).__init__(**kwds)

def compute(self, n):
Expand Down
6 changes: 4 additions & 2 deletions src/sage/combinat/species/partition_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
#*****************************************************************************

from species import GenericCombinatorialSpecies, SpeciesSeriesStream
from generating_series import _integers_from, factorial_stream
from generating_series import factorial_stream
from subset_species import SubsetSpeciesStructure
from set_species import SetSpecies
from structure import GenericSpeciesStructure
from sage.structure.unique_representation import UniqueRepresentation
from sage.rings.all import ZZ
from sage.sets.all import PositiveIntegers
from sage.combinat.species.misc import accept_size

class PartitionSpeciesStructure(GenericSpeciesStructure):
Expand Down Expand Up @@ -269,9 +270,10 @@ def _cis(self, series_ring, base_ring):
5/6*p[1, 1, 1] + 3/2*p[2, 1] + 2/3*p[3],
5/8*p[1, 1, 1, 1] + 7/4*p[2, 1, 1] + 7/8*p[2, 2] + p[3, 1] + 3/4*p[4]]
"""
PP = PositiveIntegers()
ciset = SetSpecies().cycle_index_series(base_ring)
CIS = ciset.parent()
res = CIS.sum_generator(((1/n)*ciset).stretch(n) for n in _integers_from(ZZ(1))).exponential()
res = CIS.sum_generator(((1/n)*ciset).stretch(n) for n in PP).exponential()
if self.is_weighted():
res *= self._weight
return res
Expand Down
53 changes: 16 additions & 37 deletions src/sage/combinat/species/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
This file provides an implementation of lazy univariate power
series, which uses the stream class for its internal data
structure. The lazy power series keep track of their approximate
order as much as possible without forcing the computation of any
additional coefficients. This is required for recursively defined
power series.
structure.
This code is based on the work of Ralf Hemmecke and Martin Rubey's
Aldor-Combinat, which can be found at
Expand All @@ -28,8 +25,6 @@
#
# http://www.gnu.org/licenses/
#*****************************************************************************
#from stream import Stream, Stream_class
from new_stream import OldStreamBehavior as Stream, Stream as Stream_class
from series_order import inf, unk
from sage.rings.all import Integer
from sage.misc.misc import repr_lincomb, is_iterator
Expand All @@ -46,9 +41,6 @@
from sage.algebras.algebra import Algebra
from sage.categories.algebras import Algebras




class LazyPowerSeriesRing(UniqueRepresentation, Algebra):
def __init__(self, R, element_class=None, category=None, names=None):
"""
Expand Down Expand Up @@ -159,7 +151,6 @@ def _element_constructor_(self, x=None, order=unk):
"""
EXAMPLES::
sage: from sage.combinat.species.stream import Stream
sage: L = LazyPowerSeriesRing(QQ)
sage: L()
O(1)
Expand All @@ -169,8 +160,16 @@ def _element_constructor_(self, x=None, order=unk):
[0, 1, -1, 2, -2, 3, -3, 4, -4, 5]
sage: L(iter(ZZ)).coefficients(10)
[0, 1, -1, 2, -2, 3, -3, 4, -4, 5]
sage: L(Stream(ZZ)).coefficients(10)
::
sage: from sage.combinat.species.series_stream import SeriesStreamFromIterator
sage: L = LazyPowerSeriesRing(QQ)
sage: s = SeriesStreamFromIterator(iterator=ZZ, base_ring=QQ, convert=True)
sage: L(s).coefficients(10)
[0, 1, -1, 2, -2, 3, -3, 4, -4, 5]
sage: _[0].parent()
Rational Field
::
Expand Down Expand Up @@ -218,13 +217,17 @@ def _element_constructor_(self, x=None, order=unk):
new_ring=base_ring)


if isinstance(x, SeriesStream):
assert x.base_ring() == base_ring
return cls(self, x)

if isinstance(x, (list, tuple)):
x = SeriesStreamFromList(list=x, base_ring=base_ring)
elif hasattr(x, "__iter__") and not isinstance(x, Stream_class):
elif hasattr(x, "__iter__"):
x = iter(x)

if is_iterator(x):
x = SeriesStreamFromIterator(iterator=x, base_ring=base_ring)
x = SeriesStreamFromIterator(iterator=x, base_ring=base_ring, convert=True)

if isinstance(x, SeriesStream):
return cls(self, x)
Expand Down Expand Up @@ -311,29 +314,6 @@ def sum(self, a):
"""
return self._new(ListSumStream, [x._stream for x in a])

#Potentially infinite sum
def _sum_generator_gen(self, g):
"""
EXAMPLES::
sage: L = LazyPowerSeriesRing(QQ)
sage: s = L([1])
sage: def f():
... while True:
... yield s
sage: g = L._sum_generator_gen(f())
sage: [g.next() for i in range(10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
s = Stream(g)
n = 0
while True:
r = s[n].coefficient(n)
for i in range(min(n, len(s)-1)):
r += s[i].coefficient(n)
yield r
n += 1

def sum_generator(self, g):
"""
EXAMPLES::
Expand Down Expand Up @@ -1017,7 +997,6 @@ def iterator(self, n=0, initial=None):
EXAMPLES::
sage: from sage.combinat.species.stream import Stream
sage: L = LazyPowerSeriesRing(QQ)
sage: f = L(range(10))
sage: g = f.iterator(2)
Expand Down
12 changes: 10 additions & 2 deletions src/sage/combinat/species/series_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class SeriesStream(ListCachedStream):
def __init__(self, order=unk, aorder=unk, base_ring=None,
aorder_changed=True, **kwds):
aorder_changed=True, convert=False, **kwds):
assert base_ring is not None
self._base_ring = base_ring
self.aorder = aorder
Expand All @@ -31,6 +31,7 @@ def __init__(self, order=unk, aorder=unk, base_ring=None,
self.order = inf
self.aorder_changed = aorder_changed
self._zero = base_ring(0)
self._convert = convert
self._children = kwds.pop('children', [])
super(SeriesStream, self).__init__(**kwds)

Expand All @@ -44,7 +45,14 @@ def __getitem__(self, n):
# because comparison of Integer and InfinityOrder is not implemented.
if self.get_aorder() > n:
return self._zero
return super(SeriesStream, self).__getitem__(n)
result = super(SeriesStream, self).__getitem__(n)
if self._convert:
return self._base_ring(result)
else:
return result

def base_ring(self):
return self._base_ring

def children(self):
return self._children
Expand Down
7 changes: 4 additions & 3 deletions src/sage/combinat/species/set_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
# http://www.gnu.org/licenses/
#*****************************************************************************
from species import GenericCombinatorialSpecies, SpeciesSeriesStream
from generating_series import factorial_stream, _integers_from
from generating_series import factorial_stream
from series import SeriesStreamFromList
from sage.combinat.species.structure import GenericSpeciesStructure
from sage.combinat.species.misc import accept_size
from sage.combinat.sf.sf import SymmetricFunctions
from sage.structure.unique_representation import UniqueRepresentation
from sage.sets.all import PositiveIntegers

class SetSpeciesStructure(GenericSpeciesStructure):
def __repr__(self):
Expand Down Expand Up @@ -201,10 +203,9 @@ def _cis_gen(self, base_ring):
sage: [g.next() for i in range(5)]
[0, p[1], 1/2*p[2], 1/3*p[3], 1/4*p[4]]
"""
from sage.combinat.sf.sf import SymmetricFunctions
p = SymmetricFunctions(base_ring).power()
yield p(0)
for n in _integers_from(1):
for n in PositiveIntegers():
yield p([n])/n


Expand Down
5 changes: 3 additions & 2 deletions src/sage/combinat/species/subset_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#*****************************************************************************

from species import GenericCombinatorialSpecies, SpeciesSeriesStream
from generating_series import _integers_from, factorial_stream
from generating_series import factorial_stream
from structure import GenericSpeciesStructure
from sage.rings.all import ZZ
from sage.sets.all import PositiveIntegers
from sage.combinat.species.misc import accept_size
from sage.structure.unique_representation import UniqueRepresentation

Expand Down Expand Up @@ -237,7 +238,7 @@ def _cis_gen(self, base_ring):
from sage.combinat.sf.sf import SymmetricFunctions
p = SymmetricFunctions(base_ring).power()
yield base_ring(0)
for n in _integers_from(ZZ(1)):
for n in PositiveIntegers():
yield 2*p([n])/n

#Backward compatibility
Expand Down

0 comments on commit 6525200

Please sign in to comment.