8#include "akreon/linalg/assert.hpp"
15template <linalg::RnSpace Space>
19template <linalg::RnSpace Space>
20 requires(linalg::space_dim_v<Space> == 1)
24template <linalg::RnSpace Space>
25 requires(linalg::space_dim_v<Space> == 1)
29template <linalg::RnSpace Dom, linalg::RnSpace Codom>
34template <linalg::RnSpace Dom, linalg::RnSpace Codom>
45template <
typename Meta,
typename Child>
65 for (
const auto& x :
p) {
67 "KLDivergence expects each fixed p entry to satisfy x > 0");
83 for (
const auto& x :
p) {
85 "KLDivergence expects each fixed p entry to satisfy x > 0");
119 for (std::size_t
i = 0;
i <
N; ++
i) {
121 res +=
p(
i) * linalg::constexpr_ln(
p(
i) /
q(
i)).value();
125 else return T(-1) * res;
134 constexpr auto mul()
const {
135 if constexpr (Scalar <
T(0)) {
136 const auto alpha =
T(-1) * Scalar;
150 affine::AffineMap<LiftedVarMat<T>>
new_map(
163 constexpr auto add(
T beta)
const {
201 template <AtomType Type, conic::BuilderLike Builder>
209 static constexpr std::size_t Dom = linalg::mat_cols_v<AffineMatT>;
210 static constexpr std::size_t Codom = linalg::mat_rows_v<AffineMatT>;
211 static_assert(Codom ==
N,
212 "Input affine map from cone block has invalid dom which is a "
213 "mismatch between domain in the convex function");
214 static_assert(Dom ==
N,
215 "Expecting child affine map to have square linear part");
216 linalg::Matrix<T, 3 * N + 1, 3 * N + 1>
F;
217 linalg::Vector<T, 3 * N + 1>
g;
218 for (std::size_t
i = 0;
i <
N; ++
i)
221 const auto I = linalg::make_identity<T, N>();
223 F = linalg::set_block<0, N, 2 * N, 3 * N>(
F,
T(-1) *
I);
224 else F = linalg::set_block<0, N, 2 * N, 3 * N>(
F,
I);
226 F = linalg::set_block<N, 2 * N, 0, N>(
F,
I);
227 F = linalg::set_block<2 * N, 3 * N, N, 2 * N>(
F,
child_coord.linear_part());
228 for (std::size_t
i = 0;
i <
N; ++
i)
F(3 *
N, 2 *
N +
i) =
T(-1);
229 F(3 *
N, 3 *
N) =
T(1);
247template <
typename Po
int,
typename Child>
251 linalg::Matrix<dom_type_t<meta_codom_t<atom_meta_t<Child>>>, 1, 1>>,
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
CRTP base for convex atoms.
ChildrenT children
Tuple of normalized child atoms.
dom_point_t< Domain > PointT
Point type accepted by the atom domain.
dom_type_t< Domain > T
Scalar field for the atom domain.
static constexpr std::size_t N
Domain dimension.
Domain wrapper with a static sign label.
forward declaration - kl divergence
constexpr T eval(const PointT &q) const
eval impl
constexpr MonotonicityType monotonicity() const
mono
constexpr KLDivergence(const PointT &p_in, const Child &child_in, const affine::AffineMap< LiftedVarMat< T > > &map_in)
CTOR.
constexpr auto mul() const
mul
static constexpr CurvatureType Tag
curvature tag
constexpr T operator()(const PointT &q) const
eval via operator overload
affine::AffineMap< LiftedVarMat< T > > lift_map
constexpr KLDivergence(const PointT &p_in, const Child &child_in)
CTOR.
constexpr auto canonicalize_impl(const Builder &builder) const
canonicalize impl
constexpr auto add(T beta) const
add