如何在Python中使用 NumPy 将多项式除以另一个?
在本文中,我们将编写一个 NumPy 程序来将一个多项式除以另一个。给出两个多项式作为输入,结果是除法的商和余数。
- 多项式p(x) = C3 x2 + C2 x + C1在 NumPy 中表示为: ( C1, C2, C3 ) {系数(常数)}。
- 让我们取两个多项式 p(x) 和 g(x),然后将它们除以得到商 q(x) = p(x) // g(x) 和余数 r(x) = p(x) % g(x)因此。
If p(x) = A3 x2 + A2 x + A1
and
g(x) = B3 x2 + B2 x + B1
then result is
q(x) = p(x) // g(x) and r(x) = p(x) % g(x)
and the output is coefficientes of remainder and
the coefficientes of quotient.
这可以使用 polydiv() 方法计算。此方法计算两个多项式的除法并返回多项式除法的商和余数。
句法:
numpy.polydiv(p1, p2)
下面是一些示例的实现:
示例 1:
Python3
# importing package
import numpy
# define the polynomials
# p(x) = 5(x**2) + (-2)x +5
px = (5, -2, 5)
# g(x) = x +2
gx = (2, 1, 0)
# divide the polynomials
qx, rx = numpy.polynomial.polynomial.polydiv(px, gx)
# print the result
# quotiient
print(qx)
# remainder
print(rx)
Python3
# importing package
import numpy
# define the polynomials
# p(x) = (x**2) + 3x + 2
px = (1,3,2)
# g(x) = x + 1
gx = (1,1,0)
# divide the polynomials
qx,rx = numpy.polynomial.polynomial.polydiv(px,gx)
# print the result
# quotiient
print(qx)
# remainder
print(rx)
输出 :
[-12. 5.]
[ 29.]
示例 2:
Python3
# importing package
import numpy
# define the polynomials
# p(x) = (x**2) + 3x + 2
px = (1,3,2)
# g(x) = x + 1
gx = (1,1,0)
# divide the polynomials
qx,rx = numpy.polynomial.polynomial.polydiv(px,gx)
# print the result
# quotiient
print(qx)
# remainder
print(rx)
输出 :
[ 1. 2.]
[ 0.]