Package PyFoam :: Package ThirdParty :: Package Gnuplot :: Module funcutils
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.ThirdParty.Gnuplot.funcutils

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