generated from eigerco/beerus
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: fast interpolation bin search closest+bug fix (#286)
- implement a gas efficient implementation of interpolation - implement binary search for closest element in sorted array - fix interpolation error in the case of `Interpolation::ConstantLeft` - additional tests are provided ## Pull Request type Please check the type of change your PR introduces: - [ ] Bugfix - [X] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? Interpolation is gas inefficient with large arrays closes #285 ## What is the new behavior? Fast interpolation complexity is now logarithmic (vs linear) in the number of array elements. ## Does this introduce a breaking change? - [ ] Yes - [X] No <!-- If this does introduce a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information Bug fix in the original interpolation is corrected and more tests cases are provided.
- Loading branch information
Showing
10 changed files
with
467 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use alexandria_numeric::interpolate::{ | ||
interpolate_fast as interpolate, Interpolation, Extrapolation | ||
}; | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn interp_extrapolation_test() { | ||
let xs: Array::<u64> = array![3, 5, 7]; | ||
let ys = array![11, 13, 17]; | ||
assert_eq!( | ||
interpolate(0, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Constant), 11 | ||
); | ||
assert_eq!( | ||
interpolate(9, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Constant), 17 | ||
); | ||
assert_eq!(interpolate(0, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Null), 0); | ||
assert_eq!(interpolate(9, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Null), 0); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn interp_linear_test() { | ||
let xs: Array::<u64> = array![3, 5, 7]; | ||
let ys = array![11, 13, 17]; | ||
assert_eq!( | ||
interpolate(4, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Constant), 12 | ||
); | ||
assert_eq!( | ||
interpolate(4, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Constant), 12 | ||
); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn interp_nearest_test() { | ||
let xs: Array::<u64> = array![3, 5, 7]; | ||
let ys = array![11, 13, 17]; | ||
assert_eq!( | ||
interpolate(4, xs.span(), ys.span(), Interpolation::Nearest, Extrapolation::Constant), 11 | ||
); | ||
assert_eq!( | ||
interpolate(6, xs.span(), ys.span(), Interpolation::Nearest, Extrapolation::Constant), 13 | ||
); | ||
assert_eq!( | ||
interpolate(7, xs.span(), ys.span(), Interpolation::Nearest, Extrapolation::Constant), 17 | ||
); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn interp_constant_left_test() { | ||
let xs: Array::<u64> = array![3, 5, 7]; | ||
let ys = array![11, 13, 17]; | ||
assert_eq!( | ||
interpolate(4, xs.span(), ys.span(), Interpolation::ConstantLeft, Extrapolation::Constant), | ||
13 | ||
); | ||
assert_eq!( | ||
interpolate(6, xs.span(), ys.span(), Interpolation::ConstantLeft, Extrapolation::Constant), | ||
17 | ||
); | ||
assert_eq!( | ||
interpolate(7, xs.span(), ys.span(), Interpolation::ConstantLeft, Extrapolation::Constant), | ||
17 | ||
); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn interp_constant_left_diff() { | ||
let xs: Span<u64> = array![0, 2, 4, 6, 8].span(); | ||
let ys: Span<u64> = array![0, 2, 4, 6, 8].span(); | ||
let inter = Interpolation::ConstantLeft; | ||
let extra = Extrapolation::Constant; | ||
assert_eq!(@interpolate(0, xs, ys, inter, extra), @0); | ||
assert_eq!(@interpolate(1, xs, ys, inter, extra), @2); | ||
assert_eq!(@interpolate(2, xs, ys, inter, extra), @2); | ||
assert_eq!(@interpolate(3, xs, ys, inter, extra), @4); | ||
assert_eq!(@interpolate(4, xs, ys, inter, extra), @4); | ||
assert_eq!(@interpolate(5, xs, ys, inter, extra), @6); | ||
assert_eq!(@interpolate(6, xs, ys, inter, extra), @6); | ||
assert_eq!(@interpolate(7, xs, ys, inter, extra), @8); | ||
assert_eq!(@interpolate(8, xs, ys, inter, extra), @8); | ||
assert_eq!(@interpolate(9, xs, ys, inter, extra), @8); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn interp_constant_right_test() { | ||
let xs: Array::<u64> = array![3, 5, 8]; | ||
let ys = array![11, 13, 17]; | ||
assert_eq!( | ||
interpolate(4, xs.span(), ys.span(), Interpolation::ConstantRight, Extrapolation::Constant), | ||
11 | ||
); | ||
assert_eq!( | ||
interpolate(6, xs.span(), ys.span(), Interpolation::ConstantRight, Extrapolation::Constant), | ||
13 | ||
); | ||
assert_eq!( | ||
interpolate(7, xs.span(), ys.span(), Interpolation::ConstantRight, Extrapolation::Constant), | ||
13 | ||
); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ("Arrays must have the same len",))] | ||
#[available_gas(2000000)] | ||
fn interp_revert_len_mismatch() { | ||
let xs: Array::<u64> = array![3, 5]; | ||
let ys = array![11]; | ||
interpolate(4, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Constant); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ("Array must have at least 2 elts",))] | ||
#[available_gas(2000000)] | ||
fn interp_revert_len_too_short() { | ||
let xs: Array::<u64> = array![3]; | ||
let ys = array![11]; | ||
interpolate(4, xs.span(), ys.span(), Interpolation::Linear, Extrapolation::Constant); | ||
} |
Oops, something went wrong.