-
Notifications
You must be signed in to change notification settings - Fork 37
/
stat.d
3546 lines (2968 loc) · 88.1 KB
/
stat.d
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
/++
This module contains base statistical algorithms.
Note that used specialized summing algorithms execute more primitive operations
than vanilla summation. Therefore, if in certain cases maximum speed is required
at expense of precision, one can use $(REF_ALTTEXT $(TT Summation.fast), Summation.fast, mir, math, sum)$(NBSP).
License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0)
Authors: Shigeki Karita (original numir code), Ilya Yaroshenko, John Michael Hall
Copyright: 2020 Ilya Yaroshenko, Kaleidic Associates Advisory Limited, Symmetry Investments
Macros:
SUBREF = $(REF_ALTTEXT $(TT $2), $2, mir, math, $1)$(NBSP)
T2=$(TR $(TDNW $(LREF $1)) $(TD $+))
T4=$(TR $(TDNW $(LREF $1)) $(TD $2) $(TD $3) $(TD $4))
+/
module mir.math.stat;
import core.lifetime: move;
import mir.internal.utility: isFloatingPoint;
import mir.math.common: fmamath;
import mir.math.sum;
import mir.ndslice.slice: Slice, SliceKind, hasAsSlice;
import mir.primitives;
import std.traits: Unqual, isArray, isMutable, isIterable, isIntegral, CommonType;
public import mir.math.sum: Summation;
///
package(mir)
template statType(T, bool checkComplex = true)
{
import mir.internal.utility: isFloatingPoint;
static if (isFloatingPoint!T) {
import std.traits: Unqual;
alias statType = Unqual!T;
} else static if (is(T : double)) {
alias statType = double;
} else static if (checkComplex) {
import mir.internal.utility: isComplex;
static if (isComplex!T) {
import std.traits: Unqual;
static if (is(T : cdouble)) {
deprecated("Built-in complex types deprecated in D language version 2.097") alias statType = Unqual!T;
} else {
alias statType = Unqual!T;
}
} else static if (is(T : cdouble)) {
deprecated("Built-in complex types deprecated in D language version 2.097") alias statType = cdouble;
} else {
static assert(0, "statType: type " ~ T.stringof ~ " must be convertible to a complex floating point type");
}
} else {
static assert(0, "statType: type " ~ T.stringof ~ " must be convertible to a floating point type");
}
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static assert(is(statType!int == double));
static assert(is(statType!uint == double));
static assert(is(statType!double == double));
static assert(is(statType!float == float));
static assert(is(statType!real == real));
static assert(is(statType!(const(int)) == double));
static assert(is(statType!(immutable(int)) == double));
static assert(is(statType!(const(double)) == double));
static assert(is(statType!(immutable(double)) == double));
}
version(mir_builtincomplex_test)
@safe pure nothrow @nogc
unittest
{
static assert(is(statType!cfloat == cfloat));
static assert(is(statType!cdouble == cdouble));
static assert(is(statType!creal == creal));
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
import std.complex: Complex;
static assert(is(statType!(Complex!float) == Complex!float));
static assert(is(statType!(Complex!double) == Complex!double));
static assert(is(statType!(Complex!real) == Complex!real));
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
float x;
alias x this;
}
static assert(is(statType!Foo == double)); // note: this is not float
}
version(mir_builtincomplex_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
cfloat x;
alias x this;
}
static assert(is(statType!Foo == cdouble)); // note: this is not Complex!float
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
double x;
alias x this;
}
static assert(is(statType!Foo == double));
}
version(mir_builtincomplex_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
cdouble x;
alias x this;
}
static assert(is(statType!Foo == cdouble));
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
real x;
alias x this;
}
static assert(is(statType!Foo == double)); // note: this is not real
}
version(mir_builtincomplex_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
creal x;
alias x this;
}
static assert(is(statType!Foo == cdouble)); // note: this is not Complex!real
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
int x;
alias x this;
}
static assert(is(statType!Foo == double)); // note: this is not ints
}
///
package(mir)
template meanType(T)
{
import mir.math.sum: sumType;
alias U = sumType!T;
static if (__traits(compiles, {
auto temp = U.init + U.init;
auto a = temp / 2;
temp += U.init;
})) {
alias V = typeof((U.init + U.init) / 2);
alias meanType = statType!V;
} else {
static assert(0, "meanType: Can't calculate mean of elements of type " ~ U.stringof);
}
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static assert(is(meanType!(int[]) == double));
static assert(is(meanType!(double[]) == double));
static assert(is(meanType!(float[]) == float));
}
version(mir_builtincomplex_test)
@safe pure nothrow @nogc
unittest
{
static assert(is(meanType!(cfloat[]) == cfloat));
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
float x;
alias x this;
}
static assert(is(meanType!(Foo[]) == float));
}
version(mir_builtincomplex_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
cfloat x;
alias x this;
}
static assert(is(meanType!(Foo[]) == cfloat));
}
/++
Output range for mean.
+/
struct MeanAccumulator(T, Summation summation)
{
///
size_t count;
///
Summator!(T, summation) summator;
///
F mean(F = T)() const @safe @property pure nothrow @nogc
{
return cast(F) summator.sum / cast(F) count;
}
///
F sum(F = T)() const @safe @property pure nothrow @nogc
{
return cast(F) summator.sum;
}
///
void put(Range)(Range r)
if (isIterable!Range)
{
static if (hasShape!Range)
{
count += r.elementCount;
summator.put(r);
}
else
{
foreach(x; r)
{
count++;
summator.put(x);
}
}
}
///
void put()(T x)
{
count++;
summator.put(x);
}
///
void put(F = T)(MeanAccumulator!(F, summation) m)
{
count += m.count;
summator.put(cast(T) m.summator);
}
}
///
version(mir_test)
@safe pure nothrow
unittest
{
import mir.ndslice.slice: sliced;
MeanAccumulator!(double, Summation.pairwise) x;
x.put([0.0, 1, 2, 3, 4].sliced);
assert(x.mean == 2);
x.put(5);
assert(x.mean == 2.5);
}
version(mir_test)
@safe pure nothrow
unittest
{
import mir.ndslice.slice: sliced;
MeanAccumulator!(float, Summation.pairwise) x;
x.put([0, 1, 2, 3, 4].sliced);
assert(x.mean == 2);
assert(x.sum == 10);
x.put(5);
assert(x.mean == 2.5);
}
version(mir_test)
@safe pure nothrow
unittest
{
double[] x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25];
double[] y = [2.0, 7.5, 5.0, 1.0, 1.5, 0.0];
MeanAccumulator!(float, Summation.pairwise) m0;
m0.put(x);
MeanAccumulator!(float, Summation.pairwise) m1;
m1.put(y);
m0.put(m1);
assert(m0.mean == 29.25 / 12);
}
/++
Computes the mean of the input.
By default, if `F` is not floating point type or complex type, then the result
will have a `double` type if `F` is implicitly convertible to a floating point
type or a type for which `isComplex!F` is true.
Params:
F = controls type of output
summation = algorithm for calculating sums (default: Summation.appropriate)
Returns:
The mean of all the elements in the input, must be floating point or complex type
See_also:
$(SUBREF sum, Summation)
+/
template mean(F, Summation summation = Summation.appropriate)
{
/++
Params:
r = range, must be finite iterable
+/
@fmamath meanType!F mean(Range)(Range r)
if (isIterable!Range)
{
alias G = typeof(return);
MeanAccumulator!(G, ResolveSummationType!(summation, Range, G)) mean;
mean.put(r.move);
return mean.mean;
}
/++
Params:
ar = values
+/
@fmamath meanType!F mean(scope const F[] ar...)
{
alias G = typeof(return);
MeanAccumulator!(G, ResolveSummationType!(summation, const(G)[], G)) mean;
mean.put(ar);
return mean.mean;
}
}
/// ditto
template mean(Summation summation = Summation.appropriate)
{
/++
Params:
r = range, must be finite iterable
+/
@fmamath meanType!Range mean(Range)(Range r)
if (isIterable!Range)
{
alias F = typeof(return);
return .mean!(F, summation)(r.move);
}
/++
Params:
ar = values
+/
@fmamath meanType!T mean(T)(scope const T[] ar...)
{
alias F = typeof(return);
return .mean!(F, summation)(ar);
}
}
/// ditto
template mean(F, string summation)
{
mixin("alias mean = .mean!(F, Summation." ~ summation ~ ");");
}
/// ditto
template mean(string summation)
{
mixin("alias mean = .mean!(Summation." ~ summation ~ ");");
}
///
version(mir_test)
@safe pure nothrow
unittest
{
import mir.ndslice.slice: sliced;
assert(mean([1.0, 2, 3]) == 2);
assert(mean([1.0 + 3i, 2, 3]) == 2 + 1i);
assert(mean!float([0, 1, 2, 3, 4, 5].sliced(3, 2)) == 2.5);
static assert(is(typeof(mean!float([1, 2, 3])) == float));
}
/// Mean of vector
version(mir_test)
@safe pure nothrow
unittest
{
import mir.ndslice.slice: sliced;
auto x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25,
2.0, 7.5, 5.0, 1.0, 1.5, 0.0].sliced;
assert(x.mean == 29.25 / 12);
}
/// Mean of matrix
version(mir_test)
@safe pure
unittest
{
import mir.ndslice.fuse: fuse;
auto x = [
[0.0, 1.0, 1.5, 2.0, 3.5, 4.25],
[2.0, 7.5, 5.0, 1.0, 1.5, 0.0]
].fuse;
assert(x.mean == 29.25 / 12);
}
/// Column mean of matrix
version(mir_test)
@safe pure
unittest
{
import mir.ndslice.fuse: fuse;
import mir.ndslice.topology: alongDim, byDim, map;
import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
auto x = [
[0.0, 1.0, 1.5, 2.0, 3.5, 4.25],
[2.0, 7.5, 5.0, 1.0, 1.5, 0.0]
].fuse;
auto result = [1, 4.25, 3.25, 1.5, 2.5, 2.125];
// Use byDim or alongDim with map to compute mean of row/column.
assert(x.byDim!1.map!mean.all!approxEqual(result));
assert(x.alongDim!0.map!mean.all!approxEqual(result));
// FIXME
// Without using map, computes the mean of the whole slice
// assert(x.byDim!1.mean == x.sliced.mean);
// assert(x.alongDim!0.mean == x.sliced.mean);
}
/// Can also set algorithm or output type
version(mir_test)
@safe pure nothrow
unittest
{
import mir.ndslice.slice: sliced;
import mir.ndslice.topology: repeat;
//Set sum algorithm or output type
auto a = [1, 1e100, 1, -1e100].sliced;
auto x = a * 10_000;
assert(x.mean!"kbn" == 20_000 / 4);
assert(x.mean!"kb2" == 20_000 / 4);
assert(x.mean!"precise" == 20_000 / 4);
assert(x.mean!(double, "precise") == 20_000.0 / 4);
auto y = uint.max.repeat(3);
assert(y.mean!ulong == 12884901885 / 3);
}
/++
For integral slices, pass output type as template parameter to ensure output
type is correct.
+/
version(mir_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
auto x = [0, 1, 1, 2, 4, 4,
2, 7, 5, 1, 2, 0].sliced;
auto y = x.mean;
assert(y.approxEqual(29.0 / 12, 1.0e-10));
static assert(is(typeof(y) == double));
assert(x.mean!float.approxEqual(29f / 12, 1.0e-10));
}
/++
Mean works for complex numbers and other user-defined types (provided they
can be converted to a floating point or complex type)
+/
version(mir_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
auto x = [1.0 + 2i, 2 + 3i, 3 + 4i, 4 + 5i].sliced;
assert(x.mean.approxEqual(2.5 + 3.5i));
}
/// Compute mean tensors along specified dimention of tensors
version(mir_test)
@safe pure nothrow
unittest
{
import mir.ndslice: alongDim, iota, as, map;
/++
[[0,1,2],
[3,4,5]]
+/
auto x = iota(2, 3).as!double;
assert(x.mean == (5.0 / 2.0));
auto m0 = [(0.0+3.0)/2.0, (1.0+4.0)/2.0, (2.0+5.0)/2.0];
assert(x.alongDim!0.map!mean == m0);
assert(x.alongDim!(-2).map!mean == m0);
auto m1 = [(0.0+1.0+2.0)/3.0, (3.0+4.0+5.0)/3.0];
assert(x.alongDim!1.map!mean == m1);
assert(x.alongDim!(-1).map!mean == m1);
assert(iota(2, 3, 4, 5).as!double.alongDim!0.map!mean == iota([3, 4, 5], 3 * 4 * 5 / 2));
}
/// Arbitrary mean
version(mir_test)
@safe pure nothrow @nogc
unittest
{
assert(mean(1.0, 2, 3) == 2);
assert(mean!float(1, 2, 3) == 2);
}
version(mir_test)
@safe pure nothrow
unittest
{
assert([1.0, 2, 3, 4].mean == 2.5);
}
version(mir_test)
@safe pure nothrow
unittest
{
import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice.topology: iota, alongDim, map;
auto x = iota([2, 2], 1);
auto y = x.alongDim!1.map!mean;
assert(y.all!approxEqual([1.5, 3.5]));
static assert(is(meanType!(typeof(y)) == double));
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.ndslice.slice: sliced;
static immutable x = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25,
2.0, 7.5, 5.0, 1.0, 1.5, 0.0];
assert(x.sliced.mean == 29.25 / 12);
assert(x.sliced.mean!float == 29.25 / 12);
}
///
package(mir)
template hmeanType(T)
{
import mir.math.sum: sumType;
alias U = sumType!T;
static if (__traits(compiles, {
U t = U.init + cast(U) 1; //added for when U.init = 0
auto temp = cast(U) 1 / t + cast(U) 1 / t;
})) {
alias V = typeof(cast(U) 1 / ((cast(U) 1 / U.init + cast(U) 1 / U.init) / cast(U) 2));
alias hmeanType = statType!V;
} else {
static assert(0, "hmeanType: Can't calculate hmean of elements of type " ~ U.stringof);
}
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static assert(is(hmeanType!(int[]) == double));
static assert(is(hmeanType!(double[]) == double));
static assert(is(hmeanType!(float[]) == float));
static assert(is(hmeanType!(cfloat[]) == cfloat));
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
static struct Foo {
float x;
alias x this;
}
static struct Bar {
cfloat x;
alias x this;
}
static assert(is(hmeanType!(Foo[]) == float));
static assert(is(hmeanType!(Bar[]) == cfloat));
}
/++
Computes the harmonic mean of the input.
By default, if `F` is not floating point type or complex type, then the result
will have a `double` type if `F` is implicitly convertible to a floating point
type or a type for which `isComplex!F` is true.
Params:
F = controls type of output
summation = algorithm for calculating sums (default: Summation.appropriate)
Returns:
harmonic mean of all the elements of the input, must be floating point or complex type
See_also:
$(SUBREF sum, Summation)
+/
template hmean(F, Summation summation = Summation.appropriate)
{
/++
Params:
r = range
+/
@fmamath hmeanType!F hmean(Range)(Range r)
if (isIterable!Range)
{
import mir.ndslice.topology: map;
alias G = typeof(return);
auto numerator = cast(G) 1;
static if (summation == Summation.fast && __traits(compiles, r.move.map!"numerator / a"))
{
return numerator / r.move.map!"numerator / a".mean!(G, summation);
}
else
{
MeanAccumulator!(G, ResolveSummationType!(summation, Range, G)) imean;
foreach (e; r)
imean.put(numerator / e);
return numerator / imean.mean;
}
}
/++
Params:
ar = values
+/
@fmamath hmeanType!F hmean(scope const F[] ar...)
{
alias G = typeof(return);
auto numerator = cast(G) 1;
static if (summation == Summation.fast && __traits(compiles, ar.map!"numerator / a"))
{
return numerator / ar.map!"numerator / a".mean!(G, summation);
}
else
{
MeanAccumulator!(G, ResolveSummationType!(summation, const(G)[], G)) imean;
foreach (e; ar)
imean.put(numerator / e);
return numerator / imean.mean;
}
}
}
/// ditto
template hmean(Summation summation = Summation.appropriate)
{
/++
Params:
r = range
+/
@fmamath hmeanType!Range hmean(Range)(Range r)
if (isIterable!Range)
{
alias F = typeof(return);
return .hmean!(F, summation)(r.move);
}
/++
Params:
ar = values
+/
@fmamath hmeanType!T hmean(T)(scope const T[] ar...)
{
alias F = typeof(return);
return .hmean!(F, summation)(ar);
}
}
/// ditto
template hmean(F, string summation)
{
mixin("alias hmean = .hmean!(F, Summation." ~ summation ~ ");");
}
/// ditto
template hmean(string summation)
{
mixin("alias hmean = .hmean!(Summation." ~ summation ~ ");");
}
/// Harmonic mean of vector
version(mir_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
auto x = [20.0, 100.0, 2000.0, 10.0, 5.0, 2.0].sliced;
assert(x.hmean.approxEqual(6.97269));
}
/// Harmonic mean of matrix
version(mir_test)
pure @safe
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.fuse: fuse;
auto x = [
[20.0, 100.0, 2000.0],
[10.0, 5.0, 2.0]
].fuse;
assert(x.hmean.approxEqual(6.97269));
}
/// Column harmonic mean of matrix
version(mir_test)
pure @safe
unittest
{
import mir.algorithm.iteration: all;
import mir.math.common: approxEqual;
import mir.ndslice: fuse;
import mir.ndslice.topology: alongDim, byDim, map;
auto x = [
[20.0, 100.0, 2000.0],
[ 10.0, 5.0, 2.0]
].fuse;
auto y = [13.33333, 9.52381, 3.996004];
// Use byDim or alongDim with map to compute mean of row/column.
assert(x.byDim!1.map!hmean.all!approxEqual(y));
assert(x.alongDim!0.map!hmean.all!approxEqual(y));
}
/// Can also pass arguments to hmean
version(mir_test)
pure @safe nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.topology: repeat;
import mir.ndslice.slice: sliced;
//Set sum algorithm or output type
auto x = [1, 1e-100, 1, -1e-100].sliced;
assert(x.hmean!"kb2".approxEqual(2));
assert(x.hmean!"precise".approxEqual(2));
assert(x.hmean!(double, "precise").approxEqual(2));
//Provide the summation type
assert(float.max.repeat(3).hmean!double.approxEqual(float.max));
}
/++
For integral slices, pass output type as template parameter to ensure output
type is correct.
+/
version(mir_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
auto x = [20, 100, 2000, 10, 5, 2].sliced;
auto y = x.hmean;
assert(y.approxEqual(6.97269));
static assert(is(typeof(y) == double));
assert(x.hmean!float.approxEqual(6.97269));
}
/++
hmean works for complex numbers and other user-defined types (provided they
can be converted to a floating point or complex type)
+/
version(mir_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
auto x = [1.0 + 2i, 2 + 3i, 3 + 4i, 4 + 5i].sliced;
assert(x.hmean.approxEqual(1.97110904 + 3.14849332i));
}
/// Arbitrary harmonic mean
version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
auto x = hmean(20.0, 100, 2000, 10, 5, 2);
assert(x.approxEqual(6.97269));
auto y = hmean!float(20, 100, 2000, 10, 5, 2);
assert(y.approxEqual(6.97269));
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
static immutable x = [20.0, 100.0, 2000.0, 10.0, 5.0, 2.0];
assert(x.sliced.hmean.approxEqual(6.97269));
assert(x.sliced.hmean!float.approxEqual(6.97269));
}
private
F nthroot(F)(in F x, in size_t n)
if (isFloatingPoint!F)
{
import mir.math.common: sqrt, pow;
if (n > 2) {
return pow(x, cast(F) 1 / cast(F) n);
} else if (n == 2) {
return sqrt(x);
} else if (n == 1) {
return x;
} else {
return cast(F) 1;
}
}
version(mir_test)
@safe pure nothrow @nogc
unittest
{
import mir.math.common: approxEqual;
assert(nthroot(9.0, 0).approxEqual(1));
assert(nthroot(9.0, 1).approxEqual(9));
assert(nthroot(9.0, 2).approxEqual(3));
assert(nthroot(9.5, 2).approxEqual(3.08220700));
assert(nthroot(9.0, 3).approxEqual(2.08008382));
}
/++
Output range for gmean.
+/
struct GMeanAccumulator(T)
if (isMutable!T && isFloatingPoint!T)
{
import mir.math.numeric: ProdAccumulator;
///
size_t count;
///
ProdAccumulator!T prodAccumulator;
///
F gmean(F = T)() @property
if (isFloatingPoint!F)
{
import mir.math.common: exp2;
return nthroot(cast(F) prodAccumulator.mantissa, count) * exp2(cast(F) prodAccumulator.exp / count);
}
///
void put(Range)(Range r)
if (isIterable!Range)
{
static if (hasShape!Range)
{
count += r.elementCount;
prodAccumulator.put(r);
}
else
{
foreach(x; r)
{
count++;
prodAccumulator.put(x);
}
}
}
///
void put()(T x)
{
count++;
prodAccumulator.put(x);
}
}
///
version(mir_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
GMeanAccumulator!double x;
x.put([1.0, 2, 3, 4].sliced);
assert(x.gmean.approxEqual(2.21336384));
x.put(5);
assert(x.gmean.approxEqual(2.60517108));
}
version(mir_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;