ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 27 Sep 2019 10:06:34 +0200How to calculate the value of polynomials with large coefficientshttps://ask.sagemath.org/question/48066/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?
Wed, 25 Sep 2019 23:18:29 +0200https://ask.sagemath.org/question/48066/how-to-calculate-the-value-of-polynomials-with-large-coefficients/Answer by John Palmieri for <pre><code>R = ZZ['x']
x = R.gen()
</code></pre>
<p>Under this setup, I define a function like <code>f(x) = 122313*x^23 + 445*x^12 + 2013*x + 2345</code> of high degree with large coefficients. Then, I define</p>
<pre><code>def h(x):
return diff(f(x),x)
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/48066/how-to-calculate-the-value-of-polynomials-with-large-coefficients/?answer=48070#post-id-48070Following 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'>Thu, 26 Sep 2019 01:45:06 +0200https://ask.sagemath.org/question/48066/how-to-calculate-the-value-of-polynomials-with-large-coefficients/?answer=48070#post-id-48070Answer by tmonteil for <pre><code>R = ZZ['x']
x = R.gen()
</code></pre>
<p>Under this setup, I define a function like <code>f(x) = 122313*x^23 + 445*x^12 + 2013*x + 2345</code> of high degree with large coefficients. Then, I define</p>
<pre><code>def h(x):
return diff(f(x),x)
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/48066/how-to-calculate-the-value-of-polynomials-with-large-coefficients/?answer=48068#post-id-48068Because 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
Thu, 26 Sep 2019 00:27:52 +0200https://ask.sagemath.org/question/48066/how-to-calculate-the-value-of-polynomials-with-large-coefficients/?answer=48068#post-id-48068Comment by captain for <p>Because of Sage prepersing, when you define <code>f(x)=...</code> you define a symbolic function, where the <code>x</code> is a symbol, not an element of the ring you just defined, see</p>
<pre><code>sage: parent(f)
Callable function ring with argument x
</code></pre>
<p>If you want to stay in <code>R</code>, you should define <code>f</code> as follows:</p>
<pre><code>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
</code></pre>
<p>Then you can define the derivative of <code>f</code> directly as:</p>
<pre><code>sage: h = f.derivative()
sage: h
2813199*x^22 + 5340*x^11 + 2013
</code></pre>
<p>and evauate it:</p>
<pre><code>sage: h(5)
6707189083360107423888
</code></pre>
https://ask.sagemath.org/question/48066/how-to-calculate-the-value-of-polynomials-with-large-coefficients/?comment=48079#post-id-48079Thank 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?Fri, 27 Sep 2019 10:06:34 +0200https://ask.sagemath.org/question/48066/how-to-calculate-the-value-of-polynomials-with-large-coefficients/?comment=48079#post-id-48079