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
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
36namespace PhQ {
37
38/// \brief Frequency. Inverse of a time duration. See also PhQ::Time.
39template <typename NumericType = double>
40class Frequency : public DimensionalScalar<Unit::Frequency, NumericType> {
41public:
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) {
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
168 constexpr AngularSpeed<NumericType> operator*(const Angle<NumericType>& angle) const;
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
230private:
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
237template <typename NumericType>
238inline constexpr bool operator==(
239 const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
240 return left.Value() == right.Value();
241}
242
243template <typename NumericType>
244inline constexpr bool operator!=(
245 const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
246 return left.Value() != right.Value();
247}
248
249template <typename NumericType>
250inline constexpr bool operator<(
251 const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
252 return left.Value() < right.Value();
253}
254
255template <typename NumericType>
256inline constexpr bool operator>(
257 const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
258 return left.Value() > right.Value();
259}
260
261template <typename NumericType>
262inline constexpr bool operator<=(
263 const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
264 return left.Value() <= right.Value();
265}
266
267template <typename NumericType>
268inline constexpr bool operator>=(
269 const Frequency<NumericType>& left, const Frequency<NumericType>& right) noexcept {
270 return left.Value() >= right.Value();
271}
272
273template <typename NumericType>
274inline std::ostream& operator<<(std::ostream& stream, const Frequency<NumericType>& frequency) {
275 stream << frequency.Print();
276 return stream;
277}
278
279template <typename NumericType>
281 const NumericType number, const Frequency<NumericType>& frequency) {
282 return frequency * number;
283}
284
285template <typename NumericType>
286inline constexpr Time<NumericType>::Time(const PhQ::Frequency<NumericType>& frequency)
287 : Time<NumericType>(1.0 / frequency.Value()) {}
288
289template <typename NumericType>
293
294template <typename NumericType>
295inline 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
302namespace std {
303
304template <typename NumericType>
305struct 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...
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...
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 Frequency< NumericType > & operator=(Frequency< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this frequency by moving another one.
constexpr Frequency(Frequency< NumericType > &&other) noexcept=default
Move constructor. Constructs a frequency by moving another one.
static constexpr Frequency< NumericType > Create(const NumericType value)
Statically creates a frequency with a given value expressed in a given frequency unit.
constexpr Frequency< NumericType > operator/(const NumericType number) const
constexpr void operator+=(const Frequency< NumericType > &frequency) noexcept
constexpr Frequency< NumericType > & operator=(const Frequency< NumericType > &other)=default
Copy assignment operator. Assigns this frequency by copying another one.
constexpr Frequency< NumericType > operator-(const Frequency< NumericType > &frequency) const
constexpr NumericType operator/(const Frequency< NumericType > &frequency) const noexcept
static constexpr Frequency< NumericType > Zero()
Statically creates a frequency of zero.
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 void operator-=(const Frequency< NumericType > &frequency) noexcept
~Frequency() noexcept=default
Destructor. Destroys this frequency.
constexpr NumericType operator*(const Time< NumericType > &time) const noexcept
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 Frequency< NumericType > & operator=(const Frequency< OtherNumericType > &other)
Copy assignment operator. Assigns this frequency by copying another one.
constexpr Frequency(const NumericType value)
Constructor. Constructs a frequency with a given value expressed in the standard frequency unit.
constexpr Time< NumericType > Period() const
Time period that corresponds to this frequency.
constexpr Frequency< NumericType > operator*(const NumericType number) const
Frequency()=default
Default constructor. Constructs a frequency with an uninitialized value.
constexpr void operator/=(const NumericType number) noexcept
constexpr Frequency< NumericType > operator+(const Frequency< NumericType > &frequency) const
constexpr void operator*=(const NumericType number) noexcept
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...
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....
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
constexpr Time< NumericType > operator*(const NumericType number) const
Definition Time.hpp:278
constexpr PhQ::Frequency< NumericType > Frequency() const
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....
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
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