Skip to content

Improvements to Modelica.Math.Vectors.interpolate #4639

@DagBruck

Description

@DagBruck

I suggest the following fairly simple performance improvements. They should be fully backwards compatible.

  1. Special handling of nx=2
    • This is a small change that avoids the entire double iteration to find an index.
    • This case is quite common.
  2. Default iLast calculated using linear interpolation.
    • If iLast is not specified by the caller, then we do a linear search from the start. This is very inefficient for all but the smallest tables.
    • Calculating an initial guess value by linear interpolation is a a reasonable alternative.
    • As far as I can tell, there is very little active use of iLast so we often fall back to the inefficient default.
  3. Remove assertion x2>x1.
    • The test is not very useful because it just tests the two points where our index search stopped. It does not test the input requirement that x is non-decreasing.
    • The test clutters the code.

I also considered two more extensive changes, but do not propose them.

  • Replace the linear search by binary search. This has big potential, but the implementation is complicated because the abscissa may contain several identical values. The documentation even defines what should happen.
  • Remove the iLast argument and the return value iNew. This would be a significant simplification, especially because the function would just return a single number, not a pair of numbers, Also, almost nobody seems to use iLast/iNew. This would obviously be a non-compatible change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    L: MathIssue addresses Modelica.MathenhancementNew feature or enhancement

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions