Skip to content

Commit 104b040

Browse files
authored
GH-44952: [C++][Python] Add Hyperbolic Trig functions (#44630)
### Rationale for this change Hyperbolic trigonometric functions are a common transformation for dealing with skewed data. And they are built into the core C++ libraries so require minimal change. ### What changes are included in this PR? Adding `a?(sin|cos|tan)h` to the base C++ library and substrait and tests for these functions in pyarrow. ### Are these changes tested? Yes, in the same style as the trigonometric functions. ### Are there any user-facing changes? Yes. Additional compute functions are added. * GitHub Issue: #44952 Authored-by: Kevin H Wilson <[email protected]> Signed-off-by: Felipe Oliveira Carvalho <[email protected]>
1 parent f1b293d commit 104b040

7 files changed

Lines changed: 320 additions & 13 deletions

File tree

‎cpp/src/arrow/compute/api_scalar.cc‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,20 +732,26 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
732732

733733
SCALAR_ARITHMETIC_UNARY(AbsoluteValue, "abs", "abs_checked")
734734
SCALAR_ARITHMETIC_UNARY(Acos, "acos", "acos_checked")
735+
SCALAR_ARITHMETIC_UNARY(Acosh, "acosh", "acosh_checked")
735736
SCALAR_ARITHMETIC_UNARY(Asin, "asin", "asin_checked")
737+
SCALAR_ARITHMETIC_UNARY(Atanh, "atanh", "atanh_checked")
736738
SCALAR_ARITHMETIC_UNARY(Cos, "cos", "cos_checked")
737739
SCALAR_ARITHMETIC_UNARY(Ln, "ln", "ln_checked")
738740
SCALAR_ARITHMETIC_UNARY(Log10, "log10", "log10_checked")
739741
SCALAR_ARITHMETIC_UNARY(Log1p, "log1p", "log1p_checked")
740742
SCALAR_ARITHMETIC_UNARY(Log2, "log2", "log2_checked")
741-
SCALAR_ARITHMETIC_UNARY(Sqrt, "sqrt", "sqrt_checked")
742743
SCALAR_ARITHMETIC_UNARY(Negate, "negate", "negate_checked")
743744
SCALAR_ARITHMETIC_UNARY(Sin, "sin", "sin_checked")
745+
SCALAR_ARITHMETIC_UNARY(Sqrt, "sqrt", "sqrt_checked")
744746
SCALAR_ARITHMETIC_UNARY(Tan, "tan", "tan_checked")
747+
SCALAR_EAGER_UNARY(Asinh, "asinh")
745748
SCALAR_EAGER_UNARY(Atan, "atan")
749+
SCALAR_EAGER_UNARY(Cosh, "cosh")
746750
SCALAR_EAGER_UNARY(Exp, "exp")
747751
SCALAR_EAGER_UNARY(Expm1, "expm1")
748752
SCALAR_EAGER_UNARY(Sign, "sign")
753+
SCALAR_EAGER_UNARY(Sinh, "sinh")
754+
SCALAR_EAGER_UNARY(Tanh, "tanh")
749755

750756
Result<Datum> Round(const Datum& arg, RoundOptions options, ExecContext* ctx) {
751757
return CallFunction("round", {arg}, &options, ctx);

‎cpp/src/arrow/compute/api_scalar.h‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,52 @@ Result<Datum> Atan(const Datum& arg, ExecContext* ctx = NULLPTR);
784784
ARROW_EXPORT
785785
Result<Datum> Atan2(const Datum& y, const Datum& x, ExecContext* ctx = NULLPTR);
786786

787+
/// \brief Compute the hyperbolic sine of the array values.
788+
/// \param[in] arg The values to compute the hyperbolic sine for.
789+
/// \param[in] ctx the function execution context, optional
790+
/// \return the elementwise hyperbolic sine of the values
791+
ARROW_EXPORT
792+
Result<Datum> Sinh(const Datum& arg, ExecContext* ctx = NULLPTR);
793+
794+
/// \brief Compute the hyperbolic cosine of the array values.
795+
/// \param[in] arg The values to compute the hyperbolic cosine for.
796+
/// \param[in] ctx the function execution context, optional
797+
/// \return the elementwise hyperbolic cosine of the values
798+
ARROW_EXPORT
799+
Result<Datum> Cosh(const Datum& arg, ExecContext* ctx = NULLPTR);
800+
801+
/// \brief Compute the hyperbolic tangent of the array values.
802+
/// \param[in] arg The values to compute the hyperbolic tangent for.
803+
/// \param[in] ctx the function execution context, optional
804+
/// \return the elementwise hyperbolic tangent of the values
805+
ARROW_EXPORT
806+
Result<Datum> Tanh(const Datum& arg, ExecContext* ctx = NULLPTR);
807+
808+
/// \brief Compute the inverse hyperbolic sine of the array values.
809+
/// \param[in] arg The values to compute the inverse hyperbolic sine for.
810+
/// \param[in] ctx the function execution context, optional
811+
/// \return the elementwise inverse hyperbolic sine of the values
812+
ARROW_EXPORT
813+
Result<Datum> Asinh(const Datum& arg, ExecContext* ctx = NULLPTR);
814+
815+
/// \brief Compute the inverse hyperbolic cosine of the array values.
816+
/// \param[in] arg The values to compute the inverse hyperbolic cosine for.
817+
/// \param[in] options arithmetic options (enable/disable overflow checking), optional
818+
/// \param[in] ctx the function execution context, optional
819+
/// \return the elementwise inverse hyperbolic cosine of the values
820+
ARROW_EXPORT
821+
Result<Datum> Acosh(const Datum& arg, ArithmeticOptions options = ArithmeticOptions(),
822+
ExecContext* ctx = NULLPTR);
823+
824+
/// \brief Compute the inverse hyperbolic tangent of the array values.
825+
/// \param[in] arg The values to compute the inverse hyperbolic tangent for.
826+
/// \param[in] options arithmetic options (enable/disable overflow checking), optional
827+
/// \param[in] ctx the function execution context, optional
828+
/// \return the elementwise inverse hyperbolic tangent of the values
829+
ARROW_EXPORT
830+
Result<Datum> Atanh(const Datum& arg, ArithmeticOptions options = ArithmeticOptions(),
831+
ExecContext* ctx = NULLPTR);
832+
787833
/// \brief Get the natural log of a value.
788834
///
789835
/// If argument is null the result will be null.

‎cpp/src/arrow/compute/kernels/scalar_arithmetic.cc‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ struct SinChecked {
178178
}
179179
};
180180

181+
struct Sinh {
182+
template <typename T, typename Arg0>
183+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
184+
static_assert(std::is_same<T, Arg0>::value, "");
185+
return std::sinh(val);
186+
}
187+
};
188+
181189
struct Cos {
182190
template <typename T, typename Arg0>
183191
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
@@ -198,6 +206,14 @@ struct CosChecked {
198206
}
199207
};
200208

209+
struct Cosh {
210+
template <typename T, typename Arg0>
211+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
212+
static_assert(std::is_same<T, Arg0>::value, "");
213+
return std::cosh(val);
214+
}
215+
};
216+
201217
struct Tan {
202218
template <typename T, typename Arg0>
203219
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
@@ -219,6 +235,14 @@ struct TanChecked {
219235
}
220236
};
221237

238+
struct Tanh {
239+
template <typename T, typename Arg0>
240+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
241+
static_assert(std::is_same<T, Arg0>::value, "");
242+
return std::tanh(val);
243+
}
244+
};
245+
222246
struct Asin {
223247
template <typename T, typename Arg0>
224248
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
@@ -242,6 +266,14 @@ struct AsinChecked {
242266
}
243267
};
244268

269+
struct Asinh {
270+
template <typename T, typename Arg0>
271+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
272+
static_assert(std::is_same<T, Arg0>::value, "");
273+
return std::asinh(val);
274+
}
275+
};
276+
245277
struct Acos {
246278
template <typename T, typename Arg0>
247279
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
@@ -265,6 +297,29 @@ struct AcosChecked {
265297
}
266298
};
267299

300+
struct Acosh {
301+
template <typename T, typename Arg0>
302+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
303+
static_assert(std::is_same<T, Arg0>::value, "");
304+
if (ARROW_PREDICT_FALSE(val < 1.0)) {
305+
return std::numeric_limits<T>::quiet_NaN();
306+
}
307+
return std::acosh(val);
308+
}
309+
};
310+
311+
struct AcoshChecked {
312+
template <typename T, typename Arg0>
313+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status* st) {
314+
static_assert(std::is_same<T, Arg0>::value, "");
315+
if (ARROW_PREDICT_FALSE(val < 1.0)) {
316+
*st = Status::Invalid("domain error");
317+
return val;
318+
}
319+
return std::acosh(val);
320+
}
321+
};
322+
268323
struct Atan {
269324
template <typename T, typename Arg0>
270325
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
@@ -273,6 +328,35 @@ struct Atan {
273328
}
274329
};
275330

331+
struct Atanh {
332+
template <typename T, typename Arg0>
333+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status*) {
334+
static_assert(std::is_same<T, Arg0>::value, "");
335+
if (ARROW_PREDICT_FALSE((val < -1.0 || val > 1.0))) {
336+
// N.B. This predicate does *not* match the predicate in AtanhChecked. In
337+
// GH-44630 it was decided that the checked version should error when asked
338+
// for +/- 1 as an input and the unchecked version should return +/- oo
339+
return std::numeric_limits<T>::quiet_NaN();
340+
}
341+
return std::atanh(val);
342+
}
343+
};
344+
345+
struct AtanhChecked {
346+
template <typename T, typename Arg0>
347+
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 val, Status* st) {
348+
static_assert(std::is_same<T, Arg0>::value, "");
349+
if (ARROW_PREDICT_FALSE((val <= -1.0 || val >= 1.0))) {
350+
// N.B. This predicate does *not* match the predicate in Atanh. In GH-44630 it was
351+
// decided that the checked version should error when asked for +/- 1 as an input
352+
// and the unchecked version should return +/- oo
353+
*st = Status::Invalid("domain error");
354+
return val;
355+
}
356+
return std::atanh(val);
357+
}
358+
};
359+
276360
struct Atan2 {
277361
template <typename T, typename Arg0, typename Arg1>
278362
static enable_if_floating_value<Arg0, T> Call(KernelContext*, Arg0 y, Arg1 x, Status*) {
@@ -1178,6 +1262,8 @@ const FunctionDoc sin_checked_doc{"Compute the sine",
11781262
"to return NaN instead, see \"sin\"."),
11791263
{"x"}};
11801264

1265+
const FunctionDoc sinh_doc{"Compute the hyperbolic sine", (""), {"x"}};
1266+
11811267
const FunctionDoc cos_doc{"Compute the cosine",
11821268
("NaN is returned for invalid input values;\n"
11831269
"to raise an error instead, see \"cos_checked\"."),
@@ -1188,6 +1274,8 @@ const FunctionDoc cos_checked_doc{"Compute the cosine",
11881274
"to return NaN instead, see \"cos\"."),
11891275
{"x"}};
11901276

1277+
const FunctionDoc cosh_doc{"Compute the hyperbolic cosine", (""), {"x"}};
1278+
11911279
const FunctionDoc tan_doc{"Compute the tangent",
11921280
("NaN is returned for invalid input values;\n"
11931281
"to raise an error instead, see \"tan_checked\"."),
@@ -1198,6 +1286,8 @@ const FunctionDoc tan_checked_doc{"Compute the tangent",
11981286
"to return NaN instead, see \"tan\"."),
11991287
{"x"}};
12001288

1289+
const FunctionDoc tanh_doc{"Compute the hyperbolic tangent", (""), {"x"}};
1290+
12011291
const FunctionDoc asin_doc{"Compute the inverse sine",
12021292
("NaN is returned for invalid input values;\n"
12031293
"to raise an error instead, see \"asin_checked\"."),
@@ -1208,6 +1298,8 @@ const FunctionDoc asin_checked_doc{"Compute the inverse sine",
12081298
"to return NaN instead, see \"asin\"."),
12091299
{"x"}};
12101300

1301+
const FunctionDoc asinh_doc{"Compute the inverse hyperbolic sine", (""), {"x"}};
1302+
12111303
const FunctionDoc acos_doc{"Compute the inverse cosine",
12121304
("NaN is returned for invalid input values;\n"
12131305
"to raise an error instead, see \"acos_checked\"."),
@@ -1218,6 +1310,16 @@ const FunctionDoc acos_checked_doc{"Compute the inverse cosine",
12181310
"to return NaN instead, see \"acos\"."),
12191311
{"x"}};
12201312

1313+
const FunctionDoc acosh_doc{"Compute the inverse hyperbolic cosine",
1314+
("NaN is returned for input values < 1.0;\n"
1315+
"to raise an error instead, see \"acosh_checked\"."),
1316+
{"x"}};
1317+
1318+
const FunctionDoc acosh_checked_doc{"Compute the inverse hyperbolic cosine",
1319+
("Input values < 1.0 raise an error;\n"
1320+
"to return NaN instead, see \"acosh\"."),
1321+
{"x"}};
1322+
12211323
const FunctionDoc atan_doc{"Compute the inverse tangent of x",
12221324
("The return value is in the range [-pi/2, pi/2];\n"
12231325
"for a full return range [-pi, pi], see \"atan2\"."),
@@ -1227,6 +1329,17 @@ const FunctionDoc atan2_doc{"Compute the inverse tangent of y/x",
12271329
("The return value is in the range [-pi, pi]."),
12281330
{"y", "x"}};
12291331

1332+
const FunctionDoc atanh_doc{"Compute the inverse hyperbolic tangent",
1333+
("NaN is returned for input values x with |x| > 1.\n"
1334+
"At x = +/- 1, returns +/- infinity.\n"
1335+
"To raise an error instead, see \"atanh_checked\"."),
1336+
{"x"}};
1337+
1338+
const FunctionDoc atanh_checked_doc{"Compute the inverse hyperbolic tangent",
1339+
("Input values x with |x| >= 1.0 raise an error\n"
1340+
"to return NaN instead, see \"atanh\"."),
1341+
{"x"}};
1342+
12301343
const FunctionDoc ln_doc{
12311344
"Compute natural logarithm",
12321345
("Non-positive values return -inf or NaN. Null values return null.\n"
@@ -1691,40 +1804,66 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
16911804
"sin_checked", sin_checked_doc);
16921805
DCHECK_OK(registry->AddFunction(std::move(sin_checked)));
16931806

1807+
auto sinh = MakeUnaryArithmeticFunctionFloatingPoint<Sinh>("sinh", sinh_doc);
1808+
DCHECK_OK(registry->AddFunction(std::move(sinh)));
1809+
16941810
auto cos = MakeUnaryArithmeticFunctionFloatingPoint<Cos>("cos", cos_doc);
16951811
DCHECK_OK(registry->AddFunction(std::move(cos)));
16961812

16971813
auto cos_checked = MakeUnaryArithmeticFunctionFloatingPointNotNull<CosChecked>(
16981814
"cos_checked", cos_checked_doc);
16991815
DCHECK_OK(registry->AddFunction(std::move(cos_checked)));
17001816

1817+
auto cosh = MakeUnaryArithmeticFunctionFloatingPoint<Cosh>("cosh", cosh_doc);
1818+
DCHECK_OK(registry->AddFunction(std::move(cosh)));
1819+
17011820
auto tan = MakeUnaryArithmeticFunctionFloatingPoint<Tan>("tan", tan_doc);
17021821
DCHECK_OK(registry->AddFunction(std::move(tan)));
17031822

17041823
auto tan_checked = MakeUnaryArithmeticFunctionFloatingPointNotNull<TanChecked>(
17051824
"tan_checked", tan_checked_doc);
17061825
DCHECK_OK(registry->AddFunction(std::move(tan_checked)));
17071826

1827+
auto tanh = MakeUnaryArithmeticFunctionFloatingPoint<Tanh>("tanh", tanh_doc);
1828+
DCHECK_OK(registry->AddFunction(std::move(tanh)));
1829+
17081830
auto asin = MakeUnaryArithmeticFunctionFloatingPoint<Asin>("asin", asin_doc);
17091831
DCHECK_OK(registry->AddFunction(std::move(asin)));
17101832

17111833
auto asin_checked = MakeUnaryArithmeticFunctionFloatingPointNotNull<AsinChecked>(
17121834
"asin_checked", asin_checked_doc);
17131835
DCHECK_OK(registry->AddFunction(std::move(asin_checked)));
17141836

1837+
auto asinh = MakeUnaryArithmeticFunctionFloatingPoint<Asinh>("asinh", asinh_doc);
1838+
DCHECK_OK(registry->AddFunction(std::move(asinh)));
1839+
17151840
auto acos = MakeUnaryArithmeticFunctionFloatingPoint<Acos>("acos", acos_doc);
17161841
DCHECK_OK(registry->AddFunction(std::move(acos)));
17171842

17181843
auto acos_checked = MakeUnaryArithmeticFunctionFloatingPointNotNull<AcosChecked>(
17191844
"acos_checked", acos_checked_doc);
17201845
DCHECK_OK(registry->AddFunction(std::move(acos_checked)));
17211846

1847+
auto acosh = MakeUnaryArithmeticFunctionFloatingPoint<Acosh>("acosh", acosh_doc);
1848+
DCHECK_OK(registry->AddFunction(std::move(acosh)));
1849+
1850+
auto acosh_checked = MakeUnaryArithmeticFunctionFloatingPointNotNull<AcoshChecked>(
1851+
"acosh_checked", acosh_checked_doc);
1852+
DCHECK_OK(registry->AddFunction(std::move(acosh_checked)));
1853+
17221854
auto atan = MakeUnaryArithmeticFunctionFloatingPoint<Atan>("atan", atan_doc);
17231855
DCHECK_OK(registry->AddFunction(std::move(atan)));
17241856

17251857
auto atan2 = MakeArithmeticFunctionFloatingPoint<Atan2>("atan2", atan2_doc);
17261858
DCHECK_OK(registry->AddFunction(std::move(atan2)));
17271859

1860+
auto atanh = MakeUnaryArithmeticFunctionFloatingPoint<Atanh>("atanh", atanh_doc);
1861+
DCHECK_OK(registry->AddFunction(std::move(atanh)));
1862+
1863+
auto atanh_checked = MakeUnaryArithmeticFunctionFloatingPointNotNull<AtanhChecked>(
1864+
"atanh_checked", atanh_checked_doc);
1865+
DCHECK_OK(registry->AddFunction(std::move(atanh_checked)));
1866+
17281867
// ----------------------------------------------------------------------
17291868
// Logarithms
17301869
auto ln = MakeUnaryArithmeticFunctionFloatingPoint<LogNatural>("ln", ln_doc);

0 commit comments

Comments
 (0)