You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/01/26 17:47:43 UTC

[GitHub] [arrow] jorgecarleitao commented on a change in pull request #9313: [Rust] [Experiment] Trigonometry kernels

jorgecarleitao commented on a change in pull request #9313:
URL: https://github.com/apache/arrow/pull/9313#discussion_r564708601



##########
File path: rust/arrow/src/compute/kernels/trigonometry.rs
##########
@@ -0,0 +1,248 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Defines trigonometry kernels for `PrimitiveArrays` that are
+//! floats, restricted by the [num::Float] trait.
+
+use std::{iter::FromIterator, ops::Div};
+
+use num::{Float, Zero};
+
+use super::math::*;
+use crate::buffer::Buffer;
+use crate::datatypes::ArrowNumericType;
+use crate::error::Result;
+use crate::{
+    array::*,
+    compute::{add, math_op, multiply, subtract},
+    datatypes::ArrowPrimitiveType,
+    error::ArrowError,
+    float_unary,
+};
+
+use super::unary::{into_primitive_array_data, unary};
+
+float_unary!(to_degrees);
+float_unary!(to_radians);
+float_unary!(sin);
+float_unary!(cos);
+float_unary!(tan);
+float_unary!(asin);
+float_unary!(acos);
+float_unary!(atan);
+float_unary!(sinh);
+float_unary!(cosh);
+float_unary!(tanh);
+float_unary!(asinh);
+float_unary!(acosh);
+float_unary!(atanh);
+
+pub fn atan2<T>(
+    left: &PrimitiveArray<T>,
+    right: &PrimitiveArray<T>,
+) -> Result<PrimitiveArray<T>>
+where
+    T: ArrowNumericType,
+    T::Native: num::traits::Float,
+{
+    math_op(left, right, |x, y| x.atan2(y))
+}
+
+/// Perform `left * right` operation on two arrays. If either left or right value is null
+/// then the result is also null.
+pub fn sin_cos<T>(array: &PrimitiveArray<T>) -> (PrimitiveArray<T>, PrimitiveArray<T>)
+where
+    T: ArrowNumericType,
+    T::Native: num::traits::Float,
+{
+    let (sin, cos): (Vec<T::Native>, Vec<T::Native>) =
+        array.values().iter().map(|v| v.sin_cos()).unzip();
+    // NOTE: due to `unzip` collecting and splitting the arrays,
+    // this might be slower than Buffer::from_trusted_len_iter
+    let sin_buffer = Buffer::from_iter(sin);
+    let cos_buffer = Buffer::from_iter(cos);
+
+    let sin_data = into_primitive_array_data::<_, T>(array, sin_buffer);
+    let cos_data = into_primitive_array_data::<_, T>(array, cos_buffer);
+    (
+        PrimitiveArray::<T>::from(std::sync::Arc::new(sin_data)),
+        PrimitiveArray::<T>::from(std::sync::Arc::new(cos_data)),
+    )
+}
+
+/// Calculate the Haversine distance between two geographic coordinates.
+/// Based on the haversine crate.
+///
+/// The distance returned is in meters, and the radius must be specified in meters.
+pub fn haversine<T>(
+    lat_a: &PrimitiveArray<T>,
+    lng_a: &PrimitiveArray<T>,
+    lat_b: &PrimitiveArray<T>,
+    lng_b: &PrimitiveArray<T>,
+    radius: impl num::traits::Float,
+) -> Result<PrimitiveArray<T>>
+where
+    T: ArrowPrimitiveType + ArrowNumericType,
+    T::Native: num::traits::Float + Div<Output = T::Native> + Zero + num::NumCast,
+{
+    // Check array lengths, must all equal
+    let len = lat_a.len();
+    if lat_b.len() != len || lng_a.len() != len || lng_b.len() != len {
+        return Err(ArrowError::ComputeError(
+            "Cannot perform math operation on arrays of different length".to_string(),
+        ));
+    }
+    // These casts normally get optimized to f64 as f64
+    let one = num::cast::<_, T::Native>(1.0).unwrap();

Review comment:
       I did not find any easy way of doing this, also. It feels like we would need a generic of a generic. It looks good to me, though.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org