#Importing all the functions of NumPy from numpy import * #Importing NumPy as numpy import numpy #Importing NumPy with the alias of np (recommended) import numpy as np #Basic functions print np.exp(10.5), np.log(52.3), np.log10(63.9), np.sqrt(10.0) #Trigonometric functions print np.sin(5.0), np.cos(9.6), np.arcsin(0.5), np.arctan(5) print "The value of PI is %1.6f"%( np.pi ) x.append x.count x.extend x.index x.insert x.pop x.remove x.reverse x.sort x.T x.clip x.dot x.item x.prod x.setfield x.take x.all x.compress x.dtype x.itemset x.ptp x.setflags x.tofile x.any x.conj x.dump x.itemsize x.put x.shape x.tolist x.argmax x.conjugate x.dumps x.max x.ravel x.size x.tostring x.argmin x.copy x.fill x.mean x.real x.sort x.trace x.argsort x.ctypes x.flags x.min x.repeat x.squeeze x.transpose x.astype x.cumprod x.flat x.nbytes x.reshape x.std x.var x.base x.cumsum x.flatten x.ndim x.resize x.strides x.view x.byteswap x.data x.getfield x.newbyteorder x.round x.sum x.choose x.diagonal x.imag x.nonzero x.searchsorted x.swapaxes #Lists and numpy arrays can both store any type of data x1 = [1.2, 3.5, 1.9] x2 = np.array([1.6, -2.6, 6.9]) print x1, x2 #For lists, new elements can be added using append method x1 = [1.2, 3.5, 1.9] x1.append(5.9) #For arrays, new elements can be added using append function of NumPy x2 = np.array([1.6, -2.6, 6.9]) x2 = np.append(x2,3) print x1,x2 #A list can be converted into a numpy array x = [1.1,3.4,1.0] x = np.array(x) #And a numpy array into a list as well x = list(x) #Operator + for lists is overloaded for concatenating x1 = [1,2,3] x2 = [3,2,1] print x1+x2 #Operator + for numpy arrays is overloaded for adding x1 = np.array([1,2,3]) x2 = np.array([3,2,1]) print x1+x2 #Lists do not support other operators x1 = [1.2,4.8,6.9] x2 = [2.6,2.8,1.1] #Multiplication print x1*x2 #Division print x1/x2 #Subtraction print x1-x2 #Power print x1**x2 #Numpy arrays support any mathematical operation (element by element) x1 = np.array([1.2,4.8,6.9]) x2 = np.array([2.6,2.8,1.1]) print "Adding", x1+x2 print "Multiplication", x1*x2 print "Division", x1/x2 print "Subtraction", x1-x2 print "Power", x1**x2 A = np.array([[1,2],[3,4]]) B = np.array([[4,3],[2,1]]) print A print B print A*B #It is possible to access elements of a numpy array using booleans x = np.array([1,2,3,4]) y = np.array([True, False, True, False]) x[y] #Operators >, <, >=, <= and == are also overloaded for numpy arrays x = np.array([0,5,8,0]) y = np.array([0,6,5,1]) print x>y print x4 #Combining these features, we can perform searches and comparisons far more efficient x = np.array([1,4,2,6,8,4,3,0,9,1,3,6,7,]) #A new list with numbers greater than 4 print x[x>4] #Native methods of numpy arrays allow to calculate basic quantities x = np.array([1,4,2,6,8,4,3,0,9,1,3,6,7,]) #Maximum element print "Maximum element", x.max() #Minimum element print "Minimum element", x.min() #Sorted arguments of the array print "Sorted arguments", x.argsort() #Sorted array print "Sorted array", x[x.argsort()] #Mean value print "Mean value", x.mean() #Create an array of 1's with a given size (even 2D sizes) x = np.ones(5) print x #Create an array of zeros with a given size (even 2D sizes) x = np.zeros( (2,5) ) print x #Create an array with a given range and a number of intervals x = np.linspace( -np.pi, np.pi, 10 ) print x #Create an array with a given range and a given step x = np.arange( 1, 5, 0.2 ) print x #Using the function savetxt, it is possible to store data from a numpy array data = np.array([[3.2, 2.1],[3.1, 4.1]]) np.savetxt( "file.dat", data, fmt="%1.5e %1.5e" ) #In the same way, using the function loadtxt it is possible to load external data files data = np.loadtxt("file.dat") #Data is then a multidimensional array with the loaded data print data #Histograms are also useful and NumPy provides functions to do so x = [1,5,3,7,2,4,6,8,9,5,2,3,5,6,7,8,3,4,5] np.histogram(x) #Importing integrate package import scipy.integrate as integ integ.Tester integ.fixed_quad integ.odepack integ.quadrature integ.test integ.complex_ode integ.newton_cotes integ.quad integ.romb integ.tplquad integ.cumtrapz integ.ode integ.quad_explain integ.romberg integ.trapz integ.dblquad integ.odeint integ.quadpack integ.simps integ.vode