mlpack  2.0.1
clamp.hpp
Go to the documentation of this file.
1 
13 #ifndef __MLPACK_CORE_MATH_CLAMP_HPP
14 #define __MLPACK_CORE_MATH_CLAMP_HPP
15 
16 #include <stdlib.h>
17 #include <math.h>
18 #include <float.h>
19 
20 namespace mlpack {
21 namespace math {
22 
30 inline double ClampNonNegative(const double d)
31 {
32  return (d + fabs(d)) / 2;
33 }
34 
42 inline double ClampNonPositive(const double d)
43 {
44  return (d - fabs(d)) / 2;
45 }
46 
55 inline double ClampRange(double value,
56  const double rangeMin,
57  const double rangeMax)
58 {
59  value -= rangeMax;
60  value = ClampNonPositive(value) + rangeMax;
61  value -= rangeMin;
62  value = ClampNonNegative(value) + rangeMin;
63  return value;
64 }
65 
66 } // namespace math
67 } // namespace mlpack
68 
69 #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:30
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:42
double ClampRange(double value, const double rangeMin, const double rangeMax)
Clamp a number between a particular range.
Definition: clamp.hpp:55