From 669a86bd322c473f781673ec57604da2f39b9967 Mon Sep 17 00:00:00 2001 From: Alexandre Gobbo Date: Thu, 12 Dec 2019 15:58:12 +0100 Subject: [PATCH] Script execution --- script/scitest/numpy_test.py | 44 +++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/script/scitest/numpy_test.py b/script/scitest/numpy_test.py index 3d2003a..95afb86 100644 --- a/script/scitest/numpy_test.py +++ b/script/scitest/numpy_test.py @@ -318,4 +318,46 @@ for i in range(4): # [ 8 8 10] # [11 11 13]] print(y) - \ No newline at end of file + + + + +################################################################################ +# 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])