Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
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
38namespace PhQ {
39
40// Forward declaration for class PhQ::ScalarAcceleration.
41template <typename NumericType>
42class Acceleration;
43
44// Forward declaration for class PhQ::ScalarAcceleration.
45template <typename NumericType>
46class Direction;
47
48// Forward declaration for class PhQ::ScalarAcceleration.
49template <typename NumericType>
50class PlanarAcceleration;
51
52// Forward declaration for class PhQ::ScalarAcceleration.
53template <typename NumericType>
54class 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.
59template <typename NumericType = double>
60class ScalarAcceleration : public DimensionalScalar<Unit::Acceleration, NumericType> {
61public:
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.
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
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
180private:
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
193template <typename NumericType>
194inline constexpr bool operator==(const ScalarAcceleration<NumericType>& left,
195 const ScalarAcceleration<NumericType>& right) noexcept {
196 return left.Value() == right.Value();
197}
198
199template <typename NumericType>
200inline constexpr bool operator!=(const ScalarAcceleration<NumericType>& left,
201 const ScalarAcceleration<NumericType>& right) noexcept {
202 return left.Value() != right.Value();
203}
204
205template <typename NumericType>
206inline constexpr bool operator<(const ScalarAcceleration<NumericType>& left,
207 const ScalarAcceleration<NumericType>& right) noexcept {
208 return left.Value() < right.Value();
209}
210
211template <typename NumericType>
212inline constexpr bool operator>(const ScalarAcceleration<NumericType>& left,
213 const ScalarAcceleration<NumericType>& right) noexcept {
214 return left.Value() > right.Value();
215}
216
217template <typename NumericType>
218inline constexpr bool operator<=(const ScalarAcceleration<NumericType>& left,
219 const ScalarAcceleration<NumericType>& right) noexcept {
220 return left.Value() <= right.Value();
221}
222
223template <typename NumericType>
224inline constexpr bool operator>=(const ScalarAcceleration<NumericType>& left,
225 const ScalarAcceleration<NumericType>& right) noexcept {
226 return left.Value() >= right.Value();
227}
228
229template <typename NumericType>
230inline std::ostream& operator<<(
231 std::ostream& stream, const ScalarAcceleration<NumericType>& scalar_acceleration) {
232 stream << scalar_acceleration.Print();
233 return stream;
234}
235
236template <typename NumericType>
238 const NumericType number, const ScalarAcceleration<NumericType>& scalar_acceleration) {
239 return scalar_acceleration * number;
240}
241
242template <typename NumericType>
243inline constexpr Time<NumericType>::Time(
244 const Speed<NumericType>& speed, const ScalarAcceleration<NumericType>& scalar_acceleration)
245 : Time<NumericType>(speed.Value() / scalar_acceleration.Value()) {}
246
247template <typename NumericType>
249 const ScalarAcceleration<NumericType>& scalar_acceleration, const Speed<NumericType>& speed)
250 : Frequency<NumericType>(scalar_acceleration.Value() / speed.Value()) {}
251
252template <typename NumericType>
254 const ScalarAcceleration<NumericType>& scalar_acceleration, const Time<NumericType>& time)
255 : Speed<NumericType>(scalar_acceleration.Value() * time.Value()) {}
256
257template <typename NumericType>
259 const ScalarAcceleration<NumericType>& scalar_acceleration,
260 const Frequency<NumericType>& frequency)
261 : Speed<NumericType>(scalar_acceleration.Value() / frequency.Value()) {}
262
263template <typename NumericType>
265 const Speed<NumericType>& speed) const {
266 return ScalarAcceleration<NumericType>{speed, *this};
267}
268
269template <typename NumericType>
271 const ScalarAcceleration<NumericType>& scalar_acceleration) const {
272 return Speed<NumericType>{scalar_acceleration, *this};
273}
274
275template <typename NumericType>
277 const Frequency<NumericType>& frequency) const {
278 return ScalarAcceleration<NumericType>{*this, frequency};
279}
280
281template <typename NumericType>
283 const Time<NumericType>& time) const {
284 return ScalarAcceleration<NumericType>{*this, time};
285}
286
287template <typename NumericType>
289 const ScalarAcceleration<NumericType>& scalar_acceleration) const {
290 return Time<NumericType>{*this, scalar_acceleration};
291}
292
293} // namespace PhQ
294
295namespace std {
296
297template <typename NumericType>
298struct 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...
NumericType value
Value of this physical quantity expressed in its standard unit of measure.
constexpr NumericType Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr UnitType 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...
constexpr const PhQ::Vector< NumericType > & Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
Three-dimensional Euclidean direction vector. Contains three components in Cartesian coordinates: x,...
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition Frequency.hpp:40
constexpr Frequency< NumericType > operator*(const NumericType number) const
Frequency()=default
Default constructor. Constructs a frequency with an uninitialized value.
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 void operator-=(const ScalarAcceleration< NumericType > &scalar_acceleration) noexcept
constexpr ScalarAcceleration< NumericType > & operator=(ScalarAcceleration< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this scalar acceleration by moving another one.
constexpr ScalarAcceleration< NumericType > operator*(const NumericType number) const
constexpr ScalarAcceleration(const NumericType value)
Constructor. Constructs a scalar acceleration with a given value expressed in the standard accelerati...
constexpr void operator/=(const NumericType number) noexcept
constexpr Speed< NumericType > operator*(const Time< NumericType > &time) const
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
ScalarAcceleration(const NumericType value, const Unit::Acceleration unit)
Constructor. Constructs a scalar acceleration with a given value expressed in a given acceleration un...
static constexpr ScalarAcceleration< NumericType > Create(const NumericType value)
Statically creates a scalar acceleration with a given value expressed in a given acceleration unit.
constexpr Frequency< NumericType > operator/(const Speed< NumericType > &speed) const
constexpr void operator*=(const NumericType number) noexcept
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< NumericType > & operator=(const ScalarAcceleration< OtherNumericType > &other)
Copy assignment operator. Assigns this scalar acceleration by copying another one.
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 NumericType number) const
constexpr Speed< NumericType > operator/(const Frequency< NumericType > &frequency) const
static constexpr ScalarAcceleration< NumericType > Zero()
Statically creates a scalar acceleration of zero.
constexpr ScalarAcceleration< NumericType > & operator=(const ScalarAcceleration< NumericType > &other)=default
Copy assignment operator. Assigns this scalar acceleration by copying another one.
constexpr ScalarAcceleration< NumericType > operator+(const ScalarAcceleration< NumericType > &scalar_acceleration) const
~ScalarAcceleration() noexcept=default
Destructor. Destroys this acceleration scalar.
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition Speed.hpp:100
Speed()=default
Default constructor. Constructs a speed with an uninitialized value.
constexpr Speed< NumericType > operator*(const NumericType number) const
Definition Speed.hpp:205
constexpr Speed< NumericType > operator/(const NumericType number) const
Definition Speed.hpp:224
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition Time.hpp:172
constexpr Time< NumericType > operator*(const NumericType number) const
Definition Time.hpp:278
Time()=default
Default constructor. Constructs a time quantity with an uninitialized value.
Acceleration
Acceleration units.
Namespace that encompasses all of the Physical Quantities library's content.
std::ostream & operator<<(std::ostream &stream, 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 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