Skip to content

Commit

Permalink
Added math funtions acosh, asinh, log1p and log2.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxSagebaum committed Feb 9, 2024
1 parent 5a2a78a commit 924c1d3
Show file tree
Hide file tree
Showing 9 changed files with 671 additions and 2 deletions.
159 changes: 159 additions & 0 deletions include/codi/expressions/real/unaryOperators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ namespace codi {

using std::abs;
using std::acos;
using std::acosh;
using std::asin;
using std::asinh;
using std::atan;
using std::atanh;
using std::cbrt;
Expand All @@ -105,6 +107,8 @@ namespace codi {
using std::isnormal;
using std::log;
using std::log10;
using std::log1p;
using std::log2;
using std::round;
using std::sin;
using std::sinh;
Expand Down Expand Up @@ -190,6 +194,43 @@ namespace codi {

#define OPERATION_LOGIC OperationAcos
#define FUNCTION acosl
#include "unaryOverloads.tpp"

/// UnaryOperation implementation for acosh
template<typename T_Real>
struct OperationAcosh : public UnaryOperation<T_Real> {
public:

using Real = CODI_DD(T_Real, double); ///< See BinaryOperation.

/// \copydoc UnaryOperation::primal
template<typename Arg>
static CODI_INLINE Real primal(Arg const& arg) {
return acosh(arg);
}

/// \copydoc UnaryOperation::gradient
template<typename Arg>
static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
CODI_UNUSED(result);
if (Config::CheckExpressionArguments) {
if (RealTraits::getPassiveValue(arg) <= 1.0) {
CODI_EXCEPTION("acosh outside of (1, inf).(Value: %0.15e)", RealTraits::getPassiveValue(arg));
}
}
return 1.0 / sqrt(arg * arg - 1.0);
}
};
#define OPERATION_LOGIC OperationAcosh
#define FUNCTION acosh
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationAcosh
#define FUNCTION acoshf
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationAcosh
#define FUNCTION acoshl
#include "unaryOverloads.tpp"

/// UnaryOperation implementation for asin
Expand Down Expand Up @@ -227,6 +268,38 @@ namespace codi {

#define OPERATION_LOGIC OperationAsin
#define FUNCTION asinl
#include "unaryOverloads.tpp"

/// UnaryOperation implementation for asinh
template<typename T_Real>
struct OperationAsinh : public UnaryOperation<T_Real> {
public:

using Real = CODI_DD(T_Real, double); ///< See BinaryOperation.

/// \copydoc UnaryOperation::primal
template<typename Arg>
static CODI_INLINE Real primal(Arg const& arg) {
return asinh(arg);
}

/// \copydoc UnaryOperation::gradient
template<typename Arg>
static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
CODI_UNUSED(result);
return 1.0 / sqrt(arg * arg + 1.0);
}
};
#define OPERATION_LOGIC OperationAsinh
#define FUNCTION asinh
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationAsinh
#define FUNCTION asinhf
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationAsinh
#define FUNCTION asinhl
#include "unaryOverloads.tpp"

/// UnaryOperation implementation for atan
Expand Down Expand Up @@ -630,6 +703,80 @@ namespace codi {

#define OPERATION_LOGIC OperationLog10
#define FUNCTION log10l
#include "unaryOverloads.tpp"

/// UnaryOperation implementation for log1p
template<typename T_Real>
struct OperationLog1p : public UnaryOperation<T_Real> {
public:

using Real = CODI_DD(T_Real, double); ///< See BinaryOperation.

/// \copydoc UnaryOperation::primal
template<typename Arg>
static CODI_INLINE Real primal(Arg const& arg) {
return log1p(arg);
}

/// \copydoc UnaryOperation::gradient
template<typename Arg>
static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
CODI_UNUSED(result);
if (Config::CheckExpressionArguments) {
if (0.0 > RealTraits::getPassiveValue(arg)) {
CODI_EXCEPTION("Logarithm of negative value or zero.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
}
}
return 1.0 / (arg + 1.0);
}
};
#define OPERATION_LOGIC OperationLog1p
#define FUNCTION log1p
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationLog1p
#define FUNCTION log1pf
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationLog1p
#define FUNCTION log1pl
#include "unaryOverloads.tpp"

/// UnaryOperation implementation for log2
template<typename T_Real>
struct OperationLog2 : public UnaryOperation<T_Real> {
public:

using Real = CODI_DD(T_Real, double); ///< See BinaryOperation.

/// \copydoc UnaryOperation::primal
template<typename Arg>
static CODI_INLINE Real primal(Arg const& arg) {
return log2(arg);
}

/// \copydoc UnaryOperation::gradient
template<typename Arg>
static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
CODI_UNUSED(result);
if (Config::CheckExpressionArguments) {
if (0.0 > RealTraits::getPassiveValue(arg)) {
CODI_EXCEPTION("Logarithm of negative value or zero.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
}
}
return 1.442695040888963 / arg;
}
};
#define OPERATION_LOGIC OperationLog2
#define FUNCTION log2
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationLog2
#define FUNCTION log2f
#include "unaryOverloads.tpp"

#define OPERATION_LOGIC OperationLog2
#define FUNCTION log2l
#include "unaryOverloads.tpp"

/// Function overload for round
Expand Down Expand Up @@ -912,9 +1059,15 @@ namespace std {
using codi::abs;
using codi::acos;
using codi::acosf;
using codi::acosh;
using codi::acoshf;
using codi::acoshl;
using codi::acosl;
using codi::asin;
using codi::asinf;
using codi::asinh;
using codi::asinhf;
using codi::asinhl;
using codi::asinl;
using codi::atan;
using codi::atanf;
Expand Down Expand Up @@ -957,6 +1110,12 @@ namespace std {
using codi::log10;
using codi::log10f;
using codi::log10l;
using codi::log1p;
using codi::log1pf;
using codi::log1pl;
using codi::log2;
using codi::log2f;
using codi::log2l;
using codi::logf;
using codi::logl;
using codi::round;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct TestOneArgumentExpr1 : public TestInterface {
public:
NAME("OneArgumentExpr1")
IN(1)
OUT(20)
OUT(21)
POINTS(41) = // clang-format off
{
{-10.0000},
Expand Down Expand Up @@ -107,5 +107,6 @@ struct TestOneArgumentExpr1 : public TestInterface {
y[18] = ldexp(x[0], 7); // R
int temp = 0;
y[19] = frexp(x[0], &temp); // R
y[20] = asinh(x[0]); // R
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct TestOneArgumentExpr2 : public TestInterface {
public:
NAME("OneArgumentExpr2")
IN(1)
OUT(4)
OUT(7)
POINTS(20) = // clang-format off
{
{ 0.5000},
Expand Down Expand Up @@ -69,5 +69,8 @@ struct TestOneArgumentExpr2 : public TestInterface {
y[1] = log10(x[0]); // (0, inf)
y[2] = sqrt(x[0]); // [0, inf)
y[3] = tgamma(x[0]); // R currently only defined for positive arguments
y[4] = acosh(x[0]); // (1, inf)
y[5] = log1p(x[0]); // (0, inf)
y[6] = log2(x[0]); // (0, inf)
}
};
Loading

0 comments on commit 924c1d3

Please sign in to comment.