📌  相关文章
📜  如何在Python中使用 NumPy 将一个多项式减去另一个?

📅  最后修改于: 2022-05-13 01:54:35.364000             🧑  作者: Mango

如何在Python中使用 NumPy 将一个多项式减去另一个?

在本文中,让我们讨论如何将一个多项式与另一个相减。给出两个多项式作为输入,结果是两个多项式的减法。

  • 多项式p(x) = C3 x2 + C2 x + C1在 NumPy 中表示为: ( C1, C2, C3 ) {系数(常数)}。
  • 让两个多项式 p(x) 和 q(x) 相减,得到 r(x) = p(x) – q(x) 作为两个输入多项式相减的结果。
If p(x) = A3 x2 + A2 x + A1 
and
q(x) = B3 x2 + B2 x + B1 

then result is 
r(x) = p(x) - q(x) i.e;
r(x) = (A3 - B3) x2 + (A2 - B2) x + (A1 - B1) 

and output is 
( (A1 - B1), (A2 - B2), (A3 - B3) ).

在 NumPy 中,可以使用 polysub() 方法解决。此函数有助于找到两个多项式的差异,然后将结果作为多项式返回

下面是一些示例的实现:

示例 1:使用 polysub()

Python3
# importing package
import numpy
  
# define the polynomials
# p(x) = 5(x**2) + (-2)x +5
px = (5,-2,5)
  
# q(x) = 2(x**2) + (-5)x +2
qx = (2,-5,2)
  
# subtract the polynomials
rx = numpy.polynomial.polynomial.polysub(px,qx)
  
# print the resultant polynomial
print(rx)


Python3
# importing package
import numpy
  
# define the polynomials
# p(x) = 2.2
px = (0,0,2.2)
  
# q(x) = 9.8(x**2) + 4
qx = (9.8,0,4)
  
# subtract the polynomials
rx = numpy.polynomial.polynomial.polysub(px,qx)
  
# print the resultant polynomial
print(rx)


Python3
# importing package
import numpy
  
# define the polynomials
# p(x) = (5/3)x
px = (0,5/3,0)
  
# q(x) = (-7/4)(x**2) + (9/5)
qx = (-7/4,0,9/5)
  
# subtract the polynomials
rx = numpy.polynomial.polynomial.polysub(px,qx)
  
# print the resultant polynomial
print(rx)


输出 :

[ 3.  3.  3.]

示例 2: sub_with_decimals

Python3

# importing package
import numpy
  
# define the polynomials
# p(x) = 2.2
px = (0,0,2.2)
  
# q(x) = 9.8(x**2) + 4
qx = (9.8,0,4)
  
# subtract the polynomials
rx = numpy.polynomial.polynomial.polysub(px,qx)
  
# print the resultant polynomial
print(rx)

输出 :

[-9.8  0.  -1.8]

示例 3: #eval_then_sub

Python3

# importing package
import numpy
  
# define the polynomials
# p(x) = (5/3)x
px = (0,5/3,0)
  
# q(x) = (-7/4)(x**2) + (9/5)
qx = (-7/4,0,9/5)
  
# subtract the polynomials
rx = numpy.polynomial.polynomial.polysub(px,qx)
  
# print the resultant polynomial
print(rx)

输出 :

[ 1.75        1.66666667 -1.8       ]