






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A chapter from a textbook on numerical computing with Python, focusing on array computing and curve plotting. It explains the concept of arrays, their advantages over lists, and how to visualize functions by plotting curves. the basics of array computations, vectorization, and the use of NumPy arrays for efficient storage and computation. It also discusses the importance of vectorization for faster and more readable code.
What you will learn
Typology: Study Guides, Projects, Research
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Simula Research Laboratory^1
University of Oslo, Dept. of Informatics^2
Vectors and arrays are key concepts in this chapter. It takes separate math courses to understand what vectors and arrays really are, but in this course we only need a small subset of the complete story. A learning strategy may be to just start using vectors/arrays in programs and later, if necessary, go back to the more mathematical details in the rst part of Ch. 5.
def f(x): ... return x**
...
n = 5 # no of points dx = 1.0/(n-1) # x spacing in [0,1] xlist = [i*dx for i in range(n)] ylist = [f(x) for x in xlist]
pairs = [[x, y] for x, y in zip(xlist, ylist)]
import numpy as np # module for arrays x = np.array(xlist) # turn list xlist into array y = np.array(ylist)
n = 5 # number of points x = np.linspace(0, 1, n) # n points in [0, 1] y = np.zeros(n) # n zeros (float data type) for i in xrange(n): ... y[i] = f(x[i]) ...
from math import sin
for i in xrange(len(x)): y[i] = sin(x[i])
y = np.sin(x) # x: array, y: array
In [1]: n = 100000
In [2]: import numpy as np
In [3]: x = np.linspace(0, 2*np.pi, n+1)
In [4]: y = np.zeros(len(x))
In [5]: %timeit for i in xrange(len(x)):
y[i] = np.sin(x[i])*np.exp(-x[i]) 1 loops, best of 3: 247 ms per loop
In [6]: %timeit y = np.sin(x)*np.exp(-x) 100 loops, best of 3: 4.77 ms per loop
In [7]: 247/4. Out[7]: 51.781970649895186 # vectorization: 50x speed-up!
from numpy import sin, exp, linspace
def f(x): return x3 + sin(x)exp(-3x)
x = 1.2 # float object y = f(x) # y is float
x = linspace(0, 3, 10001) # 10000 intervals in [0,3] y = f(x) # y is array
import math, numpy x = numpy.linspace(0, 1, 11) math.sin(x[3])
math.sin(x) ... TypeError: only length-1 arrays can be converted to Python scalars numpy.sin(x) array([ 0. , 0.09983, 0.19866, 0.29552, 0.38941, 0.47942, 0.56464, 0.64421, 0.71735, 0.78332, 0.84147])
plot(t, y, xlabel='t', ylabel='y', legend='t^2*exp(-t^2)', axis=[0, 3, -0.05, 0.6], title='My First Easyviz Demo', savefig='tmp1.png', show=True) # display on the screen (default)
0.0 0.5 1.0 1.5t 2.0 2.5 3.
y
My First Matplotlib Demo t^2*exp(-t^2)
2
2
from scitools.std import * # curve plotting + array computing
def f1(t): return t2exp(-t*2)
def f2(t): return t*2f1(t)
t = linspace(0, 3, 51) y1 = f1(t) y2 = f2(t)
plot(t, y1) hold('on') # continue plotting in the same plot plot(t, y2)
xlabel('t') ylabel('y') legend('t^2exp(-t^2)', 't^4exp(-t^2)') title('Plotting two curves in the same plot') savefig('tmp2.png')
plot(t, y1, t, y2, xlabel='t', ylabel='y', legend=('t^2exp(-t^2)', 't^4exp(-t^2)'), title='Plotting two curves in the same plot', savefig='tmp2.pdf')
plot(t, y1) hold('on') plot(t, y2)
xlabel('t') ylabel('y') legend('t^2exp(-t^2)', 't^4exp(-t^2)') title('Plotting two curves in the same plot') savefig('tmp2.pdf')
0.0 0.5 1.0 1.5 2.0 2.5 3. t
y
Plotting two curves in the same plot
t^2exp(-t^2) t^4exp(-t^2)
plot(t, y1, 'r-') # red (r) line (-) hold('on') plot(t, y2, 'bo') # blue (b) circles (o)
plot(t, y1, 'r-', t, y2, 'bo')
Unix> pydoc scitools.easyviz
t = linspace(0, 3, 51) plot(t, t2exp(-t2), t, t4exp(-t**2))
Terminal> python plotf.py expression xmin xmax Terminal> python plotf.py "exp(-0.2x)sin(2pix)" 0 4*pi
from scitools.std import *
from numpy import * from matplotlib.pyplot import *
formula = sys.argv[1] xmin = eval(sys.argv[2]) xmax = eval(sys.argv[3])
x = linspace(xmin, xmax, 101) y = eval(formula) plot(x, y, title=formula)
0
1
2
-6 -4 -2 0 2 4 6
s=0. s= s=
0
1
2
-6 -4 -2 0 2 4 6
s=0.2 s= s=
from scitools.std import * import time
def f(x, m, s): return (1.0/(sqrt(2pi)s))exp(-0.5((x-m)/s)**2)
m = 0; s_start = 2; s_stop = 0. s_values = linspace(s_start, s_stop, 30)
x = linspace(m -3s_start, m + 3s_start, 1000)
max_f = f(m, m, s_stop)
import time frame_counter = 0
for s in s_values: y = f(x, m, s) plot(x, y, axis=[x[0], x[-1], -0.1, max_f], xlabel='x', ylabel='f', legend='s=%4.2f' % s, savefig='tmp_%04d.png' % frame_counter) frame_counter += 1 #time.sleep(0.2) # pause to control movie speed
def H(x): return (0 if x < 0 else 1)