|
Core 1.0
|
Core class implementing some useful functions and constants. More...
#include <mathematics.h>
Static Public Member Functions | |
| static double | Clamp (const double &, const double &=0.0, const double &=1.0) |
| Array of binomial coefficients. | |
| static int | Clamp (int, int=0, int=255) |
| Clamp an integer value between two bounds. | |
| static constexpr double | Sqr (const double &) |
| Squares a double value. | |
| static constexpr double | Cube (const double &) |
| Cubes a double value. | |
| static double | Sqr4 (const double &) |
| Fourth power \(x^4\). | |
| static double | SymmetricSqr (const double &) |
| Symmetric of square function over unit interval. | |
| static double | Pow (const double &, const double &) |
| Power. | |
| static double | Abs (const double &) |
| Absolute value. | |
| static double | Sqrt32 (const double &) |
| Compute \(x^{3/2}\) as the square root of \(x^3\). | |
| static double | Sqrt4 (const double &) |
| Compute \(\sqrt[4]{x}\). | |
| static void | Powers (const double &, int, double *) |
| Compute the powers series of a real value \(1, x, \ldots, x^{n-1}\). | |
| static void | Powers (const double &, int, double *, double *) |
| Compute the powers series of a real value \(1, x, \ldots, x^{n-1}\) and \(1, 1-x, \ldots, (1-x)^{n-1}\). | |
| static constexpr double | Mod (const double &, const double &) |
| Modulus for reals with negative values handled properly. | |
| static double | Floor (const double &) |
| Floor function. | |
| static double | Fract (const double &) |
| Fractional part of a real. | |
| static double | FractFloor (const double &, double &) |
| Fractional part of a real. | |
| static double | Ceil (const double &) |
| Ceil function. | |
| static double | Min (const double &, const double &) |
| Minimum of two reals. | |
| static double | Max (const double &, const double &) |
| Maximum of two reals. | |
| static double | Min (const double &, const double &, const double &) |
| Minimum of three reals. | |
| static double | Max (const double &, const double &, const double &) |
| Maximum of three reals. | |
| static double | Min (const double &, const double &, const double &, const double &) |
| Minimum of four reals. | |
| static double | Max (const double &, const double &, const double &, const double &) |
| Maximum of four reals. | |
| static constexpr double | DegreeToRadian (const double &) |
| Convert degrees to randians. | |
| static constexpr double | RadianToDegree (const double &) |
| Convert radian to degrees. | |
| static constexpr double | Angle (int, int) |
| Compute \(2 k \pi / n\). | |
| static constexpr double | Angle (const double &) |
| Angle strictly in [0,2 π[. | |
| static double | AngleNorm (const double &, const double &) |
| Compute the (positive) angle distance between two angles. | |
| static double | AngleSigned (const double &, const double &) |
| Compute the signed angle norm between two angles. | |
| static double | AngleLerp (const double &, const double &, const double &) |
| Compute the interpolation between two angles, using the smallest angle rotation. | |
| static double | ArcTan (const double &, const double &) |
| Inline version of the atan2() function. | |
| static double | SinCard (double, bool=false) |
| Cardinal sine. | |
| static double | FoldAngle (const double &, bool=true) |
| Fold the argument angle to \([0,2\pi[ or [-\pi;\pi[\). | |
| static double | SinAtan (const double &) |
| Trigonometric simplification of sin(atan(x)). | |
| static double | CosAtan (const double &) |
| Trigonometric simplification of cos(atan(x)). | |
| static double | Step (double, double) |
| Step function. | |
| static double | Cycloidal (const double &) |
| Sine wave over unit interval. | |
| static double | Triangle (const double &) |
| Triangle wave over unit interval. | |
| static double | Lerp (const double &, const double &, const double &) |
| Linear interpolation. | |
| static double | Bilinear (const double &, const double &, const double &, const double &, const double &, const double &) |
| Bi-linear interpolation between four values. | |
| static double | Trilinear (const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &) |
| Trilinear interpolation between eight values. | |
| static double | Cubic (const double &, const double &, const double &, const double &, const double &) |
| Cubic Hermite interpolation. | |
| static double | CubicPoints (const double &, const double &, const double &, const double &, const double &) |
| Cubic point interpolation. | |
| static double | BiCubic (const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &, const double &=0.0, const double &=0.0, const double &=0.0, const double &=0.0) |
| Bi-cubic interpolation between four values, given partial derivatives. | |
| static double | BiCubic (const double &, const double &, const double &, const double &, const double &, const double &) |
| Bi-cubic interpolation between four values, partial derivatives are implicitly defined as null. | |
| static double | Sigmoid (const double &, const double &=1.0) |
| Sigmoid-like function. | |
| static double | SigmoidQuadric (const double &) |
| Compactly supported sigmoid-like function implemented using a \(C^1\) piecewise symmetric quadric. | |
| static double | Impulse (const double &, const double &=1.0) |
| Impulse function. | |
| static double | Warp (const double &) |
| Unit interval warping function. | |
| static double | Warp (const double &, const double &) |
| Unit interval gain function. | |
| static bool | InRange (const double &, const double &, const double &, const double &) |
| Check if a values lies within a prescribed range. | |
| static void | SetMinMax (const double &, double &, double &) |
| Update the minimum and maximum values given a double value. | |
| static int | Integer (const double &) |
| Compute the integer part of a real. | |
| static void | Sort (int &, int &) |
| Sort two integers. | |
| static void | Sort (double &, double &) |
| Sort two reals. | |
| static void | Sort (double &, double &, double &) |
| Sort three reals. | |
| static void | Sort (double &, double &, double &, double &) |
| Sort four reals, with a maximum of 5 comparisons. | |
| static void | Sort5 (double *) |
| static void | Swap (int &, int &) |
| Swap two integers. | |
| static void | Swap (double &, double &) |
| Swap two reals. | |
| static bool | IsNumber (double) |
| Check if a real number is not NaN. | |
| static bool | IsFinite (double) |
| Check if a real is not infinite. | |
| static int | IntegerSign (const double &, const double &=0.0) |
| Compute the integer sign of a real. | |
| static bool | SameSign (const double &, const double &) |
| Check if two reals have the same signs. | |
| static bool | SameSign (const double &, const double &, const double &) |
| Check if three reals have the same signs. | |
| static double | CopySign (const double &, const double &) |
| Composes a real with the magnitude of x and the sign of y. | |
| static double | Sign (const double &) |
| Returns +1 or -1 depending on the sign of argument. | |
| static long | Binomial (int, int) |
| Calculate the Binomial coefficent. | |
| static constexpr double | Unit (int, int) |
| Unit real value in [0,1] from two integers. | |
| static double | Gaussian (const double &, const double &) |
| Gaussian. | |
| static double | GaussianProbability (const double &, const double &) |
| Gaussian probability density function. | |
| static double | CompactGaussian (const double &, const double &) |
| Compact support Gaussian approximation. | |
| static double | Fract (const double &, double &) |
| Compute integer and fractionnal parts. | |
| static double | Fract (const double &, const double &, double &) |
| Compute integer and fractionnal parts. | |
| static double | Fract (const double &, const double &, const double &, double &) |
| Compute integer and fractionnal parts given an interval. | |
| static double | Geometric (double, int) |
| Compute the sum of the terms of a geometric series. | |
| static void | Swap (double *&, double *&) |
| Swap two reals. | |
| static void | Swap (int *&, int *&) |
| Swap two integers. | |
| static void | BernsteinSeries (const double &, int, double *) |
| Compute the Bernstein terms. | |
| static double | Bernstein (const double &, int, int) |
| Compute the i-th Bernstein term. | |
| static double | SmoothingCubic (double, double, double) |
| Generalized C2 polynomial smoothing function between two distances. | |
| static double | SmoothingQuadric (double, double, double) |
| Generalized C1 polynomial smoothing function between two distances. | |
| static double | FastAtan (double) |
| Fast second order rationnal approximation of arc-tangent. | |
Static Public Attributes | |
| static constexpr double | Pi = 3.14159265358979323846 |
| \(\pi\). | |
| static constexpr double | HalfPi = Math::Pi / 2.0 |
| \(2\pi\). | |
| static constexpr double | E = 2.7182818284590452354 |
| \(\pi/2\). | |
| static constexpr double | TwoPiOverThree = 2.0943951023931954923084 |
| \(e\). | |
| static constexpr double | Sqrt5 = 2.23606797749978969640917366873127623 |
| \(4/3 \pi\). | |
| static constexpr double | Sqrt3 = 1.73205080756887729352744634150587236 |
| Constant \(\sqrt{3}\). | |
| static constexpr double | Sqrt2 = 1.41421356237309504880168872420969807 |
| Constant \(\sqrt{2}\). | |
| static constexpr double | Sqrt12 = 1.0 / Math::Sqrt2 |
| Constant \(1/\sqrt{2}\). | |
| static constexpr double | Large = 1.0e20 |
| Constant \((\sqrt{5}+1)/2\). | |
| static constexpr double | Infinity = 1.7976931348623158e+308 |
| Large constant \(10^{20}\). | |
| static constexpr double | InfinityEpsilon = 2.2250738585072014e-308 |
| Smallest positive \(\epsilon\). | |
Core class implementing some useful functions and constants.
How can I use the constant Pi?
Simply use the static constant Math::Pi as follows:
Note that in this case, you could also have used:
How many min/max functions have been implemented?
Up to four arguments are supported; for a larger number of arguments, a specific routine operating on an array should be written.
Is there a function to compute the square of a real sumber?
Use the following:
For a Vector, use SquaredNorm(const Vector&);
Are there predefined square roots constants?
The sqrt function used not te be constexpr, so square roots of reals are not computed at compilation time. Some constants are provided, such as the following one:
What is the relative performance of mathematical functions such as square root or cosine?
The relative cost of commonly used mathematical functions is summarized in the following table.
| Function | Cost |
|---|---|
| + - * fabs | 1 |
| / sqrt modf | ~4.8 |
| / sin cos | ~8.9 |
| / exp log | ~10.5 |
Math provides some functions such as Math::Sqrt32 or Math::Sqrt4 to avoid computationally intensive standard functions like pow:
How are implemented the step and smooth-step functions that are often used in procedural modeling? Different smoothing kernels, such as Cubic::Smooth(), are implented in odd-degree polynomials Cubic, Quintic and Septic. The corresponding step functions, such as Cubic::SmoothStep(), are also implemented.
|
inlinestatic |
Absolute value.
| x | Real. |
|
inlinestaticconstexpr |
Angle strictly in [0,2 π[.
| a | Angle. |
|
inlinestaticconstexpr |
Compute \(2 k \pi / n\).
| k,n | Integers. |
|
inlinestatic |
Compute the interpolation between two angles, using the smallest angle rotation.
| a,b | Angles. |
| t | Interpolant. |
|
static |
Compute the (positive) angle distance between two angles.
Angles need not be in [0,2 π[.
| a,b | Angles. |
|
static |
Compute the signed angle norm between two angles.
| a,b | Angles. |
|
static |
Compute the i-th Bernstein term.
| u | Interpolant. |
| n | Degree. |
| i | Term. |
|
static |
Compute the Bernstein terms.
| u | Interpolant. |
| n | Degree. |
| b | Return array of terms. |
|
static |
Bi-cubic interpolation between four values, partial derivatives are implicitly defined as null.
This is an optimized implementation.
| u,v | Interpolation coefficients. |
| a00,a10,a11,a01 | Interpolated values. |
|
static |
Bi-cubic interpolation between four values, given partial derivatives.
The values are given in trigonometric order.
| u,v | Interpolation coefficients. |
| a00,a10,a11,a01 | Interpolated values. |
| u00,u10,u11,u01,v00,v10,v11,v01 | Partial derivatives with respect to u and v. |
| x00,x10,x11,x01 | Cross derivatives. |
|
inlinestatic |
Bi-linear interpolation between four values.
The values are given in trigonometric order.
| a00,a10,a11,a01 | Interpolated values. |
| u,v | Interpolation coefficients. |
|
static |
Calculate the Binomial coefficent.
| n,r | Binomial coefficient parameters. |
|
inlinestatic |
|
inlinestatic |
Array of binomial coefficients.
Clamp a double value between two bounds.
| x | Input value. |
| a,b | Lower and upper bounds. |
|
inlinestatic |
Clamp an integer value between two bounds.
| x | Input value. |
| a,b | Lower and upper bounds. |
|
inlinestatic |
Compact support Gaussian approximation.
The compact support has a radius r = 3 sigma.
| x | Real. |
| s | Sigma. |
|
inlinestatic |
Composes a real with the magnitude of x and the sign of y.
| x | Real. |
| y | Real whose sign will be copied. |
|
static |
Trigonometric simplification of cos(atan(x)).
| x | Real value. |
|
inlinestaticconstexpr |
Cubes a double value.
| x | Real value. |
|
static |
Cubic Hermite interpolation.
| u | Interpolation coefficient. |
| a,b | Values for t=0 and t=1. |
| ta,tb | Derivatives for t=0 and t=1. |
|
static |
Cubic point interpolation.
| u | Interpolation coefficient. |
| a,b,c,d | Values for t=-1, t=0, t=1 and t=2. The derivatives at t=0 and t=1 will be derived from those |
|
static |
Sine wave over unit interval.
| x | Real value. |
|
inlinestaticconstexpr |
Convert degrees to randians.
| a | Angle in degrees. |
|
static |
Fast second order rationnal approximation of arc-tangent.
| x | Real. |
|
inlinestatic |
Floor function.
While it is easier to use the C++ function, this has been implemented to be consistent with Fract().
| x | Real. |
|
static |
Fold the argument angle to \([0,2\pi[ or [-\pi;\pi[\).
| a | Angle. |
| s | Strategy, map to \([0,2\pi[\) if true, \([-\pi;\pi[\) otherwise. |
|
inlinestatic |
|
inlinestatic |
Compute integer and fractionnal parts given an interval.
| x | Real. |
| a,b | Interval. |
| i | Returned integer part. |
|
inlinestatic |
Compute integer and fractionnal parts.
| x | Real. |
| a | Modulus. |
| i | Returned integer part. |
|
inlinestatic |
Compute integer and fractionnal parts.
| x | Real. |
| i | Returned integer part. |
|
inlinestatic |
Fractional part of a real.
Implemented as:
| x | Real. |
| y | Return floor. |
|
inlinestatic |
Gaussian.
| x | Real. |
| s | Sigma. |
|
inlinestatic |
Gaussian probability density function.
| x | Real. |
| s | Sigma. |
|
static |
Compute the sum of the terms of a geometric series.
Compute \(1+x+x^2+\ldots+x^{n-1} = \frac{1-x^n}{1-x}\). If \(x=1.0\), return \(n\).
|
inlinestatic |
Impulse function.
An impulse function that doesn't use exponentials, k controls the falloff of the function.
Derivative is 2*sqrt(k)*(1-k*x*x)/(k*x*x+1), maximum is reached x = sqrt(1/k), value at maximum is 1.
| k | Control parameter. |
| x | Real. |
|
inlinestatic |
Check if a values lies within a prescribed range.
| x | Value. |
| a,b | Interval. |
| epsilon | Epsilon tolerance. |
|
inlinestatic |
Compute the integer part of a real.
This function handles negative values differently by subtracting 1 from the result.
| x | Real. |
|
inlinestatic |
Compute the integer sign of a real.
| x | Real. |
| t | Threshold. |
|
static |
Check if a real is not infinite.
| x | Real value. |
|
static |
Check if a real number is not NaN.
The code is simply:
This looks like it should always be true, but it's false if x is a NaN.
| x | Real value. |
|
inlinestatic |
|
inlinestatic |
Maximum of two reals.
| a,b | Real values. |
|
inlinestatic |
Maximum of three reals.
| a,b,c | Real values. |
|
inlinestatic |
Maximum of four reals.
| a,b,c,d | Real values. |
|
inlinestatic |
Minimum of two reals.
| a,b | Real values. |
|
inlinestatic |
Minimum of three reals.
| a,b,c | Real values. |
|
inlinestatic |
Minimum of four reals.
| a,b,c,d | Real values. |
|
inlinestaticconstexpr |
Modulus for reals with negative values handled properly.
| x | Real. |
| a | Modulo. |
|
inlinestatic |
Power.
Returns base x raised to the power exponent e if x>0, and -x raised to the power exponent e otherwise.
| x | Base. |
| ee | Exponent. |
|
inlinestatic |
Compute the powers series of a real value \(1, x, \ldots, x^{n-1}\).
| x | Real. |
| n | Maximum power. |
| a | Array of real, should be at least of size n. |
|
inlinestatic |
Compute the powers series of a real value \(1, x, \ldots, x^{n-1}\) and \(1, 1-x, \ldots, (1-x)^{n-1}\).
| x | Real. |
| n | Maximum power. |
| a,b | Array of real, should be at least of size n. |
|
inlinestaticconstexpr |
Convert radian to degrees.
| a | Angle in radian. |
|
inlinestatic |
Check if two reals have the same signs.
| a,b | Reals. |
|
inlinestatic |
Check if three reals have the same signs.
| a,b,c | Reals. |
|
inlinestatic |
Update the minimum and maximum values given a double value.
| x | Input value. |
| a,b | Lower and upper bounds that will be updated according to x. |
|
inlinestatic |
Sigmoid-like function.
This function is more efficient than the real sigmoid function which requires the computation of the exponential.
| s | Sigma. |
| x | Real. |
|
inlinestatic |
Compactly supported sigmoid-like function implemented using a \(C^1\) piecewise symmetric quadric.
The compact support is [-2,2] and the range [-1,1].
The quadric was obtaied by solving the Hermite Cubic contraints:
| x | Real. |
|
inlinestatic |
Returns +1 or -1 depending on the sign of argument.
| x | Real. |
|
static |
Trigonometric simplification of sin(atan(x)).
| x | Real value. |
|
inlinestatic |
Cardinal sine.
| x | Real. |
| normalized | If true, then the function is normalized to the interval \([-1,1]\) instead of \([-\pi,\pi]\). |
Use the first order Taylor expansion \(sin(x)/x \approx 1 - x^2/6 + x^4/120 + o(x^4)\) for small values to avoid numerical issues.
|
inlinestatic |
Generalized C2 polynomial smoothing function between two distances.
The function is not associative.
| a,b | Distances. |
| sr | Smoothing radius. |
|
inlinestatic |
Generalized C1 polynomial smoothing function between two distances.
This is a general version of the union, intersection and difference function of Media Molecule using in the Dreams video game.
The function is not associative.
| a,b | Distances. |
| sr | Smoothing radius. |
|
inlinestatic |
Sort two reals.
| a,b | Real arguments, which will be swapped so that a<=b. |
|
inlinestatic |
Sort three reals.
| a,b,c | Real arguments, which will be swapped so that a<=b<=c. |
|
inlinestatic |
Sort four reals, with a maximum of 5 comparisons.
| a,b,c,d | Real arguments, which will be swapped into ascending order. |
|
inlinestatic |
Sort two integers.
| a,b | Real arguments, which will be swapped so that a<=b. |
|
static |
Sort five reals with only seven comparisons.
| A | Array of five reals. |
|
inlinestaticconstexpr |
|
inlinestatic |
Fourth power \(x^4\).
| x | Real value. |
|
inlinestatic |
Compute \(x^{3/2}\) as the square root of \(x^3\).
This function is more efficient than pow() which is extremely slow.
| x | Real value. |
|
inlinestatic |
Compute \(\sqrt[4]{x}\).
This function is more efficient than pow() which is extremely slow.
| x | Real value. |
|
inlinestatic |
Step function.
| e | Step position. |
| x | Variable. |
|
inlinestatic |
|
inlinestatic |
Swap two reals.
| a,b | Arguments. |
|
inlinestatic |
|
inlinestatic |
Swap two integers.
| a,b | Arguments. |
|
inlinestatic |
Symmetric of square function over unit interval.
Simply compute \(1-(1-x)^2\).
| x | Real value. |
|
static |
Triangle wave over unit interval.
| x | Real value. |
|
inlinestatic |
Trilinear interpolation between eight values.
| a,b,c,d,e,f,g,h | Interpolated values. |
| u,v,w | Interpolation coefficients. |
|
inlinestaticconstexpr |
Unit real value in [0,1] from two integers.
Usefull for loops, same as:
| i,n | Integers. |
|
inlinestatic |
Unit interval warping function.
Remaps the unit interval into the unit interval by expanding the sides and compressing the center, keeping 1/2 mapped to 1/2.
| x | Real in [0,1]. |
|
inlinestatic |
Unit interval gain function.
Remaps the unit interval into the unit interval by expanding the sides and compressing the center, keeping 1/2 mapped to 1/2.
This function uses calls to pow and is computationally intensive.
| x | Real in [0,1]. |
| k | Exponent. |
|
staticconstexpr |
Large constant \(10^{20}\).
\(\infty\).
|
staticconstexpr |
\(4/3 \pi\).
Constant \(\sqrt{5}\).
|
staticconstexpr |
\(e\).
\(2/3 \pi\).