-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT]: sin/cos/tan expression implementation
- Loading branch information
Showing
15 changed files
with
280 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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use num_traits::Float; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use common_error::DaftResult; | ||
|
||
use crate::array::DataArray; | ||
use crate::datatypes::DaftFloatType; | ||
|
||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
pub enum TrigonometricFunction { | ||
Sin, | ||
Cos, | ||
Tan, | ||
} | ||
|
||
impl TrigonometricFunction { | ||
pub fn fn_name(&self) -> &'static str { | ||
use TrigonometricFunction::*; | ||
match self { | ||
Sin => "sin", | ||
Cos => "cos", | ||
Tan => "tan", | ||
} | ||
} | ||
} | ||
|
||
impl<T> DataArray<T> | ||
where | ||
T: DaftFloatType, | ||
T::Native: Float, | ||
{ | ||
pub fn trigonometry(&self, func: &TrigonometricFunction) -> DaftResult<Self> { | ||
use TrigonometricFunction::*; | ||
match func { | ||
Sin => self.apply(|v| v.sin()), | ||
Cos => self.apply(|v| v.cos()), | ||
Tan => self.apply(|v| v.tan()), | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::array::ops::trigonometry::TrigonometricFunction; | ||
use crate::datatypes::DataType; | ||
use crate::series::Series; | ||
use crate::IntoSeries; | ||
use common_error::DaftError; | ||
use common_error::DaftResult; | ||
|
||
impl Series { | ||
pub fn trigonometry(&self, trig_function: &TrigonometricFunction) -> DaftResult<Self> { | ||
match self.data_type() { | ||
DataType::Float32 => { | ||
let ca = self.f32().unwrap(); | ||
Ok(ca.trigonometry(trig_function)?.into_series()) | ||
} | ||
DataType::Float64 => { | ||
let ca = self.f64().unwrap(); | ||
Ok(ca.trigonometry(trig_function)?.into_series()) | ||
} | ||
dt if dt.is_numeric() => { | ||
let s = self.cast(&DataType::Float64)?; | ||
let ca = s.f64().unwrap(); | ||
Ok(ca.trigonometry(trig_function)?.into_series()) | ||
} | ||
dt => Err(DaftError::TypeError(format!( | ||
"Expected input to trigonometry to be numeric, got {}", | ||
dt | ||
))), | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use common_error::{DaftError, DaftResult}; | ||
pub use daft_core::array::ops::trigonometry::TrigonometricFunction; | ||
use daft_core::datatypes::Field; | ||
use daft_core::schema::Schema; | ||
use daft_core::{DataType, Series}; | ||
|
||
use crate::functions::FunctionEvaluator; | ||
use crate::Expr; | ||
|
||
pub(super) struct TrigonometryEvaluator(pub TrigonometricFunction); | ||
|
||
impl FunctionEvaluator for TrigonometryEvaluator { | ||
fn fn_name(&self) -> &'static str { | ||
self.0.fn_name() | ||
} | ||
|
||
fn to_field(&self, inputs: &[Expr], schema: &Schema, _: &Expr) -> DaftResult<Field> { | ||
if inputs.len() != 1 { | ||
return Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))); | ||
}; | ||
let field = inputs.first().unwrap().to_field(schema)?; | ||
let dtype = match field.dtype { | ||
DataType::Float32 => DataType::Float32, | ||
dt if dt.is_numeric() => DataType::Float64, | ||
_ => { | ||
return Err(DaftError::TypeError(format!( | ||
"Expected input to trigonometry to be numeric, got {}", | ||
field.dtype | ||
))) | ||
} | ||
}; | ||
Ok(Field::new(field.name, dtype)) | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series], _: &Expr) -> DaftResult<Series> { | ||
if inputs.len() != 1 { | ||
return Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))); | ||
} | ||
inputs.first().unwrap().trigonometry(&self.0) | ||
} | ||
} |
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
Oops, something went wrong.