Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
QuantityFuncs.hpp
Go to the documentation of this file.
1#ifndef PHYTH_QUANTITY_FUNCS_HPP
2#define PHYTH_QUANTITY_FUNCS_HPP
3
4#include <algorithm>
5#include <cmath>
6
9
10namespace Phyth::Utils {
17 inline Scalar sin(const Scalar q) {
18 return {std::sin(q.value)};
19 }
20
27 inline Scalar cos(const Scalar q) {
28 return {std::cos(q.value)};
29 }
30
37 inline Scalar tan(const Scalar q) {
38 return {std::tan(q.value)};
39 }
40
47 inline Scalar asin(const Scalar q) {
48 return {std::asin(q.value)};
49 }
50
57 inline Scalar acos(const Scalar q) {
58 return {std::acos(q.value)};
59 }
60
67 inline Scalar atan(const Scalar q) {
68 return {std::atan(q.value)};
69 }
70
78 inline Scalar atan2(const Scalar y, const Scalar x) {
79 return {std::atan2(y.value, x.value)};
80 }
81
88 inline Scalar sinh(const Scalar q) {
89 return {std::sinh(q.value)};
90 }
91
98 inline Scalar cosh(const Scalar q) {
99 return {std::cosh(q.value)};
100 }
101
108 inline Scalar tanh(const Scalar q) {
109 return {std::tanh(q.value)};
110 }
111
118 inline Scalar asinh(const Scalar q) {
119 return {std::asinh(q.value)};
120 }
121
128 inline Scalar acosh(const Scalar q) {
129 return {std::acosh(q.value)};
130 }
131
138 inline Scalar atanh(const Scalar q) {
139 return {std::atanh(q.value)};
140 }
141
148 inline Scalar log(const Scalar q) {
149 return {std::log(q.value)};
150 }
151
158 inline Scalar log10(const Scalar q) {
159 return {std::log10(q.value)};
160 }
161
168 inline Scalar log2(const Scalar q) {
169 return {std::log2(q.value)};
170 }
171
178 inline Scalar log1p(const Scalar q) {
179 return {std::log1p(q.value)};
180 }
181
188 inline Scalar exp(const Scalar q) {
189 return {std::exp(q.value)};
190 }
191
198 inline Scalar expm1(const Scalar q) {
199 return {std::expm1(q.value)};
200 }
201
208 inline Scalar exp2(const Scalar q) {
209 return {std::exp2(q.value)};
210 }
211
227 template<int Power, typename UnitT>
229 if (Power == 0) {
230 return Scalar(1.0);
231 }
232 if (Power > 0) {
233 using DimT = typename UnitT::DimensionT;
236 return Quantity<ResultUnit>(std::pow(q.value, Power));
237 }
238 using DimT = typename UnitT::DimensionT;
241 return Quantity<ResultUnit>(std::pow(q.value, Power));
242 }
243
255 template<typename UnitT>
257 using DimT = typename UnitT::DimensionT;
260 return Quantity<ResultUnit>(q.value * q.value);
261 }
262
270 template<typename UnitT>
272 using DimT = typename UnitT::DimensionT;
275 return Quantity<ResultUnit>(q.value * q.value * q.value);
276 }
277
290 template<int N, typename UnitT>
292 static_assert(N > 0, "Root index must be positive");
293 using DimT = typename UnitT::DimensionT;
296 return Quantity<ResultUnit>(std::pow(q.value, 1.0 / N));
297 }
298
309 template<typename UnitT>
311 return root<2>(q);
312 }
313
320 template<typename UnitT>
322 return root<3>(q);
323 }
324
331 template<typename UnitT>
333 return Quantity<UnitT>(std::abs(q.value));
334 }
335
343 template<typename UnitT>
345 static_assert(is_unit_v<UnitT>, "Unit type cannot be a non-unit");
346 return Quantity<UnitT>(std::fmod(x.value, y.value));
347 }
348
360 template<typename UnitT>
362 return Quantity<UnitT>(std::modf(x.value, &y.value));
363 }
364
372 template<typename UnitT>
374 static_assert(is_unit_v<UnitT>, "Unit type cannot be a non-unit");
375 return Quantity<UnitT>(std::max(x.value, y.value));
376 }
377
385 template<typename UnitT>
387 static_assert(is_unit_v<UnitT>, "Unit type cannot be a non-unit");
388 return Quantity<UnitT>(std::min(x.value, y.value));
389 }
390
399 template<typename UnitT>
401 static_assert(is_unit_v<UnitT>, "Unit type cannot be a non-unit");
402 return Quantity<UnitT>(std::clamp(x.value, min.value, max.value));
403 }
404
411 template<typename UnitT>
413 return Quantity<UnitT>(std::floor(x.value));
414 }
415
422 template<typename UnitT>
424 return std::isnan(x.value);
425 }
426
433 template<typename UnitT>
435 return std::isfinite(x.value);
436 }
437}
438
439#endif // PHYTH_QUANTITY_FUNCS_HPP
Scalar cos(const Scalar q)
Cosine of a dimensionless quantity.
bool isnan(Quantity< UnitT > x)
Determine whether a quantity is nan.
auto cbrt(Quantity< UnitT > q)
Cube root of a quantity.
Scalar atan2(const Scalar y, const Scalar x)
Two-argument arc tangent (atan2)
Scalar log2(const Scalar q)
Base-2 logarithm.
auto square(Quantity< UnitT > q)
Square of a quantity.
auto sqrt(Quantity< UnitT > q)
Square root of a quantity.
Scalar log(const Scalar q)
Natural logarithm (base e)
Quantity< UnitT > fmod(Quantity< UnitT > x, Quantity< UnitT > y)
Floating-point remainder of x / y.
auto cube(Quantity< UnitT > q)
Cube of a quantity.
Scalar log10(const Scalar q)
Base-10 logarithm.
Scalar exp(const Scalar q)
Exponential (e^q)
Scalar tanh(const Scalar q)
Hyperbolic tangent.
Quantity< UnitT > max(Quantity< UnitT > x, Quantity< UnitT > y)
Maximum of two quantities.
auto root(Quantity< UnitT > q)
N-th root of a quantity.
Scalar atanh(const Scalar q)
Inverse hyperbolic tangent.
auto abs(Quantity< UnitT > q)
Absolute value of a quantity.
Scalar asin(const Scalar q)
Arc sine (inverse sine)
Scalar atan(const Scalar q)
Arc tangent (inverse tangent)
Quantity< UnitT > min(Quantity< UnitT > x, Quantity< UnitT > y)
Minimum of two quantities.
Quantity< UnitT > floor(Quantity< UnitT > x)
Floor of a quantity (largest integer <= x)
Scalar asinh(const Scalar q)
Inverse hyperbolic sine.
Scalar tan(const Scalar q)
Tangent of a dimensionless quantity.
Quantity< UnitT > modf(Quantity< UnitT > x, Quantity< UnitT > &y)
Decompose into integer and fractional parts.
Quantity< UnitT > clamp(Quantity< UnitT > x, Quantity< UnitT > min, Quantity< UnitT > max)
Clamp a quantity between min and max bounds.
Scalar acos(const Scalar q)
Arc cosine (inverse cosine)
Scalar acosh(const Scalar q)
Inverse hyperbolic cosine.
Scalar exp2(const Scalar q)
Base-2 exponential (2^q)
auto pow(Quantity< UnitT > q)
Raise a quantity to an integer power.
Scalar cosh(const Scalar q)
Hyperbolic cosine.
Scalar expm1(const Scalar q)
exp(q) - 1 with higher precision for small q
Scalar sinh(const Scalar q)
Hyperbolic sine.
bool isfinite(Quantity< UnitT > x)
Determine whether a quantity is finite.
Scalar log1p(const Scalar q)
log(1 + q) with higher precision for small q
Scalar sin(const Scalar q)
Sine of a dimensionless quantity.
typename DimPowerType< Power, D >::type DimPowerTypeT
Convenience alias for DimPowerType.
DimDivT< Energy, Time > Power
Quantity< ScalarUnit > Scalar
Definition Quantities.hpp:9
typename DimRcp< D >::type DimRcpT
Convenience alias for DimRcp.
typename DimMul< D1, D2 >::type DimMulT
Convenience alias for DimMul.
Definition Dimension.hpp:71
constexpr bool is_unit_v
Definition Unit.hpp:79
typename DimRoot< N, D >::type DimRootT
Convenience alias for DimRoot.
Physical unit with dimension and scale factor relative to SI base.
Definition Unit.hpp:41