generated from eigerco/beerus
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds `norm` operator. ## 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 Norm of a given 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. --> --------- Co-authored-by: Lucas @ StarkWare <70894690+LucasLvy@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
77 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod dot; | ||
mod kron; | ||
mod norm; | ||
|
||
#[cfg(test)] | ||
mod tests; |
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,36 @@ | ||
//! Norm of an T array. | ||
use alexandria_math::fast_root::fast_nr_optimize; | ||
use alexandria_math::pow; | ||
|
||
/// Compute the norm for an T array. | ||
/// # Arguments | ||
/// * `array` - The inputted array. | ||
/// * `ord` - The order of the norm. | ||
/// * `iter` - The number of iterations to run the algorithm | ||
/// # Returns | ||
/// * `u128` - The norm for the array. | ||
fn norm<T, +Into<T, u128>, +Zeroable<T>, +Copy<T>>( | ||
mut xs: Span<T>, ord: u128, iter: usize | ||
) -> u128 { | ||
let mut norm: u128 = 0; | ||
loop { | ||
match xs.pop_front() { | ||
Option::Some(x_value) => { | ||
if ord == 0 { | ||
if (*x_value).is_non_zero() { | ||
norm += 1; | ||
} | ||
} else { | ||
norm += pow((*x_value).into(), ord); | ||
} | ||
}, | ||
Option::None => { break; }, | ||
}; | ||
}; | ||
|
||
if ord == 0 { | ||
return norm; | ||
} | ||
|
||
fast_nr_optimize(norm, ord, iter) | ||
} |
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,2 +1,3 @@ | ||
mod dot_test; | ||
mod kron_test; | ||
mod norm_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,29 @@ | ||
use alexandria_linalg::norm::norm; | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn norm_test_1() { | ||
let mut array: Array<u128> = array![3, 4]; | ||
assert(norm(array.span(), 2, 10) == 5, 'invalid l2 norm'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn norm_test_2() { | ||
let mut array: Array<u128> = array![3, 4]; | ||
assert(norm(array.span(), 1, 10) == 7, 'invalid l1 norm'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn norm_test_3() { | ||
let mut array: Array<u128> = array![3, 4]; | ||
assert(norm(array.span(), 0, 10) == 2, 'invalid l1 norm'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn norm_test_into() { | ||
let mut array: Array<u32> = array![3, 4]; | ||
assert(norm(array.span(), 2, 10) == 5, 'invalid l2 norm'); | ||
} |