- Gnuplot is now in version 4.0 with a lot of syntax changes compared
with version 3.7. The examples in the book work smoothly if you install
Gnuplot 3.7.3. A few changes are necessary for Gnuplot 4.0.
For example, if the PNG terminal makes use of the new GD support,
the color option in set term png small color
in simviz1.py-like scripts is no longer valid.
The scripts in scripting-src.tgz and scripting-src.zip
have already been updated, but the particular set term png small color
commands in the book are only valid with Gnuplot 3.7 or the old PNG support.
At present, I don't know how well the Python Gnuplot module works with
Gnuplot 4.0.
- Page 56, bottom: ymin = None; ymin = None should be
ymin = None; ymax = None
- Page 76: A correct
root function, also valid for complex coefficients, could be
like this:
def roots(a, b, c):
"""
Return two roots of the quadratic algebraic equation
ax^2 + bx + c = 0, where a, b, and c may be complex.
"""
import cmath # complex functions
q = b*b - 4*a*c
r1 = -(b - cmath.sqrt(q))/(2*a)
r2 = -(b + cmath.sqrt(q))/(2*a)
# r1 and r2 are complex because cmath.sqrt returns complex,
# convert to real if possible:
if r1.imag == 0.0: r1 = r1.real
if r2.imag == 0.0: r2 = r2.real
if r1 == r2: r2 = None # use r2=None to indicate double root
return r1, r2
- Page 80: In the first for loop,
xlist[1] should be xlist[i].
- Page 87: The try-except statement gets a printout from
the try block even if except is invoked. A better
solution is
try: # program was found if program_path is defined
print '%s found in %s' % (program, program_path)
except:
print '%s not found' % program
- Page 90: In the first for loop,
filestr = file.read().replace should be filestr = f.read().replace.
- Page 96, next last paragraph: "... but the list is then
[4,3,1] ... and index 1 is then the elmeent 3" should
read
"... but the list is then
[4,2,1] ... and index 1 is then the elmeent 2".
- Page 101: elif should be else.
- Page 103: With the sum function in Python 2.3, a more
compact version of statistics can be written
def statistics(*args):
return sum(args)/float(len(args)), min(args), max(args)
- Page 104: somefunc(a, 'copy', 'True') should be
somefunc(a, 'copy', True), and in the printout
'install': 'yes' should be 'install': 'no'.
- Page 106: [0.0]*n should be [0.0]*10.
- Page 123: The definition of sequence should have
inc=1.0 (not inc=0.1).
- Page 131: The True and False values were exchanged.
Here is the correct interactive code segment:
>>> x = array([0.1,-0.4,2,5,6,-3])
>>> u = x < 0
>>> u # u[i] is 1 if x[i]<0
array([0, 1, 0, 0, 0, 1])
>>> bool(u) # result of if u:
True
>>> bool(zeros(6,Float)) # is [0,0,0,...] true?
False # no
>>> bool(array([-1,0]))
True # at least one element != 0 gives a true array
The boolean evaluation of an array only works with Numeric, not with
numarray.
- Page 145: g.vectorized_eval(f) should be g.vectorized_eval(myfunc).
- Page 152: yc = sin(x) should be yc = sin(xc).
The x variable is 2 (from the previous page) and hence
outside the xc grid unless we set (e.g.) xc = seq(0, 4, 0.05)
.
- Page 192: In Exercise 5.3, the MyFunc1 class displayed
in detail does not contain a t attribute, which makes the
example in the
main function somewhat confusing. The remedy is to ignore
the assignment f.t = M_PI.
- Page 184: Newer SWIG versions give a
TypeError when we pass s to a pointer argument in
the hw3 function.
- Page 190: The
%apply statement must have s_ not
just s.
- Page 231: The option -screenplot should be removed
from the cmd variable.
- Page 237: InputFields(root, status) should be
InputFields(root, status_line).
- Page 368: Method coor_mapping in
class TransFunc must take self as first argument.
Moreover, the a parameter has fallen out of formulas and
the implementation (the original code is for a=0).
The return sum in integrate also needs the
"Jacobian factor" 0.5*h. The corrected code becomes
def integrate(integrator, a, b, f, n):
# integrator is an instance of a subclass of Integrator
sum = 0.0
h = (b-a)/float(n)
g = TransFunc(f, h, a)
for j in range(1, n+1):
g.j = j
sum += integrator.eval(g)
return 0.5*h*sum
class TransFunc:
def __init__(self, f, h, a):
self.f = f; self.h = h; self.a = a
def coor_mapping(self, xi):
"""Map local xi in (-1,1) in interval j to global x."""
return self.a + (self.j-0.5)*self.h + 0.5*self.h*xi
def __call__(self, xi):
x = self.coor_mapping(xi)
return self.f(x)
Many thanks go to Mario Pernici (