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

Fix trigonometric functions to not raise on f32 inputs #1051

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
160 changes: 144 additions & 16 deletions native/explorer/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,50 +1588,178 @@ pub fn s_clip_float(s: ExSeries, min: f64, max: f64) -> Result<ExSeries, Explore

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_sin(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.sin()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.sin()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.sin())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_cos(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.cos()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.cos()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.cos())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_tan(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.tan()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.tan()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.tan())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_asin(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.asin()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.asin()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.asin())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_acos(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.acos()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.acos()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.acos())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_atan(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.atan()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.atan()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.atan())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_degrees(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.to_degrees()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.to_degrees()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.to_degrees())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
pub fn s_radians(s: ExSeries) -> Result<ExSeries, ExplorerError> {
let s1 = s.f64()?.apply_values(|o| o.to_radians()).into();
Ok(ExSeries::new(s1))
match s.dtype() {
DataType::Float64 => {
let s1 = s.f64()?.apply_values(|o| o.to_radians()).into();
Ok(ExSeries::new(s1))
}
DataType::Float32 => {
let s1 = s
.f32()?
.cast(&DataType::Float64)?
.f64()?
.apply_values(|o| o.to_radians())
.into();
Ok(ExSeries::new(s1))
}
_ => Err(ExplorerError::Other(
"Only f32 and f64 dtypes are supported".into(),
)),
}
}

#[rustler::nif(schedule = "DirtyCpu")]
Expand Down
104 changes: 104 additions & 0 deletions test/explorer/series_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3204,6 +3204,22 @@

assert Series.to_list(series) == [0.0, 1.0, 1.2246467991473532e-16, -2.4492935982947064e-16]
end

test "calculates the sine of all elements in the series for f32 input and outputs f64" do
pi = :math.pi()
s = Explorer.Series.from_list([0, pi / 2, pi, 2 * pi], dtype: :f32)

series = Series.sin(s)

assert Series.to_list(series) == [
0.0,
0.999999999999999,
-8.742278000372475e-8,
1.7484556000744883e-7
]

assert Series.dtype(series) == {:f, 64}
end
end

describe "cos/1" do
Expand All @@ -3215,6 +3231,22 @@

assert Series.to_list(series) == [1.0, 6.123233995736766e-17, -1.0, 1.0]
end

test "calculates the cosine of all elements in the series for f32 input and outputs f64" do
pi = :math.pi()
s = Explorer.Series.from_list([0, pi / 2, pi, 2 * pi], dtype: :f32)

series = Series.cos(s)

assert Series.to_list(series) == [
1.0,
-4.371139000186241e-8,
-0.9999999999999962,
0.9999999999999847
]

assert Series.dtype(series) == {:f, 64}
end
end

describe "tan/1" do
Expand All @@ -3231,6 +3263,22 @@
-2.4492935982947064e-16
]
end

test "calculates the tangent of all elements in the series for f32 input and outputs f64" do

Check failure on line 3267 in test/explorer/series_test.exs

View workflow job for this annotation

GitHub Actions / test (25.3, 1.14)

test tan/1 calculates the tangent of all elements in the series for f32 input and outputs f64 (Explorer.SeriesTest)

Check failure on line 3267 in test/explorer/series_test.exs

View workflow job for this annotation

GitHub Actions / test (27.2, 1.18)

test tan/1 calculates the tangent of all elements in the series for f32 input and outputs f64 (Explorer.SeriesTest)
pi = :math.pi()
s = Explorer.Series.from_list([0, pi / 2, pi, 2 * pi], dtype: :f32)

series = Series.tan(s)

assert Series.to_list(series) == [
0.0,
-22_877_332.42885646,
8.742278000372508e-8,
1.7484556000745148e-7
]

assert Series.dtype(series) == {:f, 64}
end
end

describe "asin/1" do
Expand All @@ -3241,6 +3289,15 @@

assert Series.to_list(series) == [0.0, 1.5707963267948966]
end

test "calculates the arcsine of all elements in the series for f32 input and outputs f64" do
s = Explorer.Series.from_list([0.0, 1.0], dtype: :f32)

series = Series.asin(s)

assert Series.to_list(series) == [0.0, 1.5707963267948966]
assert Series.dtype(series) == {:f, 64}
end
end

describe "acos/1" do
Expand All @@ -3251,6 +3308,15 @@

assert Series.to_list(series) == [1.5707963267948966, 0.0]
end

test "calculates the arccosine of all elements in the series for f32 input and outputs f64" do
s = Explorer.Series.from_list([0.0, 1.0], dtype: :f32)

series = Series.acos(s)

assert Series.to_list(series) == [1.5707963267948966, 0.0]
assert Series.dtype(series) == {:f, 64}
end
end

describe "atan/1" do
Expand All @@ -3261,6 +3327,15 @@

assert Series.to_list(series) == [0.0, 0.7853981633974483]
end

test "calculates the arctangent of all elements in the series for f32 input and outputs f64" do
s = Explorer.Series.from_list([0.0, 1.0], dtype: :f32)

series = Series.atan(s)

assert Series.to_list(series) == [0.0, 0.7853981633974483]
assert Series.dtype(series) == {:f, 64}
end
end

describe "degrees/1" do
Expand All @@ -3272,6 +3347,25 @@

assert Series.to_list(series) == [-360.0, -180.0, -90.0, 0.0, 90.0, 180.0, 360.0]
end

test "converts the given series of radians to degrees for f32 input and outputs f64" do
pi = :math.pi()
s = Explorer.Series.from_list([-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi], dtype: :f32)

series = Series.degrees(s)

assert Series.to_list(series) == [
-360.00001001791264,
-180.00000500895632,
-90.00000250447816,
0.0,
90.00000250447816,
180.00000500895632,
360.00001001791264
]

assert Series.dtype(series) == {:f, 64}
end
end

describe "radians/1" do
Expand All @@ -3283,6 +3377,16 @@

assert Series.to_list(series) == [-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi]
end

test "converts the given series of degrees to radians for f32 input and outputs f64" do
pi = :math.pi()
s = Explorer.Series.from_list([-360.0, -180.0, -90.0, 0.0, 90.0, 180.0, 360.0], dtype: :f32)

series = Series.radians(s)

assert Series.to_list(series) == [-2 * pi, -pi, -pi / 2, 0, pi / 2, pi, 2 * pi]
assert Series.dtype(series) == {:f, 64}
end
end

describe "format/1" do
Expand Down
Loading