Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
DimensionlessScalar.hpp
Go to the documentation of this file.
1 // Copyright © 2020-2024 Alexandre Coderre-Chabot
2 //
3 // This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
4 // models, and units of measure for scientific computing.
5 //
6 // Physical Quantities is hosted at:
7 // https://github.com/acodcha/phq
8 //
9 // Physical Quantities is licensed under the MIT License:
10 // https://mit-license.org
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
13 // associated documentation files (the "Software"), to deal in the Software without restriction,
14 // including without limitation the rights to use, copy, modify, merge, publish, distribute,
15 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 // - The above copyright notice and this permission notice shall be included in all copies or
18 // substantial portions of the Software.
19 // - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20 // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 #ifndef PHQ_DIMENSIONLESS_SCALAR_HPP
26 #define PHQ_DIMENSIONLESS_SCALAR_HPP
27 
28 #include <cmath>
29 #include <cstddef>
30 #include <cstdint>
31 #include <functional>
32 #include <ostream>
33 #include <string>
34 #include <type_traits>
35 
36 #include "Base.hpp"
37 #include "Dimensions.hpp"
38 
39 namespace PhQ {
40 
41 /// \brief Abstract base class that represents any dimensionless scalar physical quantity. Such a
42 /// physical quantity is composed only of a value where the value is a scalar number. Such a
43 /// physical quantity has no unit of measure and no dimension set.
44 /// \tparam NumericType Floating-point numeric type: float, double, or long double. Defaults to
45 /// double if unspecified.
46 template <typename NumericType = double>
48  static_assert(std::is_floating_point<NumericType>::value,
49  "The NumericType template parameter of a physical quantity must be a numeric "
50  "floating-point type: float, double, or long double.");
51 
52 public:
53  /// \brief Physical dimension set of this physical quantity. Since this physical quantity is
54  /// dimensionless, its physical dimension set is simply the null set.
55  [[nodiscard]] static constexpr PhQ::Dimensions Dimensions() {
56  return PhQ::Dimensionless;
57  }
58 
59  /// \brief Value of this physical quantity.
60  [[nodiscard]] inline constexpr NumericType Value() const noexcept {
61  return value;
62  }
63 
64  /// \brief Returns the value of this physical quantity as a mutable value.
65  [[nodiscard]] inline constexpr NumericType& MutableValue() noexcept {
66  return value;
67  }
68 
69  /// \brief Sets the value of this physical quantity to the given value.
70  inline constexpr void SetValue(const NumericType value) noexcept {
71  this->value = value;
72  }
73 
74  /// \brief Prints this physical quantity as a string.
75  [[nodiscard]] std::string Print() const {
76  return PhQ::Print(value);
77  }
78 
79  /// \brief Serializes this physical quantity as a JSON message.
80  [[nodiscard]] std::string JSON() const {
81  return PhQ::Print(value);
82  }
83 
84  /// \brief Serializes this physical quantity as an XML message.
85  [[nodiscard]] std::string XML() const {
86  return PhQ::Print(value);
87  }
88 
89  /// \brief Serializes this physical quantity as a YAML message.
90  [[nodiscard]] std::string YAML() const {
91  return PhQ::Print(value);
92  }
93 
94 protected:
95  /// \brief Default constructor. Constructs a dimensionless scalar physical quantity with an
96  /// uninitialized value.
97  DimensionlessScalar() = default;
98 
99  /// \brief Constructor. Constructs a dimensionless scalar physical quantity with a given value.
100  explicit constexpr DimensionlessScalar(const NumericType value) : value(value) {}
101 
102  /// \brief Destructor. Destroys this dimensionless scalar physical quantity.
103  ~DimensionlessScalar() noexcept = default;
104 
105  /// \brief Copy constructor. Constructs a dimensionless scalar physical quantity by copying
106  /// another one.
107  constexpr DimensionlessScalar(const DimensionlessScalar<NumericType>& other) = default;
108 
109  /// \brief Copy constructor. Constructs a dimensionless scalar physical quantity by copying
110  /// another one.
111  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
112  /// automatically.
113  template <typename OtherNumericType>
114  explicit constexpr DimensionlessScalar(const DimensionlessScalar<OtherNumericType>& other)
115  : value(static_cast<NumericType>(other.Value())) {}
116 
117  /// \brief Move constructor. Constructs a dimensionless scalar physical quantity by moving another
118  /// one.
119  constexpr DimensionlessScalar(DimensionlessScalar<NumericType>&& other) noexcept = default;
120 
121  /// \brief Copy assignment operator. Assigns this dimensionless scalar physical quantity by
122  /// copying another one.
124  const DimensionlessScalar<NumericType>& other) = default;
125 
126  /// \brief Copy assignment operator. Assigns this dimensionless scalar physical quantity by
127  /// copying another one.
128  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
129  /// automatically.
130  template <typename OtherNumericType>
133  value = static_cast<NumericType>(other.Value());
134  return *this;
135  }
136 
137  /// \brief Move assignment operator. Assigns this dimensionless scalar physical quantity by moving
138  /// another one.
140  DimensionlessScalar<NumericType>&& other) noexcept = default;
141 
142  /// \brief Value of this physical quantity.
143  NumericType value;
144 };
145 
146 } // namespace PhQ
147 
148 namespace std {
149 
150 template <typename NumericType>
151 inline constexpr NumericType abs(
152  const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar) {
153  return abs(dimensionless_scalar.Value());
154 }
155 
156 template <typename NumericType>
157 inline NumericType cbrt(
158  const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar) noexcept {
159  return cbrt(dimensionless_scalar.Value());
160 };
161 
162 template <typename NumericType>
163 inline NumericType exp(const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar) noexcept {
164  return exp(dimensionless_scalar.Value());
165 };
166 
167 template <typename NumericType>
168 inline NumericType log(const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar) noexcept {
169  return log(dimensionless_scalar.Value());
170 };
171 
172 template <typename NumericType>
173 inline NumericType log2(
174  const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar) noexcept {
175  return log2(dimensionless_scalar.Value());
176 };
177 
178 template <typename NumericType>
179 inline NumericType log10(
180  const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar) noexcept {
181  return log10(dimensionless_scalar.Value());
182 };
183 
184 template <typename NumericType, typename OtherNumericType>
185 inline NumericType pow(const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar,
186  const OtherNumericType exponent) noexcept {
187  return pow(dimensionless_scalar.Value(), exponent);
188 };
189 
190 template <typename NumericType>
191 inline NumericType sqrt(
192  const PhQ::DimensionlessScalar<NumericType>& dimensionless_scalar) noexcept {
193  return sqrt(dimensionless_scalar.Value());
194 };
195 
196 } // namespace std
197 
198 #endif // PHQ_DIMENSIONLESS_SCALAR_HPP
Abstract base class that represents any dimensionless scalar physical quantity. Such a physical quant...
constexpr NumericType Value() const noexcept
Value of this physical quantity.
std::string XML() const
Serializes this physical quantity as an XML message.
static constexpr PhQ::Dimensions Dimensions()
Physical dimension set of this physical quantity. Since this physical quantity is dimensionless,...
constexpr DimensionlessScalar< NumericType > & operator=(const DimensionlessScalar< OtherNumericType > &other)
Copy assignment operator. Assigns this dimensionless scalar physical quantity by copying another one.
std::string JSON() const
Serializes this physical quantity as a JSON message.
constexpr DimensionlessScalar(const NumericType value)
Constructor. Constructs a dimensionless scalar physical quantity with a given value.
constexpr DimensionlessScalar(DimensionlessScalar< NumericType > &&other) noexcept=default
Move constructor. Constructs a dimensionless scalar physical quantity by moving another one.
NumericType value
Value of this physical quantity.
std::string YAML() const
Serializes this physical quantity as a YAML message.
constexpr DimensionlessScalar< NumericType > & operator=(DimensionlessScalar< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this dimensionless scalar physical quantity by moving another one.
std::string Print() const
Prints this physical quantity as a string.
constexpr DimensionlessScalar< NumericType > & operator=(const DimensionlessScalar< NumericType > &other)=default
Copy assignment operator. Assigns this dimensionless scalar physical quantity by copying another one.
DimensionlessScalar()=default
Default constructor. Constructs a dimensionless scalar physical quantity with an uninitialized value.
~DimensionlessScalar() noexcept=default
Destructor. Destroys this dimensionless scalar physical quantity.
constexpr NumericType & MutableValue() noexcept
Returns the value of this physical quantity as a mutable value.
constexpr void SetValue(const NumericType value) noexcept
Sets the value of this physical quantity to the given value.
Physical dimension set of a unit of measure or physical quantity. Composed of the seven independent b...
Definition: Dimensions.hpp:49
Namespace that encompasses all of the Physical Quantities library's content.
constexpr Dimensions Dimensionless
Dimensionless physical dimension set. This dimension set has all base dimensions of zero....
Definition: Dimensions.hpp:406
std::string Print(const NumericType value)
Prints a given floating-point number as a string. Prints enough digits to represent the number exactl...
Definition: Base.hpp:170