Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ndarray support for arithmetic operations #583

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ byteorder = { default-features = false, optional = true, version = "1.0" }
bytes = { default-features = false, optional = true, version = "1.0" }
diesel1 = { default-features = false, optional = true, package = "diesel", version = "1.0" }
diesel2 = { default-features = false, optional = true, package = "diesel", version = "2.0" }
ndarray = { default-features = false, optional = true, version = "0.15.6" }
num-traits = { default-features = false, features = ["i128"], version = "0.2" }
postgres = { default-features = false, optional = true, version = "0.19" }
rand = { default-features = false, optional = true, version = "0.8" }
Expand Down Expand Up @@ -69,6 +70,7 @@ db-tokio-postgres = ["dep:byteorder", "dep:bytes", "dep:postgres", "std", "dep:t
legacy-ops = []
maths = []
maths-nopanic = ["maths"]
ndarray = ["dep:ndarray"]
rand = ["dep:rand"]
rkyv = ["dep:rkyv"]
rkyv-safe = ["dep:bytecheck", "rkyv/validation"]
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ assert_eq!(total, dec!(27.26));
* [c-repr](#c-repr)
* [legacy-ops](#legacy-ops)
* [maths](#maths)
* [ndarray](#ndarray)
* [rkyv](#rkyv)
* [rocket-traits](#rocket-traits)
* [rust-fuzz](#rust-fuzz)
Expand Down Expand Up @@ -168,6 +169,10 @@ Please note that `ln` and `log10` will panic on invalid input with `checked_ln`
to curb against this. When the `maths` feature was first developed the library would instead return `0` on invalid input. To re-enable this
non-panicking behavior, please use the feature: `maths-nopanic`.

### `ndarray`

Enables arithmetic operations using [`ndarray`](https://github.com/rust-ndarray/ndarray) on arrays of `Decimal`.

### `rand`

Implements `rand::distributions::Distribution<Decimal>` to allow the creation of random instances.
Expand Down
4 changes: 4 additions & 0 deletions make/tests/misc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ args = ["test", "--workspace", "--features=rocket-traits"]
command = "cargo"
args = ["test", "--workspace", "--features=borsh", "--", "--skip", "generated"]

[tasks.test-ndarray]
command = "cargo"
args = ["test", "--workspace", "--features=ndarray", "--", "--skip", "generated"]

[tasks.test-rkyv]
command = "cargo"
args = ["test", "--workspace", "--features=rkyv", "--features=rkyv-safe", "--", "--skip", "generated"]
Expand Down
3 changes: 3 additions & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ pub struct Decimal {
mid: u32,
}

#[cfg(feature = "ndarray")]
impl ndarray::ScalarOperand for Decimal {}

/// `RoundingStrategy` represents the different rounding strategies that can be used by
/// `round_dp_with_strategy`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down
49 changes: 49 additions & 0 deletions tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,55 @@ fn it_can_serialize_deserialize_borsh() {
}
}

#[test]
#[cfg(feature = "ndarray")]
fn it_can_do_scalar_ops_in_ndarray() {
use ndarray::Array1;
use num_traits::FromPrimitive;

let array_a = Array1::from(vec![
Decimal::from_f32(1.0).unwrap(),
Decimal::from_f32(2.0).unwrap(),
Decimal::from_f32(3.0).unwrap(),
]);

// Add
let output = array_a.clone() + Decimal::from_f32(5.0).unwrap();
let expectation = Array1::from(vec![
Decimal::from_f32(6.0).unwrap(),
Decimal::from_f32(7.0).unwrap(),
Decimal::from_f32(8.0).unwrap(),
]);
assert_eq!(output, expectation);

// Sub
let output = array_a.clone() - Decimal::from_f32(5.0).unwrap();
let expectation = Array1::from(vec![
Decimal::from_f32(-4.0).unwrap(),
Decimal::from_f32(-3.0).unwrap(),
Decimal::from_f32(-2.0).unwrap(),
]);
assert_eq!(output, expectation);

// Mul
let output = array_a.clone() * Decimal::from_f32(5.0).unwrap();
let expectation = Array1::from(vec![
Decimal::from_f32(5.0).unwrap(),
Decimal::from_f32(10.0).unwrap(),
Decimal::from_f32(15.0).unwrap(),
]);
assert_eq!(output, expectation);

// Div
let output = array_a / Decimal::from_f32(5.0).unwrap();
let expectation = Array1::from(vec![
Decimal::from_f32(0.2).unwrap(),
Decimal::from_f32(0.4).unwrap(),
Decimal::from_f32(0.6).unwrap(),
]);
assert_eq!(output, expectation);
}

#[test]
#[cfg(feature = "rkyv")]
fn it_can_serialize_deserialize_rkyv() {
Expand Down