Function Class¶

class
rocketpy.Function.
Function
(source, inputs=['Scalar'], outputs=['Scalar'], interpolation=None, extrapolation=None)[source]¶ Class converts a python function or a data sequence into an object which can be handled more naturally, enabling easy interpolation, extrapolation, ploting and algebra.
Convert source into a Function, to be used more naturally. Set inputs, outputs, domain dimension, interpolation and extrapolation method, and process the source.
 Parameters
source (function, scalar, ndarray, string) – The actual function. If type is function, it will be called for evaluation. If type is int or float, it will be treated as a constant function. If ndarray, its points will be used for interpolation. An ndarray should be as [(x0, y0, z0), (x1, y1, z1), (x2, y2, z2), …] where x0 and y0 are inputs and z0 is output. If string, imports file named by the string and treats it as csv. The file is converted into ndarray and should not have headers.
inputs (string, sequence of strings, optional) – The name of the inputs of the function. Will be used for representation and graphing (axis names). ‘Scalar’ is default. If source is function, int or float and has multiple inputs, this parameter must be given for correct operation.
outputs (string, sequence of strings, optional) – The name of the outputs of the function. Will be used for representation and graphing (axis names). Scalar is default.
interpolation (string, optional) – Interpolation method to be used if source type is ndarray. For 1D functions, linear, polynomial, akima and spline are supported. For ND functions, only shepard is suporrted. Default for 1D functions is spline.
extrapolation (string, optional) – Extrapolation method to be used if source type is ndarray. Options are ‘natural’, which keeps interpolation, ‘constant’, which returns the value of the function at the edge of the interval, and ‘zero’, which returns zero for all points outside of source range. Default for 1D functions is constant.
 Returns
 Return type
None

setInputs
(inputs)[source]¶ Set the name and number of the incoming arguments of the Function.
 Parameters
inputs (string, sequence of strings) – The name of the parameters (inputs) of the Function.
 Returns
self
 Return type

setOutputs
(outputs)[source]¶ Set the name and number of the output of the Function.
 Parameters
outputs (string, sequence of strings) – The name of the output of the function. Example: Distance (m).
 Returns
self
 Return type

setSource
(source)[source]¶ Set the source which defines the output of the function giving a certain input.
 Parameters
source (function, scalar, ndarray, string) – The actual function. If type is function, it will be called for evaluation. If type is int or float, it will be treated as a constant function. If ndarray, its points will be used for interpolation. An ndarray should be as [(x0, y0, z0), (x1, y1, z1), (x2, y2, z2), …] where x0 and y0 are inputs and z0 is output. If string, imports file named by the string and treats it as csv. The file is converted into ndarray and should not have headers.
 Returns
self
 Return type

setInterpolation
(method='spline')[source]¶ Set interpolation method and process data is method requires.
 Parameters
method (string, optional) – Interpolation method to be used if source type is ndarray. For 1D functions, linear, polynomial, akima and spline is supported. For ND functions, only shepard is suporrted. Default is ‘spline’.
 Returns
self
 Return type

setExtrapolation
(method='constant')[source]¶ Set extrapolation behavior of data set.
 Parameters
extrapolation (string, optional) – Extrapolation method to be used if source type is ndarray. Options are ‘natural’, which keeps interpolation, ‘constant’, which returns the value of the function at the edge of the interval, and ‘zero’, which returns zero for all points outside of source range. Default is ‘zero’.
 Returns
self
 Return type

setGetValueOpt
()[source]¶ Crates a method that evaluates interpolations rather quickly when compared to other options available, such as just calling the object instance or calling self.getValue directly. See Function.getValueOpt for documentation.
 Returns
self
 Return type

setDiscrete
(lower=0, upper=10, samples=200, interpolation='spline', extrapolation='constant', oneByOne=True)[source]¶ This method transforms function defined Functions into list defined Functions. It evaluates the function at certain points (sampling range) and stores the results in a list, which is converted into a Function and then returned. The original Function object is replaced by the new one.
 Parameters
lower (scalar, optional) – Value where sampling range will start. Default is 0.
upper (scalar, optional) – Value where sampling range will end. Default is 10.
samples (int, optional) – Number of samples to be taken from inside range. Default is 200.
interpolation (string) – Interpolation method to be used if source type is ndarray. For 1D functions, linear, polynomail, akima and spline is supported. For ND functions, only shepard is suporrted. Default is ‘spline’.
extrapolation (string, optional) – Extrapolation method to be used if source type is ndarray. Options are ‘natural’, which keeps interpolation, ‘constant’, which returns the value of the function at the edge of the interval, and ‘zero’, which returns zero for all points outside of source range. Default is ‘constant’.
oneByOne (boolean, optional) – If True, evaluate Function in each sample point separately. If False, evaluates Function in vectorized form. Default is True.
 Returns
self
 Return type

getValue
(*args)[source]¶ This method returns the value of the Function at the specified point. See Function.getValueOpt for a faster, but limited, implementation.
 Parameters
args (scalar, list) – Value where the Function is to be evaluated. If the Function is 1D, only one argument is expected, which may be an int, a float or a list of ints or floats, in which case the Function will be evaluated at all points in the list and a list of floats will be returned. If the function is ND, N arguments must be given, each one being an scalar or list.
 Returns
ans
 Return type
scalar, list

getValueOpt_deprecated
(*args)[source]¶ THE CODE BELOW IS HERE FOR DOCUMENTATION PURPOSES ONLY. IT WAS REPLACED FOR ALL INSTANCES BY THE FUNCTION.SETGETVALUEOPT METHOD.
This method returns the value of the Function at the specified point in a limited but optimized manner. See Function.getValue for an implementation which allows more kinds of inputs. This method optimizes the Function.getValue method by only implementing function evaluations of single inputs, i.e., it is not vectorized. Furthermore, it actually implements a different method for each interpolation type, eliminating some if statements. Currently supports callables and spline, linear, akima, polynomial and shepard interpolated Function objects.
 Parameters
args (scalar) – Value where the Function is to be evaluated. If the Function is 1D, only one argument is expected, which may be an int or a float If the function is ND, N arguments must be given, each one being an int or a float.
 Returns
x
 Return type
scalar

getValueOpt2
(*args)[source]¶ DEPRECATED!!  See Function.getValueOpt for new version. This method returns the value of the Function at the specified point in a limited but optimized manner. See Function.getValue for an implementation which allows more kinds of inputs. This method optimizes the Function.getValue method by only implementing function evaluations of single inputs, i.e., it is not vectorized. Furthermore, it actually implements a different method for each interpolation type, eliminating some if statements. Finally, it uses Numba to compile the methods, which further optimizes the implementation. The code below is here for documentation purposes only. It is overwritten for all instances by the Function.setGetValuteOpt2 method.
 Parameters
args (scalar) – Value where the Function is to be evaluated. If the Function is 1D, only one argument is expected, which may be an int or a float If the function is ND, N arguments must be given, each one being an int or a float.
 Returns
x
 Return type
scalar

plot
(*args, **kwargs)[source]¶ Call Function.plot1D if Function is 1Dimensional or call Function.plot2D if Function is 2Dimensional and forward arguments and keyword arguments.

plot1D
(lower=None, upper=None, samples=1000, forceData=False, forcePoints=False, returnObject=False)[source]¶ Plot 1Dimensional Function, from a lower limit to an upper limit, by sampling the Function several times in the interval. The title of the graph is given by the name of the axes, which are taken from the Function`s input and output names.
 Parameters
lower (scalar, optional) – The lower limit of the interval in which the function is to be ploted. The default value for function type Functions is 0. By contrast, if the Function is given by a dataset, the default value is the start of the dataset.
upper (scalar, optional) – The upper limit of the interval in which the function is to be ploted. The default value for function type Functions is 10. By contrast, if the Function is given by a dataset, the default value is the end of the dataset.
samples (int, optional) – The number of samples in which the function will be evaluated for plotting it, which draws lines between each evaluated point. The default value is 1000.
forceData (Boolean, optional) – If Function is given by an interpolated dataset, setting forceData to True will plot all points, as a scatter, in the dataset. Default value is False.
forcePoints (Boolean, optional) – Setting forcePoints to True will plot all points, as a scatter, in which the Function was evaluated in the dataset. Default value is False.
 Returns
 Return type
None

plot2D
(lower=None, upper=None, samples=[30, 30], forceData=True, dispType='surface')[source]¶ Plot 2Dimensional Function, from a lower limit to an upper limit, by sampling the Function several times in the interval. The title of the graph is given by the name of the axis, which are taken from the Function`s inputs and output names.
 Parameters
lower (scalar, array of int or float, optional) – The lower limits of the interval in which the function is to be ploted, which can be an int or float, which is repeated for both axis, or an array specifying the limit for each axis. The default value for function type Functions is 0. By contrast, if the Function is given by a dataset, the default value is the start of the dataset for each axis.
upper (scalar, array of int or float, optional) – The upper limits of the interval in which the function is to be ploted, which can be an int or float, which is repeated for both axis, or an array specifying the limit for each axis. The default value for function type Functions is 0. By contrast, if the Function is given by a dataset, the default value is the end of the dataset for each axis.
samples (int, array of int, optional) – The number of samples in which the function will be evaluated for plotting it, which draws lines between each evaluated point. The default value is 30 for each axis.
forceData (Boolean, optional) – If Function is given by an interpolated dataset, setting forceData to True will plot all points, as a scatter, in the dataset. Default value is False.
dispType (string, optional) – Display type of plotted graph, which can be surface, wireframe, contour, or contourf. Default value is surface.
 Returns
 Return type
None

static
comparePlots
(plot_list, lower=None, upper=None, samples=1000, title='', xlabel='', ylabel='', forceData=False, forcePoints=False, returnObject=False)[source]¶ Plots N 1Dimensional Functions in the same plot, from a lower limit to an upper limit, by sampling the Functions several times in the interval.
 Parameters
plot_list (list) – List of Functions or list of tuples in the format (Function, label), where label is a string which will be displayed in the legend.
lower (scalar, optional) – The lower limit of the interval in which the Functions are to be ploted. The default value for function type Functions is 0. By contrast, if the Functions given are defined by a dataset, the default value is the lowest value of the datasets.
upper (scalar, optional) – The upper limit of the interval in which the Functions are to be ploted. The default value for function type Functions is 10. By contrast, if the Functions given are defined by a dataset, the default value is the highest value of the datasets.
samples (int, optional) – The number of samples in which the functions will be evaluated for plotting it, which draws lines between each evaluated point. The default value is 1000.
title (string, optional) – Title of the plot. Default value is an empty string.
xlabel (string, optional) – Xaxis label. Default value is an empty string.
ylabel (string, optional) – Yaxis label. Default value is an empty string.
forceData (Boolean, optional) – If Function is given by an interpolated dataset, setting forceData to True will plot all points, as a scatter, in the dataset. Default value is False.
forcePoints (Boolean, optional) – Setting forcePoints to True will plot all points, as a scatter, in which the Function was evaluated to plot it. Default value is False.
 Returns
 Return type
None

integral
(a, b, numerical=False)[source]¶ Evaluate a definite integral of a 1D Function in the interval from a to b.
 Parameters
a (float) – Lower limit of integration.
b (float) – Upper limit of integration.
numerical (bool) – If True, forces the definite integral to be evaluated numerically. The current numerical method used is scipy.integrate.quad. If False, try to calculate using interpolation information. Currently, only available for spline and linear interpolation. If unavailable, calculate numerically anyways.
 Returns
ans – Evaluated integral.
 Return type
float