1
2
3
4
5
6
7
8
9
10 """funcutils.py -- Subroutines that tabulate a function's values.
11
12 Convenience functions that evaluate a python function on a grid of
13 points and tabulate the output to be used with Gnuplot.
14
15 """
16
17 __cvs_version__ = '$Revision: 2.5 $'
18
19 try:
20 import Numeric
21 except ImportError:
22 import numpy as Numeric
23
24 import Gnuplot, utils
25
26
28 """Evaluate and tabulate a function on a 1- or 2-D grid of points.
29
30 f should be a function taking one or two floating-point
31 parameters.
32
33 If f takes one parameter, then xvals should be a 1-D array and
34 yvals should be None. The return value is a Numeric array
35 '[f(x[0]), f(x[1]), ..., f(x[-1])]'.
36
37 If f takes two parameters, then 'xvals' and 'yvals' should each be
38 1-D arrays listing the values of x and y at which 'f' should be
39 tabulated. The return value is a matrix M where 'M[i,j] =
40 f(xvals[i],yvals[j])', which can for example be used in the
41 'GridData' constructor.
42
43 If 'ufunc=0', then 'f' is evaluated at each point using a Python
44 loop. This can be slow if the number of points is large. If
45 speed is an issue, you should write 'f' in terms of Numeric ufuncs
46 and use the 'ufunc=1' feature described next.
47
48 If called with 'ufunc=1', then 'f' should be a function that is
49 composed entirely of ufuncs (i.e., a function that can operate
50 element-by-element on whole matrices). It will be passed the
51 xvals and yvals as rectangular matrices.
52
53 """
54
55 if yvals is None:
56
57 xvals = Numeric.asarray(xvals, typecode)
58
59 if ufunc:
60 return f(xvals)
61 else:
62 if typecode is None:
63 typecode = xvals.typecode()
64
65 m = Numeric.zeros((len(xvals),), typecode)
66 for xi in range(len(xvals)):
67 x = xvals[xi]
68 m[xi] = f(x)
69 return m
70 else:
71
72 xvals = Numeric.asarray(xvals, typecode)
73 yvals = Numeric.asarray(yvals, typecode)
74
75 if ufunc:
76 return f(xvals[:,Numeric.NewAxis], yvals[Numeric.NewAxis,:])
77 else:
78 if typecode is None:
79
80
81 typecode = (Numeric.zeros((1,), xvals.typecode()) +
82 Numeric.zeros((1,), yvals.typecode())).typecode()
83
84 m = Numeric.zeros((len(xvals), len(yvals)), typecode)
85 for xi in range(len(xvals)):
86 x = xvals[xi]
87 for yi in range(len(yvals)):
88 y = yvals[yi]
89 m[xi,yi] = f(x,y)
90 return m
91
92
93
94 grid_function = tabulate_function
95
96
98 """Evaluate a function of 1 variable and store the results in a Data.
99
100 Computes a function f of one variable on a set of specified points
101 using 'tabulate_function', then store the results into a 'Data' so
102 that it can be plotted. After calculation, the data are written
103 to a file; no copy is kept in memory. Note that this is quite
104 different than 'Func' (which tells gnuplot to evaluate the
105 function).
106
107 Arguments:
108
109 'xvals' -- a 1-d array with dimension 'numx'
110
111 'f' -- the function to plot--a callable object for which
112 f(x) returns a number.
113
114 'ufunc=<bool>' -- evaluate 'f' as a ufunc?
115
116 Other keyword arguments are passed through to the Data
117 constructor.
118
119 'f' should be a callable object taking one argument. 'f(x)' will
120 be computed at all values in xvals.
121
122 If called with 'ufunc=1', then 'f' should be a function that is
123 composed entirely of ufuncs, and it will be passed the 'xvals' and
124 'yvals' as rectangular matrices.
125
126 Thus if you have a function 'f', a vector 'xvals', and a Gnuplot
127 instance called 'g', you can plot the function by typing
128 'g.splot(compute_Data(xvals, f))'.
129
130 """
131
132 xvals = utils.float_array(xvals)
133
134
135 data = tabulate_function(f, xvals, ufunc=ufunc)
136
137 return apply(Gnuplot.Data, (xvals, data), keyw)
138
139
141 """Evaluate a function of 2 variables and store the results in a GridData.
142
143 Computes a function 'f' of two variables on a rectangular grid
144 using 'tabulate_function', then store the results into a
145 'GridData' so that it can be plotted. After calculation the data
146 are written to a file; no copy is kept in memory. Note that this
147 is quite different than 'Func' (which tells gnuplot to evaluate
148 the function).
149
150 Arguments:
151
152 'xvals' -- a 1-d array with dimension 'numx'
153
154 'yvals' -- a 1-d array with dimension 'numy'
155
156 'f' -- the function to plot--a callable object for which
157 'f(x,y)' returns a number.
158
159 'ufunc=<bool>' -- evaluate 'f' as a ufunc?
160
161 Other keyword arguments are passed to the 'GridData' constructor.
162
163 'f' should be a callable object taking two arguments.
164 'f(x,y)' will be computed at all grid points obtained by
165 combining elements from 'xvals' and 'yvals'.
166
167 If called with 'ufunc=1', then 'f' should be a function that is
168 composed entirely of ufuncs, and it will be passed the 'xvals' and
169 'yvals' as rectangular matrices.
170
171 Thus if you have a function 'f' and two vectors 'xvals' and
172 'yvals' and a Gnuplot instance called 'g', you can plot the
173 function by typing 'g.splot(compute_GridData(f, xvals, yvals))'.
174
175 """
176
177 xvals = utils.float_array(xvals)
178 yvals = utils.float_array(yvals)
179
180
181 data = tabulate_function(f, xvals, yvals, ufunc=ufunc)
182
183 return apply(Gnuplot.GridData, (data, xvals, yvals), keyw)
184
185
186
189