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