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
ElectricCurrent.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_ELECTRIC_CURRENT_HPP
26#define PHQ_ELECTRIC_CURRENT_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
33#include "ElectricCharge.hpp"
34#include "Frequency.hpp"
35#include "Time.hpp"
37
38namespace PhQ {
39
40/// \brief Electric current, also known as amperage. Represents a flow of electric charge or a time
41/// rate of change of electric charge.
42template <typename NumericType = double>
43class ElectricCurrent : public DimensionalScalar<Unit::ElectricCurrent, NumericType> {
44public:
45 /// \brief Default constructor. Constructs an electric current with an uninitialized value.
46 ElectricCurrent() = default;
47
48 /// \brief Constructor. Constructs an electric current with a given value expressed in a given
49 /// electric current unit.
50 ElectricCurrent(const NumericType value, const Unit::ElectricCurrent unit)
51 : DimensionalScalar<Unit::ElectricCurrent, NumericType>(value, unit) {}
52
53 /// \brief Constructor. Constructs an electric current from a given electric charge and time using
54 /// the definition of electric current.
55 constexpr ElectricCurrent(
56 const ElectricCharge<NumericType>& electric_charge, const Time<NumericType>& time)
57 : ElectricCurrent<NumericType>(electric_charge.Value() / time.Value()) {}
58
59 /// \brief Constructor. Constructs an electric current from a given electric charge and frequency
60 /// using the definition of electric current.
61 constexpr ElectricCurrent(
62 const ElectricCharge<NumericType>& electric_charge, const Frequency<NumericType>& frequency)
63 : ElectricCurrent<NumericType>(electric_charge.Value() * frequency.Value()) {}
64
65 /// \brief Destructor. Destroys this electric current.
66 ~ElectricCurrent() noexcept = default;
67
68 /// \brief Copy constructor. Constructs an electric current by copying another one.
69 constexpr ElectricCurrent(const ElectricCurrent<NumericType>& other) = default;
70
71 /// \brief Copy constructor. Constructs an electric current by copying another one.
72 template <typename OtherNumericType>
73 explicit constexpr ElectricCurrent(const ElectricCurrent<OtherNumericType>& other)
74 : ElectricCurrent(static_cast<NumericType>(other.Value())) {}
75
76 /// \brief Move constructor. Constructs an electric current by moving another one.
77 constexpr ElectricCurrent(ElectricCurrent<NumericType>&& other) noexcept = default;
78
79 /// \brief Copy assignment operator. Assigns this electric current by copying another one.
81 const ElectricCurrent<NumericType>& other) = default;
82
83 /// \brief Copy assignment operator. Assigns this electric current by copying another one.
84 template <typename OtherNumericType>
87 this->value = static_cast<NumericType>(other.Value());
88 return *this;
89 }
90
91 /// \brief Move assignment operator. Assigns this electric current by moving another one.
93 ElectricCurrent<NumericType>&& other) noexcept = default;
94
95 /// \brief Statically creates an electric current of zero.
96 [[nodiscard]] static constexpr ElectricCurrent<NumericType> Zero() {
97 return ElectricCurrent<NumericType>{static_cast<NumericType>(0)};
98 }
99
100 /// \brief Statically creates an electric current with a given value expressed in a given electric
101 /// current unit.
102 template <Unit::ElectricCurrent Unit>
103 [[nodiscard]] static constexpr ElectricCurrent<NumericType> Create(const NumericType value) {
105 ConvertStatically<Unit::ElectricCurrent, Unit, Standard<Unit::ElectricCurrent>>(value)};
106 }
107
109 const ElectricCurrent<NumericType>& electric_current) const {
110 return ElectricCurrent<NumericType>{this->value + electric_current.value};
111 }
112
114 const ElectricCurrent<NumericType>& electric_current) const {
115 return ElectricCurrent<NumericType>{this->value - electric_current.value};
116 }
117
118 constexpr ElectricCurrent<NumericType> operator*(const NumericType number) const {
119 return ElectricCurrent<NumericType>{this->value * number};
120 }
121
123 return ElectricCharge<NumericType>{*this, time};
124 }
125
126 constexpr ElectricCurrent<NumericType> operator/(const NumericType number) const {
127 return ElectricCurrent<NumericType>{this->value / number};
128 }
129
130 constexpr NumericType operator/(
131 const ElectricCurrent<NumericType>& electric_current) const noexcept {
132 return this->value / electric_current.value;
133 }
134
136 return ElectricCharge<NumericType>{*this, frequency};
137 }
138
140 const ElectricCharge<NumericType>& electric_charge) const {
141 return Frequency<NumericType>{*this, electric_charge};
142 }
143
144 constexpr void operator+=(const ElectricCurrent<NumericType>& electric_current) noexcept {
145 this->value += electric_current.value;
146 }
147
148 constexpr void operator-=(const ElectricCurrent<NumericType>& electric_current) noexcept {
149 this->value -= electric_current.value;
150 }
151
152 constexpr void operator*=(const NumericType number) noexcept {
153 this->value *= number;
154 }
155
156 constexpr void operator/=(const NumericType number) noexcept {
157 this->value /= number;
158 }
159
160private:
161 /// \brief Constructor. Constructs an electric current with a given value expressed in the
162 /// standard electric current unit.
163 explicit constexpr ElectricCurrent(const NumericType value)
164 : DimensionalScalar<Unit::ElectricCurrent, NumericType>(value) {}
165};
166
167template <typename NumericType>
168inline constexpr bool operator==(
169 const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
170 return left.Value() == right.Value();
171}
172
173template <typename NumericType>
174inline constexpr bool operator!=(
175 const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
176 return left.Value() != right.Value();
177}
178
179template <typename NumericType>
180inline constexpr bool operator<(
181 const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
182 return left.Value() < right.Value();
183}
184
185template <typename NumericType>
186inline constexpr bool operator>(
187 const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
188 return left.Value() > right.Value();
189}
190
191template <typename NumericType>
192inline constexpr bool operator<=(
193 const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
194 return left.Value() <= right.Value();
195}
196
197template <typename NumericType>
198inline constexpr bool operator>=(
199 const ElectricCurrent<NumericType>& left, const ElectricCurrent<NumericType>& right) noexcept {
200 return left.Value() >= right.Value();
201}
202
203template <typename NumericType>
204inline std::ostream& operator<<(
205 std::ostream& stream, const ElectricCurrent<NumericType>& electric_current) {
206 stream << electric_current.Print();
207 return stream;
208}
209
210template <typename NumericType>
212 const NumericType number, const ElectricCurrent<NumericType>& electric_current) {
213 return electric_current * number;
214}
215
216template <typename NumericType>
217inline constexpr Time<NumericType>::Time(const ElectricCharge<NumericType>& electric_charge,
218 const ElectricCurrent<NumericType>& electric_current)
219 : Time<NumericType>(electric_charge.Value() / electric_current.Value()) {}
220
221template <typename NumericType>
223 const ElectricCurrent<NumericType>& electric_current,
224 const ElectricCharge<NumericType>& electric_charge)
225 : Frequency<NumericType>(electric_current.Value() / electric_charge.Value()) {}
226
227template <typename NumericType>
229 const ElectricCurrent<NumericType>& electric_current, const Time<NumericType>& time)
230 : ElectricCharge<NumericType>(electric_current.Value() * time.Value()) {}
231
232template <typename NumericType>
234 const ElectricCurrent<NumericType>& electric_current, const Frequency<NumericType>& frequency)
235 : ElectricCharge<NumericType>(electric_current.Value() / frequency.Value()) {}
236
237template <typename NumericType>
239 const ElectricCurrent<NumericType>& electric_current) const {
240 return ElectricCharge<NumericType>{electric_current, *this};
241}
242
243template <typename NumericType>
245 const ElectricCharge<NumericType>& electric_charge) const {
246 return ElectricCurrent<NumericType>{electric_charge, *this};
247}
248
249template <typename NumericType>
251 const Frequency<NumericType>& frequency) const {
252 return ElectricCurrent<NumericType>{*this, frequency};
253}
254
255template <typename NumericType>
257 const Time<NumericType>& time) const {
258 return ElectricCurrent<NumericType>{*this, time};
259}
260
261template <typename NumericType>
263 const ElectricCurrent<NumericType>& electric_current) const {
264 return Time<NumericType>{*this, electric_current};
265}
266
267} // namespace PhQ
268
269namespace std {
270
271template <typename NumericType>
272struct hash<PhQ::ElectricCurrent<NumericType>> {
273 inline size_t operator()(const PhQ::ElectricCurrent<NumericType>& electric_current) const {
274 return hash<NumericType>()(electric_current.Value());
275 }
276};
277
278} // namespace std
279
280#endif // PHQ_ELECTRIC_CURRENT_HPP
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...
Electric charge.
constexpr ElectricCharge< NumericType > operator/(const NumericType number) const
ElectricCharge()=default
Default constructor. Constructs an electric charge with an uninitialized value.
constexpr ElectricCharge< NumericType > operator*(const NumericType number) const
Electric current, also known as amperage. Represents a flow of electric charge or a time rate of chan...
static constexpr ElectricCurrent< NumericType > Create(const NumericType value)
Statically creates an electric current with a given value expressed in a given electric current unit.
constexpr ElectricCurrent< NumericType > operator*(const NumericType number) const
~ElectricCurrent() noexcept=default
Destructor. Destroys this electric current.
constexpr void operator/=(const NumericType number) noexcept
constexpr void operator*=(const NumericType number) noexcept
constexpr void operator+=(const ElectricCurrent< NumericType > &electric_current) noexcept
constexpr ElectricCurrent< NumericType > & operator=(const ElectricCurrent< NumericType > &other)=default
Copy assignment operator. Assigns this electric current by copying another one.
constexpr ElectricCurrent(const ElectricCharge< NumericType > &electric_charge, const Time< NumericType > &time)
Constructor. Constructs an electric current from a given electric charge and time using the definitio...
constexpr ElectricCurrent(const ElectricCharge< NumericType > &electric_charge, const Frequency< NumericType > &frequency)
Constructor. Constructs an electric current from a given electric charge and frequency using the defi...
constexpr ElectricCurrent< NumericType > & operator=(const ElectricCurrent< OtherNumericType > &other)
Copy assignment operator. Assigns this electric current by copying another one.
constexpr ElectricCurrent< NumericType > operator/(const NumericType number) const
static constexpr ElectricCurrent< NumericType > Zero()
Statically creates an electric current of zero.
constexpr void operator-=(const ElectricCurrent< NumericType > &electric_current) noexcept
ElectricCurrent(const NumericType value, const Unit::ElectricCurrent unit)
Constructor. Constructs an electric current with a given value expressed in a given electric current ...
constexpr ElectricCurrent< NumericType > & operator=(ElectricCurrent< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this electric current by moving another one.
constexpr ElectricCurrent< NumericType > operator+(const ElectricCurrent< NumericType > &electric_current) const
constexpr NumericType operator/(const ElectricCurrent< NumericType > &electric_current) const noexcept
ElectricCurrent()=default
Default constructor. Constructs an electric current with an uninitialized value.
constexpr ElectricCharge< NumericType > operator/(const Frequency< NumericType > &frequency) const
constexpr ElectricCurrent< NumericType > operator-(const ElectricCurrent< NumericType > &electric_current) const
constexpr ElectricCharge< NumericType > operator*(const Time< NumericType > &time) const
constexpr ElectricCurrent(const NumericType value)
Constructor. Constructs an electric current with a given value expressed in the standard electric cur...
constexpr Frequency< NumericType > operator/(const ElectricCharge< NumericType > &electric_charge) const
constexpr ElectricCurrent(ElectricCurrent< NumericType > &&other) noexcept=default
Move constructor. Constructs an electric current by moving another one.
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.
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.
ElectricCurrent
Electric current 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