DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
basicMath.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: basicMath.hpp
5// Author: cdrisko
6// Date: 01/31/2020-15:42:41
7// Description: Common mathematical functions
8
9#ifndef DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_MATH_UTILS_BASICMATH_HPP
10#define DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_MATH_UTILS_BASICMATH_HPP
11
12#include <algorithm>
13#include <type_traits>
14#include <vector>
15
16namespace CppUtils::Math
17{
25 template<typename T, typename = std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
26 constexpr bool isEven(T value)
27 {
28 return !(value % 2);
29 }
30
38 template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
39 constexpr bool isOdd(T value)
40 {
41 return !isEven(value);
42 }
43
44
55 template<typename T>
56 constexpr bool withinRange(T value, T min, T max)
57 {
58 return value <= max && value >= min;
59 }
60
64 template<int Min, int Max, typename T>
65 constexpr bool withinRange(T value)
66 {
67 return withinRange(value, static_cast<T>(Min), static_cast<T>(Max));
68 }
69
70
82 template<typename T>
83 constexpr std::vector<T> linearlyInterpolate(const std::vector<T>& x, T y1, T y2)
84 {
85 std::vector<T> y(x.size());
86
87 T slope = (y2 - y1) / (x.back() - x.front());
88 T intercept = y1 - slope * x.front();
89
90 std::transform(x.begin(), x.end(), y.begin(), [=](auto val) { return slope * val + intercept; });
91 return y;
92 }
93} // namespace CppUtils::Math
94
95#endif
Definition backwardsDifferenceMethod.hpp:20
constexpr bool withinRange(T value, T min, T max)
Definition basicMath.hpp:56
constexpr std::vector< T > linearlyInterpolate(const std::vector< T > &x, T y1, T y2)
Definition basicMath.hpp:83
constexpr bool isEven(T value)
Definition basicMath.hpp:26
constexpr bool isOdd(T value)
Definition basicMath.hpp:39