-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::series::Series; | ||
|
||
impl Series<f64> { | ||
pub fn cross_over(&self, rhs: &Series<f64>) -> Series<bool> { | ||
self.gt(rhs) & self.shift(1).lt(&rhs.shift(1)) | ||
} | ||
|
||
pub fn cross_under(&self, rhs: &Series<f64>) -> Series<bool> { | ||
self.lt(rhs) & self.shift(1).gt(&rhs.shift(1)) | ||
} | ||
|
||
pub fn cross(&self, rhs: &Series<f64>) -> Series<bool> { | ||
self.cross_over(rhs) | self.cross_under(rhs) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_cross_over() { | ||
let a = Series::from([5.5, 5.0, 4.5, 3.0, 2.5]); | ||
let b = Series::from([4.5, 2.0, 3.0, 3.5, 2.0]); | ||
let expected: Series<bool> = Series::from([f64::NAN, 0.0, 0.0, 0.0, 1.0]).into(); | ||
|
||
let result = a.cross_over(&b); | ||
|
||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_cross_under() { | ||
let a = Series::from([5.5, 5.0, 4.5, 3.0, 2.5]); | ||
let b = Series::from([4.5, 2.0, 3.0, 3.5, 2.0]); | ||
let expected: Series<bool> = Series::from([f64::NAN, 0.0, 0.0, 1.0, 0.0]).into(); | ||
|
||
let result = a.cross_under(&b); | ||
|
||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_cross() { | ||
let a = Series::from([5.5, 5.0, 4.5, 3.0, 2.5]); | ||
let b = Series::from([4.5, 2.0, 3.0, 3.5, 2.0]); | ||
let expected: Series<bool> = Series::from([f64::NAN, 0.0, 0.0, 1.0, 1.0]).into(); | ||
|
||
let result = a.cross(&b); | ||
|
||
assert_eq!(result, expected); | ||
} | ||
} |
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,4 +1,5 @@ | ||
pub mod bool; | ||
pub mod helper; | ||
pub mod math; | ||
pub mod ops; | ||
pub mod series; |
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