Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
ScalarAcceleration.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_SCALAR_ACCELERATION_HPP
26 #define PHQ_SCALAR_ACCELERATION_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Frequency.hpp"
34 #include "Speed.hpp"
35 #include "Time.hpp"
36 #include "Unit/Acceleration.hpp"
37 
38 namespace PhQ {
39 
40 // Forward declaration for class PhQ::ScalarAcceleration.
41 template <typename NumericType>
42 class Acceleration;
43 
44 // Forward declaration for class PhQ::ScalarAcceleration.
45 template <typename NumericType>
46 class Direction;
47 
48 // Forward declaration for class PhQ::ScalarAcceleration.
49 template <typename NumericType>
50 class PlanarAcceleration;
51 
52 // Forward declaration for class PhQ::ScalarAcceleration.
53 template <typename NumericType>
54 class PlanarDirection;
55 
56 /// \brief Scalar acceleration component or magnitude of an acceleration vector. For a
57 /// three-dimensional Euclidean acceleration vector, see PhQ::Acceleration. For a two-dimensional
58 /// Euclidean acceleration vector in the XY plane, see PhQ::PlanarAcceleration.
59 template <typename NumericType = double>
60 class ScalarAcceleration : public DimensionalScalar<Unit::Acceleration, NumericType> {
61 public:
62  /// \brief Default constructor. Constructs a scalar acceleration with an uninitialized value.
63  ScalarAcceleration() = default;
64 
65  /// \brief Constructor. Constructs a scalar acceleration with a given value expressed in a given
66  /// acceleration unit.
67  ScalarAcceleration(const NumericType value, const Unit::Acceleration unit)
68  : DimensionalScalar<Unit::Acceleration, NumericType>(value, unit) {}
69 
70  /// \brief Constructor. Constructs a scalar acceleration from a given speed and time using the
71  /// definition of acceleration.
72  constexpr ScalarAcceleration(const Speed<NumericType>& speed, const Time<NumericType>& time)
73  : ScalarAcceleration<NumericType>(speed.Value() / time.Value()) {}
74 
75  /// \brief Constructor. Constructs a scalar acceleration from a given speed and frequency using
76  /// the definition of acceleration.
77  constexpr ScalarAcceleration(
78  const Speed<NumericType>& speed, const Frequency<NumericType>& frequency)
79  : ScalarAcceleration<NumericType>(speed.Value() * frequency.Value()) {}
80 
81  /// \brief Destructor. Destroys this acceleration scalar.
82  ~ScalarAcceleration() noexcept = default;
83 
84  /// \brief Copy constructor. Constructs a scalar acceleration by copying another one.
85  constexpr ScalarAcceleration(const ScalarAcceleration<NumericType>& other) = default;
86 
87  /// \brief Copy constructor. Constructs a scalar acceleration by copying another one.
88  template <typename OtherNumericType>
89  explicit constexpr ScalarAcceleration(const ScalarAcceleration<OtherNumericType>& other)
90  : ScalarAcceleration(static_cast<NumericType>(other.Value())) {}
91 
92  /// \brief Move constructor. Constructs a scalar acceleration by moving another one.
93  constexpr ScalarAcceleration(ScalarAcceleration<NumericType>&& other) noexcept = default;
94 
95  /// \brief Copy assignment operator. Assigns this scalar acceleration by copying another one.
97  const ScalarAcceleration<NumericType>& other) = default;
98 
99  /// \brief Copy assignment operator. Assigns this scalar acceleration by copying another one.
100  template <typename OtherNumericType>
103  this->value = static_cast<NumericType>(other.Value());
104  return *this;
105  }
106 
107  /// \brief Move assignment operator. Assigns this scalar acceleration by moving another one.
109  ScalarAcceleration<NumericType>&& other) noexcept = default;
110 
111  /// \brief Statically creates a scalar acceleration of zero.
112  [[nodiscard]] static constexpr ScalarAcceleration<NumericType> Zero() {
113  return ScalarAcceleration<NumericType>{static_cast<NumericType>(0)};
114  }
115 
116  /// \brief Statically creates a scalar acceleration with a given value expressed in a given
117  /// acceleration unit.
118  template <Unit::Acceleration Unit>
119  [[nodiscard]] static constexpr ScalarAcceleration<NumericType> Create(const NumericType value) {
121  ConvertStatically<Unit::Acceleration, Unit, Standard<Unit::Acceleration>>(value)};
122  }
123 
125  const ScalarAcceleration<NumericType>& scalar_acceleration) const {
126  return ScalarAcceleration<NumericType>{this->value + scalar_acceleration.value};
127  }
128 
130  const ScalarAcceleration<NumericType>& scalar_acceleration) const {
131  return ScalarAcceleration<NumericType>{this->value - scalar_acceleration.value};
132  }
133 
134  constexpr ScalarAcceleration<NumericType> operator*(const NumericType number) const {
135  return ScalarAcceleration<NumericType>{this->value * number};
136  }
137 
138  constexpr Speed<NumericType> operator*(const Time<NumericType>& time) const {
139  return Speed<NumericType>{*this, time};
140  }
141 
143  const PlanarDirection<NumericType>& planar_direction) const;
144 
145  constexpr Acceleration<NumericType> operator*(const Direction<NumericType>& direction) const;
146 
147  constexpr ScalarAcceleration<NumericType> operator/(const NumericType number) const {
148  return ScalarAcceleration<NumericType>{this->value / number};
149  }
150 
151  constexpr Speed<NumericType> operator/(const Frequency<NumericType>& frequency) const {
152  return Speed<NumericType>{*this, frequency};
153  }
154 
155  constexpr Frequency<NumericType> operator/(const Speed<NumericType>& speed) const {
156  return Frequency<NumericType>{*this, speed};
157  }
158 
159  constexpr NumericType operator/(
160  const ScalarAcceleration<NumericType>& scalar_acceleration) const noexcept {
161  return this->value / scalar_acceleration.value;
162  }
163 
164  constexpr void operator+=(const ScalarAcceleration<NumericType>& scalar_acceleration) noexcept {
165  this->value += scalar_acceleration.value;
166  }
167 
168  constexpr void operator-=(const ScalarAcceleration<NumericType>& scalar_acceleration) noexcept {
169  this->value -= scalar_acceleration.value;
170  }
171 
172  constexpr void operator*=(const NumericType number) noexcept {
173  this->value *= number;
174  }
175 
176  constexpr void operator/=(const NumericType number) noexcept {
177  this->value /= number;
178  }
179 
180 private:
181  /// \brief Constructor. Constructs a scalar acceleration with a given value expressed in the
182  /// standard acceleration unit.
183  explicit constexpr ScalarAcceleration(const NumericType value)
184  : DimensionalScalar<Unit::Acceleration, NumericType>(value) {}
185 
186  template <typename OtherNumericType>
187  friend class PlanarAcceleration;
188 
189  template <typename OtherNumericType>
190  friend class Acceleration;
191 };
192 
193 template <typename NumericType>
194 inline constexpr bool operator==(const ScalarAcceleration<NumericType>& left,
195  const ScalarAcceleration<NumericType>& right) noexcept {
196  return left.Value() == right.Value();
197 }
198 
199 template <typename NumericType>
200 inline constexpr bool operator!=(const ScalarAcceleration<NumericType>& left,
201  const ScalarAcceleration<NumericType>& right) noexcept {
202  return left.Value() != right.Value();
203 }
204 
205 template <typename NumericType>
206 inline constexpr bool operator<(const ScalarAcceleration<NumericType>& left,
207  const ScalarAcceleration<NumericType>& right) noexcept {
208  return left.Value() < right.Value();
209 }
210 
211 template <typename NumericType>
212 inline constexpr bool operator>(const ScalarAcceleration<NumericType>& left,
213  const ScalarAcceleration<NumericType>& right) noexcept {
214  return left.Value() > right.Value();
215 }
216 
217 template <typename NumericType>
218 inline constexpr bool operator<=(const ScalarAcceleration<NumericType>& left,
219  const ScalarAcceleration<NumericType>& right) noexcept {
220  return left.Value() <= right.Value();
221 }
222 
223 template <typename NumericType>
224 inline constexpr bool operator>=(const ScalarAcceleration<NumericType>& left,
225  const ScalarAcceleration<NumericType>& right) noexcept {
226  return left.Value() >= right.Value();
227 }
228 
229 template <typename NumericType>
230 inline std::ostream& operator<<(
231  std::ostream& stream, const ScalarAcceleration<NumericType>& scalar_acceleration) {
232  stream << scalar_acceleration.Print();
233  return stream;
234 }
235 
236 template <typename NumericType>
238  const NumericType number, const ScalarAcceleration<NumericType>& scalar_acceleration) {
239  return scalar_acceleration * number;
240 }
241 
242 template <typename NumericType>
243 inline constexpr Time<NumericType>::Time(
244  const Speed<NumericType>& speed, const ScalarAcceleration<NumericType>& scalar_acceleration)
245  : Time<NumericType>(speed.Value() / scalar_acceleration.Value()) {}
246 
247 template <typename NumericType>
248 inline constexpr Frequency<NumericType>::Frequency(
249  const ScalarAcceleration<NumericType>& scalar_acceleration, const Speed<NumericType>& speed)
250  : Frequency<NumericType>(scalar_acceleration.Value() / speed.Value()) {}
251 
252 template <typename NumericType>
253 inline constexpr Speed<NumericType>::Speed(
254  const ScalarAcceleration<NumericType>& scalar_acceleration, const Time<NumericType>& time)
255  : Speed<NumericType>(scalar_acceleration.Value() * time.Value()) {}
256 
257 template <typename NumericType>
258 inline constexpr Speed<NumericType>::Speed(
259  const ScalarAcceleration<NumericType>& scalar_acceleration,
260  const Frequency<NumericType>& frequency)
261  : Speed<NumericType>(scalar_acceleration.Value() / frequency.Value()) {}
262 
263 template <typename NumericType>
264 inline constexpr ScalarAcceleration<NumericType> Frequency<NumericType>::operator*(
265  const Speed<NumericType>& speed) const {
266  return ScalarAcceleration<NumericType>{speed, *this};
267 }
268 
269 template <typename NumericType>
270 inline constexpr Speed<NumericType> Time<NumericType>::operator*(
271  const ScalarAcceleration<NumericType>& scalar_acceleration) const {
272  return Speed<NumericType>{scalar_acceleration, *this};
273 }
274 
275 template <typename NumericType>
276 inline constexpr ScalarAcceleration<NumericType> Speed<NumericType>::operator*(
277  const Frequency<NumericType>& frequency) const {
278  return ScalarAcceleration<NumericType>{*this, frequency};
279 }
280 
281 template <typename NumericType>
282 inline constexpr ScalarAcceleration<NumericType> Speed<NumericType>::operator/(
283  const Time<NumericType>& time) const {
284  return ScalarAcceleration<NumericType>{*this, time};
285 }
286 
287 template <typename NumericType>
288 inline constexpr Time<NumericType> Speed<NumericType>::operator/(
289  const ScalarAcceleration<NumericType>& scalar_acceleration) const {
290  return Time<NumericType>{*this, scalar_acceleration};
291 }
292 
293 } // namespace PhQ
294 
295 namespace std {
296 
297 template <typename NumericType>
298 struct hash<PhQ::ScalarAcceleration<NumericType>> {
299  inline size_t operator()(const PhQ::ScalarAcceleration<NumericType>& scalar_acceleration) const {
300  return hash<NumericType>()(scalar_acceleration.Value());
301  }
302 };
303 
304 } // namespace std
305 
306 #endif // PHQ_SCALAR_ACCELERATION_HPP
Three-dimensional Euclidean acceleration vector. Contains three components in Cartesian coordinates: ...
Abstract base class that represents any dimensional scalar physical quantity. Such a physical quantit...
double value
Value of this physical quantity expressed in its standard unit of measure.
constexpr double Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr Unit::Acceleration Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
Three-dimensional Euclidean direction vector. Contains three components in Cartesian coordinates: x,...
Definition: Direction.hpp:115
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition: Frequency.hpp:40
Two-dimensional Euclidean acceleration vector in the XY plane. Contains two components in Cartesian c...
two-dimensional Euclidean direction vector in the XY plane. Contains two components in Cartesian coor...
Scalar acceleration component or magnitude of an acceleration vector. For a three-dimensional Euclide...
constexpr Frequency< NumericType > operator/(const Speed< NumericType > &speed) const
constexpr void operator-=(const ScalarAcceleration< NumericType > &scalar_acceleration) noexcept
constexpr ScalarAcceleration(const NumericType value)
Constructor. Constructs a scalar acceleration with a given value expressed in the standard accelerati...
constexpr ScalarAcceleration< NumericType > operator-(const ScalarAcceleration< NumericType > &scalar_acceleration) const
constexpr void operator/=(const NumericType number) noexcept
constexpr ScalarAcceleration< NumericType > & operator=(const ScalarAcceleration< OtherNumericType > &other)
Copy assignment operator. Assigns this scalar acceleration by copying another one.
constexpr ScalarAcceleration(const Speed< NumericType > &speed, const Time< NumericType > &time)
Constructor. Constructs a scalar acceleration from a given speed and time using the definition of acc...
constexpr NumericType operator/(const ScalarAcceleration< NumericType > &scalar_acceleration) const noexcept
constexpr Speed< NumericType > operator/(const Frequency< NumericType > &frequency) const
constexpr ScalarAcceleration< NumericType > operator/(const NumericType number) const
ScalarAcceleration(const NumericType value, const Unit::Acceleration unit)
Constructor. Constructs a scalar acceleration with a given value expressed in a given acceleration un...
constexpr Speed< NumericType > operator*(const Time< NumericType > &time) const
constexpr void operator*=(const NumericType number) noexcept
constexpr ScalarAcceleration< NumericType > & operator=(ScalarAcceleration< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this scalar acceleration by moving another one.
constexpr ScalarAcceleration(ScalarAcceleration< NumericType > &&other) noexcept=default
Move constructor. Constructs a scalar acceleration by moving another one.
constexpr void operator+=(const ScalarAcceleration< NumericType > &scalar_acceleration) noexcept
constexpr ScalarAcceleration< NumericType > operator+(const ScalarAcceleration< NumericType > &scalar_acceleration) const
ScalarAcceleration()=default
Default constructor. Constructs a scalar acceleration with an uninitialized value.
constexpr ScalarAcceleration(const Speed< NumericType > &speed, const Frequency< NumericType > &frequency)
Constructor. Constructs a scalar acceleration from a given speed and frequency using the definition o...
constexpr ScalarAcceleration< NumericType > & operator=(const ScalarAcceleration< NumericType > &other)=default
Copy assignment operator. Assigns this scalar acceleration by copying another one.
static constexpr ScalarAcceleration< NumericType > Zero()
Statically creates a scalar acceleration of zero.
constexpr ScalarAcceleration< NumericType > operator*(const NumericType number) const
~ScalarAcceleration() noexcept=default
Destructor. Destroys this acceleration scalar.
static constexpr ScalarAcceleration< NumericType > Create(const NumericType value)
Statically creates a scalar acceleration with a given value expressed in a given acceleration unit.
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition: Time.hpp:172
Time()=default
Default constructor. Constructs a time quantity with an uninitialized value.
Frequency
Frequency units.
Definition: Frequency.hpp:53
Acceleration
Acceleration units.
Time
Time units.
Definition: Time.hpp:53
Speed
Speed units.
Definition: Speed.hpp:53
Namespace that encompasses all of the Physical Quantities library's content.
constexpr bool operator<(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr ScalarAcceleration< NumericType > operator*(const NumericType number, const ScalarAcceleration< NumericType > &scalar_acceleration)
constexpr bool operator<=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr Acceleration< NumericType > operator*(const NumericType number, const Acceleration< NumericType > &acceleration)
constexpr bool operator>(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator==(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr Dyad< NumericType > operator/(const Dyad< NumericType > &dyad, const OtherNumericType number)
Definition: Dyad.hpp:696
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)