Computation for Physics 計算物理概論 Introduction to NumPy and SciPy
List v.s. Array (numpy package) List – Variable length, you can add or remove elements. – Elements can be of different types. – Flexible. Array – The number of elements is fixed. – The elements must be of the same type. – Behave roughly like vectors and matrices. – Faster.
1D Array >>>from numpy import zeros >>>a = zeros(4, float) >>>print(a) [ ] >>>a = zeros(5, float) >>>print(a) [ ] >>>a = zeros(3,complex) >>>print(a)
2D Array >>>a = zeros([3,4],float) >>>print(a) [[ ] [ ] [ ]]
Array Initialization in NumPy zeros – Create an array with all elements equal to zero. ones – Create an array with all elements equal to one. empty – Create an empty array. (Faster.) – In practice the aren't empty. (Random stuff in the memory).
List Array List >>>r = [1.0, 1.5, -2.2] >>>a = array(r,float) >>>a = array([1.0, 1.5, -2.2], float) >>>print(a[0]) 1.0 >>>a = array([[1,2,3],[4,5,6]], int) >>>print(a) [[ 1 2 3] [ 4 5 6]] >>>print(a[ >>>r = list(a)
Accessing and Modifying Elements >>>a = array([1.0, 1.5, -2.2], float) >>>a[0] 1.0 >>>a[2]=4 (4 will be converted into float) >>>a = array([[1,2,3],[4,5,6]], int) >>>a[0][1] 2 >>>a[0][1]=0 >>>print(a) [[ 1 0 3] [ 4 5 6]]
Reading a 1D Array from a File values.txt >>>from numpy import loadtxt >>>a = loadtxt("values.txt", float) >>>print(a) [ ]
Reading a 2D Array from a File values.txt >>>from numpy import loadtxt >>>a = loadtxt("values.txt", float) >>>print(a) [ ]
Arithmetic with Arrays >>>a[0] = a[1] + 1 >>>x = a[2]**2 -2*a[3]/y >>>r = [1,2,3,4] >>>a = array(r, int) >>>s = 2*r >>>b = 2*a >>>print(s) [1,2,3,4,1,2,3,4] >>>print(b) [ ]
Add or Subtract Two Arrays >>>a = array([1,2,3,4],int) >>>b = array([2,4,6,8],int) >>>print(a+b) [ ] >>>print(b-a) [ ] (Two arrays must have the same size) >>>>print(a+1)
Multiply Two Arrays ≠ Dot Product >>>a = array([1,2,3,4],int) >>>b = array([2,4,6,8],int) >>>print(a*b) [ ] >>>print(b/a) [ ]
"Dot Product" of Two 1D Arrays="Inner Product" >>>from numpy import array, dot >>>a = array([1,2,3,4],int) >>>b = array([2,4,5,8],int) >>>print(dot(a,b)) 60
"Dot Product" of Two 2D Arrays = "Matrix Product" >>>a = array([[1,3],[2,4]],int) >>>b = array([[4,-2],[-3,1]],int) >>>c = array([[1,2],[2,1]],int) >>>print(dot(a,b)+2*c) [[-3 5] [ 0 2]]
Array Functions Most list functions can be applied to 1D array – sum – max – min – len You can also apply math functions >>>a = array([1,2,3,4],float) >>>print(sin(a)) [ ]
Array Methods >>>a = array([1,2,3,4],float) >>>a.size 4 >>>a.shape (4,) >>>a = array([[1,2,3],[4,5,6],int) >>>a.size 6 >>>a.shape (2,3)
Try: Average of a Set of Values in a File A set of numbers stored in a file values.txt. We don't know how many numbers there are. Want to calculate their mean.
Mean and Mean-Square from numpy import loadtxt values = loadtxt("values.txt",float) mean = sum(values)/len(values) mean_sqr = sum(values*values)/len(values) print(mean) print(mean_sqr)
Geometric Mean from numpy import loadtxt,log,exp from math import log,exp values = loadtxt("values.txt",float) geometric=exp(log(values)/len(values)) print(geometric)
Linspace Return evenly spaced numbers over a specified interval. >>>linspace(2.0, 3.0, num=5) array([ 2., 2.25, 2.5, 2.75, 3. ]) >>>linspace(2.0, 3.0, num=5, endpoint=False) array([ 2., 2.2, 2.4, 2.6, 2.8]) >>>linspace(2.0, 3.0, num=5, retstep=True) (array([ 2., 2.25, 2.5, 2.75, 3. ]), 0.25)
Numpy: random=random_sample() >>>random_sample() >>>random_sample(100)
Try: Random Number Input N. Generate N random numbers. Output the mean and the standard deviation.