forked from weinkauf/Persistence1D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_equality_constraints.m
51 lines (41 loc) · 1.72 KB
/
build_equality_constraints.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BUILD_EQUALITY_CONSTRAINTS
%
% Creates the equality constraints matrix and vector for quadprog.
% Constrained vertices include - all persistence features (their paired minima and
% maxima), the global minimum and the domain edges. The reconstructed function
% is guarnteed to preserve their location and their value - no new minima or maxima are
% created.
%
% @param[in] mins Minima used for reconstruction
% @param[in] maxs Maxima used for reconstruction
% @param[in] globalMinIndex Index of global minimum. Will be used for
% reconstruction.
% @param[in] data Original data vector
% @param[out] A A sparse n x m matrix, where m = length(data) and m = 2 x number of
% persistence pairs + 1 + # unconstrained domain edges. Non zero entries
% are only for constrained vertices.
% @param[out] Aeq m x 1 vector which contains the data values at the constrained points.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [A Aeq] = build_equality_constraints(mins, maxs, globalMinIndex, data)
idx = [mins; maxs; globalMinIndex];
idx = sort(idx);
rows = size(idx, 1);
cols = length(data);
add_left_edge = 0;
add_right_edge = 0;
%if the left edge is constrained
if (sum(idx==1) ~= 1)
add_left_edge = 1;
idx = [1 ; idx];
end
if (sum(idx==cols) ~= 1)
add_right_edge = 1;
idx = [idx ; length(data)];
end
irows = 1:length(idx);
%general sparse matrix syntax - sparse(i,j,s,m,n)
A = sparse(double(irows), double(idx), double(ones(length(idx),1)), (rows + add_right_edge + add_left_edge), (cols));
Aeq(irows) = data(idx);
Aeq = Aeq';
end