-
Notifications
You must be signed in to change notification settings - Fork 0
/
alglibinternal.cpp
18597 lines (16211 loc) · 519 KB
/
alglibinternal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************
ALGLIB 3.17.0 (source code generated 2020-12-27)
Copyright (c) Sergey Bochkanov (ALGLIB project).
>>> SOURCE LICENSE >>>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (www.fsf.org); either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
A copy of the GNU General Public License is available at
http://www.fsf.org/licensing/licenses
>>> END OF LICENSE >>>
*************************************************************************/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "stdafx.h"
#include "alglibinternal.h"
// disable some irrelevant warnings
#if (AE_COMPILER==AE_MSVC) && !defined(AE_ALL_WARNINGS)
#pragma warning(disable:4100)
#pragma warning(disable:4127)
#pragma warning(disable:4611)
#pragma warning(disable:4702)
#pragma warning(disable:4996)
#endif
/////////////////////////////////////////////////////////////////////////
//
// THIS SECTION CONTAINS IMPLEMENTATION OF C++ INTERFACE
//
/////////////////////////////////////////////////////////////////////////
namespace alglib
{
}
/////////////////////////////////////////////////////////////////////////
//
// THIS SECTION CONTAINS IMPLEMENTATION OF COMPUTATIONAL CORE
//
/////////////////////////////////////////////////////////////////////////
namespace alglib_impl
{
#if defined(AE_COMPILE_SCODES) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_APSERV) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_TSORT) || !defined(AE_PARTIAL_BUILD)
static void tsort_tagsortfastirec(/* Real */ ae_vector* a,
/* Integer */ ae_vector* b,
/* Real */ ae_vector* bufa,
/* Integer */ ae_vector* bufb,
ae_int_t i1,
ae_int_t i2,
ae_state *_state);
static void tsort_tagsortfastrrec(/* Real */ ae_vector* a,
/* Real */ ae_vector* b,
/* Real */ ae_vector* bufa,
/* Real */ ae_vector* bufb,
ae_int_t i1,
ae_int_t i2,
ae_state *_state);
static void tsort_tagsortfastrec(/* Real */ ae_vector* a,
/* Real */ ae_vector* bufa,
ae_int_t i1,
ae_int_t i2,
ae_state *_state);
#endif
#if defined(AE_COMPILE_ABLASF) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_ABLASMKL) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_CREFLECTIONS) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_ROTATIONS) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_TRLINSOLVE) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_SAFESOLVE) || !defined(AE_PARTIAL_BUILD)
static ae_bool safesolve_cbasicsolveandupdate(ae_complex alpha,
ae_complex beta,
double lnmax,
double bnorm,
double maxgrowth,
double* xnorm,
ae_complex* x,
ae_state *_state);
#endif
#if defined(AE_COMPILE_HBLAS) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_SBLAS) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_BLAS) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_LINMIN) || !defined(AE_PARTIAL_BUILD)
static double linmin_ftol = 0.001;
static double linmin_xtol = 100*ae_machineepsilon;
static ae_int_t linmin_maxfev = 20;
static double linmin_stpmin = 1.0E-50;
static double linmin_defstpmax = 1.0E+50;
static double linmin_armijofactor = 1.3;
static void linmin_mcstep(double* stx,
double* fx,
double* dx,
double* sty,
double* fy,
double* dy,
double* stp,
double fp,
double dp,
ae_bool* brackt,
double stmin,
double stmax,
ae_int_t* info,
ae_state *_state);
#endif
#if defined(AE_COMPILE_XBLAS) || !defined(AE_PARTIAL_BUILD)
static void xblas_xsum(/* Real */ ae_vector* w,
double mx,
ae_int_t n,
double* r,
double* rerr,
ae_state *_state);
static double xblas_xfastpow(double r, ae_int_t n, ae_state *_state);
#endif
#if defined(AE_COMPILE_BASICSTATOPS) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_HPCCORES) || !defined(AE_PARTIAL_BUILD)
static ae_bool hpccores_hpcpreparechunkedgradientx(/* Real */ ae_vector* weights,
ae_int_t wcount,
/* Real */ ae_vector* hpcbuf,
ae_state *_state);
static ae_bool hpccores_hpcfinalizechunkedgradientx(/* Real */ ae_vector* buf,
ae_int_t wcount,
/* Real */ ae_vector* grad,
ae_state *_state);
#endif
#if defined(AE_COMPILE_NTHEORY) || !defined(AE_PARTIAL_BUILD)
static ae_bool ntheory_isprime(ae_int_t n, ae_state *_state);
static ae_int_t ntheory_modmul(ae_int_t a,
ae_int_t b,
ae_int_t n,
ae_state *_state);
static ae_int_t ntheory_modexp(ae_int_t a,
ae_int_t b,
ae_int_t n,
ae_state *_state);
#endif
#if defined(AE_COMPILE_FTBASE) || !defined(AE_PARTIAL_BUILD)
static ae_int_t ftbase_coltype = 0;
static ae_int_t ftbase_coloperandscnt = 1;
static ae_int_t ftbase_coloperandsize = 2;
static ae_int_t ftbase_colmicrovectorsize = 3;
static ae_int_t ftbase_colparam0 = 4;
static ae_int_t ftbase_colparam1 = 5;
static ae_int_t ftbase_colparam2 = 6;
static ae_int_t ftbase_colparam3 = 7;
static ae_int_t ftbase_colscnt = 8;
static ae_int_t ftbase_opend = 0;
static ae_int_t ftbase_opcomplexreffft = 1;
static ae_int_t ftbase_opbluesteinsfft = 2;
static ae_int_t ftbase_opcomplexcodeletfft = 3;
static ae_int_t ftbase_opcomplexcodelettwfft = 4;
static ae_int_t ftbase_opradersfft = 5;
static ae_int_t ftbase_opcomplextranspose = -1;
static ae_int_t ftbase_opcomplexfftfactors = -2;
static ae_int_t ftbase_opstart = -3;
static ae_int_t ftbase_opjmp = -4;
static ae_int_t ftbase_opparallelcall = -5;
static ae_int_t ftbase_maxradix = 6;
static ae_int_t ftbase_updatetw = 16;
static ae_int_t ftbase_recursivethreshold = 1024;
static ae_int_t ftbase_raderthreshold = 19;
static ae_int_t ftbase_ftbasecodeletrecommended = 5;
static double ftbase_ftbaseinefficiencyfactor = 1.3;
static ae_int_t ftbase_ftbasemaxsmoothfactor = 5;
static void ftbase_ftdeterminespacerequirements(ae_int_t n,
ae_int_t* precrsize,
ae_int_t* precisize,
ae_state *_state);
static void ftbase_ftcomplexfftplanrec(ae_int_t n,
ae_int_t k,
ae_bool childplan,
ae_bool topmostplan,
ae_int_t* rowptr,
ae_int_t* bluesteinsize,
ae_int_t* precrptr,
ae_int_t* preciptr,
fasttransformplan* plan,
ae_state *_state);
static void ftbase_ftpushentry(fasttransformplan* plan,
ae_int_t* rowptr,
ae_int_t etype,
ae_int_t eopcnt,
ae_int_t eopsize,
ae_int_t emcvsize,
ae_int_t eparam0,
ae_state *_state);
static void ftbase_ftpushentry2(fasttransformplan* plan,
ae_int_t* rowptr,
ae_int_t etype,
ae_int_t eopcnt,
ae_int_t eopsize,
ae_int_t emcvsize,
ae_int_t eparam0,
ae_int_t eparam1,
ae_state *_state);
static void ftbase_ftpushentry4(fasttransformplan* plan,
ae_int_t* rowptr,
ae_int_t etype,
ae_int_t eopcnt,
ae_int_t eopsize,
ae_int_t emcvsize,
ae_int_t eparam0,
ae_int_t eparam1,
ae_int_t eparam2,
ae_int_t eparam3,
ae_state *_state);
static void ftbase_ftapplysubplan(fasttransformplan* plan,
ae_int_t subplan,
/* Real */ ae_vector* a,
ae_int_t abase,
ae_int_t aoffset,
/* Real */ ae_vector* buf,
ae_int_t repcnt,
ae_state *_state);
static void ftbase_ftapplycomplexreffft(/* Real */ ae_vector* a,
ae_int_t offs,
ae_int_t operandscnt,
ae_int_t operandsize,
ae_int_t microvectorsize,
/* Real */ ae_vector* buf,
ae_state *_state);
static void ftbase_ftapplycomplexcodeletfft(/* Real */ ae_vector* a,
ae_int_t offs,
ae_int_t operandscnt,
ae_int_t operandsize,
ae_int_t microvectorsize,
ae_state *_state);
static void ftbase_ftapplycomplexcodelettwfft(/* Real */ ae_vector* a,
ae_int_t offs,
ae_int_t operandscnt,
ae_int_t operandsize,
ae_int_t microvectorsize,
ae_state *_state);
static void ftbase_ftprecomputebluesteinsfft(ae_int_t n,
ae_int_t m,
/* Real */ ae_vector* precr,
ae_int_t offs,
ae_state *_state);
static void ftbase_ftbluesteinsfft(fasttransformplan* plan,
/* Real */ ae_vector* a,
ae_int_t abase,
ae_int_t aoffset,
ae_int_t operandscnt,
ae_int_t n,
ae_int_t m,
ae_int_t precoffs,
ae_int_t subplan,
/* Real */ ae_vector* bufa,
/* Real */ ae_vector* bufb,
/* Real */ ae_vector* bufc,
/* Real */ ae_vector* bufd,
ae_state *_state);
static void ftbase_ftprecomputeradersfft(ae_int_t n,
ae_int_t rq,
ae_int_t riq,
/* Real */ ae_vector* precr,
ae_int_t offs,
ae_state *_state);
static void ftbase_ftradersfft(fasttransformplan* plan,
/* Real */ ae_vector* a,
ae_int_t abase,
ae_int_t aoffset,
ae_int_t operandscnt,
ae_int_t n,
ae_int_t subplan,
ae_int_t rq,
ae_int_t riq,
ae_int_t precoffs,
/* Real */ ae_vector* buf,
ae_state *_state);
static void ftbase_ftfactorize(ae_int_t n,
ae_bool isroot,
ae_int_t* n1,
ae_int_t* n2,
ae_state *_state);
static ae_int_t ftbase_ftoptimisticestimate(ae_int_t n, ae_state *_state);
static void ftbase_ffttwcalc(/* Real */ ae_vector* a,
ae_int_t aoffset,
ae_int_t n1,
ae_int_t n2,
ae_state *_state);
static void ftbase_internalcomplexlintranspose(/* Real */ ae_vector* a,
ae_int_t m,
ae_int_t n,
ae_int_t astart,
/* Real */ ae_vector* buf,
ae_state *_state);
static void ftbase_ffticltrec(/* Real */ ae_vector* a,
ae_int_t astart,
ae_int_t astride,
/* Real */ ae_vector* b,
ae_int_t bstart,
ae_int_t bstride,
ae_int_t m,
ae_int_t n,
ae_state *_state);
static void ftbase_fftirltrec(/* Real */ ae_vector* a,
ae_int_t astart,
ae_int_t astride,
/* Real */ ae_vector* b,
ae_int_t bstart,
ae_int_t bstride,
ae_int_t m,
ae_int_t n,
ae_state *_state);
static void ftbase_ftbasefindsmoothrec(ae_int_t n,
ae_int_t seed,
ae_int_t leastfactor,
ae_int_t* best,
ae_state *_state);
#endif
#if defined(AE_COMPILE_NEARUNITYUNIT) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_ALGLIBBASICS) || !defined(AE_PARTIAL_BUILD)
#endif
#if defined(AE_COMPILE_SCODES) || !defined(AE_PARTIAL_BUILD)
ae_int_t getrdfserializationcode(ae_state *_state)
{
ae_int_t result;
result = 1;
return result;
}
ae_int_t getkdtreeserializationcode(ae_state *_state)
{
ae_int_t result;
result = 2;
return result;
}
ae_int_t getmlpserializationcode(ae_state *_state)
{
ae_int_t result;
result = 3;
return result;
}
ae_int_t getmlpeserializationcode(ae_state *_state)
{
ae_int_t result;
result = 4;
return result;
}
ae_int_t getrbfserializationcode(ae_state *_state)
{
ae_int_t result;
result = 5;
return result;
}
ae_int_t getspline2dserializationcode(ae_state *_state)
{
ae_int_t result;
result = 6;
return result;
}
ae_int_t getidwserializationcode(ae_state *_state)
{
ae_int_t result;
result = 7;
return result;
}
ae_int_t getknnserializationcode(ae_state *_state)
{
ae_int_t result;
result = 108;
return result;
}
#endif
#if defined(AE_COMPILE_APSERV) || !defined(AE_PARTIAL_BUILD)
/*************************************************************************
Internally calls SetErrorFlag() with condition:
Abs(Val-RefVal)>Tol*Max(Abs(RefVal),S)
This function is used to test relative error in Val against RefVal, with
relative error being replaced by absolute when scale of RefVal is less
than S.
This function returns value of COND.
*************************************************************************/
void seterrorflagdiff(ae_bool* flag,
double val,
double refval,
double tol,
double s,
ae_state *_state)
{
ae_set_error_flag(flag, ae_fp_greater(ae_fabs(val-refval, _state),tol*ae_maxreal(ae_fabs(refval, _state), s, _state)), __FILE__, __LINE__, "apserv.ap:162");
}
/*************************************************************************
The function always returns False.
It may be used sometimes to prevent spurious warnings.
-- ALGLIB --
Copyright 17.09.2012 by Bochkanov Sergey
*************************************************************************/
ae_bool alwaysfalse(ae_state *_state)
{
ae_bool result;
result = ae_false;
return result;
}
/*************************************************************************
The function "touches" integer - it is used to avoid compiler messages
about unused variables (in rare cases when we do NOT want to remove these
variables).
-- ALGLIB --
Copyright 17.09.2012 by Bochkanov Sergey
*************************************************************************/
void touchint(ae_int_t* a, ae_state *_state)
{
}
/*************************************************************************
The function "touches" real - it is used to avoid compiler messages
about unused variables (in rare cases when we do NOT want to remove these
variables).
-- ALGLIB --
Copyright 17.09.2012 by Bochkanov Sergey
*************************************************************************/
void touchreal(double* a, ae_state *_state)
{
}
/*************************************************************************
The function performs zero-coalescing on real value.
NOTE: no check is performed for B<>0
-- ALGLIB --
Copyright 18.05.2015 by Bochkanov Sergey
*************************************************************************/
double coalesce(double a, double b, ae_state *_state)
{
double result;
result = a;
if( ae_fp_eq(a,0.0) )
{
result = b;
}
return result;
}
/*************************************************************************
The function performs zero-coalescing on integer value.
NOTE: no check is performed for B<>0
-- ALGLIB --
Copyright 18.05.2015 by Bochkanov Sergey
*************************************************************************/
ae_int_t coalescei(ae_int_t a, ae_int_t b, ae_state *_state)
{
ae_int_t result;
result = a;
if( a==0 )
{
result = b;
}
return result;
}
/*************************************************************************
The function convert integer value to real value.
-- ALGLIB --
Copyright 17.09.2012 by Bochkanov Sergey
*************************************************************************/
double inttoreal(ae_int_t a, ae_state *_state)
{
double result;
result = (double)(a);
return result;
}
/*************************************************************************
The function calculates binary logarithm.
NOTE: it costs twice as much as Ln(x)
-- ALGLIB --
Copyright 17.09.2012 by Bochkanov Sergey
*************************************************************************/
double logbase2(double x, ae_state *_state)
{
double result;
result = ae_log(x, _state)/ae_log((double)(2), _state);
return result;
}
/*************************************************************************
This function compares two numbers for approximate equality, with tolerance
to errors as large as tol.
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
ae_bool approxequal(double a, double b, double tol, ae_state *_state)
{
ae_bool result;
result = ae_fp_less_eq(ae_fabs(a-b, _state),tol);
return result;
}
/*************************************************************************
This function compares two numbers for approximate equality, with tolerance
to errors as large as max(|a|,|b|)*tol.
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
ae_bool approxequalrel(double a, double b, double tol, ae_state *_state)
{
ae_bool result;
result = ae_fp_less_eq(ae_fabs(a-b, _state),ae_maxreal(ae_fabs(a, _state), ae_fabs(b, _state), _state)*tol);
return result;
}
/*************************************************************************
This function generates 1-dimensional general interpolation task with
moderate Lipshitz constant (close to 1.0)
If N=1 then suborutine generates only one point at the middle of [A,B]
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
void taskgenint1d(double a,
double b,
ae_int_t n,
/* Real */ ae_vector* x,
/* Real */ ae_vector* y,
ae_state *_state)
{
ae_int_t i;
double h;
ae_vector_clear(x);
ae_vector_clear(y);
ae_assert(n>=1, "TaskGenInterpolationEqdist1D: N<1!", _state);
ae_vector_set_length(x, n, _state);
ae_vector_set_length(y, n, _state);
if( n>1 )
{
x->ptr.p_double[0] = a;
y->ptr.p_double[0] = 2*ae_randomreal(_state)-1;
h = (b-a)/(n-1);
for(i=1; i<=n-1; i++)
{
if( i!=n-1 )
{
x->ptr.p_double[i] = a+(i+0.2*(2*ae_randomreal(_state)-1))*h;
}
else
{
x->ptr.p_double[i] = b;
}
y->ptr.p_double[i] = y->ptr.p_double[i-1]+(2*ae_randomreal(_state)-1)*(x->ptr.p_double[i]-x->ptr.p_double[i-1]);
}
}
else
{
x->ptr.p_double[0] = 0.5*(a+b);
y->ptr.p_double[0] = 2*ae_randomreal(_state)-1;
}
}
/*************************************************************************
This function generates 1-dimensional equidistant interpolation task with
moderate Lipshitz constant (close to 1.0)
If N=1 then suborutine generates only one point at the middle of [A,B]
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
void taskgenint1dequidist(double a,
double b,
ae_int_t n,
/* Real */ ae_vector* x,
/* Real */ ae_vector* y,
ae_state *_state)
{
ae_int_t i;
double h;
ae_vector_clear(x);
ae_vector_clear(y);
ae_assert(n>=1, "TaskGenInterpolationEqdist1D: N<1!", _state);
ae_vector_set_length(x, n, _state);
ae_vector_set_length(y, n, _state);
if( n>1 )
{
x->ptr.p_double[0] = a;
y->ptr.p_double[0] = 2*ae_randomreal(_state)-1;
h = (b-a)/(n-1);
for(i=1; i<=n-1; i++)
{
x->ptr.p_double[i] = a+i*h;
y->ptr.p_double[i] = y->ptr.p_double[i-1]+(2*ae_randomreal(_state)-1)*h;
}
}
else
{
x->ptr.p_double[0] = 0.5*(a+b);
y->ptr.p_double[0] = 2*ae_randomreal(_state)-1;
}
}
/*************************************************************************
This function generates 1-dimensional Chebyshev-1 interpolation task with
moderate Lipshitz constant (close to 1.0)
If N=1 then suborutine generates only one point at the middle of [A,B]
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
void taskgenint1dcheb1(double a,
double b,
ae_int_t n,
/* Real */ ae_vector* x,
/* Real */ ae_vector* y,
ae_state *_state)
{
ae_int_t i;
ae_vector_clear(x);
ae_vector_clear(y);
ae_assert(n>=1, "TaskGenInterpolation1DCheb1: N<1!", _state);
ae_vector_set_length(x, n, _state);
ae_vector_set_length(y, n, _state);
if( n>1 )
{
for(i=0; i<=n-1; i++)
{
x->ptr.p_double[i] = 0.5*(b+a)+0.5*(b-a)*ae_cos(ae_pi*(2*i+1)/(2*n), _state);
if( i==0 )
{
y->ptr.p_double[i] = 2*ae_randomreal(_state)-1;
}
else
{
y->ptr.p_double[i] = y->ptr.p_double[i-1]+(2*ae_randomreal(_state)-1)*(x->ptr.p_double[i]-x->ptr.p_double[i-1]);
}
}
}
else
{
x->ptr.p_double[0] = 0.5*(a+b);
y->ptr.p_double[0] = 2*ae_randomreal(_state)-1;
}
}
/*************************************************************************
This function generates 1-dimensional Chebyshev-2 interpolation task with
moderate Lipshitz constant (close to 1.0)
If N=1 then suborutine generates only one point at the middle of [A,B]
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
void taskgenint1dcheb2(double a,
double b,
ae_int_t n,
/* Real */ ae_vector* x,
/* Real */ ae_vector* y,
ae_state *_state)
{
ae_int_t i;
ae_vector_clear(x);
ae_vector_clear(y);
ae_assert(n>=1, "TaskGenInterpolation1DCheb2: N<1!", _state);
ae_vector_set_length(x, n, _state);
ae_vector_set_length(y, n, _state);
if( n>1 )
{
for(i=0; i<=n-1; i++)
{
x->ptr.p_double[i] = 0.5*(b+a)+0.5*(b-a)*ae_cos(ae_pi*i/(n-1), _state);
if( i==0 )
{
y->ptr.p_double[i] = 2*ae_randomreal(_state)-1;
}
else
{
y->ptr.p_double[i] = y->ptr.p_double[i-1]+(2*ae_randomreal(_state)-1)*(x->ptr.p_double[i]-x->ptr.p_double[i-1]);
}
}
}
else
{
x->ptr.p_double[0] = 0.5*(a+b);
y->ptr.p_double[0] = 2*ae_randomreal(_state)-1;
}
}
/*************************************************************************
This function checks that all values from X[] are distinct. It does more
than just usual floating point comparison:
* first, it calculates max(X) and min(X)
* second, it maps X[] from [min,max] to [1,2]
* only at this stage actual comparison is done
The meaning of such check is to ensure that all values are "distinct enough"
and will not cause interpolation subroutine to fail.
NOTE:
X[] must be sorted by ascending (subroutine ASSERT's it)
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
ae_bool aredistinct(/* Real */ ae_vector* x,
ae_int_t n,
ae_state *_state)
{
double a;
double b;
ae_int_t i;
ae_bool nonsorted;
ae_bool result;
ae_assert(n>=1, "APSERVAreDistinct: internal error (N<1)", _state);
if( n==1 )
{
/*
* everything is alright, it is up to caller to decide whether it
* can interpolate something with just one point
*/
result = ae_true;
return result;
}
a = x->ptr.p_double[0];
b = x->ptr.p_double[0];
nonsorted = ae_false;
for(i=1; i<=n-1; i++)
{
a = ae_minreal(a, x->ptr.p_double[i], _state);
b = ae_maxreal(b, x->ptr.p_double[i], _state);
nonsorted = nonsorted||ae_fp_greater_eq(x->ptr.p_double[i-1],x->ptr.p_double[i]);
}
ae_assert(!nonsorted, "APSERVAreDistinct: internal error (not sorted)", _state);
for(i=1; i<=n-1; i++)
{
if( ae_fp_eq((x->ptr.p_double[i]-a)/(b-a)+1,(x->ptr.p_double[i-1]-a)/(b-a)+1) )
{
result = ae_false;
return result;
}
}
result = ae_true;
return result;
}
/*************************************************************************
This function checks that two boolean values are the same (both are True
or both are False).
-- ALGLIB --
Copyright 02.12.2009 by Bochkanov Sergey
*************************************************************************/
ae_bool aresameboolean(ae_bool v1, ae_bool v2, ae_state *_state)
{
ae_bool result;
result = (v1&&v2)||(!v1&&!v2);
return result;
}
/*************************************************************************
Resizes X and fills by zeros
-- ALGLIB --
Copyright 20.03.2009 by Bochkanov Sergey
*************************************************************************/
void setlengthzero(/* Real */ ae_vector* x,
ae_int_t n,
ae_state *_state)
{
ae_int_t i;
ae_assert(n>=0, "SetLengthZero: N<0", _state);
ae_vector_set_length(x, n, _state);
for(i=0; i<=n-1; i++)
{
x->ptr.p_double[i] = (double)(0);
}
}
/*************************************************************************
If Length(X)<N, resizes X
-- ALGLIB --
Copyright 20.03.2009 by Bochkanov Sergey
*************************************************************************/
void bvectorsetlengthatleast(/* Boolean */ ae_vector* x,
ae_int_t n,
ae_state *_state)
{
if( x->cnt<n )
{
ae_vector_set_length(x, n, _state);
}
}
/*************************************************************************
If Length(X)<N, resizes X
-- ALGLIB --
Copyright 20.03.2009 by Bochkanov Sergey
*************************************************************************/
void ivectorsetlengthatleast(/* Integer */ ae_vector* x,
ae_int_t n,
ae_state *_state)
{
if( x->cnt<n )
{
ae_vector_set_length(x, n, _state);
}
}
/*************************************************************************
If Length(X)<N, resizes X
-- ALGLIB --
Copyright 20.03.2009 by Bochkanov Sergey
*************************************************************************/
void rvectorsetlengthatleast(/* Real */ ae_vector* x,
ae_int_t n,
ae_state *_state)
{
if( x->cnt<n )
{
ae_vector_set_length(x, n, _state);
}
}
/*************************************************************************
If Cols(X)<N or Rows(X)<M, resizes X
-- ALGLIB --
Copyright 20.03.2009 by Bochkanov Sergey