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
DimensionalVector.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_DIMENSIONAL_VECTOR_HPP
26#define PHQ_DIMENSIONAL_VECTOR_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31#include <string>
32
33#include "Base.hpp"
34#include "Dimensions.hpp"
35#include "Unit.hpp"
36#include "Vector.hpp"
37
38namespace PhQ {
39
40/// \brief Abstract base class that represents any dimensional vector physical quantity. Such a
41/// physical quantity is composed of a value and a unit of measure where the value is a
42/// three-dimensional vector.
43/// \tparam UnitType Unit of measure enumeration type.
44/// \tparam NumericType Floating-point numeric type: float, double, or long double. Defaults to
45/// double if unspecified.
46template <typename UnitType, typename NumericType = double>
48public:
49 /// \brief Physical dimension set of this physical quantity.
50 [[nodiscard]] static constexpr const PhQ::Dimensions& Dimensions() {
51 return PhQ::RelatedDimensions<UnitType>;
52 }
53
54 /// \brief Standard unit of measure for this physical quantity. This physical quantity's value is
55 /// stored internally in this unit of measure.
56 [[nodiscard]] static constexpr UnitType Unit() {
57 return PhQ::Standard<UnitType>;
58 }
59
60 /// \brief Value of this physical quantity expressed in its standard unit of measure.
61 [[nodiscard]] constexpr const PhQ::Vector<NumericType>& Value() const noexcept {
62 return value;
63 }
64
65 /// \brief Value of this physical quantity expressed in a given unit of measure.
66 [[nodiscard]] PhQ::Vector<NumericType> Value(const UnitType unit) const {
67 return PhQ::Convert(value, PhQ::Standard<UnitType>, unit);
68 }
69
70 /// \brief Value of this physical quantity expressed in a given unit of measure. This method can
71 /// be evaluated statically at compile-time.
72 template <UnitType NewUnit>
73 [[nodiscard]] constexpr PhQ::Vector<NumericType> StaticValue() const {
74 return PhQ::ConvertStatically<UnitType, PhQ::Standard<UnitType>, NewUnit>(value);
75 }
76
77 /// \brief Returns the value of this physical quantity expressed in its standard unit of measure
78 /// as a mutable value.
79 [[nodiscard]] constexpr PhQ::Vector<NumericType>& MutableValue() noexcept {
80 return value;
81 }
82
83 /// \brief Sets the value of this physical quantity expressed in its standard unit of measure to
84 /// the given value.
85 constexpr void SetValue(const PhQ::Vector<NumericType>& value) noexcept {
86 this->value = value;
87 }
88
89 /// \brief Prints this physical quantity as a string. This physical quantity's value is expressed
90 /// in its standard unit of measure.
91 [[nodiscard]] std::string Print() const {
92 return value.Print().append(" ").append(PhQ::Abbreviation(PhQ::Standard<UnitType>));
93 }
94
95 /// \brief Prints this physical quantity as a string. This physical quantity's value is expressed
96 /// in the given unit of measure.
97 [[nodiscard]] std::string Print(const UnitType unit) const {
98 return Value(unit).Print().append(" ").append(PhQ::Abbreviation(unit));
99 }
100
101 /// \brief Serializes this physical quantity as a JSON message. This physical quantity's value is
102 /// expressed in its standard unit of measure.
103 [[nodiscard]] std::string JSON() const {
104 return std::string{"{\"value\":"}
105 .append(value.JSON())
106 .append(R"(,"unit":")")
107 .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
108 .append("\"}");
109 }
110
111 /// \brief Serializes this physical quantity as a JSON message. This physical quantity's value is
112 /// expressed in the given unit of measure.
113 [[nodiscard]] std::string JSON(const UnitType unit) const {
114 return std::string{"{\"value\":"}
115 .append(Value(unit).JSON())
116 .append(R"(,"unit":")")
117 .append(PhQ::Abbreviation(unit))
118 .append("\"}");
119 }
120
121 /// \brief Serializes this physical quantity as an XML message. This physical quantity's value is
122 /// expressed in its standard unit of measure.
123 [[nodiscard]] std::string XML() const {
124 return std::string{"<value>"}
125 .append(value.XML())
126 .append("</value><unit>")
127 .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
128 .append("</unit>");
129 }
130
131 /// \brief Serializes this physical quantity as an XML message. This physical quantity's value is
132 /// expressed in the given unit of measure.
133 [[nodiscard]] std::string XML(const UnitType unit) const {
134 return std::string{"<value>"}
135 .append(Value(unit).XML())
136 .append("</value><unit>")
137 .append(PhQ::Abbreviation(unit))
138 .append("</unit>");
139 }
140
141 /// \brief Serializes this physical quantity as a YAML message. This physical quantity's value is
142 /// expressed in its standard unit of measure.
143 [[nodiscard]] std::string YAML() const {
144 return std::string{"{value:"}
145 .append(value.YAML())
146 .append(",unit:\"")
147 .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
148 .append("\"}");
149 }
150
151 /// \brief Serializes this physical quantity as a YAML message. This physical quantity's value is
152 /// expressed in the given unit of measure.
153 [[nodiscard]] std::string YAML(const UnitType unit) const {
154 return std::string{"{value:"}
155 .append(Value(unit).YAML())
156 .append(",unit:\"")
157 .append(PhQ::Abbreviation(unit))
158 .append("\"}");
159 }
160
161protected:
162 /// \brief Default constructor. Constructs a dimensional vector physical quantity with an
163 /// uninitialized value.
164 DimensionalVector() = default;
165
166 /// \brief Constructor. Constructs a dimensional vector physical quantity with a given value
167 /// expressed in its standard unit of measure.
169
170 /// \brief Constructor. Constructs a dimensional vector physical quantity with a given value
171 /// expressed in a given unit of measure.
173 PhQ::ConvertInPlace(this->value, unit, PhQ::Standard<UnitType>);
174 }
175
176 /// \brief Destructor. Destroys this dimensional vector physical quantity.
177 ~DimensionalVector() noexcept = default;
178
179 /// \brief Copy constructor. Constructs a dimensional vector physical quantity by copying another
180 /// one.
181 constexpr DimensionalVector(const DimensionalVector<UnitType, NumericType>& other) = default;
182
183 /// \brief Copy constructor. Constructs a dimensional vector physical quantity by copying another
184 /// one.
185 /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
186 /// automatically.
187 template <typename OtherNumericType>
188 explicit constexpr DimensionalVector(const DimensionalVector<UnitType, OtherNumericType>& other)
189 : value(static_cast<PhQ::Vector<NumericType>>(other.Value())) {}
190
191 /// \brief Move constructor. Constructs a dimensional vector physical quantity by moving another
192 /// one.
193 constexpr DimensionalVector(DimensionalVector<UnitType, NumericType>&& other) noexcept = default;
194
195 /// \brief Copy assignment operator. Assigns this dimensional vector physical quantity by copying
196 /// another one.
198 const DimensionalVector<UnitType, NumericType>& other) = default;
199
200 /// \brief Copy assignment operator. Assigns this dimensional vector physical quantity by copying
201 /// another one.
202 /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
203 /// automatically.
204 template <typename OtherNumericType>
207 value = static_cast<PhQ::Vector<NumericType>>(other.Value());
208 return *this;
209 }
210
211 /// \brief Move assignment operator. Assigns this dimensional vector physical quantity by moving
212 /// another one.
214 DimensionalVector<UnitType, NumericType>&& other) noexcept = default;
215
216 /// \brief Value of this physical quantity expressed in its standard unit of measure.
218};
219
220} // namespace PhQ
221
222#endif // PHQ_DIMENSIONAL_VECTOR_HPP
Abstract base class that represents any dimensional vector physical quantity. Such a physical quantit...
constexpr PhQ::Vector< NumericType > & MutableValue() noexcept
Returns the value of this physical quantity expressed in its standard unit of measure as a mutable va...
constexpr DimensionalVector< UnitType, NumericType > & operator=(DimensionalVector< UnitType, NumericType > &&other) noexcept=default
Move assignment operator. Assigns this dimensional vector physical quantity by moving another one.
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
std::string Print(const UnitType unit) const
Prints this physical quantity as a string. This physical quantity's value is expressed in the given u...
constexpr void SetValue(const PhQ::Vector< NumericType > &value) noexcept
Sets the value of this physical quantity expressed in its standard unit of measure to the given value...
std::string JSON() const
Serializes this physical quantity as a JSON message. This physical quantity's value is expressed in i...
~DimensionalVector() noexcept=default
Destructor. Destroys this dimensional vector physical quantity.
std::string YAML(const UnitType unit) const
Serializes this physical quantity as a YAML message. This physical quantity's value is expressed in t...
std::string XML(const UnitType unit) const
Serializes this physical quantity as an XML message. This physical quantity's value is expressed in t...
std::string JSON(const UnitType unit) const
Serializes this physical quantity as a JSON message. This physical quantity's value is expressed in t...
static constexpr UnitType Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
PhQ::Vector< NumericType > Value(const UnitType unit) const
Value of this physical quantity expressed in a given unit of measure.
DimensionalVector(const PhQ::Vector< NumericType > &value, const UnitType unit)
Constructor. Constructs a dimensional vector physical quantity with a given value expressed in a give...
DimensionalVector()=default
Default constructor. Constructs a dimensional vector physical quantity with an uninitialized value.
PhQ::Vector< NumericType > value
Value of this physical quantity expressed in its standard unit of measure.
std::string XML() const
Serializes this physical quantity as an XML message. This physical quantity's value is expressed in i...
constexpr const PhQ::Vector< NumericType > & Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
constexpr PhQ::Vector< NumericType > StaticValue() const
Value of this physical quantity expressed in a given unit of measure. This method can be evaluated st...
static constexpr const PhQ::Dimensions & Dimensions()
Physical dimension set of this physical quantity.
constexpr DimensionalVector(const PhQ::Vector< NumericType > &value)
Constructor. Constructs a dimensional vector physical quantity with a given value expressed in its st...
constexpr DimensionalVector(DimensionalVector< UnitType, NumericType > &&other) noexcept=default
Move constructor. Constructs a dimensional vector physical quantity by moving another one.
constexpr DimensionalVector< UnitType, NumericType > & operator=(const DimensionalVector< UnitType, NumericType > &other)=default
Copy assignment operator. Assigns this dimensional vector physical quantity by copying another one.
constexpr DimensionalVector< UnitType, NumericType > & operator=(const DimensionalVector< UnitType, OtherNumericType > &other)
Copy assignment operator. Assigns this dimensional vector physical quantity by copying another one.
std::string YAML() const
Serializes this physical quantity as a YAML message. This physical quantity's value is expressed in i...
Physical dimension set of a unit of measure or physical quantity. Composed of the seven independent b...
Three-dimensional Euclidean vector. Contains three components in Cartesian coordinates: x,...
Definition Vector.hpp:60
Namespace that encompasses all of the Physical Quantities library's content.
NumericType Convert(const NumericType value, const Unit original_unit, const Unit new_unit)
Converts a value expressed in a given unit of measure to a new unit of measure. Returns the converted...
Definition Unit.hpp:210
void ConvertInPlace(NumericType &value, const Unit original_unit, const Unit new_unit)
Converts a value expressed in a given unit of measure to a new unit of measure. The conversion is per...
Definition Unit.hpp:128
std::string_view Abbreviation(const Enumeration enumeration)
Returns the abbreviation of a given enumeration value. For example, PhQ::Abbreviation(PhQ::Unit::Time...
Definition Base.hpp:89