Skip to content

Commit

Permalink
define algebra type and basis vectors
Browse files Browse the repository at this point in the history
Change-Id: I86dc9c296ef465eebc42b91a1c9a437086b9a5cd
  • Loading branch information
oliverlee committed Jan 6, 2024
1 parent 68d4073 commit cc42fae
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 22 deletions.
4 changes: 3 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ filegroup(

cc_library(
name = "geometry",
srcs = [],
srcs = [
"geometry/src/algebra.hpp",
],
hdrs = ["geometry/geometry.hpp"],
visibility = ["//visibility:public"],
)
9 changes: 1 addition & 8 deletions geometry/geometry.hpp
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"
98 changes: 98 additions & 0 deletions geometry/src/algebra.hpp
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
4 changes: 2 additions & 2 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@rules_cc//cc:defs.bzl", "cc_test")

cc_test(
name = "dummy_test",
srcs = ["dummy_test.cpp"],
name = "algebra_test",
srcs = ["algebra_test.cpp"],
deps = [
"//:geometry",
"@skytest",
Expand Down
56 changes: 56 additions & 0 deletions test/algebra_test.cpp
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));
};
}
11 changes: 0 additions & 11 deletions test/dummy_test.cpp

This file was deleted.

0 comments on commit cc42fae

Please sign in to comment.