Script execution

This commit is contained in:
2019-12-13 08:49:20 +01:00
parent 669a86bd32
commit d4269a3880

View File

@@ -344,13 +344,15 @@ print p.order #2
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.]))
###TODO: Not supported
#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 np.square(p) # square of individual coefficients array([1, 4, 9])
#print p**2 # square of polynomial
print np.poly1d([ 1, 4, 10, 12, 9])
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')
@@ -359,5 +361,49 @@ print(p) # 2
#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])
###TODO: Not supported
#This is the same polynomial as obtained by:
#print np.poly1d([1, -1]) * np.poly1d([1, -2])
coeff = [3.2, 2, 1]
print np.roots(coeff) #array([-0.3125+0.46351241j, -0.3125-0.46351241j])
################################################################################
# Fit
################################################################################
x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
z = np.fit.polyfit(x, y, 3)
print z #array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254]) # may vary
p = np.poly1d(z)
p(0.5) #0.6143849206349179
################################################################################
# Convolution
################################################################################
#Note how the convolution operator flips the second array before “sliding” the two across one another:
print np.convolve([1, 2, 3], [0, 1, 0.5]) #array([0. , 1. , 2.5, 4. , 1.5])
#Only return the middle values of the convolution. Contains boundary effects, where zeros are taken into account:
print np.convolve([1,2,3],[0,1,0.5], 'same') #array([1. , 2.5, 4. ])
#The two arrays are of the same length, so there is only one position where they completely overlap:
print np.convolve([1,2,3],[0,1,0.5], 'valid') #array([2.5])
print np.correlate([1, 2, 3], [0, 1, 0.5]) #array([3.5])
print np.correlate([1, 2, 3], [0, 1, 0.5], "same") #array([2. , 3.5, 3. ])
print np.correlate([1, 2, 3], [0, 1, 0.5], "full") #array([0.5, 2. , 3.5, 3. , 0. ])
###TODO: Not supported
#Using complex sequences:
#print np.correlate([1+1j, 2, 3-1j], [0, 1, 0.5j], 'full') #array([ 0.5-0.5j, 1.0+0.j , 1.5-1.5j, 3.0-1.j , 0.0+0.j ])
#Note that you get the time reversed, complex conjugated result when the two input sequences change places, i.e., c_{va}[k] = c^{*}_{av}[-k]:
#print np.correlate([0, 1, 0.5j], [1+1j, 2, 3-1j], 'full') #array([ 0.0+0.j , 3.0+1.j , 1.5+1.5j, 1.0+0.j , 0.5+0.5j])