1#ifndef PHYTH_DIMENSION_HPP
2#define PHYTH_DIMENSION_HPP
24 template<
typename... Ratios>
26 static constexpr int size =
sizeof...(Ratios);
38 template<
int N,
typename D>
41 template<
int N,
typename... Ratios>
43 static_assert(N <
sizeof...(Ratios),
"Index out of range");
44 using type = std::tuple_element_t<N, std::tuple<Ratios...> >;
50 template<
int N,
typename D>
58 template<
typename D1,
typename D2>
61 template<
typename... R1,
typename... R2>
63 static_assert(
sizeof...(R1) ==
sizeof...(R2),
"Dimension count mismatch");
70 template<
typename D1,
typename D2>
78 template<
typename D1,
typename D2>
81 template<
typename... R1,
typename... R2>
83 static_assert(
sizeof...(R1) ==
sizeof...(R2),
"Dimension count mismatch");
90 template<
typename D1,
typename D2>
100 template<
int N,
typename D>
103 template<
int N,
typename... Ratios>
105 static_assert(N > 0,
"Root index must be positive");
112 template<
int N,
typename D>
123 template<
typename... Ratios>
137 template<
typename D1,
typename D2>
140 template<
typename... R1,
typename... R2>
142 static constexpr bool value = (std::is_same_v<R1, R2> && ...);
148 template<
typename D1,
typename D2>
158 template<
typename... Ratios>
175 if (std::floor(power + 0.5) == power) {
176 return std::to_string(
static_cast<int>(power));
178 return std::to_string(power);
196 template<
typename... Ratios>
199 const std::string names[] = {
"m",
"kg",
"s",
"A",
"K",
"mol",
"cd"};
201 std::string numerator, denominator;
202 bool has_num =
false, has_den =
false;
206 const double power =
static_cast<double>(Ratios::num) / Ratios::den;
212 const std::string unit = names[idx];
213 std::string power_str;
214 if (power != 1 && power != -1) {
215 power_str =
formatPower(power < 0 ? -power : power);
219 if (has_num) numerator +=
'*';
222 numerator +=
"^" + power_str;
226 if (has_den) denominator +=
'*';
229 denominator +=
"^" + power_str;
239 }
else if (has_den) {
245 if (denominator.find(
'*') != std::string::npos) {
246 result +=
"(" + denominator +
")";
248 result += denominator;
257 std::ratio<0>, std::ratio<0>, std::ratio<0>,
258 std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>
262 std::ratio<1>, std::ratio<0>, std::ratio<0>,
263 std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>
267 std::ratio<0>, std::ratio<1>, std::ratio<0>,
268 std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>
272 std::ratio<0>, std::ratio<0>, std::ratio<1>,
273 std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>
277 std::ratio<0>, std::ratio<0>, std::ratio<0>,
278 std::ratio<1>, std::ratio<0>, std::ratio<0>, std::ratio<0>
282 std::ratio<0>, std::ratio<0>, std::ratio<0>,
283 std::ratio<0>, std::ratio<1>, std::ratio<0>, std::ratio<0>
287 std::ratio<0>, std::ratio<0>, std::ratio<0>,
288 std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>
292 std::ratio<0>, std::ratio<0>, std::ratio<0>,
293 std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>
332 template<
typename UnitT>
334 static constexpr bool value = std::is_same_v<typename UnitT::DimensionT, Dimensionless>;
340 template<
typename UnitT>
349 template<
int N,
typename D>
352 template<
int N,
typename... Ratios>
354 static constexpr double value =
static_cast<double>(
355 std::tuple_element_t<N, std::tuple<Ratios...> >::num
356 ) / std::tuple_element_t<N, std::tuple<Ratios...> >::den;
366 template<
int N,
typename D>
375 template<
int P,
typename D>
385 template<
int Power,
typename D>
397 template<
int Power,
typename D>
typename DimPowerType< Power, D >::type DimPowerTypeT
Convenience alias for DimPowerType.
DimDivT< Mass, Time > DampingCoefficient
constexpr bool dim_equal_v
Convenience variable template for DimEqual.
DimDivT< Force, Volume > ForceDensity
DimDivT< Energy, Time > Power
DimMulT< Mass, Area > MomentOfInertia
typename DimDiv< D1, D2 >::type DimDivT
Convenience alias for DimDiv.
DimDivT< Force, Charge > ElectricFieldIntensity
DimDivT< Dimensionless, Time > Frequency
DimDivT< Force, DimMulT< Length, Length > > Pressure
DimDivT< Voltage, DimDivT< ElectricCurrent, Time > > Inductance
std::tuple_element_t< N, std::tuple< Ratios... > > type
typename DimRcp< D >::type DimRcpT
Convenience alias for DimRcp.
DimDivT< Mass, Volume > MassDensity
DimDivT< Velocity, Time > Acceleration
constexpr bool is_dimensionless_v
Convenience variable template for is_dimensionless.
typename DimMul< D1, D2 >::type DimMulT
Convenience alias for DimMul.
std::string formatPower(const double power)
Format a floating-point exponent for string output.
DimMulT< Voltage, Time > MagneticFlux
typename DimPowerHelper< Power, D >::type type
DimMulT< Length, Area > Volume
DimMulT< Time, ElectricCurrent > Charge
DimDivT< MagneticFlux, DimMulT< Length, Length > > MagneticFluxDensity
constexpr double dim_power_v
Convenience variable template for DimPower.
Dimension< std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 > > Dimensionless
constexpr bool is_dim_v
Convenience variable template for is_dim.
DimDivT< Force, Length > TranslationalStiffness
DimMulT< ElectricCurrent, Area > MagneticDipoleMoment
DimMulT< Mass, Acceleration > Force
DimMulT< D, typename DimPowerHelper< P - 1, D >::type > type
DimDivT< Charge, Voltage > Capacitance
DimDivT< Charge, Length > LinearChargeDensity
DimDivT< Dimensionless, Time > AngularVelocity
DimDivT< Charge, Area > SurfaceChargeDensity
DimDivT< Charge, Volume > BulkChargeDensity
DimDivT< Length, Time > Velocity
DimDivT< Voltage, ElectricCurrent > Resistance
DimDivT< Capacitance, Length > ElectricPotential
typename DimRoot< N, D >::type DimRootT
Convenience alias for DimRoot.
DimDivT< Energy, Volume > EnergyDensity
typename DimComponent< N, D >::type DimComponentT
Convenience alias for DimComponent.
DimMulT< Length, Length > Area
DimMulT< Charge, Length > ElectricDipoleMoment
DimMulT< Force, Length > Energy
DimDivT< Power, ElectricCurrent > Voltage
Extract the N-th base exponent from a Dimension.
Divide one dimension by another: exponent-wise subtraction.
Check if two dimensions are identical (all exponents equal)
Add two dimensions: exponent-wise addition.
Extract the numeric value of the N-th exponent from a Dimension.
Raise a Dimension to an integer power.
Reciprocal of a dimension: negate all exponents.
Take the N-th root of a dimension: divide all exponents by N.
Convert a Dimension to a human-readable string.
static std::string value()
Compile-time physical dimension as 7 SI base exponents.
std::tuple< Ratios... > ratio_tuple
static constexpr int size
Trait: check if a type is a Dimension instantiation.
Trait: check if a quantity's dimension is dimensionless.
static constexpr bool value