akreon-convex
Typed convex modeling, conic canonicalization, and primal-dual optimization in C++26.
Loading...
Searching...
No Matches
polyhedron.hpp
Go to the documentation of this file.
1/*
2 * Copyright © 2025-2026 Akreon LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#pragma once
7
8#include "halfspace.hpp"
9#include "hyperplane.hpp"
10
11namespace convex {
12
14template <linalg::RnSpace Space>
16
18template <linalg::RnSpace Space>
20
22template <linalg::RnSpace Dom, linalg::RnSpace Codom>
26
34template <typename Meta, typename Child>
35struct Polyhedron : ConvexSet<Meta, Polyhedron<Meta, Child>, Child> {
38 using ConvexBase::M;
39 using ConvexBase::N;
42
44 using Mat = linalg::Matrix<T, M, N>;
45 using Bias = linalg::Vector<T, M>;
46 using Space = linalg::Matrix<T, 1, N>;
47
56 constexpr explicit Polyhedron(const Mat& A_in, const Bias& b_in,
57 const Child& child_in)
58 : ConvexBase(child_in), A(A_in), b(b_in) {}
59
65 constexpr bool is_well_formed() const {
66 for (std::size_t i = 0; i < M; ++i)
68 A.row(i), b(i), std::get<0>(this->children))
70 return false;
71 return true;
72 }
73
81 constexpr bool contains_impl(const PointT& x, T tol) const {
82 for (std::size_t i = 0; i < M; ++i) {
83 const auto half =
85 A.row(i), b(i), std::get<0>(this->children));
86 if (!half.contains(x, tol)) return false;
87 }
88 return true;
89 }
90
106 template <linalg::SquareMatrix Mat>
107 requires(std::same_as<T, linalg::matrix_scalar_t<Mat>> &&
108 linalg::mat_cols_v<Mat> == N)
109 constexpr auto transform_impl(const affine::AffineMap<Mat>& map) const {
110 const auto& U = map.linear_part();
111 const auto& trans = map.translation();
112
113 const auto [U_inv, event] = linalg::MatrixInverse<Mat>()(U);
114 static_cast<void>(event);
115 const auto A_new = A * U_inv;
116 const auto b_new = b + A_new * trans;
117
118 return Polyhedron<Meta, Child>(A_new, b_new, std::get<0>(this->children));
119 }
120
128 template <typename Other>
129 constexpr bool is_equal_impl(const Other& other) const {
130 return other.self().A == A && other.self().b == b;
131 }
132
143 template <AtomType Type, conic::BuilderLike Builder>
144 constexpr auto canonicalize_set(const Builder& builder) const {
145 const auto child_builder =
146 std::get<0>(this->children)
147 .template canonicalize<AtomType::SET>(builder);
148 const auto child_coord = child_builder.coord_map;
149
150 static constexpr std::size_t ChildCodom =
151 linalg::mat_cols_v<decltype(child_coord.linear_part())>;
152 static_assert(ChildCodom == N,
153 "Convex set domain does not equal the child coordinate "
154 "linear map's codomain in size");
155
156 const auto map = affine::AffineMap<decltype(A)>(T(-1) * A, b);
158 const auto cones = std::make_tuple(rplus);
159 const auto emission =
160 conic::LocalEmission(map, cones, child_builder.eq_map);
161
162 return child_builder.template realize<Type>(emission);
163 }
164
166 Mat A; //< set of finite number of halfspaces
167 Bias b; //< codomain vector describing halfspace inequalities
168};
169
170template <typename Mat, typename Bias, typename Child>
171Polyhedron(const Mat&, const Bias&, const Child&) -> Polyhedron<
173 linalg::Matrix<dom_type_t<meta_codom_t<atom_meta_t<Child>>>,
174 1, linalg::mat_rows_v<Mat>>>,
176
177} // namespace convex
constexpr std::size_t dom_size_v
linalg::remove_cvref_t< T > atom_decay_t
normalize atom-facing types to a stable internal form.
Atom-local canonical emission bundle.
forward decl for nonneg orthant cone
Definition rplus.hpp:47
static constexpr std::size_t M
Codomain dimension.
Definition atom.hpp:37
ChildrenT children
Tuple of normalized child atoms.
Definition atom.hpp:240
dom_point_t< Domain > PointT
Point type accepted by the atom domain.
Definition atom.hpp:32
dom_type_t< Domain > T
Scalar field for the atom domain.
Definition atom.hpp:30
static constexpr std::size_t N
Domain dimension.
Definition atom.hpp:35
Atom metadata bundling domain, codomain, and curvature.
Domain wrapper with a static sign label.
forward declaration of convex set
forward declaration of halfspace
Definition halfspace.hpp:35
forward declaration of polyhedron set
constexpr Polyhedron(const Mat &A_in, const Bias &b_in, const Child &child_in)
CTOR for polyhedron set.
constexpr bool is_well_formed() const
is_well_formed implementation
constexpr auto transform_impl(const affine::AffineMap< Mat > &map) const
transform polyhedron by affine map
linalg::Matrix< T, 1, N > Space
Mat A
member variables in the polyhedron
constexpr bool is_equal_impl(const Other &other) const
equality comparison
linalg::Vector< T, M > Bias
constexpr auto canonicalize_set(const Builder &builder) const
canonicalize impl
constexpr bool contains_impl(const PointT &x, T tol) const
check if candidate vector is in the set
linalg::Matrix< T, M, N > Mat
bring into scope type traits from MatA