| 1 | #cxx . . $ `<file>/dwsys/SlopeSelector.h`/ifndef _SlopeSelector_h_ |
| 2 | #define _SlopeSelector_h_cxx . . $ `dwsys/SlopeSelector.h:2:9`! |
| 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"cxx . . $ `<file>/dwsys/PermutationInversionCounter.h`/ |
| 45 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 46 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 47 | #include "SlopeSelector_enums.h"cxx . . $ `<file>/dwsys/SlopeSelector_enums.h`/ |
| 48 | #include "ExtendedReal.h"cxx . . $ `<file>/dwsys/ExtendedReal.h`/ |
| 49 | |
| 50 | #include "SlopeSelector_def.h"cxx . . $ `<file>/dwsys/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 | autoSlopeSelectorcxx . . $ autoSlopeSelector# SlopeSelector_createcxx . . $ SlopeSelector_create(c8b26b5ce4b835b3). (constVECcxx . . $ constVEC# const& x, constVECcxx . . $ constVEC# const& y); |
| 59 | |
| 60 | autoSlopeSelectorcxx . . $ autoSlopeSelector# SlopeSelector_createcxx . . $ SlopeSelector_create(534d4ee597326f98). (integercxx . . $ integer# numberOfPoints); |
| 61 | |
| 62 | void SlopeSelector_getSlopeAndInterceptcxx . . $ SlopeSelector_getSlopeAndIntercept(3ee7399ea56bc19b). (SlopeSelectorcxx . . $ SlopeSelector# me, double &slope, double &intercept, kSlopeSelector_methodcxx . . $ kSlopeSelector_method# method); |
| 63 | |
| 64 | /****************/ |
| 65 | |
| 66 | void timeSlopeSelectioncxx . . $ timeSlopeSelection(49f6e7a06ebc5aa8). (); |
| 67 | |
| 68 | #endif /* _SlopeSelector_h_ */ |