akreon-convex
Typed convex modeling, conic canonicalization, and primal-dual optimization in C++26.
Loading...
Searching...
No Matches
convex_traits.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 "akreon/linalg/affine/affine_traits.hpp"
9#include "cone_traits.hpp"
10#include "akreon/linalg/norms/norm_traits.hpp"
11
12namespace convex {
13
15template <typename T>
16using LiftedVarMat = linalg::RowVector<T, 1>;
17
19template <typename T>
20using LiftedVarBias = linalg::Vector<T, 1>;
21
23enum struct AtomType : uint8_t { VAR = 0, INDEX, FN, SET, ADD, CAP };
24
26enum struct VarType : uint8_t { FREE = 0, NONNEG, NONPOS, POS, NEG, PSD };
27
29enum struct CurvatureType : uint8_t { AFFINE = 0, CONVEX, CONCAVE };
30
33
35enum struct InequalityType : uint8_t { EQ = 0, LEQ, GEQ };
36
39
41template <linalg::RnSpace Space, VarType Sign>
42struct ConvexDomain {};
43
45template <typename Dom>
46struct dom_traits {};
47
48template <linalg::RnSpace SpaceIn, VarType Sign>
50 using Space = SpaceIn;
51 using T = linalg::space_type_t<Space>;
52 using Point = linalg::space_point_t<Space>;
53 static constexpr std::size_t N = linalg::space_dim_v<Space>;
54 static constexpr VarType sign = Sign;
55};
56
57template <typename Dom>
59
60template <typename Dom>
62
63template <typename Dom>
65
66template <typename Dom>
67inline constexpr std::size_t dom_size_v =
69
70template <typename Dom>
71inline constexpr VarType dom_vartype_v =
73
75template <typename Dom, typename Codom, CurvatureType Curv>
76 requires(std::same_as<dom_type_t<Dom>, dom_type_t<Codom>>)
78
80template <typename Meta>
81struct meta_traits {};
82
83template <typename Dom, typename Codom, CurvatureType Curv>
84struct meta_traits<ConvexAtomMeta<Dom, Codom, Curv>> {
85 using Domain = Dom;
86 using Codomain = Codom;
87 static constexpr CurvatureType curvature = Curv;
88};
89
90template <typename Meta>
92
93template <typename Meta>
96
97template <typename Meta>
98inline constexpr CurvatureType meta_curv_v =
100
101template <typename Meta, typename Derived, typename... Cs>
102struct Atom;
103
104template <typename Meta>
105struct Variable;
106
107template <typename Meta, typename Child>
108 requires(dom_size_v<meta_dom_t<Meta>> == 1)
109struct ScalarView;
110
112template <typename Meta, typename Child>
113struct Max;
114
116template <typename Meta, typename Child, linalg::NormFunction NormFunc>
117 requires(dom_size_v<meta_dom_t<Meta>> == linalg::norm_dim_v<NormFunc>)
118struct Norm;
119
121template <typename Meta, typename Child>
122struct GeoMean;
123
125template <typename Meta, typename Child>
126 requires(dom_size_v<meta_dom_t<Meta>> == 1)
127struct Exp;
128
130template <typename Meta, typename Child>
131 requires(dom_size_v<meta_dom_t<Meta>> == 1)
132struct Log;
133
135template <typename Meta, typename Child>
136 requires(dom_size_v<meta_dom_t<Meta>> == 1)
137struct Square;
138
140template <typename Meta, typename Child>
141 requires(dom_size_v<meta_dom_t<Meta>> == 1)
142struct Abs;
143
145template <typename Meta, typename Child>
146 requires(dom_size_v<meta_dom_t<Meta>> == 1)
147struct InvPos;
148
150template <typename Meta, typename Child>
151struct LogSumExp;
152
154template <typename Meta, typename Child>
155 requires(dom_size_v<meta_dom_t<Meta>> == 1)
156struct Sqrt;
157
159template <typename Meta, typename Child>
160 requires(dom_size_v<meta_dom_t<Meta>> == 1)
161struct Softplus;
162
164template <typename Meta, typename Child>
165 requires(dom_size_v<meta_dom_t<Meta>> == 2)
166struct HarmonicMean;
167
169template <typename Meta, typename Child>
170struct KLDivergence;
171
173template <typename Meta, typename Child>
174struct Huber;
175
177template <typename Meta, typename... Cs>
178struct Sum;
179
181template <typename Meta, typename Derived, typename Child>
182struct ConvexSet;
183
185template <typename Meta, typename Child>
186struct AffineSubspace;
187
189template <typename Meta, typename Child>
190struct Ellipsoid;
191
193enum struct HalfspaceType : uint8_t { LH = 0, UH };
194
196template <typename Meta, typename Child, HalfspaceType Type>
197struct Halfspace;
198
200template <typename Meta, typename Child>
201struct Hyperplane;
202
204template <typename Meta, typename Child, linalg::SquareMatrix MatIn>
205struct LMI;
206
208enum struct NormBallType : uint8_t { L2 = 0, L1, Linf };
209
211template <typename Meta, typename Child, NormBallType Type>
212struct NormBall;
213
215template <typename Meta, typename Child>
216struct Polyhedron;
217
219template <typename Meta, typename... Cs>
220struct Intersection;
221
223template <conic::BuilderLike Builder, StageType Stage>
224struct ConvexProblem;
225
227template <typename Prob>
229
231template <conic::BuilderLike Builder, StageType Stage>
232struct prob_traits<ConvexProblem<Builder, Stage>> {
233 using BuilderT = Builder;
234 static constexpr StageType StageV = Stage;
235};
236
238template <typename Prob>
241
243template <typename Prob>
244inline constexpr bool is_problem_set_v =
246
248template <typename Problem>
249concept ProblemLike = requires(const Problem& prob) {
250 { prob.linear_objective() };
251 { prob.constraint_map() };
252 { prob.constraint_cones() };
253 { prob.equality_map() };
254};
255
257template <typename Atom>
258struct atom_traits {};
259
260template <typename Meta, typename Child>
261struct atom_traits<Atom<Meta, Child>> {
262 using ConvexMeta = Meta;
263};
264
265template <typename Meta>
266struct atom_traits<Variable<Meta>> : std::false_type {
267 using ConvexMeta = Meta;
268 static constexpr AtomType Type = AtomType::VAR;
269};
270
271template <typename Meta, typename Child>
273 using ConvexMeta = Meta;
274 static constexpr AtomType Type = AtomType::INDEX;
275};
276
277template <typename Meta, typename Child>
278struct atom_traits<Abs<Meta, Child>> {
279 using ConvexMeta = Meta;
280 static constexpr AtomType Type = AtomType::FN;
281};
282
283template <typename Meta, typename Child>
284struct atom_traits<Exp<Meta, Child>> {
285 using ConvexMeta = Meta;
286 static constexpr AtomType Type = AtomType::FN;
287};
288
289template <typename Meta, typename Child>
290struct atom_traits<GeoMean<Meta, Child>> {
291 using ConvexMeta = Meta;
292 static constexpr AtomType Type = AtomType::FN;
293};
294
295template <typename Meta, typename Child>
296struct atom_traits<InvPos<Meta, Child>> {
297 using ConvexMeta = Meta;
298 static constexpr AtomType Type = AtomType::FN;
299};
300
301template <typename Meta, typename Child>
302struct atom_traits<Log<Meta, Child>> {
303 using ConvexMeta = Meta;
304 static constexpr AtomType Type = AtomType::FN;
305};
306
307template <typename Meta, typename Child>
309 using ConvexMeta = Meta;
310 static constexpr AtomType Type = AtomType::FN;
311};
312
313template <typename Meta, typename Child>
314struct atom_traits<Max<Meta, Child>> {
315 using ConvexMeta = Meta;
316 static constexpr AtomType Type = AtomType::FN;
317};
318
319template <typename Meta, typename Child>
320struct atom_traits<Square<Meta, Child>> {
321 using ConvexMeta = Meta;
322 static constexpr AtomType Type = AtomType::FN;
323};
324
325template <typename Meta, typename Child, linalg::NormFunction NormFunc>
327 using ConvexMeta = Meta;
328 static constexpr AtomType Type = AtomType::FN;
329};
330
331template <typename Meta, typename Child>
332struct atom_traits<Sqrt<Meta, Child>> {
333 using ConvexMeta = Meta;
334 static constexpr AtomType Type = AtomType::FN;
335};
336
337template <typename Meta, typename Child>
338struct atom_traits<Softplus<Meta, Child>> {
339 using ConvexMeta = Meta;
340 static constexpr AtomType Type = AtomType::FN;
341};
342
343template <typename Meta, typename Child>
345 using ConvexMeta = Meta;
346 static constexpr AtomType Type = AtomType::FN;
347};
348
349template <typename Meta, typename Child>
351 using ConvexMeta = Meta;
352 static constexpr AtomType Type = AtomType::FN;
353};
354
355template <typename Meta, typename Child>
356struct atom_traits<Huber<Meta, Child>> {
357 using ConvexMeta = Meta;
358 static constexpr AtomType Type = AtomType::FN;
359};
360
361template <typename Meta, typename Derived, typename Child>
363 using ConvexMeta = Meta;
364 static constexpr AtomType Type = AtomType::SET;
365};
366
367template <typename Meta, typename Child>
369 using ConvexMeta = Meta;
370 static constexpr AtomType Type = AtomType::SET;
371};
372
373template <typename Meta, typename Child>
375 using ConvexMeta = Meta;
376 static constexpr AtomType Type = AtomType::SET;
377};
378
379template <typename Meta, typename Child, HalfspaceType HType>
381 using ConvexMeta = Meta;
382 static constexpr AtomType Type = AtomType::SET;
383};
384
385template <typename Meta, typename Child>
387 using ConvexMeta = Meta;
388 static constexpr AtomType Type = AtomType::SET;
389};
390
391template <typename Meta, typename Child, typename Mat>
392struct atom_traits<LMI<Meta, Child, Mat>> {
393 using ConvexMeta = Meta;
394 static constexpr AtomType Type = AtomType::SET;
395};
396
397template <typename Meta, typename Child, NormBallType BType>
399 using ConvexMeta = Meta;
400 static constexpr AtomType Type = AtomType::SET;
401};
402
403template <typename Meta, typename Child>
405 using ConvexMeta = Meta;
406 static constexpr AtomType Type = AtomType::SET;
407};
408
410template <linalg::RnSpace Dom>
412
414template <linalg::RnSpace Codom>
415 requires(linalg::space_dim_v<Codom> == 1)
417
419template <linalg::RnSpace Dom, linalg::RnSpace Codom>
420using SumMeta =
422
424template <linalg::RnSpace Dom, linalg::RnSpace Codom, typename... Cs>
425struct atom_traits<Sum<SumMeta<Dom, Codom>, Cs...>> {
427 static constexpr AtomType Type = AtomType::ADD;
428};
429
431template <linalg::RnSpace Space>
433
435template <linalg::RnSpace Space>
437
439template <linalg::RnSpace Space>
443
445template <linalg::RnSpace Space, typename... Cs>
450
452template <typename Atom>
453concept AtomLike = requires(const Atom& atom) {
454 { atom.validate_domain() } -> std::convertible_to<void>;
455 { atom.children };
456};
457
459template <InequalityType IType, AtomLike LHS, typename RHS>
460struct Inequality;
461
463template <typename T>
464using atom_decay_t = linalg::remove_cvref_t<T>;
465
467template <AtomLike Atom>
468using atom_meta_t = typename atom_traits<atom_decay_t<Atom>>::ConvexMeta;
469
471template <AtomLike Atom>
473
475template <AtomLike Atom>
476inline constexpr bool is_fn_type_v = atom_type_v<Atom> == AtomType::FN;
477
478template <AtomLike Atom>
480
481template <AtomLike Atom>
483
484template <AtomLike Atom>
486
487template <AtomLike Atom>
489
490template <AtomLike Atom>
492
493} // namespace convex
Concept for any convex atom node in the expression tree.
ProblemLike Concept.
VarType
Domain/variable annotation attached to domains/codomains.
typename dom_traits< linalg::remove_cvref_t< Dom > >::Space dom_space_t
constexpr AtomType atom_type_v
Compile-time atom category.
constexpr std::size_t dom_size_v
constexpr bool is_index_type_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.
HalfspaceType
simple enum to describe type of halfspace
NormBallType
simple enum to describe ball type
constexpr CurvatureType meta_curv_v
constexpr bool is_set_type_v
typename dom_traits< linalg::remove_cvref_t< Dom > >::T dom_type_t
AtomType
Atom category used by canonicalization and DCP checks.
linalg::Vector< T, 1 > LiftedVarBias
alias for lifted var map
constexpr bool is_fn_type_v
Helper predicates on atom category.
constexpr bool is_add_type_v
linalg::RowVector< T, 1 > LiftedVarMat
alias for lifted var map
typename dom_traits< linalg::remove_cvref_t< Dom > >::Point dom_point_t
constexpr bool is_cap_type_v
typename meta_traits< linalg::remove_cvref_t< Meta > >::Codomain meta_codom_t
typename atom_traits< atom_decay_t< Atom > >::ConvexMeta atom_meta_t
Extract metadata type from atom.
constexpr VarType dom_vartype_v
InequalityType
InequalityType for inequality atom.
constexpr bool is_var_type_v
typename prob_traits< linalg::remove_cvref_t< Prob > >::BuilderT prob_builder_t
builder type from prob
StageType
Problem construction lifecycle stage.
typename meta_traits< linalg::remove_cvref_t< Meta > >::Domain meta_dom_t
constexpr bool is_problem_set_v
stage type from prob
forward declaration - abs fn
Definition abs.hpp:45
forward declaration of affine subspace
CRTP base for convex atoms.
Definition atom.hpp:22
Atom metadata bundling domain, codomain, and curvature.
Domain wrapper with a static sign label.
forward declaration of convex set
forward declaration of ellipsoid set
Definition ellipsoid.hpp:32
forward declaration - exp fn
Definition exp.hpp:49
forward declaration - geo mean fn
Definition geo_mean.hpp:49
forward declaration of halfspace
Definition halfspace.hpp:35
forward declaration - harmonic mean fn
forward declaration - huber
Definition huber.hpp:45
forward declaration of hyperplane
forward declaration of inequality
forward declaration of set intersection operator.
forward declaration - inv pos fn
Definition inv_pos.hpp:47
forward declaration - kl divergence
Definition kl_div.hpp:46
forward declaration of LMI type
Definition lmi.hpp:33
forward declaration - log sum exp fn
forward declaration - log fn
Definition log.hpp:49
forward declaration - max fn
Definition max.hpp:45
forward declaration of norm ball set
Definition norm_ball.hpp:35
forward declaration - norm fn
Definition norm.hpp:42
forward declaration of polyhedron set
forward declaration - softplus fn
Definition softplus.hpp:50
forward declaration - log sum exp fn
Definition sqrt.hpp:43
forward declaration - square fn
Definition square.hpp:47
forward declaration - sum op
Definition sum.hpp:116
variable atom
Definition variable.hpp:163
Base atom traits.
Base domain traits.
Base metadata traits.
problem type traits