1 | /*************************************************************************
|
---|
2 | ALGLIB 3.9.0 (source code generated 2014-12-11)
|
---|
3 | Copyright (c) Sergey Bochkanov (ALGLIB project).
|
---|
4 |
|
---|
5 | >>> SOURCE LICENSE >>>
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
9 | License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | A copy of the GNU General Public License is available at
|
---|
17 | http://www.fsf.org/licensing/licenses
|
---|
18 | >>> END OF LICENSE >>>
|
---|
19 | *************************************************************************/
|
---|
20 | #pragma warning disable 162
|
---|
21 | #pragma warning disable 219
|
---|
22 | using System;
|
---|
23 |
|
---|
24 | public partial class alglib
|
---|
25 | {
|
---|
26 |
|
---|
27 |
|
---|
28 | /*************************************************************************
|
---|
29 | Portable high quality random number generator state.
|
---|
30 | Initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
31 |
|
---|
32 | Fields:
|
---|
33 | S1, S2 - seed values
|
---|
34 | V - precomputed value
|
---|
35 | MagicV - 'magic' value used to determine whether State structure
|
---|
36 | was correctly initialized.
|
---|
37 | *************************************************************************/
|
---|
38 | public class hqrndstate : alglibobject
|
---|
39 | {
|
---|
40 | //
|
---|
41 | // Public declarations
|
---|
42 | //
|
---|
43 |
|
---|
44 | public hqrndstate()
|
---|
45 | {
|
---|
46 | _innerobj = new hqrnd.hqrndstate();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override alglib.alglibobject make_copy()
|
---|
50 | {
|
---|
51 | return new hqrndstate((hqrnd.hqrndstate)_innerobj.make_copy());
|
---|
52 | }
|
---|
53 |
|
---|
54 | //
|
---|
55 | // Although some of declarations below are public, you should not use them
|
---|
56 | // They are intended for internal use only
|
---|
57 | //
|
---|
58 | private hqrnd.hqrndstate _innerobj;
|
---|
59 | public hqrnd.hqrndstate innerobj { get { return _innerobj; } }
|
---|
60 | public hqrndstate(hqrnd.hqrndstate obj)
|
---|
61 | {
|
---|
62 | _innerobj = obj;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*************************************************************************
|
---|
67 | HQRNDState initialization with random values which come from standard
|
---|
68 | RNG.
|
---|
69 |
|
---|
70 | -- ALGLIB --
|
---|
71 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
72 | *************************************************************************/
|
---|
73 | public static void hqrndrandomize(out hqrndstate state)
|
---|
74 | {
|
---|
75 | state = new hqrndstate();
|
---|
76 | hqrnd.hqrndrandomize(state.innerobj);
|
---|
77 | return;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*************************************************************************
|
---|
81 | HQRNDState initialization with seed values
|
---|
82 |
|
---|
83 | -- ALGLIB --
|
---|
84 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
85 | *************************************************************************/
|
---|
86 | public static void hqrndseed(int s1, int s2, out hqrndstate state)
|
---|
87 | {
|
---|
88 | state = new hqrndstate();
|
---|
89 | hqrnd.hqrndseed(s1, s2, state.innerobj);
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*************************************************************************
|
---|
94 | This function generates random real number in (0,1),
|
---|
95 | not including interval boundaries
|
---|
96 |
|
---|
97 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
98 |
|
---|
99 | -- ALGLIB --
|
---|
100 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
101 | *************************************************************************/
|
---|
102 | public static double hqrnduniformr(hqrndstate state)
|
---|
103 | {
|
---|
104 |
|
---|
105 | double result = hqrnd.hqrnduniformr(state.innerobj);
|
---|
106 | return result;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*************************************************************************
|
---|
110 | This function generates random integer number in [0, N)
|
---|
111 |
|
---|
112 | 1. State structure must be initialized with HQRNDRandomize() or HQRNDSeed()
|
---|
113 | 2. N can be any positive number except for very large numbers:
|
---|
114 | * close to 2^31 on 32-bit systems
|
---|
115 | * close to 2^62 on 64-bit systems
|
---|
116 | An exception will be generated if N is too large.
|
---|
117 |
|
---|
118 | -- ALGLIB --
|
---|
119 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
120 | *************************************************************************/
|
---|
121 | public static int hqrnduniformi(hqrndstate state, int n)
|
---|
122 | {
|
---|
123 |
|
---|
124 | int result = hqrnd.hqrnduniformi(state.innerobj, n);
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*************************************************************************
|
---|
129 | Random number generator: normal numbers
|
---|
130 |
|
---|
131 | This function generates one random number from normal distribution.
|
---|
132 | Its performance is equal to that of HQRNDNormal2()
|
---|
133 |
|
---|
134 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
135 |
|
---|
136 | -- ALGLIB --
|
---|
137 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
138 | *************************************************************************/
|
---|
139 | public static double hqrndnormal(hqrndstate state)
|
---|
140 | {
|
---|
141 |
|
---|
142 | double result = hqrnd.hqrndnormal(state.innerobj);
|
---|
143 | return result;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*************************************************************************
|
---|
147 | Random number generator: random X and Y such that X^2+Y^2=1
|
---|
148 |
|
---|
149 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
150 |
|
---|
151 | -- ALGLIB --
|
---|
152 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
153 | *************************************************************************/
|
---|
154 | public static void hqrndunit2(hqrndstate state, out double x, out double y)
|
---|
155 | {
|
---|
156 | x = 0;
|
---|
157 | y = 0;
|
---|
158 | hqrnd.hqrndunit2(state.innerobj, ref x, ref y);
|
---|
159 | return;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*************************************************************************
|
---|
163 | Random number generator: normal numbers
|
---|
164 |
|
---|
165 | This function generates two independent random numbers from normal
|
---|
166 | distribution. Its performance is equal to that of HQRNDNormal()
|
---|
167 |
|
---|
168 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
169 |
|
---|
170 | -- ALGLIB --
|
---|
171 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
172 | *************************************************************************/
|
---|
173 | public static void hqrndnormal2(hqrndstate state, out double x1, out double x2)
|
---|
174 | {
|
---|
175 | x1 = 0;
|
---|
176 | x2 = 0;
|
---|
177 | hqrnd.hqrndnormal2(state.innerobj, ref x1, ref x2);
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*************************************************************************
|
---|
182 | Random number generator: exponential distribution
|
---|
183 |
|
---|
184 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
185 |
|
---|
186 | -- ALGLIB --
|
---|
187 | Copyright 11.08.2007 by Bochkanov Sergey
|
---|
188 | *************************************************************************/
|
---|
189 | public static double hqrndexponential(hqrndstate state, double lambdav)
|
---|
190 | {
|
---|
191 |
|
---|
192 | double result = hqrnd.hqrndexponential(state.innerobj, lambdav);
|
---|
193 | return result;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*************************************************************************
|
---|
197 | This function generates random number from discrete distribution given by
|
---|
198 | finite sample X.
|
---|
199 |
|
---|
200 | INPUT PARAMETERS
|
---|
201 | State - high quality random number generator, must be
|
---|
202 | initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
203 | X - finite sample
|
---|
204 | N - number of elements to use, N>=1
|
---|
205 |
|
---|
206 | RESULT
|
---|
207 | this function returns one of the X[i] for random i=0..N-1
|
---|
208 |
|
---|
209 | -- ALGLIB --
|
---|
210 | Copyright 08.11.2011 by Bochkanov Sergey
|
---|
211 | *************************************************************************/
|
---|
212 | public static double hqrnddiscrete(hqrndstate state, double[] x, int n)
|
---|
213 | {
|
---|
214 |
|
---|
215 | double result = hqrnd.hqrnddiscrete(state.innerobj, x, n);
|
---|
216 | return result;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*************************************************************************
|
---|
220 | This function generates random number from continuous distribution given
|
---|
221 | by finite sample X.
|
---|
222 |
|
---|
223 | INPUT PARAMETERS
|
---|
224 | State - high quality random number generator, must be
|
---|
225 | initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
226 | X - finite sample, array[N] (can be larger, in this case only
|
---|
227 | leading N elements are used). THIS ARRAY MUST BE SORTED BY
|
---|
228 | ASCENDING.
|
---|
229 | N - number of elements to use, N>=1
|
---|
230 |
|
---|
231 | RESULT
|
---|
232 | this function returns random number from continuous distribution which
|
---|
233 | tries to approximate X as mush as possible. min(X)<=Result<=max(X).
|
---|
234 |
|
---|
235 | -- ALGLIB --
|
---|
236 | Copyright 08.11.2011 by Bochkanov Sergey
|
---|
237 | *************************************************************************/
|
---|
238 | public static double hqrndcontinuous(hqrndstate state, double[] x, int n)
|
---|
239 | {
|
---|
240 |
|
---|
241 | double result = hqrnd.hqrndcontinuous(state.innerobj, x, n);
|
---|
242 | return result;
|
---|
243 | }
|
---|
244 |
|
---|
245 | }
|
---|
246 | public partial class alglib
|
---|
247 | {
|
---|
248 |
|
---|
249 |
|
---|
250 | /*************************************************************************
|
---|
251 |
|
---|
252 | *************************************************************************/
|
---|
253 | public class kdtree : alglibobject
|
---|
254 | {
|
---|
255 | //
|
---|
256 | // Public declarations
|
---|
257 | //
|
---|
258 |
|
---|
259 | public kdtree()
|
---|
260 | {
|
---|
261 | _innerobj = new nearestneighbor.kdtree();
|
---|
262 | }
|
---|
263 |
|
---|
264 | public override alglib.alglibobject make_copy()
|
---|
265 | {
|
---|
266 | return new kdtree((nearestneighbor.kdtree)_innerobj.make_copy());
|
---|
267 | }
|
---|
268 |
|
---|
269 | //
|
---|
270 | // Although some of declarations below are public, you should not use them
|
---|
271 | // They are intended for internal use only
|
---|
272 | //
|
---|
273 | private nearestneighbor.kdtree _innerobj;
|
---|
274 | public nearestneighbor.kdtree innerobj { get { return _innerobj; } }
|
---|
275 | public kdtree(nearestneighbor.kdtree obj)
|
---|
276 | {
|
---|
277 | _innerobj = obj;
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | /*************************************************************************
|
---|
283 | This function serializes data structure to string.
|
---|
284 |
|
---|
285 | Important properties of s_out:
|
---|
286 | * it contains alphanumeric characters, dots, underscores, minus signs
|
---|
287 | * these symbols are grouped into words, which are separated by spaces
|
---|
288 | and Windows-style (CR+LF) newlines
|
---|
289 | * although serializer uses spaces and CR+LF as separators, you can
|
---|
290 | replace any separator character by arbitrary combination of spaces,
|
---|
291 | tabs, Windows or Unix newlines. It allows flexible reformatting of
|
---|
292 | the string in case you want to include it into text or XML file.
|
---|
293 | But you should not insert separators into the middle of the "words"
|
---|
294 | nor you should change case of letters.
|
---|
295 | * s_out can be freely moved between 32-bit and 64-bit systems, little
|
---|
296 | and big endian machines, and so on. You can serialize structure on
|
---|
297 | 32-bit machine and unserialize it on 64-bit one (or vice versa), or
|
---|
298 | serialize it on SPARC and unserialize on x86. You can also
|
---|
299 | serialize it in C# version of ALGLIB and unserialize in C++ one,
|
---|
300 | and vice versa.
|
---|
301 | *************************************************************************/
|
---|
302 | public static void kdtreeserialize(kdtree obj, out string s_out)
|
---|
303 | {
|
---|
304 | alglib.serializer s = new alglib.serializer();
|
---|
305 | s.alloc_start();
|
---|
306 | nearestneighbor.kdtreealloc(s, obj.innerobj);
|
---|
307 | s.sstart_str();
|
---|
308 | nearestneighbor.kdtreeserialize(s, obj.innerobj);
|
---|
309 | s.stop();
|
---|
310 | s_out = s.get_string();
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /*************************************************************************
|
---|
315 | This function unserializes data structure from string.
|
---|
316 | *************************************************************************/
|
---|
317 | public static void kdtreeunserialize(string s_in, out kdtree obj)
|
---|
318 | {
|
---|
319 | alglib.serializer s = new alglib.serializer();
|
---|
320 | obj = new kdtree();
|
---|
321 | s.ustart_str(s_in);
|
---|
322 | nearestneighbor.kdtreeunserialize(s, obj.innerobj);
|
---|
323 | s.stop();
|
---|
324 | }
|
---|
325 |
|
---|
326 | /*************************************************************************
|
---|
327 | KD-tree creation
|
---|
328 |
|
---|
329 | This subroutine creates KD-tree from set of X-values and optional Y-values
|
---|
330 |
|
---|
331 | INPUT PARAMETERS
|
---|
332 | XY - dataset, array[0..N-1,0..NX+NY-1].
|
---|
333 | one row corresponds to one point.
|
---|
334 | first NX columns contain X-values, next NY (NY may be zero)
|
---|
335 | columns may contain associated Y-values
|
---|
336 | N - number of points, N>=0.
|
---|
337 | NX - space dimension, NX>=1.
|
---|
338 | NY - number of optional Y-values, NY>=0.
|
---|
339 | NormType- norm type:
|
---|
340 | * 0 denotes infinity-norm
|
---|
341 | * 1 denotes 1-norm
|
---|
342 | * 2 denotes 2-norm (Euclidean norm)
|
---|
343 |
|
---|
344 | OUTPUT PARAMETERS
|
---|
345 | KDT - KD-tree
|
---|
346 |
|
---|
347 |
|
---|
348 | NOTES
|
---|
349 |
|
---|
350 | 1. KD-tree creation have O(N*logN) complexity and O(N*(2*NX+NY)) memory
|
---|
351 | requirements.
|
---|
352 | 2. Although KD-trees may be used with any combination of N and NX, they
|
---|
353 | are more efficient than brute-force search only when N >> 4^NX. So they
|
---|
354 | are most useful in low-dimensional tasks (NX=2, NX=3). NX=1 is another
|
---|
355 | inefficient case, because simple binary search (without additional
|
---|
356 | structures) is much more efficient in such tasks than KD-trees.
|
---|
357 |
|
---|
358 | -- ALGLIB --
|
---|
359 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
360 | *************************************************************************/
|
---|
361 | public static void kdtreebuild(double[,] xy, int n, int nx, int ny, int normtype, out kdtree kdt)
|
---|
362 | {
|
---|
363 | kdt = new kdtree();
|
---|
364 | nearestneighbor.kdtreebuild(xy, n, nx, ny, normtype, kdt.innerobj);
|
---|
365 | return;
|
---|
366 | }
|
---|
367 | public static void kdtreebuild(double[,] xy, int nx, int ny, int normtype, out kdtree kdt)
|
---|
368 | {
|
---|
369 | int n;
|
---|
370 |
|
---|
371 | kdt = new kdtree();
|
---|
372 | n = ap.rows(xy);
|
---|
373 | nearestneighbor.kdtreebuild(xy, n, nx, ny, normtype, kdt.innerobj);
|
---|
374 |
|
---|
375 | return;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*************************************************************************
|
---|
379 | KD-tree creation
|
---|
380 |
|
---|
381 | This subroutine creates KD-tree from set of X-values, integer tags and
|
---|
382 | optional Y-values
|
---|
383 |
|
---|
384 | INPUT PARAMETERS
|
---|
385 | XY - dataset, array[0..N-1,0..NX+NY-1].
|
---|
386 | one row corresponds to one point.
|
---|
387 | first NX columns contain X-values, next NY (NY may be zero)
|
---|
388 | columns may contain associated Y-values
|
---|
389 | Tags - tags, array[0..N-1], contains integer tags associated
|
---|
390 | with points.
|
---|
391 | N - number of points, N>=0
|
---|
392 | NX - space dimension, NX>=1.
|
---|
393 | NY - number of optional Y-values, NY>=0.
|
---|
394 | NormType- norm type:
|
---|
395 | * 0 denotes infinity-norm
|
---|
396 | * 1 denotes 1-norm
|
---|
397 | * 2 denotes 2-norm (Euclidean norm)
|
---|
398 |
|
---|
399 | OUTPUT PARAMETERS
|
---|
400 | KDT - KD-tree
|
---|
401 |
|
---|
402 | NOTES
|
---|
403 |
|
---|
404 | 1. KD-tree creation have O(N*logN) complexity and O(N*(2*NX+NY)) memory
|
---|
405 | requirements.
|
---|
406 | 2. Although KD-trees may be used with any combination of N and NX, they
|
---|
407 | are more efficient than brute-force search only when N >> 4^NX. So they
|
---|
408 | are most useful in low-dimensional tasks (NX=2, NX=3). NX=1 is another
|
---|
409 | inefficient case, because simple binary search (without additional
|
---|
410 | structures) is much more efficient in such tasks than KD-trees.
|
---|
411 |
|
---|
412 | -- ALGLIB --
|
---|
413 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
414 | *************************************************************************/
|
---|
415 | public static void kdtreebuildtagged(double[,] xy, int[] tags, int n, int nx, int ny, int normtype, out kdtree kdt)
|
---|
416 | {
|
---|
417 | kdt = new kdtree();
|
---|
418 | nearestneighbor.kdtreebuildtagged(xy, tags, n, nx, ny, normtype, kdt.innerobj);
|
---|
419 | return;
|
---|
420 | }
|
---|
421 | public static void kdtreebuildtagged(double[,] xy, int[] tags, int nx, int ny, int normtype, out kdtree kdt)
|
---|
422 | {
|
---|
423 | int n;
|
---|
424 | if( (ap.rows(xy)!=ap.len(tags)))
|
---|
425 | throw new alglibexception("Error while calling 'kdtreebuildtagged': looks like one of arguments has wrong size");
|
---|
426 | kdt = new kdtree();
|
---|
427 | n = ap.rows(xy);
|
---|
428 | nearestneighbor.kdtreebuildtagged(xy, tags, n, nx, ny, normtype, kdt.innerobj);
|
---|
429 |
|
---|
430 | return;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /*************************************************************************
|
---|
434 | K-NN query: K nearest neighbors
|
---|
435 |
|
---|
436 | INPUT PARAMETERS
|
---|
437 | KDT - KD-tree
|
---|
438 | X - point, array[0..NX-1].
|
---|
439 | K - number of neighbors to return, K>=1
|
---|
440 | SelfMatch - whether self-matches are allowed:
|
---|
441 | * if True, nearest neighbor may be the point itself
|
---|
442 | (if it exists in original dataset)
|
---|
443 | * if False, then only points with non-zero distance
|
---|
444 | are returned
|
---|
445 | * if not given, considered True
|
---|
446 |
|
---|
447 | RESULT
|
---|
448 | number of actual neighbors found (either K or N, if K>N).
|
---|
449 |
|
---|
450 | This subroutine performs query and stores its result in the internal
|
---|
451 | structures of the KD-tree. You can use following subroutines to obtain
|
---|
452 | these results:
|
---|
453 | * KDTreeQueryResultsX() to get X-values
|
---|
454 | * KDTreeQueryResultsXY() to get X- and Y-values
|
---|
455 | * KDTreeQueryResultsTags() to get tag values
|
---|
456 | * KDTreeQueryResultsDistances() to get distances
|
---|
457 |
|
---|
458 | -- ALGLIB --
|
---|
459 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
460 | *************************************************************************/
|
---|
461 | public static int kdtreequeryknn(kdtree kdt, double[] x, int k, bool selfmatch)
|
---|
462 | {
|
---|
463 |
|
---|
464 | int result = nearestneighbor.kdtreequeryknn(kdt.innerobj, x, k, selfmatch);
|
---|
465 | return result;
|
---|
466 | }
|
---|
467 | public static int kdtreequeryknn(kdtree kdt, double[] x, int k)
|
---|
468 | {
|
---|
469 | bool selfmatch;
|
---|
470 |
|
---|
471 |
|
---|
472 | selfmatch = true;
|
---|
473 | int result = nearestneighbor.kdtreequeryknn(kdt.innerobj, x, k, selfmatch);
|
---|
474 |
|
---|
475 | return result;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /*************************************************************************
|
---|
479 | R-NN query: all points within R-sphere centered at X
|
---|
480 |
|
---|
481 | INPUT PARAMETERS
|
---|
482 | KDT - KD-tree
|
---|
483 | X - point, array[0..NX-1].
|
---|
484 | R - radius of sphere (in corresponding norm), R>0
|
---|
485 | SelfMatch - whether self-matches are allowed:
|
---|
486 | * if True, nearest neighbor may be the point itself
|
---|
487 | (if it exists in original dataset)
|
---|
488 | * if False, then only points with non-zero distance
|
---|
489 | are returned
|
---|
490 | * if not given, considered True
|
---|
491 |
|
---|
492 | RESULT
|
---|
493 | number of neighbors found, >=0
|
---|
494 |
|
---|
495 | This subroutine performs query and stores its result in the internal
|
---|
496 | structures of the KD-tree. You can use following subroutines to obtain
|
---|
497 | actual results:
|
---|
498 | * KDTreeQueryResultsX() to get X-values
|
---|
499 | * KDTreeQueryResultsXY() to get X- and Y-values
|
---|
500 | * KDTreeQueryResultsTags() to get tag values
|
---|
501 | * KDTreeQueryResultsDistances() to get distances
|
---|
502 |
|
---|
503 | -- ALGLIB --
|
---|
504 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
505 | *************************************************************************/
|
---|
506 | public static int kdtreequeryrnn(kdtree kdt, double[] x, double r, bool selfmatch)
|
---|
507 | {
|
---|
508 |
|
---|
509 | int result = nearestneighbor.kdtreequeryrnn(kdt.innerobj, x, r, selfmatch);
|
---|
510 | return result;
|
---|
511 | }
|
---|
512 | public static int kdtreequeryrnn(kdtree kdt, double[] x, double r)
|
---|
513 | {
|
---|
514 | bool selfmatch;
|
---|
515 |
|
---|
516 |
|
---|
517 | selfmatch = true;
|
---|
518 | int result = nearestneighbor.kdtreequeryrnn(kdt.innerobj, x, r, selfmatch);
|
---|
519 |
|
---|
520 | return result;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /*************************************************************************
|
---|
524 | K-NN query: approximate K nearest neighbors
|
---|
525 |
|
---|
526 | INPUT PARAMETERS
|
---|
527 | KDT - KD-tree
|
---|
528 | X - point, array[0..NX-1].
|
---|
529 | K - number of neighbors to return, K>=1
|
---|
530 | SelfMatch - whether self-matches are allowed:
|
---|
531 | * if True, nearest neighbor may be the point itself
|
---|
532 | (if it exists in original dataset)
|
---|
533 | * if False, then only points with non-zero distance
|
---|
534 | are returned
|
---|
535 | * if not given, considered True
|
---|
536 | Eps - approximation factor, Eps>=0. eps-approximate nearest
|
---|
537 | neighbor is a neighbor whose distance from X is at
|
---|
538 | most (1+eps) times distance of true nearest neighbor.
|
---|
539 |
|
---|
540 | RESULT
|
---|
541 | number of actual neighbors found (either K or N, if K>N).
|
---|
542 |
|
---|
543 | NOTES
|
---|
544 | significant performance gain may be achieved only when Eps is is on
|
---|
545 | the order of magnitude of 1 or larger.
|
---|
546 |
|
---|
547 | This subroutine performs query and stores its result in the internal
|
---|
548 | structures of the KD-tree. You can use following subroutines to obtain
|
---|
549 | these results:
|
---|
550 | * KDTreeQueryResultsX() to get X-values
|
---|
551 | * KDTreeQueryResultsXY() to get X- and Y-values
|
---|
552 | * KDTreeQueryResultsTags() to get tag values
|
---|
553 | * KDTreeQueryResultsDistances() to get distances
|
---|
554 |
|
---|
555 | -- ALGLIB --
|
---|
556 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
557 | *************************************************************************/
|
---|
558 | public static int kdtreequeryaknn(kdtree kdt, double[] x, int k, bool selfmatch, double eps)
|
---|
559 | {
|
---|
560 |
|
---|
561 | int result = nearestneighbor.kdtreequeryaknn(kdt.innerobj, x, k, selfmatch, eps);
|
---|
562 | return result;
|
---|
563 | }
|
---|
564 | public static int kdtreequeryaknn(kdtree kdt, double[] x, int k, double eps)
|
---|
565 | {
|
---|
566 | bool selfmatch;
|
---|
567 |
|
---|
568 |
|
---|
569 | selfmatch = true;
|
---|
570 | int result = nearestneighbor.kdtreequeryaknn(kdt.innerobj, x, k, selfmatch, eps);
|
---|
571 |
|
---|
572 | return result;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /*************************************************************************
|
---|
576 | X-values from last query
|
---|
577 |
|
---|
578 | INPUT PARAMETERS
|
---|
579 | KDT - KD-tree
|
---|
580 | X - possibly pre-allocated buffer. If X is too small to store
|
---|
581 | result, it is resized. If size(X) is enough to store
|
---|
582 | result, it is left unchanged.
|
---|
583 |
|
---|
584 | OUTPUT PARAMETERS
|
---|
585 | X - rows are filled with X-values
|
---|
586 |
|
---|
587 | NOTES
|
---|
588 | 1. points are ordered by distance from the query point (first = closest)
|
---|
589 | 2. if XY is larger than required to store result, only leading part will
|
---|
590 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
591 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
592 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
593 | you want function to resize array according to result size, use
|
---|
594 | function with same name and suffix 'I'.
|
---|
595 |
|
---|
596 | SEE ALSO
|
---|
597 | * KDTreeQueryResultsXY() X- and Y-values
|
---|
598 | * KDTreeQueryResultsTags() tag values
|
---|
599 | * KDTreeQueryResultsDistances() distances
|
---|
600 |
|
---|
601 | -- ALGLIB --
|
---|
602 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
603 | *************************************************************************/
|
---|
604 | public static void kdtreequeryresultsx(kdtree kdt, ref double[,] x)
|
---|
605 | {
|
---|
606 |
|
---|
607 | nearestneighbor.kdtreequeryresultsx(kdt.innerobj, ref x);
|
---|
608 | return;
|
---|
609 | }
|
---|
610 |
|
---|
611 | /*************************************************************************
|
---|
612 | X- and Y-values from last query
|
---|
613 |
|
---|
614 | INPUT PARAMETERS
|
---|
615 | KDT - KD-tree
|
---|
616 | XY - possibly pre-allocated buffer. If XY is too small to store
|
---|
617 | result, it is resized. If size(XY) is enough to store
|
---|
618 | result, it is left unchanged.
|
---|
619 |
|
---|
620 | OUTPUT PARAMETERS
|
---|
621 | XY - rows are filled with points: first NX columns with
|
---|
622 | X-values, next NY columns - with Y-values.
|
---|
623 |
|
---|
624 | NOTES
|
---|
625 | 1. points are ordered by distance from the query point (first = closest)
|
---|
626 | 2. if XY is larger than required to store result, only leading part will
|
---|
627 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
628 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
629 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
630 | you want function to resize array according to result size, use
|
---|
631 | function with same name and suffix 'I'.
|
---|
632 |
|
---|
633 | SEE ALSO
|
---|
634 | * KDTreeQueryResultsX() X-values
|
---|
635 | * KDTreeQueryResultsTags() tag values
|
---|
636 | * KDTreeQueryResultsDistances() distances
|
---|
637 |
|
---|
638 | -- ALGLIB --
|
---|
639 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
640 | *************************************************************************/
|
---|
641 | public static void kdtreequeryresultsxy(kdtree kdt, ref double[,] xy)
|
---|
642 | {
|
---|
643 |
|
---|
644 | nearestneighbor.kdtreequeryresultsxy(kdt.innerobj, ref xy);
|
---|
645 | return;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /*************************************************************************
|
---|
649 | Tags from last query
|
---|
650 |
|
---|
651 | INPUT PARAMETERS
|
---|
652 | KDT - KD-tree
|
---|
653 | Tags - possibly pre-allocated buffer. If X is too small to store
|
---|
654 | result, it is resized. If size(X) is enough to store
|
---|
655 | result, it is left unchanged.
|
---|
656 |
|
---|
657 | OUTPUT PARAMETERS
|
---|
658 | Tags - filled with tags associated with points,
|
---|
659 | or, when no tags were supplied, with zeros
|
---|
660 |
|
---|
661 | NOTES
|
---|
662 | 1. points are ordered by distance from the query point (first = closest)
|
---|
663 | 2. if XY is larger than required to store result, only leading part will
|
---|
664 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
665 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
666 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
667 | you want function to resize array according to result size, use
|
---|
668 | function with same name and suffix 'I'.
|
---|
669 |
|
---|
670 | SEE ALSO
|
---|
671 | * KDTreeQueryResultsX() X-values
|
---|
672 | * KDTreeQueryResultsXY() X- and Y-values
|
---|
673 | * KDTreeQueryResultsDistances() distances
|
---|
674 |
|
---|
675 | -- ALGLIB --
|
---|
676 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
677 | *************************************************************************/
|
---|
678 | public static void kdtreequeryresultstags(kdtree kdt, ref int[] tags)
|
---|
679 | {
|
---|
680 |
|
---|
681 | nearestneighbor.kdtreequeryresultstags(kdt.innerobj, ref tags);
|
---|
682 | return;
|
---|
683 | }
|
---|
684 |
|
---|
685 | /*************************************************************************
|
---|
686 | Distances from last query
|
---|
687 |
|
---|
688 | INPUT PARAMETERS
|
---|
689 | KDT - KD-tree
|
---|
690 | R - possibly pre-allocated buffer. If X is too small to store
|
---|
691 | result, it is resized. If size(X) is enough to store
|
---|
692 | result, it is left unchanged.
|
---|
693 |
|
---|
694 | OUTPUT PARAMETERS
|
---|
695 | R - filled with distances (in corresponding norm)
|
---|
696 |
|
---|
697 | NOTES
|
---|
698 | 1. points are ordered by distance from the query point (first = closest)
|
---|
699 | 2. if XY is larger than required to store result, only leading part will
|
---|
700 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
701 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
702 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
703 | you want function to resize array according to result size, use
|
---|
704 | function with same name and suffix 'I'.
|
---|
705 |
|
---|
706 | SEE ALSO
|
---|
707 | * KDTreeQueryResultsX() X-values
|
---|
708 | * KDTreeQueryResultsXY() X- and Y-values
|
---|
709 | * KDTreeQueryResultsTags() tag values
|
---|
710 |
|
---|
711 | -- ALGLIB --
|
---|
712 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
713 | *************************************************************************/
|
---|
714 | public static void kdtreequeryresultsdistances(kdtree kdt, ref double[] r)
|
---|
715 | {
|
---|
716 |
|
---|
717 | nearestneighbor.kdtreequeryresultsdistances(kdt.innerobj, ref r);
|
---|
718 | return;
|
---|
719 | }
|
---|
720 |
|
---|
721 | /*************************************************************************
|
---|
722 | X-values from last query; 'interactive' variant for languages like Python
|
---|
723 | which support constructs like "X = KDTreeQueryResultsXI(KDT)" and
|
---|
724 | interactive mode of interpreter.
|
---|
725 |
|
---|
726 | This function allocates new array on each call, so it is significantly
|
---|
727 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
728 | when you call it from command line.
|
---|
729 |
|
---|
730 | -- ALGLIB --
|
---|
731 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
732 | *************************************************************************/
|
---|
733 | public static void kdtreequeryresultsxi(kdtree kdt, out double[,] x)
|
---|
734 | {
|
---|
735 | x = new double[0,0];
|
---|
736 | nearestneighbor.kdtreequeryresultsxi(kdt.innerobj, ref x);
|
---|
737 | return;
|
---|
738 | }
|
---|
739 |
|
---|
740 | /*************************************************************************
|
---|
741 | XY-values from last query; 'interactive' variant for languages like Python
|
---|
742 | which support constructs like "XY = KDTreeQueryResultsXYI(KDT)" and
|
---|
743 | interactive mode of interpreter.
|
---|
744 |
|
---|
745 | This function allocates new array on each call, so it is significantly
|
---|
746 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
747 | when you call it from command line.
|
---|
748 |
|
---|
749 | -- ALGLIB --
|
---|
750 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
751 | *************************************************************************/
|
---|
752 | public static void kdtreequeryresultsxyi(kdtree kdt, out double[,] xy)
|
---|
753 | {
|
---|
754 | xy = new double[0,0];
|
---|
755 | nearestneighbor.kdtreequeryresultsxyi(kdt.innerobj, ref xy);
|
---|
756 | return;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /*************************************************************************
|
---|
760 | Tags from last query; 'interactive' variant for languages like Python
|
---|
761 | which support constructs like "Tags = KDTreeQueryResultsTagsI(KDT)" and
|
---|
762 | interactive mode of interpreter.
|
---|
763 |
|
---|
764 | This function allocates new array on each call, so it is significantly
|
---|
765 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
766 | when you call it from command line.
|
---|
767 |
|
---|
768 | -- ALGLIB --
|
---|
769 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
770 | *************************************************************************/
|
---|
771 | public static void kdtreequeryresultstagsi(kdtree kdt, out int[] tags)
|
---|
772 | {
|
---|
773 | tags = new int[0];
|
---|
774 | nearestneighbor.kdtreequeryresultstagsi(kdt.innerobj, ref tags);
|
---|
775 | return;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /*************************************************************************
|
---|
779 | Distances from last query; 'interactive' variant for languages like Python
|
---|
780 | which support constructs like "R = KDTreeQueryResultsDistancesI(KDT)"
|
---|
781 | and interactive mode of interpreter.
|
---|
782 |
|
---|
783 | This function allocates new array on each call, so it is significantly
|
---|
784 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
785 | when you call it from command line.
|
---|
786 |
|
---|
787 | -- ALGLIB --
|
---|
788 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
789 | *************************************************************************/
|
---|
790 | public static void kdtreequeryresultsdistancesi(kdtree kdt, out double[] r)
|
---|
791 | {
|
---|
792 | r = new double[0];
|
---|
793 | nearestneighbor.kdtreequeryresultsdistancesi(kdt.innerobj, ref r);
|
---|
794 | return;
|
---|
795 | }
|
---|
796 |
|
---|
797 | }
|
---|
798 | public partial class alglib
|
---|
799 | {
|
---|
800 |
|
---|
801 |
|
---|
802 | /*************************************************************************
|
---|
803 |
|
---|
804 | *************************************************************************/
|
---|
805 | public class xdebugrecord1 : alglibobject
|
---|
806 | {
|
---|
807 | //
|
---|
808 | // Public declarations
|
---|
809 | //
|
---|
810 | public int i { get { return _innerobj.i; } set { _innerobj.i = value; } }
|
---|
811 | public complex c { get { return _innerobj.c; } set { _innerobj.c = value; } }
|
---|
812 | public double[] a { get { return _innerobj.a; } set { _innerobj.a = value; } }
|
---|
813 |
|
---|
814 | public xdebugrecord1()
|
---|
815 | {
|
---|
816 | _innerobj = new xdebug.xdebugrecord1();
|
---|
817 | }
|
---|
818 |
|
---|
819 | public override alglib.alglibobject make_copy()
|
---|
820 | {
|
---|
821 | return new xdebugrecord1((xdebug.xdebugrecord1)_innerobj.make_copy());
|
---|
822 | }
|
---|
823 |
|
---|
824 | //
|
---|
825 | // Although some of declarations below are public, you should not use them
|
---|
826 | // They are intended for internal use only
|
---|
827 | //
|
---|
828 | private xdebug.xdebugrecord1 _innerobj;
|
---|
829 | public xdebug.xdebugrecord1 innerobj { get { return _innerobj; } }
|
---|
830 | public xdebugrecord1(xdebug.xdebugrecord1 obj)
|
---|
831 | {
|
---|
832 | _innerobj = obj;
|
---|
833 | }
|
---|
834 | }
|
---|
835 |
|
---|
836 | /*************************************************************************
|
---|
837 | This is debug function intended for testing ALGLIB interface generator.
|
---|
838 | Never use it in any real life project.
|
---|
839 |
|
---|
840 | Creates and returns XDebugRecord1 structure:
|
---|
841 | * integer and complex fields of Rec1 are set to 1 and 1+i correspondingly
|
---|
842 | * array field of Rec1 is set to [2,3]
|
---|
843 |
|
---|
844 | -- ALGLIB --
|
---|
845 | Copyright 27.05.2014 by Bochkanov Sergey
|
---|
846 | *************************************************************************/
|
---|
847 | public static void xdebuginitrecord1(out xdebugrecord1 rec1)
|
---|
848 | {
|
---|
849 | rec1 = new xdebugrecord1();
|
---|
850 | xdebug.xdebuginitrecord1(rec1.innerobj);
|
---|
851 | return;
|
---|
852 | }
|
---|
853 |
|
---|
854 | /*************************************************************************
|
---|
855 | This is debug function intended for testing ALGLIB interface generator.
|
---|
856 | Never use it in any real life project.
|
---|
857 |
|
---|
858 | Counts number of True values in the boolean 1D array.
|
---|
859 |
|
---|
860 | -- ALGLIB --
|
---|
861 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
862 | *************************************************************************/
|
---|
863 | public static int xdebugb1count(bool[] a)
|
---|
864 | {
|
---|
865 |
|
---|
866 | int result = xdebug.xdebugb1count(a);
|
---|
867 | return result;
|
---|
868 | }
|
---|
869 |
|
---|
870 | /*************************************************************************
|
---|
871 | This is debug function intended for testing ALGLIB interface generator.
|
---|
872 | Never use it in any real life project.
|
---|
873 |
|
---|
874 | Replace all values in array by NOT(a[i]).
|
---|
875 | Array is passed using "shared" convention.
|
---|
876 |
|
---|
877 | -- ALGLIB --
|
---|
878 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
879 | *************************************************************************/
|
---|
880 | public static void xdebugb1not(ref bool[] a)
|
---|
881 | {
|
---|
882 |
|
---|
883 | xdebug.xdebugb1not(a);
|
---|
884 | return;
|
---|
885 | }
|
---|
886 |
|
---|
887 | /*************************************************************************
|
---|
888 | This is debug function intended for testing ALGLIB interface generator.
|
---|
889 | Never use it in any real life project.
|
---|
890 |
|
---|
891 | Appends copy of array to itself.
|
---|
892 | Array is passed using "var" convention.
|
---|
893 |
|
---|
894 | -- ALGLIB --
|
---|
895 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
896 | *************************************************************************/
|
---|
897 | public static void xdebugb1appendcopy(ref bool[] a)
|
---|
898 | {
|
---|
899 |
|
---|
900 | xdebug.xdebugb1appendcopy(ref a);
|
---|
901 | return;
|
---|
902 | }
|
---|
903 |
|
---|
904 | /*************************************************************************
|
---|
905 | This is debug function intended for testing ALGLIB interface generator.
|
---|
906 | Never use it in any real life project.
|
---|
907 |
|
---|
908 | Generate N-element array with even-numbered elements set to True.
|
---|
909 | Array is passed using "out" convention.
|
---|
910 |
|
---|
911 | -- ALGLIB --
|
---|
912 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
913 | *************************************************************************/
|
---|
914 | public static void xdebugb1outeven(int n, out bool[] a)
|
---|
915 | {
|
---|
916 | a = new bool[0];
|
---|
917 | xdebug.xdebugb1outeven(n, ref a);
|
---|
918 | return;
|
---|
919 | }
|
---|
920 |
|
---|
921 | /*************************************************************************
|
---|
922 | This is debug function intended for testing ALGLIB interface generator.
|
---|
923 | Never use it in any real life project.
|
---|
924 |
|
---|
925 | Returns sum of elements in the array.
|
---|
926 |
|
---|
927 | -- ALGLIB --
|
---|
928 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
929 | *************************************************************************/
|
---|
930 | public static int xdebugi1sum(int[] a)
|
---|
931 | {
|
---|
932 |
|
---|
933 | int result = xdebug.xdebugi1sum(a);
|
---|
934 | return result;
|
---|
935 | }
|
---|
936 |
|
---|
937 | /*************************************************************************
|
---|
938 | This is debug function intended for testing ALGLIB interface generator.
|
---|
939 | Never use it in any real life project.
|
---|
940 |
|
---|
941 | Replace all values in array by -A[I]
|
---|
942 | Array is passed using "shared" convention.
|
---|
943 |
|
---|
944 | -- ALGLIB --
|
---|
945 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
946 | *************************************************************************/
|
---|
947 | public static void xdebugi1neg(ref int[] a)
|
---|
948 | {
|
---|
949 |
|
---|
950 | xdebug.xdebugi1neg(a);
|
---|
951 | return;
|
---|
952 | }
|
---|
953 |
|
---|
954 | /*************************************************************************
|
---|
955 | This is debug function intended for testing ALGLIB interface generator.
|
---|
956 | Never use it in any real life project.
|
---|
957 |
|
---|
958 | Appends copy of array to itself.
|
---|
959 | Array is passed using "var" convention.
|
---|
960 |
|
---|
961 | -- ALGLIB --
|
---|
962 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
963 | *************************************************************************/
|
---|
964 | public static void xdebugi1appendcopy(ref int[] a)
|
---|
965 | {
|
---|
966 |
|
---|
967 | xdebug.xdebugi1appendcopy(ref a);
|
---|
968 | return;
|
---|
969 | }
|
---|
970 |
|
---|
971 | /*************************************************************************
|
---|
972 | This is debug function intended for testing ALGLIB interface generator.
|
---|
973 | Never use it in any real life project.
|
---|
974 |
|
---|
975 | Generate N-element array with even-numbered A[I] set to I, and odd-numbered
|
---|
976 | ones set to 0.
|
---|
977 |
|
---|
978 | Array is passed using "out" convention.
|
---|
979 |
|
---|
980 | -- ALGLIB --
|
---|
981 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
982 | *************************************************************************/
|
---|
983 | public static void xdebugi1outeven(int n, out int[] a)
|
---|
984 | {
|
---|
985 | a = new int[0];
|
---|
986 | xdebug.xdebugi1outeven(n, ref a);
|
---|
987 | return;
|
---|
988 | }
|
---|
989 |
|
---|
990 | /*************************************************************************
|
---|
991 | This is debug function intended for testing ALGLIB interface generator.
|
---|
992 | Never use it in any real life project.
|
---|
993 |
|
---|
994 | Returns sum of elements in the array.
|
---|
995 |
|
---|
996 | -- ALGLIB --
|
---|
997 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
998 | *************************************************************************/
|
---|
999 | public static double xdebugr1sum(double[] a)
|
---|
1000 | {
|
---|
1001 |
|
---|
1002 | double result = xdebug.xdebugr1sum(a);
|
---|
1003 | return result;
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | /*************************************************************************
|
---|
1007 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1008 | Never use it in any real life project.
|
---|
1009 |
|
---|
1010 | Replace all values in array by -A[I]
|
---|
1011 | Array is passed using "shared" convention.
|
---|
1012 |
|
---|
1013 | -- ALGLIB --
|
---|
1014 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1015 | *************************************************************************/
|
---|
1016 | public static void xdebugr1neg(ref double[] a)
|
---|
1017 | {
|
---|
1018 |
|
---|
1019 | xdebug.xdebugr1neg(a);
|
---|
1020 | return;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /*************************************************************************
|
---|
1024 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1025 | Never use it in any real life project.
|
---|
1026 |
|
---|
1027 | Appends copy of array to itself.
|
---|
1028 | Array is passed using "var" convention.
|
---|
1029 |
|
---|
1030 | -- ALGLIB --
|
---|
1031 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1032 | *************************************************************************/
|
---|
1033 | public static void xdebugr1appendcopy(ref double[] a)
|
---|
1034 | {
|
---|
1035 |
|
---|
1036 | xdebug.xdebugr1appendcopy(ref a);
|
---|
1037 | return;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | /*************************************************************************
|
---|
1041 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1042 | Never use it in any real life project.
|
---|
1043 |
|
---|
1044 | Generate N-element array with even-numbered A[I] set to I*0.25,
|
---|
1045 | and odd-numbered ones are set to 0.
|
---|
1046 |
|
---|
1047 | Array is passed using "out" convention.
|
---|
1048 |
|
---|
1049 | -- ALGLIB --
|
---|
1050 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1051 | *************************************************************************/
|
---|
1052 | public static void xdebugr1outeven(int n, out double[] a)
|
---|
1053 | {
|
---|
1054 | a = new double[0];
|
---|
1055 | xdebug.xdebugr1outeven(n, ref a);
|
---|
1056 | return;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /*************************************************************************
|
---|
1060 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1061 | Never use it in any real life project.
|
---|
1062 |
|
---|
1063 | Returns sum of elements in the array.
|
---|
1064 |
|
---|
1065 | -- ALGLIB --
|
---|
1066 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1067 | *************************************************************************/
|
---|
1068 | public static complex xdebugc1sum(complex[] a)
|
---|
1069 | {
|
---|
1070 |
|
---|
1071 | complex result = xdebug.xdebugc1sum(a);
|
---|
1072 | return result;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | /*************************************************************************
|
---|
1076 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1077 | Never use it in any real life project.
|
---|
1078 |
|
---|
1079 | Replace all values in array by -A[I]
|
---|
1080 | Array is passed using "shared" convention.
|
---|
1081 |
|
---|
1082 | -- ALGLIB --
|
---|
1083 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1084 | *************************************************************************/
|
---|
1085 | public static void xdebugc1neg(ref complex[] a)
|
---|
1086 | {
|
---|
1087 |
|
---|
1088 | xdebug.xdebugc1neg(a);
|
---|
1089 | return;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | /*************************************************************************
|
---|
1093 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1094 | Never use it in any real life project.
|
---|
1095 |
|
---|
1096 | Appends copy of array to itself.
|
---|
1097 | Array is passed using "var" convention.
|
---|
1098 |
|
---|
1099 | -- ALGLIB --
|
---|
1100 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1101 | *************************************************************************/
|
---|
1102 | public static void xdebugc1appendcopy(ref complex[] a)
|
---|
1103 | {
|
---|
1104 |
|
---|
1105 | xdebug.xdebugc1appendcopy(ref a);
|
---|
1106 | return;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /*************************************************************************
|
---|
1110 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1111 | Never use it in any real life project.
|
---|
1112 |
|
---|
1113 | Generate N-element array with even-numbered A[K] set to (x,y) = (K*0.25, K*0.125)
|
---|
1114 | and odd-numbered ones are set to 0.
|
---|
1115 |
|
---|
1116 | Array is passed using "out" convention.
|
---|
1117 |
|
---|
1118 | -- ALGLIB --
|
---|
1119 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1120 | *************************************************************************/
|
---|
1121 | public static void xdebugc1outeven(int n, out complex[] a)
|
---|
1122 | {
|
---|
1123 | a = new complex[0];
|
---|
1124 | xdebug.xdebugc1outeven(n, ref a);
|
---|
1125 | return;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | /*************************************************************************
|
---|
1129 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1130 | Never use it in any real life project.
|
---|
1131 |
|
---|
1132 | Counts number of True values in the boolean 2D array.
|
---|
1133 |
|
---|
1134 | -- ALGLIB --
|
---|
1135 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1136 | *************************************************************************/
|
---|
1137 | public static int xdebugb2count(bool[,] a)
|
---|
1138 | {
|
---|
1139 |
|
---|
1140 | int result = xdebug.xdebugb2count(a);
|
---|
1141 | return result;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | /*************************************************************************
|
---|
1145 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1146 | Never use it in any real life project.
|
---|
1147 |
|
---|
1148 | Replace all values in array by NOT(a[i]).
|
---|
1149 | Array is passed using "shared" convention.
|
---|
1150 |
|
---|
1151 | -- ALGLIB --
|
---|
1152 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1153 | *************************************************************************/
|
---|
1154 | public static void xdebugb2not(ref bool[,] a)
|
---|
1155 | {
|
---|
1156 |
|
---|
1157 | xdebug.xdebugb2not(a);
|
---|
1158 | return;
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | /*************************************************************************
|
---|
1162 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1163 | Never use it in any real life project.
|
---|
1164 |
|
---|
1165 | Transposes array.
|
---|
1166 | Array is passed using "var" convention.
|
---|
1167 |
|
---|
1168 | -- ALGLIB --
|
---|
1169 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1170 | *************************************************************************/
|
---|
1171 | public static void xdebugb2transpose(ref bool[,] a)
|
---|
1172 | {
|
---|
1173 |
|
---|
1174 | xdebug.xdebugb2transpose(ref a);
|
---|
1175 | return;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | /*************************************************************************
|
---|
1179 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1180 | Never use it in any real life project.
|
---|
1181 |
|
---|
1182 | Generate MxN matrix with elements set to "Sin(3*I+5*J)>0"
|
---|
1183 | Array is passed using "out" convention.
|
---|
1184 |
|
---|
1185 | -- ALGLIB --
|
---|
1186 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1187 | *************************************************************************/
|
---|
1188 | public static void xdebugb2outsin(int m, int n, out bool[,] a)
|
---|
1189 | {
|
---|
1190 | a = new bool[0,0];
|
---|
1191 | xdebug.xdebugb2outsin(m, n, ref a);
|
---|
1192 | return;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | /*************************************************************************
|
---|
1196 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1197 | Never use it in any real life project.
|
---|
1198 |
|
---|
1199 | Returns sum of elements in the array.
|
---|
1200 |
|
---|
1201 | -- ALGLIB --
|
---|
1202 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1203 | *************************************************************************/
|
---|
1204 | public static int xdebugi2sum(int[,] a)
|
---|
1205 | {
|
---|
1206 |
|
---|
1207 | int result = xdebug.xdebugi2sum(a);
|
---|
1208 | return result;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | /*************************************************************************
|
---|
1212 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1213 | Never use it in any real life project.
|
---|
1214 |
|
---|
1215 | Replace all values in array by -a[i,j]
|
---|
1216 | Array is passed using "shared" convention.
|
---|
1217 |
|
---|
1218 | -- ALGLIB --
|
---|
1219 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1220 | *************************************************************************/
|
---|
1221 | public static void xdebugi2neg(ref int[,] a)
|
---|
1222 | {
|
---|
1223 |
|
---|
1224 | xdebug.xdebugi2neg(a);
|
---|
1225 | return;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | /*************************************************************************
|
---|
1229 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1230 | Never use it in any real life project.
|
---|
1231 |
|
---|
1232 | Transposes array.
|
---|
1233 | Array is passed using "var" convention.
|
---|
1234 |
|
---|
1235 | -- ALGLIB --
|
---|
1236 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1237 | *************************************************************************/
|
---|
1238 | public static void xdebugi2transpose(ref int[,] a)
|
---|
1239 | {
|
---|
1240 |
|
---|
1241 | xdebug.xdebugi2transpose(ref a);
|
---|
1242 | return;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | /*************************************************************************
|
---|
1246 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1247 | Never use it in any real life project.
|
---|
1248 |
|
---|
1249 | Generate MxN matrix with elements set to "Sign(Sin(3*I+5*J))"
|
---|
1250 | Array is passed using "out" convention.
|
---|
1251 |
|
---|
1252 | -- ALGLIB --
|
---|
1253 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1254 | *************************************************************************/
|
---|
1255 | public static void xdebugi2outsin(int m, int n, out int[,] a)
|
---|
1256 | {
|
---|
1257 | a = new int[0,0];
|
---|
1258 | xdebug.xdebugi2outsin(m, n, ref a);
|
---|
1259 | return;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | /*************************************************************************
|
---|
1263 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1264 | Never use it in any real life project.
|
---|
1265 |
|
---|
1266 | Returns sum of elements in the array.
|
---|
1267 |
|
---|
1268 | -- ALGLIB --
|
---|
1269 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1270 | *************************************************************************/
|
---|
1271 | public static double xdebugr2sum(double[,] a)
|
---|
1272 | {
|
---|
1273 |
|
---|
1274 | double result = xdebug.xdebugr2sum(a);
|
---|
1275 | return result;
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | /*************************************************************************
|
---|
1279 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1280 | Never use it in any real life project.
|
---|
1281 |
|
---|
1282 | Replace all values in array by -a[i,j]
|
---|
1283 | Array is passed using "shared" convention.
|
---|
1284 |
|
---|
1285 | -- ALGLIB --
|
---|
1286 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1287 | *************************************************************************/
|
---|
1288 | public static void xdebugr2neg(ref double[,] a)
|
---|
1289 | {
|
---|
1290 |
|
---|
1291 | xdebug.xdebugr2neg(a);
|
---|
1292 | return;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | /*************************************************************************
|
---|
1296 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1297 | Never use it in any real life project.
|
---|
1298 |
|
---|
1299 | Transposes array.
|
---|
1300 | Array is passed using "var" convention.
|
---|
1301 |
|
---|
1302 | -- ALGLIB --
|
---|
1303 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1304 | *************************************************************************/
|
---|
1305 | public static void xdebugr2transpose(ref double[,] a)
|
---|
1306 | {
|
---|
1307 |
|
---|
1308 | xdebug.xdebugr2transpose(ref a);
|
---|
1309 | return;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /*************************************************************************
|
---|
1313 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1314 | Never use it in any real life project.
|
---|
1315 |
|
---|
1316 | Generate MxN matrix with elements set to "Sin(3*I+5*J)"
|
---|
1317 | Array is passed using "out" convention.
|
---|
1318 |
|
---|
1319 | -- ALGLIB --
|
---|
1320 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1321 | *************************************************************************/
|
---|
1322 | public static void xdebugr2outsin(int m, int n, out double[,] a)
|
---|
1323 | {
|
---|
1324 | a = new double[0,0];
|
---|
1325 | xdebug.xdebugr2outsin(m, n, ref a);
|
---|
1326 | return;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | /*************************************************************************
|
---|
1330 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1331 | Never use it in any real life project.
|
---|
1332 |
|
---|
1333 | Returns sum of elements in the array.
|
---|
1334 |
|
---|
1335 | -- ALGLIB --
|
---|
1336 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1337 | *************************************************************************/
|
---|
1338 | public static complex xdebugc2sum(complex[,] a)
|
---|
1339 | {
|
---|
1340 |
|
---|
1341 | complex result = xdebug.xdebugc2sum(a);
|
---|
1342 | return result;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | /*************************************************************************
|
---|
1346 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1347 | Never use it in any real life project.
|
---|
1348 |
|
---|
1349 | Replace all values in array by -a[i,j]
|
---|
1350 | Array is passed using "shared" convention.
|
---|
1351 |
|
---|
1352 | -- ALGLIB --
|
---|
1353 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1354 | *************************************************************************/
|
---|
1355 | public static void xdebugc2neg(ref complex[,] a)
|
---|
1356 | {
|
---|
1357 |
|
---|
1358 | xdebug.xdebugc2neg(a);
|
---|
1359 | return;
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | /*************************************************************************
|
---|
1363 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1364 | Never use it in any real life project.
|
---|
1365 |
|
---|
1366 | Transposes array.
|
---|
1367 | Array is passed using "var" convention.
|
---|
1368 |
|
---|
1369 | -- ALGLIB --
|
---|
1370 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1371 | *************************************************************************/
|
---|
1372 | public static void xdebugc2transpose(ref complex[,] a)
|
---|
1373 | {
|
---|
1374 |
|
---|
1375 | xdebug.xdebugc2transpose(ref a);
|
---|
1376 | return;
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | /*************************************************************************
|
---|
1380 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1381 | Never use it in any real life project.
|
---|
1382 |
|
---|
1383 | Generate MxN matrix with elements set to "Sin(3*I+5*J),Cos(3*I+5*J)"
|
---|
1384 | Array is passed using "out" convention.
|
---|
1385 |
|
---|
1386 | -- ALGLIB --
|
---|
1387 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1388 | *************************************************************************/
|
---|
1389 | public static void xdebugc2outsincos(int m, int n, out complex[,] a)
|
---|
1390 | {
|
---|
1391 | a = new complex[0,0];
|
---|
1392 | xdebug.xdebugc2outsincos(m, n, ref a);
|
---|
1393 | return;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | /*************************************************************************
|
---|
1397 | This is debug function intended for testing ALGLIB interface generator.
|
---|
1398 | Never use it in any real life project.
|
---|
1399 |
|
---|
1400 | Returns sum of a[i,j]*(1+b[i,j]) such that c[i,j] is True
|
---|
1401 |
|
---|
1402 | -- ALGLIB --
|
---|
1403 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
1404 | *************************************************************************/
|
---|
1405 | public static double xdebugmaskedbiasedproductsum(int m, int n, double[,] a, double[,] b, bool[,] c)
|
---|
1406 | {
|
---|
1407 |
|
---|
1408 | double result = xdebug.xdebugmaskedbiasedproductsum(m, n, a, b, c);
|
---|
1409 | return result;
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | }
|
---|
1413 | public partial class alglib
|
---|
1414 | {
|
---|
1415 | public class hqrnd
|
---|
1416 | {
|
---|
1417 | /*************************************************************************
|
---|
1418 | Portable high quality random number generator state.
|
---|
1419 | Initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1420 |
|
---|
1421 | Fields:
|
---|
1422 | S1, S2 - seed values
|
---|
1423 | V - precomputed value
|
---|
1424 | MagicV - 'magic' value used to determine whether State structure
|
---|
1425 | was correctly initialized.
|
---|
1426 | *************************************************************************/
|
---|
1427 | public class hqrndstate : apobject
|
---|
1428 | {
|
---|
1429 | public int s1;
|
---|
1430 | public int s2;
|
---|
1431 | public int magicv;
|
---|
1432 | public hqrndstate()
|
---|
1433 | {
|
---|
1434 | init();
|
---|
1435 | }
|
---|
1436 | public override void init()
|
---|
1437 | {
|
---|
1438 | }
|
---|
1439 | public override alglib.apobject make_copy()
|
---|
1440 | {
|
---|
1441 | hqrndstate _result = new hqrndstate();
|
---|
1442 | _result.s1 = s1;
|
---|
1443 | _result.s2 = s2;
|
---|
1444 | _result.magicv = magicv;
|
---|
1445 | return _result;
|
---|
1446 | }
|
---|
1447 | };
|
---|
1448 |
|
---|
1449 |
|
---|
1450 |
|
---|
1451 |
|
---|
1452 | public const int hqrndmax = 2147483561;
|
---|
1453 | public const int hqrndm1 = 2147483563;
|
---|
1454 | public const int hqrndm2 = 2147483399;
|
---|
1455 | public const int hqrndmagic = 1634357784;
|
---|
1456 |
|
---|
1457 |
|
---|
1458 | /*************************************************************************
|
---|
1459 | HQRNDState initialization with random values which come from standard
|
---|
1460 | RNG.
|
---|
1461 |
|
---|
1462 | -- ALGLIB --
|
---|
1463 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
1464 | *************************************************************************/
|
---|
1465 | public static void hqrndrandomize(hqrndstate state)
|
---|
1466 | {
|
---|
1467 | int s0 = 0;
|
---|
1468 | int s1 = 0;
|
---|
1469 |
|
---|
1470 | s0 = math.randominteger(hqrndm1);
|
---|
1471 | s1 = math.randominteger(hqrndm2);
|
---|
1472 | hqrndseed(s0, s1, state);
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 |
|
---|
1476 | /*************************************************************************
|
---|
1477 | HQRNDState initialization with seed values
|
---|
1478 |
|
---|
1479 | -- ALGLIB --
|
---|
1480 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
1481 | *************************************************************************/
|
---|
1482 | public static void hqrndseed(int s1,
|
---|
1483 | int s2,
|
---|
1484 | hqrndstate state)
|
---|
1485 | {
|
---|
1486 |
|
---|
1487 | //
|
---|
1488 | // Protection against negative seeds:
|
---|
1489 | //
|
---|
1490 | // SEED := -(SEED+1)
|
---|
1491 | //
|
---|
1492 | // We can use just "-SEED" because there exists such integer number N
|
---|
1493 | // that N<0, -N=N<0 too. (This number is equal to 0x800...000). Need
|
---|
1494 | // to handle such seed correctly forces us to use a bit complicated
|
---|
1495 | // formula.
|
---|
1496 | //
|
---|
1497 | if( s1<0 )
|
---|
1498 | {
|
---|
1499 | s1 = -(s1+1);
|
---|
1500 | }
|
---|
1501 | if( s2<0 )
|
---|
1502 | {
|
---|
1503 | s2 = -(s2+1);
|
---|
1504 | }
|
---|
1505 | state.s1 = s1%(hqrndm1-1)+1;
|
---|
1506 | state.s2 = s2%(hqrndm2-1)+1;
|
---|
1507 | state.magicv = hqrndmagic;
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 |
|
---|
1511 | /*************************************************************************
|
---|
1512 | This function generates random real number in (0,1),
|
---|
1513 | not including interval boundaries
|
---|
1514 |
|
---|
1515 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1516 |
|
---|
1517 | -- ALGLIB --
|
---|
1518 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
1519 | *************************************************************************/
|
---|
1520 | public static double hqrnduniformr(hqrndstate state)
|
---|
1521 | {
|
---|
1522 | double result = 0;
|
---|
1523 |
|
---|
1524 | result = (double)(hqrndintegerbase(state)+1)/(double)(hqrndmax+2);
|
---|
1525 | return result;
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 |
|
---|
1529 | /*************************************************************************
|
---|
1530 | This function generates random integer number in [0, N)
|
---|
1531 |
|
---|
1532 | 1. State structure must be initialized with HQRNDRandomize() or HQRNDSeed()
|
---|
1533 | 2. N can be any positive number except for very large numbers:
|
---|
1534 | * close to 2^31 on 32-bit systems
|
---|
1535 | * close to 2^62 on 64-bit systems
|
---|
1536 | An exception will be generated if N is too large.
|
---|
1537 |
|
---|
1538 | -- ALGLIB --
|
---|
1539 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
1540 | *************************************************************************/
|
---|
1541 | public static int hqrnduniformi(hqrndstate state,
|
---|
1542 | int n)
|
---|
1543 | {
|
---|
1544 | int result = 0;
|
---|
1545 | int maxcnt = 0;
|
---|
1546 | int mx = 0;
|
---|
1547 | int a = 0;
|
---|
1548 | int b = 0;
|
---|
1549 |
|
---|
1550 | alglib.ap.assert(n>0, "HQRNDUniformI: N<=0!");
|
---|
1551 | maxcnt = hqrndmax+1;
|
---|
1552 |
|
---|
1553 | //
|
---|
1554 | // Two branches: one for N<=MaxCnt, another for N>MaxCnt.
|
---|
1555 | //
|
---|
1556 | if( n>maxcnt )
|
---|
1557 | {
|
---|
1558 |
|
---|
1559 | //
|
---|
1560 | // N>=MaxCnt.
|
---|
1561 | //
|
---|
1562 | // We have two options here:
|
---|
1563 | // a) N is exactly divisible by MaxCnt
|
---|
1564 | // b) N is not divisible by MaxCnt
|
---|
1565 | //
|
---|
1566 | // In both cases we reduce problem on interval spanning [0,N)
|
---|
1567 | // to several subproblems on intervals spanning [0,MaxCnt).
|
---|
1568 | //
|
---|
1569 | if( n%maxcnt==0 )
|
---|
1570 | {
|
---|
1571 |
|
---|
1572 | //
|
---|
1573 | // N is exactly divisible by MaxCnt.
|
---|
1574 | //
|
---|
1575 | // [0,N) range is dividided into N/MaxCnt bins,
|
---|
1576 | // each of them having length equal to MaxCnt.
|
---|
1577 | //
|
---|
1578 | // We generate:
|
---|
1579 | // * random bin number B
|
---|
1580 | // * random offset within bin A
|
---|
1581 | // Both random numbers are generated by recursively
|
---|
1582 | // calling HQRNDUniformI().
|
---|
1583 | //
|
---|
1584 | // Result is equal to A+MaxCnt*B.
|
---|
1585 | //
|
---|
1586 | alglib.ap.assert(n/maxcnt<=maxcnt, "HQRNDUniformI: N is too large");
|
---|
1587 | a = hqrnduniformi(state, maxcnt);
|
---|
1588 | b = hqrnduniformi(state, n/maxcnt);
|
---|
1589 | result = a+maxcnt*b;
|
---|
1590 | }
|
---|
1591 | else
|
---|
1592 | {
|
---|
1593 |
|
---|
1594 | //
|
---|
1595 | // N is NOT exactly divisible by MaxCnt.
|
---|
1596 | //
|
---|
1597 | // [0,N) range is dividided into Ceil(N/MaxCnt) bins,
|
---|
1598 | // each of them having length equal to MaxCnt.
|
---|
1599 | //
|
---|
1600 | // We generate:
|
---|
1601 | // * random bin number B in [0, Ceil(N/MaxCnt)-1]
|
---|
1602 | // * random offset within bin A
|
---|
1603 | // * if both of what is below is true
|
---|
1604 | // 1) bin number B is that of the last bin
|
---|
1605 | // 2) A >= N mod MaxCnt
|
---|
1606 | // then we repeat generation of A/B.
|
---|
1607 | // This stage is essential in order to avoid bias in the result.
|
---|
1608 | // * otherwise, we return A*MaxCnt+N
|
---|
1609 | //
|
---|
1610 | alglib.ap.assert(n/maxcnt+1<=maxcnt, "HQRNDUniformI: N is too large");
|
---|
1611 | result = -1;
|
---|
1612 | do
|
---|
1613 | {
|
---|
1614 | a = hqrnduniformi(state, maxcnt);
|
---|
1615 | b = hqrnduniformi(state, n/maxcnt+1);
|
---|
1616 | if( b==n/maxcnt && a>=n%maxcnt )
|
---|
1617 | {
|
---|
1618 | continue;
|
---|
1619 | }
|
---|
1620 | result = a+maxcnt*b;
|
---|
1621 | }
|
---|
1622 | while( result<0 );
|
---|
1623 | }
|
---|
1624 | }
|
---|
1625 | else
|
---|
1626 | {
|
---|
1627 |
|
---|
1628 | //
|
---|
1629 | // N<=MaxCnt
|
---|
1630 | //
|
---|
1631 | // Code below is a bit complicated because we can not simply
|
---|
1632 | // return "HQRNDIntegerBase() mod N" - it will be skewed for
|
---|
1633 | // large N's in [0.1*HQRNDMax...HQRNDMax].
|
---|
1634 | //
|
---|
1635 | mx = maxcnt-maxcnt%n;
|
---|
1636 | do
|
---|
1637 | {
|
---|
1638 | result = hqrndintegerbase(state);
|
---|
1639 | }
|
---|
1640 | while( result>=mx );
|
---|
1641 | result = result%n;
|
---|
1642 | }
|
---|
1643 | return result;
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 |
|
---|
1647 | /*************************************************************************
|
---|
1648 | Random number generator: normal numbers
|
---|
1649 |
|
---|
1650 | This function generates one random number from normal distribution.
|
---|
1651 | Its performance is equal to that of HQRNDNormal2()
|
---|
1652 |
|
---|
1653 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1654 |
|
---|
1655 | -- ALGLIB --
|
---|
1656 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
1657 | *************************************************************************/
|
---|
1658 | public static double hqrndnormal(hqrndstate state)
|
---|
1659 | {
|
---|
1660 | double result = 0;
|
---|
1661 | double v1 = 0;
|
---|
1662 | double v2 = 0;
|
---|
1663 |
|
---|
1664 | hqrndnormal2(state, ref v1, ref v2);
|
---|
1665 | result = v1;
|
---|
1666 | return result;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 |
|
---|
1670 | /*************************************************************************
|
---|
1671 | Random number generator: random X and Y such that X^2+Y^2=1
|
---|
1672 |
|
---|
1673 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1674 |
|
---|
1675 | -- ALGLIB --
|
---|
1676 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
1677 | *************************************************************************/
|
---|
1678 | public static void hqrndunit2(hqrndstate state,
|
---|
1679 | ref double x,
|
---|
1680 | ref double y)
|
---|
1681 | {
|
---|
1682 | double v = 0;
|
---|
1683 | double mx = 0;
|
---|
1684 | double mn = 0;
|
---|
1685 |
|
---|
1686 | x = 0;
|
---|
1687 | y = 0;
|
---|
1688 |
|
---|
1689 | do
|
---|
1690 | {
|
---|
1691 | hqrndnormal2(state, ref x, ref y);
|
---|
1692 | }
|
---|
1693 | while( !((double)(x)!=(double)(0) || (double)(y)!=(double)(0)) );
|
---|
1694 | mx = Math.Max(Math.Abs(x), Math.Abs(y));
|
---|
1695 | mn = Math.Min(Math.Abs(x), Math.Abs(y));
|
---|
1696 | v = mx*Math.Sqrt(1+math.sqr(mn/mx));
|
---|
1697 | x = x/v;
|
---|
1698 | y = y/v;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 |
|
---|
1702 | /*************************************************************************
|
---|
1703 | Random number generator: normal numbers
|
---|
1704 |
|
---|
1705 | This function generates two independent random numbers from normal
|
---|
1706 | distribution. Its performance is equal to that of HQRNDNormal()
|
---|
1707 |
|
---|
1708 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1709 |
|
---|
1710 | -- ALGLIB --
|
---|
1711 | Copyright 02.12.2009 by Bochkanov Sergey
|
---|
1712 | *************************************************************************/
|
---|
1713 | public static void hqrndnormal2(hqrndstate state,
|
---|
1714 | ref double x1,
|
---|
1715 | ref double x2)
|
---|
1716 | {
|
---|
1717 | double u = 0;
|
---|
1718 | double v = 0;
|
---|
1719 | double s = 0;
|
---|
1720 |
|
---|
1721 | x1 = 0;
|
---|
1722 | x2 = 0;
|
---|
1723 |
|
---|
1724 | while( true )
|
---|
1725 | {
|
---|
1726 | u = 2*hqrnduniformr(state)-1;
|
---|
1727 | v = 2*hqrnduniformr(state)-1;
|
---|
1728 | s = math.sqr(u)+math.sqr(v);
|
---|
1729 | if( (double)(s)>(double)(0) && (double)(s)<(double)(1) )
|
---|
1730 | {
|
---|
1731 |
|
---|
1732 | //
|
---|
1733 | // two Sqrt's instead of one to
|
---|
1734 | // avoid overflow when S is too small
|
---|
1735 | //
|
---|
1736 | s = Math.Sqrt(-(2*Math.Log(s)))/Math.Sqrt(s);
|
---|
1737 | x1 = u*s;
|
---|
1738 | x2 = v*s;
|
---|
1739 | return;
|
---|
1740 | }
|
---|
1741 | }
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 |
|
---|
1745 | /*************************************************************************
|
---|
1746 | Random number generator: exponential distribution
|
---|
1747 |
|
---|
1748 | State structure must be initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1749 |
|
---|
1750 | -- ALGLIB --
|
---|
1751 | Copyright 11.08.2007 by Bochkanov Sergey
|
---|
1752 | *************************************************************************/
|
---|
1753 | public static double hqrndexponential(hqrndstate state,
|
---|
1754 | double lambdav)
|
---|
1755 | {
|
---|
1756 | double result = 0;
|
---|
1757 |
|
---|
1758 | alglib.ap.assert((double)(lambdav)>(double)(0), "HQRNDExponential: LambdaV<=0!");
|
---|
1759 | result = -(Math.Log(hqrnduniformr(state))/lambdav);
|
---|
1760 | return result;
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 |
|
---|
1764 | /*************************************************************************
|
---|
1765 | This function generates random number from discrete distribution given by
|
---|
1766 | finite sample X.
|
---|
1767 |
|
---|
1768 | INPUT PARAMETERS
|
---|
1769 | State - high quality random number generator, must be
|
---|
1770 | initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1771 | X - finite sample
|
---|
1772 | N - number of elements to use, N>=1
|
---|
1773 |
|
---|
1774 | RESULT
|
---|
1775 | this function returns one of the X[i] for random i=0..N-1
|
---|
1776 |
|
---|
1777 | -- ALGLIB --
|
---|
1778 | Copyright 08.11.2011 by Bochkanov Sergey
|
---|
1779 | *************************************************************************/
|
---|
1780 | public static double hqrnddiscrete(hqrndstate state,
|
---|
1781 | double[] x,
|
---|
1782 | int n)
|
---|
1783 | {
|
---|
1784 | double result = 0;
|
---|
1785 |
|
---|
1786 | alglib.ap.assert(n>0, "HQRNDDiscrete: N<=0");
|
---|
1787 | alglib.ap.assert(n<=alglib.ap.len(x), "HQRNDDiscrete: Length(X)<N");
|
---|
1788 | result = x[hqrnduniformi(state, n)];
|
---|
1789 | return result;
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 |
|
---|
1793 | /*************************************************************************
|
---|
1794 | This function generates random number from continuous distribution given
|
---|
1795 | by finite sample X.
|
---|
1796 |
|
---|
1797 | INPUT PARAMETERS
|
---|
1798 | State - high quality random number generator, must be
|
---|
1799 | initialized with HQRNDRandomize() or HQRNDSeed().
|
---|
1800 | X - finite sample, array[N] (can be larger, in this case only
|
---|
1801 | leading N elements are used). THIS ARRAY MUST BE SORTED BY
|
---|
1802 | ASCENDING.
|
---|
1803 | N - number of elements to use, N>=1
|
---|
1804 |
|
---|
1805 | RESULT
|
---|
1806 | this function returns random number from continuous distribution which
|
---|
1807 | tries to approximate X as mush as possible. min(X)<=Result<=max(X).
|
---|
1808 |
|
---|
1809 | -- ALGLIB --
|
---|
1810 | Copyright 08.11.2011 by Bochkanov Sergey
|
---|
1811 | *************************************************************************/
|
---|
1812 | public static double hqrndcontinuous(hqrndstate state,
|
---|
1813 | double[] x,
|
---|
1814 | int n)
|
---|
1815 | {
|
---|
1816 | double result = 0;
|
---|
1817 | double mx = 0;
|
---|
1818 | double mn = 0;
|
---|
1819 | int i = 0;
|
---|
1820 |
|
---|
1821 | alglib.ap.assert(n>0, "HQRNDContinuous: N<=0");
|
---|
1822 | alglib.ap.assert(n<=alglib.ap.len(x), "HQRNDContinuous: Length(X)<N");
|
---|
1823 | if( n==1 )
|
---|
1824 | {
|
---|
1825 | result = x[0];
|
---|
1826 | return result;
|
---|
1827 | }
|
---|
1828 | i = hqrnduniformi(state, n-1);
|
---|
1829 | mn = x[i];
|
---|
1830 | mx = x[i+1];
|
---|
1831 | alglib.ap.assert((double)(mx)>=(double)(mn), "HQRNDDiscrete: X is not sorted by ascending");
|
---|
1832 | if( (double)(mx)!=(double)(mn) )
|
---|
1833 | {
|
---|
1834 | result = (mx-mn)*hqrnduniformr(state)+mn;
|
---|
1835 | }
|
---|
1836 | else
|
---|
1837 | {
|
---|
1838 | result = mn;
|
---|
1839 | }
|
---|
1840 | return result;
|
---|
1841 | }
|
---|
1842 |
|
---|
1843 |
|
---|
1844 | /*************************************************************************
|
---|
1845 | This function returns random integer in [0,HQRNDMax]
|
---|
1846 |
|
---|
1847 | L'Ecuyer, Efficient and portable combined random number generators
|
---|
1848 | *************************************************************************/
|
---|
1849 | private static int hqrndintegerbase(hqrndstate state)
|
---|
1850 | {
|
---|
1851 | int result = 0;
|
---|
1852 | int k = 0;
|
---|
1853 |
|
---|
1854 | alglib.ap.assert(state.magicv==hqrndmagic, "HQRNDIntegerBase: State is not correctly initialized!");
|
---|
1855 | k = state.s1/53668;
|
---|
1856 | state.s1 = 40014*(state.s1-k*53668)-k*12211;
|
---|
1857 | if( state.s1<0 )
|
---|
1858 | {
|
---|
1859 | state.s1 = state.s1+2147483563;
|
---|
1860 | }
|
---|
1861 | k = state.s2/52774;
|
---|
1862 | state.s2 = 40692*(state.s2-k*52774)-k*3791;
|
---|
1863 | if( state.s2<0 )
|
---|
1864 | {
|
---|
1865 | state.s2 = state.s2+2147483399;
|
---|
1866 | }
|
---|
1867 |
|
---|
1868 | //
|
---|
1869 | // Result
|
---|
1870 | //
|
---|
1871 | result = state.s1-state.s2;
|
---|
1872 | if( result<1 )
|
---|
1873 | {
|
---|
1874 | result = result+2147483562;
|
---|
1875 | }
|
---|
1876 | result = result-1;
|
---|
1877 | return result;
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 |
|
---|
1881 | }
|
---|
1882 | public class nearestneighbor
|
---|
1883 | {
|
---|
1884 | public class kdtree : apobject
|
---|
1885 | {
|
---|
1886 | public int n;
|
---|
1887 | public int nx;
|
---|
1888 | public int ny;
|
---|
1889 | public int normtype;
|
---|
1890 | public double[,] xy;
|
---|
1891 | public int[] tags;
|
---|
1892 | public double[] boxmin;
|
---|
1893 | public double[] boxmax;
|
---|
1894 | public int[] nodes;
|
---|
1895 | public double[] splits;
|
---|
1896 | public double[] x;
|
---|
1897 | public int kneeded;
|
---|
1898 | public double rneeded;
|
---|
1899 | public bool selfmatch;
|
---|
1900 | public double approxf;
|
---|
1901 | public int kcur;
|
---|
1902 | public int[] idx;
|
---|
1903 | public double[] r;
|
---|
1904 | public double[] buf;
|
---|
1905 | public double[] curboxmin;
|
---|
1906 | public double[] curboxmax;
|
---|
1907 | public double curdist;
|
---|
1908 | public int debugcounter;
|
---|
1909 | public kdtree()
|
---|
1910 | {
|
---|
1911 | init();
|
---|
1912 | }
|
---|
1913 | public override void init()
|
---|
1914 | {
|
---|
1915 | xy = new double[0,0];
|
---|
1916 | tags = new int[0];
|
---|
1917 | boxmin = new double[0];
|
---|
1918 | boxmax = new double[0];
|
---|
1919 | nodes = new int[0];
|
---|
1920 | splits = new double[0];
|
---|
1921 | x = new double[0];
|
---|
1922 | idx = new int[0];
|
---|
1923 | r = new double[0];
|
---|
1924 | buf = new double[0];
|
---|
1925 | curboxmin = new double[0];
|
---|
1926 | curboxmax = new double[0];
|
---|
1927 | }
|
---|
1928 | public override alglib.apobject make_copy()
|
---|
1929 | {
|
---|
1930 | kdtree _result = new kdtree();
|
---|
1931 | _result.n = n;
|
---|
1932 | _result.nx = nx;
|
---|
1933 | _result.ny = ny;
|
---|
1934 | _result.normtype = normtype;
|
---|
1935 | _result.xy = (double[,])xy.Clone();
|
---|
1936 | _result.tags = (int[])tags.Clone();
|
---|
1937 | _result.boxmin = (double[])boxmin.Clone();
|
---|
1938 | _result.boxmax = (double[])boxmax.Clone();
|
---|
1939 | _result.nodes = (int[])nodes.Clone();
|
---|
1940 | _result.splits = (double[])splits.Clone();
|
---|
1941 | _result.x = (double[])x.Clone();
|
---|
1942 | _result.kneeded = kneeded;
|
---|
1943 | _result.rneeded = rneeded;
|
---|
1944 | _result.selfmatch = selfmatch;
|
---|
1945 | _result.approxf = approxf;
|
---|
1946 | _result.kcur = kcur;
|
---|
1947 | _result.idx = (int[])idx.Clone();
|
---|
1948 | _result.r = (double[])r.Clone();
|
---|
1949 | _result.buf = (double[])buf.Clone();
|
---|
1950 | _result.curboxmin = (double[])curboxmin.Clone();
|
---|
1951 | _result.curboxmax = (double[])curboxmax.Clone();
|
---|
1952 | _result.curdist = curdist;
|
---|
1953 | _result.debugcounter = debugcounter;
|
---|
1954 | return _result;
|
---|
1955 | }
|
---|
1956 | };
|
---|
1957 |
|
---|
1958 |
|
---|
1959 |
|
---|
1960 |
|
---|
1961 | public const int splitnodesize = 6;
|
---|
1962 | public const int kdtreefirstversion = 0;
|
---|
1963 |
|
---|
1964 |
|
---|
1965 | /*************************************************************************
|
---|
1966 | KD-tree creation
|
---|
1967 |
|
---|
1968 | This subroutine creates KD-tree from set of X-values and optional Y-values
|
---|
1969 |
|
---|
1970 | INPUT PARAMETERS
|
---|
1971 | XY - dataset, array[0..N-1,0..NX+NY-1].
|
---|
1972 | one row corresponds to one point.
|
---|
1973 | first NX columns contain X-values, next NY (NY may be zero)
|
---|
1974 | columns may contain associated Y-values
|
---|
1975 | N - number of points, N>=0.
|
---|
1976 | NX - space dimension, NX>=1.
|
---|
1977 | NY - number of optional Y-values, NY>=0.
|
---|
1978 | NormType- norm type:
|
---|
1979 | * 0 denotes infinity-norm
|
---|
1980 | * 1 denotes 1-norm
|
---|
1981 | * 2 denotes 2-norm (Euclidean norm)
|
---|
1982 |
|
---|
1983 | OUTPUT PARAMETERS
|
---|
1984 | KDT - KD-tree
|
---|
1985 |
|
---|
1986 |
|
---|
1987 | NOTES
|
---|
1988 |
|
---|
1989 | 1. KD-tree creation have O(N*logN) complexity and O(N*(2*NX+NY)) memory
|
---|
1990 | requirements.
|
---|
1991 | 2. Although KD-trees may be used with any combination of N and NX, they
|
---|
1992 | are more efficient than brute-force search only when N >> 4^NX. So they
|
---|
1993 | are most useful in low-dimensional tasks (NX=2, NX=3). NX=1 is another
|
---|
1994 | inefficient case, because simple binary search (without additional
|
---|
1995 | structures) is much more efficient in such tasks than KD-trees.
|
---|
1996 |
|
---|
1997 | -- ALGLIB --
|
---|
1998 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
1999 | *************************************************************************/
|
---|
2000 | public static void kdtreebuild(double[,] xy,
|
---|
2001 | int n,
|
---|
2002 | int nx,
|
---|
2003 | int ny,
|
---|
2004 | int normtype,
|
---|
2005 | kdtree kdt)
|
---|
2006 | {
|
---|
2007 | int[] tags = new int[0];
|
---|
2008 | int i = 0;
|
---|
2009 |
|
---|
2010 | alglib.ap.assert(n>=0, "KDTreeBuild: N<0");
|
---|
2011 | alglib.ap.assert(nx>=1, "KDTreeBuild: NX<1");
|
---|
2012 | alglib.ap.assert(ny>=0, "KDTreeBuild: NY<0");
|
---|
2013 | alglib.ap.assert(normtype>=0 && normtype<=2, "KDTreeBuild: incorrect NormType");
|
---|
2014 | alglib.ap.assert(alglib.ap.rows(xy)>=n, "KDTreeBuild: rows(X)<N");
|
---|
2015 | alglib.ap.assert(alglib.ap.cols(xy)>=nx+ny || n==0, "KDTreeBuild: cols(X)<NX+NY");
|
---|
2016 | alglib.ap.assert(apserv.apservisfinitematrix(xy, n, nx+ny), "KDTreeBuild: XY contains infinite or NaN values");
|
---|
2017 | if( n>0 )
|
---|
2018 | {
|
---|
2019 | tags = new int[n];
|
---|
2020 | for(i=0; i<=n-1; i++)
|
---|
2021 | {
|
---|
2022 | tags[i] = 0;
|
---|
2023 | }
|
---|
2024 | }
|
---|
2025 | kdtreebuildtagged(xy, tags, n, nx, ny, normtype, kdt);
|
---|
2026 | }
|
---|
2027 |
|
---|
2028 |
|
---|
2029 | /*************************************************************************
|
---|
2030 | KD-tree creation
|
---|
2031 |
|
---|
2032 | This subroutine creates KD-tree from set of X-values, integer tags and
|
---|
2033 | optional Y-values
|
---|
2034 |
|
---|
2035 | INPUT PARAMETERS
|
---|
2036 | XY - dataset, array[0..N-1,0..NX+NY-1].
|
---|
2037 | one row corresponds to one point.
|
---|
2038 | first NX columns contain X-values, next NY (NY may be zero)
|
---|
2039 | columns may contain associated Y-values
|
---|
2040 | Tags - tags, array[0..N-1], contains integer tags associated
|
---|
2041 | with points.
|
---|
2042 | N - number of points, N>=0
|
---|
2043 | NX - space dimension, NX>=1.
|
---|
2044 | NY - number of optional Y-values, NY>=0.
|
---|
2045 | NormType- norm type:
|
---|
2046 | * 0 denotes infinity-norm
|
---|
2047 | * 1 denotes 1-norm
|
---|
2048 | * 2 denotes 2-norm (Euclidean norm)
|
---|
2049 |
|
---|
2050 | OUTPUT PARAMETERS
|
---|
2051 | KDT - KD-tree
|
---|
2052 |
|
---|
2053 | NOTES
|
---|
2054 |
|
---|
2055 | 1. KD-tree creation have O(N*logN) complexity and O(N*(2*NX+NY)) memory
|
---|
2056 | requirements.
|
---|
2057 | 2. Although KD-trees may be used with any combination of N and NX, they
|
---|
2058 | are more efficient than brute-force search only when N >> 4^NX. So they
|
---|
2059 | are most useful in low-dimensional tasks (NX=2, NX=3). NX=1 is another
|
---|
2060 | inefficient case, because simple binary search (without additional
|
---|
2061 | structures) is much more efficient in such tasks than KD-trees.
|
---|
2062 |
|
---|
2063 | -- ALGLIB --
|
---|
2064 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2065 | *************************************************************************/
|
---|
2066 | public static void kdtreebuildtagged(double[,] xy,
|
---|
2067 | int[] tags,
|
---|
2068 | int n,
|
---|
2069 | int nx,
|
---|
2070 | int ny,
|
---|
2071 | int normtype,
|
---|
2072 | kdtree kdt)
|
---|
2073 | {
|
---|
2074 | int i = 0;
|
---|
2075 | int j = 0;
|
---|
2076 | int maxnodes = 0;
|
---|
2077 | int nodesoffs = 0;
|
---|
2078 | int splitsoffs = 0;
|
---|
2079 | int i_ = 0;
|
---|
2080 | int i1_ = 0;
|
---|
2081 |
|
---|
2082 | alglib.ap.assert(n>=0, "KDTreeBuildTagged: N<0");
|
---|
2083 | alglib.ap.assert(nx>=1, "KDTreeBuildTagged: NX<1");
|
---|
2084 | alglib.ap.assert(ny>=0, "KDTreeBuildTagged: NY<0");
|
---|
2085 | alglib.ap.assert(normtype>=0 && normtype<=2, "KDTreeBuildTagged: incorrect NormType");
|
---|
2086 | alglib.ap.assert(alglib.ap.rows(xy)>=n, "KDTreeBuildTagged: rows(X)<N");
|
---|
2087 | alglib.ap.assert(alglib.ap.cols(xy)>=nx+ny || n==0, "KDTreeBuildTagged: cols(X)<NX+NY");
|
---|
2088 | alglib.ap.assert(apserv.apservisfinitematrix(xy, n, nx+ny), "KDTreeBuildTagged: XY contains infinite or NaN values");
|
---|
2089 |
|
---|
2090 | //
|
---|
2091 | // initialize
|
---|
2092 | //
|
---|
2093 | kdt.n = n;
|
---|
2094 | kdt.nx = nx;
|
---|
2095 | kdt.ny = ny;
|
---|
2096 | kdt.normtype = normtype;
|
---|
2097 | kdt.kcur = 0;
|
---|
2098 |
|
---|
2099 | //
|
---|
2100 | // N=0 => quick exit
|
---|
2101 | //
|
---|
2102 | if( n==0 )
|
---|
2103 | {
|
---|
2104 | return;
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | //
|
---|
2108 | // Allocate
|
---|
2109 | //
|
---|
2110 | kdtreeallocdatasetindependent(kdt, nx, ny);
|
---|
2111 | kdtreeallocdatasetdependent(kdt, n, nx, ny);
|
---|
2112 |
|
---|
2113 | //
|
---|
2114 | // Initial fill
|
---|
2115 | //
|
---|
2116 | for(i=0; i<=n-1; i++)
|
---|
2117 | {
|
---|
2118 | for(i_=0; i_<=nx-1;i_++)
|
---|
2119 | {
|
---|
2120 | kdt.xy[i,i_] = xy[i,i_];
|
---|
2121 | }
|
---|
2122 | i1_ = (0) - (nx);
|
---|
2123 | for(i_=nx; i_<=2*nx+ny-1;i_++)
|
---|
2124 | {
|
---|
2125 | kdt.xy[i,i_] = xy[i,i_+i1_];
|
---|
2126 | }
|
---|
2127 | kdt.tags[i] = tags[i];
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | //
|
---|
2131 | // Determine bounding box
|
---|
2132 | //
|
---|
2133 | for(i_=0; i_<=nx-1;i_++)
|
---|
2134 | {
|
---|
2135 | kdt.boxmin[i_] = kdt.xy[0,i_];
|
---|
2136 | }
|
---|
2137 | for(i_=0; i_<=nx-1;i_++)
|
---|
2138 | {
|
---|
2139 | kdt.boxmax[i_] = kdt.xy[0,i_];
|
---|
2140 | }
|
---|
2141 | for(i=1; i<=n-1; i++)
|
---|
2142 | {
|
---|
2143 | for(j=0; j<=nx-1; j++)
|
---|
2144 | {
|
---|
2145 | kdt.boxmin[j] = Math.Min(kdt.boxmin[j], kdt.xy[i,j]);
|
---|
2146 | kdt.boxmax[j] = Math.Max(kdt.boxmax[j], kdt.xy[i,j]);
|
---|
2147 | }
|
---|
2148 | }
|
---|
2149 |
|
---|
2150 | //
|
---|
2151 | // prepare tree structure
|
---|
2152 | // * MaxNodes=N because we guarantee no trivial splits, i.e.
|
---|
2153 | // every split will generate two non-empty boxes
|
---|
2154 | //
|
---|
2155 | maxnodes = n;
|
---|
2156 | kdt.nodes = new int[splitnodesize*2*maxnodes];
|
---|
2157 | kdt.splits = new double[2*maxnodes];
|
---|
2158 | nodesoffs = 0;
|
---|
2159 | splitsoffs = 0;
|
---|
2160 | for(i_=0; i_<=nx-1;i_++)
|
---|
2161 | {
|
---|
2162 | kdt.curboxmin[i_] = kdt.boxmin[i_];
|
---|
2163 | }
|
---|
2164 | for(i_=0; i_<=nx-1;i_++)
|
---|
2165 | {
|
---|
2166 | kdt.curboxmax[i_] = kdt.boxmax[i_];
|
---|
2167 | }
|
---|
2168 | kdtreegeneratetreerec(kdt, ref nodesoffs, ref splitsoffs, 0, n, 8);
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 |
|
---|
2172 | /*************************************************************************
|
---|
2173 | K-NN query: K nearest neighbors
|
---|
2174 |
|
---|
2175 | INPUT PARAMETERS
|
---|
2176 | KDT - KD-tree
|
---|
2177 | X - point, array[0..NX-1].
|
---|
2178 | K - number of neighbors to return, K>=1
|
---|
2179 | SelfMatch - whether self-matches are allowed:
|
---|
2180 | * if True, nearest neighbor may be the point itself
|
---|
2181 | (if it exists in original dataset)
|
---|
2182 | * if False, then only points with non-zero distance
|
---|
2183 | are returned
|
---|
2184 | * if not given, considered True
|
---|
2185 |
|
---|
2186 | RESULT
|
---|
2187 | number of actual neighbors found (either K or N, if K>N).
|
---|
2188 |
|
---|
2189 | This subroutine performs query and stores its result in the internal
|
---|
2190 | structures of the KD-tree. You can use following subroutines to obtain
|
---|
2191 | these results:
|
---|
2192 | * KDTreeQueryResultsX() to get X-values
|
---|
2193 | * KDTreeQueryResultsXY() to get X- and Y-values
|
---|
2194 | * KDTreeQueryResultsTags() to get tag values
|
---|
2195 | * KDTreeQueryResultsDistances() to get distances
|
---|
2196 |
|
---|
2197 | -- ALGLIB --
|
---|
2198 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2199 | *************************************************************************/
|
---|
2200 | public static int kdtreequeryknn(kdtree kdt,
|
---|
2201 | double[] x,
|
---|
2202 | int k,
|
---|
2203 | bool selfmatch)
|
---|
2204 | {
|
---|
2205 | int result = 0;
|
---|
2206 |
|
---|
2207 | alglib.ap.assert(k>=1, "KDTreeQueryKNN: K<1!");
|
---|
2208 | alglib.ap.assert(alglib.ap.len(x)>=kdt.nx, "KDTreeQueryKNN: Length(X)<NX!");
|
---|
2209 | alglib.ap.assert(apserv.isfinitevector(x, kdt.nx), "KDTreeQueryKNN: X contains infinite or NaN values!");
|
---|
2210 | result = kdtreequeryaknn(kdt, x, k, selfmatch, 0.0);
|
---|
2211 | return result;
|
---|
2212 | }
|
---|
2213 |
|
---|
2214 |
|
---|
2215 | /*************************************************************************
|
---|
2216 | R-NN query: all points within R-sphere centered at X
|
---|
2217 |
|
---|
2218 | INPUT PARAMETERS
|
---|
2219 | KDT - KD-tree
|
---|
2220 | X - point, array[0..NX-1].
|
---|
2221 | R - radius of sphere (in corresponding norm), R>0
|
---|
2222 | SelfMatch - whether self-matches are allowed:
|
---|
2223 | * if True, nearest neighbor may be the point itself
|
---|
2224 | (if it exists in original dataset)
|
---|
2225 | * if False, then only points with non-zero distance
|
---|
2226 | are returned
|
---|
2227 | * if not given, considered True
|
---|
2228 |
|
---|
2229 | RESULT
|
---|
2230 | number of neighbors found, >=0
|
---|
2231 |
|
---|
2232 | This subroutine performs query and stores its result in the internal
|
---|
2233 | structures of the KD-tree. You can use following subroutines to obtain
|
---|
2234 | actual results:
|
---|
2235 | * KDTreeQueryResultsX() to get X-values
|
---|
2236 | * KDTreeQueryResultsXY() to get X- and Y-values
|
---|
2237 | * KDTreeQueryResultsTags() to get tag values
|
---|
2238 | * KDTreeQueryResultsDistances() to get distances
|
---|
2239 |
|
---|
2240 | -- ALGLIB --
|
---|
2241 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2242 | *************************************************************************/
|
---|
2243 | public static int kdtreequeryrnn(kdtree kdt,
|
---|
2244 | double[] x,
|
---|
2245 | double r,
|
---|
2246 | bool selfmatch)
|
---|
2247 | {
|
---|
2248 | int result = 0;
|
---|
2249 | int i = 0;
|
---|
2250 | int j = 0;
|
---|
2251 |
|
---|
2252 | alglib.ap.assert((double)(r)>(double)(0), "KDTreeQueryRNN: incorrect R!");
|
---|
2253 | alglib.ap.assert(alglib.ap.len(x)>=kdt.nx, "KDTreeQueryRNN: Length(X)<NX!");
|
---|
2254 | alglib.ap.assert(apserv.isfinitevector(x, kdt.nx), "KDTreeQueryRNN: X contains infinite or NaN values!");
|
---|
2255 |
|
---|
2256 | //
|
---|
2257 | // Handle special case: KDT.N=0
|
---|
2258 | //
|
---|
2259 | if( kdt.n==0 )
|
---|
2260 | {
|
---|
2261 | kdt.kcur = 0;
|
---|
2262 | result = 0;
|
---|
2263 | return result;
|
---|
2264 | }
|
---|
2265 |
|
---|
2266 | //
|
---|
2267 | // Prepare parameters
|
---|
2268 | //
|
---|
2269 | kdt.kneeded = 0;
|
---|
2270 | if( kdt.normtype!=2 )
|
---|
2271 | {
|
---|
2272 | kdt.rneeded = r;
|
---|
2273 | }
|
---|
2274 | else
|
---|
2275 | {
|
---|
2276 | kdt.rneeded = math.sqr(r);
|
---|
2277 | }
|
---|
2278 | kdt.selfmatch = selfmatch;
|
---|
2279 | kdt.approxf = 1;
|
---|
2280 | kdt.kcur = 0;
|
---|
2281 |
|
---|
2282 | //
|
---|
2283 | // calculate distance from point to current bounding box
|
---|
2284 | //
|
---|
2285 | kdtreeinitbox(kdt, x);
|
---|
2286 |
|
---|
2287 | //
|
---|
2288 | // call recursive search
|
---|
2289 | // results are returned as heap
|
---|
2290 | //
|
---|
2291 | kdtreequerynnrec(kdt, 0);
|
---|
2292 |
|
---|
2293 | //
|
---|
2294 | // pop from heap to generate ordered representation
|
---|
2295 | //
|
---|
2296 | // last element is not pop'ed because it is already in
|
---|
2297 | // its place
|
---|
2298 | //
|
---|
2299 | result = kdt.kcur;
|
---|
2300 | j = kdt.kcur;
|
---|
2301 | for(i=kdt.kcur; i>=2; i--)
|
---|
2302 | {
|
---|
2303 | tsort.tagheappopi(ref kdt.r, ref kdt.idx, ref j);
|
---|
2304 | }
|
---|
2305 | return result;
|
---|
2306 | }
|
---|
2307 |
|
---|
2308 |
|
---|
2309 | /*************************************************************************
|
---|
2310 | K-NN query: approximate K nearest neighbors
|
---|
2311 |
|
---|
2312 | INPUT PARAMETERS
|
---|
2313 | KDT - KD-tree
|
---|
2314 | X - point, array[0..NX-1].
|
---|
2315 | K - number of neighbors to return, K>=1
|
---|
2316 | SelfMatch - whether self-matches are allowed:
|
---|
2317 | * if True, nearest neighbor may be the point itself
|
---|
2318 | (if it exists in original dataset)
|
---|
2319 | * if False, then only points with non-zero distance
|
---|
2320 | are returned
|
---|
2321 | * if not given, considered True
|
---|
2322 | Eps - approximation factor, Eps>=0. eps-approximate nearest
|
---|
2323 | neighbor is a neighbor whose distance from X is at
|
---|
2324 | most (1+eps) times distance of true nearest neighbor.
|
---|
2325 |
|
---|
2326 | RESULT
|
---|
2327 | number of actual neighbors found (either K or N, if K>N).
|
---|
2328 |
|
---|
2329 | NOTES
|
---|
2330 | significant performance gain may be achieved only when Eps is is on
|
---|
2331 | the order of magnitude of 1 or larger.
|
---|
2332 |
|
---|
2333 | This subroutine performs query and stores its result in the internal
|
---|
2334 | structures of the KD-tree. You can use following subroutines to obtain
|
---|
2335 | these results:
|
---|
2336 | * KDTreeQueryResultsX() to get X-values
|
---|
2337 | * KDTreeQueryResultsXY() to get X- and Y-values
|
---|
2338 | * KDTreeQueryResultsTags() to get tag values
|
---|
2339 | * KDTreeQueryResultsDistances() to get distances
|
---|
2340 |
|
---|
2341 | -- ALGLIB --
|
---|
2342 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2343 | *************************************************************************/
|
---|
2344 | public static int kdtreequeryaknn(kdtree kdt,
|
---|
2345 | double[] x,
|
---|
2346 | int k,
|
---|
2347 | bool selfmatch,
|
---|
2348 | double eps)
|
---|
2349 | {
|
---|
2350 | int result = 0;
|
---|
2351 | int i = 0;
|
---|
2352 | int j = 0;
|
---|
2353 |
|
---|
2354 | alglib.ap.assert(k>0, "KDTreeQueryAKNN: incorrect K!");
|
---|
2355 | alglib.ap.assert((double)(eps)>=(double)(0), "KDTreeQueryAKNN: incorrect Eps!");
|
---|
2356 | alglib.ap.assert(alglib.ap.len(x)>=kdt.nx, "KDTreeQueryAKNN: Length(X)<NX!");
|
---|
2357 | alglib.ap.assert(apserv.isfinitevector(x, kdt.nx), "KDTreeQueryAKNN: X contains infinite or NaN values!");
|
---|
2358 |
|
---|
2359 | //
|
---|
2360 | // Handle special case: KDT.N=0
|
---|
2361 | //
|
---|
2362 | if( kdt.n==0 )
|
---|
2363 | {
|
---|
2364 | kdt.kcur = 0;
|
---|
2365 | result = 0;
|
---|
2366 | return result;
|
---|
2367 | }
|
---|
2368 |
|
---|
2369 | //
|
---|
2370 | // Prepare parameters
|
---|
2371 | //
|
---|
2372 | k = Math.Min(k, kdt.n);
|
---|
2373 | kdt.kneeded = k;
|
---|
2374 | kdt.rneeded = 0;
|
---|
2375 | kdt.selfmatch = selfmatch;
|
---|
2376 | if( kdt.normtype==2 )
|
---|
2377 | {
|
---|
2378 | kdt.approxf = 1/math.sqr(1+eps);
|
---|
2379 | }
|
---|
2380 | else
|
---|
2381 | {
|
---|
2382 | kdt.approxf = 1/(1+eps);
|
---|
2383 | }
|
---|
2384 | kdt.kcur = 0;
|
---|
2385 |
|
---|
2386 | //
|
---|
2387 | // calculate distance from point to current bounding box
|
---|
2388 | //
|
---|
2389 | kdtreeinitbox(kdt, x);
|
---|
2390 |
|
---|
2391 | //
|
---|
2392 | // call recursive search
|
---|
2393 | // results are returned as heap
|
---|
2394 | //
|
---|
2395 | kdtreequerynnrec(kdt, 0);
|
---|
2396 |
|
---|
2397 | //
|
---|
2398 | // pop from heap to generate ordered representation
|
---|
2399 | //
|
---|
2400 | // last element is non pop'ed because it is already in
|
---|
2401 | // its place
|
---|
2402 | //
|
---|
2403 | result = kdt.kcur;
|
---|
2404 | j = kdt.kcur;
|
---|
2405 | for(i=kdt.kcur; i>=2; i--)
|
---|
2406 | {
|
---|
2407 | tsort.tagheappopi(ref kdt.r, ref kdt.idx, ref j);
|
---|
2408 | }
|
---|
2409 | return result;
|
---|
2410 | }
|
---|
2411 |
|
---|
2412 |
|
---|
2413 | /*************************************************************************
|
---|
2414 | X-values from last query
|
---|
2415 |
|
---|
2416 | INPUT PARAMETERS
|
---|
2417 | KDT - KD-tree
|
---|
2418 | X - possibly pre-allocated buffer. If X is too small to store
|
---|
2419 | result, it is resized. If size(X) is enough to store
|
---|
2420 | result, it is left unchanged.
|
---|
2421 |
|
---|
2422 | OUTPUT PARAMETERS
|
---|
2423 | X - rows are filled with X-values
|
---|
2424 |
|
---|
2425 | NOTES
|
---|
2426 | 1. points are ordered by distance from the query point (first = closest)
|
---|
2427 | 2. if XY is larger than required to store result, only leading part will
|
---|
2428 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
2429 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
2430 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
2431 | you want function to resize array according to result size, use
|
---|
2432 | function with same name and suffix 'I'.
|
---|
2433 |
|
---|
2434 | SEE ALSO
|
---|
2435 | * KDTreeQueryResultsXY() X- and Y-values
|
---|
2436 | * KDTreeQueryResultsTags() tag values
|
---|
2437 | * KDTreeQueryResultsDistances() distances
|
---|
2438 |
|
---|
2439 | -- ALGLIB --
|
---|
2440 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2441 | *************************************************************************/
|
---|
2442 | public static void kdtreequeryresultsx(kdtree kdt,
|
---|
2443 | ref double[,] x)
|
---|
2444 | {
|
---|
2445 | int i = 0;
|
---|
2446 | int k = 0;
|
---|
2447 | int i_ = 0;
|
---|
2448 | int i1_ = 0;
|
---|
2449 |
|
---|
2450 | if( kdt.kcur==0 )
|
---|
2451 | {
|
---|
2452 | return;
|
---|
2453 | }
|
---|
2454 | if( alglib.ap.rows(x)<kdt.kcur || alglib.ap.cols(x)<kdt.nx )
|
---|
2455 | {
|
---|
2456 | x = new double[kdt.kcur, kdt.nx];
|
---|
2457 | }
|
---|
2458 | k = kdt.kcur;
|
---|
2459 | for(i=0; i<=k-1; i++)
|
---|
2460 | {
|
---|
2461 | i1_ = (kdt.nx) - (0);
|
---|
2462 | for(i_=0; i_<=kdt.nx-1;i_++)
|
---|
2463 | {
|
---|
2464 | x[i,i_] = kdt.xy[kdt.idx[i],i_+i1_];
|
---|
2465 | }
|
---|
2466 | }
|
---|
2467 | }
|
---|
2468 |
|
---|
2469 |
|
---|
2470 | /*************************************************************************
|
---|
2471 | X- and Y-values from last query
|
---|
2472 |
|
---|
2473 | INPUT PARAMETERS
|
---|
2474 | KDT - KD-tree
|
---|
2475 | XY - possibly pre-allocated buffer. If XY is too small to store
|
---|
2476 | result, it is resized. If size(XY) is enough to store
|
---|
2477 | result, it is left unchanged.
|
---|
2478 |
|
---|
2479 | OUTPUT PARAMETERS
|
---|
2480 | XY - rows are filled with points: first NX columns with
|
---|
2481 | X-values, next NY columns - with Y-values.
|
---|
2482 |
|
---|
2483 | NOTES
|
---|
2484 | 1. points are ordered by distance from the query point (first = closest)
|
---|
2485 | 2. if XY is larger than required to store result, only leading part will
|
---|
2486 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
2487 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
2488 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
2489 | you want function to resize array according to result size, use
|
---|
2490 | function with same name and suffix 'I'.
|
---|
2491 |
|
---|
2492 | SEE ALSO
|
---|
2493 | * KDTreeQueryResultsX() X-values
|
---|
2494 | * KDTreeQueryResultsTags() tag values
|
---|
2495 | * KDTreeQueryResultsDistances() distances
|
---|
2496 |
|
---|
2497 | -- ALGLIB --
|
---|
2498 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2499 | *************************************************************************/
|
---|
2500 | public static void kdtreequeryresultsxy(kdtree kdt,
|
---|
2501 | ref double[,] xy)
|
---|
2502 | {
|
---|
2503 | int i = 0;
|
---|
2504 | int k = 0;
|
---|
2505 | int i_ = 0;
|
---|
2506 | int i1_ = 0;
|
---|
2507 |
|
---|
2508 | if( kdt.kcur==0 )
|
---|
2509 | {
|
---|
2510 | return;
|
---|
2511 | }
|
---|
2512 | if( alglib.ap.rows(xy)<kdt.kcur || alglib.ap.cols(xy)<kdt.nx+kdt.ny )
|
---|
2513 | {
|
---|
2514 | xy = new double[kdt.kcur, kdt.nx+kdt.ny];
|
---|
2515 | }
|
---|
2516 | k = kdt.kcur;
|
---|
2517 | for(i=0; i<=k-1; i++)
|
---|
2518 | {
|
---|
2519 | i1_ = (kdt.nx) - (0);
|
---|
2520 | for(i_=0; i_<=kdt.nx+kdt.ny-1;i_++)
|
---|
2521 | {
|
---|
2522 | xy[i,i_] = kdt.xy[kdt.idx[i],i_+i1_];
|
---|
2523 | }
|
---|
2524 | }
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 |
|
---|
2528 | /*************************************************************************
|
---|
2529 | Tags from last query
|
---|
2530 |
|
---|
2531 | INPUT PARAMETERS
|
---|
2532 | KDT - KD-tree
|
---|
2533 | Tags - possibly pre-allocated buffer. If X is too small to store
|
---|
2534 | result, it is resized. If size(X) is enough to store
|
---|
2535 | result, it is left unchanged.
|
---|
2536 |
|
---|
2537 | OUTPUT PARAMETERS
|
---|
2538 | Tags - filled with tags associated with points,
|
---|
2539 | or, when no tags were supplied, with zeros
|
---|
2540 |
|
---|
2541 | NOTES
|
---|
2542 | 1. points are ordered by distance from the query point (first = closest)
|
---|
2543 | 2. if XY is larger than required to store result, only leading part will
|
---|
2544 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
2545 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
2546 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
2547 | you want function to resize array according to result size, use
|
---|
2548 | function with same name and suffix 'I'.
|
---|
2549 |
|
---|
2550 | SEE ALSO
|
---|
2551 | * KDTreeQueryResultsX() X-values
|
---|
2552 | * KDTreeQueryResultsXY() X- and Y-values
|
---|
2553 | * KDTreeQueryResultsDistances() distances
|
---|
2554 |
|
---|
2555 | -- ALGLIB --
|
---|
2556 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2557 | *************************************************************************/
|
---|
2558 | public static void kdtreequeryresultstags(kdtree kdt,
|
---|
2559 | ref int[] tags)
|
---|
2560 | {
|
---|
2561 | int i = 0;
|
---|
2562 | int k = 0;
|
---|
2563 |
|
---|
2564 | if( kdt.kcur==0 )
|
---|
2565 | {
|
---|
2566 | return;
|
---|
2567 | }
|
---|
2568 | if( alglib.ap.len(tags)<kdt.kcur )
|
---|
2569 | {
|
---|
2570 | tags = new int[kdt.kcur];
|
---|
2571 | }
|
---|
2572 | k = kdt.kcur;
|
---|
2573 | for(i=0; i<=k-1; i++)
|
---|
2574 | {
|
---|
2575 | tags[i] = kdt.tags[kdt.idx[i]];
|
---|
2576 | }
|
---|
2577 | }
|
---|
2578 |
|
---|
2579 |
|
---|
2580 | /*************************************************************************
|
---|
2581 | Distances from last query
|
---|
2582 |
|
---|
2583 | INPUT PARAMETERS
|
---|
2584 | KDT - KD-tree
|
---|
2585 | R - possibly pre-allocated buffer. If X is too small to store
|
---|
2586 | result, it is resized. If size(X) is enough to store
|
---|
2587 | result, it is left unchanged.
|
---|
2588 |
|
---|
2589 | OUTPUT PARAMETERS
|
---|
2590 | R - filled with distances (in corresponding norm)
|
---|
2591 |
|
---|
2592 | NOTES
|
---|
2593 | 1. points are ordered by distance from the query point (first = closest)
|
---|
2594 | 2. if XY is larger than required to store result, only leading part will
|
---|
2595 | be overwritten; trailing part will be left unchanged. So if on input
|
---|
2596 | XY = [[A,B],[C,D]], and result is [1,2], then on exit we will get
|
---|
2597 | XY = [[1,2],[C,D]]. This is done purposely to increase performance; if
|
---|
2598 | you want function to resize array according to result size, use
|
---|
2599 | function with same name and suffix 'I'.
|
---|
2600 |
|
---|
2601 | SEE ALSO
|
---|
2602 | * KDTreeQueryResultsX() X-values
|
---|
2603 | * KDTreeQueryResultsXY() X- and Y-values
|
---|
2604 | * KDTreeQueryResultsTags() tag values
|
---|
2605 |
|
---|
2606 | -- ALGLIB --
|
---|
2607 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2608 | *************************************************************************/
|
---|
2609 | public static void kdtreequeryresultsdistances(kdtree kdt,
|
---|
2610 | ref double[] r)
|
---|
2611 | {
|
---|
2612 | int i = 0;
|
---|
2613 | int k = 0;
|
---|
2614 |
|
---|
2615 | if( kdt.kcur==0 )
|
---|
2616 | {
|
---|
2617 | return;
|
---|
2618 | }
|
---|
2619 | if( alglib.ap.len(r)<kdt.kcur )
|
---|
2620 | {
|
---|
2621 | r = new double[kdt.kcur];
|
---|
2622 | }
|
---|
2623 | k = kdt.kcur;
|
---|
2624 |
|
---|
2625 | //
|
---|
2626 | // unload norms
|
---|
2627 | //
|
---|
2628 | // Abs() call is used to handle cases with negative norms
|
---|
2629 | // (generated during KFN requests)
|
---|
2630 | //
|
---|
2631 | if( kdt.normtype==0 )
|
---|
2632 | {
|
---|
2633 | for(i=0; i<=k-1; i++)
|
---|
2634 | {
|
---|
2635 | r[i] = Math.Abs(kdt.r[i]);
|
---|
2636 | }
|
---|
2637 | }
|
---|
2638 | if( kdt.normtype==1 )
|
---|
2639 | {
|
---|
2640 | for(i=0; i<=k-1; i++)
|
---|
2641 | {
|
---|
2642 | r[i] = Math.Abs(kdt.r[i]);
|
---|
2643 | }
|
---|
2644 | }
|
---|
2645 | if( kdt.normtype==2 )
|
---|
2646 | {
|
---|
2647 | for(i=0; i<=k-1; i++)
|
---|
2648 | {
|
---|
2649 | r[i] = Math.Sqrt(Math.Abs(kdt.r[i]));
|
---|
2650 | }
|
---|
2651 | }
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 |
|
---|
2655 | /*************************************************************************
|
---|
2656 | X-values from last query; 'interactive' variant for languages like Python
|
---|
2657 | which support constructs like "X = KDTreeQueryResultsXI(KDT)" and
|
---|
2658 | interactive mode of interpreter.
|
---|
2659 |
|
---|
2660 | This function allocates new array on each call, so it is significantly
|
---|
2661 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
2662 | when you call it from command line.
|
---|
2663 |
|
---|
2664 | -- ALGLIB --
|
---|
2665 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2666 | *************************************************************************/
|
---|
2667 | public static void kdtreequeryresultsxi(kdtree kdt,
|
---|
2668 | ref double[,] x)
|
---|
2669 | {
|
---|
2670 | x = new double[0,0];
|
---|
2671 |
|
---|
2672 | kdtreequeryresultsx(kdt, ref x);
|
---|
2673 | }
|
---|
2674 |
|
---|
2675 |
|
---|
2676 | /*************************************************************************
|
---|
2677 | XY-values from last query; 'interactive' variant for languages like Python
|
---|
2678 | which support constructs like "XY = KDTreeQueryResultsXYI(KDT)" and
|
---|
2679 | interactive mode of interpreter.
|
---|
2680 |
|
---|
2681 | This function allocates new array on each call, so it is significantly
|
---|
2682 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
2683 | when you call it from command line.
|
---|
2684 |
|
---|
2685 | -- ALGLIB --
|
---|
2686 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2687 | *************************************************************************/
|
---|
2688 | public static void kdtreequeryresultsxyi(kdtree kdt,
|
---|
2689 | ref double[,] xy)
|
---|
2690 | {
|
---|
2691 | xy = new double[0,0];
|
---|
2692 |
|
---|
2693 | kdtreequeryresultsxy(kdt, ref xy);
|
---|
2694 | }
|
---|
2695 |
|
---|
2696 |
|
---|
2697 | /*************************************************************************
|
---|
2698 | Tags from last query; 'interactive' variant for languages like Python
|
---|
2699 | which support constructs like "Tags = KDTreeQueryResultsTagsI(KDT)" and
|
---|
2700 | interactive mode of interpreter.
|
---|
2701 |
|
---|
2702 | This function allocates new array on each call, so it is significantly
|
---|
2703 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
2704 | when you call it from command line.
|
---|
2705 |
|
---|
2706 | -- ALGLIB --
|
---|
2707 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2708 | *************************************************************************/
|
---|
2709 | public static void kdtreequeryresultstagsi(kdtree kdt,
|
---|
2710 | ref int[] tags)
|
---|
2711 | {
|
---|
2712 | tags = new int[0];
|
---|
2713 |
|
---|
2714 | kdtreequeryresultstags(kdt, ref tags);
|
---|
2715 | }
|
---|
2716 |
|
---|
2717 |
|
---|
2718 | /*************************************************************************
|
---|
2719 | Distances from last query; 'interactive' variant for languages like Python
|
---|
2720 | which support constructs like "R = KDTreeQueryResultsDistancesI(KDT)"
|
---|
2721 | and interactive mode of interpreter.
|
---|
2722 |
|
---|
2723 | This function allocates new array on each call, so it is significantly
|
---|
2724 | slower than its 'non-interactive' counterpart, but it is more convenient
|
---|
2725 | when you call it from command line.
|
---|
2726 |
|
---|
2727 | -- ALGLIB --
|
---|
2728 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2729 | *************************************************************************/
|
---|
2730 | public static void kdtreequeryresultsdistancesi(kdtree kdt,
|
---|
2731 | ref double[] r)
|
---|
2732 | {
|
---|
2733 | r = new double[0];
|
---|
2734 |
|
---|
2735 | kdtreequeryresultsdistances(kdt, ref r);
|
---|
2736 | }
|
---|
2737 |
|
---|
2738 |
|
---|
2739 | /*************************************************************************
|
---|
2740 | Serializer: allocation
|
---|
2741 |
|
---|
2742 | -- ALGLIB --
|
---|
2743 | Copyright 14.03.2011 by Bochkanov Sergey
|
---|
2744 | *************************************************************************/
|
---|
2745 | public static void kdtreealloc(alglib.serializer s,
|
---|
2746 | kdtree tree)
|
---|
2747 | {
|
---|
2748 |
|
---|
2749 | //
|
---|
2750 | // Header
|
---|
2751 | //
|
---|
2752 | s.alloc_entry();
|
---|
2753 | s.alloc_entry();
|
---|
2754 |
|
---|
2755 | //
|
---|
2756 | // Data
|
---|
2757 | //
|
---|
2758 | s.alloc_entry();
|
---|
2759 | s.alloc_entry();
|
---|
2760 | s.alloc_entry();
|
---|
2761 | s.alloc_entry();
|
---|
2762 | apserv.allocrealmatrix(s, tree.xy, -1, -1);
|
---|
2763 | apserv.allocintegerarray(s, tree.tags, -1);
|
---|
2764 | apserv.allocrealarray(s, tree.boxmin, -1);
|
---|
2765 | apserv.allocrealarray(s, tree.boxmax, -1);
|
---|
2766 | apserv.allocintegerarray(s, tree.nodes, -1);
|
---|
2767 | apserv.allocrealarray(s, tree.splits, -1);
|
---|
2768 | }
|
---|
2769 |
|
---|
2770 |
|
---|
2771 | /*************************************************************************
|
---|
2772 | Serializer: serialization
|
---|
2773 |
|
---|
2774 | -- ALGLIB --
|
---|
2775 | Copyright 14.03.2011 by Bochkanov Sergey
|
---|
2776 | *************************************************************************/
|
---|
2777 | public static void kdtreeserialize(alglib.serializer s,
|
---|
2778 | kdtree tree)
|
---|
2779 | {
|
---|
2780 |
|
---|
2781 | //
|
---|
2782 | // Header
|
---|
2783 | //
|
---|
2784 | s.serialize_int(scodes.getkdtreeserializationcode());
|
---|
2785 | s.serialize_int(kdtreefirstversion);
|
---|
2786 |
|
---|
2787 | //
|
---|
2788 | // Data
|
---|
2789 | //
|
---|
2790 | s.serialize_int(tree.n);
|
---|
2791 | s.serialize_int(tree.nx);
|
---|
2792 | s.serialize_int(tree.ny);
|
---|
2793 | s.serialize_int(tree.normtype);
|
---|
2794 | apserv.serializerealmatrix(s, tree.xy, -1, -1);
|
---|
2795 | apserv.serializeintegerarray(s, tree.tags, -1);
|
---|
2796 | apserv.serializerealarray(s, tree.boxmin, -1);
|
---|
2797 | apserv.serializerealarray(s, tree.boxmax, -1);
|
---|
2798 | apserv.serializeintegerarray(s, tree.nodes, -1);
|
---|
2799 | apserv.serializerealarray(s, tree.splits, -1);
|
---|
2800 | }
|
---|
2801 |
|
---|
2802 |
|
---|
2803 | /*************************************************************************
|
---|
2804 | Serializer: unserialization
|
---|
2805 |
|
---|
2806 | -- ALGLIB --
|
---|
2807 | Copyright 14.03.2011 by Bochkanov Sergey
|
---|
2808 | *************************************************************************/
|
---|
2809 | public static void kdtreeunserialize(alglib.serializer s,
|
---|
2810 | kdtree tree)
|
---|
2811 | {
|
---|
2812 | int i0 = 0;
|
---|
2813 | int i1 = 0;
|
---|
2814 |
|
---|
2815 |
|
---|
2816 | //
|
---|
2817 | // check correctness of header
|
---|
2818 | //
|
---|
2819 | i0 = s.unserialize_int();
|
---|
2820 | alglib.ap.assert(i0==scodes.getkdtreeserializationcode(), "KDTreeUnserialize: stream header corrupted");
|
---|
2821 | i1 = s.unserialize_int();
|
---|
2822 | alglib.ap.assert(i1==kdtreefirstversion, "KDTreeUnserialize: stream header corrupted");
|
---|
2823 |
|
---|
2824 | //
|
---|
2825 | // Unserialize data
|
---|
2826 | //
|
---|
2827 | tree.n = s.unserialize_int();
|
---|
2828 | tree.nx = s.unserialize_int();
|
---|
2829 | tree.ny = s.unserialize_int();
|
---|
2830 | tree.normtype = s.unserialize_int();
|
---|
2831 | apserv.unserializerealmatrix(s, ref tree.xy);
|
---|
2832 | apserv.unserializeintegerarray(s, ref tree.tags);
|
---|
2833 | apserv.unserializerealarray(s, ref tree.boxmin);
|
---|
2834 | apserv.unserializerealarray(s, ref tree.boxmax);
|
---|
2835 | apserv.unserializeintegerarray(s, ref tree.nodes);
|
---|
2836 | apserv.unserializerealarray(s, ref tree.splits);
|
---|
2837 | kdtreealloctemporaries(tree, tree.n, tree.nx, tree.ny);
|
---|
2838 | }
|
---|
2839 |
|
---|
2840 |
|
---|
2841 | /*************************************************************************
|
---|
2842 | Rearranges nodes [I1,I2) using partition in D-th dimension with S as threshold.
|
---|
2843 | Returns split position I3: [I1,I3) and [I3,I2) are created as result.
|
---|
2844 |
|
---|
2845 | This subroutine doesn't create tree structures, just rearranges nodes.
|
---|
2846 | *************************************************************************/
|
---|
2847 | private static void kdtreesplit(kdtree kdt,
|
---|
2848 | int i1,
|
---|
2849 | int i2,
|
---|
2850 | int d,
|
---|
2851 | double s,
|
---|
2852 | ref int i3)
|
---|
2853 | {
|
---|
2854 | int i = 0;
|
---|
2855 | int j = 0;
|
---|
2856 | int ileft = 0;
|
---|
2857 | int iright = 0;
|
---|
2858 | double v = 0;
|
---|
2859 |
|
---|
2860 | i3 = 0;
|
---|
2861 |
|
---|
2862 | alglib.ap.assert(kdt.n>0, "KDTreeSplit: internal error");
|
---|
2863 |
|
---|
2864 | //
|
---|
2865 | // split XY/Tags in two parts:
|
---|
2866 | // * [ILeft,IRight] is non-processed part of XY/Tags
|
---|
2867 | //
|
---|
2868 | // After cycle is done, we have Ileft=IRight. We deal with
|
---|
2869 | // this element separately.
|
---|
2870 | //
|
---|
2871 | // After this, [I1,ILeft) contains left part, and [ILeft,I2)
|
---|
2872 | // contains right part.
|
---|
2873 | //
|
---|
2874 | ileft = i1;
|
---|
2875 | iright = i2-1;
|
---|
2876 | while( ileft<iright )
|
---|
2877 | {
|
---|
2878 | if( (double)(kdt.xy[ileft,d])<=(double)(s) )
|
---|
2879 | {
|
---|
2880 |
|
---|
2881 | //
|
---|
2882 | // XY[ILeft] is on its place.
|
---|
2883 | // Advance ILeft.
|
---|
2884 | //
|
---|
2885 | ileft = ileft+1;
|
---|
2886 | }
|
---|
2887 | else
|
---|
2888 | {
|
---|
2889 |
|
---|
2890 | //
|
---|
2891 | // XY[ILeft,..] must be at IRight.
|
---|
2892 | // Swap and advance IRight.
|
---|
2893 | //
|
---|
2894 | for(i=0; i<=2*kdt.nx+kdt.ny-1; i++)
|
---|
2895 | {
|
---|
2896 | v = kdt.xy[ileft,i];
|
---|
2897 | kdt.xy[ileft,i] = kdt.xy[iright,i];
|
---|
2898 | kdt.xy[iright,i] = v;
|
---|
2899 | }
|
---|
2900 | j = kdt.tags[ileft];
|
---|
2901 | kdt.tags[ileft] = kdt.tags[iright];
|
---|
2902 | kdt.tags[iright] = j;
|
---|
2903 | iright = iright-1;
|
---|
2904 | }
|
---|
2905 | }
|
---|
2906 | if( (double)(kdt.xy[ileft,d])<=(double)(s) )
|
---|
2907 | {
|
---|
2908 | ileft = ileft+1;
|
---|
2909 | }
|
---|
2910 | else
|
---|
2911 | {
|
---|
2912 | iright = iright-1;
|
---|
2913 | }
|
---|
2914 | i3 = ileft;
|
---|
2915 | }
|
---|
2916 |
|
---|
2917 |
|
---|
2918 | /*************************************************************************
|
---|
2919 | Recursive kd-tree generation subroutine.
|
---|
2920 |
|
---|
2921 | PARAMETERS
|
---|
2922 | KDT tree
|
---|
2923 | NodesOffs unused part of Nodes[] which must be filled by tree
|
---|
2924 | SplitsOffs unused part of Splits[]
|
---|
2925 | I1, I2 points from [I1,I2) are processed
|
---|
2926 |
|
---|
2927 | NodesOffs[] and SplitsOffs[] must be large enough.
|
---|
2928 |
|
---|
2929 | -- ALGLIB --
|
---|
2930 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
2931 | *************************************************************************/
|
---|
2932 | private static void kdtreegeneratetreerec(kdtree kdt,
|
---|
2933 | ref int nodesoffs,
|
---|
2934 | ref int splitsoffs,
|
---|
2935 | int i1,
|
---|
2936 | int i2,
|
---|
2937 | int maxleafsize)
|
---|
2938 | {
|
---|
2939 | int n = 0;
|
---|
2940 | int nx = 0;
|
---|
2941 | int ny = 0;
|
---|
2942 | int i = 0;
|
---|
2943 | int j = 0;
|
---|
2944 | int oldoffs = 0;
|
---|
2945 | int i3 = 0;
|
---|
2946 | int cntless = 0;
|
---|
2947 | int cntgreater = 0;
|
---|
2948 | double minv = 0;
|
---|
2949 | double maxv = 0;
|
---|
2950 | int minidx = 0;
|
---|
2951 | int maxidx = 0;
|
---|
2952 | int d = 0;
|
---|
2953 | double ds = 0;
|
---|
2954 | double s = 0;
|
---|
2955 | double v = 0;
|
---|
2956 | double v0 = 0;
|
---|
2957 | double v1 = 0;
|
---|
2958 | int i_ = 0;
|
---|
2959 | int i1_ = 0;
|
---|
2960 |
|
---|
2961 | alglib.ap.assert(kdt.n>0, "KDTreeGenerateTreeRec: internal error");
|
---|
2962 | alglib.ap.assert(i2>i1, "KDTreeGenerateTreeRec: internal error");
|
---|
2963 |
|
---|
2964 | //
|
---|
2965 | // Generate leaf if needed
|
---|
2966 | //
|
---|
2967 | if( i2-i1<=maxleafsize )
|
---|
2968 | {
|
---|
2969 | kdt.nodes[nodesoffs+0] = i2-i1;
|
---|
2970 | kdt.nodes[nodesoffs+1] = i1;
|
---|
2971 | nodesoffs = nodesoffs+2;
|
---|
2972 | return;
|
---|
2973 | }
|
---|
2974 |
|
---|
2975 | //
|
---|
2976 | // Load values for easier access
|
---|
2977 | //
|
---|
2978 | nx = kdt.nx;
|
---|
2979 | ny = kdt.ny;
|
---|
2980 |
|
---|
2981 | //
|
---|
2982 | // Select dimension to split:
|
---|
2983 | // * D is a dimension number
|
---|
2984 | // In case bounding box has zero size, we enforce creation of the leaf node.
|
---|
2985 | //
|
---|
2986 | d = 0;
|
---|
2987 | ds = kdt.curboxmax[0]-kdt.curboxmin[0];
|
---|
2988 | for(i=1; i<=nx-1; i++)
|
---|
2989 | {
|
---|
2990 | v = kdt.curboxmax[i]-kdt.curboxmin[i];
|
---|
2991 | if( (double)(v)>(double)(ds) )
|
---|
2992 | {
|
---|
2993 | ds = v;
|
---|
2994 | d = i;
|
---|
2995 | }
|
---|
2996 | }
|
---|
2997 | if( (double)(ds)==(double)(0) )
|
---|
2998 | {
|
---|
2999 | kdt.nodes[nodesoffs+0] = i2-i1;
|
---|
3000 | kdt.nodes[nodesoffs+1] = i1;
|
---|
3001 | nodesoffs = nodesoffs+2;
|
---|
3002 | return;
|
---|
3003 | }
|
---|
3004 |
|
---|
3005 | //
|
---|
3006 | // Select split position S using sliding midpoint rule,
|
---|
3007 | // rearrange points into [I1,I3) and [I3,I2).
|
---|
3008 | //
|
---|
3009 | // In case all points has same value of D-th component
|
---|
3010 | // (MinV=MaxV) we enforce D-th dimension of bounding
|
---|
3011 | // box to become exactly zero and repeat tree construction.
|
---|
3012 | //
|
---|
3013 | s = kdt.curboxmin[d]+0.5*ds;
|
---|
3014 | i1_ = (i1) - (0);
|
---|
3015 | for(i_=0; i_<=i2-i1-1;i_++)
|
---|
3016 | {
|
---|
3017 | kdt.buf[i_] = kdt.xy[i_+i1_,d];
|
---|
3018 | }
|
---|
3019 | n = i2-i1;
|
---|
3020 | cntless = 0;
|
---|
3021 | cntgreater = 0;
|
---|
3022 | minv = kdt.buf[0];
|
---|
3023 | maxv = kdt.buf[0];
|
---|
3024 | minidx = i1;
|
---|
3025 | maxidx = i1;
|
---|
3026 | for(i=0; i<=n-1; i++)
|
---|
3027 | {
|
---|
3028 | v = kdt.buf[i];
|
---|
3029 | if( (double)(v)<(double)(minv) )
|
---|
3030 | {
|
---|
3031 | minv = v;
|
---|
3032 | minidx = i1+i;
|
---|
3033 | }
|
---|
3034 | if( (double)(v)>(double)(maxv) )
|
---|
3035 | {
|
---|
3036 | maxv = v;
|
---|
3037 | maxidx = i1+i;
|
---|
3038 | }
|
---|
3039 | if( (double)(v)<(double)(s) )
|
---|
3040 | {
|
---|
3041 | cntless = cntless+1;
|
---|
3042 | }
|
---|
3043 | if( (double)(v)>(double)(s) )
|
---|
3044 | {
|
---|
3045 | cntgreater = cntgreater+1;
|
---|
3046 | }
|
---|
3047 | }
|
---|
3048 | if( (double)(minv)==(double)(maxv) )
|
---|
3049 | {
|
---|
3050 |
|
---|
3051 | //
|
---|
3052 | // In case all points has same value of D-th component
|
---|
3053 | // (MinV=MaxV) we enforce D-th dimension of bounding
|
---|
3054 | // box to become exactly zero and repeat tree construction.
|
---|
3055 | //
|
---|
3056 | v0 = kdt.curboxmin[d];
|
---|
3057 | v1 = kdt.curboxmax[d];
|
---|
3058 | kdt.curboxmin[d] = minv;
|
---|
3059 | kdt.curboxmax[d] = maxv;
|
---|
3060 | kdtreegeneratetreerec(kdt, ref nodesoffs, ref splitsoffs, i1, i2, maxleafsize);
|
---|
3061 | kdt.curboxmin[d] = v0;
|
---|
3062 | kdt.curboxmax[d] = v1;
|
---|
3063 | return;
|
---|
3064 | }
|
---|
3065 | if( cntless>0 && cntgreater>0 )
|
---|
3066 | {
|
---|
3067 |
|
---|
3068 | //
|
---|
3069 | // normal midpoint split
|
---|
3070 | //
|
---|
3071 | kdtreesplit(kdt, i1, i2, d, s, ref i3);
|
---|
3072 | }
|
---|
3073 | else
|
---|
3074 | {
|
---|
3075 |
|
---|
3076 | //
|
---|
3077 | // sliding midpoint
|
---|
3078 | //
|
---|
3079 | if( cntless==0 )
|
---|
3080 | {
|
---|
3081 |
|
---|
3082 | //
|
---|
3083 | // 1. move split to MinV,
|
---|
3084 | // 2. place one point to the left bin (move to I1),
|
---|
3085 | // others - to the right bin
|
---|
3086 | //
|
---|
3087 | s = minv;
|
---|
3088 | if( minidx!=i1 )
|
---|
3089 | {
|
---|
3090 | for(i=0; i<=2*nx+ny-1; i++)
|
---|
3091 | {
|
---|
3092 | v = kdt.xy[minidx,i];
|
---|
3093 | kdt.xy[minidx,i] = kdt.xy[i1,i];
|
---|
3094 | kdt.xy[i1,i] = v;
|
---|
3095 | }
|
---|
3096 | j = kdt.tags[minidx];
|
---|
3097 | kdt.tags[minidx] = kdt.tags[i1];
|
---|
3098 | kdt.tags[i1] = j;
|
---|
3099 | }
|
---|
3100 | i3 = i1+1;
|
---|
3101 | }
|
---|
3102 | else
|
---|
3103 | {
|
---|
3104 |
|
---|
3105 | //
|
---|
3106 | // 1. move split to MaxV,
|
---|
3107 | // 2. place one point to the right bin (move to I2-1),
|
---|
3108 | // others - to the left bin
|
---|
3109 | //
|
---|
3110 | s = maxv;
|
---|
3111 | if( maxidx!=i2-1 )
|
---|
3112 | {
|
---|
3113 | for(i=0; i<=2*nx+ny-1; i++)
|
---|
3114 | {
|
---|
3115 | v = kdt.xy[maxidx,i];
|
---|
3116 | kdt.xy[maxidx,i] = kdt.xy[i2-1,i];
|
---|
3117 | kdt.xy[i2-1,i] = v;
|
---|
3118 | }
|
---|
3119 | j = kdt.tags[maxidx];
|
---|
3120 | kdt.tags[maxidx] = kdt.tags[i2-1];
|
---|
3121 | kdt.tags[i2-1] = j;
|
---|
3122 | }
|
---|
3123 | i3 = i2-1;
|
---|
3124 | }
|
---|
3125 | }
|
---|
3126 |
|
---|
3127 | //
|
---|
3128 | // Generate 'split' node
|
---|
3129 | //
|
---|
3130 | kdt.nodes[nodesoffs+0] = 0;
|
---|
3131 | kdt.nodes[nodesoffs+1] = d;
|
---|
3132 | kdt.nodes[nodesoffs+2] = splitsoffs;
|
---|
3133 | kdt.splits[splitsoffs+0] = s;
|
---|
3134 | oldoffs = nodesoffs;
|
---|
3135 | nodesoffs = nodesoffs+splitnodesize;
|
---|
3136 | splitsoffs = splitsoffs+1;
|
---|
3137 |
|
---|
3138 | //
|
---|
3139 | // Recirsive generation:
|
---|
3140 | // * update CurBox
|
---|
3141 | // * call subroutine
|
---|
3142 | // * restore CurBox
|
---|
3143 | //
|
---|
3144 | kdt.nodes[oldoffs+3] = nodesoffs;
|
---|
3145 | v = kdt.curboxmax[d];
|
---|
3146 | kdt.curboxmax[d] = s;
|
---|
3147 | kdtreegeneratetreerec(kdt, ref nodesoffs, ref splitsoffs, i1, i3, maxleafsize);
|
---|
3148 | kdt.curboxmax[d] = v;
|
---|
3149 | kdt.nodes[oldoffs+4] = nodesoffs;
|
---|
3150 | v = kdt.curboxmin[d];
|
---|
3151 | kdt.curboxmin[d] = s;
|
---|
3152 | kdtreegeneratetreerec(kdt, ref nodesoffs, ref splitsoffs, i3, i2, maxleafsize);
|
---|
3153 | kdt.curboxmin[d] = v;
|
---|
3154 | }
|
---|
3155 |
|
---|
3156 |
|
---|
3157 | /*************************************************************************
|
---|
3158 | Recursive subroutine for NN queries.
|
---|
3159 |
|
---|
3160 | -- ALGLIB --
|
---|
3161 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
3162 | *************************************************************************/
|
---|
3163 | private static void kdtreequerynnrec(kdtree kdt,
|
---|
3164 | int offs)
|
---|
3165 | {
|
---|
3166 | double ptdist = 0;
|
---|
3167 | int i = 0;
|
---|
3168 | int j = 0;
|
---|
3169 | int nx = 0;
|
---|
3170 | int i1 = 0;
|
---|
3171 | int i2 = 0;
|
---|
3172 | int d = 0;
|
---|
3173 | double s = 0;
|
---|
3174 | double v = 0;
|
---|
3175 | double t1 = 0;
|
---|
3176 | int childbestoffs = 0;
|
---|
3177 | int childworstoffs = 0;
|
---|
3178 | int childoffs = 0;
|
---|
3179 | double prevdist = 0;
|
---|
3180 | bool todive = new bool();
|
---|
3181 | bool bestisleft = new bool();
|
---|
3182 | bool updatemin = new bool();
|
---|
3183 |
|
---|
3184 | alglib.ap.assert(kdt.n>0, "KDTreeQueryNNRec: internal error");
|
---|
3185 |
|
---|
3186 | //
|
---|
3187 | // Leaf node.
|
---|
3188 | // Process points.
|
---|
3189 | //
|
---|
3190 | if( kdt.nodes[offs]>0 )
|
---|
3191 | {
|
---|
3192 | i1 = kdt.nodes[offs+1];
|
---|
3193 | i2 = i1+kdt.nodes[offs];
|
---|
3194 | for(i=i1; i<=i2-1; i++)
|
---|
3195 | {
|
---|
3196 |
|
---|
3197 | //
|
---|
3198 | // Calculate distance
|
---|
3199 | //
|
---|
3200 | ptdist = 0;
|
---|
3201 | nx = kdt.nx;
|
---|
3202 | if( kdt.normtype==0 )
|
---|
3203 | {
|
---|
3204 | for(j=0; j<=nx-1; j++)
|
---|
3205 | {
|
---|
3206 | ptdist = Math.Max(ptdist, Math.Abs(kdt.xy[i,j]-kdt.x[j]));
|
---|
3207 | }
|
---|
3208 | }
|
---|
3209 | if( kdt.normtype==1 )
|
---|
3210 | {
|
---|
3211 | for(j=0; j<=nx-1; j++)
|
---|
3212 | {
|
---|
3213 | ptdist = ptdist+Math.Abs(kdt.xy[i,j]-kdt.x[j]);
|
---|
3214 | }
|
---|
3215 | }
|
---|
3216 | if( kdt.normtype==2 )
|
---|
3217 | {
|
---|
3218 | for(j=0; j<=nx-1; j++)
|
---|
3219 | {
|
---|
3220 | ptdist = ptdist+math.sqr(kdt.xy[i,j]-kdt.x[j]);
|
---|
3221 | }
|
---|
3222 | }
|
---|
3223 |
|
---|
3224 | //
|
---|
3225 | // Skip points with zero distance if self-matches are turned off
|
---|
3226 | //
|
---|
3227 | if( (double)(ptdist)==(double)(0) && !kdt.selfmatch )
|
---|
3228 | {
|
---|
3229 | continue;
|
---|
3230 | }
|
---|
3231 |
|
---|
3232 | //
|
---|
3233 | // We CAN'T process point if R-criterion isn't satisfied,
|
---|
3234 | // i.e. (RNeeded<>0) AND (PtDist>R).
|
---|
3235 | //
|
---|
3236 | if( (double)(kdt.rneeded)==(double)(0) || (double)(ptdist)<=(double)(kdt.rneeded) )
|
---|
3237 | {
|
---|
3238 |
|
---|
3239 | //
|
---|
3240 | // R-criterion is satisfied, we must either:
|
---|
3241 | // * replace worst point, if (KNeeded<>0) AND (KCur=KNeeded)
|
---|
3242 | // (or skip, if worst point is better)
|
---|
3243 | // * add point without replacement otherwise
|
---|
3244 | //
|
---|
3245 | if( kdt.kcur<kdt.kneeded || kdt.kneeded==0 )
|
---|
3246 | {
|
---|
3247 |
|
---|
3248 | //
|
---|
3249 | // add current point to heap without replacement
|
---|
3250 | //
|
---|
3251 | tsort.tagheappushi(ref kdt.r, ref kdt.idx, ref kdt.kcur, ptdist, i);
|
---|
3252 | }
|
---|
3253 | else
|
---|
3254 | {
|
---|
3255 |
|
---|
3256 | //
|
---|
3257 | // New points are added or not, depending on their distance.
|
---|
3258 | // If added, they replace element at the top of the heap
|
---|
3259 | //
|
---|
3260 | if( (double)(ptdist)<(double)(kdt.r[0]) )
|
---|
3261 | {
|
---|
3262 | if( kdt.kneeded==1 )
|
---|
3263 | {
|
---|
3264 | kdt.idx[0] = i;
|
---|
3265 | kdt.r[0] = ptdist;
|
---|
3266 | }
|
---|
3267 | else
|
---|
3268 | {
|
---|
3269 | tsort.tagheapreplacetopi(ref kdt.r, ref kdt.idx, kdt.kneeded, ptdist, i);
|
---|
3270 | }
|
---|
3271 | }
|
---|
3272 | }
|
---|
3273 | }
|
---|
3274 | }
|
---|
3275 | return;
|
---|
3276 | }
|
---|
3277 |
|
---|
3278 | //
|
---|
3279 | // Simple split
|
---|
3280 | //
|
---|
3281 | if( kdt.nodes[offs]==0 )
|
---|
3282 | {
|
---|
3283 |
|
---|
3284 | //
|
---|
3285 | // Load:
|
---|
3286 | // * D dimension to split
|
---|
3287 | // * S split position
|
---|
3288 | //
|
---|
3289 | d = kdt.nodes[offs+1];
|
---|
3290 | s = kdt.splits[kdt.nodes[offs+2]];
|
---|
3291 |
|
---|
3292 | //
|
---|
3293 | // Calculate:
|
---|
3294 | // * ChildBestOffs child box with best chances
|
---|
3295 | // * ChildWorstOffs child box with worst chances
|
---|
3296 | //
|
---|
3297 | if( (double)(kdt.x[d])<=(double)(s) )
|
---|
3298 | {
|
---|
3299 | childbestoffs = kdt.nodes[offs+3];
|
---|
3300 | childworstoffs = kdt.nodes[offs+4];
|
---|
3301 | bestisleft = true;
|
---|
3302 | }
|
---|
3303 | else
|
---|
3304 | {
|
---|
3305 | childbestoffs = kdt.nodes[offs+4];
|
---|
3306 | childworstoffs = kdt.nodes[offs+3];
|
---|
3307 | bestisleft = false;
|
---|
3308 | }
|
---|
3309 |
|
---|
3310 | //
|
---|
3311 | // Navigate through childs
|
---|
3312 | //
|
---|
3313 | for(i=0; i<=1; i++)
|
---|
3314 | {
|
---|
3315 |
|
---|
3316 | //
|
---|
3317 | // Select child to process:
|
---|
3318 | // * ChildOffs current child offset in Nodes[]
|
---|
3319 | // * UpdateMin whether minimum or maximum value
|
---|
3320 | // of bounding box is changed on update
|
---|
3321 | //
|
---|
3322 | if( i==0 )
|
---|
3323 | {
|
---|
3324 | childoffs = childbestoffs;
|
---|
3325 | updatemin = !bestisleft;
|
---|
3326 | }
|
---|
3327 | else
|
---|
3328 | {
|
---|
3329 | updatemin = bestisleft;
|
---|
3330 | childoffs = childworstoffs;
|
---|
3331 | }
|
---|
3332 |
|
---|
3333 | //
|
---|
3334 | // Update bounding box and current distance
|
---|
3335 | //
|
---|
3336 | if( updatemin )
|
---|
3337 | {
|
---|
3338 | prevdist = kdt.curdist;
|
---|
3339 | t1 = kdt.x[d];
|
---|
3340 | v = kdt.curboxmin[d];
|
---|
3341 | if( (double)(t1)<=(double)(s) )
|
---|
3342 | {
|
---|
3343 | if( kdt.normtype==0 )
|
---|
3344 | {
|
---|
3345 | kdt.curdist = Math.Max(kdt.curdist, s-t1);
|
---|
3346 | }
|
---|
3347 | if( kdt.normtype==1 )
|
---|
3348 | {
|
---|
3349 | kdt.curdist = kdt.curdist-Math.Max(v-t1, 0)+s-t1;
|
---|
3350 | }
|
---|
3351 | if( kdt.normtype==2 )
|
---|
3352 | {
|
---|
3353 | kdt.curdist = kdt.curdist-math.sqr(Math.Max(v-t1, 0))+math.sqr(s-t1);
|
---|
3354 | }
|
---|
3355 | }
|
---|
3356 | kdt.curboxmin[d] = s;
|
---|
3357 | }
|
---|
3358 | else
|
---|
3359 | {
|
---|
3360 | prevdist = kdt.curdist;
|
---|
3361 | t1 = kdt.x[d];
|
---|
3362 | v = kdt.curboxmax[d];
|
---|
3363 | if( (double)(t1)>=(double)(s) )
|
---|
3364 | {
|
---|
3365 | if( kdt.normtype==0 )
|
---|
3366 | {
|
---|
3367 | kdt.curdist = Math.Max(kdt.curdist, t1-s);
|
---|
3368 | }
|
---|
3369 | if( kdt.normtype==1 )
|
---|
3370 | {
|
---|
3371 | kdt.curdist = kdt.curdist-Math.Max(t1-v, 0)+t1-s;
|
---|
3372 | }
|
---|
3373 | if( kdt.normtype==2 )
|
---|
3374 | {
|
---|
3375 | kdt.curdist = kdt.curdist-math.sqr(Math.Max(t1-v, 0))+math.sqr(t1-s);
|
---|
3376 | }
|
---|
3377 | }
|
---|
3378 | kdt.curboxmax[d] = s;
|
---|
3379 | }
|
---|
3380 |
|
---|
3381 | //
|
---|
3382 | // Decide: to dive into cell or not to dive
|
---|
3383 | //
|
---|
3384 | if( (double)(kdt.rneeded)!=(double)(0) && (double)(kdt.curdist)>(double)(kdt.rneeded) )
|
---|
3385 | {
|
---|
3386 | todive = false;
|
---|
3387 | }
|
---|
3388 | else
|
---|
3389 | {
|
---|
3390 | if( kdt.kcur<kdt.kneeded || kdt.kneeded==0 )
|
---|
3391 | {
|
---|
3392 |
|
---|
3393 | //
|
---|
3394 | // KCur<KNeeded (i.e. not all points are found)
|
---|
3395 | //
|
---|
3396 | todive = true;
|
---|
3397 | }
|
---|
3398 | else
|
---|
3399 | {
|
---|
3400 |
|
---|
3401 | //
|
---|
3402 | // KCur=KNeeded, decide to dive or not to dive
|
---|
3403 | // using point position relative to bounding box.
|
---|
3404 | //
|
---|
3405 | todive = (double)(kdt.curdist)<=(double)(kdt.r[0]*kdt.approxf);
|
---|
3406 | }
|
---|
3407 | }
|
---|
3408 | if( todive )
|
---|
3409 | {
|
---|
3410 | kdtreequerynnrec(kdt, childoffs);
|
---|
3411 | }
|
---|
3412 |
|
---|
3413 | //
|
---|
3414 | // Restore bounding box and distance
|
---|
3415 | //
|
---|
3416 | if( updatemin )
|
---|
3417 | {
|
---|
3418 | kdt.curboxmin[d] = v;
|
---|
3419 | }
|
---|
3420 | else
|
---|
3421 | {
|
---|
3422 | kdt.curboxmax[d] = v;
|
---|
3423 | }
|
---|
3424 | kdt.curdist = prevdist;
|
---|
3425 | }
|
---|
3426 | return;
|
---|
3427 | }
|
---|
3428 | }
|
---|
3429 |
|
---|
3430 |
|
---|
3431 | /*************************************************************************
|
---|
3432 | Copies X[] to KDT.X[]
|
---|
3433 | Loads distance from X[] to bounding box.
|
---|
3434 | Initializes CurBox[].
|
---|
3435 |
|
---|
3436 | -- ALGLIB --
|
---|
3437 | Copyright 28.02.2010 by Bochkanov Sergey
|
---|
3438 | *************************************************************************/
|
---|
3439 | private static void kdtreeinitbox(kdtree kdt,
|
---|
3440 | double[] x)
|
---|
3441 | {
|
---|
3442 | int i = 0;
|
---|
3443 | double vx = 0;
|
---|
3444 | double vmin = 0;
|
---|
3445 | double vmax = 0;
|
---|
3446 |
|
---|
3447 | alglib.ap.assert(kdt.n>0, "KDTreeInitBox: internal error");
|
---|
3448 |
|
---|
3449 | //
|
---|
3450 | // calculate distance from point to current bounding box
|
---|
3451 | //
|
---|
3452 | kdt.curdist = 0;
|
---|
3453 | if( kdt.normtype==0 )
|
---|
3454 | {
|
---|
3455 | for(i=0; i<=kdt.nx-1; i++)
|
---|
3456 | {
|
---|
3457 | vx = x[i];
|
---|
3458 | vmin = kdt.boxmin[i];
|
---|
3459 | vmax = kdt.boxmax[i];
|
---|
3460 | kdt.x[i] = vx;
|
---|
3461 | kdt.curboxmin[i] = vmin;
|
---|
3462 | kdt.curboxmax[i] = vmax;
|
---|
3463 | if( (double)(vx)<(double)(vmin) )
|
---|
3464 | {
|
---|
3465 | kdt.curdist = Math.Max(kdt.curdist, vmin-vx);
|
---|
3466 | }
|
---|
3467 | else
|
---|
3468 | {
|
---|
3469 | if( (double)(vx)>(double)(vmax) )
|
---|
3470 | {
|
---|
3471 | kdt.curdist = Math.Max(kdt.curdist, vx-vmax);
|
---|
3472 | }
|
---|
3473 | }
|
---|
3474 | }
|
---|
3475 | }
|
---|
3476 | if( kdt.normtype==1 )
|
---|
3477 | {
|
---|
3478 | for(i=0; i<=kdt.nx-1; i++)
|
---|
3479 | {
|
---|
3480 | vx = x[i];
|
---|
3481 | vmin = kdt.boxmin[i];
|
---|
3482 | vmax = kdt.boxmax[i];
|
---|
3483 | kdt.x[i] = vx;
|
---|
3484 | kdt.curboxmin[i] = vmin;
|
---|
3485 | kdt.curboxmax[i] = vmax;
|
---|
3486 | if( (double)(vx)<(double)(vmin) )
|
---|
3487 | {
|
---|
3488 | kdt.curdist = kdt.curdist+vmin-vx;
|
---|
3489 | }
|
---|
3490 | else
|
---|
3491 | {
|
---|
3492 | if( (double)(vx)>(double)(vmax) )
|
---|
3493 | {
|
---|
3494 | kdt.curdist = kdt.curdist+vx-vmax;
|
---|
3495 | }
|
---|
3496 | }
|
---|
3497 | }
|
---|
3498 | }
|
---|
3499 | if( kdt.normtype==2 )
|
---|
3500 | {
|
---|
3501 | for(i=0; i<=kdt.nx-1; i++)
|
---|
3502 | {
|
---|
3503 | vx = x[i];
|
---|
3504 | vmin = kdt.boxmin[i];
|
---|
3505 | vmax = kdt.boxmax[i];
|
---|
3506 | kdt.x[i] = vx;
|
---|
3507 | kdt.curboxmin[i] = vmin;
|
---|
3508 | kdt.curboxmax[i] = vmax;
|
---|
3509 | if( (double)(vx)<(double)(vmin) )
|
---|
3510 | {
|
---|
3511 | kdt.curdist = kdt.curdist+math.sqr(vmin-vx);
|
---|
3512 | }
|
---|
3513 | else
|
---|
3514 | {
|
---|
3515 | if( (double)(vx)>(double)(vmax) )
|
---|
3516 | {
|
---|
3517 | kdt.curdist = kdt.curdist+math.sqr(vx-vmax);
|
---|
3518 | }
|
---|
3519 | }
|
---|
3520 | }
|
---|
3521 | }
|
---|
3522 | }
|
---|
3523 |
|
---|
3524 |
|
---|
3525 | /*************************************************************************
|
---|
3526 | This function allocates all dataset-independent array fields of KDTree,
|
---|
3527 | i.e. such array fields that their dimensions do not depend on dataset
|
---|
3528 | size.
|
---|
3529 |
|
---|
3530 | This function do not sets KDT.NX or KDT.NY - it just allocates arrays
|
---|
3531 |
|
---|
3532 | -- ALGLIB --
|
---|
3533 | Copyright 14.03.2011 by Bochkanov Sergey
|
---|
3534 | *************************************************************************/
|
---|
3535 | private static void kdtreeallocdatasetindependent(kdtree kdt,
|
---|
3536 | int nx,
|
---|
3537 | int ny)
|
---|
3538 | {
|
---|
3539 | alglib.ap.assert(kdt.n>0, "KDTreeAllocDatasetIndependent: internal error");
|
---|
3540 | kdt.x = new double[nx];
|
---|
3541 | kdt.boxmin = new double[nx];
|
---|
3542 | kdt.boxmax = new double[nx];
|
---|
3543 | kdt.curboxmin = new double[nx];
|
---|
3544 | kdt.curboxmax = new double[nx];
|
---|
3545 | }
|
---|
3546 |
|
---|
3547 |
|
---|
3548 | /*************************************************************************
|
---|
3549 | This function allocates all dataset-dependent array fields of KDTree, i.e.
|
---|
3550 | such array fields that their dimensions depend on dataset size.
|
---|
3551 |
|
---|
3552 | This function do not sets KDT.N, KDT.NX or KDT.NY -
|
---|
3553 | it just allocates arrays.
|
---|
3554 |
|
---|
3555 | -- ALGLIB --
|
---|
3556 | Copyright 14.03.2011 by Bochkanov Sergey
|
---|
3557 | *************************************************************************/
|
---|
3558 | private static void kdtreeallocdatasetdependent(kdtree kdt,
|
---|
3559 | int n,
|
---|
3560 | int nx,
|
---|
3561 | int ny)
|
---|
3562 | {
|
---|
3563 | alglib.ap.assert(n>0, "KDTreeAllocDatasetDependent: internal error");
|
---|
3564 | kdt.xy = new double[n, 2*nx+ny];
|
---|
3565 | kdt.tags = new int[n];
|
---|
3566 | kdt.idx = new int[n];
|
---|
3567 | kdt.r = new double[n];
|
---|
3568 | kdt.x = new double[nx];
|
---|
3569 | kdt.buf = new double[Math.Max(n, nx)];
|
---|
3570 | kdt.nodes = new int[splitnodesize*2*n];
|
---|
3571 | kdt.splits = new double[2*n];
|
---|
3572 | }
|
---|
3573 |
|
---|
3574 |
|
---|
3575 | /*************************************************************************
|
---|
3576 | This function allocates temporaries.
|
---|
3577 |
|
---|
3578 | This function do not sets KDT.N, KDT.NX or KDT.NY -
|
---|
3579 | it just allocates arrays.
|
---|
3580 |
|
---|
3581 | -- ALGLIB --
|
---|
3582 | Copyright 14.03.2011 by Bochkanov Sergey
|
---|
3583 | *************************************************************************/
|
---|
3584 | private static void kdtreealloctemporaries(kdtree kdt,
|
---|
3585 | int n,
|
---|
3586 | int nx,
|
---|
3587 | int ny)
|
---|
3588 | {
|
---|
3589 | alglib.ap.assert(n>0, "KDTreeAllocTemporaries: internal error");
|
---|
3590 | kdt.x = new double[nx];
|
---|
3591 | kdt.idx = new int[n];
|
---|
3592 | kdt.r = new double[n];
|
---|
3593 | kdt.buf = new double[Math.Max(n, nx)];
|
---|
3594 | kdt.curboxmin = new double[nx];
|
---|
3595 | kdt.curboxmax = new double[nx];
|
---|
3596 | }
|
---|
3597 |
|
---|
3598 |
|
---|
3599 | }
|
---|
3600 | public class xdebug
|
---|
3601 | {
|
---|
3602 | public class xdebugrecord1 : apobject
|
---|
3603 | {
|
---|
3604 | public int i;
|
---|
3605 | public complex c;
|
---|
3606 | public double[] a;
|
---|
3607 | public xdebugrecord1()
|
---|
3608 | {
|
---|
3609 | init();
|
---|
3610 | }
|
---|
3611 | public override void init()
|
---|
3612 | {
|
---|
3613 | a = new double[0];
|
---|
3614 | }
|
---|
3615 | public override alglib.apobject make_copy()
|
---|
3616 | {
|
---|
3617 | xdebugrecord1 _result = new xdebugrecord1();
|
---|
3618 | _result.i = i;
|
---|
3619 | _result.c = c;
|
---|
3620 | _result.a = (double[])a.Clone();
|
---|
3621 | return _result;
|
---|
3622 | }
|
---|
3623 | };
|
---|
3624 |
|
---|
3625 |
|
---|
3626 |
|
---|
3627 |
|
---|
3628 | /*************************************************************************
|
---|
3629 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3630 | Never use it in any real life project.
|
---|
3631 |
|
---|
3632 | Creates and returns XDebugRecord1 structure:
|
---|
3633 | * integer and complex fields of Rec1 are set to 1 and 1+i correspondingly
|
---|
3634 | * array field of Rec1 is set to [2,3]
|
---|
3635 |
|
---|
3636 | -- ALGLIB --
|
---|
3637 | Copyright 27.05.2014 by Bochkanov Sergey
|
---|
3638 | *************************************************************************/
|
---|
3639 | public static void xdebuginitrecord1(xdebugrecord1 rec1)
|
---|
3640 | {
|
---|
3641 | rec1.i = 1;
|
---|
3642 | rec1.c.x = 1;
|
---|
3643 | rec1.c.y = 1;
|
---|
3644 | rec1.a = new double[2];
|
---|
3645 | rec1.a[0] = 2;
|
---|
3646 | rec1.a[1] = 3;
|
---|
3647 | }
|
---|
3648 |
|
---|
3649 |
|
---|
3650 | /*************************************************************************
|
---|
3651 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3652 | Never use it in any real life project.
|
---|
3653 |
|
---|
3654 | Counts number of True values in the boolean 1D array.
|
---|
3655 |
|
---|
3656 | -- ALGLIB --
|
---|
3657 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3658 | *************************************************************************/
|
---|
3659 | public static int xdebugb1count(bool[] a)
|
---|
3660 | {
|
---|
3661 | int result = 0;
|
---|
3662 | int i = 0;
|
---|
3663 |
|
---|
3664 | result = 0;
|
---|
3665 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3666 | {
|
---|
3667 | if( a[i] )
|
---|
3668 | {
|
---|
3669 | result = result+1;
|
---|
3670 | }
|
---|
3671 | }
|
---|
3672 | return result;
|
---|
3673 | }
|
---|
3674 |
|
---|
3675 |
|
---|
3676 | /*************************************************************************
|
---|
3677 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3678 | Never use it in any real life project.
|
---|
3679 |
|
---|
3680 | Replace all values in array by NOT(a[i]).
|
---|
3681 | Array is passed using "shared" convention.
|
---|
3682 |
|
---|
3683 | -- ALGLIB --
|
---|
3684 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3685 | *************************************************************************/
|
---|
3686 | public static void xdebugb1not(bool[] a)
|
---|
3687 | {
|
---|
3688 | int i = 0;
|
---|
3689 |
|
---|
3690 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3691 | {
|
---|
3692 | a[i] = !a[i];
|
---|
3693 | }
|
---|
3694 | }
|
---|
3695 |
|
---|
3696 |
|
---|
3697 | /*************************************************************************
|
---|
3698 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3699 | Never use it in any real life project.
|
---|
3700 |
|
---|
3701 | Appends copy of array to itself.
|
---|
3702 | Array is passed using "var" convention.
|
---|
3703 |
|
---|
3704 | -- ALGLIB --
|
---|
3705 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3706 | *************************************************************************/
|
---|
3707 | public static void xdebugb1appendcopy(ref bool[] a)
|
---|
3708 | {
|
---|
3709 | int i = 0;
|
---|
3710 | bool[] b = new bool[0];
|
---|
3711 |
|
---|
3712 | b = new bool[alglib.ap.len(a)];
|
---|
3713 | for(i=0; i<=alglib.ap.len(b)-1; i++)
|
---|
3714 | {
|
---|
3715 | b[i] = a[i];
|
---|
3716 | }
|
---|
3717 | a = new bool[2*alglib.ap.len(b)];
|
---|
3718 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3719 | {
|
---|
3720 | a[i] = b[i%alglib.ap.len(b)];
|
---|
3721 | }
|
---|
3722 | }
|
---|
3723 |
|
---|
3724 |
|
---|
3725 | /*************************************************************************
|
---|
3726 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3727 | Never use it in any real life project.
|
---|
3728 |
|
---|
3729 | Generate N-element array with even-numbered elements set to True.
|
---|
3730 | Array is passed using "out" convention.
|
---|
3731 |
|
---|
3732 | -- ALGLIB --
|
---|
3733 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3734 | *************************************************************************/
|
---|
3735 | public static void xdebugb1outeven(int n,
|
---|
3736 | ref bool[] a)
|
---|
3737 | {
|
---|
3738 | int i = 0;
|
---|
3739 |
|
---|
3740 | a = new bool[0];
|
---|
3741 |
|
---|
3742 | a = new bool[n];
|
---|
3743 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3744 | {
|
---|
3745 | a[i] = i%2==0;
|
---|
3746 | }
|
---|
3747 | }
|
---|
3748 |
|
---|
3749 |
|
---|
3750 | /*************************************************************************
|
---|
3751 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3752 | Never use it in any real life project.
|
---|
3753 |
|
---|
3754 | Returns sum of elements in the array.
|
---|
3755 |
|
---|
3756 | -- ALGLIB --
|
---|
3757 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3758 | *************************************************************************/
|
---|
3759 | public static int xdebugi1sum(int[] a)
|
---|
3760 | {
|
---|
3761 | int result = 0;
|
---|
3762 | int i = 0;
|
---|
3763 |
|
---|
3764 | result = 0;
|
---|
3765 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3766 | {
|
---|
3767 | result = result+a[i];
|
---|
3768 | }
|
---|
3769 | return result;
|
---|
3770 | }
|
---|
3771 |
|
---|
3772 |
|
---|
3773 | /*************************************************************************
|
---|
3774 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3775 | Never use it in any real life project.
|
---|
3776 |
|
---|
3777 | Replace all values in array by -A[I]
|
---|
3778 | Array is passed using "shared" convention.
|
---|
3779 |
|
---|
3780 | -- ALGLIB --
|
---|
3781 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3782 | *************************************************************************/
|
---|
3783 | public static void xdebugi1neg(int[] a)
|
---|
3784 | {
|
---|
3785 | int i = 0;
|
---|
3786 |
|
---|
3787 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3788 | {
|
---|
3789 | a[i] = -a[i];
|
---|
3790 | }
|
---|
3791 | }
|
---|
3792 |
|
---|
3793 |
|
---|
3794 | /*************************************************************************
|
---|
3795 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3796 | Never use it in any real life project.
|
---|
3797 |
|
---|
3798 | Appends copy of array to itself.
|
---|
3799 | Array is passed using "var" convention.
|
---|
3800 |
|
---|
3801 | -- ALGLIB --
|
---|
3802 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3803 | *************************************************************************/
|
---|
3804 | public static void xdebugi1appendcopy(ref int[] a)
|
---|
3805 | {
|
---|
3806 | int i = 0;
|
---|
3807 | int[] b = new int[0];
|
---|
3808 |
|
---|
3809 | b = new int[alglib.ap.len(a)];
|
---|
3810 | for(i=0; i<=alglib.ap.len(b)-1; i++)
|
---|
3811 | {
|
---|
3812 | b[i] = a[i];
|
---|
3813 | }
|
---|
3814 | a = new int[2*alglib.ap.len(b)];
|
---|
3815 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3816 | {
|
---|
3817 | a[i] = b[i%alglib.ap.len(b)];
|
---|
3818 | }
|
---|
3819 | }
|
---|
3820 |
|
---|
3821 |
|
---|
3822 | /*************************************************************************
|
---|
3823 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3824 | Never use it in any real life project.
|
---|
3825 |
|
---|
3826 | Generate N-element array with even-numbered A[I] set to I, and odd-numbered
|
---|
3827 | ones set to 0.
|
---|
3828 |
|
---|
3829 | Array is passed using "out" convention.
|
---|
3830 |
|
---|
3831 | -- ALGLIB --
|
---|
3832 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3833 | *************************************************************************/
|
---|
3834 | public static void xdebugi1outeven(int n,
|
---|
3835 | ref int[] a)
|
---|
3836 | {
|
---|
3837 | int i = 0;
|
---|
3838 |
|
---|
3839 | a = new int[0];
|
---|
3840 |
|
---|
3841 | a = new int[n];
|
---|
3842 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3843 | {
|
---|
3844 | if( i%2==0 )
|
---|
3845 | {
|
---|
3846 | a[i] = i;
|
---|
3847 | }
|
---|
3848 | else
|
---|
3849 | {
|
---|
3850 | a[i] = 0;
|
---|
3851 | }
|
---|
3852 | }
|
---|
3853 | }
|
---|
3854 |
|
---|
3855 |
|
---|
3856 | /*************************************************************************
|
---|
3857 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3858 | Never use it in any real life project.
|
---|
3859 |
|
---|
3860 | Returns sum of elements in the array.
|
---|
3861 |
|
---|
3862 | -- ALGLIB --
|
---|
3863 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3864 | *************************************************************************/
|
---|
3865 | public static double xdebugr1sum(double[] a)
|
---|
3866 | {
|
---|
3867 | double result = 0;
|
---|
3868 | int i = 0;
|
---|
3869 |
|
---|
3870 | result = 0;
|
---|
3871 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3872 | {
|
---|
3873 | result = result+a[i];
|
---|
3874 | }
|
---|
3875 | return result;
|
---|
3876 | }
|
---|
3877 |
|
---|
3878 |
|
---|
3879 | /*************************************************************************
|
---|
3880 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3881 | Never use it in any real life project.
|
---|
3882 |
|
---|
3883 | Replace all values in array by -A[I]
|
---|
3884 | Array is passed using "shared" convention.
|
---|
3885 |
|
---|
3886 | -- ALGLIB --
|
---|
3887 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3888 | *************************************************************************/
|
---|
3889 | public static void xdebugr1neg(double[] a)
|
---|
3890 | {
|
---|
3891 | int i = 0;
|
---|
3892 |
|
---|
3893 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3894 | {
|
---|
3895 | a[i] = -a[i];
|
---|
3896 | }
|
---|
3897 | }
|
---|
3898 |
|
---|
3899 |
|
---|
3900 | /*************************************************************************
|
---|
3901 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3902 | Never use it in any real life project.
|
---|
3903 |
|
---|
3904 | Appends copy of array to itself.
|
---|
3905 | Array is passed using "var" convention.
|
---|
3906 |
|
---|
3907 | -- ALGLIB --
|
---|
3908 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3909 | *************************************************************************/
|
---|
3910 | public static void xdebugr1appendcopy(ref double[] a)
|
---|
3911 | {
|
---|
3912 | int i = 0;
|
---|
3913 | double[] b = new double[0];
|
---|
3914 |
|
---|
3915 | b = new double[alglib.ap.len(a)];
|
---|
3916 | for(i=0; i<=alglib.ap.len(b)-1; i++)
|
---|
3917 | {
|
---|
3918 | b[i] = a[i];
|
---|
3919 | }
|
---|
3920 | a = new double[2*alglib.ap.len(b)];
|
---|
3921 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3922 | {
|
---|
3923 | a[i] = b[i%alglib.ap.len(b)];
|
---|
3924 | }
|
---|
3925 | }
|
---|
3926 |
|
---|
3927 |
|
---|
3928 | /*************************************************************************
|
---|
3929 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3930 | Never use it in any real life project.
|
---|
3931 |
|
---|
3932 | Generate N-element array with even-numbered A[I] set to I*0.25,
|
---|
3933 | and odd-numbered ones are set to 0.
|
---|
3934 |
|
---|
3935 | Array is passed using "out" convention.
|
---|
3936 |
|
---|
3937 | -- ALGLIB --
|
---|
3938 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3939 | *************************************************************************/
|
---|
3940 | public static void xdebugr1outeven(int n,
|
---|
3941 | ref double[] a)
|
---|
3942 | {
|
---|
3943 | int i = 0;
|
---|
3944 |
|
---|
3945 | a = new double[0];
|
---|
3946 |
|
---|
3947 | a = new double[n];
|
---|
3948 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3949 | {
|
---|
3950 | if( i%2==0 )
|
---|
3951 | {
|
---|
3952 | a[i] = i*0.25;
|
---|
3953 | }
|
---|
3954 | else
|
---|
3955 | {
|
---|
3956 | a[i] = 0;
|
---|
3957 | }
|
---|
3958 | }
|
---|
3959 | }
|
---|
3960 |
|
---|
3961 |
|
---|
3962 | /*************************************************************************
|
---|
3963 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3964 | Never use it in any real life project.
|
---|
3965 |
|
---|
3966 | Returns sum of elements in the array.
|
---|
3967 |
|
---|
3968 | -- ALGLIB --
|
---|
3969 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3970 | *************************************************************************/
|
---|
3971 | public static complex xdebugc1sum(complex[] a)
|
---|
3972 | {
|
---|
3973 | complex result = 0;
|
---|
3974 | int i = 0;
|
---|
3975 |
|
---|
3976 | result = 0;
|
---|
3977 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
3978 | {
|
---|
3979 | result = result+a[i];
|
---|
3980 | }
|
---|
3981 | return result;
|
---|
3982 | }
|
---|
3983 |
|
---|
3984 |
|
---|
3985 | /*************************************************************************
|
---|
3986 | This is debug function intended for testing ALGLIB interface generator.
|
---|
3987 | Never use it in any real life project.
|
---|
3988 |
|
---|
3989 | Replace all values in array by -A[I]
|
---|
3990 | Array is passed using "shared" convention.
|
---|
3991 |
|
---|
3992 | -- ALGLIB --
|
---|
3993 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
3994 | *************************************************************************/
|
---|
3995 | public static void xdebugc1neg(complex[] a)
|
---|
3996 | {
|
---|
3997 | int i = 0;
|
---|
3998 |
|
---|
3999 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
4000 | {
|
---|
4001 | a[i] = -a[i];
|
---|
4002 | }
|
---|
4003 | }
|
---|
4004 |
|
---|
4005 |
|
---|
4006 | /*************************************************************************
|
---|
4007 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4008 | Never use it in any real life project.
|
---|
4009 |
|
---|
4010 | Appends copy of array to itself.
|
---|
4011 | Array is passed using "var" convention.
|
---|
4012 |
|
---|
4013 | -- ALGLIB --
|
---|
4014 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4015 | *************************************************************************/
|
---|
4016 | public static void xdebugc1appendcopy(ref complex[] a)
|
---|
4017 | {
|
---|
4018 | int i = 0;
|
---|
4019 | complex[] b = new complex[0];
|
---|
4020 |
|
---|
4021 | b = new complex[alglib.ap.len(a)];
|
---|
4022 | for(i=0; i<=alglib.ap.len(b)-1; i++)
|
---|
4023 | {
|
---|
4024 | b[i] = a[i];
|
---|
4025 | }
|
---|
4026 | a = new complex[2*alglib.ap.len(b)];
|
---|
4027 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
4028 | {
|
---|
4029 | a[i] = b[i%alglib.ap.len(b)];
|
---|
4030 | }
|
---|
4031 | }
|
---|
4032 |
|
---|
4033 |
|
---|
4034 | /*************************************************************************
|
---|
4035 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4036 | Never use it in any real life project.
|
---|
4037 |
|
---|
4038 | Generate N-element array with even-numbered A[K] set to (x,y) = (K*0.25, K*0.125)
|
---|
4039 | and odd-numbered ones are set to 0.
|
---|
4040 |
|
---|
4041 | Array is passed using "out" convention.
|
---|
4042 |
|
---|
4043 | -- ALGLIB --
|
---|
4044 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4045 | *************************************************************************/
|
---|
4046 | public static void xdebugc1outeven(int n,
|
---|
4047 | ref complex[] a)
|
---|
4048 | {
|
---|
4049 | int i = 0;
|
---|
4050 |
|
---|
4051 | a = new complex[0];
|
---|
4052 |
|
---|
4053 | a = new complex[n];
|
---|
4054 | for(i=0; i<=alglib.ap.len(a)-1; i++)
|
---|
4055 | {
|
---|
4056 | if( i%2==0 )
|
---|
4057 | {
|
---|
4058 | a[i].x = i*0.250;
|
---|
4059 | a[i].y = i*0.125;
|
---|
4060 | }
|
---|
4061 | else
|
---|
4062 | {
|
---|
4063 | a[i] = 0;
|
---|
4064 | }
|
---|
4065 | }
|
---|
4066 | }
|
---|
4067 |
|
---|
4068 |
|
---|
4069 | /*************************************************************************
|
---|
4070 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4071 | Never use it in any real life project.
|
---|
4072 |
|
---|
4073 | Counts number of True values in the boolean 2D array.
|
---|
4074 |
|
---|
4075 | -- ALGLIB --
|
---|
4076 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4077 | *************************************************************************/
|
---|
4078 | public static int xdebugb2count(bool[,] a)
|
---|
4079 | {
|
---|
4080 | int result = 0;
|
---|
4081 | int i = 0;
|
---|
4082 | int j = 0;
|
---|
4083 |
|
---|
4084 | result = 0;
|
---|
4085 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4086 | {
|
---|
4087 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4088 | {
|
---|
4089 | if( a[i,j] )
|
---|
4090 | {
|
---|
4091 | result = result+1;
|
---|
4092 | }
|
---|
4093 | }
|
---|
4094 | }
|
---|
4095 | return result;
|
---|
4096 | }
|
---|
4097 |
|
---|
4098 |
|
---|
4099 | /*************************************************************************
|
---|
4100 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4101 | Never use it in any real life project.
|
---|
4102 |
|
---|
4103 | Replace all values in array by NOT(a[i]).
|
---|
4104 | Array is passed using "shared" convention.
|
---|
4105 |
|
---|
4106 | -- ALGLIB --
|
---|
4107 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4108 | *************************************************************************/
|
---|
4109 | public static void xdebugb2not(bool[,] a)
|
---|
4110 | {
|
---|
4111 | int i = 0;
|
---|
4112 | int j = 0;
|
---|
4113 |
|
---|
4114 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4115 | {
|
---|
4116 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4117 | {
|
---|
4118 | a[i,j] = !a[i,j];
|
---|
4119 | }
|
---|
4120 | }
|
---|
4121 | }
|
---|
4122 |
|
---|
4123 |
|
---|
4124 | /*************************************************************************
|
---|
4125 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4126 | Never use it in any real life project.
|
---|
4127 |
|
---|
4128 | Transposes array.
|
---|
4129 | Array is passed using "var" convention.
|
---|
4130 |
|
---|
4131 | -- ALGLIB --
|
---|
4132 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4133 | *************************************************************************/
|
---|
4134 | public static void xdebugb2transpose(ref bool[,] a)
|
---|
4135 | {
|
---|
4136 | int i = 0;
|
---|
4137 | int j = 0;
|
---|
4138 | bool[,] b = new bool[0,0];
|
---|
4139 |
|
---|
4140 | b = new bool[alglib.ap.rows(a), alglib.ap.cols(a)];
|
---|
4141 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4142 | {
|
---|
4143 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4144 | {
|
---|
4145 | b[i,j] = a[i,j];
|
---|
4146 | }
|
---|
4147 | }
|
---|
4148 | a = new bool[alglib.ap.cols(b), alglib.ap.rows(b)];
|
---|
4149 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4150 | {
|
---|
4151 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4152 | {
|
---|
4153 | a[j,i] = b[i,j];
|
---|
4154 | }
|
---|
4155 | }
|
---|
4156 | }
|
---|
4157 |
|
---|
4158 |
|
---|
4159 | /*************************************************************************
|
---|
4160 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4161 | Never use it in any real life project.
|
---|
4162 |
|
---|
4163 | Generate MxN matrix with elements set to "Sin(3*I+5*J)>0"
|
---|
4164 | Array is passed using "out" convention.
|
---|
4165 |
|
---|
4166 | -- ALGLIB --
|
---|
4167 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4168 | *************************************************************************/
|
---|
4169 | public static void xdebugb2outsin(int m,
|
---|
4170 | int n,
|
---|
4171 | ref bool[,] a)
|
---|
4172 | {
|
---|
4173 | int i = 0;
|
---|
4174 | int j = 0;
|
---|
4175 |
|
---|
4176 | a = new bool[0,0];
|
---|
4177 |
|
---|
4178 | a = new bool[m, n];
|
---|
4179 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4180 | {
|
---|
4181 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4182 | {
|
---|
4183 | a[i,j] = (double)(Math.Sin(3*i+5*j))>(double)(0);
|
---|
4184 | }
|
---|
4185 | }
|
---|
4186 | }
|
---|
4187 |
|
---|
4188 |
|
---|
4189 | /*************************************************************************
|
---|
4190 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4191 | Never use it in any real life project.
|
---|
4192 |
|
---|
4193 | Returns sum of elements in the array.
|
---|
4194 |
|
---|
4195 | -- ALGLIB --
|
---|
4196 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4197 | *************************************************************************/
|
---|
4198 | public static int xdebugi2sum(int[,] a)
|
---|
4199 | {
|
---|
4200 | int result = 0;
|
---|
4201 | int i = 0;
|
---|
4202 | int j = 0;
|
---|
4203 |
|
---|
4204 | result = 0;
|
---|
4205 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4206 | {
|
---|
4207 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4208 | {
|
---|
4209 | result = result+a[i,j];
|
---|
4210 | }
|
---|
4211 | }
|
---|
4212 | return result;
|
---|
4213 | }
|
---|
4214 |
|
---|
4215 |
|
---|
4216 | /*************************************************************************
|
---|
4217 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4218 | Never use it in any real life project.
|
---|
4219 |
|
---|
4220 | Replace all values in array by -a[i,j]
|
---|
4221 | Array is passed using "shared" convention.
|
---|
4222 |
|
---|
4223 | -- ALGLIB --
|
---|
4224 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4225 | *************************************************************************/
|
---|
4226 | public static void xdebugi2neg(int[,] a)
|
---|
4227 | {
|
---|
4228 | int i = 0;
|
---|
4229 | int j = 0;
|
---|
4230 |
|
---|
4231 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4232 | {
|
---|
4233 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4234 | {
|
---|
4235 | a[i,j] = -a[i,j];
|
---|
4236 | }
|
---|
4237 | }
|
---|
4238 | }
|
---|
4239 |
|
---|
4240 |
|
---|
4241 | /*************************************************************************
|
---|
4242 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4243 | Never use it in any real life project.
|
---|
4244 |
|
---|
4245 | Transposes array.
|
---|
4246 | Array is passed using "var" convention.
|
---|
4247 |
|
---|
4248 | -- ALGLIB --
|
---|
4249 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4250 | *************************************************************************/
|
---|
4251 | public static void xdebugi2transpose(ref int[,] a)
|
---|
4252 | {
|
---|
4253 | int i = 0;
|
---|
4254 | int j = 0;
|
---|
4255 | int[,] b = new int[0,0];
|
---|
4256 |
|
---|
4257 | b = new int[alglib.ap.rows(a), alglib.ap.cols(a)];
|
---|
4258 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4259 | {
|
---|
4260 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4261 | {
|
---|
4262 | b[i,j] = a[i,j];
|
---|
4263 | }
|
---|
4264 | }
|
---|
4265 | a = new int[alglib.ap.cols(b), alglib.ap.rows(b)];
|
---|
4266 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4267 | {
|
---|
4268 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4269 | {
|
---|
4270 | a[j,i] = b[i,j];
|
---|
4271 | }
|
---|
4272 | }
|
---|
4273 | }
|
---|
4274 |
|
---|
4275 |
|
---|
4276 | /*************************************************************************
|
---|
4277 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4278 | Never use it in any real life project.
|
---|
4279 |
|
---|
4280 | Generate MxN matrix with elements set to "Sign(Sin(3*I+5*J))"
|
---|
4281 | Array is passed using "out" convention.
|
---|
4282 |
|
---|
4283 | -- ALGLIB --
|
---|
4284 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4285 | *************************************************************************/
|
---|
4286 | public static void xdebugi2outsin(int m,
|
---|
4287 | int n,
|
---|
4288 | ref int[,] a)
|
---|
4289 | {
|
---|
4290 | int i = 0;
|
---|
4291 | int j = 0;
|
---|
4292 |
|
---|
4293 | a = new int[0,0];
|
---|
4294 |
|
---|
4295 | a = new int[m, n];
|
---|
4296 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4297 | {
|
---|
4298 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4299 | {
|
---|
4300 | a[i,j] = Math.Sign(Math.Sin(3*i+5*j));
|
---|
4301 | }
|
---|
4302 | }
|
---|
4303 | }
|
---|
4304 |
|
---|
4305 |
|
---|
4306 | /*************************************************************************
|
---|
4307 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4308 | Never use it in any real life project.
|
---|
4309 |
|
---|
4310 | Returns sum of elements in the array.
|
---|
4311 |
|
---|
4312 | -- ALGLIB --
|
---|
4313 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4314 | *************************************************************************/
|
---|
4315 | public static double xdebugr2sum(double[,] a)
|
---|
4316 | {
|
---|
4317 | double result = 0;
|
---|
4318 | int i = 0;
|
---|
4319 | int j = 0;
|
---|
4320 |
|
---|
4321 | result = 0;
|
---|
4322 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4323 | {
|
---|
4324 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4325 | {
|
---|
4326 | result = result+a[i,j];
|
---|
4327 | }
|
---|
4328 | }
|
---|
4329 | return result;
|
---|
4330 | }
|
---|
4331 |
|
---|
4332 |
|
---|
4333 | /*************************************************************************
|
---|
4334 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4335 | Never use it in any real life project.
|
---|
4336 |
|
---|
4337 | Replace all values in array by -a[i,j]
|
---|
4338 | Array is passed using "shared" convention.
|
---|
4339 |
|
---|
4340 | -- ALGLIB --
|
---|
4341 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4342 | *************************************************************************/
|
---|
4343 | public static void xdebugr2neg(double[,] a)
|
---|
4344 | {
|
---|
4345 | int i = 0;
|
---|
4346 | int j = 0;
|
---|
4347 |
|
---|
4348 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4349 | {
|
---|
4350 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4351 | {
|
---|
4352 | a[i,j] = -a[i,j];
|
---|
4353 | }
|
---|
4354 | }
|
---|
4355 | }
|
---|
4356 |
|
---|
4357 |
|
---|
4358 | /*************************************************************************
|
---|
4359 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4360 | Never use it in any real life project.
|
---|
4361 |
|
---|
4362 | Transposes array.
|
---|
4363 | Array is passed using "var" convention.
|
---|
4364 |
|
---|
4365 | -- ALGLIB --
|
---|
4366 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4367 | *************************************************************************/
|
---|
4368 | public static void xdebugr2transpose(ref double[,] a)
|
---|
4369 | {
|
---|
4370 | int i = 0;
|
---|
4371 | int j = 0;
|
---|
4372 | double[,] b = new double[0,0];
|
---|
4373 |
|
---|
4374 | b = new double[alglib.ap.rows(a), alglib.ap.cols(a)];
|
---|
4375 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4376 | {
|
---|
4377 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4378 | {
|
---|
4379 | b[i,j] = a[i,j];
|
---|
4380 | }
|
---|
4381 | }
|
---|
4382 | a = new double[alglib.ap.cols(b), alglib.ap.rows(b)];
|
---|
4383 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4384 | {
|
---|
4385 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4386 | {
|
---|
4387 | a[j,i] = b[i,j];
|
---|
4388 | }
|
---|
4389 | }
|
---|
4390 | }
|
---|
4391 |
|
---|
4392 |
|
---|
4393 | /*************************************************************************
|
---|
4394 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4395 | Never use it in any real life project.
|
---|
4396 |
|
---|
4397 | Generate MxN matrix with elements set to "Sin(3*I+5*J)"
|
---|
4398 | Array is passed using "out" convention.
|
---|
4399 |
|
---|
4400 | -- ALGLIB --
|
---|
4401 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4402 | *************************************************************************/
|
---|
4403 | public static void xdebugr2outsin(int m,
|
---|
4404 | int n,
|
---|
4405 | ref double[,] a)
|
---|
4406 | {
|
---|
4407 | int i = 0;
|
---|
4408 | int j = 0;
|
---|
4409 |
|
---|
4410 | a = new double[0,0];
|
---|
4411 |
|
---|
4412 | a = new double[m, n];
|
---|
4413 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4414 | {
|
---|
4415 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4416 | {
|
---|
4417 | a[i,j] = Math.Sin(3*i+5*j);
|
---|
4418 | }
|
---|
4419 | }
|
---|
4420 | }
|
---|
4421 |
|
---|
4422 |
|
---|
4423 | /*************************************************************************
|
---|
4424 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4425 | Never use it in any real life project.
|
---|
4426 |
|
---|
4427 | Returns sum of elements in the array.
|
---|
4428 |
|
---|
4429 | -- ALGLIB --
|
---|
4430 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4431 | *************************************************************************/
|
---|
4432 | public static complex xdebugc2sum(complex[,] a)
|
---|
4433 | {
|
---|
4434 | complex result = 0;
|
---|
4435 | int i = 0;
|
---|
4436 | int j = 0;
|
---|
4437 |
|
---|
4438 | result = 0;
|
---|
4439 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4440 | {
|
---|
4441 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4442 | {
|
---|
4443 | result = result+a[i,j];
|
---|
4444 | }
|
---|
4445 | }
|
---|
4446 | return result;
|
---|
4447 | }
|
---|
4448 |
|
---|
4449 |
|
---|
4450 | /*************************************************************************
|
---|
4451 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4452 | Never use it in any real life project.
|
---|
4453 |
|
---|
4454 | Replace all values in array by -a[i,j]
|
---|
4455 | Array is passed using "shared" convention.
|
---|
4456 |
|
---|
4457 | -- ALGLIB --
|
---|
4458 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4459 | *************************************************************************/
|
---|
4460 | public static void xdebugc2neg(complex[,] a)
|
---|
4461 | {
|
---|
4462 | int i = 0;
|
---|
4463 | int j = 0;
|
---|
4464 |
|
---|
4465 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4466 | {
|
---|
4467 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4468 | {
|
---|
4469 | a[i,j] = -a[i,j];
|
---|
4470 | }
|
---|
4471 | }
|
---|
4472 | }
|
---|
4473 |
|
---|
4474 |
|
---|
4475 | /*************************************************************************
|
---|
4476 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4477 | Never use it in any real life project.
|
---|
4478 |
|
---|
4479 | Transposes array.
|
---|
4480 | Array is passed using "var" convention.
|
---|
4481 |
|
---|
4482 | -- ALGLIB --
|
---|
4483 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4484 | *************************************************************************/
|
---|
4485 | public static void xdebugc2transpose(ref complex[,] a)
|
---|
4486 | {
|
---|
4487 | int i = 0;
|
---|
4488 | int j = 0;
|
---|
4489 | complex[,] b = new complex[0,0];
|
---|
4490 |
|
---|
4491 | b = new complex[alglib.ap.rows(a), alglib.ap.cols(a)];
|
---|
4492 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4493 | {
|
---|
4494 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4495 | {
|
---|
4496 | b[i,j] = a[i,j];
|
---|
4497 | }
|
---|
4498 | }
|
---|
4499 | a = new complex[alglib.ap.cols(b), alglib.ap.rows(b)];
|
---|
4500 | for(i=0; i<=alglib.ap.rows(b)-1; i++)
|
---|
4501 | {
|
---|
4502 | for(j=0; j<=alglib.ap.cols(b)-1; j++)
|
---|
4503 | {
|
---|
4504 | a[j,i] = b[i,j];
|
---|
4505 | }
|
---|
4506 | }
|
---|
4507 | }
|
---|
4508 |
|
---|
4509 |
|
---|
4510 | /*************************************************************************
|
---|
4511 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4512 | Never use it in any real life project.
|
---|
4513 |
|
---|
4514 | Generate MxN matrix with elements set to "Sin(3*I+5*J),Cos(3*I+5*J)"
|
---|
4515 | Array is passed using "out" convention.
|
---|
4516 |
|
---|
4517 | -- ALGLIB --
|
---|
4518 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4519 | *************************************************************************/
|
---|
4520 | public static void xdebugc2outsincos(int m,
|
---|
4521 | int n,
|
---|
4522 | ref complex[,] a)
|
---|
4523 | {
|
---|
4524 | int i = 0;
|
---|
4525 | int j = 0;
|
---|
4526 |
|
---|
4527 | a = new complex[0,0];
|
---|
4528 |
|
---|
4529 | a = new complex[m, n];
|
---|
4530 | for(i=0; i<=alglib.ap.rows(a)-1; i++)
|
---|
4531 | {
|
---|
4532 | for(j=0; j<=alglib.ap.cols(a)-1; j++)
|
---|
4533 | {
|
---|
4534 | a[i,j].x = Math.Sin(3*i+5*j);
|
---|
4535 | a[i,j].y = Math.Cos(3*i+5*j);
|
---|
4536 | }
|
---|
4537 | }
|
---|
4538 | }
|
---|
4539 |
|
---|
4540 |
|
---|
4541 | /*************************************************************************
|
---|
4542 | This is debug function intended for testing ALGLIB interface generator.
|
---|
4543 | Never use it in any real life project.
|
---|
4544 |
|
---|
4545 | Returns sum of a[i,j]*(1+b[i,j]) such that c[i,j] is True
|
---|
4546 |
|
---|
4547 | -- ALGLIB --
|
---|
4548 | Copyright 11.10.2013 by Bochkanov Sergey
|
---|
4549 | *************************************************************************/
|
---|
4550 | public static double xdebugmaskedbiasedproductsum(int m,
|
---|
4551 | int n,
|
---|
4552 | double[,] a,
|
---|
4553 | double[,] b,
|
---|
4554 | bool[,] c)
|
---|
4555 | {
|
---|
4556 | double result = 0;
|
---|
4557 | int i = 0;
|
---|
4558 | int j = 0;
|
---|
4559 |
|
---|
4560 | alglib.ap.assert(m>=alglib.ap.rows(a));
|
---|
4561 | alglib.ap.assert(m>=alglib.ap.rows(b));
|
---|
4562 | alglib.ap.assert(m>=alglib.ap.rows(c));
|
---|
4563 | alglib.ap.assert(n>=alglib.ap.cols(a));
|
---|
4564 | alglib.ap.assert(n>=alglib.ap.cols(b));
|
---|
4565 | alglib.ap.assert(n>=alglib.ap.cols(c));
|
---|
4566 | result = 0.0;
|
---|
4567 | for(i=0; i<=m-1; i++)
|
---|
4568 | {
|
---|
4569 | for(j=0; j<=n-1; j++)
|
---|
4570 | {
|
---|
4571 | if( c[i,j] )
|
---|
4572 | {
|
---|
4573 | result = result+a[i,j]*(1+b[i,j]);
|
---|
4574 | }
|
---|
4575 | }
|
---|
4576 | }
|
---|
4577 | return result;
|
---|
4578 | }
|
---|
4579 |
|
---|
4580 |
|
---|
4581 | }
|
---|
4582 | }
|
---|
4583 |
|
---|