generated from eigerco/beerus
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a Cairo implementations of
cumprod()
(#251)
This PR adds a Cairo implementations of `cumprod()`. ## Pull Request type <!-- Please try to limit your pull request to one type; submit multiple pull requests if needed. --> 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? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Issue Number: N/A ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Calculating cumulative production (`cumprod()`) of a list. - All tests are passed. ## 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 <!-- Any other information that is important to this PR, such as screenshots of how the component looks before and after the change. -->
- Loading branch information
Showing
5 changed files
with
52 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//! The cumulative product of the elements. | ||
|
||
/// Compute the cumulative product of a sequence. | ||
/// # Arguments | ||
/// * `sequence` - The sequence to operate. | ||
/// # Returns | ||
/// * `Array<T>` - The cumulative product of sequence. | ||
fn cumprod<T, +Mul<T>, +Copy<T>, +Drop<T>,>(mut sequence: Span<T>) -> Array<T> { | ||
// [Check] Inputs | ||
assert(sequence.len() >= 1, 'Array must have at least 1 elt'); | ||
|
||
// [Compute] Interpolation | ||
let mut array = array![]; | ||
let mut prev_value = *sequence.pop_front().unwrap(); | ||
array.append(prev_value); | ||
loop { | ||
match sequence.pop_front() { | ||
Option::Some(current_value) => { | ||
let prod = *current_value * prev_value; | ||
array.append(prod); | ||
prev_value = prod; | ||
}, | ||
Option::None => { break; }, | ||
}; | ||
}; | ||
array | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod cumprod; | ||
mod cumsum; | ||
mod diff; | ||
mod integers; | ||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod cumprod_test; | ||
mod cumsum_test; | ||
mod diff_test; | ||
mod integers_test; | ||
|
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,19 @@ | ||
use alexandria_numeric::cumprod::cumprod; | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn cumprod_test() { | ||
let xs: Array<u64> = array![3, 5, 7]; | ||
let ys = cumprod(xs.span()); | ||
assert(*ys[0] == *xs[0], 'wrong value at index 0'); | ||
assert(*ys[1] == *xs[0] * *xs[1], 'wrong value at index 1'); | ||
assert(*ys[2] == *xs[0] * *xs[1] * *xs[2], 'wrong value at index 2'); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ('Array must have at least 1 elt',))] | ||
#[available_gas(2000000)] | ||
fn cumprod_test_revert_empty() { | ||
let xs: Array<u64> = array![]; | ||
let ys = cumprod(xs.span()); | ||
} |