Some useful global variables:
Natural Numbers (semiring) | ℕ | NN |
---|---|---|
Integer Ring | ℤ | ZZ |
Rational Field | ℚ | |
Real Field | ℝ | RR |
Complex Field | ℂ | CC |
At first we create a polynomial ring over a field/ring
R.<x> = PolynomialRing(QQ)
R.<x> = QQ[]
Also a multivariate polynomial ring is possible
R.<x,y> = PolynomialRing(QQ)
Another useful way to get x
x = PolynomialRing(QQ, 'x').gen()
Now we can declare some polynomial:
p = x^3 + 0.3*x^2 - x - 0.3
p
How to evaluate the value at some given point a?
p(-0.1)
We can also factor p
p.factor()
At next we calculate the roots
p.roots()
A short introduction into plotting polynomials
plot(p)
In this example, the domain is chosen by sagemath. In the following, we supply minimum and maximum x-value explicitly:
var('s')
plot(p(s), (s, -5, 5))
Calculate the derivative
p_diff = p.diff()
p_diff
Combine plots and zoom in
plot(p(s), (s, -10, 10)) + plot(p_diff(s), (s, -10, 10), color='red')
Calculate the integral
p_int = p.integral()
p_int
Plot the integral and the polynomial
plot(p(s), (s, -10, 10)) + plot(p_int(s), (s, -10, 10), color='green')
One particular case of a polynomial is a power series. Sine is a trigonometric function defined as a power series:
x = var('x')
f = sin(x)
s = f.series(x, 5)
show(s)
show(s(x=5))
Now we can draw the sine and the polynomial to see the differences
plot(s.truncate(), xmin=-pi, xmax=pi) + plot(f, color='green', xmin=-pi, xmax=pi)