Debian Codegraph @

dwsys/SlopeSelector.h

raw symbols SCIP
1 #ifndef _SlopeSelector_h_
2 #define _SlopeSelector_h_
3 /* SlopeSelector.h
4 *
5 * Copyright (C) 2025 David Weenink
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 The algorithm to find the kth (or median) slope in O (n log(n)) time is described in
23 Matousek (1991): Randomized optimal algorithm for slope selection.
24 Information processing letters 39: 183-187.
25 Given points p[i] = (ax[i], ay[i]), i = 1..N, with ax[i] < ax[i+1] for i = 1..N-1.
26 Find the line with the k-th largest slope of all the N(N-1)/2 lines connecting
27 p[i] = (ax[i],ay[i]) with p[j] = (ax[j],ay[j]) where i = 1..N,and j = i+1..N.
28 We consider this problem in the dual space where we have N lines line[l] where
29 line[l] is y = ax[N-l+1] * x - ay[N-l+1].
30 The ordering of the lines is such that when x = -inf, line [1] has the lowest y-value and
31 line [N] the highest. Therefore, the correspondence between line number 'l' and point number 'i' is
32 l = N-i+1, for i = 1..N.
33 The x coordinate of the intersection of line[i] and line[j] is at x = (ay[N-i+1]-ay[N-j+1])/(ax[N-i+1]-ax[N-j+1]),
34 which equals the slope of the line connecting p[i] and p[j].
35 In the dual space (x,y) we then search for the intersection with the k-th largest x-coordinate.
36 For x = -inf the y-values of line[1]..line[N] are y[1] > y[2] > .. y[N], and
37 for x = +inf the y-values of line[1]..line[N] are in reverse order y[N] > y[N-1] > .. y[1].
38 If we sort the N crossings of the lines at x = a according to line number this will be a permutation
39 of the numbers 1..N. The number of inversions in this permutation has to be equal to the number of intersections
40 to the left of x=a (an inversion occurs when i < j && perm[i] > perm[j]).
41 The number of inversions in a permutation of size N can be counted in O(N log(N)).
42 */
43
44 #include "PermutationInversionCounter.h"
45 #include "melder.h"
46 #include "NUM2.h"
47 #include "SlopeSelector_enums.h"
48 #include "ExtendedReal.h"
49
50 #include "SlopeSelector_def.h"
51
52 /*
53 Preconditions:
54 x.size == y.size && x.size > 0
55 i < j && x[i] < x[j]
56 */
57
58 autoSlopeSelector SlopeSelector_create (constVEC const& x, constVEC const& y);
59
60 autoSlopeSelector SlopeSelector_create (integer numberOfPoints);
61
62 void SlopeSelector_getSlopeAndIntercept (SlopeSelector me, double &slope, double &intercept, kSlopeSelector_method method);
63
64 /****************/
65
66 void timeSlopeSelection ();
67
68 #endif /* _SlopeSelector_h_ */