aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/math/utility.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/math/utility.h')
-rw-r--r--meowpp/math/utility.h52
1 files changed, 30 insertions, 22 deletions
diff --git a/meowpp/math/utility.h b/meowpp/math/utility.h
index 43b4785..d153b68 100644
--- a/meowpp/math/utility.h
+++ b/meowpp/math/utility.h
@@ -6,7 +6,7 @@
#include <algorithm>
#include <cmath>
-namespace meow{
+namespace meow {
//! 圓周率...
static const double PI = 3.14159265358979323846264338327950288;
@@ -15,7 +15,7 @@ static const double PI = 3.14159265358979323846264338327950288;
* @brief 如果abs(輸入的數值) < eps, 則回傳0, 否則回傳輸入的數值
*/
template<class T>
-inline T noEPS(T value, T eps = 1e-9){
+inline T noEPS(T value, T eps = 1e-9) {
T epsp((eps < T(0)) ? -eps : eps);
return ((value < -epsp || value > epsp) ? value : T(0));
}
@@ -24,7 +24,7 @@ inline T noEPS(T value, T eps = 1e-9){
* @brief \c (value-lower)/(upper-lower)
*/
template<class T>
-inline T normalize(T lower, T upper, T value){
+inline T normalize(T lower, T upper, T value) {
return (value - lower) / (upper - lower);
}
@@ -32,7 +32,7 @@ inline T normalize(T lower, T upper, T value){
* @brief \c (lower+_ratio*(upper-lower))
*/
template<class T>
-inline T denormalize(T lower, T upper, T _ratio){
+inline T denormalize(T lower, T upper, T _ratio) {
return lower + _ratio * (upper - lower);
}
@@ -40,7 +40,7 @@ inline T denormalize(T lower, T upper, T _ratio){
* @brief \c denormalize(l2,u2,normalize(l1,u1,m1))
*/
template<class T>
-inline T ratioMapping(T l1, T u1, T m1, T l2, T u2){
+inline T ratioMapping(T l1, T u1, T m1, T l2, T u2) {
return denormalize(l2, u2, normalize(l1, u1, m1));
}
@@ -48,15 +48,23 @@ inline T ratioMapping(T l1, T u1, T m1, T l2, T u2){
* @brief \c std::min(mx,std::max(mn,v))
*/
template<class T>
-inline T inRange(T const& mn, T const& mx, T const& v){
+inline T inRange(T const& mn, T const& mx, T const& v) {
return std::min(mx, std::max(mn, v));
}
/*!
+ * @brief (mn <= x && x <= mx)
+ */
+template<class T>
+inline T isInRange(T const& mn, T const& mx, T const& x) {
+ return (mn <= x && x <= mx);
+}
+
+/*!
* @brief \c x*x
*/
template<class T>
-inline T squ(T const& x){
+inline T squ(T const& x) {
return x * x;
}
@@ -64,7 +72,7 @@ inline T squ(T const& x){
* @brief \c x*x*x
*/
template<class T>
-inline T cub(T const& x){
+inline T cub(T const& x) {
return x * x * x;
}
@@ -72,22 +80,22 @@ inline T cub(T const& x){
* @brief 只將 \c sigs 個標準差以內的數據拿來取平均
*/
template<class T>
-inline double average(T const& beg, T const& end, double sigs){
+inline double average(T const& beg, T const& end, double sigs) {
int N = 0;
double av = 0;
- for(T it = beg; it != end; it++, N++){
+ for (T it = beg; it != end; ++it, ++N) {
av += *it;
}
av /= N;
double sig = 0;
- for(T it = beg; it != end; it++){
+ for (T it = beg; it != end; ++it) {
sig += (*it - av) * (*it - av);
}
sig = sqrt(sig / N);
double lower = av - sig * sigs, upper = av + sig * sigs;
double ret = 0, retn = 0;
- for(T it = beg; it != end; it++){
- if(lower <= *it && *it <= upper){
+ for (T it = beg; it != end; ++it) {
+ if (lower <= *it && *it <= upper) {
ret += *it;
retn++;
}
@@ -99,30 +107,30 @@ inline double average(T const& beg, T const& end, double sigs){
* @brief 只將 \c sigs 個標準差以內的數據拿來取平均, 不過這次用 \c p 來加權平均
*/
template<class T>
-inline double average(T const& beg, T const& end, T const& p, double sigs){
+inline double average(T const& beg, T const& end, T const& p, double sigs) {
int N = 0;
double ps = 0;
- for(T it = beg, ip = p; it != end; it++, N++, ip++){
+ for (T it = beg, ip = p; it != end; ++it, ++N, ++ip) {
ps += *ip;
}
double av = 0;
- for(T it = beg, ip = p; it != end; it++, ip++){
+ for (T it = beg, ip = p; it != end; ++it, ++ip) {
av += *it * *ip / ps;
}
double sig = 0;
- for(T it = beg, ip = p; it != end; it++, ip++){
+ for (T it = beg, ip = p; it != end; ++it, ++ip) {
sig += *ip / ps * (*it - av) * (*it - av);
}
sig = sqrt(sig);
double lower = av - sig * sigs, upper = av + sig * sigs;
double ret = 0, retn = 0;
- for(T it = beg, ip = p; it != end; it++, ip++){
- if(lower <= *it && *it <= upper){
+ for (T it = beg, ip = p; it != end; ++it, ++ip) {
+ if (lower <= *it && *it <= upper) {
ret += *it * *ip;
retn += *ip;
}
}
- if(retn <= 1e-10) return av;
+ if (retn <= 1e-10) return av;
return ret / retn;
}
@@ -130,10 +138,10 @@ inline double average(T const& beg, T const& end, T const& p, double sigs){
* @brief 就只是個取絕對值
*/
template<class T>
-inline T tAbs(T const& t){
+inline T tAbs(T const& t) {
return (t < 0 ? -t : t);
}
-}
+} // meow
#endif // math_utility_H__