ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Math.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5
6#include "utils/Types.hpp"
7
8#define APPROX_THRESHOLD 0.01
9
11namespace ppforest2::math {
20 template<typename A, typename B, typename T> inline bool is_approx(A a, B b, T threshold) {
21 return fabs(a - b) < threshold;
22 }
23
25 template<typename A, typename B> inline bool is_approx(A a, B b) {
26 return is_approx(a, b, APPROX_THRESHOLD);
27 }
28
32 template<typename A, typename B> inline bool is_module_approx(A a, B b) {
33 return is_approx(fabs(a), fabs(b));
34 }
35
43 template<typename T> bool collinear(types::Vector<T> const& a, types::Vector<T> const& b) {
44 return is_module_approx(a.dot(b) / (a.norm() * b.norm()), 1.0);
45 }
46
66 inline int proportion_to_count(float p, unsigned int total) {
67 float const value = static_cast<float>(total) * p;
68 float const lower = std::floor(value);
69 float const fract = value - lower;
70
71 int count = static_cast<int>(lower);
72 // Round up past the halfway point, or exactly at it when the floor is odd
73 // (ties to even). Mirrors std::nearbyint's default-mode result without
74 // reading the floating-point environment.
75 if (fract > 0.5F || (fract == 0.5F && count % 2 != 0)) {
76 count += 1;
77 }
78 return std::max(1, count);
79 }
80}
#define APPROX_THRESHOLD
Definition Math.hpp:8
Numeric comparison utilities.
Definition Math.hpp:11
bool collinear(types::Vector< T > const &a, types::Vector< T > const &b)
Check whether two vectors are collinear (parallel or anti-parallel).
Definition Math.hpp:43
bool is_module_approx(A a, B b)
Check whether the absolute values of two scalars are approximately equal.
Definition Math.hpp:32
int proportion_to_count(float p, unsigned int total)
Convert a proportion of a total into a count.
Definition Math.hpp:66
bool is_approx(A a, B b, T threshold)
Check whether two scalars are approximately equal.
Definition Math.hpp:20
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
Generic dynamic-size column vector.
Definition Types.hpp:55