-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfulgurance.h
5493 lines (5132 loc) · 163 KB
/
fulgurance.h
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
#include <iostream>
#include <vector>
#include <deque>
#include <math.h>
#include <chrono>
#include <thread>
//@I Stylished documentation is available <a href="https://julienlargetpiet.tech/static/files/fulgurance.html">here</a>
//@I In current development.
//@I This framework provides functions for statistical analysis, machine learning, parsing and data manipulation with its own implementation of matrices and dataframes. Other tools can be found at fulgurance_tools part.
//@I The framework is developped with C++ 14 but should work properly with 11 and 17 and furthers.
//@I The main branch provides algorithms developped on the top of stl vector, but a deque version is coming.
//@G2 Philosophy
//@I Matrices and dataframes implementation are classes. All functions that will transform 'voidly' (internaly) the relative data are built in the classes. All functions that copy and transform the relative data are extern to classes.
//@I Also, all the functions relative to matrices classes exist for more standard type of matrices that is stl 2D vectors.
//@X
//@L1 Commun functions
//@L2 On elements
//@L3 Standard operations
//@T mod
//@U template <typename T> double mod(T &dividend, T &divider)
//@X
//@D Returns the mod of 2 number (int, float or double)
//@A a : is an the dividend (int, float, double)
//@A b : is the divider (int, float, double)
//@X
//@E float a = 45.216;
//@E float b = 3.2164;
//@E mod(a, b)
//@E 0.186401
//@X
template <typename T> double mod(T ÷nd, T ÷r) {
int x = dividend / divider;
return dividend - x * divider;
};
//@T int_lngth
//@U int int_lngth(const int &x)
//@X
//@D Returns the length of an int.
//@A x : is an int
//@X
//@E int a = 896;
//@E int_lngth(a);
//@E 3
//@X
int int_lngth(const int &x) {
float x2 = x;
int i = 0;
while (x2 >= 1) {
x2 /= 10;
i +=1;
};
return i;
}
//@T roundout
//@U template <typename T> T roundout(T x, int n)
//@X
//@D Returns a rounded value with decimal precision.
//@A x : is an int, float, double
//@A n : is an int indicating the decimal precision
//@X
//@E float x = 34.476;
//@E int n = 2;
//@E float out = roundout(x, n);
//@E 34.48
//@E n = 0;
//@E out = roundout(x, n);
//@E 34
//@E n = -1;
//@E out = roundout(x, n)
//@E 30
//@X
template <typename T> T roundout(T x, int n) {
unsigned int mlt = 1;
int binc;
if (n > -1) {
for (unsigned int i = 0; i < n; ++i) {
mlt *= 10;
};
x *= mlt;
binc = x;
if (x - binc > 0.5) {
binc += 1;
};
return (double)binc / mlt;
} else {
n *= -1;
for (unsigned int i = 0; i < n; ++i) {
mlt *= 10;
};
x /= mlt;
binc = x;
if (x - binc > 0.5) {
binc += 1;
};
return (double)binc * mlt;
};
};
//@T roundin
//@U template <typename T> void roundin(T &x, int n)
//@X
//@D Transforms the input value to a rounded value with decimal precision.
//@A x : is an int, float, double
//@A n : is an int indicating the decimal precision
//@X
//@E float x = 34.476
//@E int n = 2;
//@E roundin(x, n);
//@E 34.48
//@E n = 0;
//@E x = 67.754;
//@E roundin(x, n);
//@E 68
//@E n = -1;
//@E roundin(x, n);
//@E 70
//@X
template <typename T> void roundin(T &x, int n) {
unsigned int mlt = 1;
int binc;
if (n > -1) {
for (unsigned int i = 0; i < n; ++i) {
mlt *= 10;
};
x *= mlt;
binc = x;
if (x - binc > 0.5) {
binc += 1;
};
x = (double)binc / mlt;
} else {
n *= -1;
for (unsigned int i = 0; i < n; ++i) {
mlt *= 10;
};
x /= mlt;
binc = x;
if (x - binc > 0.5) {
binc += 1;
};
x = (double)binc * mlt;
};
};
//@T randint
//@U auto randint(const int &min, const int max, int seed = -1)
//@X
//@D Returns a pseudo-random number between min and max.
//@A min : is an int
//@A max : is a max
//@A seed : is an int that determines the pseudo-random output, defaults to -1, so the seed will be randomly picked up by default, if you want to determine the output, choose a seed between 0 and 9.
//@X
//@E int min = -300;
//@E int max = 100;
//@E randint(min, max);
//@E -14
//@E randint(min, max);
//@E -231
//@E // If you want to generate a float just do:
//@E double x = randint(min, max);
//@E min = 0;
//@E max = 900;
//@E x += randint(min, max) / 1000;
//@E -13.257
//@X
int randint(const int &min, const int max, int seed = -1) {
unsigned int cnt = 0;
unsigned long int mlc = 1;
unsigned int offset;
if (abs(max) > abs(min)) {
offset = abs(max);
} else {
offset = abs(min);
};
while (offset >= 1) {
offset /= 10;
cnt += 1;
};
double rtn;
unsigned int cur_val;
long unsigned int cur_valint;
unsigned int cnt2 = 0;
std::vector<int> vec = {4, 8, 1, 2, 9, 0, 5, 3, 6, 7};
unsigned int addr;
unsigned int delta = (max - min) + 1;
unsigned int post_rtn;
if (seed == -1) {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
rtn = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
rtn /= 10;
mlc = rtn;
rtn -= mlc;
rtn *= 10;
post_rtn = rtn;
if (rtn - post_rtn > 0.5) {
post_rtn += 1;
};
} else {
rtn = (int)seed;
};
if (mlc % 9 == 0) {
addr = 3;
} else if (mlc % 7 == 0) {
addr = 4;
} else if (mlc % 8 == 0) {
addr = 7;
} else if (mlc % 6 == 0) {
addr = 9;
} else if (mlc % 3 == 0) {
addr = 13;
} else if (mlc % 4 == 0) {
addr = 1;
} else if (mlc % 2 == 0) {
addr = 8;
} else {
addr = 5;
};
cnt2 = 1;
cur_val = rtn;
while (cnt2 < cnt) {
cur_valint = pow(cur_val, 2);
cur_val = vec[(cur_valint + addr) % 10];
post_rtn *= 10;
post_rtn += cur_val;
cnt2 += 1;
};
cur_valint = post_rtn;
return (cur_valint % delta) + min;
};
//@T logn
//@U template <typename T, typename T2> double logn(T &val, T2 &base)
//@X
//@D Returns the logarithm of any positive number to any base. This generalizes the <code>log(value) / log(base)</code> method.
//@A val : is the value of the logarith base n (must be positive)
//@A base : is the base of the logarithm
//@X
//@E double val = 2.63;
//@E int base = 10;
//@E logn(val, base);
//@E 0.419956
//@E base = 2;
//@E 1.39506
//@X
template <typename T, typename T2> double logn(T &val, T2 &base) {
return log(val) / log(base);
};
//@T Facto
//@U unsigned int Facto(unsigned int x)
//@X
//@D Returns the factorial of a positive integer.
//@A x : is a unsigned integer
//@X
//@E Facto(7);
//@E 5040
//@E Facto(0);
//@E 1
//@X
unsigned int Facto(unsigned int x) {
if (x == 0) {
return 1;
};
unsigned int rtn = x;
x -= 1;
while (x > 1) {
rtn *= x;
x -= 1;
};
return rtn;
};
//@T Comb
//@U double Comb(double r, double n)
//@X
//@D Returns the result of the combination formula for given parameters.
//@A r : is the number of objects choosen among the set
//@A n : is the number of objects in the set
//@X
//@E Comb(2, 5);
//@E 10
//@E Comb(5, 12);
//@E 792
//@X
double Comb(double r, double n) {
if ((long unsigned int)n % 2 == 0) {
if (r > n / 2) {
r = n - r;
};
} else if ((long unsigned int)n / 2 + 1 < r) {
r = n - r;
};
double rslt = n / r;
r -= 1;
n -= 1;
while (r > 0) {
rslt *= (n / r);
r -= 1;
n -= 1;
};
return rslt;
};
//@L3 String to int, float, double
//@T si
//@U int si(const std::string &x)
//@X
//@D Returns a std::string that can be converted to an int, to an int.
//@A x : is a stl string that can be converted to an int
//@X
//@E std::string a = "341";
//@E int out = si(a);
//@E 341
//@X
int si(const std::string &x) {
int rtn = int(x[0]) - 48;
const int n = x.size();
for (int i = 1; i < n; ++i) {
rtn *= 10;
rtn += (int(x[i]) - 48);
};
return rtn;
}
//@T sf
//@U float sf(const std::string &x)
//@X
//@D Returns a converted std::string that can be converted to a float, to a float. Produces the same results than <code>stof</code>.
//@A x : is a stl string that can be converted to a float
//@X
//@E std::string a = "44.23";
//@E float out = sf(a);
//@E 44.23
//@X
float sf(const std::string &x) { // same results as stof
float rtn = int(x[0]) - 48;
const int n = x.size();
int i = 1;
int m = 1;
while (int(x[i]) != 46) {
rtn *= 10;
rtn += (int(x[i]) - 48);
i += 1;
};
i += 1;
while (i < n) {
m *= 10;
rtn += (float(x[i]) - 48) / m;
i += 1;
};
return rtn;
};
//@T sf2
//@U float sf2(const std::string &x)
//@X
//@D Returns a converted std::string that can be converted to a float, to a float. Uses another algorithm than <code>edm1_sf</code>.
//@A x : is a stl string that can be converted to a float
//@X
//@E std::string a = "44.23";
//@E float out = sf2(a);
//@E 44.23
//@X
float sf2(const std::string &x) {
float rtn = int(x[0]) - 48;
const int n = x.size();
int i = 1;
int m = 1;
while (int(x[i]) != 46) {
rtn *= 10;
rtn += (int(x[i]) - 48);
i += 1;
};
i += 1;
while (i < n) {
m *= 10;
rtn *= 10;
rtn += (int(x[i]) - 48);
i += 1;
};
return rtn / m;
};
//@T stod
//@U double stod(const std::string &x)
//@X
//@D Returns a converted std::string, that can be converted to a double, to a double.
//@A x : is a stl string
//@X
//@E std::string a = "4566.132214";
//@E double out = stod(a);
//@E 4566.132214
//@X
double stod(const std::string &x) {
double rtn = int(x[0]) - 48;
const int n = x.size();
int i = 1;
int m = 1;
while (int(x[i]) != 46) {
rtn *= 10;
rtn += (int(x[i]) - 48);
i += 1;
};
i += 1;
while (i < n) {
m *= 10;
rtn *= 10;
rtn += (int(x[i]) - 48);
i += 1;
};
return rtn / m;
};
//@L3 Int, double, to string
//@T
//@U std::string itos(unsigned int x)
//@X
//@D Returns the input integer as a std string.
//@A : is an unsigned int
//@X
//@E itos(45897);
//@E "45897"
//@X
std::string itos(unsigned int x) {
unsigned int cnt;
unsigned int mlt_val = 1;
std::string rtn_str = "";
while (mlt_val <= x) {
cnt = 0;
while (x % (10 * mlt_val) != 0) {
x = x - mlt_val;
cnt += 1;
};
mlt_val *= 10;
rtn_str = char(cnt + 48) + rtn_str;
};
return rtn_str;
};
//@L2 On std::vector<Type>
//@L3 Statistical functions
//@T sum
//@U template <typename T> T sum(const std::vector<T> &x)
//@X
//@D Returns the sum of all elements in a vector (int, float, double, bool).
//@A x : is a stl vector (int, float, double, bool)
//@X
//@E std::vector<double> vec = {1.434, 22.3322, 32423.097};
//@E double out = sum(vec);
//@E 32446.8632
//@X
template <typename T> double sum(const std::vector<T> &x) {
double rtn = 0;
for (typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); ++it) {
rtn += *it;
};
return rtn;
};
//@T Mean
//@U template <typename T> T Mean(const std::vector<T> &x)
//@X
//@D Returns the mean of all elements in a vector (int, float, double, bool).
//@A x : is a stl vector (int, float, double, bool)
//@X
//@E std::vector<int> vec = {1, 4, 2};
//@E double out = Mean(vec);
//@E 2.333333
//@X
template <typename T> T Mean(const std::vector<T> &x) {
float rtn = 0;
for (typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); ++it) {
rtn += *it;
}
return rtn / x.size();
};
//@T quantile
//@U template <typename T, typename T2> double quantile(std::vector<T> &x, T2 &prob, double precision = 0.001)
//@X
//@D Returns the quantile value for a given probability between 0 and 1 for an input stl vector (int, float, double, bool). If you just want to calculate median, the <code>med()</code> function is more straight forward.
//@A x : stl vector (int, float, double, bool), must be ascendly sorted
//@A prob : is the probability(float, double)
//@A precision : is a double value representing the accuracy of the result. The lower the value is, higher the accuracy will be.
//@X
//@E std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//@E std::vector<int> vec2 = {1, 2, 3, 4};
//@E double prob = 0.89;
//@E quantile(vec2, prob);
//@E 3.67188
//@E prob = 0.65;
//@E quantile(vec, prob);
//@E 6.84375
//@X
template <typename T, typename T2> double quantile(std::vector<T> &x, T2 &prob, double precision = 0.001) {
double n = x.size();
double pre_rnd = (n - 1) * prob;
unsigned int idx = pre_rnd;
if (pre_rnd - idx > 0.5) {
idx += 1;
};
const unsigned int fidx = idx;
double l_lim = prob - precision;
double h_lim = prob + precision;
double cur_vl = idx / (n - 1);
double rtn_val = x[idx];
double rtn_val2;
double divider = 2;
if (cur_vl < l_lim) {
rtn_val2 = (x[idx + 1] - x[idx]) / divider;
} else if (cur_vl > l_lim) {
rtn_val2 = (x[idx] - x[idx - 1]) / divider;
};
while (cur_vl < l_lim || cur_vl > h_lim) {
if (cur_vl < l_lim) {
idx += idx;
idx += 1;
n += (n - 1);
rtn_val += rtn_val2;
} else {
idx += idx;
idx -= 1;
n += (n - 1);
rtn_val -= rtn_val2;
};
cur_vl = idx / (n - 1);
rtn_val2 /= 2;
};
return rtn_val;
};
//@T med
//@U template <typename T> double med(std::vector<T> &x)
//@X
//@D Returns the median of a stl vector (int, float, double, bool).
//@A x : is an stl vector (int, float, double, bool), must be ascendly sorted
//@X
//@E std::vector<int> vec = {1, 2, 3, 4};
//@E double out = med(vec);
//@E 2.5
//@X
template <typename T> double med(std::vector<T> &x) {
const unsigned int n = x.size();
const unsigned int idx = (n - 1) * 0.5;
if (n % 2 == 0) {
double rslt = x[idx] + x[idx + 1];
return rslt / 2;
} else {
return(x[idx]);
};
};
//@T cor
//@U template <typename T, typename T2> double cor(const std::vector<T> &x, const std::vector<T2> &x2)
//@X
//@D Returns the correlation between two variables / two stl vectors (int, float, double, bool)
//@A x : is an stl vector (int, float, double, bool)
//@A x2 : is an stl vector (int, float, double, bool)
//@X
//@E std::vector<int> vec1 = {1, 2, 3, 4, 5, 6};
//@E std::vector<int> vec2 = {-6, -5, -4, -3, -2, -1};
//@E double out = cor(vec1, vec2);
//@E 1
//@X
template <typename T, typename T2> double cor(const std::vector<T> &x, const std::vector<T2> &x2) {
double m1 = x[0];
double m2 = x2[0];
int n = x.size();
int i;
for (i = 1; i < n; ++i) {
m1 += x[i];
m2 += x2[i];
};
m1 /= n;
m2 /= n;
double c1 = (x[0] - m1);
double c2 = (x2[0] - m2);
double d1 = c1 * c2;
double s1 = c1 * c1;
double s2 = c2 * c2;
for (i = 1; i < n; ++i) {
c1 = (x[i] - m1);
c2 = (x2[i] - m2);
d1 += (c1 * c2);
s1 += (c1 * c1);
s2 += (c2 * c2);
};
double rtn = d1 / sqrt(s1 * s2);
return rtn;
};
//@T Sd
//@U template <typename T> double Sd(std::vector<T> &x)
//@X
//@D Returns the standard deviation of a stl vector (int, float, double, bool).
//@A x : is an stl vector (int, float, double, bool)
//@X
//@E std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 4, 5};
//@E double out = Sd(vec);
//@E 1.224745
//@X
template <typename T> double Sd(std::vector<T> &x) {
unsigned int i;
const double n = x.size();
double mean = x[0];
for (i = 1; i < n; ++i) {
mean += x[i];
};
mean /= n;
double delta_sq = 0;
for (i = 0; i < n; ++i) {
delta_sq += pow(x[i] - mean, 2);
};
return pow(delta_sq / n, 0.5);
};
//@L4 Uniform distribution
//@T dunif
//@U template <typename T> std::vector<double> dunif(std::vector<T> &x, double &min, double &max)
//@X
//@D Returns the probability distribution of the uniform distribution.
//@A x : is a vector containing all the values you want the probability from
//@A min : is the minimum of the uniform distribution
//@A max : is the maximum of the uniform distribution
//@X
//@E double min = -2;
//@E double max = 10;
//@E std::vector<double> vec = {-7, -2, 3.5, 8, 12, 56};
//@E std::vector<double> out = dunif(vec, min, max);
//@E print_nvec(out);
//@E :0: 0 0.0833333 0.0833333 0.0833333 0 0
//@X
template <typename T> std::vector<double> dunif(std::vector<T> &x, double &min, double &max) {
double cur_prob = 1 / (max - min);
std::vector<double> rtn_v;
for (double i : x) {
if (i >= min & i <= max) {
rtn_v.push_back(cur_prob);
} else {
rtn_v.push_back(0);
}
};
return rtn_v;
};
//@T punif
//@U template <typename T> std::vector<double> punif(std::vector<T> &x, double &min, double &max, double step = 0.01)
//@X
//@D Returns the cumulative probablity distribution of the uniform distribution.
//@A x : is a vector containing the values you want the cumulative probability from, must be ascendly sorted
//@A min : is the minimum of the probability distribution
//@A max : is the maximum of the probability distribution
//@A step : the lower it is, the more accurate the result gets
//@X
//@E double min = -2;
//@E double max = 10;
//@E std::vector<double> vec = {-7, -2, 3.5, 8, 12, 56};
//@E std::vector<double> out = punif(vec, min, max);
//@E print_nvec(out);
//@E :0: 0 0.000833333 0.459167 0.834167 1 1
//@X
template <typename T> std::vector<double> punif(std::vector<T> &x, double &min, double &max, double step = 0.01) {
double cur_prob = 0;
double ref_prob = 1 / (max - min) * step;
std::vector<double> rtn_v;
unsigned int i = 0;
const unsigned int n = x.size();
while (x[i] < min) {
i += 1;
rtn_v.push_back(0);
};
double lst_x = x[i];
while (i < n) {
if (x[i] <= max) {
while (lst_x <= x[i]) {
cur_prob += ref_prob;
lst_x += step;
};
rtn_v.push_back(cur_prob);
} else {
while (i < n) {
rtn_v.push_back(1);
i+= 1;
};
return rtn_v;
}
i += 1;
};
return rtn_v;
};
//@T qunif
//@U std::vector<double> qunif(std::vector<double> &x, double &min, double &max)
//@X
//@D Returns the quantile of the uniform distribution.
//@A x : is the probability vector
//@A min : is the minimum of the uniform distribution
//@A max : is the maximum of the uniform distribution
//@X
//@E double min = -2;
//@E double max = 10;
//@E std::vector<double> vec = {-7, -2, 3.5, 8, 12, 56};
//@E std::vector<double> vec2 = {0.2, 0.4, 0.5, 0.6, 0.75};
//@E std::vector<double> out = qunif(vec2, min, max);
//@E print_nvec(out);
//@E :0: 3.6 5.2 6 6.8 8
//@X
std::vector<double> qunif(std::vector<double> &x, double &min, double &max) {
double coeff = 1 / (max - min);
std::vector<double> rtn_v;
for (double i : x) {
rtn_v.push_back(i / coeff + min);
};
return rtn_v;
};
//@T runif
//@U std::vector<double> unif(unsigned int &n, double &min, double &max, double noise = 0.1, int seed = -1)
//@X
//@D Returns a stl double vector containing pseudo-random uniform distribution between a min and max.
//@A n : is the number of elements of the output stl vector
//@A min : is the minimum of the uniform distribution
//@A max : is the maximum of the uniform distribution
//@A noise : is the noise in the returnde uniform distribution
//@A seed : is an int, controlling the output values, defaults to -1, so by default the function returns pseudo-random uniform distribution. If you want to manually control the output, enter a positive int for this parameter.
//@X
//@E unsigned int n = 1500;
//@E double min = 1;
//@E double max = 55;
//@E std::vector<double> out = unif(n, min, max);
//@E print_nvec(out);
//@E :0: 1 1.03701 1.07557 1.10951 1.14757 1.18151 1.21957 1.25351 1.29157 1.32551 1.36357 1.39751 1.43557 1.46951 1.50757 1.54151 1.57957 1.61351 1.65157 1.68551 1.72357 1.75751 1.79557 1.82951
//@E ...
//@E :1475: 54.1015 54.1396 54.1735 54.2116 54.2455 54.2836 54.3175 54.3556 54.3895 54.4276 54.4615 54.4996 54.5335 54.5716 54.6055 54.6436 54.6775 54.7156 54.7495 54.7876 54.8215 54.8596 54.8935 54.9316
//@E :1500: 55
//@X
std::vector<double> runif(unsigned &n, double &min, double &max, double noise = 0.1, int seed = -1) {
long double step;
unsigned long int valint = 1;
unsigned int i;
if (seed == -1) {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
step = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
step /= 10;
valint = step;
step -= valint;
step *= 10;
if (step == 0) {
i = 1;
while (valint % i != 0) {
i+= 1;
};
step += i;
};
} else {
step = seed;
};
double cur_step = (max - min) / n;
std::vector<double> rtn_v;
rtn_v.reserve(n);
std::vector<int> ref_vec = {5, 3, 1, 6, 8, 2, 4, 7, 9};
unsigned int addr;
if (valint % 9 == 0) {
addr = 3;
} else if (valint % 7 == 0) {
addr = 4;
} else if (valint % 8 == 0) {
addr = 7;
} else if (valint % 6 == 0) {
addr = 9;
} else if (valint % 3 == 0) {
addr = 13;
} else if (valint % 4 == 0) {
addr = 1;
} else if (valint % 2 == 0) {
addr = 8;
} else {
addr = 5;
};
unsigned long int Step = step;
unsigned int lst_val;
rtn_v.push_back(min);
for (i = 1; i < n - 1; ++i) {
lst_val = ref_vec[Step % 9] + addr;
rtn_v.push_back(noise * cur_step * sin(lst_val) + min + cur_step * i);
Step += lst_val;
};
rtn_v.push_back(max);
return rtn_v;
};
//@L4 Normal distribution
//@T rnorm
//@U std::vector<double> rnorm(unsigned int &n, double &mean, double &sd, double noise = 0.05, int seed = -1)
//@X
//@D Returns a pseudo-random normal distribution as a double stl vector. Note, if you can it is preferable to choose the smallest standard deviation possible to increase speed. Example: N(14, 10) -> N(1.4, 1).
//@A n : is the number of elements in the output stl vector
//@A mean : is the mean of the normal distribution
//@A sd : is the standard deviation of the normal distribution
//@A noise : is the noise, defaults to 0.05
//@A seed : is an int that dictates the result, defaults to -1, so by default the output is pseudo-random
//@X
//@E unsigned int n = 10000;
//@E double sd = 250;
//@E double mean = 155;
//@E std::vector<double> out;
//@E double result;
//@E out = rnorm(n, mean, sd);
//@E Sd(out);
//@E 250.6228
//@E Mean(out);
//@E 154.9945
//@M example.jpg
//@X
std::vector<double> rnorm(unsigned int &n, double &mean, double &sd, double noise = 0.05, int seed = -1) {
long double step;
unsigned long int valint = 1;
unsigned int i;
bool evenval;
if (n % 2 == 0) {
evenval = 0;
} else {
evenval = 1;
n += 1;
};
if (seed == -1) {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
step = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
step /= 10;
valint = step;
step -= valint;
step *= 10;
if (step == 0) {
i = 1;
while (valint % i != 0) {
i+= 1;
};
step += i;
};
} else {
step = seed;
};
std::vector<int> ref_vec = {5, 3, 1, 6, 8, 2, 4, 7, 9};
unsigned int addr;
long double n2 = n;
long double prob_step = 1 / n2;
if (valint % 9 == 0) {
addr = 3;
} else if (valint % 7 == 0) {
addr = 4;
} else if (valint % 8 == 0) {
addr = 7;
} else if (valint % 6 == 0) {
addr = 9;
} else if (valint % 3 == 0) {
addr = 13;
} else if (valint % 4 == 0) {
addr = 1;
} else if (valint % 2 == 0) {
addr = 8;
} else {
addr = 5;
};
std::vector<double> rtn_v = {};
rtn_v.reserve(n);
double cur_prob = 0;
double x_step = 1 / n2;
double x_step2 = 0;
unsigned int cur_n = 0;
unsigned int Step = step;
double cur_noise;
double cnst = 1 / (sd * sqrt(2 * M_PI));
while (cur_n < n) {
cur_prob += cnst * exp(-0.5 * (x_step2 / sd) * (x_step2 / sd)) * x_step;
while ((cur_prob - prob_step) >= 0) {
Step += ref_vec[Step % 9] + addr;
cur_noise = sin(Step) * noise * x_step2;
rtn_v.push_back(mean + x_step2 + cur_noise);
Step += ref_vec[Step % 9] + addr;
cur_noise = sin(Step) * noise * x_step2;
rtn_v.push_back(mean - x_step2 + cur_noise);
cur_prob -= prob_step;
cur_n += 2;
};
x_step2 += x_step;
};
if (evenval) {
rtn_v.pop_back();
};
return rtn_v;
};
//@T rnorm2
//@U std::vector<double> rnorm2(unsigned int &n, double &mean, double &sd, double noise = 0.05, int seed = -1)
//@X
//@D Same as <code>norm()</code>, but faster and less accurate.
//@A n : is the number of elements in the output stl vector
//@A mean : is the mean of the normal distribution
//@A sd : is the standard deviation of the normal distribution
//@A noise : is the noise, defaults to 0.05
//@A seed : is an int that dictates the result, defaults to -1, so by default the output is pseudo-random
//@X
//@E unsigned int n = 10000;
//@E double sd = 50;
//@E double mean = 155;
//@E std::vector<double> out;
//@E double result;
//@E out = rnorm2(n, mean, sd);
//@E Sd(out);
//@E 42.06729
//@E Mean(out);
//@E 155.0009
//@M example2.jpg
//@X
std::vector<double> rnorm2(unsigned int &n, double &mean, double &sd, double noise = 0.05, int seed = -1) {
long double step;
bool evenval;
if (n % 2 == 0) {
evenval = 0;
} else {
evenval = 1;
};
unsigned long int valint = 1;
unsigned int i;
if (seed == -1) {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
step = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
step /= 10;
valint = step;
step -= valint;
step *= 10;
if (step == 0) {
i = 1;
while (valint % i != 0) {
i+= 1;
};
step += i;
};
} else {
step = seed;
};
std::vector<int> ref_vec = {5, 3, 1, 6, 8, 2, 4, 7, 9};
unsigned int addr;
long double n2 = n;
long double prob_step = 1 / n2;
if (valint % 9 == 0) {
addr = 3;
} else if (valint % 7 == 0) {
addr = 4;
} else if (valint % 8 == 0) {
addr = 7;
} else if (valint % 6 == 0) {
addr = 9;
} else if (valint % 3 == 0) {
addr = 13;
} else if (valint % 4 == 0) {
addr = 1;
} else if (valint % 2 == 0) {
addr = 8;
} else {
addr = 5;