Visual Computing Library  devel
Loading...
Searching...
No Matches
math.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BASE_MATH_H
24#define VCL_BASE_MATH_H
25
26#include <cassert>
27#include <cmath>
28#include <limits>
29
30namespace vcl {
31
42template<typename Scalar>
43bool isDegenerate(Scalar number)
44{
45 return std::isinf(number) || std::isnan(number);
46}
47
63template<typename Scalar>
65 Scalar n1,
66 Scalar n2,
67 Scalar epsilon = std::numeric_limits<Scalar>::epsilon())
68{
69 return (std::abs(n1 - n2) <= epsilon);
70}
71
80template<typename Scalar>
81Scalar toRad(const Scalar& deg)
82{
83 return M_PI * deg / 180.0;
84}
85
94template<typename Scalar>
95Scalar toDeg(const Scalar& rad)
96{
97 return rad * 180.0 / M_PI;
98}
99
112inline double lnOfFactorial(int n)
113{
114 static const int FAK_LEN = 1024;
115
116 // define constants
117 static const double // coefficients in Stirling approximation
118 C0 = 0.918938533204672722, // ln(sqrt(2*pi))
119 C1 = 1. / 12., C3 = -1. / 360.;
120 // C5 = 1./1260., // use r^5 term if FAK_LEN < 50
121 // C7 = -1./1680.; // use r^7 term if FAK_LEN < 20
122 // static variables
123 static double fac_table[FAK_LEN]; // table of ln(n!):
124 // remember if fac_table has been initialized
125 static bool initialized = false;
126
127 if (n < FAK_LEN) {
128 if (n <= 1) {
129 if (n < 0)
130 assert(0); //("Parameter negative in LnFac function");
131 return 0;
132 }
133 if (!initialized) { // first time. Must initialize table
134 // make table of ln(n!)
135 double sum = fac_table[0] = 0.;
136 for (uint i = 1; i < FAK_LEN; i++) {
137 sum += std::log(double(i));
138 fac_table[i] = sum;
139 }
140 initialized = true;
141 }
142 return fac_table[n];
143 }
144 else {
145 // not found in table. use Stirling approximation
146 double n1, r;
147 n1 = n;
148 r = 1. / n1;
149 return (n1 + 0.5) * std::log(n1) - n1 + C0 + r * (C1 + r * r * C3);
150 }
151}
152
153} // namespace vcl
154
155#endif // VCL_BASE_MATH_H
A class representing a box in N-dimensional space.
Definition box.h:46
bool epsilonEquals(Scalar n1, Scalar n2, Scalar epsilon=std::numeric_limits< Scalar >::epsilon())
Checks if two floating point numbers are equal within an epsilon value.
Definition math.h:64
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition math.h:81
bool isDegenerate(Scalar number)
Checks if a floating point number is degenerate.
Definition math.h:43
double lnOfFactorial(int n)
Computes and caches the result of the natural logarithm of n!
Definition math.h:112
Scalar toDeg(const Scalar &rad)
Converts an angle in radians to degrees.
Definition math.h:95