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

ENH: Function Reverse Arithmetic Priority #488

Merged
merged 5 commits into from
Nov 28, 2023

Conversation

phmbressan
Copy link
Collaborator

@phmbressan phmbressan commented Nov 28, 2023

Pull request type

  • Code changes (bugfix, features)

Checklist

  • Tests for the changes have been added (if needed)
  • Lint (black rocketpy/ tests/) has passed locally
  • All tests (pytest --runslow) have passed locally
  • CHANGELOG.md has been updated (if relevant)

Current behavior

Operations that involve reverse Function operations with numpy.ndarrays result, usually unexpectedly, in a numpy.ndarray as a result. This happens since numpy.ndarray implement an __radd__ that overrides Function.__radd__ operation.

New behavior

The variable __array_ufunc__ is a way to enforce Function priority regarding reverse operations.

Breaking change

  • No

Additional information

This same issue was found during mathutils.vector and mathutils.matrix development and the same fix was applied (using __array_ufunc__).

@phmbressan phmbressan added the Enhancement New feature or request, including adjustments in current codes label Nov 28, 2023
@phmbressan phmbressan added this to the Release v1.X.0 milestone Nov 28, 2023
@phmbressan phmbressan self-assigned this Nov 28, 2023
Copy link

codecov bot commented Nov 28, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0ef9849) 70.91% compared to head (beb22ab) 71.05%.
Report is 3 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #488      +/-   ##
===========================================
+ Coverage    70.91%   71.05%   +0.14%     
===========================================
  Files           55       55              
  Lines         9261     9262       +1     
===========================================
+ Hits          6567     6581      +14     
+ Misses        2694     2681      -13     
Flag Coverage Δ
unittests 71.05% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@giovaniceotto giovaniceotto left a comment

Choose a reason for hiding this comment

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

This will prevent me from dying a couple of years earlier due to stress. Thank you!

@Gui-FernandesBR
Copy link
Member

Why exactly is this important? I couldn't figure that out by simply reading the PR description.
Seeing the tests that were created gave me an idea, but I'm not sure.

Does it also works for lists instead of np.ndarrays ?

So are you saying this magic line array_ufunc = None is extremely important for solving the issue, is that right?

@Gui-FernandesBR
Copy link
Member

The CHANGELOG.md needs to be updated before merging.

@Gui-FernandesBR
Copy link
Member

Some of the modified lines were not covered by tests. Could you check/fix that please?

@giovaniceotto
Copy link
Member

Why exactly is this important? I couldn't figure that out by simply reading the PR description. Seeing the tests that were created gave me an idea, but I'm not sure.

To speed things up, let me try to help here.

As the PR description says:

Operations that involve reverse Function operations with numpy.ndarrays result, usually unexpectedly, in a numpy.ndarray as a result.

Which means, for example, np.array([1, 2, 3]) * Function([(0, 0), (1, 1), (2, 2)]) returns a np.array instead of returning a Function. Even worse: a * Function([(0, 0), (1, 1), (2, 2)]) returns a np.array if a is a np.float64 or another numpy dtype (https://numpy.org/doc/stable/user/basics.types.html).

This causes a lot of problems, since we expect a * Function([(0, 0), (1, 1), (2, 2)]) to return a Function. The solution for this is presented in this PR, and probably inspired by references such as: https://numpy.org/neps/nep-0018-array-function-protocol.html

@phmbressan
Copy link
Collaborator Author

@Gui-FernandesBR just made some commits expanding the tests for these operations and updated the CHANGELOG file.

Copy link
Member

@Gui-FernandesBR Gui-FernandesBR left a comment

Choose a reason for hiding this comment

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

LGTM,

Good use of tests

@phmbressan phmbressan merged commit 191ab9f into develop Nov 28, 2023
13 checks passed
@phmbressan phmbressan deleted the enh/function-reverse-arith branch November 28, 2023 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request, including adjustments in current codes
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

BUG: KeyError: 'I_11' and AttributeError: 'NoneType' object has no attribute 'reset'
3 participants