akreon-convex
Typed convex modeling, conic canonicalization, and primal-dual optimization in C++26.
Loading...
Searching...
No Matches
max.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 <algorithm>
9
12
13namespace convex {
14
16template <linalg::RnSpace Space>
18
20template <linalg::RnSpace Space>
21 requires(linalg::space_dim_v<Space> == 1)
23
25template <linalg::RnSpace Space>
26 requires(linalg::space_dim_v<Space> == 1)
28
30template <linalg::RnSpace Dom, linalg::RnSpace Codom>
31using MaxMeta =
33
35template <linalg::RnSpace Dom, linalg::RnSpace Codom>
38
44template <typename Meta, typename Child>
45struct Max : Atom<Meta, Max<Meta, Child>, Child> {
48 using ConvexBase::N;
51
54
57
63 constexpr explicit Max(const Child& child_in)
65 }
66
73 constexpr explicit Max(const Child& child_in,
74 const affine::AffineMap<LiftedVarMat<T>>& map_in)
76
82 constexpr MonotonicityType monotonicity() const {
83 if constexpr (Tag == CurvatureType::CONVEX) return MonotonicityType::NONDEC;
84 else return MonotonicityType::NONINC;
85 }
86
94 constexpr T operator()(const PointT& x) const { return eval(x); }
95
103 constexpr T eval(const PointT& x) const {
104 T mx(0);
105 std::for_each(x.begin(), x.end(), [&mx](const auto& pt) {
106 if (mx < pt) mx = pt;
107 });
108
109 const auto alpha = lift_map.linear_part()(0, 0);
110 const auto beta = lift_map.translation()(0);
111 if constexpr (Tag == CurvatureType::CONVEX) return alpha * mx + beta;
112 else return T(-1) * alpha * mx + beta;
113 }
114
120 template <T Scalar>
121 constexpr auto mul() const {
122 if constexpr (Scalar < T(0)) {
123 const auto alpha = T(-1) * Scalar;
124 affine::AffineMap<LiftedVarMat<T>> new_map(alpha * lift_map.linear_part(),
125 lift_map.translation());
126 using Dom = dom_space_t<meta_dom_t<Meta>>;
127 using Codom = dom_space_t<meta_codom_t<Meta>>;
128 if constexpr (Tag == CurvatureType::CONCAVE) {
129 return Max<MaxMeta<Dom, Codom>, Child>(std::get<0>(this->children),
130 new_map);
131 } else {
133 std::get<0>(this->children), new_map);
134 }
135 } else {
136 affine::AffineMap<LiftedVarMat<T>> new_map(
137 Scalar * lift_map.linear_part(), lift_map.translation());
138 return Max<Meta, Child>(std::get<0>(this->children), new_map);
139 }
140 }
141
149 constexpr auto add(T beta) const {
150 LiftedVarBias<T> bias{beta + lift_map.translation()(0)};
151 affine::AffineMap<LiftedVarMat<T>> new_map(lift_map.linear_part(), bias);
152 return Max<Meta, Child>(std::get<0>(this->children), new_map);
153 }
154
175 template <AtomType Type, conic::BuilderLike Builder>
176 constexpr auto canonicalize_impl(const Builder& builder) const {
177 const auto child_builder =
178 std::get<0>(this->children)
179 .template canonicalize<AtomType::FN>(builder);
180 const auto child_coord = child_builder.coord_map;
181
182 using AffineMatT = affine::affine_mat_t<decltype(child_coord)>;
183 static constexpr std::size_t Dom = linalg::mat_cols_v<AffineMatT>;
184 static constexpr std::size_t Codom = linalg::mat_rows_v<AffineMatT>;
185 static_assert(Codom == N,
186 "Input affine map from cone block has invalid dom which is a "
187 "mismatch between domain in the convex function");
188 static_assert(Dom == N,
189 "Expecting child affine map to have square linear part");
190 linalg::Matrix<T, N, Dom + 1> F;
191 F = linalg::set_block<0, N, 0, Dom>(F, T(-1) * child_coord.linear_part());
192
193 linalg::Vector<T, N> g;
194 for (std::size_t i = 0; i < N; ++i)
195 g(i) = T(-1) * child_coord.translation()(i);
196
197 if constexpr (Tag == CurvatureType::CONVEX) {
198 for (std::size_t i = 0; i < N; ++i) F(i, N) = T(1);
199 } else {
200 for (std::size_t i = 0; i < N; ++i) F(i, N) = T(-1);
201 }
202
203 const auto coord_map_up = affine::AffineMap<decltype(F)>(F, g);
205 const auto cones = std::make_tuple(rplus);
206 const auto emission =
208
210 }
211
213 affine::AffineMap<LiftedVarMat<T>> lift_map;
214 const int idx{-1};
215};
216
217template <typename Child>
218Max(const Child&) -> Max<
220 linalg::Matrix<dom_type_t<meta_codom_t<atom_meta_t<Child>>>, 1, 1>>,
222
223} // namespace convex
typename dom_traits< linalg::remove_cvref_t< Dom > >::Space dom_space_t
constexpr std::size_t dom_size_v
CurvatureType
Curvature annotation for atom codomain.
linalg::remove_cvref_t< T > atom_decay_t
normalize atom-facing types to a stable internal form.
MonotonicityType
Monotonicity annotation for function atoms.
linalg::Vector< T, 1 > LiftedVarBias
alias for lifted var map
linalg::RowVector< T, 1 > LiftedVarMat
alias for lifted var map
Atom-local canonical emission bundle.
forward decl for nonneg orthant cone
Definition rplus.hpp:47
CRTP base for convex atoms.
Definition atom.hpp:22
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 - max fn
Definition max.hpp:45
dom_space_t< meta_dom_t< Meta > > Space
alias to extract space type
Definition max.hpp:53
constexpr Max(const Child &child_in, const affine::AffineMap< LiftedVarMat< T > > &map_in)
CTOR.
Definition max.hpp:73
const int idx
Definition max.hpp:214
constexpr T eval(const PointT &x) const
eval impl
Definition max.hpp:103
constexpr auto mul() const
mul
Definition max.hpp:121
static constexpr CurvatureType Tag
curvature tag
Definition max.hpp:56
constexpr auto canonicalize_impl(const Builder &builder) const
canonicalize_impl
Definition max.hpp:176
constexpr Max(const Child &child_in)
CTOR.
Definition max.hpp:63
constexpr auto add(T beta) const
add
Definition max.hpp:149
constexpr T operator()(const PointT &x) const
eval via operator overload
Definition max.hpp:94
affine::AffineMap< LiftedVarMat< T > > lift_map
member vars
Definition max.hpp:213
constexpr MonotonicityType monotonicity() const
mono
Definition max.hpp:82