cyqlone develop
Fast, parallel and vectorized solver for linear systems with optimal control structure.
Loading...
Searching...
No Matches
breakpoint.cpp
Go to the documentation of this file.
3#include <guanaqo/trace.hpp>
4
5namespace CYQLONE_NS(cyqlone::qpalm) {
6
7PartitionedBreakpoints partition_breakpoints_default(std::span<Breakpoint> breakpoints) {
8 GUANAQO_TRACE("linesearch partition", 0);
9 using std::isfinite;
10 // Move all infinite t[i] to the back
11 const auto is_finite = [](Breakpoint b) { return isfinite(b.t); };
12 const auto infinite = partition(breakpoints, is_finite);
13 const auto finite = breakpoints.first(breakpoints.size() - infinite.size());
14 // Move all nonpositive t[i] to the front
15 const auto le_zero = [](Breakpoint b) { return b.t <= 0; };
16 const auto positive = partition(finite, le_zero);
17 const auto first_pos = finite.size() - positive.size();
18 // Return the negative and positive partitions.
19 return {
20 .neg_bp = finite.first(first_pos),
21 .pos_bp = finite.subspan(first_pos),
22 };
23}
24
25ABSums partial_sum_negative(PartitionedBreakpoints breakpoints, real_t η, real_t β) {
26 // TODO: single pass over each partition.
27 GUANAQO_TRACE("partial sum negative", 0);
28 auto &neg_bp = breakpoints.neg_bp, &pos_bp = breakpoints.pos_bp;
29 // Initialize a_j and b_j, where t_j is the first positive (see notes)
30 auto a_plus = qpalm::transform_reduce(neg_bp.begin(), neg_bp.end(), ABSum_t{}, std::plus{},
31 [](Breakpoint p) { return p.δ > 0 ? p.δ * p.δ : 0; });
32 auto a_minus = qpalm::transform_reduce(pos_bp.begin(), pos_bp.end(), ABSum_t{}, std::plus{},
33 [](Breakpoint p) { return p.δ < 0 ? p.δ * p.δ : 0; });
34 auto a = a_plus + a_minus + η;
35 auto b_plus = qpalm::transform_reduce(neg_bp.begin(), neg_bp.end(), ABSum_t{}, std::plus{},
36 [](Breakpoint p) { return p.δ > 0 ? p.δ * p.α() : 0; });
37 auto b_minus = qpalm::transform_reduce(pos_bp.begin(), pos_bp.end(), ABSum_t{}, std::plus{},
38 [](Breakpoint p) { return p.δ < 0 ? p.δ * p.α() : 0; });
39 auto b = b_plus + b_minus - β;
40 return {.a = a, .b = b};
41}
42
43} // namespace CYQLONE_NS(cyqlone::qpalm)
#define CYQLONE_NS(ns)
Definition config.hpp:10
#define GUANAQO_TRACE(name, instance,...)
static decltype(auto) partition(R &&range, F key)
T transform_reduce(I first, I last, T init, BinOp binary_op, UnOp unary_op)
ABSums partial_sum_negative(PartitionedBreakpoints breakpoints, real_t η=0, real_t β=0)
PartitionedBreakpoints partition_breakpoints_default(std::span< Breakpoint > breakpoints)
Moves any non-finite elements in t to the end of the range, and all negative elements to the front.
Definition breakpoint.cpp:7