use std::any::Any;
use std::sync::Arc;
use arrow::array::{ArrayRef, Float32Array, Float64Array};
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::{Float32, Float64};
use datafusion_common::{exec_err, DataFusionError, Result};
use datafusion_expr::ColumnarValue;
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
use crate::utils::make_scalar_function;
#[derive(Debug)]
pub struct CotFunc {
signature: Signature,
}
impl Default for CotFunc {
fn default() -> Self {
CotFunc::new()
}
}
impl CotFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::uniform(
1,
vec![Float64, Float32],
Volatility::Immutable,
),
}
}
}
impl ScalarUDFImpl for CotFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"cot"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
match arg_types[0] {
Float32 => Ok(Float32),
_ => Ok(Float64),
}
}
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
make_scalar_function(cot, vec![])(args)
}
}
fn cot(args: &[ArrayRef]) -> Result<ArrayRef> {
match args[0].data_type() {
Float64 => Ok(Arc::new(make_function_scalar_inputs!(
&args[0],
"x",
Float64Array,
{ compute_cot64 }
)) as ArrayRef),
Float32 => Ok(Arc::new(make_function_scalar_inputs!(
&args[0],
"x",
Float32Array,
{ compute_cot32 }
)) as ArrayRef),
other => exec_err!("Unsupported data type {other:?} for function cot"),
}
}
fn compute_cot32(x: f32) -> f32 {
let a = f32::tan(x);
1.0 / a
}
fn compute_cot64(x: f64) -> f64 {
let a = f64::tan(x);
1.0 / a
}
#[cfg(test)]
mod test {
use crate::math::cot::cot;
use arrow::array::{ArrayRef, Float32Array, Float64Array};
use datafusion_common::cast::{as_float32_array, as_float64_array};
use std::sync::Arc;
#[test]
fn test_cot_f32() {
let args: Vec<ArrayRef> =
vec![Arc::new(Float32Array::from(vec![12.1, 30.0, 90.0, -30.0]))];
let result = cot(&args).expect("failed to initialize function cot");
let floats =
as_float32_array(&result).expect("failed to initialize function cot");
let expected = Float32Array::from(vec![
-1.986_460_4,
-0.156_119_96,
-0.501_202_8,
0.156_119_96,
]);
let eps = 1e-6;
assert_eq!(floats.len(), 4);
assert!((floats.value(0) - expected.value(0)).abs() < eps);
assert!((floats.value(1) - expected.value(1)).abs() < eps);
assert!((floats.value(2) - expected.value(2)).abs() < eps);
assert!((floats.value(3) - expected.value(3)).abs() < eps);
}
#[test]
fn test_cot_f64() {
let args: Vec<ArrayRef> =
vec![Arc::new(Float64Array::from(vec![12.1, 30.0, 90.0, -30.0]))];
let result = cot(&args).expect("failed to initialize function cot");
let floats =
as_float64_array(&result).expect("failed to initialize function cot");
let expected = Float64Array::from(vec![
-1.986_458_685_881_4,
-0.156_119_952_161_6,
-0.501_202_783_380_1,
0.156_119_952_161_6,
]);
let eps = 1e-12;
assert_eq!(floats.len(), 4);
assert!((floats.value(0) - expected.value(0)).abs() < eps);
assert!((floats.value(1) - expected.value(1)).abs() < eps);
assert!((floats.value(2) - expected.value(2)).abs() < eps);
assert!((floats.value(3) - expected.value(3)).abs() < eps);
}
}