Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Frequency.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_FREQUENCY_HPP
26 #define PHQ_FREQUENCY_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Time.hpp"
34 #include "Unit/Frequency.hpp"
35 
36 namespace PhQ {
37 
38 /// \brief Frequency. Inverse of a time duration. See also PhQ::Time.
39 template <typename NumericType = double>
40 class Frequency : public DimensionalScalar<Unit::Frequency, NumericType> {
41 public:
42  /// \brief Default constructor. Constructs a frequency with an uninitialized value.
43  Frequency() = default;
44 
45  /// \brief Constructor. Constructs a frequency with a given value expressed in a given frequency
46  /// unit.
47  Frequency(const NumericType value, const Unit::Frequency unit)
48  : DimensionalScalar<Unit::Frequency, NumericType>(value, unit) {}
49 
50  /// \brief Constructor. Constructs a frequency from a given time period using the definition of
51  /// frequency.
52  constexpr explicit Frequency(const Time<NumericType>& time)
53  : Frequency<NumericType>(1.0 / time.Value()) {}
54 
55  /// \brief Constructor. Constructs a frequency from a given scalar acceleration and speed using
56  /// the definitions of acceleration and frequency.
57  constexpr Frequency(
58  const ScalarAcceleration<NumericType>& scalar_acceleration, const Speed<NumericType>& speed);
59 
60  /// \brief Constructor. Constructs a frequency from a given scalar angular acceleration and
61  /// angular speed using the definitions of angular acceleration and frequency.
62  constexpr Frequency(const ScalarAngularAcceleration<NumericType>& scalar_angular_acceleration,
63  const AngularSpeed<NumericType>& angular_speed);
64 
65  /// \brief Constructor. Constructs a frequency from a given angular speed and angle using the
66  /// definitions of angular speed and frequency.
67  constexpr Frequency(
68  const AngularSpeed<NumericType>& angular_speed, const Angle<NumericType>& angle);
69 
70  /// \brief Constructor. Constructs a frequency from a given electric current and electric charge
71  /// using the definitions of electric current and frequency.
72  constexpr Frequency(const ElectricCurrent<NumericType>& electric_current,
73  const ElectricCharge<NumericType>& electric_charge);
74 
75  /// \brief Constructor. Constructs a frequency from a given mass rate and mass using the
76  /// definitions of mass rate and frequency.
77  constexpr Frequency(const MassRate<NumericType>& mass_rate, const Mass<NumericType>& mass);
78 
79  /// \brief Constructor. Constructs a frequency from a given memory rate and memory using the
80  /// definitions of memory rate and frequency.
81  constexpr Frequency(
82  const MemoryRate<NumericType>& memory_rate, const Memory<NumericType>& memory);
83 
84  /// \brief Constructor. Constructs a frequency from a given power and energy using the definitions
85  /// of power and frequency.
86  constexpr Frequency(const Power<NumericType>& power, const Energy<NumericType>& energy);
87 
88  /// \brief Constructor. Constructs a frequency from a given specific power and specific energy
89  /// using the definitions of specific power and frequency.
90  constexpr Frequency(const SpecificPower<NumericType>& specific_power,
91  const SpecificEnergy<NumericType>& specific_energy);
92 
93  /// \brief Constructor. Constructs a frequency from a given speed and length using the definitions
94  /// of speed and frequency.
95  constexpr Frequency(const Speed<NumericType>& speed, const Length<NumericType>& length);
96 
97  /// \brief Constructor. Constructs a frequency from a given volume rate and volume using the
98  /// definitions of volume rate and frequency.
99  constexpr Frequency(
100  const VolumeRate<NumericType>& volume_rate, const Volume<NumericType>& volume);
101 
102  /// \brief Destructor. Destroys this frequency.
103  ~Frequency() noexcept = default;
104 
105  /// \brief Copy constructor. Constructs a frequency by copying another one.
106  constexpr Frequency(const Frequency<NumericType>& other) = default;
107 
108  /// \brief Copy constructor. Constructs a frequency by copying another one.
109  template <typename OtherNumericType>
110  explicit constexpr Frequency(const Frequency<OtherNumericType>& other)
111  : Frequency(static_cast<NumericType>(other.Value())) {}
112 
113  /// \brief Move constructor. Constructs a frequency by moving another one.
114  constexpr Frequency(Frequency<NumericType>&& other) noexcept = default;
115 
116  /// \brief Copy assignment operator. Assigns this frequency by copying another one.
117  constexpr Frequency<NumericType>& operator=(const Frequency<NumericType>& other) = default;
118 
119  /// \brief Copy assignment operator. Assigns this frequency by copying another one.
120  template <typename OtherNumericType>
122  this->value = static_cast<NumericType>(other.Value());
123  return *this;
124  }
125 
126  /// \brief Move assignment operator. Assigns this frequency by moving another one.
127  constexpr Frequency<NumericType>& operator=(Frequency<NumericType>&& other) noexcept = default;
128 
129  /// \brief Statically creates a frequency of zero.
130  [[nodiscard]] static constexpr Frequency<NumericType> Zero() {
131  return Frequency<NumericType>{static_cast<NumericType>(0)};
132  }
133 
134  /// \brief Statically creates a frequency with a given value expressed in a given frequency unit.
135  template <Unit::Frequency Unit>
136  [[nodiscard]] static constexpr Frequency<NumericType> Create(const NumericType value) {
137  return Frequency<NumericType>{
138  ConvertStatically<Unit::Frequency, Unit, Standard<Unit::Frequency>>(value)};
139  }
140 
141  /// \brief Time period that corresponds to this frequency.
142  [[nodiscard]] constexpr Time<NumericType> Period() const {
143  return Time<NumericType>{*this};
144  }
145 
146  constexpr Frequency<NumericType> operator+(const Frequency<NumericType>& frequency) const {
147  return Frequency<NumericType>{this->value + frequency.value};
148  }
149 
150  constexpr Frequency<NumericType> operator-(const Frequency<NumericType>& frequency) const {
151  return Frequency<NumericType>{this->value - frequency.value};
152  }
153 
154  constexpr Frequency<NumericType> operator*(const NumericType number) const {
155  return Frequency<NumericType>{this->value * number};
156  }
157 
158  constexpr Frequency<NumericType> operator/(const NumericType number) const {
159  return Frequency<NumericType>{this->value / number};
160  }
161 
162  constexpr NumericType operator*(const Time<NumericType>& time) const noexcept {
163  return this->value * time.Value();
164  }
165 
166  constexpr Speed<NumericType> operator*(const Length<NumericType>& length) const;
167 
169 
171  const ElectricCharge<NumericType>& electric_charge) const;
172 
174  const PlanarDisplacement<NumericType>& displacement) const;
175 
176  constexpr Velocity<NumericType> operator*(const Displacement<NumericType>& displacement) const;
177 
179  const PlanarVelocity<NumericType>& displacement) const;
180 
181  constexpr Acceleration<NumericType> operator*(const Velocity<NumericType>& displacement) const;
182 
184  const DisplacementGradient<NumericType>& displacement_gradient) const;
185 
187  const ScalarDisplacementGradient<NumericType>& scalar_displacement_gradient) const;
188 
189  constexpr MemoryRate<NumericType> operator*(const Memory<NumericType>& memory) const;
190 
192 
194  const AngularSpeed<NumericType>& angular_speed) const;
195 
196  constexpr MassRate<NumericType> operator*(const Mass<NumericType>& mass) const;
197 
198  constexpr VolumeRate<NumericType> operator*(const Volume<NumericType>& volume) const;
199 
200  constexpr Power<NumericType> operator*(const Energy<NumericType>& energy) const;
201 
203  const SpecificEnergy<NumericType>& specific_energy) const;
204 
205  constexpr StrainRate<NumericType> operator*(const Strain<NumericType>& strain) const;
206 
208  const ScalarStrain<NumericType>& scalar_strain) const;
209 
210  constexpr NumericType operator/(const Frequency<NumericType>& frequency) const noexcept {
211  return this->value / frequency.value;
212  }
213 
214  constexpr void operator+=(const Frequency<NumericType>& frequency) noexcept {
215  this->value += frequency.value;
216  }
217 
218  constexpr void operator-=(const Frequency<NumericType>& frequency) noexcept {
219  this->value -= frequency.value;
220  }
221 
222  constexpr void operator*=(const NumericType number) noexcept {
223  this->value *= number;
224  }
225 
226  constexpr void operator/=(const NumericType number) noexcept {
227  this->value /= number;
228  }
229 
230 private:
231  /// \brief Constructor. Constructs a frequency with a given value expressed in the standard
232  /// frequency unit.
233  explicit constexpr Frequency(const NumericType value)
234  : DimensionalScalar<Unit::Frequency, NumericType>(value) {}
235 };
236 
237 template <typename NumericType>
238 inline constexpr bool operator==(
239  const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
240  return left.Value() == right.Value();
241 }
242 
243 template <typename NumericType>
244 inline constexpr bool operator!=(
245  const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
246  return left.Value() != right.Value();
247 }
248 
249 template <typename NumericType>
250 inline constexpr bool operator<(
251  const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
252  return left.Value() < right.Value();
253 }
254 
255 template <typename NumericType>
256 inline constexpr bool operator>(
257  const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
258  return left.Value() > right.Value();
259 }
260 
261 template <typename NumericType>
262 inline constexpr bool operator<=(
263  const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
264  return left.Value() <= right.Value();
265 }
266 
267 template <typename NumericType>
268 inline constexpr bool operator>=(
269  const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
270  return left.Value() >= right.Value();
271 }
272 
273 template <typename NumericType>
274 inline std::ostream& operator<<(std::ostream& stream, const Frequency<NumericType>& frequency) {
275  stream << frequency.Print();
276  return stream;
277 }
278 
279 template <typename NumericType>
281  const NumericType number, const Frequency<NumericType>& frequency) {
282  return frequency * number;
283 }
284 
285 template <typename NumericType>
286 inline constexpr Time<NumericType>::Time(const PhQ::Frequency<NumericType>& frequency)
287  : Time<NumericType>(1.0 / frequency.Value()) {}
288 
289 template <typename NumericType>
291  return PhQ::Frequency<NumericType>{*this};
292 }
293 
294 template <typename NumericType>
295 inline constexpr NumericType Time<NumericType>::operator*(
296  const PhQ::Frequency<NumericType>& frequency) const noexcept {
297  return this->value * frequency.Value();
298 }
299 
300 } // namespace PhQ
301 
302 namespace std {
303 
304 template <typename NumericType>
305 struct hash<PhQ::Frequency<NumericType>> {
306  inline size_t operator()(const PhQ::Frequency<NumericType>& frequency) const {
307  return hash<NumericType>()(frequency.Value());
308  }
309 };
310 
311 } // namespace std
312 
313 #endif // PHQ_FREQUENCY_HPP
Three-dimensional Euclidean acceleration vector. Contains three components in Cartesian coordinates: ...
Plane angle between two lines or dihedral angle between two planes.
Definition: Angle.hpp:130
Angular speed, also known as angular rate or angular frequency. Represents the time rate of change of...
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::Frequency 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 displacement gradient dyadic tensor. Gradient of the displacement vector....
Three-dimensional Euclidean displacement vector. Contains three components in Cartesian coordinates: ...
Electric charge.
Electric current, also known as amperage. Represents a flow of electric charge or a time rate of chan...
Energy physical quantity. Can represent any kind of energy, such as kinetic energy,...
Definition: Energy.hpp:73
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition: Frequency.hpp:40
constexpr PlanarAcceleration< NumericType > operator*(const PlanarVelocity< NumericType > &displacement) const
constexpr Frequency(const VolumeRate< NumericType > &volume_rate, const Volume< NumericType > &volume)
Constructor. Constructs a frequency from a given volume rate and volume using the definitions of volu...
constexpr Frequency(Frequency< NumericType > &&other) noexcept=default
Move constructor. Constructs a frequency by moving another one.
constexpr Frequency(const SpecificPower< NumericType > &specific_power, const SpecificEnergy< NumericType > &specific_energy)
Constructor. Constructs a frequency from a given specific power and specific energy using the definit...
constexpr Frequency(const MemoryRate< NumericType > &memory_rate, const Memory< NumericType > &memory)
Constructor. Constructs a frequency from a given memory rate and memory using the definitions of memo...
constexpr Frequency< NumericType > operator-(const Frequency< NumericType > &frequency) const
Definition: Frequency.hpp:150
static constexpr Frequency< NumericType > Create(const NumericType value)
Statically creates a frequency with a given value expressed in a given frequency unit.
Definition: Frequency.hpp:136
constexpr Frequency< NumericType > operator/(const NumericType number) const
Definition: Frequency.hpp:158
constexpr void operator+=(const Frequency< NumericType > &frequency) noexcept
Definition: Frequency.hpp:214
constexpr Frequency(const ScalarAcceleration< NumericType > &scalar_acceleration, const Speed< NumericType > &speed)
Constructor. Constructs a frequency from a given scalar acceleration and speed using the definitions ...
constexpr Frequency(const MassRate< NumericType > &mass_rate, const Mass< NumericType > &mass)
Constructor. Constructs a frequency from a given mass rate and mass using the definitions of mass rat...
constexpr Frequency(const ElectricCurrent< NumericType > &electric_current, const ElectricCharge< NumericType > &electric_charge)
Constructor. Constructs a frequency from a given electric current and electric charge using the defin...
constexpr ScalarAcceleration< NumericType > operator*(const Speed< NumericType > &speed) const
constexpr Frequency(const Power< NumericType > &power, const Energy< NumericType > &energy)
Constructor. Constructs a frequency from a given power and energy using the definitions of power and ...
constexpr NumericType operator/(const Frequency< NumericType > &frequency) const noexcept
Definition: Frequency.hpp:210
constexpr Time< NumericType > Period() const
Time period that corresponds to this frequency.
Definition: Frequency.hpp:142
constexpr Frequency< NumericType > & operator=(const Frequency< NumericType > &other)=default
Copy assignment operator. Assigns this frequency by copying another one.
constexpr Frequency(const ScalarAngularAcceleration< NumericType > &scalar_angular_acceleration, const AngularSpeed< NumericType > &angular_speed)
Constructor. Constructs a frequency from a given scalar angular acceleration and angular speed using ...
constexpr ElectricCurrent< NumericType > operator*(const ElectricCharge< NumericType > &electric_charge) const
constexpr Frequency(const Time< NumericType > &time)
Constructor. Constructs a frequency from a given time period using the definition of frequency.
Definition: Frequency.hpp:52
constexpr Acceleration< NumericType > operator*(const Velocity< NumericType > &displacement) const
constexpr Frequency< NumericType > operator*(const NumericType number) const
Definition: Frequency.hpp:154
constexpr PlanarVelocity< NumericType > operator*(const PlanarDisplacement< NumericType > &displacement) const
constexpr void operator-=(const Frequency< NumericType > &frequency) noexcept
Definition: Frequency.hpp:218
constexpr Speed< NumericType > operator*(const Length< NumericType > &length) const
~Frequency() noexcept=default
Destructor. Destroys this frequency.
constexpr Frequency< NumericType > operator+(const Frequency< NumericType > &frequency) const
Definition: Frequency.hpp:146
constexpr NumericType operator*(const Time< NumericType > &time) const noexcept
Definition: Frequency.hpp:162
constexpr SpecificPower< NumericType > operator*(const SpecificEnergy< NumericType > &specific_energy) const
Frequency(const NumericType value, const Unit::Frequency unit)
Constructor. Constructs a frequency with a given value expressed in a given frequency unit.
Definition: Frequency.hpp:47
constexpr ScalarVelocityGradient< NumericType > operator*(const ScalarDisplacementGradient< NumericType > &scalar_displacement_gradient) const
constexpr Velocity< NumericType > operator*(const Displacement< NumericType > &displacement) const
constexpr Frequency(const Speed< NumericType > &speed, const Length< NumericType > &length)
Constructor. Constructs a frequency from a given speed and length using the definitions of speed and ...
constexpr ScalarStrainRate< NumericType > operator*(const ScalarStrain< NumericType > &scalar_strain) const
static constexpr Frequency< NumericType > Zero()
Statically creates a frequency of zero.
Definition: Frequency.hpp:130
constexpr Frequency< NumericType > & operator=(const Frequency< OtherNumericType > &other)
Copy assignment operator. Assigns this frequency by copying another one.
Definition: Frequency.hpp:121
constexpr MassRate< NumericType > operator*(const Mass< NumericType > &mass) const
constexpr Frequency(const AngularSpeed< NumericType > &angular_speed, const Angle< NumericType > &angle)
Constructor. Constructs a frequency from a given angular speed and angle using the definitions of ang...
constexpr ScalarAngularAcceleration< NumericType > operator*(const AngularSpeed< NumericType > &angular_speed) const
constexpr VolumeRate< NumericType > operator*(const Volume< NumericType > &volume) const
constexpr Frequency(const NumericType value)
Constructor. Constructs a frequency with a given value expressed in the standard frequency unit.
Definition: Frequency.hpp:233
constexpr Power< NumericType > operator*(const Energy< NumericType > &energy) const
constexpr AngularSpeed< NumericType > operator*(const Angle< NumericType > &angle) const
constexpr StrainRate< NumericType > operator*(const Strain< NumericType > &strain) const
Frequency()=default
Default constructor. Constructs a frequency with an uninitialized value.
constexpr MemoryRate< NumericType > operator*(const Memory< NumericType > &memory) const
constexpr void operator/=(const NumericType number) noexcept
Definition: Frequency.hpp:226
constexpr Frequency< NumericType > & operator=(Frequency< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this frequency by moving another one.
constexpr VelocityGradient< NumericType > operator*(const DisplacementGradient< NumericType > &displacement_gradient) const
constexpr void operator*=(const NumericType number) noexcept
Definition: Frequency.hpp:222
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition: Length.hpp:111
Mass rate. Can represent the time rate of change of a mass or a mass flow rate; see PhQ::Mass,...
Definition: MassRate.hpp:51
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition: Mass.hpp:100
Computer memory rate. Can represent the time rate of change of memory or a memory transfer speed; see...
Definition: MemoryRate.hpp:43
Computer memory. For the time rate of change of computer memory, see PhQ::MemoryRate; see also PhQ::T...
Definition: Memory.hpp:52
Two-dimensional Euclidean acceleration vector in the XY plane. Contains two components in Cartesian c...
Two-dimensional Euclidean displacement vector in the XY plane. Contains two components in Cartesian c...
Two-dimensional Euclidean velocity vector in the XY plane. Contains two components in Cartesian coord...
Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,...
Definition: Power.hpp:51
Scalar acceleration component or magnitude of an acceleration vector. For a three-dimensional Euclide...
Scalar angular acceleration. Represents the time rate of change of an angular speed; see also PhQ::An...
Scalar component or resultant of a three-dimensional Euclidean displacement gradient dyadic tensor....
Scalar component or resultant of a three-dimensional Euclidean strain rate symmetric dyadic tensor....
Scalar component or resultant of a three-dimensional Euclidean strain symmetric dyadic tensor....
Scalar component or resultant of a three-dimensional Euclidean velocity gradient dyadic tensor....
Mass-specific energy. Energy per unit mass; see PhQ::Energy and PhQ::Mass.
Mass-specific power. Power per unit mass; see PhQ::Power and PhQ::Mass.
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
Three-dimensional Euclidean strain rate symmetric dyadic tensor. Time rate of change of strain....
Definition: StrainRate.hpp:51
Three-dimensional Euclidean strain symmetric dyadic tensor. Contains six components in Cartesian coor...
Definition: Strain.hpp:68
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.
Three-dimensional Euclidean velocity gradient dyadic tensor. Gradient of the velocity vector....
Three-dimensional Euclidean velocity vector. Contains three components in Cartesian coordinates: x,...
Definition: Velocity.hpp:55
Volume rate. Can represent a time rate of change of a volume or a volume flow rate....
Definition: VolumeRate.hpp:51
Volume. For the time rate of change of volume, see PhQ::VolumeRate; see also PhQ::Time and PhQ::Frequ...
Definition: Volume.hpp:62
Frequency
Frequency units.
Definition: Frequency.hpp:53
Time
Time units.
Definition: Time.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 Frequency< NumericType > operator*(const NumericType number, const Frequency< NumericType > &frequency)
Definition: Frequency.hpp:280
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 bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)