1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Unary operations on GPU-resident columns.
//!
//! Provides element-wise mathematical functions, null/NaN checks,
//! and type casting for [`Column`]s.
//!
//! # Examples
//!
//! ```rust,no_run
//! use cudf::{Column, DataType, TypeId};
//! use cudf::unary::UnaryOp;
//!
//! let col = Column::from_slice(&[1.0f64, 4.0, 9.0]).unwrap();
//! let result = col.unary_op(UnaryOp::Sqrt).unwrap();
//!
//! // Cast to a different type
//! let as_i32 = col.cast(DataType::new(TypeId::Int32)).unwrap();
//! ```
use crate::column::Column;
use crate::error::{CudfError, Result};
use crate::types::DataType;
/// Unary operations supported by libcudf.
///
/// These map to `cudf::unary_operator` enum values in libcudf 26.x.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum UnaryOp {
/// Trigonometric sine
Sin = 0,
/// Trigonometric cosine
Cos = 1,
/// Trigonometric tangent
Tan = 2,
/// Inverse sine
Arcsin = 3,
/// Inverse cosine
Arccos = 4,
/// Inverse tangent
Arctan = 5,
/// Hyperbolic sine
Sinh = 6,
/// Hyperbolic cosine
Cosh = 7,
/// Hyperbolic tangent
Tanh = 8,
/// Inverse hyperbolic sine
Arcsinh = 9,
/// Inverse hyperbolic cosine
Arccosh = 10,
/// Inverse hyperbolic tangent
Arctanh = 11,
/// Exponential (e^x)
Exp = 12,
/// Natural logarithm
Log = 13,
/// Square root
Sqrt = 14,
/// Cube root
Cbrt = 15,
/// Ceiling
Ceil = 16,
/// Floor
Floor = 17,
/// Absolute value
Abs = 18,
/// Round to nearest integer
Rint = 19,
/// Count of set bits
BitCount = 20,
/// Bitwise invert
BitInvert = 21,
/// Logical not
Not = 22,
/// Negate
Negate = 23,
}
impl Column {
/// Apply a unary operation element-wise, returning a new column.
///
/// # Examples
///
/// ```rust,no_run
/// use cudf::Column;
/// use cudf::unary::UnaryOp;
///
/// let col = Column::from_slice(&[1.0f64, 4.0, 9.0]).unwrap();
/// let sqrt = col.unary_op(UnaryOp::Sqrt).unwrap();
/// ```
pub fn unary_op(&self, op: UnaryOp) -> Result<Column> {
let result = cudf_cxx::unary::ffi::unary_operation(&self.inner, op as i32)
.map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
/// Return a bool8 column indicating which elements are null.
pub fn is_null(&self) -> Result<Column> {
let result = cudf_cxx::unary::ffi::is_null(&self.inner).map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
/// Return a bool8 column indicating which elements are valid (non-null).
pub fn is_valid(&self) -> Result<Column> {
let result = cudf_cxx::unary::ffi::is_valid(&self.inner).map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
/// Return a bool8 column indicating which elements are NaN.
///
/// Only applicable to floating-point columns.
pub fn is_nan(&self) -> Result<Column> {
let result = cudf_cxx::unary::ffi::is_nan(&self.inner).map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
/// Return a bool8 column indicating which elements are not NaN.
///
/// Only applicable to floating-point columns.
pub fn is_not_nan(&self) -> Result<Column> {
let result = cudf_cxx::unary::ffi::is_not_nan(&self.inner).map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
/// Return a bool8 column indicating which elements are +/-infinity.
///
/// Only applicable to floating-point columns.
pub fn is_inf(&self) -> Result<Column> {
let result = cudf_cxx::unary::ffi::is_inf(&self.inner).map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
/// Return a bool8 column indicating which elements are not +/-infinity.
///
/// Only applicable to floating-point columns.
pub fn is_not_inf(&self) -> Result<Column> {
let result = cudf_cxx::unary::ffi::is_not_inf(&self.inner).map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
/// Cast this column to a different data type.
///
/// # Examples
///
/// ```rust,no_run
/// use cudf::{Column, DataType, TypeId};
///
/// let col = Column::from_slice(&[1.5f64, 2.7, 3.1]).unwrap();
/// let as_i32 = col.cast(DataType::new(TypeId::Int32)).unwrap();
/// ```
pub fn cast(&self, dtype: DataType) -> Result<Column> {
let result = cudf_cxx::unary::ffi::cast(&self.inner, dtype.id() as i32)
.map_err(CudfError::from_cxx)?;
Ok(Column { inner: result })
}
}
/// Check if a cast between two data types is supported.
///
/// Returns `true` if casting from `from` to `to` is supported.
pub fn is_supported_cast(from: DataType, to: DataType) -> bool {
cudf_cxx::unary::ffi::is_supported_cast(from.id() as i32, to.id() as i32)
}