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 try:
18 import numpy
19 except ImportError:
20
21 import numpypy
22 import numpy
23
24 from . import Gnuplot, utils, PlotItems
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 numpy 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 numpy 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 = numpy.asarray(xvals, dtype)
58
59 if ufunc:
60 return f(xvals)
61 else:
62 if dtype is None:
63 try:
64 dtype = xvals.dtype.char
65 except AttributeError:
66 dtype = xvals.typecode()
67
68 m = numpy.zeros((len(xvals),), dtype)
69 for xi in range(len(xvals)):
70 x = xvals[xi]
71 m[xi] = f(x)
72 return m
73 else:
74
75 xvals = numpy.asarray(xvals, dtype)
76 yvals = numpy.asarray(yvals, dtype)
77
78 if ufunc:
79 return f(xvals[:,numpy.newaxis], yvals[numpy.newaxis,:])
80 else:
81 if dtype is None:
82
83
84
85 try:
86 dtype = (numpy.zeros((1,), xvals.dtype.char) +
87 numpy.zeros((1,), yvals.dtype.char)).dtype.char
88 except AttributeError:
89 dtype = (numpy.zeros((1,), xvals.typecode()) +
90 numpy.zeros((1,), xvals.typecode())).typecode()
91
92 m = numpy.zeros((len(xvals), len(yvals)), dtype)
93 for xi in range(len(xvals)):
94 x = xvals[xi]
95 for yi in range(len(yvals)):
96 y = yvals[yi]
97 m[xi,yi] = f(x,y)
98 return m
99
100
101
102 grid_function = tabulate_function
103
104
106 """Evaluate a function of 1 variable and store the results in a Data.
107
108 Computes a function f of one variable on a set of specified points
109 using 'tabulate_function', then store the results into a 'Data' so
110 that it can be plotted. After calculation, the data are written
111 to a file; no copy is kept in memory. Note that this is quite
112 different than 'Func' (which tells gnuplot to evaluate the
113 function).
114
115 Arguments:
116
117 'xvals' -- a 1-d array with dimension 'numx'
118
119 'f' -- the function to plot--a callable object for which
120 f(x) returns a number.
121
122 'ufunc=<bool>' -- evaluate 'f' as a ufunc?
123
124 Other keyword arguments are passed through to the Data
125 constructor.
126
127 'f' should be a callable object taking one argument. 'f(x)' will
128 be computed at all values in xvals.
129
130 If called with 'ufunc=1', then 'f' should be a function that is
131 composed entirely of ufuncs, and it will be passed the 'xvals' and
132 'yvals' as rectangular matrices.
133
134 Thus if you have a function 'f', a vector 'xvals', and a Gnuplot
135 instance called 'g', you can plot the function by typing
136 'g.splot(compute_Data(xvals, f))'.
137
138 """
139
140 xvals = utils.float_array(xvals)
141
142
143 data = tabulate_function(f, xvals, ufunc=ufunc)
144
145 return PlotItems.Data(xvals, data, **keyw)
146
147
149 """Evaluate a function of 2 variables and store the results in a GridData.
150
151 Computes a function 'f' of two variables on a rectangular grid
152 using 'tabulate_function', then store the results into a
153 'GridData' so that it can be plotted. After calculation the data
154 are written to a file; no copy is kept in memory. Note that this
155 is quite different than 'Func' (which tells gnuplot to evaluate
156 the function).
157
158 Arguments:
159
160 'xvals' -- a 1-d array with dimension 'numx'
161
162 'yvals' -- a 1-d array with dimension 'numy'
163
164 'f' -- the function to plot--a callable object for which
165 'f(x,y)' returns a number.
166
167 'ufunc=<bool>' -- evaluate 'f' as a ufunc?
168
169 Other keyword arguments are passed to the 'GridData' constructor.
170
171 'f' should be a callable object taking two arguments.
172 'f(x,y)' will be computed at all grid points obtained by
173 combining elements from 'xvals' and 'yvals'.
174
175 If called with 'ufunc=1', then 'f' should be a function that is
176 composed entirely of ufuncs, and it will be passed the 'xvals' and
177 'yvals' as rectangular matrices.
178
179 Thus if you have a function 'f' and two vectors 'xvals' and
180 'yvals' and a Gnuplot instance called 'g', you can plot the
181 function by typing 'g.splot(compute_GridData(f, xvals, yvals))'.
182
183 """
184
185 xvals = utils.float_array(xvals)
186 yvals = utils.float_array(yvals)
187
188
189 data = tabulate_function(f, xvals, yvals, ufunc=ufunc)
190
191 return PlotItems.GridData(data, xvals, yvals, **keyw)
192
193
194
197
198
199