XS/aggregate.xs

filter: Statementstmt 91.6% 55 / 60 Branchbran 79.4% 27 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.2% 82 / 94 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« const-c.inc ↑ Summary histogram_agg.c »
1 MODULE = Math::SimpleHisto::XS PACKAGE = Math::SimpleHisto::XS
2  
3  
4 double
5 integral(self, from, to, type = 0)
6 simple_histo_1d* self
7 double from
8 double to
9 int type
10 PREINIT:
11 double* data;
12 unsigned int i, n;
13 double binsize;
14 bool invert = 0;
15 CODE:
16 /* TODO nonconstant bins */
17 38 ▶ if (from > to) {
Branch: 1 branch
BranchTrueFalseTotal
if (from > to) { 1 37 38
18 binsize = from; /* abuse as temp var */
19 from = to;
20 to = binsize;
21 invert = 1;
22 }
23  
24 38 data = self->data;
25 38 binsize = self->binsize;
26  
27 /* FIXME handle both to/from being off limits on the same side*/
28 38 ▶ if (to >= self->max)
Branch: 1 branch
BranchTrueFalseTotal
if (to >= self->max) 15 23 38
29 to = self->max;
30 38 ▶ if (from < self->min)
Branch: 1 branch
BranchTrueFalseTotal
if (from < self->min) 3 35 38
31 from = self->min;
32  
33 /*for (i = 1; i < self->nbins; ++i)
34 printf("%u: %f ", i, data[i]);
35 printf("\n");
36 */
37  
38 38 ▶ switch(type) {
Branch: 1 branch
BranchTrueFalseTotal
switch(type) { 38 0 38
39 38 case INTEGRAL_CONSTANT:
40 38 ▶ if (self->bins == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->bins == NULL) { 18 20 38
41 /* first (fractional) bin */
42 18 from = (from - self->min) / binsize;
43 18 i = (int)from;
44 18 from -= (double)i;
45  
46 /* last (fractional) bin */
47 18 to = (to - self->min) / binsize;
48 18 n = (int)to;
49 18 to -= (double)n;
50 18 ▶ if (i == n) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == n) { 3 15 18
51 3 RETVAL = (to-from) * data[i];
52 }
53 else {
54 15 RETVAL = data[i] * (1.-from)
55 15 + data[n] * to;
56 15 ++i;
57 394 ▶ for (; i < n; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < n; ++i) 379 15 394
58 379 RETVAL += data[i];
59 }
60 }
61 else { /* variable bin size */
62 /* TODO optimize */
63 double* bins = self->bins;
64 20 unsigned int nbins = self->nbins;
65  
66 20 i = find_bin_nonconstant(from, nbins, bins);
67 20 binsize = (bins[i+1]-bins[i]);
68 20 RETVAL = (bins[i+1]-from)/binsize * data[i]; /* distance from 'from' to upper boundary of bin times data in bin */
69  
70 20 n = find_bin_nonconstant(to, nbins, bins);
71 20 ▶ if (i == n) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == n) { 4 16 20
72 4 RETVAL -= (bins[i+1]-to)/binsize * data[i];
73 }
74 else {
75 ++i;
76 42 ▶ for (; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < n; ++i) { 26 16 42
77 26 RETVAL += data[i];
78 }
79 16 binsize = bins[n+1]-bins[n];
80 16 RETVAL += data[n] * (to-bins[n])/binsize;
81 }
82 }
83 break;
84 0 default:
85 0 croak("Invalid integration type");
86 };
87 38 ▶ if (invert)
Branch: 1 branch
BranchTrueFalseTotal
if (invert) 1 37 38
88 1 RETVAL *= -1.;
89 OUTPUT: RETVAL
90  
91  
92 double
93 mean(self)
94 simple_histo_1d* self
95 CODE:
96 6 RETVAL = histo_mean(aTHX_ self);
97 OUTPUT: RETVAL
98  
99 double
100 standard_deviation(self, ...)
101 simple_histo_1d* self
102 CODE:
103 4 ▶ if (items > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) 0 4 4
104 0 RETVAL = histo_standard_deviation_with_mean(aTHX_ self, SvNV(ST(1)));
105 else
106 4 RETVAL = histo_standard_deviation(aTHX_ self);
107 OUTPUT: RETVAL
108  
109 double
110 median(self)
111 simple_histo_1d* self
112 PREINIT:
113 CODE:
114 7 RETVAL = histo_median(aTHX_ self);
115 OUTPUT: RETVAL
116  
117  
118 double
119 median_absolute_deviation(self, ...)
120 simple_histo_1d* self
121 PREINIT:
122 double median;
123 double *x, *data;
124 unsigned int i, n;
125 simple_histo_1d* madhist;
126 CODE:
127
128 6 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 2 4 6
129 2 median = SvNV(ST(1));
130 4 ▶ else if (items == 1)
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 1) 4 0 4
131 4 median = histo_median(aTHX_ self);
132  
133 /* FIXME think hard about the optimal nbins here. Also wrt. variable bin size */
134 6 madhist = histo_alloc_new_fixed_bins(aTHX_ self->nbins, 0., self->width);
135
136 6 n = self->nbins;
137 6 data = self->data;
138 6 Newx(x, n, double);
139 6 ▶ if (self->bins == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->bins == 0) { 6 0 6
140 6 double min = self->min;
141 6 double binsize = self->binsize;
142 600006 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 600000 6 600006
143 /* abs(median-bin_center) */
144 600000 x[i] = fabs( median - (min + binsize * (i + 0.5)) );
145 }
146 }
147 else { /* variable bin size */
148 double* bins = self->bins;
149 0 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 0 0 0
150 0 x[i] = fabs( median - 0.5*(bins[i]+bins[i+1]) );
151 }
152 }
153 6 histo_fill(madhist, n, x, data);
154 6 Safefree(x);
155  
156 6 RETVAL = histo_median(aTHX_ madhist);
157 6 ▶ HS_DEALLOCATE(madhist);
Branch: 1 branch
BranchTrueFalseTotal
HS_DEALLOCATE(madhist); 0 6 6
158 OUTPUT: RETVAL
159  
160  
« const-c.inc ↑ Summary histogram_agg.c »