Here’s a minimal C++20–only solution that defines two concepts:
- Group – requires two binary operations
comp(a,b) and inv_comp(a,b) returning the same type.
- LieGroup – refines Group by also requiring
exp(x) and log(x) free‐functions.
#include <concepts>
#include <type_traits>
//-----------------------------------------------------------------------------
// 1) The Group concept
//-----------------------------------------------------------------------------
template<typename G>
concept Group =
requires(G a, G b)
{
// a • b
{ comp(a, b) } -> std::same_as<G>;
// a • b^{-1}
{ inv_comp(a, b) } -> std::same_as<G>;
};
//-----------------------------------------------------------------------------
// 2) The LieGroup concept (a Group with exp/log)
//-----------------------------------------------------------------------------
template<typename G>
concept LieGroup =
Group<G> &&
requires(G a)
{
{ exp(a) } -> std::same_as<G>;
{ log(a) } -> std::same_as<G>;
};
//-----------------------------------------------------------------------------
// Example: ℝ under addition is a (Lie) group, if we pretend exp/log are
// trivial (identity) for demonstration purposes.
//-----------------------------------------------------------------------------
struct RealAdd {
double v;
};
// group composition: addition
constexpr RealAdd comp(RealAdd a, RealAdd b) {
return { a.v + b.v };
}
// inverse composition: subtraction
constexpr RealAdd inv_comp(RealAdd a, RealAdd b) {
return { a.v - b.v };
}
// for demo only: exp/log as identity maps
constexpr RealAdd exp(RealAdd a) {
return a;
}
constexpr RealAdd log(RealAdd a) {
return a;
}
// static assertions
static_assert(Group<RealAdd>, "RealAdd should satisfy Group");
static_assert(LieGroup<RealAdd>,"RealAdd should satisfy LieGroup");
//-----------------------------------------------------------------------------
// Example: 2D rotations SO(2)
//-----------------------------------------------------------------------------
#include <cmath>
struct SO2 {
double theta; // angle in radians
};
// composition = add angles
constexpr SO2 comp(SO2 a, SO2 b) {
return { a.theta + b.theta };
}
// inverse composition = subtract angles
constexpr SO2 inv_comp(SO2 a, SO2 b) {
return { a.theta - b.theta };
}
// exponential map: ℝ → SO2
constexpr SO2 exp(SO2 a) {
return { a.theta };
}
// logarithm: SO2 → ℝ
constexpr SO2 log(SO2 a) {
return { a.theta };
}
static_assert(Group<SO2>, "SO2 is a group");
static_assert(LieGroup<SO2>,"SO2 is a Lie group");
int main(){
// use it:
SO2 r1{1.0}, r2{2.0};
auto r3 = comp(r1, r2); // 3.0
auto r4 = inv_comp(r3, r1); // 2.0
auto r5 = exp( SO2{0.5} ); // {0.5}
auto r6 = log(r5); // {0.5}
return 0;
}
Explanation
- We use
std::same_as<G> to insist that each operation returns exactly the same type G.
Group<G> only cares about comp and inv_comp.
LieGroup<G> adds exp/log on top of Group<G>.
Feel free to adapt the signature of exp/log if you want them to live in a different type (e.g. mapping between your group and a separate “algebra” type). 🚀