Skip to content

Commit

Permalink
[JMT] Make sure smooth_abs is 0 at x=0
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Manzanero committed Aug 2, 2024
1 parent 746b4a3 commit ab0ba7d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lion/foundation/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,17 @@ constexpr T smooth_neg_derivative(const T &x, S eps2);
template<bool ActuallySmooth = true, typename T, typename S>
constexpr T smooth_neg_second_derivative(const T &x, S eps2);

template<bool ActuallySmooth = true, bool UseSinAtanFormula = true, typename T, typename S>
template<bool ActuallySmooth = true, bool UseSmoothAbsFormula = true, typename T, typename S>
constexpr T smooth_sign(const T &x, S eps);

template<bool ActuallySmooth = true, bool UseSinAtanFormula = true, typename T, typename S>
template<bool ActuallySmooth = true, bool UseSmoothAbsFormula = true, typename T, typename S>
constexpr T smooth_sign_derivative(const T &x, S eps);

template<bool ActuallySmooth = true, typename T, typename S>
constexpr T smooth_abs(const T &x, S eps2);
constexpr T smooth_abs(const T &x, S eps);

template<bool ActuallySmooth = true, typename T, typename S>
constexpr T smooth_hypot(const T &x, const T &y, S eps2);
constexpr T smooth_hypot(const T &x, const T &y, S eps);

template<bool ActuallySmooth = true, typename T, typename T1, typename S>
constexpr T smooth_max(const T &x, const T1 &lo, S eps);
Expand Down
14 changes: 7 additions & 7 deletions lion/foundation/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ constexpr T smooth_sign(const T &x, S eps)

if constexpr (ActuallySmooth) {
if constexpr (UseSmoothAbsFormula) {
return x / smooth_abs(x, eps * eps);
return x / sqrt(x * x + eps * eps);
}
else {
using std::tanh;
Expand Down Expand Up @@ -370,7 +370,7 @@ constexpr T smooth_sign_derivative(const T &x, S eps)


template<bool ActuallySmooth, typename T, typename S>
constexpr T smooth_abs(const T &x, S eps2)
constexpr T smooth_abs(const T &x, S eps)
{
//
// Returns the absolute value of input "x", smoothed
Expand All @@ -383,20 +383,20 @@ constexpr T smooth_abs(const T &x, S eps2)
if constexpr (ActuallySmooth) {
using std::sqrt;

return sqrt(x * x + eps2);
return sqrt(x * x + eps * eps) - eps;
}
else {
using std::abs;

(void)eps2;
(void)eps;

return abs(x);
}
}


template<bool ActuallySmooth, typename T, typename S>
constexpr T smooth_hypot(const T &x, const T &y, S eps2)
constexpr T smooth_hypot(const T &x, const T &y, S eps)
{
//
// Returns "sqrt(x^2 + y^2)", smoothed out near [0, 0]
Expand All @@ -408,13 +408,13 @@ constexpr T smooth_hypot(const T &x, const T &y, S eps2)
if constexpr (ActuallySmooth) {
using std::sqrt;

return sqrt(x * x + y * y + eps2);
return sqrt(x * x + y * y + eps * eps) - eps;
}
else {
//using std::hypot; -> not implemented in CppAD
using std::sqrt;

(void)eps2;
(void)eps;

//return hypot(x, y);
return sqrt(x * x + y * y);
Expand Down

0 comments on commit ab0ba7d

Please sign in to comment.