sagemath / sage

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

LazyPowerSeriesRing.sum not working #37386

Open mantepse opened 8 months ago

mantepse commented 8 months ago

Steps To Reproduce

define:

R.<p, q> = QQ[]
L.<x> = LazyPowerSeriesRing(R)
@cached_function
def D_le(h):
    if not h:
        return 1
    P = L.undefined()
    P.define(x*(D_le(h-1) - 1 + p)*P + 1)
    return P

def D_eq(h):
    if not h:
        return 1
    return D_le(h) - D_le(h-1)

D = L.sum(lambda h: q^h*D_eq(h), 0)

Expected Behavior

sage: D
1 + p*q*x + ((p^2*q+p*q^2)*x^2) + ((p^3*q+3*p^2*q^2+p*q^3)*x^3) + ((p^4*q+6*p^3*q^2+5*p^2*q^3+p*q^4+p^2*q^2)*x^4) + ((p^5*q+10*p^4*q^2+15*p^3*q^3+7*p^2*q^4+p*q^5+5*p^3*q^2+3*p^2*q^3)*x^5) + ((p^6*q+15*p^5*q^2+35*p^4*q^3+28*p^3*q^4+9*p^2*q^5+p*q^6+15*p^4*q^2+21*p^3*q^3+5*p^2*q^4+p^3*q^2+p^2*q^3)*x^6) + O(x^7)

Actual Behavior

we actually have

sage: D
<repr(<sage.rings.lazy_series_ring.LazyPowerSeriesRing_with_category.element_class at 0x7f670bf99cc0>) failed: AttributeError: 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular' object has no attribute '_coeff_stream'. Did you mean: '_coeff_repr'?>
sage: D
p*q*x + ((p^2*q+p*q^2)*x^2) + ((p^3*q+3*p^2*q^2+p*q^3)*x^3) + ((p^4*q+6*p^3*q^2+5*p^2*q^3+p*q^4+p^2*q^2)*x^4) + ((p^5*q+10*p^4*q^2+15*p^3*q^3+7*p^2*q^4+p*q^5+5*p^3*q^2+3*p^2*q^3)*x^5) + ((p^6*q+15*p^5*q^2+35*p^4*q^3+28*p^3*q^4+9*p^2*q^5+p*q^6+15*p^4*q^2+21*p^3*q^3+5*p^2*q^4+p^3*q^2+p^2*q^3)*x^6) + O(x^7)

Additional Information

A workaround is to define

D = L.sum(lambda h: L(q^h)*D_eq(h), 0)

Environment

- **Sage Version**: 10.3.beta8

Checklist

tscrim commented 7 months ago

Strictly speaking, this isn't a bug but invalid input. The base case is not returning an element in L, and the input to sum is expecting an element of L from the function. So if you change the return 1 -> return L.one(), it works.

That being said, it would be desirable to automatically coerce (convert?) the inputs into L, but that would require the stream to know something about the parent.