Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define algebra and blade types #1

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ build --show_timestamps
build --announce_rc
build --color=yes
build --terminal_columns=120
build --remote_download_minimal
build --curses=no

test --test_output=errors
Expand Down
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 S, std::size_t N>
struct algebra
{
using scalar_type = S;

template <std::size_t... Is>
struct blade
{
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 scalar_type = typename algebra::scalar_type;

static constexpr auto grade = sizeof...(Is);

scalar_type coefficient{};

blade() = default;

template <std::size_t M = grade, std::enable_if_t<M == 0, bool> = true>
constexpr blade(scalar_type a) : coefficient{a}
{}
template <std::size_t M = grade, std::enable_if_t<M != 0, bool> = true>
constexpr explicit blade(scalar_type a) : coefficient{a}
{}

[[nodiscard]]
friend constexpr auto
operator==(blade x, blade y) noexcept -> bool
{
return x.coefficient == y.coefficient;
}
[[nodiscard]]
friend constexpr auto
operator!=(blade x, blade y) noexcept -> bool
{
return x.coefficient != y.coefficient;
}
[[nodiscard]]
friend constexpr auto
operator<(blade x, blade y) noexcept -> bool
{
return x.coefficient < y.coefficient;
}
[[nodiscard]]
friend constexpr auto
operator>(blade x, blade y) noexcept -> bool
{
return x.coefficient > y.coefficient;
}
[[nodiscard]]
friend constexpr auto
operator<=(blade x, blade y) noexcept -> bool
{
return x.coefficient <= y.coefficient;
}
[[nodiscard]]
friend constexpr auto
operator>=(blade x, blade y) noexcept -> bool
{
return x.coefficient >= y.coefficient;
}

[[nodiscard]]
friend constexpr auto
operator-(blade x) -> blade
{
return blade{-x.coefficient};
}

[[nodiscard]]
friend constexpr auto
operator*(scalar_type a, blade x) -> blade
{
return blade{a * x.coefficient};
}
[[nodiscard]]
friend constexpr auto
operator*(blade x, scalar_type a) -> blade
{
return a * x;
}
};

template <std::size_t... Is>
static constexpr auto e = blade<Is...>{scalar_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
55 changes: 55 additions & 0 deletions test/algebra_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#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 unit_blades = 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-blade implicitly constructible"_ctest = [] {
return expect(
eq(e<>, e<>) and //
eq(0, 0 * e<>) and //
eq(1, 1 * e<>) and //
eq(-1, -e<>));
};

"grade 1+ blade explicitly constructible"_ctest * param_ref<unit_blades> = //
[](auto ei) {
return expect(
ei.grade == 0 or //
not std::is_convertible_v<double, decltype(ei)>);
};

"grade 0+ blades comparable"_ctest * param_ref<unit_blades> = //
[](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.

2 changes: 1 addition & 1 deletion tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sh_binary(
)

lcov_attrs = {
"instrumented_targets": ["//:skytest"],
"instrumented_targets": ["//:geometry"],
"test_targets": ["//test/..."],
"coverage_opts": [
"--combined_report=lcov",
Expand Down