Skip to content

Commit

Permalink
add abs function
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkichler committed Jul 8, 2024
1 parent 95d19f8 commit 2bbc6e8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/cutangent/arithmetic/basic.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ fn tangent<T> min(tangent<T> a, tangent<T> b)
.d = a.v <= b.v ? a.d : b.d }; // '<=' instead of '<' due to subgradient
}

template<typename T>
fn tangent<T> abs(tangent<T> x)
{
using std::abs;
using std::copysign;
// NOTE: not differentiable at x = 0.
return { .v = abs(x.v), .d = copysign(1.0, x.v) * x.d };
}

template<typename T>
fn tangent<T> clamp(tangent<T> v, tangent<T> lb, tangent<T> ub)
{
Expand Down

0 comments on commit 2bbc6e8

Please sign in to comment.