# How to calculate the value of polynomials with large coefficients

R = ZZ['x']
x = R.gen()


Under this setup, I define a function like f(x) = 122313*x^23 + 445*x^12 + 2013*x + 2345 of high degree with large coefficients. Then, I define

def h(x):
return diff(f(x),x)


Then, Sage cannot compute the value of h, like h(5),h(10) etc. It returns 0 somehow. How can I fix this problem and get the values of the function h?

edit retag close merge delete

Sort by » oldest newest most voted

Because of Sage prepersing, when you define f(x)=... you define a symbolic function, where the x is a symbol, not an element of the ring you just defined, see

sage: parent(f)
Callable function ring with argument x


If you want to stay in R, you should define f as follows:

sage: R = ZZ['x']
....: x = R.gen()
sage: f = 122313*x^23 + 445*x^12 + 2013*x + 2345
sage: parent(f)
Univariate Polynomial Ring in x over Integer Ring


Then you can define the derivative of f directly as:

sage: h = f.derivative()
sage: h
2813199*x^22 + 5340*x^11 + 2013


and evauate it:

sage: h(5)
6707189083360107423888

more

Thank you! Now, I want to define a function as the following:

 f(k,x) = sum( (x+i)^k for i in (1..k))


How can I do that?

( 2019-09-27 03:06:34 -0600 )edit

Following on tmonteil's answer, you shouldn't mix the symbolic function f(x) = ... with a Python function def h(x): .... You can define f as a polynomial in ZZ['x'] as in the other answer, but you can also use the symbolic approach:

sage: f(x) = 122313*x^23 + 445*x^12 + 2013*x + 2345
sage: h(x) = diff(f, x)
sage: h(x)
2813199*x^22 + 5340*x^11 + 2013
sage: h(5)
6707189083360107423888


The two approaches when defining f:

sage: R.<x> = ZZ[]
sage: f = 122313*x^23 + 445*x^12 + 2013*x + 2345 # polynomial
sage: type(f)
<class 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>
sage: f(x) = 122313*x^23 + 445*x^12 + 2013*x + 2345 # symbolic
sage: type(f)
<class 'sage.symbolic.expression.Expression'>

more