Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Simplified arithemtics kernels.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Nov 15, 2021
1 parent d0c0187 commit a367ac5
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 324 deletions.
71 changes: 70 additions & 1 deletion src/compute/arithmetics/basic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Contains arithemtic functions for [`PrimitiveArray`](crate::array::PrimitiveArray)s.
//! Contains arithemtic functions for [`PrimitiveArray`]s.
//!
//! Each operation has four variants, like the rest of Rust's ecosystem:
//! * usual, that [`panic!`]s on overflow
Expand All @@ -20,3 +20,72 @@ pub use sub::*;

mod common;
pub(crate) use common::*;

use std::ops::Neg;

use num_traits::{CheckedNeg, WrappingNeg};

use crate::{
array::{Array, PrimitiveArray},
types::NativeType,
};

use super::super::arity::{unary, unary_checked};

/// Negates values from array.
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::negate;
/// use arrow2::array::PrimitiveArray;
///
/// let a = PrimitiveArray::from([None, Some(6), None, Some(7)]);
/// let result = negate(&a);
/// let expected = PrimitiveArray::from([None, Some(-6), None, Some(-7)]);
/// assert_eq!(result, expected)
/// ```
pub fn negate<T>(array: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + Neg<Output = T>,
{
unary(array, |a| -a, array.data_type().clone())
}

/// Checked negates values from array.
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::checked_negate;
/// use arrow2::array::{Array, PrimitiveArray};
///
/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]);
/// let result = checked_negate(&a);
/// let expected = PrimitiveArray::from([None, Some(-6), None, Some(-7)]);
/// assert_eq!(result, expected);
/// assert!(!result.is_valid(2))
/// ```
pub fn checked_negate<T>(array: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + CheckedNeg,
{
unary_checked(array, |a| a.checked_neg(), array.data_type().clone())
}

/// Wrapping negates values from array.
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::wrapping_negate;
/// use arrow2::array::{Array, PrimitiveArray};
///
/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]);
/// let result = wrapping_negate(&a);
/// let expected = PrimitiveArray::from([None, Some(-6), Some(i8::MIN), Some(-7)]);
/// assert_eq!(result, expected);
/// ```
pub fn wrapping_negate<T>(array: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + WrappingNeg,
{
unary(array, |a| a.wrapping_neg(), array.data_type().clone())
}
12 changes: 8 additions & 4 deletions src/compute/arithmetics/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
//! precision and scale parameters. These affect the arithmetic operations and
//! need to be considered while doing operations with Decimal numbers.
pub mod add;
pub mod div;
pub mod mul;
pub mod sub;
mod add;
pub use add::*;
mod div;
pub use div::*;
mod mul;
pub use mul::*;
mod sub;
pub use sub::*;

use crate::datatypes::DataType;
use crate::error::{ArrowError, Result};
Expand Down
Loading

0 comments on commit a367ac5

Please sign in to comment.