akreon-convex
Typed convex modeling, conic canonicalization, and primal-dual optimization in C++26.
Loading...
Searching...
No Matches
halfspace.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 "convex_set.hpp"
9
10namespace convex {
11
13template <linalg::RnSpace Space>
15
17template <linalg::RnSpace Space>
19
21template <linalg::RnSpace Space>
24
34template <typename Meta, typename Child, HalfspaceType Type>
35struct Halfspace : ConvexSet<Meta, Halfspace<Meta, Child, Type>, Child> {
40
42 using ConvexBase::N;
43
51 constexpr explicit Halfspace(const PointT& a_in, T b_in,
52 const Child& child_in)
53 : ConvexBase(child_in), a(a_in), b(b_in) {}
54
65 constexpr bool contains_impl(const PointT& x, T /* tol */) const {
66 switch (Type) {
68 return linalg::inner_product(a, x) >= b;
70 default:
71 return linalg::inner_product(a, x) <= b;
72 }
73 }
74
81 constexpr bool is_well_formed() const {
82 if (linalg::all_zero(a)) {
83 if constexpr (Type == HalfspaceType::LH) {
84 if (b < T(0)) return false;
85 } else {
86 if (b > T(0)) return false;
87 }
88 }
89 return true;
90 }
91
105 template <linalg::SquareMatrix Mat>
106 requires(std::same_as<T, linalg::matrix_scalar_t<Mat>> &&
107 linalg::mat_cols_v<Mat> == N)
108 constexpr auto transform_impl(const affine::AffineMap<Mat>& map) const {
109 const auto [U_inv, event] = linalg::MatrixInverse<Mat>()(map.linear_part());
110 static_cast<void>(event);
111 const auto aT_U = transpose(as_col_vector(a)) * U_inv;
113 b + (aT_U * map.translation())(0),
114 std::get<0>(this->children));
115 }
116
122 template <typename Other>
123 constexpr bool is_equal_impl(const Other& other) const {
124 return other.self().a == a && other.self().b == b;
125 }
126
137 template <AtomType AType, conic::BuilderLike Builder>
138 constexpr auto canonicalize_set(const Builder& builder) const {
139 const auto child_builder =
140 std::get<0>(this->children)
141 .template canonicalize<AtomType::SET>(builder);
142 const auto child_coord = child_builder.coord_map;
143
144 static constexpr std::size_t ChildCodom =
145 linalg::mat_cols_v<decltype(child_coord.linear_part())>;
146 static_assert(ChildCodom == N,
147 "Convex set domain does not equal the child coordinate "
148 "linear map's codomain in size");
149
150 linalg::RowVector<T, N> F;
151 F = linalg::set_block<0, 1, 0, N>(F, T(-1) * linalg::as_row_vector(a));
152 linalg::Vector<T, 1> g{b};
153 const auto map = affine::AffineMap<decltype(F)>(F, g);
155 const auto cones = std::make_tuple(rplus);
156 const auto emission =
157 conic::LocalEmission(map, cones, child_builder.eq_map);
158
159 return child_builder.template realize<AType>(emission);
160 }
161
163 PointT a; //< n-dim vector of type T
164 T b; //< bias scalar of type T
165};
166
167} // namespace convex
constexpr std::size_t dom_size_v
Atom-local canonical emission bundle.
forward decl for nonneg orthant cone
Definition rplus.hpp:47
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
constexpr auto canonicalize_set(const Builder &builder) const
canonicalize impl
constexpr Halfspace(const PointT &a_in, T b_in, const Child &child_in)
CTOR for halfspace set.
Definition halfspace.hpp:51
PointT a
member variables in the halfspace
constexpr auto transform_impl(const affine::AffineMap< Mat > &map) const
transform halfspace by affine map
constexpr bool contains_impl(const PointT &x, T) const
contains function to see if x is in the halfspace a^T x <= b
Definition halfspace.hpp:65
constexpr bool is_well_formed() const
is_well_formed checks to see if underlying set is a valid convex set
Definition halfspace.hpp:81
constexpr bool is_equal_impl(const Other &other) const
is_equal implementation