Ask Your Question

# 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

## Comments

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?

Try

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


## 1 Answer

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

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2020-10-14 04:57:27 +0200

Seen: 37 times

Last updated: Oct 14 '20