Script execution
This commit is contained in:
@@ -318,4 +318,46 @@ for i in range(4):
|
||||
# [ 8 8 10]
|
||||
# [11 11 13]]
|
||||
print(y)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
################################################################################
|
||||
# Polynomial
|
||||
################################################################################
|
||||
|
||||
p = np.poly1d([1, 2, 3])
|
||||
|
||||
print p(0.5) #4.25
|
||||
#Roots
|
||||
print p.r #([-1.+1.41421356j, -1.-1.41421356j])
|
||||
print p(p.r)
|
||||
|
||||
|
||||
#Show the coefficients:
|
||||
print p.c #array([1, 2, 3])
|
||||
|
||||
#Display the order (the leading zero-coefficients are removed):
|
||||
print p.order #2
|
||||
#Show the coefficient of the k-th power in the polynomial (which is equivalent to p.c[-(i+1)]):
|
||||
|
||||
print p[1] #2
|
||||
#Polynomials can be added, subtracted, multiplied, and divided (returns quotient and remainder):
|
||||
|
||||
print p * p #poly1d([ 1, 4, 10, 12, 9])
|
||||
print (p**3 + 4) / p #(poly1d([ 1., 4., 10., 12., 9.]), poly1d([4.]))
|
||||
#asarray(p) gives the coefficient array, so polynomials can be used in all functions that accept arrays:
|
||||
|
||||
print p**2 # square of polynomial
|
||||
print poly1d([ 1, 4, 10, 12, 9])
|
||||
print np.square(p) # square of individual coefficients array([1, 4, 9])
|
||||
#The variable used in the string representation of p can be modified, using the variable parameter:
|
||||
|
||||
p = np.poly1d([1,2,3], variable='z')
|
||||
print(p) # 2
|
||||
# 1 z + 2 z + 3
|
||||
#Construct a polynomial from its roots:
|
||||
print np.poly1d([1, 2], True) #poly1d([ 1., -3., 2.])
|
||||
|
||||
3This is the same polynomial as obtained by:
|
||||
print np.poly1d([1, -1]) * np.poly1d([1, -2])
|
||||
|
||||
Reference in New Issue
Block a user