Skip to content

Arithmetic Operations between Functions with the Same Domain #8

Closed

Description

@giovaniceotto

The arithmetic operations defined in the Function class of rocketpyAlpha are not very well optimized for Function objects defined in the same grid points.

Functions defined in terms of data points that have the same domain should have a different implementation of the arithmetic operations. Since they are defined in the same grid points, it should be possible add and multiply them and return a new interpolated Function instead of a Function based on lambda functions, which results in much slower evaluations.

Activity

  1. giovaniceotto commented on Sep 19, 2018

    @giovaniceotto
    MemberAuthor

    Working on this in the functions_arithmetic_operations branch.

  2. giovaniceotto commented on Oct 3, 2018

    @giovaniceotto
    MemberAuthor

    The benefit of the current implementation has to do with its simplicity and flexibility, allowing all objects of the Function class to be operated on using any arithmetic operation, be it by floats or other objects of the Function class.

    This means that two Function objects with completely different interpolation schemes and defined in different grid points can be multiplied and the result is exactly as expected.

    However, in terms of the time taken to evaluate the result when the returned object is called, this implementation doesn't do a good job.

    The object returned by the arithmetic operation between two objects of the Function class, or even by the arithmetic operation of a float and a object of the Function class, is another object of the Function class. This makes sense, however, this new object is defined by a lambda function. Therefore, whenever it is called to be evaluated, it calls the lambda function, which in turn calls the original Function objects to get their value.

    This makes the implementation highly inefficient, specially when multiple operations are made. It easily takes a Function object returned after arithmetic operations 10 times longer to evaluate than an usual Function object.

    This is the main reason that the Function.setDiscrete method was created. It redefines a Function object based on lambdas by sampling it in several grid points and storing the results in an array which is interpolated anytime the object is called. It can take advantage of vectorization to evaluate many values quickly, costing less time overall and speeding up evaluations for later on.

    However, the Function.setDiscrete method is no perfect solution.

    Since arithmetic operations are very often performed between Function objects defined by datasets or arrays, one idea is for the arimethic operation to be evaluated in every grid point of both Function objects and a new Function object is returned, being defined by a new array which is interpolated when needed, just as usual Function objects. Whenever this is not possible, the new implementation can fallback to the old one.

    The big problem with this implementation is the behavior between grid points. Consider two Function objects interpolated piecewise lienarly. Their multiplication should return something with quadratic behavior. This is satisfied by the current implementation but wouldn't be by the proposed one.

  3. giovaniceotto commented on Oct 4, 2018

    @giovaniceotto
    MemberAuthor

    A possible solution which satisfies both quick evaluation and expected function behaviour between grid points would be to make use of rational polynomial function interpolation.

    If both objects of the Function class which are being added, multiplied, etc, have piecewise polynomial interpolation, such as linear, akima or spline, the new implementation could apply the arimethic operation on the polynomials defined between each grid point. In the general case, the result would be a rational polynomial. The Function class could then use the coefficients of this rational polynomial defined piecewisely to interpolate the returned object.

    This implies implementing rational polynomial interpolated capabilities to the Function class, which shouldn't be too hard.

    This new implementation has several benefits. Since it calculates the resulting rational polynomial interpolation based on other Function objects, it would not waste time solving algebraic systems to compute interpolating coefficients. Furthermore, it would evaluate quickly when needed since it is only based on interpolation. As mentioned before, it also returns a Function object which behaves exactly as expected, that is, if two linear Function objects are multiplied, a quadratic one will be returned.

    However, Function objects using other interpolation, such as Shepard or Kriging, would have to fall back to the old implementation.

  4. giovaniceotto commented on Oct 4, 2018

    @giovaniceotto
    MemberAuthor

    For now, I believe the right decision would be to implement the simplest thing possible, which was the first suggestion: reproduce the arithmetic operations on the grid points and create a new interpolation in between.

    This would only apply to Function objects which have the same interpolation method and would return a Function object with this interpolation method.

    The reason for this choice is the fact that the added benefits of using more sophisticated solutions which have the right behavior between grid points is not yet justified. Most Function objects used in the current code have spline or linear piecewise interpolations between grid points even though there is no physical basis for using either spline or linear methods. Therefore, the added work it would take to make sure that this unjustified behavior persists is currently not worth it.

  5. giovaniceotto commented on Oct 4, 2018

    @giovaniceotto
    MemberAuthor

    I just finished implementing it, but should undergo more tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions