Skip to content

Commit

Permalink
feat: add a Cairo implementations of cumprod() (#251)
Browse files Browse the repository at this point in the history
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
Soptq authored Jan 24, 2024
1 parent 3d6d49d commit 7474cca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/numeric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Several interpolation methods are supported as follow:

Return the cumulative sum of the elements ([see also](https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html#numpy-cumsum)).

## [Cumprod](./src/cumprod.cairo)

Return the cumulative product of the elements ([see also](https://numpy.org/doc/stable/reference/generated/numpy.cumprod.html#numpy-cumprod)).

## [Diff](./src/diff.cairo)

Return the discrete difference of the elements ([see also](https://numpy.org/doc/stable/reference/generated/numpy.diff.html#numpy.diff)).
27 changes: 27 additions & 0 deletions src/numeric/src/cumprod.cairo
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
}
1 change: 1 addition & 0 deletions src/numeric/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod cumprod;
mod cumsum;
mod diff;
mod integers;
Expand Down
1 change: 1 addition & 0 deletions src/numeric/src/tests.cairo
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;
Expand Down
19 changes: 19 additions & 0 deletions src/numeric/src/tests/cumprod_test.cairo
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());
}

0 comments on commit 7474cca

Please sign in to comment.