akreon-convex
Typed convex modeling, conic canonicalization, and primal-dual optimization in C++26.
Loading...
Searching...
No Matches
trace.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/operator/operator_traits.hpp"
9#include "weighted_sum.hpp"
10
11namespace convex {
12
14template <typename Space>
16
18template <linalg::SquareMatrix Mat>
19 requires(!linalg::is_symmetric_matrix_space_v<Mat>)
22 using T = linalg::space_type_t<Mat>;
23 static constexpr std::size_t N = linalg::mat_rows_v<Mat>;
24 static constexpr std::size_t Dim = linalg::space_dim_v<Mat>;
25 using Vec = linalg::Vector<T, Dim>;
26
28 static constexpr auto make() {
29 Vec weights{};
30 for (std::size_t i = 0; i < N; ++i) weights(i * N + i) = T(1);
31 return weights;
32 }
33};
34
36template <linalg::SquareMatrix Mat>
39 using Space = linalg::SymmetricMatrix<Mat>;
40 using T = linalg::space_type_t<Space>;
41 static constexpr std::size_t N = linalg::mat_rows_v<Mat>;
42 static constexpr std::size_t Dim = linalg::space_dim_v<Space>;
43 using Vec = linalg::Vector<T, Dim>;
44
46 static constexpr auto make() {
47 Vec weights{};
48 for (std::size_t i = 0; i < N; ++i) weights(i * (i + 1) / 2 + i) = T(1);
49 return weights;
50 }
51};
52
59template <linalg::RnSpace Space>
60struct Trace {
62 using T = linalg::space_type_t<Space>;
63 static constexpr std::size_t N = linalg::space_dim_v<Space>;
64 using Vec = linalg::Vector<T, N>;
65
66 static constexpr auto weights() { return TraceWeights<Space>::make(); }
67
75 template <AtomLike Var>
76 constexpr auto operator()(const Var& X) const {
77 static_assert(is_var_type_v<Var>, "Trace must operate on a variable atom");
78 static_assert(
79 std::same_as<dom_space_t<meta_codom_t<atom_meta_t<Var>>>, Space>,
80 "Trace variable codomain must match trace space");
81
82 return WeightedSum<Space>()(weights(), X);
83 }
84};
85
93template <AtomLike Var>
94constexpr auto make_trace(const Var& X) {
96 return Trace<Space>()(X);
97}
98
109template <std::size_t Offset, linalg::RnSpace TraceSpace, AtomLike Var>
110constexpr auto make_trace_block(const Var& X) {
111 static_assert(is_var_type_v<Var>, "Trace block must operate on a variable");
113 using T = linalg::space_type_t<VarSpace>;
114 static constexpr std::size_t VarDim = linalg::space_dim_v<VarSpace>;
115 static constexpr std::size_t TraceDim = linalg::space_dim_v<TraceSpace>;
116 static_assert(Offset + TraceDim <= VarDim,
117 "Trace block exceeds variable dimension");
118 static_assert(std::same_as<T, linalg::space_type_t<TraceSpace>>,
119 "Trace block scalar type must match variable scalar type");
120
121 linalg::Vector<T, VarDim> weights{};
123 for (std::size_t i = 0; i < TraceDim; ++i)
124 weights(Offset + i) = trace_weights(i);
125
126 return WeightedSum<VarSpace>()(weights, X);
127}
128
129} // namespace convex
typename dom_traits< linalg::remove_cvref_t< Dom > >::Space dom_space_t
constexpr std::size_t dom_size_v
constexpr auto make_trace_block(const Var &X)
make trace objective over a matrix block inside a merged variable.
Definition trace.hpp:110
constexpr auto make_trace(const Var &X)
make trace objective with domain deduced from variable atom.
Definition trace.hpp:94
static constexpr auto make()
make helper to form weights for trace objective
Definition trace.hpp:28
linalg::Vector< T, Dim > Vec
Definition trace.hpp:25
linalg::space_type_t< Mat > T
alias and type extractions
Definition trace.hpp:22
static constexpr auto make()
make helper to form weights for trace objective
Definition trace.hpp:46
linalg::SymmetricMatrix< Mat > Space
alias and type extractions
Definition trace.hpp:39
Forward decl of trace weights.
Definition trace.hpp:15
Trace objective.
Definition trace.hpp:60
linalg::space_type_t< Space > T
alias and type extractions
Definition trace.hpp:62
static constexpr std::size_t N
Definition trace.hpp:63
linalg::Vector< T, N > Vec
Definition trace.hpp:64
static constexpr auto weights()
Definition trace.hpp:66
constexpr auto operator()(const Var &X) const
operator overload
Definition trace.hpp:76