CiCueTea v0.0.1
Loading...
Searching...
No Matches
MathUtils.h
Go to the documentation of this file.
1//
2// MathUtils.h
3// CQTDSP
4//
5// Created by Juan Sierra on 5/1/25.
6//
7
15
16#pragma once
17
18#include <cmath>
19
20#include <Eigen/Core>
21
22namespace jsa::cicuetea {
23
38inline constexpr size_t nextPow2(size_t x)
39{
40 if (x == 0) return 1;
41 x--;
42 x |= x >> 1;
43 x |= x >> 2;
44 x |= x >> 4;
45 x |= x >> 8;
46 x |= x >> 16;
47#if SIZE_MAX > 0xFFFFFFFFu
48 x |= x >> 32;
49#endif
50 return x + 1;
51}
52
66inline size_t constrain(size_t idx, size_t size) { return idx % size; }
67
77inline double square(double x) { return x * x; }
78
86inline Eigen::ArrayXd regspace(Eigen::Index num)
87{
88 return Eigen::ArrayXd::LinSpaced(num, 0, num - 1);
89}
90
104inline Eigen::ArrayXd regspace(Eigen::Index low, Eigen::Index high)
105{
106 return Eigen::ArrayXd::LinSpaced(high - low + 1, low, high);
107}
108
124inline Eigen::ArrayXd logspace(double start, double end, Eigen::Index num)
125{
126 return Eigen::ArrayXd::LinSpaced(num, std::log(start), std::log(end)).exp();
127}
128
129} // namespace jsa::cicuetea
double square(double x)
Computes the square of a given number.
Definition MathUtils.h:77
Eigen::ArrayXd regspace(Eigen::Index num)
Generates a linearly spaced array of values from 0 to num-1.
Definition MathUtils.h:86
size_t constrain(size_t idx, size_t size)
Constrains an index to fit within the bounds of a given size by wrapping it around.
Definition MathUtils.h:66
Eigen::ArrayXd logspace(double start, double end, Eigen::Index num)
Generates a logarithmically spaced array.
Definition MathUtils.h:124
constexpr size_t nextPow2(size_t x)
Computes the next power of 2 greater than or equal to the given number.
Definition MathUtils.h:38