Skip to content

ENH: Function Operations #334

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

Merged
merged 40 commits into from
Apr 9, 2023
Merged

ENH: Function Operations #334

merged 40 commits into from
Apr 9, 2023

Conversation

giovaniceotto
Copy link
Member

Pull request type

Please check the type of change your PR introduces:

  • Code base additions (bugfix, features)
  • Code maintenance (refactoring, formatting, renaming, tests)
  • ReadMe, Docs and GitHub maintenance
  • Other (please describe):

Pull request checklist

Please check if your PR fulfills the following requirements, depending on the type of PR:

  • Code base additions (for bug fixes / features):

    • Tests for the changes have been added
    • Docs have been reviewed and added / updated if needed
    • Lint (black rocketpy) has passed locally and any fixes were made
    • All tests (pytest --runslow) have passed locally

What is the current behavior?

Enter text here...

What is the new behavior?

Enter text here...

Does this introduce a breaking change?

  • Yes
  • No

Other information

Enter text here...

gautamsaiy and others added 9 commits January 28, 2023 17:04
Added try/except block to invert method

Co-authored-by: phmbressan <[email protected]>
Changed output from ndarray to float

Co-authored-by: phmbressan <[email protected]>
changed invert method name to inverseFunction and improved documentation

Co-authored-by: Giovani Hidalgo Ceotto <[email protected]>
Improved documentation

Co-authored-by: Giovani Hidalgo Ceotto <[email protected]>
Improved documentation

Co-authored-by: Giovani Hidalgo Ceotto <[email protected]>
…n-updates

Added Piecewise class and other new Function methods
@giovaniceotto giovaniceotto added the Function Everything related to the Function class label Feb 14, 2023
@giovaniceotto giovaniceotto added this to the Release v1.0.0 milestone Feb 14, 2023
@giovaniceotto giovaniceotto self-assigned this Feb 14, 2023
@giovaniceotto giovaniceotto changed the base branch from beta/v1.0.0 to enh/liquid-motors March 22, 2023 23:52
@giovaniceotto giovaniceotto changed the base branch from enh/liquid-motors to beta/v1.0.0 March 22, 2023 23:54
@MateusStano MateusStano marked this pull request as ready for review March 23, 2023 00:52
@MateusStano MateusStano self-requested a review March 23, 2023 00:52
@MateusStano
Copy link
Member

Okay so I changed a few things on this one:

c3c4617 -> created the Function.xArray and Function.yArray which substitutes all the calls of Function.source[:,0] and Function.source[:,1] (there were a lot of them). I think the real right way to do this would be to use @cached_property in a lot of attributes but I think a major restructure in the Function class would be needed to do this.

3550b0f -> Improved and fixed some methods. Mostly change some numpy functions used to others that are faster.


Here are some problems:

  • The integral method is currently really weird:

RocketPy/rocketpy/Function.py

Lines 2193 to 2283 in 1126751

def integral(self, a, b, numerical=False):
"""Evaluate a definite integral of a 1-D 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 : float
Evaluated integral.
"""
if self.__interpolation__ == "linear" and not numerical:
Xs = np.linspace(a, b, int((b - a) * 5))
Ys = self.getValue(Xs)
ans = np.trapz(Ys, x=Xs)
elif self.__interpolation__ == "spline" and not numerical:
# Integrate using spline coefficients
xData = self.xArray
yData = self.yArray
coeffs = self.__splineCoefficients__
ans = 0
# Check to see if interval starts before point data
if a < xData[0]:
if self.__extrapolation__ == "constant":
ans += yData[0] * (xData[0] - a)
elif self.__extrapolation__ == "natural":
c = coeffs[:, 0]
subB = a - xData[0] # subA = 0
ans -= (
(c[3] * subB**4) / 4
+ (c[2] * subB**3 / 3)
+ (c[1] * subB**2 / 2)
+ c[0] * subB
)
else:
# self.__extrapolation__ = 'zero'
pass
# Integrate in subintervals between Xs of given data up to b
i = 0
while i < len(xData) - 1 and xData[i] < b:
if b < xData[i + 1]:
subB = b - xData[i] # subA = 0
else:
subB = xData[i + 1] - xData[i] # subA = 0
c = coeffs[:, i]
subB = xData[i + 1] - xData[i] # subA = 0
ans += (
(c[3] * subB**4) / 4
+ (c[2] * subB**3 / 3)
+ (c[1] * subB**2 / 2)
+ c[0] * subB
)
i += 1
# Check to see if interval ends after point data
if b > xData[-1]:
if self.__extrapolation__ == "constant":
ans += yData[-1] * (b - xData[-1])
elif self.__extrapolation__ == "natural":
c = coeffs[:, -1]
subA = xData[-1] - xData[-2]
subB = b - xData[-2]
ans -= (
(c[3] * subA**4) / 4
+ (c[2] * subA**3 / 3)
+ (c[1] * subA**2 / 2)
+ c[0] * subA
)
ans += (
(c[3] * subB**4) / 4
+ (c[2] * subB**3 / 3)
+ (c[1] * subB**2 / 2)
+ c[0] * subB
)
else:
# self.__extrapolation__ = 'zero'
pass
else:
# Integrate numerically
ans, _ = integrate.quad(self, a, b, epsabs=0.01, limit=10000)
return ans

First I had to add Xs and Ys to make linearly interpolated functions actually consider the a and b arguments that represent the interval of integration. Now linear interpolation integration is working fine. However, spline interpolated Functions still returns wrong values.

Why not always use scipy. integrate.quad? From what I gathered, this is slower than numpy.trapz but more accurate. However, since we need to chop the array into the intended interval to use numpy.trapz I can't be certain that it is actually faster then scipy.integrate.quad. Also the integration error with numpy.trapz can end up being pretty significant when doing operations on top of itn (like in integralFunction or averageFunction).

  • Interpolation is always set to linear when using derivativeFunction is this intended?

  • findInput needs to have a good starting guess to work well. Which is as expected, however when we use inverseFunction extra care is needed. Just important to point out

  • The averageFunction is a bit problematic because the lower argument can never be equal to x, or in the average calculation there will be a division by 0. Now, I dont think it is worth doing something to deal with exceptions for now but this means that you cant plot these functions without specifing the plot limits. It is important to remember this when using the method

@giovaniceotto
Copy link
Member Author

  • Issues with the integral method, which were focused on exact integration of spline interpolated Functions, are now solved.
  • Thorough testing has been added for the Function.integral method.

Copy link
Member

@MateusStano MateusStano left a comment

Choose a reason for hiding this comment

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

I think all is good now!

Just one question:

RocketPy/rocketpy/Function.py

Lines 2766 to 2769 in e5eefae

if __name__ == "__main__":
import doctest
doctest.testmod()

What is going on here?

@MateusStano
Copy link
Member

Also, I found this in the enh/liquid-motors branch:

RocketPy/rocketpy/Function.py

Lines 2282 to 2304 in 2381a6d

def differentiate(self, x, dx=1e-6, order=1):
"""Differentiate a Function object at a given point.
Parameters
----------
x : float
Point at which to differentiate.
dx : float
Step size to use for numerical differentiation.
order : int
Order of differentiation.
Returns
-------
ans : float
Evaluated derivative.
"""
if order == 1:
return (self.getValue(x + dx) - self.getValue(x - dx)) / (2 * dx)
elif order == 2:
return (
self.getValue(x + dx) - 2 * self.getValue(x) + self.getValue(x - dx)
) / dx**2

Should this be added here too?

@giovaniceotto
Copy link
Member Author

I think all is good now!

Just one question:

RocketPy/rocketpy/Function.py

Lines 2766 to 2769 in e5eefae

if __name__ == "__main__":
import doctest
doctest.testmod()

What is going on here?

This is doctest! It's there so that we can verify if all examples in the docstrings are working as expected.

More info here: https://docs.python.org/3/library/doctest.html

@MateusStano MateusStano merged commit 1bf4851 into beta/v1.0.0 Apr 9, 2023
MateusStano added a commit that referenced this pull request Apr 9, 2023
commit 1bf4851
Merge: c3c20d0 0636108
Author: MateusStano <[email protected]>
Date:   Sun Apr 9 13:58:17 2023 -0300

    Merge pull request #334 from RocketPy-Team/enh/Function-operations

    ENH: Function Operations

commit 0636108
Author: Pedro Bressan <[email protected]>
Date:   Mon Apr 3 23:20:12 2023 -0300

    ENH: add second order derivatives.

    Co-authored-by: giovaniceotto <[email protected]>

commit 1d39356
Author: Pedro Bressan <[email protected]>
Date:   Mon Apr 3 22:22:54 2023 -0300

    ENH: reimplement bijection checks from 10bf4cd

commit d5c4eab
Author: Pedro Bressan <[email protected]>
Date:   Mon Apr 3 21:47:45 2023 -0300

    MAINT: update inverseFunction docstrings.

commit 7847e45
Author: Pedro Bressan <[email protected]>
Date:   Mon Apr 3 21:41:20 2023 -0300

    FIX: handle all composition cases in compose.

commit e5eefae
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Sat Apr 1 23:15:59 2023 -0300

    FIX: fix left over bug in spline integral when bounds are out of domain

commit 2024ab5
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Sat Apr 1 23:15:17 2023 -0300

    TST: Improve Function.integral tests

commit 217741e
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Thu Feb 2 17:34:34 2023 +0000

    MAINT: refactor linear interpolated Function integral

commit 4f11816
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Sat Apr 1 22:29:43 2023 -0300

    FIX: solves spline integration errors

commit e7c2d98
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Sat Apr 1 22:27:50 2023 -0300

    TST: Create Function.integral tests for splines

commit c3c20d0
Merge: 35eca94 c255e1c
Author: MateusStano <[email protected]>
Date:   Fri Mar 31 23:58:44 2023 -0300

    Merge pull request #346 from RocketPy-Team/fix/flight-reynolds-dynamic-viscosity

    Fix: Flight Reynolds & Dynamic viscosity wrong values

commit 35eca94
Merge: 6a09af6 986666d
Author: MateusStano <[email protected]>
Date:   Fri Mar 31 23:58:24 2023 -0300

    Merge pull request #347 from RocketPy-Team/tst/flutter-test-fix

    TST: Flutter test fix

commit c255e1c
Merge: fea5662 986666d
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Fri Mar 31 22:54:53 2023 -0300

    Merge branch 'tst/flutter-test-fix' into fix/flight-reynolds-dynamic-viscosity

commit 986666d
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Fri Mar 31 22:48:25 2023 -0300

    TST: decrease assertion requirements for flutter test

    Co-authored-by: MateusStano <[email protected]>
    Co-authored-by: phmbressan <[email protected]>

commit fea5662
Author: Giovani Hidalgo Ceotto <[email protected]>
Date:   Fri Mar 31 22:39:05 2023 -0300

    FIX: change index from 7 to 8 to fix dynamic viscosity

    Co-authored-by: MateusStano <[email protected]>
    Co-authored-by: phmbressan <[email protected]>

commit afa44d6
Author: MateusStano <[email protected]>
Date:   Fri Mar 31 15:45:15 2023 -0300

    ENH: added integral changes from #332

commit 165bb27
Author: Lint Action <[email protected]>
Date:   Wed Mar 29 12:30:49 2023 +0000

    Fix code style issues with Black

commit 7f40fda
Author: Pedro Bressan <[email protected]>
Date:   Wed Mar 29 09:27:58 2023 -0300

    FIX: array attributes error on setDiscrete conversion.

commit d9af5fe
Author: Pedro Bressan <[email protected]>
Date:   Wed Mar 29 08:40:32 2023 -0300

    ENH: implement compose method for array based Functions.

commit 3ddfecb
Merge: b822714 1126751
Author: MateusStano <[email protected]>
Date:   Mon Mar 27 15:07:09 2023 -0300

    Merge branch 'enh/Function-operations' of https://github.com/RocketPy-Team/RocketPy into enh/Function-operations

commit b822714
Author: MateusStano <[email protected]>
Date:   Mon Mar 27 15:07:03 2023 -0300

    MAINT: more docs

commit 1126751
Author: Lint Action <[email protected]>
Date:   Mon Mar 27 17:39:47 2023 +0000

    Fix code style issues with Black

commit d851457
Merge: 1d1b2ba 6a09af6
Author: MateusStano <[email protected]>
Date:   Mon Mar 27 14:37:17 2023 -0300

    Merge branch 'beta/v1.0.0' into enh/Function-operations

commit 1d1b2ba
Author: MateusStano <[email protected]>
Date:   Mon Mar 27 14:25:11 2023 -0300

    MAINT: improved/corrected docs

commit 3550b0f
Author: MateusStano <[email protected]>
Date:   Mon Mar 27 13:36:01 2023 -0300

    ENH: improved new methods

commit 8b78bb5
Author: MateusStano <[email protected]>
Date:   Mon Mar 27 13:24:03 2023 -0300

    enh: added __bool__ dunder method

commit c3c4617
Author: MateusStano <[email protected]>
Date:   Sun Mar 26 20:05:52 2023 -0300

    enh: added self.xArray and self.yArray

commit 6a09af6
Merge: 483d9cf 803e2f8
Author: MateusStano <[email protected]>
Date:   Wed Mar 22 15:18:30 2023 -0300

    Merge pull request #342 from RocketPy-Team/enh/aerodynamic_surfaces_parent_class

    Enh/aerodynamic surfaces parent class

commit 803e2f8
Author: Lint Action <[email protected]>
Date:   Wed Mar 22 18:17:51 2023 +0000

    Fix code style issues with Black

commit db653ac
Merge: d9cf063 483d9cf
Author: MateusStano <[email protected]>
Date:   Wed Mar 22 15:17:27 2023 -0300

    Merge branch 'beta/v1.0.0' into enh/aerodynamic_surfaces_parent_class

commit d9cf063
Author: MateusStano <[email protected]>
Date:   Wed Mar 22 15:13:02 2023 -0300

    ENH: improve nose and tail prints

commit 5c9d77f
Merge: 1c60fda 794226d
Author: MateusStano <[email protected]>
Date:   Wed Mar 22 14:50:04 2023 -0300

    Merge pull request #341 from RocketPy-Team/enh/Change_aeroSurfaces_class

    Enh/change aero surfaces class

commit 794226d
Author: MateusStano <[email protected]>
Date:   Wed Mar 22 14:43:07 2023 -0300

    enh: changed __str__ back to __repr__
    added "->"

commit 1bc9227
Author: Lint Action <[email protected]>
Date:   Mon Mar 20 16:41:27 2023 +0000

    Fix code style issues with Black

commit 98534e5
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Mon Mar 20 13:40:03 2023 -0300

    running black

commit 1cbd52a
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Mon Mar 20 13:37:08 2023 -0300

    changing __repr__ to __str__

commit 1c60fda
Merge: 438074a aa6e68d
Author: MateusStano <[email protected]>
Date:   Thu Mar 16 20:05:56 2023 -0300

    Merge pull request #339 from RocketPy-Team/enh/new-nosecone-types-added

    ENH: New nosecone types added.

commit aa6e68d
Author: CabGT <[email protected]>
Date:   Sat Mar 11 17:35:37 2023 -0300

    ENH: New nosecone types added.

commit 9dd94aa
Author: MateusStano <[email protected]>
Date:   Thu Mar 9 15:19:58 2023 -0300

    enh: remade position an input for the aeroSurfaces list
    made position also save in the object

commit ecaa167
Author: Pedro Bressan <[email protected]>
Date:   Thu Feb 23 14:05:44 2023 -0300

    ENH: add average operation and minor fixes.

commit 2676e44
Author: Pedro Bressan <[email protected]>
Date:   Thu Feb 23 14:04:58 2023 -0300

    MAINT: use scipy.root as faster root finder.

commit 0ccf225
Author: Pedro Bressan <[email protected]>
Date:   Thu Feb 23 14:04:00 2023 -0300

    FIX: correct compose operation.

commit 4020cdd
Author: MateusStano <[email protected]>
Date:   Tue Feb 21 13:17:02 2023 -0300

    enh: create nosecone, fins and tail attribute

commit 438074a
Merge: a2505dd b929456
Author: MateusStano <[email protected]>
Date:   Sun Feb 19 19:09:19 2023 -0300

    Merge pull request #333 from RocketPy-Team/enh/setters_and_getters

    Enh/setters and getters

commit b929456
Author: MateusStano <[email protected]>
Date:   Sun Feb 19 19:02:24 2023 -0300

    maint: tidy up docs

commit c803668
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Sat Feb 18 12:53:16 2023 -0300

    enh: making position an atribute of the aero classes

commit a2505dd
Merge: db066ad e9b7422
Author: MateusStano <[email protected]>
Date:   Sat Feb 18 12:05:01 2023 -0300

    Merge pull request #327 from RocketPy-Team/enh/plot-airfoil-information

    enh/plot-airfoil-information

commit e9b7422
Author: Lint Action <[email protected]>
Date:   Sat Feb 18 15:03:24 2023 +0000

    Fix code style issues with Black

commit 696d631
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Sat Feb 18 12:02:55 2023 -0300

    enh:airfoilInfo

commit ff8ae3d
Author: Lint Action <[email protected]>
Date:   Sat Feb 18 01:09:05 2023 +0000

    Fix code style issues with Black

commit 1ee3c15
Author: FranzYuri <[email protected]>
Date:   Fri Feb 17 22:08:58 2023 -0300

    Update rocketpy/AeroSurfaces.py

    Co-authored-by: MateusStano <[email protected]>

commit 5f421c5
Author: FranzYuri <[email protected]>
Date:   Fri Feb 17 22:08:37 2023 -0300

    Update rocketpy/AeroSurfaces.py

    Co-authored-by: MateusStano <[email protected]>

commit 9064685
Merge: b238174 8b57822
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Fri Feb 17 22:01:44 2023 -0300

    Merge branch 'enh/setters_and_getters' of https://github.com/RocketPy-Team/RocketPy into enh/setters_and_getters

commit b238174
Merge: e1c4afe 10658c7
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Fri Feb 17 22:01:20 2023 -0300

    Merge branch 'enh/setters_and_getters' of https://github.com/RocketPy-Team/RocketPy into enh/setters_and_getters

commit 8b57822
Author: Lint Action <[email protected]>
Date:   Sat Feb 18 01:01:16 2023 +0000

    Fix code style issues with Black

commit e1c4afe
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Fri Feb 17 22:01:07 2023 -0300

    finishing setters

commit 10658c7
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Fri Feb 17 22:00:42 2023 -0300

    finishing setters

commit efc1c9f
Author: giovaniceotto <[email protected]>
Date:   Thu Feb 16 00:59:20 2023 -0300

    STY: apply black linting

commit 882921f
Author: giovaniceotto <[email protected]>
Date:   Thu Feb 16 00:58:23 2023 -0300

    MAINT: refactor integralFunction method

commit 10bf4cd
Author: giovaniceotto <[email protected]>
Date:   Thu Feb 16 00:46:17 2023 -0300

    ENH: implement Function.isStrictlyBijective

commit 4150015
Author: Pedro Bressan <[email protected]>
Date:   Tue Feb 14 17:09:37 2023 -0300

    maint: linting with black.

commit 0418dc4
Author: Pedro Bressan <[email protected]>
Date:   Tue Feb 14 17:07:48 2023 -0300

    maint: update docstrings and method naming.

commit 957352a
Author: Pedro Bressan <[email protected]>
Date:   Tue Feb 14 17:06:31 2023 -0300

    add: update inverseFunction with bijection checks.

commit 90894d0
Author: Pedro Bressan <[email protected]>
Date:   Tue Feb 14 17:05:40 2023 -0300

    add: method that checks whether a Function is bijective.

commit 029c57a
Merge: a9f9fab 66bd7e6
Author: phmbressan <[email protected]>
Date:   Mon Feb 13 22:37:42 2023 -0300

    Merge pull request #330 from Space-Enterprise-at-Berkeley/enh/function-updates

    Added Piecewise class and other new Function methods

commit 8e17bc2
Author: Lint Action <[email protected]>
Date:   Sat Feb 11 22:54:39 2023 +0000

    Fix code style issues with Black

commit 1318e39
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Sat Feb 11 19:47:43 2023 -0300

    minor improvements

commit 66bd7e6
Author: gautam <[email protected]>
Date:   Sat Feb 11 11:26:15 2023 -0800

    updated inverseFunction

commit 98ba965
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Fri Feb 10 19:35:42 2023 -0300

    adding methods to the Fins classes

    adding methods to the fins classes

commit 46ecbd1
Author: gautamsaiy <[email protected]>
Date:   Wed Feb 8 15:00:51 2023 -0800

    Update rocketpy/Function.py

    Improved documentation

    Co-authored-by: Giovani Hidalgo Ceotto <[email protected]>

commit cdc05f3
Author: gautamsaiy <[email protected]>
Date:   Wed Feb 8 15:00:30 2023 -0800

    Update rocketpy/Function.py

    Improved documentation

    Co-authored-by: Giovani Hidalgo Ceotto <[email protected]>

commit 9bc25ba
Author: gautamsaiy <[email protected]>
Date:   Wed Feb 8 15:00:02 2023 -0800

    Update rocketpy/Function.py

    changed invert method name to inverseFunction and improved documentation

    Co-authored-by: Giovani Hidalgo Ceotto <[email protected]>

commit ce84770
Author: gautam <[email protected]>
Date:   Sun Feb 5 10:53:56 2023 -0800

    Fixed a small error

commit d888f6f
Author: gautamsaiy <[email protected]>
Date:   Sat Feb 4 11:32:15 2023 -0800

    Update rocketpy/Function.py

    Changed output from ndarray to float

    Co-authored-by: phmbressan <[email protected]>

commit d9c3980
Author: gautamsaiy <[email protected]>
Date:   Sat Feb 4 11:31:45 2023 -0800

    Update rocketpy/Function.py

    Added try/except block to invert method

    Co-authored-by: phmbressan <[email protected]>

commit db066ad
Merge: d572bcc b488ac5
Author: MateusStano <[email protected]>
Date:   Sun Jan 29 23:07:03 2023 -0300

    Merge pull request #329 from RocketPy-Team/enh/fix_legends

    enh/fix_legends

commit 356ef4c
Author: gautam <[email protected]>
Date:   Sat Jan 28 17:04:39 2023 -0800

    Added Piecewise class and other new Function methods

commit 2e6dc28
Author: Lint Action <[email protected]>
Date:   Thu Jan 26 20:33:43 2023 +0000

    Fix code style issues with Black

commit d4a661e
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Thu Jan 26 17:08:44 2023 -0300

    only added one plot

commit b488ac5
Author: Franz Masatoshi Yuri <[email protected]>
Date:   Wed Jan 25 17:47:41 2023 -0300

    fixed
@Gui-FernandesBR Gui-FernandesBR deleted the enh/Function-operations branch May 26, 2023 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Function Everything related to the Function class
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants