Ask Your Question
0

How to calculate the value of polynomials with large coefficients

asked 2019-09-25 23:18:29 +0100

captain gravatar image
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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2019-09-26 00:27:52 +0100

tmonteil gravatar image

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
edit flag offensive delete link more

Comments

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?

captain gravatar imagecaptain ( 2019-09-27 10:06:34 +0100 )edit
1

answered 2019-09-26 01:45:06 +0100

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'>
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: 2019-09-25 23:18:29 +0100

Seen: 402 times

Last updated: Sep 26 '19