clamp.hpp
Go to the documentation of this file.
1 
11 #ifndef MLPACK_CORE_MATH_CLAMP_HPP
12 #define MLPACK_CORE_MATH_CLAMP_HPP
13 
14 #include <stdlib.h>
15 #include <math.h>
16 #include <float.h>
17 
18 namespace mlpack {
19 namespace math {
20 
28 inline double ClampNonNegative(const double d)
29 {
30  return (d + fabs(d)) / 2;
31 }
32 
40 inline double ClampNonPositive(const double d)
41 {
42  return (d - fabs(d)) / 2;
43 }
44 
53 inline double ClampRange(double value,
54  const double rangeMin,
55  const double rangeMax)
56 {
57  value -= rangeMax;
58  value = ClampNonPositive(value) + rangeMax;
59  value -= rangeMin;
60  value = ClampNonNegative(value) + rangeMin;
61  return value;
62 }
63 
64 } // namespace math
65 } // namespace mlpack
66 
67 #endif // MLPACK_CORE_MATH_CLAMP_HPP
double ClampNonNegative(const double d)
Forces a number to be non-negative, turning negative numbers into zero.
Definition: clamp.hpp:28
Linear algebra utility functions, generally performed on matrices or vectors.
double ClampNonPositive(const double d)
Forces a number to be non-positive, turning positive numbers into zero.
Definition: clamp.hpp:40
double ClampRange(double value, const double rangeMin, const double rangeMax)
Clamp a number between a particular range.
Definition: clamp.hpp:53