Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Power.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_POWER_HPP
26 #define PHQ_POWER_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Energy.hpp"
34 #include "Frequency.hpp"
35 #include "Time.hpp"
36 #include "Unit/Power.hpp"
37 
38 namespace PhQ {
39 
40 // Forward declaration for class PhQ::Power.
41 template <typename NumericType>
42 class Speed;
43 
44 // Forward declaration for class PhQ::Power.
45 template <typename NumericType>
47 
48 /// \brief Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,
49 /// and PhQ::Frequency.
50 template <typename NumericType = double>
51 class Power : public DimensionalScalar<Unit::Power, NumericType> {
52 public:
53  /// \brief Default constructor. Constructs a power quantity with an uninitialized value.
54  Power() = default;
55 
56  /// \brief Constructor. Constructs a power quantity with a given value expressed in a given power
57  /// unit.
58  Power(const NumericType value, const Unit::Power unit)
59  : DimensionalScalar<Unit::Power, NumericType>(value, unit) {}
60 
61  /// \brief Constructor. Constructs a power quantity from a given energy and time duration using
62  /// the definition of power.
63  constexpr Power(const Energy<NumericType>& energy, const Time<NumericType>& time)
64  : Power<NumericType>(energy.Value() / time.Value()) {}
65 
66  /// \brief Constructor. Constructs a power quantity from a given energy and frequency using the
67  /// definition of power.
68  constexpr Power(const Energy<NumericType>& energy, const Frequency<NumericType>& frequency)
69  : Power<NumericType>(energy.Value() * frequency.Value()) {}
70 
71  /// \brief Constructor. Constructs a power quantity from a given specific power and mass using the
72  /// definition of specific power.
73  constexpr Power(const SpecificPower<NumericType>& specific_power, const Mass<NumericType>& mass);
74 
75  /// \brief Constructor. Constructs a power quantity from a given transport energy consumption and
76  /// speed using the definition of transport energy consumption.
77  constexpr Power(const TransportEnergyConsumption<NumericType>& transport_energy_consumption,
78  const Speed<NumericType>& speed);
79 
80  /// \brief Destructor. Destroys this power quantity.
81  ~Power() noexcept = default;
82 
83  /// \brief Copy constructor. Constructs a power quantity by copying another one.
84  constexpr Power(const Power<NumericType>& other) = default;
85 
86  /// \brief Copy constructor. Constructs a power quantity by copying another one.
87  template <typename OtherNumericType>
88  explicit constexpr Power(const Power<OtherNumericType>& other)
89  : Power(static_cast<NumericType>(other.Value())) {}
90 
91  /// \brief Move constructor. Constructs a power quantity by moving another one.
92  constexpr Power(Power<NumericType>&& other) noexcept = default;
93 
94  /// \brief Copy assignment operator. Assigns this power quantity by copying another one.
95  constexpr Power<NumericType>& operator=(const Power<NumericType>& other) = default;
96 
97  /// \brief Copy assignment operator. Assigns this power quantity by copying another one.
98  template <typename OtherNumericType>
100  this->value = static_cast<NumericType>(other.Value());
101  return *this;
102  }
103 
104  /// \brief Move assignment operator. Assigns this power quantity by moving another one.
105  constexpr Power<NumericType>& operator=(Power<NumericType>&& other) noexcept = default;
106 
107  /// \brief Statically creates a power quantity of zero.
108  [[nodiscard]] static constexpr Power<NumericType> Zero() {
109  return Power<NumericType>{static_cast<NumericType>(0)};
110  }
111 
112  /// \brief Statically creates a power quantity with a given value expressed in a given power unit.
113  template <Unit::Power Unit>
114  [[nodiscard]] static constexpr Power<NumericType> Create(const NumericType value) {
115  return Power<NumericType>{ConvertStatically<Unit::Power, Unit, Standard<Unit::Power>>(value)};
116  }
117 
118  constexpr Power<NumericType> operator+(const Power<NumericType>& power) const {
119  return Power<NumericType>{this->value + power.value};
120  }
121 
122  constexpr Power<NumericType> operator-(const Power<NumericType>& power) const {
123  return Power<NumericType>{this->value - power.value};
124  }
125 
126  constexpr Power<NumericType> operator*(const NumericType number) const {
127  return Power<NumericType>{this->value * number};
128  }
129 
130  constexpr Energy<NumericType> operator*(const Time<NumericType>& time) const {
131  return Energy<NumericType>{*this, time};
132  }
133 
134  constexpr Power<NumericType> operator/(const NumericType number) const {
135  return Power<NumericType>{this->value / number};
136  }
137 
138  constexpr Energy<NumericType> operator/(const Frequency<NumericType>& frequency) const {
139  return Energy<NumericType>{*this, frequency};
140  }
141 
142  constexpr Frequency<NumericType> operator/(const Energy<NumericType>& energy) const {
143  return Frequency<NumericType>{*this, energy};
144  }
145 
147 
148  constexpr Mass<NumericType> operator/(const SpecificPower<NumericType>& specific_power) const;
149 
150  constexpr NumericType operator/(const Power<NumericType>& power) const noexcept {
151  return this->value / power.value;
152  }
153 
154  constexpr void operator+=(const Power<NumericType>& power) noexcept {
155  this->value += power.value;
156  }
157 
158  constexpr void operator-=(const Power<NumericType>& power) noexcept {
159  this->value -= power.value;
160  }
161 
162  constexpr void operator*=(const NumericType number) noexcept {
163  this->value *= number;
164  }
165 
166  constexpr void operator/=(const NumericType number) noexcept {
167  this->value /= number;
168  }
169 
170 private:
171  /// \brief Constructor. Constructs a power quantity with a given value expressed in the standard
172  /// power unit.
173  explicit constexpr Power(const NumericType value)
174  : DimensionalScalar<Unit::Power, NumericType>(value) {}
175 };
176 
177 template <typename NumericType>
178 inline constexpr bool operator==(
179  const Power<NumericType>& left, const Power<NumericType>& right) noexcept {
180  return left.Value() == right.Value();
181 }
182 
183 template <typename NumericType>
184 inline constexpr bool operator!=(
185  const Power<NumericType>& left, const Power<NumericType>& right) noexcept {
186  return left.Value() != right.Value();
187 }
188 
189 template <typename NumericType>
190 inline constexpr bool operator<(
191  const Power<NumericType>& left, const Power<NumericType>& right) noexcept {
192  return left.Value() < right.Value();
193 }
194 
195 template <typename NumericType>
196 inline constexpr bool operator>(
197  const Power<NumericType>& left, const Power<NumericType>& right) noexcept {
198  return left.Value() > right.Value();
199 }
200 
201 template <typename NumericType>
202 inline constexpr bool operator<=(
203  const Power<NumericType>& left, const Power<NumericType>& right) noexcept {
204  return left.Value() <= right.Value();
205 }
206 
207 template <typename NumericType>
208 inline constexpr bool operator>=(
209  const Power<NumericType>& left, const Power<NumericType>& right) noexcept {
210  return left.Value() >= right.Value();
211 }
212 
213 template <typename NumericType>
214 inline std::ostream& operator<<(std::ostream& stream, const Power<NumericType>& power) {
215  stream << power.Print();
216  return stream;
217 }
218 
219 template <typename NumericType>
220 inline constexpr Power<NumericType> operator*(
221  const NumericType number, const Power<NumericType>& power) {
222  return power * number;
223 }
224 
225 template <typename NumericType>
226 inline constexpr Time<NumericType>::Time(
227  const Energy<NumericType>& energy, const Power<NumericType>& power)
228  : Time<NumericType>(energy.Value() / power.Value()) {}
229 
230 template <typename NumericType>
231 inline constexpr Frequency<NumericType>::Frequency(
232  const Power<NumericType>& power, const Energy<NumericType>& energy)
233  : Frequency<NumericType>(power.Value() / energy.Value()) {}
234 
235 template <typename NumericType>
236 inline constexpr Energy<NumericType>::Energy(
237  const Power<NumericType>& power, const Time<NumericType>& time)
238  : Energy<NumericType>(power.Value() * time.Value()) {}
239 
240 template <typename NumericType>
241 inline constexpr Energy<NumericType>::Energy(
242  const Power<NumericType>& power, const Frequency<NumericType>& frequency)
243  : Energy<NumericType>(power.Value() / frequency.Value()) {}
244 
245 template <typename NumericType>
246 inline constexpr Energy<NumericType> Time<NumericType>::operator*(
247  const Power<NumericType>& power) const {
248  return Energy<NumericType>{power, *this};
249 }
250 
251 template <typename NumericType>
252 inline constexpr Power<NumericType> Frequency<NumericType>::operator*(
253  const Energy<NumericType>& energy) const {
254  return Power<NumericType>{energy, *this};
255 }
256 
257 template <typename NumericType>
258 inline constexpr Power<NumericType> Energy<NumericType>::operator*(
259  const Frequency<NumericType>& frequency) const {
260  return Power<NumericType>{*this, frequency};
261 }
262 
263 template <typename NumericType>
264 inline constexpr Power<NumericType> Energy<NumericType>::operator/(
265  const Time<NumericType>& time) const {
266  return Power<NumericType>{*this, time};
267 }
268 
269 template <typename NumericType>
270 inline constexpr Time<NumericType> Energy<NumericType>::operator/(
271  const Power<NumericType>& power) const {
272  return Time<NumericType>{*this, power};
273 }
274 
275 } // namespace PhQ
276 
277 namespace std {
278 
279 template <typename NumericType>
280 struct hash<PhQ::Power<NumericType>> {
281  inline size_t operator()(const PhQ::Power<NumericType>& power) const {
282  return hash<NumericType>()(power.Value());
283  }
284 };
285 
286 } // namespace std
287 
288 #endif // PHQ_POWER_HPP
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::Power 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...
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
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition: Mass.hpp:100
Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,...
Definition: Power.hpp:51
constexpr Power(const Energy< NumericType > &energy, const Frequency< NumericType > &frequency)
Constructor. Constructs a power quantity from a given energy and frequency using the definition of po...
Definition: Power.hpp:68
constexpr Energy< NumericType > operator/(const Frequency< NumericType > &frequency) const
Definition: Power.hpp:138
constexpr Energy< NumericType > operator*(const Time< NumericType > &time) const
Definition: Power.hpp:130
Power(const NumericType value, const Unit::Power unit)
Constructor. Constructs a power quantity with a given value expressed in a given power unit.
Definition: Power.hpp:58
constexpr void operator-=(const Power< NumericType > &power) noexcept
Definition: Power.hpp:158
constexpr Power< NumericType > operator*(const NumericType number) const
Definition: Power.hpp:126
constexpr NumericType operator/(const Power< NumericType > &power) const noexcept
Definition: Power.hpp:150
constexpr void operator*=(const NumericType number) noexcept
Definition: Power.hpp:162
constexpr Power(const SpecificPower< NumericType > &specific_power, const Mass< NumericType > &mass)
Constructor. Constructs a power quantity from a given specific power and mass using the definition of...
constexpr Power(Power< NumericType > &&other) noexcept=default
Move constructor. Constructs a power quantity by moving another one.
constexpr SpecificPower< NumericType > operator/(const Mass< NumericType > &mass) const
constexpr Power(const Energy< NumericType > &energy, const Time< NumericType > &time)
Constructor. Constructs a power quantity from a given energy and time duration using the definition o...
Definition: Power.hpp:63
constexpr Frequency< NumericType > operator/(const Energy< NumericType > &energy) const
Definition: Power.hpp:142
constexpr Power(const NumericType value)
Constructor. Constructs a power quantity with a given value expressed in the standard power unit.
Definition: Power.hpp:173
constexpr Power< NumericType > & operator=(Power< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this power quantity by moving another one.
~Power() noexcept=default
Destructor. Destroys this power quantity.
constexpr Power< NumericType > operator/(const NumericType number) const
Definition: Power.hpp:134
constexpr void operator/=(const NumericType number) noexcept
Definition: Power.hpp:166
constexpr Power< NumericType > & operator=(const Power< OtherNumericType > &other)
Copy assignment operator. Assigns this power quantity by copying another one.
Definition: Power.hpp:99
constexpr void operator+=(const Power< NumericType > &power) noexcept
Definition: Power.hpp:154
static constexpr Power< NumericType > Create(const NumericType value)
Statically creates a power quantity with a given value expressed in a given power unit.
Definition: Power.hpp:114
static constexpr Power< NumericType > Zero()
Statically creates a power quantity of zero.
Definition: Power.hpp:108
constexpr Power< NumericType > & operator=(const Power< NumericType > &other)=default
Copy assignment operator. Assigns this power quantity by copying another one.
constexpr Power< NumericType > operator+(const Power< NumericType > &power) const
Definition: Power.hpp:118
constexpr Power(const TransportEnergyConsumption< NumericType > &transport_energy_consumption, const Speed< NumericType > &speed)
Constructor. Constructs a power quantity from a given transport energy consumption and speed using th...
Power()=default
Default constructor. Constructs a power quantity with an uninitialized value.
constexpr Power< NumericType > operator-(const Power< NumericType > &power) const
Definition: Power.hpp:122
constexpr Mass< NumericType > operator/(const SpecificPower< NumericType > &specific_power) const
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
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.
Transport energy consumption, also known as energy consumption in transport. A measure of energy use ...
Power
Power units.
Definition: Power.hpp:53
Frequency
Frequency units.
Definition: Frequency.hpp:53
Energy
Energy units.
Definition: Energy.hpp:53
TransportEnergyConsumption
Energy consumption in transport 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 Power< NumericType > operator*(const NumericType number, const Power< NumericType > &power)
Definition: Power.hpp:220
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
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)