import numpy as np
a = [0.1, 0.2, 0.3, 0.4]
a
is any variable name.a[0]
, a[1]
, etc.0
in a[0]
is the index of the array.a = [0.1, 0.2, 0.3, 0.4]
print(a) # prints the whole array
print(a[0]) # print element 0
print(a[1]) # print element 1
print(a[2]) # etc.
print(a[3])
[0.1, 0.2, 0.3, 0.4] 0.1 0.2 0.3 0.4
x = a[1] # store element value as a separate variable
y = a[2]
print(x,y)
0.2 0.3
a[2] = 1000*a[2] # replace a[2] with 1000 times its current value
print(a)
[0.1, 0.2, 300.0, 0.4]
b = [5, 3.14, "timpanogos", np.sin]
print(b)
print(b[3](np.pi/4))
[5, 3.14, 'timpanogos', <ufunc 'sin'>] 0.707106781187
len
function
a = [1.1, 2.2, 3.3, 4.4]
print( "length of a is", len(a) )
for i in range(len(a)):
print(a[i])
length of a is 4 1.1 2.2 3.3 4.4
array
linspace
, arange
zeros
, ones
, empty
x = np.array([1,2,3,4]) # 1-D array, commas between elements
print(x)
[1 2 3 4]
A = np.array([[1,2],[3,4]]) # 2-D array
print(A[1,1])
a = np.array([1,2,3])
b = np.array([4,5,6])
A = np.array([a,b])
A
4
array([[1, 2, 3], [4, 5, 6]])
np.linspace(2,8,5) # 2 to 8 (inclusive) using 5 evenly spaced points
array([ 2. , 3.5, 5. , 6.5, 8. ])
x = np.arange(3,21,3.3) # 1-D array using arange
print(x) # Like range, but can have non-integer step size
[ 3. 6.3 9.6 12.9 16.2 19.5]
np.zeros(4) # zeros is good for initializing
array([ 0., 0., 0., 0.])
np.ones(6) # ones is good too: like x = 5.0*np.ones(6)
array([ 1., 1., 1., 1., 1., 1.])
a = np.empty(4) # empty makes space, but arbitrary values (faster)
a
array([4.9e-324, 9.9e-324, 1.5e-323, 2.0e-323])
x[i,j]
is row i
, column j
x[-1]
is the last elementx[-2]
is the second to the last element, etc.x = np.arange(3,21,3) # x is [3 6 9 12 15 18]
print(x)
x[2] # indicies are 0 1 2 3 4 5
[ 3 6 9 12 15 18]
9
print(x)
x[[1,5,3]] # array composed of elements 1, 5, 3 of x
# indexing an array with another array
i = np.array([1,5,4])
x[i]
[ 3 6 9 12 15 18]
array([ 6, 18, 15])
x = np.array([[1,2,3],[4,5,6]]) # 2D array
print(x)
print(x[1,2]) # value is 6
[[1 2 3] [4 5 6]] 6
a[s:e]
s
is the start (optional)e
is the end (optional)x = np.arange(0,11,1.5)
print(x[:]) # get all elements
print(x[2:]) # get indices 2 to the end
print(x[:-2]) # get elements start to one before 2nd to last (stops short)
print(x[2:5]) # get indices 2 to 4 (stops short of 5)
[ 0. 1.5 3. 4.5 6. 7.5 9. 10.5] [ 3. 4.5 6. 7.5 9. 10.5] [0. 1.5 3. 4.5 6. 7.5] [3. 4.5 6. ]
x = np.array([[1,2,3,4],[7,8,9,10]])
print(x)
np.shape(x)
[[ 1 2 3 4] [ 7 8 9 10]]
(2, 4)
np.size(x)
8
len(x)
2
x = np.array([1,2,3,4])
len(x)
4
a = np.arange(9)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
#------- THIS
b = 0.1 * a
#------- NOT THIS
b = np.empty(len(a))
for i in range(len(b)):
b[i] = 0.1*a[i]
#-------
print(b)
[ 3.10503618e+230 -1.73059901e-078 6.93645382e-311 2.78134232e-310]
a+b
array([0. , 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8])
a**2
array([1, 4, 9])
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([1,2,3])
x = np.linalg.solve(A,b) # solve
print("x = ",x)
print("b = ", np.dot(A,x)) # verify that A*x = b
x = [-0.33333333 0.66666667 -0. ] b = [ 1. 2. 3.]
x = np.array([1,2,3,4])
y = x # note, y is now just a reference to x.
y[3] = 44 # if we change an element of y, it will change in x too!
print(x) # this is not usually what we want
print(y)
[ 1 2 3 44] [ 1 2 3 44]
x = np.array([1,2,3,4])
y = x.copy() # now x and y are two different objects
y[3] = 44 # could also do y = np.array(x)
print(x)
print(y)
[1 2 3 4] [ 1 2 3 44]
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0,20,1000)
y = np.exp(-x/5)*np.sin(x)
plt.plot(x,y);
We have an array of times $t$, and we want the array of differences $dt$ between times.
t = np.array([
0.01136646, 0.01655847, 0.02548247, 0.07698612, 0.17964401,
0.19813924, 0.22419994, 0.28622096, 0.28884997, 0.3627257 ,
0.42555072, 0.44965767, 0.47257237, 0.59335946, 0.73842352,
0.85900369, 0.88851226, 0.89153229, 0.89355469, 0.97705246,
])
dt = np.empty(len(t)-1)
for i in range(len(dt)):
dt[i] = t[i+1]-t[i]
dt = t[1:] - t[:-1]
dt
array([0.00519201, 0.008924 , 0.05150365, 0.10265789, 0.01849523, 0.0260607 , 0.06202102, 0.00262901, 0.07387573, 0.06282502, 0.02410695, 0.0229147 , 0.12078709, 0.14506406, 0.12058017, 0.02950857, 0.00302003, 0.0020224 , 0.08349777])
where $\Delta x = x_1-x_0$.
, $$k=2.$$