Skip to content

Commit f3f2391

Browse files
committed
fix vector
1 parent c3105fb commit f3f2391

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/core/include/mp-units/cartesian_tensor.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ import std;
4646
#endif
4747
#endif
4848

49+
#if MP_UNITS_HOSTED
50+
#ifndef MP_UNITS_STD_FMT
51+
#define MP_UNITS_STD_FMT std
52+
#endif
53+
#endif
54+
4955
namespace mp_units {
5056

5157
template<class X>
@@ -209,7 +215,7 @@ inline constexpr bool treat_as_floating_point<cartesian_tensor<T, R, C>> = treat
209215
template<class S, class T, std::size_t R, std::size_t C>
210216
inline constexpr bool is_value_preserving<S, cartesian_tensor<T, R, C>> = is_value_preserving<S, T>;
211217

212-
218+
MP_UNITS_EXPORT
213219
template<class T, class U, std::size_t R, std::size_t K, std::size_t C>
214220
[[nodiscard]] constexpr auto matmul(const cartesian_tensor<T, R, K>& A, const cartesian_tensor<U, K, C>& B)
215221
{
@@ -225,6 +231,7 @@ template<class T, class U, std::size_t R, std::size_t K, std::size_t C>
225231
return out;
226232
}
227233

234+
MP_UNITS_EXPORT
228235
template<class T, class U>
229236
[[nodiscard]] constexpr auto matvec(const cartesian_tensor<T, 3, 3>& M, const cartesian_vector<U>& x)
230237
{
@@ -239,6 +246,7 @@ template<class T, class U>
239246
return y;
240247
}
241248

249+
MP_UNITS_EXPORT
242250
template<class T, class U, std::size_t R, std::size_t C>
243251
[[nodiscard]] constexpr auto double_contraction(const cartesian_tensor<T, R, C>& A, const cartesian_tensor<U, R, C>& B)
244252
{
@@ -248,6 +256,7 @@ template<class T, class U, std::size_t R, std::size_t C>
248256
return acc;
249257
}
250258

259+
MP_UNITS_EXPORT
251260
template<class T, class U>
252261
[[nodiscard]] constexpr auto outer_numeric(const cartesian_vector<T>& a, const cartesian_vector<U>& b)
253262
{

src/core/include/mp-units/cartesian_vector.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,22 @@ import std;
4747
#endif
4848
#endif
4949

50+
#if MP_UNITS_HOSTED
51+
#ifndef MP_UNITS_STD_FMT
52+
#define MP_UNITS_STD_FMT std
53+
#endif
54+
#endif
55+
5056
namespace mp_units {
5157

5258
MP_UNITS_EXPORT template<detail::Scalar T>
5359
class cartesian_vector;
5460

61+
template<class X>
62+
struct is_exact_cartesian_vector : std::false_type {};
63+
template<detail::Scalar U>
64+
struct is_exact_cartesian_vector<cartesian_vector<U>> : std::true_type {};
65+
5566
MP_UNITS_EXPORT template<detail::Scalar T = double>
5667
class cartesian_vector {
5768
public:
@@ -128,7 +139,8 @@ class cartesian_vector {
128139
}
129140

130141
template<typename S>
131-
requires requires(T& t, const S& s) { t *= s; }
142+
requires detail::Scalar<S> && (!is_exact_cartesian_vector<std::remove_cvref_t<S>>::value) &&
143+
requires(T& t, const S& s) { t *= s; }
132144
constexpr cartesian_vector& operator*=(const S& scalar)
133145
{
134146
_coordinates_[0] *= scalar;
@@ -138,7 +150,8 @@ class cartesian_vector {
138150
}
139151

140152
template<typename S>
141-
requires requires(T& t, const S& s) { t /= s; }
153+
requires detail::Scalar<S> && (!is_exact_cartesian_vector<std::remove_cvref_t<S>>::value) &&
154+
requires(T& t, const S& s) { t /= s; }
142155
constexpr cartesian_vector& operator/=(const S& scalar)
143156
{
144157
_coordinates_[0] /= scalar;
@@ -196,8 +209,10 @@ class cartesian_vector {
196209
static_cast<CT>(lhs._coordinates_[2] % rhs[2])};
197210
}
198211

212+
199213
template<typename S>
200-
requires requires(const T& t, const S& s) { t * s; }
214+
requires detail::Scalar<S> && (!is_exact_cartesian_vector<std::remove_cvref_t<S>>::value) &&
215+
requires(const T& t, const S& s) { t * s; }
201216
[[nodiscard]] friend constexpr auto operator*(const cartesian_vector<T>& vector, const S& scalar)
202217
{
203218
using CT = std::common_type_t<T, S>;
@@ -207,14 +222,16 @@ class cartesian_vector {
207222
}
208223

209224
template<typename S>
210-
requires requires(const S& s, const T& t) { s * t; }
225+
requires detail::Scalar<S> && (!is_exact_cartesian_vector<std::remove_cvref_t<S>>::value) &&
226+
requires(const S& s, const T& t) { s * t; }
211227
[[nodiscard]] friend constexpr auto operator*(const S& scalar, const cartesian_vector<T>& vector)
212228
{
213229
return vector * scalar;
214230
}
215231

216232
template<typename S>
217-
requires requires(const T& t, const S& s) { t / s; }
233+
requires detail::Scalar<S> && (!is_exact_cartesian_vector<std::remove_cvref_t<S>>::value) &&
234+
requires(const T& t, const S& s) { t / s; }
218235
[[nodiscard]] friend constexpr auto operator/(const cartesian_vector<T>& vector, const S& scalar)
219236
{
220237
using CT = std::common_type_t<T, S>;
@@ -261,11 +278,6 @@ template<typename Arg, typename... Args>
261278
requires(sizeof...(Args) <= 2) && requires { typename std::common_type_t<Arg, Args...>; }
262279
cartesian_vector(Arg, Args...) -> cartesian_vector<std::common_type_t<Arg, Args...>>;
263280

264-
// NOTE: We do NOT specialize treat_as_floating_point for cartesian_vector<T>.
265-
// Doing so would make the vector attempt to model a "scalar" in mp-units'
266-
// concept lattice and triggers recursive constraints.
267-
268-
// Keep value-preserving semantics delegated to element type.
269281
template<class S, class T>
270282
inline constexpr bool is_value_preserving<S, cartesian_vector<T>> = is_value_preserving<S, T>;
271283

0 commit comments

Comments
 (0)