Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Documentation for Function Class Usage #465

Merged
merged 14 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
385 changes: 385 additions & 0 deletions docs/user/function.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
.. _functionusage:

Function Class Usage
====================

The :class:`rocketpy.Function` class in RocketPy is an auxiliary module that allows for easy manipulation of datasets and Python functions. Some of the class features are data interpolation, extrapolation, algebra and plotting.

The basic steps to create a ``Function`` are as follows:

1. Define a data source: a dataset (e.g. x,y coordinates) or a function that maps a dataset to a value (e.g. :math:`f(x) = x^2`);
2. Construct a ``Function`` object with this dataset as ``source``;
3. Use the ``Function`` features as needed: add datasets, integrate at a point, make a scatter plot and much more.

These basic steps are detailed in this guide.

1. Define a Data Source
-----------------------

a. Datasets
~~~~~~~~~~~

The ``Function`` class supports a wide variety of dataset sources:

List or Numpy Array
^^^^^^^^^^^^^^^^^^^

A ``list`` or ``numpy.ndarray`` of datapoints that maps input values to an output can be used as a ``Function`` source. For instance, we can define a dataset that follows the function :math:`f(x) = x^2`:

.. jupyter-execute::

from rocketpy.mathutils import Function

# Source dataset [(x0, y0), (x1, y1), ...]
source = [
(-3, 9), (-2, 4), (-1, 1),
(0, 0), (1, 1), (2, 4),
(3, 9)
]

# Create a Function object with this dataset
f = Function(source, "x", "y")

One may print the source attribute from the ``Function`` object to check the inputed dataset.

.. jupyter-execute::

# Print the source to see the dataset
print(f.source)

Furthermore, in order to visualize the dataset, one may use the ``plot`` method from the ``Function`` object:

.. jupyter-execute::

# Plot the source with standard spline interpolation
f.plot()

|

The dataset can be defined as a *multidimensional* array (more than one input maps to an output), where each row is a datapoint. For example, let us define a dataset that follows the plane :math:`z = x + y`:

.. jupyter-execute::

# Source dataset [(x0, y0, z0), (x1, y1, z1), ...]
source = [
(-1, -1, -2), (-1, 0, -1), (-1, 1, 0),
(0, -1, -1), (0, 0, 0), (0, 1, 1),
(1, -1, 0), (1, 0, 1), (1, 1, 2)
]

# Create a Function object with this dataset
f = Function(source, ["x", "y"], "z")

One may print the source attribute from the ``Function`` object to check the input dataset.

.. jupyter-execute::

print(f.source)

Two dimensional plots are also supported, therefore this data source can be plotted as follows:

.. jupyter-execute::

# Plot the source with standard 2d shepard interpolation
f.plot()

.. important::
The ``Function`` class only supports interpolation ``shepard`` and extrapolation ``natural`` for datasets higher than one dimension (more than one input).

CSV File
^^^^^^^^

A CSV file path can be passed as ``string`` to the ``Function`` source. The file must contain a dataset structured so that each line is a datapoint: the last column is the output and the previous columns are the inputs.

.. jupyter-execute::

# Create a csv and save with pandas
import pandas as pd

df = pd.DataFrame({
'"x"': [-3, -2, -1, 0, 1, 2, 3],
'"y"': [9, 4, 1, 0, 1, 4, 9],
})
df.to_csv('source.csv', index=False)
pd.read_csv('source.csv')

|

Having the csv file, we can define a ``Function`` object with it:

.. jupyter-execute::

# Create a Function object with this dataset
f = Function('source.csv')

# One may even delete the csv file
import os
os.remove('source.csv')

# Print the source to see the dataset
print(f.source)

.. note::
A header in the csv file is optional, but if present must be in a string like format, i.e. beginning and ending with quotation marks.

b. Function Map
~~~~~~~~~~~~~~~

A Python function that maps a set of parameters to a result can be used as a ``Function`` source. For instance, we can define a function that maps x to :math:`f(x) = \sin(x)`:

.. jupyter-execute::

import numpy as np
from rocketpy.mathutils import Function

# Define source function
def source_func(x):
return np.sin(x)

# Create a Function from source
f = Function(source_func)

The result of this operation is a ``Function`` object that wraps the source function and features many functionalities, such as plotting.

Constant Functions
^^^^^^^^^^^^^^^^^^

A special case of the python function source is the definition of a constant ``Function``. The class supports a convenient shortcut to ease the definition of a constant source:

.. jupyter-execute::

# Constant function
f = Function(1.5)

print(f(0))


.. note::
This shortcut is completely equivalent to defining a Python constant function as the source:

.. jupyter-input::

def const_source(_):
return 1.5

g = Function(const_source)


2. Building your Function
-------------------------

In this section we are going to delve deeper on ``Function`` creation and its parameters:

- source: the ``Function`` datasource. We have explored this parameter in the section above;
- inputs: a list of strings containing each input variable name. If the source only has one input, may be abbreviated as a string (e.g. "speed (m/s)");
- outputs: a list of strings containing each output variable name. If the source only has one output, may be abbreviated as a string (e.g. "total energy (J)");
- interpolation: a string that is the interpolation method to be used if the source is a dataset. Defaults to ``spline``;
- extrapolation: a string that is the extrapolation method to be used if the source is a dataset. Defaults to ``constant``;
- title: the title to be shown in the plots.

.. seealso::
Check out more about the constructor parameters and other functionalities in the :class:`rocketpy.Function` documentation.

With these in mind, let us create a more concrete example so that each of these parameters usefulness is explored.

Suppose we have a dataset containing the data from a static fire test of a rocket engine in testing phase. The dataset contain has a column for time (s) and thrust (N). We want to create a ``Function`` object that represents the thrust curve of this engine.
MateusStano marked this conversation as resolved.
Show resolved Hide resolved

.. jupyter-execute::

from rocketpy.mathutils import Function

# Static fire data
motor_thrust = [
(0, 0), (0.5, 1500), (1, 2000),
(1.5, 2100), (2, 1900), (2.5, 800),
(3, 0)
]

# Create a Function object with this dataset
thrust = Function(
source=motor_thrust,
inputs="time (s)",
outputs="thrust (N)",
interpolation="spline",
extrapolation="zero",
title="Static Fire Thrust Curve"
)

The parameters ``interpolation`` and ``extrapolation`` are of particular importance in this example:

- Due the fact the data is quite sparse, we want to use a ``spline`` interpolation to smooth the curve.
- The extrapolation method is set to ``zero`` because we know that the thrust is zero before and after the test.

Let's plot this curve to visualize the effect of these options in action:

.. jupyter-execute::

# Plotting from 0 to 5 seconds
thrust.plot(0, 5)

.. note::
Compare the interpolation and extrapolation effects by changing their methods. Check out this plot results with ``linear`` interpolation and ``constant`` extrapolation and see their difference.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation would feel more complete if you showed the diference of the interpolations here. At least of the linear case


3. Function Features
--------------------

The ``Function`` class has many features that can be used to manipulate the source data. In this section we are going to explore some of these features, such as Function call, Function arithmetic, discretization, differentiation and integration.

a. Function Call
~~~~~~~~~~~~~~~~

A ``Function`` objects maps input data to an output, therefore should you want to get an output value from a given input, this can be accomplished by the method :meth:`rocketpy.Function.get_value`:

.. jupyter-execute::

from rocketpy.mathutils import Function

f = Function(lambda x: x**0.5)

print(f.get_value(9))

Equivalently, the same operation is defined by the Python dunder method ``__call__`` so that the object can be used like a common function. For instance:

.. jupyter-execute::

print(f(9), f(25))

Furthermore, the :meth:`rocketpy.Function.get_value` method can be used to get a list of outputs from a list of inputs:

.. jupyter-execute::

print(f.get_value([1, 4, 9, 16, 25]))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done with the call method too I believe. This is not clear here

b. Function Arithmetic
~~~~~~~~~~~~~~~~~~~~~~
Comment on lines +269 to +270
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing mentioning all the arthmetic operations that can be done. A simple listing would be fine


An important feature of the ``Function`` class is the ability to perform arithmetic operations between real values or even other ``Function`` objects.

.. jupyter-execute::

import numpy as np

f = Function(lambda x: np.sin(x))

g = f/4 + 1

Function.compare_plots([f, g], lower=0, upper=4*np.pi)

.. note::
This is an example of the static method :meth:`rocketpy.Function.compare_plots`, it is used to plot Functions in the same graph for comparison.

Arithmetic can also be performed on sets of data of the same length and same domain discretization (i.e. equal x values):

.. jupyter-execute::

source1 = [(0, 0), (0.5, 0.25), (1, 1), (1.5, 2.25), (2, 4)]
source2 = [(0, 0), (0.5, 0.5), (1, 1), (1.5, 1.5), (2, 2)]

f = Function(source1)
g = Function(source2)

h = (f + g) / 2

Function.compare_plots([f, g, h], lower=0, upper=2)

c. Discretization
~~~~~~~~~~~~~~~~~

The ``Function`` class can also convert from function sourced to a discretized dataset produced from it. This is accomplished by the method :meth:`rocketpy.Function.set_discrete` and allows for a great computational speed up if the function source is complex.

The accuracy of the discretization depends on the number of datapoints and the chosen interpolation method.

Let's compare the discretization of a sine function:

.. jupyter-execute::

import numpy as np
from copy import copy

# Function from sine
f = Function(lambda x: np.sin(x))

# Discretization
f_continuous = copy(f)
f_discrete = f.set_discrete(
lower=0,
upper=4*np.pi,
samples=20,
interpolation="linear"
)

Function.compare_plots([f_continuous, f_discrete], lower=0, upper=4*np.pi)

.. important::

A `copy` of the original continuous function was necessary in this example, since the method :meth:`rocketpy.Function.set_discrete` mutates the original ``Function``.

d. Differentiation and Integration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does everything here work with 2D or higher dimension Functions?
Same question for the Discretization section

One of the most useful ``Function`` features for data analysis is easily differentiating and integrating the data source. These methods are divided as follow:

- :meth:`rocketpy.Function.differentiate`: differentiate the ``Function`` at a given point, returning the derivative value as the result;
- :meth:`rocketpy.Function.integral`: performs a definite integral over specified limits, returns the integral value (area under ``Function``);
- :meth:`rocketpy.Function.derivative_function`: computes the derivative of the given `Function`, returning another `Function` that is the derivative of the original at each point;
- :meth:`rocketpy.Function.integral_function`: calculates the definite integral of the function from a given point up to a variable, returns a ``Function``.

Derivatives
^^^^^^^^^^^

Let's make a familiar example of differentiation: the derivative of the function :math:`f(x) = x^2` is :math:`f'(x) = 2x`. We can use the ``Function`` class to compute those:

.. jupyter-execute::

# Define the function x^2
f = Function(lambda x: x**2)

# Differentiate it at x = 3
print(f.differentiate(3))

Also one may compute the derivative function:

.. jupyter-execute::

# Define the function x^2 and its derivative
f = Function(lambda x: x**2)
f_dot = f.derivative_function()

# Compare their plots
Function.compare_plots([f, f_dot], lower=-2, upper=2)

Integrals
^^^^^^^^^

Now, to illustrate the power of the ``Function`` class in making it easy to make plots of complex functions, let's plot the integral of the gaussian function:

.. math::

f(x) = \frac{1}{\sqrt{2\pi}} \cdot e^{-\frac{x^2}{2}}

Which is non-elementary so it cannot be expressed in terms of common functions.

.. jupyter-execute::

# Define the gaussian function
def gaussian(x):
return 1 / np.sqrt(2*np.pi) * np.exp(-x**2/2)

f = Function(gaussian)

# Integrate from 0 to 1
print(f.integral(0,1))

Here we have shown that we can integrate the gaussian function over a defined interval, let's compute its integral function.

.. jupyter-execute::

# Compute the integral function from -4
f_int = f.integral_function(-4, 4, 1000)

# Compare the function with the integral
Function.compare_plots([f, f_int], lower=-4, upper=4)

........

This guide shows some of the capabilities of the ``Function`` class, but there are many other functionalities to enhance your analysis. Do not hesitate in tanking a look at the documentation :class:`rocketpy.Function`.
1 change: 1 addition & 0 deletions docs/user/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ RocketPy's User Guide
:maxdepth: 2
:caption: Further Analysis

Function <function.rst>
Utilities <analysis.rst>
Loading
Loading