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: crdrisko
6// Date: 08/25/2020-20:21:04
7// Description: Function overloads for common mathematical operations in the cmath standard library header
8
9#ifndef DRYCHEM_CPP_UNITS_INCLUDE_CPP_UNITS_MATH_BASICMATH_HPP
10#define DRYCHEM_CPP_UNITS_INCLUDE_CPP_UNITS_MATH_BASICMATH_HPP
11
12#include <cmath>
13
15
17{
18#define DECLARE_CMATH_FUNCTION(NAME) \
19 template<int L, int M, int T, int I, int Th, int N, int J> \
20 constexpr auto NAME(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>& physicalQuantity) noexcept \
21 { \
22 return PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>(std::NAME(physicalQuantity.getMagnitude())); \
23 }
24
25#define DECLARE_DIMENSIONLESS_CMATH_FUNCTION(NAME) \
26 template<int L, int M, int T, int I, int Th, int N, int J> \
27 constexpr auto NAME(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>& physicalQuantity) noexcept \
28 { \
29 return PhysicalQuantity<Dimensionality<>>(std::NAME(physicalQuantity.getMagnitude())); \
30 }
31
34
35
41
42
43 namespace details
44 {
45 template<unsigned int Power, int L, int M, int T, int I, int Th, int N, int J>
46 struct PowImpl
47 {
48 static constexpr auto result(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>& physicalQuantity) noexcept
49 {
50 return physicalQuantity * PowImpl<Power - 1, L, M, T, I, Th, N, J>::result(physicalQuantity);
51 }
52 };
53
54 template<int L, int M, int T, int I, int Th, int N, int J>
55 struct PowImpl<0, L, M, T, I, Th, N, J>
56 {
57 static constexpr auto result(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>&) noexcept
58 {
60 }
61 };
62
63 template<int Power, int L, int M, int T, int I, int Th, int N, int J>
65 {
66 static constexpr auto result(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>& physicalQuantity) noexcept
67 {
68 return 1.0 / (PowImpl<-Power, L, M, T, I, Th, N, J>::result(physicalQuantity));
69 }
70 };
71 } // namespace details
72
73 template<int Power, int L, int M, int T, int I, int Th, int N, int J>
74 constexpr auto pow(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>& physicalQuantity) noexcept
75 {
76 if constexpr (Power >= 0)
78 else
80 }
81
82 template<int L, int M, int T, int I, int Th, int N, int J,
83 typename = std::enable_if_t<(L % 2 == 0) && (M % 2 == 0) && (T % 2 == 0) && (I % 2 == 0) && (Th % 2 == 0)
84 && (N % 2 == 0) && (J % 2 == 0)>>
85 constexpr auto sqrt(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>& physicalQuantity) noexcept
86 {
87 using TReturn = PhysicalQuantity<Dimensionality<L / 2, M / 2, T / 2, I / 2, Th / 2, N / 2, J / 2>>;
88 return TReturn {std::sqrt(physicalQuantity.getMagnitude())};
89 }
90
91 template<int L, int M, int T, int I, int Th, int N, int J,
92 typename = std::enable_if_t<(L % 3 == 0) && (M % 3 == 0) && (T % 3 == 0) && (I % 3 == 0) && (Th % 3 == 0)
93 && (N % 3 == 0) && (J % 3 == 0)>>
94 constexpr auto cbrt(const PhysicalQuantity<Dimensionality<L, M, T, I, Th, N, J>>& physicalQuantity) noexcept
95 {
96 using TReturn = PhysicalQuantity<Dimensionality<L / 3, M / 3, T / 3, I / 3, Th / 3, N / 3, J / 3>>;
97 return TReturn {std::cbrt(physicalQuantity.getMagnitude())};
98 }
99
107
108
115
116#undef DECLARE_CMATH_FUNCTION
117#undef DECLARE_DIMENSIONLESS_CMATH_FUNCTION
118} // namespace CppUnits::Math
119
120#endif
Definition physicalQuantity.hpp:33
#define DECLARE_DIMENSIONLESS_CMATH_FUNCTION(NAME)
Definition basicMath.hpp:25
#define DECLARE_CMATH_FUNCTION(NAME)
Definition basicMath.hpp:18
Basic operations.
Definition basicMath.hpp:44
Definition basicMath.hpp:17
constexpr auto sqrt(const PhysicalQuantity< Dimensionality< L, M, T, I, Th, N, J > > &physicalQuantity) noexcept
Definition basicMath.hpp:85
constexpr auto pow(const PhysicalQuantity< Dimensionality< L, M, T, I, Th, N, J > > &physicalQuantity) noexcept
Definition basicMath.hpp:74
constexpr auto cbrt(const PhysicalQuantity< Dimensionality< L, M, T, I, Th, N, J > > &physicalQuantity) noexcept
Definition basicMath.hpp:94
PhysicalQuantity< Dimensionality< 2, 1, -3 > > Power
Definition physicalQuantities.hpp:43
Definition dimensionality.hpp:27
Definition basicMath.hpp:65
static constexpr auto result(const PhysicalQuantity< Dimensionality< L, M, T, I, Th, N, J > > &physicalQuantity) noexcept
Definition basicMath.hpp:66
static constexpr auto result(const PhysicalQuantity< Dimensionality< L, M, T, I, Th, N, J > > &) noexcept
Definition basicMath.hpp:57
Definition basicMath.hpp:47
static constexpr auto result(const PhysicalQuantity< Dimensionality< L, M, T, I, Th, N, J > > &physicalQuantity) noexcept
Definition basicMath.hpp:48