# substituting vars for a poly quotient in a list comprehension blows up

If I run this but don't substitute the a and b for the polynomials:

a = 3*x^2 + 2*x + 1
b = 5*x^3 - 2*x^2 + 3
c = [((3*x^2 + 2*x + 1)/(5*x^3 - 2*x^2 + 3)).n() for x in [100..30000,step=100]]
for num in c:
print(num)


I get a numeric list, as expected.

If I use f = [(a/b) for x in [100..30000,step=100]] however, I only get variables in x, all the same: (3.00000000000000x^2 + 2.00000000000000x + 1.00000000000000)/(5.00000000000000x^3 - 2.00000000000000x^2 + 3.00000000000000)

And if I try (a/b).n(), I get an error: TypeError: unable to coerce to a ComplexNumber: <class 'sage.rings.fraction_field_element.fractionfieldelement_1poly_field'="">

What's going wrong?

edit retag close merge delete

This worked when I used f(x) and g(x) instead of a and b, but I thought I could just name a function that way. I then thought I could use f and g after defining functions, insstead of f(x) and g(x), but that didn't work either, although I think it does in plotting. Where am I misconceiving?

( 2020-10-14 06:03:06 +0200 )edit

Try

 [(a/b)(x=x) for x in [100..30000,step=100]]

( 2020-10-14 08:32:53 +0200 )edit

Sort by » oldest newest most voted

Try :

sage: a = 3*x^2 + 2*x + 1
sage: b = 5*x^3 - 2*x^2 + 3
sage: c=[(a/b).subs(x==u).n() for u in range(100, 30001, 100)]


What you tried is roughly :

sage: d=[(a/b)(u).n() for u in range(100, 30001, 100)]
<ipython-input-28-1031e54150fe>:1: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.


You can do :

sage: d=[(a/b)(x=u).n() for u in range(100, 30001, 100)]


successfully...

more