Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
DimensionlessVector.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_DIMENSIONLESS_VECTOR_HPP
26 #define PHQ_DIMENSIONLESS_VECTOR_HPP
27 
28 #include <array>
29 #include <cstddef>
30 #include <functional>
31 #include <ostream>
32 #include <string>
33 #include <type_traits>
34 
35 #include "Base.hpp"
36 #include "Dimensions.hpp"
37 #include "Vector.hpp"
38 
39 namespace PhQ {
40 
41 /// \brief Abstract base class that represents any dimensionless vector physical quantity. Such a
42 /// physical quantity is composed only of a value where the value is a three-dimensional vector.
43 /// Such a physical quantity has no unit of measure and no dimension set.
44 /// \tparam NumericType Floating-point numeric type: float, double, or long double. Defaults to
45 /// double if unspecified.
46 template <typename NumericType = double>
48  static_assert(std::is_floating_point<NumericType>::value,
49  "The NumericType template parameter of a physical quantity must be a numeric "
50  "floating-point type: float, double, or long double.");
51 
52 public:
53  /// \brief Physical dimension set of this physical quantity. Since this physical quantity is
54  /// dimensionless, its physical dimension set is simply the null set.
55  [[nodiscard]] static constexpr PhQ::Dimensions Dimensions() {
56  return PhQ::Dimensionless;
57  }
58 
59  /// \brief Value of this physical quantity.
60  [[nodiscard]] constexpr const PhQ::Vector<NumericType>& Value() const noexcept {
61  return value;
62  }
63 
64  /// \brief Prints this physical quantity as a string.
65  [[nodiscard]] std::string Print() const {
66  return value.Print();
67  }
68 
69  /// \brief Serializes this physical quantity as a JSON message.
70  [[nodiscard]] std::string JSON() const {
71  return value.JSON();
72  }
73 
74  /// \brief Serializes this physical quantity as an XML message.
75  [[nodiscard]] std::string XML() const {
76  return value.XML();
77  }
78 
79  /// \brief Serializes this physical quantity as a YAML message.
80  [[nodiscard]] std::string YAML() const {
81  return value.YAML();
82  }
83 
84 protected:
85  /// \brief Default constructor. Constructs a dimensionless vector physical quantity with an
86  /// uninitialized value.
87  DimensionlessVector() = default;
88 
89  /// \brief Constructor. Constructs a dimensionless vector physical quantity whose value has the
90  /// given x, y, and z Cartesian components.
91  constexpr DimensionlessVector(const NumericType x, const NumericType y, const NumericType z)
92  : value(x, y, z) {}
93 
94  /// \brief Constructor. Constructs a dimensionless vector physical quantity from a given array
95  /// representing its value's x, y, and z Cartesian components.
96  explicit constexpr DimensionlessVector(const std::array<NumericType, 3>& x_y_z) : value(x_y_z) {}
97 
98  /// \brief Constructor. Constructs a dimensionless vector physical quantity with a given value.
100 
101  /// \brief Destructor. Destroys this dimensionless vector physical quantity.
102  ~DimensionlessVector() noexcept = default;
103 
104  /// \brief Copy constructor. Constructs a dimensionless vector physical quantity by copying
105  /// another one.
106  constexpr DimensionlessVector(const DimensionlessVector<NumericType>& other) = default;
107 
108  /// \brief Copy constructor. Constructs a dimensionless vector physical quantity by copying
109  /// another one.
110  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
111  /// automatically.
112  template <typename OtherNumericType>
113  explicit constexpr DimensionlessVector(const DimensionlessVector<OtherNumericType>& other)
114  : value(static_cast<PhQ::Vector<NumericType>>(other.Value())) {}
115 
116  /// \brief Move constructor. Constructs a dimensionless vector physical quantity by moving another
117  /// one.
118  constexpr DimensionlessVector(DimensionlessVector<NumericType>&& other) noexcept = default;
119 
120  /// \brief Copy assignment operator. Assigns this dimensionless vector physical quantity by
121  /// copying another one.
123  const DimensionlessVector<NumericType>& other) = default;
124 
125  /// \brief Copy assignment operator. Assigns this dimensionless vector physical quantity by
126  /// copying another one.
127  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
128  /// automatically.
129  template <typename OtherNumericType>
132  value = static_cast<PhQ::Vector<NumericType>>(other.Value());
133  return *this;
134  }
135 
136  /// \brief Move assignment operator. Assigns this dimensionless vector physical quantity by moving
137  /// another one.
139  DimensionlessVector<NumericType>&& other) noexcept = default;
140 
141  /// \brief Value of this physical quantity.
143 };
144 
145 } // namespace PhQ
146 
147 #endif // PHQ_DIMENSIONLESS_VECTOR_HPP
Abstract base class that represents any dimensionless vector physical quantity. Such a physical quant...
constexpr DimensionlessVector(const PhQ::Vector< NumericType > &value)
Constructor. Constructs a dimensionless vector physical quantity with a given value.
~DimensionlessVector() noexcept=default
Destructor. Destroys this dimensionless vector physical quantity.
constexpr DimensionlessVector(DimensionlessVector< NumericType > &&other) noexcept=default
Move constructor. Constructs a dimensionless vector physical quantity by moving another one.
constexpr DimensionlessVector< NumericType > & operator=(const DimensionlessVector< NumericType > &other)=default
Copy assignment operator. Assigns this dimensionless vector physical quantity by copying another one.
std::string Print() const
Prints this physical quantity as a string.
constexpr DimensionlessVector(const std::array< NumericType, 3 > &x_y_z)
Constructor. Constructs a dimensionless vector physical quantity from a given array representing its ...
std::string JSON() const
Serializes this physical quantity as a JSON message.
PhQ::Vector< NumericType > value
Value of this physical quantity.
DimensionlessVector()=default
Default constructor. Constructs a dimensionless vector physical quantity with an uninitialized value.
std::string YAML() const
Serializes this physical quantity as a YAML message.
std::string XML() const
Serializes this physical quantity as an XML message.
constexpr DimensionlessVector< NumericType > & operator=(DimensionlessVector< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this dimensionless vector physical quantity by moving another one.
static constexpr PhQ::Dimensions Dimensions()
Physical dimension set of this physical quantity. Since this physical quantity is dimensionless,...
constexpr DimensionlessVector< NumericType > & operator=(const DimensionlessVector< OtherNumericType > &other)
Copy assignment operator. Assigns this dimensionless vector physical quantity by copying another one.
constexpr const PhQ::Vector< NumericType > & Value() const noexcept
Value of this physical quantity.
constexpr DimensionlessVector(const NumericType x, const NumericType y, const NumericType z)
Constructor. Constructs a dimensionless vector physical quantity whose value has the given x,...
Physical dimension set of a unit of measure or physical quantity. Composed of the seven independent b...
Definition: Dimensions.hpp:49
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.
constexpr Dimensions Dimensionless
Dimensionless physical dimension set. This dimension set has all base dimensions of zero....
Definition: Dimensions.hpp:406