-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
define algebra type and basis vectors
Change-Id: I86dc9c296ef465eebc42b91a1c9a437086b9a5cd
- Loading branch information
Showing
7 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
#pragma once | ||
|
||
namespace geometry { | ||
|
||
constexpr auto dummy_function() | ||
{ | ||
return 0; | ||
} | ||
|
||
} // namespace geometry | ||
#include "geometry/src/algebra.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <type_traits> | ||
|
||
namespace geometry { | ||
|
||
template <class T, std::size_t N> | ||
struct algebra | ||
{ | ||
using value_type = T; | ||
|
||
template <std::size_t... Is> | ||
struct k_vector | ||
{ | ||
static_assert(sizeof...(Is) <= N + 1, "number of `Is` exceeds rank"); | ||
static_assert(((Is <= N) and ...), "`Is` exceeds rank"); | ||
// TODO check Is.. values are unique | ||
|
||
using algebra_type = algebra; | ||
using value_type = typename algebra::value_type; | ||
|
||
static constexpr auto grade = sizeof...(Is); | ||
|
||
value_type coefficient{}; | ||
|
||
k_vector() = default; | ||
|
||
template <std::size_t M = grade, std::enable_if_t<M == 0, bool> = true> | ||
constexpr k_vector(value_type a) : coefficient{a} | ||
{} | ||
template <std::size_t M = grade, std::enable_if_t<M != 0, bool> = true> | ||
constexpr explicit k_vector(value_type a) : coefficient{a} | ||
{} | ||
|
||
[[nodiscard]] | ||
friend constexpr auto | ||
operator==(const k_vector& x, const k_vector& y) noexcept -> bool | ||
{ | ||
return x.coefficient == y.coefficient; | ||
} | ||
[[nodiscard]] | ||
friend constexpr auto | ||
operator!=(const k_vector& x, const k_vector& y) noexcept -> bool | ||
{ | ||
return x.coefficient != y.coefficient; | ||
} | ||
[[nodiscard]] | ||
friend constexpr auto | ||
operator<(const k_vector& x, const k_vector& y) noexcept -> bool | ||
{ | ||
return x.coefficient < y.coefficient; | ||
} | ||
[[nodiscard]] | ||
friend constexpr auto | ||
operator>(const k_vector& x, const k_vector& y) noexcept -> bool | ||
{ | ||
return x.coefficient > y.coefficient; | ||
} | ||
[[nodiscard]] | ||
friend constexpr auto | ||
operator<=(const k_vector& x, const k_vector& y) noexcept -> bool | ||
{ | ||
return x.coefficient <= y.coefficient; | ||
} | ||
[[nodiscard]] | ||
friend constexpr auto | ||
operator>=(const k_vector& x, const k_vector& y) noexcept -> bool | ||
{ | ||
return x.coefficient >= y.coefficient; | ||
} | ||
|
||
[[nodiscard]] | ||
friend constexpr auto | ||
operator-(const k_vector& x) -> k_vector | ||
{ | ||
return k_vector{-x.coefficient}; | ||
} | ||
|
||
[[nodiscard]] | ||
friend constexpr auto | ||
operator*(value_type a, const k_vector& x) -> k_vector | ||
{ | ||
return k_vector{a * x.coefficient}; | ||
} | ||
[[nodiscard]] | ||
friend constexpr auto | ||
operator*(const k_vector& x, value_type a) -> k_vector | ||
{ | ||
return a * x; | ||
} | ||
}; | ||
|
||
template <std::size_t... Is> | ||
static constexpr auto e = k_vector<Is...>{value_type{1}}; | ||
}; | ||
|
||
} // namespace geometry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "geometry/geometry.hpp" | ||
#include "skytest/skytest.hpp" | ||
|
||
#include <cstddef> | ||
#include <tuple> | ||
#include <type_traits> | ||
|
||
template <std::size_t... Is> | ||
constexpr auto e = ::geometry::algebra<double, 2>::e<Is...>; | ||
|
||
constexpr auto basis_vectors = std::tuple{ | ||
::geometry::algebra<double, 2>::e<>, | ||
::geometry::algebra<double, 2>::e<0>, | ||
::geometry::algebra<double, 2>::e<1>, | ||
::geometry::algebra<double, 2>::e<2>, | ||
::geometry::algebra<double, 2>::e<0, 1>, | ||
::geometry::algebra<double, 2>::e<0, 2>, | ||
::geometry::algebra<double, 2>::e<1, 2>, | ||
::geometry::algebra<double, 2>::e<0, 1, 2>}; | ||
|
||
auto main() -> int | ||
{ | ||
using namespace ::skytest::literals; | ||
using ::skytest::eq; | ||
using ::skytest::expect; | ||
using ::skytest::param_ref; | ||
|
||
"0-vector implicitly constructible"_ctest = [] { | ||
return expect( | ||
eq(e<>, e<>) and // | ||
eq(0, 0 * e<>) and // | ||
eq(1, 1 * e<>) and // | ||
eq(-1, -e<>)); | ||
}; | ||
|
||
"degree 1+ k-vector explicitly constructible"_ctest * | ||
param_ref<basis_vectors> = // | ||
[](auto ei) { | ||
return expect( | ||
ei.grade == 0 or // | ||
not std::is_convertible_v<double, decltype(ei)>); | ||
}; | ||
|
||
"degree 0+ k-vectors comparable"_ctest * param_ref<basis_vectors> = // | ||
[](auto ei) { | ||
using namespace ::skytest; | ||
|
||
return expect( | ||
eq(ei, ei) and // | ||
ne(0 * ei, ei) and // | ||
lt(0 * ei, ei) and // | ||
le(0 * ei, ei) and // | ||
gt(ei, 0 * ei) and // | ||
ge(ei, 0 * ei)); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters