Ask Your Question
0

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

asked 2020-10-14 04:57:27 +0100

cybervigilante gravatar image

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 flag offensive 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?

cybervigilante gravatar imagecybervigilante ( 2020-10-14 06:03:06 +0100 )edit

Try

 [(a/b)(x=x) for x in [100..30000,step=100]]
FrédéricC gravatar imageFrédéricC ( 2020-10-14 08:32:53 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-10-14 08:35:25 +0100

Emmanuel Charpentier gravatar image

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...

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

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

Seen: 289 times

Last updated: Oct 14 '20