ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Macros.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include "utils/Invariant.hpp"
12#include "utils/Math.hpp"
13#include <Eigen/Dense>
14
16#define ASSERT_APPROX(a, b) \
17 ASSERT_TRUE(a.isApprox(b, APPROX_THRESHOLD)) << "Expected " << std::endl \
18 << a << std::endl \
19 << " to be approximate to " << std::endl \
20 << b
21
23#define ASSERT_COLLINEAR(a, b) \
24 ASSERT_TRUE(ppforest2::math::collinear(a, b)) << "Expected columns of " << std::endl \
25 << a << std::endl \
26 << " to be collinear with its respective column of " << std::endl \
27 << b
28
30#define ASSERT_EQ_DATA(a, b) \
31 ASSERT_EQ(a.size(), b.size()); \
32 ASSERT_EQ(a.rows(), b.rows()); \
33 ASSERT_EQ(a.cols(), b.cols()); \
34 ASSERT_EQ(a, b);
35
36// Workaround for GCC miscompilation of long overloaded operator, chains
37// (see https://stackoverflow.com/questions/79872387).
38// Use these instead of Eigen's comma initializer (operator<<) for matrices and vectors.
39
40constexpr int rows(int n) {
41 return n;
42}
43
44#define VEC(T, ...) \
45 ([&]() -> Eigen::Matrix<T, Eigen::Dynamic, 1> { \
46 std::vector<T> _vals = {__VA_ARGS__}; \
47 return Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>>(_vals.data(), _vals.size()); \
48 })()
49
50#define MAT(T, ROWS, ...) \
51 ([&]() -> Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> { \
52 const int _rows = static_cast<int>(ROWS); \
53 std::vector<T> _vals = {__VA_ARGS__}; \
54 invariant(_rows > 0, "ROWS must be > 0"); \
55 invariant(_vals.size() % _rows == 0, "Element count not divisible by ROWS"); \
56 const int _cols = static_cast<int>(_vals.size()) / _rows; \
57 return Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>( \
58 _vals.data(), _rows, _cols); \
59 })()
constexpr int rows(int n)
Definition Macros.hpp:40