File Coverage

Bin1D.xs
Criterion Covered Total %
statement 490 8086 6.0
branch 421 14242 2.9
condition n/a
subroutine n/a
pod n/a
total 911 22328 4.0


line stmt bran cond sub pod time code
1              
2             #line 453 "lib/PDL/PP.pm"
3             /*
4             * THIS FILE WAS GENERATED BY PDL::PP from lib/CXC/PDL/Bin1D.pd! Do not modify!
5             */
6              
7             #define PDL_FREE_CODE(trans, destroy, comp_free_code, ntpriv_free_code) \
8             if (destroy) { \
9             comp_free_code \
10             } \
11             if ((trans)->dims_redone) { \
12             ntpriv_free_code \
13             }
14              
15             #include "EXTERN.h"
16             #include "perl.h"
17             #include "XSUB.h"
18             #include "pdl.h"
19             #include "pdlcore.h"
20             #define PDL CXC_PDL_Bin1D
21             extern Core* PDL; /* Structure hold core C functions */
22             #line 23 "Bin1D.xs"
23             #include
24             #include
25             #define BIN_RC_OK 1
26             #define BIN_RC_GEWMAX 2
27             #define BIN_RC_GENMAX 4
28             #define BIN_RC_FOLDED 8
29             #define BIN_RC_GTMINSN 16
30             #define BIN_ARG_HAVE_ERROR 1
31             #define BIN_ARG_HAVE_WEIGHT 2
32             #define BIN_ARG_SET_BAD 4
33             #define BIN_ARG_FOLD 8
34             #define BIN_ARG_HAVE_WIDTH 16
35             #define BIN_ARG_ERROR_SDEV 32
36             #define BIN_ARG_ERROR_POISSON 64
37             #define BIN_ARG_ERROR_RSS 128
38             #define BIN_ARG_HAVE_SIGNAL 256
39             #define BIN_ARG_SAVE_OOB_START_END 512
40             #define BIN_ARG_SAVE_OOB_START_NBINS 1024
41             #define BIN_ARG_SHIFT_IMIN 1536
42             #define BIN_ARG_SAVE_OOB_END 2048
43             #define BIN_ARG_SAVE_OOB_NBINS 4096
44             #define BIN_ARG_SAVE_OOB 7680
45             #define BIN_ARG_WANT_EXTREMA 8192
46             #define BIN_ARG_WANT_SUM_WEIGHT 16384
47             #define BIN_ARG_WANT_SUM_WEIGHT2 32768
48              
49             #line 1846 "lib/PDL/PP.pm"
50             typedef struct pdl_params_bin_adaptive_snr {
51             #line 52 "Bin1D.xs"
52             unsigned long optflags;
53             double min_snr;
54             PDL_Indx min_nelem;
55             PDL_Indx max_nelem;
56             double min_width;
57             double max_width;
58             } pdl_params_bin_adaptive_snr;
59              
60              
61             #line 1857 "lib/PDL/PP.pm"
62             pdl_error pdl_bin_adaptive_snr_redodims(pdl_trans *__privtrans) {
63             pdl_error PDL_err = {0, NULL, 0};
64             #line 65 "Bin1D.xs"
65 35           pdl_params_bin_adaptive_snr *__params = __privtrans->params; (void)__params;
66             #ifndef PDL_DECLARE_PARAMS_bin_adaptive_snr_0
67             #define PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_TYPE_OP,PDL_PPSYM_OP,PDL_TYPE_PARAM_index,PDL_PPSYM_PARAM_index,PDL_TYPE_PARAM_nbins,PDL_PPSYM_PARAM_nbins,PDL_TYPE_PARAM_nelem,PDL_PPSYM_PARAM_nelem,PDL_TYPE_PARAM_b_signal,PDL_PPSYM_PARAM_b_signal,PDL_TYPE_PARAM_b_error,PDL_PPSYM_PARAM_b_error,PDL_TYPE_PARAM_b_mean,PDL_PPSYM_PARAM_b_mean,PDL_TYPE_PARAM_b_snr,PDL_PPSYM_PARAM_b_snr,PDL_TYPE_PARAM_b_width,PDL_PPSYM_PARAM_b_width,PDL_TYPE_PARAM_ifirst,PDL_PPSYM_PARAM_ifirst,PDL_TYPE_PARAM_ilast,PDL_PPSYM_PARAM_ilast,PDL_TYPE_PARAM_rc,PDL_PPSYM_PARAM_rc,PDL_TYPE_PARAM_b_error2,PDL_PPSYM_PARAM_b_error2,PDL_TYPE_PARAM_b_signal2,PDL_PPSYM_PARAM_b_signal2,PDL_TYPE_PARAM_b_m2,PDL_PPSYM_PARAM_b_m2,PDL_TYPE_PARAM_b_weight,PDL_PPSYM_PARAM_b_weight,PDL_TYPE_PARAM_b_weight_sig,PDL_PPSYM_PARAM_b_weight_sig,PDL_TYPE_PARAM_b_weight_sig2,PDL_PPSYM_PARAM_b_weight_sig2) \
68             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, signal, (__privtrans->pdls[0]), 0, PDL_PPSYM_OP) \
69             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, error, (__privtrans->pdls[1]), 0, PDL_PPSYM_OP) \
70             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, width, (__privtrans->pdls[2]), 0, PDL_PPSYM_OP) \
71             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_index, index, (__privtrans->pdls[3]), 0, PDL_PPSYM_PARAM_index) \
72             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_nbins, nbins, (__privtrans->pdls[4]), 0, PDL_PPSYM_PARAM_nbins) \
73             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_nelem, nelem, (__privtrans->pdls[5]), 0, PDL_PPSYM_PARAM_nelem) \
74             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_signal, b_signal, (__privtrans->pdls[6]), 0, PDL_PPSYM_PARAM_b_signal) \
75             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_error, b_error, (__privtrans->pdls[7]), 0, PDL_PPSYM_PARAM_b_error) \
76             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_mean, b_mean, (__privtrans->pdls[8]), 0, PDL_PPSYM_PARAM_b_mean) \
77             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_snr, b_snr, (__privtrans->pdls[9]), 0, PDL_PPSYM_PARAM_b_snr) \
78             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_width, b_width, (__privtrans->pdls[10]), 0, PDL_PPSYM_PARAM_b_width) \
79             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_ifirst, ifirst, (__privtrans->pdls[11]), 0, PDL_PPSYM_PARAM_ifirst) \
80             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_ilast, ilast, (__privtrans->pdls[12]), 0, PDL_PPSYM_PARAM_ilast) \
81             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_rc, rc, (__privtrans->pdls[13]), 0, PDL_PPSYM_PARAM_rc) \
82             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_error2, b_error2, (__privtrans->pdls[14]), 0, PDL_PPSYM_PARAM_b_error2) \
83             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_signal2, b_signal2, (__privtrans->pdls[15]), 0, PDL_PPSYM_PARAM_b_signal2) \
84             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_m2, b_m2, (__privtrans->pdls[16]), 0, PDL_PPSYM_PARAM_b_m2) \
85             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight, b_weight, (__privtrans->pdls[17]), 0, PDL_PPSYM_PARAM_b_weight) \
86             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight_sig, b_weight_sig, (__privtrans->pdls[18]), 0, PDL_PPSYM_PARAM_b_weight_sig) \
87             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight_sig2, b_weight_sig2, (__privtrans->pdls[19]), 0, PDL_PPSYM_PARAM_b_weight_sig2)
88             #endif
89             #define PDL_IF_BAD(t,f) f
90 35           switch (__privtrans->__datatype) { /* Start generic switch */
91 0           case PDL_SB: {
92 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_SByte,A,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
93 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
94 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
95 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
96 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
97 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
98 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
99 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
100             }
101 0           } break;
102 0           case PDL_B: {
103 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_Byte,B,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
104 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
105 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
106 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
107 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
108 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
109 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
110 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
111             }
112 0           } break;
113 0           case PDL_S: {
114 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_Short,S,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
115 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
116 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
117 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
118 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
119 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
120 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
121 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
122             }
123 0           } break;
124 0           case PDL_US: {
125 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_Ushort,U,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
126 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
127 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
128 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
129 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
130 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
131 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
132 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
133             }
134 0           } break;
135 0           case PDL_L: {
136 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_Long,L,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
137 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
138 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
139 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
140 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
141 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
142 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
143 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
144             }
145 0           } break;
146 0           case PDL_UL: {
147 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_ULong,K,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
148 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
149 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
150 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
151 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
152 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
153 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
154 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
155             }
156 0           } break;
157 0           case PDL_IND: {
158 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
159 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
160 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
161 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
162 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
163 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
164 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
165 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
166             }
167 0           } break;
168 0           case PDL_ULL: {
169 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_ULongLong,P,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
170 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
171 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
172 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
173 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
174 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
175 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
176 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
177             }
178 0           } break;
179 0           case PDL_LL: {
180 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_LongLong,Q,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
181 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
182 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
183 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
184 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
185 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
186 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
187 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
188             }
189 0           } break;
190 0           case PDL_F: {
191 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_Float,F,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
192 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
193 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
194 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
195 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
196 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
197 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
198 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
199             }
200 0           } break;
201 35           case PDL_D: {
202 35 50         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
203 35           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
204 35 100         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
205 35 100         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
206 35 100         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
207 70           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
208 35 100         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
209 35 100         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
210             }
211 35           } break;
212 0           case PDL_LD: {
213 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_0(PDL_LDouble,E,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
214 0           { PDL_Indx n = __privtrans->pdls[0]->dims[0];
215 0 0         __privtrans->ind_sizes[1] = __params->optflags & BIN_ARG_ERROR_RSS ? n : 0 ;
216 0 0         __privtrans->ind_sizes[4] = __params->optflags & BIN_ARG_HAVE_WIDTH ? n : 0 ;
217 0 0         __privtrans->ind_sizes[2] = __params->optflags & BIN_ARG_ERROR_SDEV ? n : 0 ;
218 0           __privtrans->ind_sizes[5] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
219 0 0         == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
220 0 0         __privtrans->ind_sizes[3] = __params->optflags & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
221             }
222 0           } break;
223 0           default: return PDL->make_error(PDL_EUSERERROR, "PP INTERNAL ERROR in bin_adaptive_snr: unhandled datatype(%d), only handles (ABSULKNPQFDE)! PLEASE MAKE A BUG REPORT\n", __privtrans->__datatype);
224             }
225             #undef PDL_IF_BAD
226              
227 35 50         PDL_RETERROR(PDL_err, PDL->redodims_default(__privtrans));
228 35           return PDL_err;
229             }
230              
231              
232             #line 1857 "lib/PDL/PP.pm"
233             pdl_error pdl_bin_adaptive_snr_readdata(pdl_trans *__privtrans) {
234             pdl_error PDL_err = {0, NULL, 0};
235             #line 236 "Bin1D.xs"
236 35           pdl_params_bin_adaptive_snr *__params = __privtrans->params; (void)__params;
237 35           register PDL_Indx __n_size = __privtrans->ind_sizes[0];
238 35 50         if (!__privtrans->broadcast.incs) return PDL->make_error(PDL_EUSERERROR, "Error in bin_adaptive_snr:" "broadcast.incs NULL");
239             /* broadcastloop declarations */
240             int __brcloopval;
241             register PDL_Indx __tind0,__tind1; /* counters along dim */
242 35           register PDL_Indx __tnpdls = __privtrans->broadcast.npdls;
243             /* dims here are how many steps along those dims */
244 35           register PDL_Indx __tinc0_signal = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,0,0);
245 35           register PDL_Indx __tinc0_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,1,0);
246 35           register PDL_Indx __tinc0_width = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,2,0);
247 35           register PDL_Indx __tinc0_index = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,3,0);
248 35           register PDL_Indx __tinc0_nbins = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,4,0);
249 35           register PDL_Indx __tinc0_nelem = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,5,0);
250 35           register PDL_Indx __tinc0_b_signal = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,6,0);
251 35           register PDL_Indx __tinc0_b_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,7,0);
252 35           register PDL_Indx __tinc0_b_mean = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,8,0);
253 35           register PDL_Indx __tinc0_b_snr = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,9,0);
254 35           register PDL_Indx __tinc0_b_width = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,10,0);
255 35           register PDL_Indx __tinc0_ifirst = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,11,0);
256 35           register PDL_Indx __tinc0_ilast = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,12,0);
257 35           register PDL_Indx __tinc0_rc = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,13,0);
258 35           register PDL_Indx __tinc0_b_error2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,14,0);
259 35           register PDL_Indx __tinc0_b_signal2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,15,0);
260 35           register PDL_Indx __tinc0_b_m2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,16,0);
261 35           register PDL_Indx __tinc0_b_weight = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,17,0);
262 35           register PDL_Indx __tinc0_b_weight_sig = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,18,0);
263 35           register PDL_Indx __tinc0_b_weight_sig2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,19,0);
264 35           register PDL_Indx __tinc1_signal = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,0,1);
265 35           register PDL_Indx __tinc1_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,1,1);
266 35           register PDL_Indx __tinc1_width = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,2,1);
267 35           register PDL_Indx __tinc1_index = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,3,1);
268 35           register PDL_Indx __tinc1_nbins = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,4,1);
269 35           register PDL_Indx __tinc1_nelem = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,5,1);
270 35           register PDL_Indx __tinc1_b_signal = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,6,1);
271 35           register PDL_Indx __tinc1_b_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,7,1);
272 35           register PDL_Indx __tinc1_b_mean = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,8,1);
273 35           register PDL_Indx __tinc1_b_snr = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,9,1);
274 35           register PDL_Indx __tinc1_b_width = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,10,1);
275 35           register PDL_Indx __tinc1_ifirst = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,11,1);
276 35           register PDL_Indx __tinc1_ilast = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,12,1);
277 35           register PDL_Indx __tinc1_rc = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,13,1);
278 35           register PDL_Indx __tinc1_b_error2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,14,1);
279 35           register PDL_Indx __tinc1_b_signal2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,15,1);
280 35           register PDL_Indx __tinc1_b_m2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,16,1);
281 35           register PDL_Indx __tinc1_b_weight = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,17,1);
282 35           register PDL_Indx __tinc1_b_weight_sig = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,18,1);
283 35           register PDL_Indx __tinc1_b_weight_sig2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,19,1);
284             #define PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata PDL_BROADCASTLOOP_START( \
285             readdata, \
286             __privtrans->broadcast, \
287             __privtrans->vtable, \
288             signal_datap += __offsp[0]; \
289             error_datap += __offsp[1]; \
290             width_datap += __offsp[2]; \
291             index_datap += __offsp[3]; \
292             nbins_datap += __offsp[4]; \
293             nelem_datap += __offsp[5]; \
294             b_signal_datap += __offsp[6]; \
295             b_error_datap += __offsp[7]; \
296             b_mean_datap += __offsp[8]; \
297             b_snr_datap += __offsp[9]; \
298             b_width_datap += __offsp[10]; \
299             ifirst_datap += __offsp[11]; \
300             ilast_datap += __offsp[12]; \
301             rc_datap += __offsp[13]; \
302             b_error2_datap += __offsp[14]; \
303             b_signal2_datap += __offsp[15]; \
304             b_m2_datap += __offsp[16]; \
305             b_weight_datap += __offsp[17]; \
306             b_weight_sig_datap += __offsp[18]; \
307             b_weight_sig2_datap += __offsp[19]; \
308             , \
309             ( ,signal_datap += __tinc1_signal - __tinc0_signal * __tdims0 \
310             ,error_datap += __tinc1_error - __tinc0_error * __tdims0 \
311             ,width_datap += __tinc1_width - __tinc0_width * __tdims0 \
312             ,index_datap += __tinc1_index - __tinc0_index * __tdims0 \
313             ,nbins_datap += __tinc1_nbins - __tinc0_nbins * __tdims0 \
314             ,nelem_datap += __tinc1_nelem - __tinc0_nelem * __tdims0 \
315             ,b_signal_datap += __tinc1_b_signal - __tinc0_b_signal * __tdims0 \
316             ,b_error_datap += __tinc1_b_error - __tinc0_b_error * __tdims0 \
317             ,b_mean_datap += __tinc1_b_mean - __tinc0_b_mean * __tdims0 \
318             ,b_snr_datap += __tinc1_b_snr - __tinc0_b_snr * __tdims0 \
319             ,b_width_datap += __tinc1_b_width - __tinc0_b_width * __tdims0 \
320             ,ifirst_datap += __tinc1_ifirst - __tinc0_ifirst * __tdims0 \
321             ,ilast_datap += __tinc1_ilast - __tinc0_ilast * __tdims0 \
322             ,rc_datap += __tinc1_rc - __tinc0_rc * __tdims0 \
323             ,b_error2_datap += __tinc1_b_error2 - __tinc0_b_error2 * __tdims0 \
324             ,b_signal2_datap += __tinc1_b_signal2 - __tinc0_b_signal2 * __tdims0 \
325             ,b_m2_datap += __tinc1_b_m2 - __tinc0_b_m2 * __tdims0 \
326             ,b_weight_datap += __tinc1_b_weight - __tinc0_b_weight * __tdims0 \
327             ,b_weight_sig_datap += __tinc1_b_weight_sig - __tinc0_b_weight_sig * __tdims0 \
328             ,b_weight_sig2_datap += __tinc1_b_weight_sig2 - __tinc0_b_weight_sig2 * __tdims0 \
329             ), \
330             ( ,signal_datap += __tinc0_signal \
331             ,error_datap += __tinc0_error \
332             ,width_datap += __tinc0_width \
333             ,index_datap += __tinc0_index \
334             ,nbins_datap += __tinc0_nbins \
335             ,nelem_datap += __tinc0_nelem \
336             ,b_signal_datap += __tinc0_b_signal \
337             ,b_error_datap += __tinc0_b_error \
338             ,b_mean_datap += __tinc0_b_mean \
339             ,b_snr_datap += __tinc0_b_snr \
340             ,b_width_datap += __tinc0_b_width \
341             ,ifirst_datap += __tinc0_ifirst \
342             ,ilast_datap += __tinc0_ilast \
343             ,rc_datap += __tinc0_rc \
344             ,b_error2_datap += __tinc0_b_error2 \
345             ,b_signal2_datap += __tinc0_b_signal2 \
346             ,b_m2_datap += __tinc0_b_m2 \
347             ,b_weight_datap += __tinc0_b_weight \
348             ,b_weight_sig_datap += __tinc0_b_weight_sig \
349             ,b_weight_sig2_datap += __tinc0_b_weight_sig2 \
350             ) \
351             )
352             #define PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata PDL_BROADCASTLOOP_END( \
353             __privtrans->broadcast, \
354             signal_datap -= __tinc1_signal * __tdims1 + __offsp[0]; \
355             error_datap -= __tinc1_error * __tdims1 + __offsp[1]; \
356             width_datap -= __tinc1_width * __tdims1 + __offsp[2]; \
357             index_datap -= __tinc1_index * __tdims1 + __offsp[3]; \
358             nbins_datap -= __tinc1_nbins * __tdims1 + __offsp[4]; \
359             nelem_datap -= __tinc1_nelem * __tdims1 + __offsp[5]; \
360             b_signal_datap -= __tinc1_b_signal * __tdims1 + __offsp[6]; \
361             b_error_datap -= __tinc1_b_error * __tdims1 + __offsp[7]; \
362             b_mean_datap -= __tinc1_b_mean * __tdims1 + __offsp[8]; \
363             b_snr_datap -= __tinc1_b_snr * __tdims1 + __offsp[9]; \
364             b_width_datap -= __tinc1_b_width * __tdims1 + __offsp[10]; \
365             ifirst_datap -= __tinc1_ifirst * __tdims1 + __offsp[11]; \
366             ilast_datap -= __tinc1_ilast * __tdims1 + __offsp[12]; \
367             rc_datap -= __tinc1_rc * __tdims1 + __offsp[13]; \
368             b_error2_datap -= __tinc1_b_error2 * __tdims1 + __offsp[14]; \
369             b_signal2_datap -= __tinc1_b_signal2 * __tdims1 + __offsp[15]; \
370             b_m2_datap -= __tinc1_b_m2 * __tdims1 + __offsp[16]; \
371             b_weight_datap -= __tinc1_b_weight * __tdims1 + __offsp[17]; \
372             b_weight_sig_datap -= __tinc1_b_weight_sig * __tdims1 + __offsp[18]; \
373             b_weight_sig2_datap -= __tinc1_b_weight_sig2 * __tdims1 + __offsp[19]; \
374             )
375 35           register PDL_Indx __inc_b_error_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,7,0)]; (void)__inc_b_error_n;
376 35           register PDL_Indx __inc_b_error2_nrss = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,14,0)]; (void)__inc_b_error2_nrss;
377 35           register PDL_Indx __inc_b_m2_nsdev = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,16,0)]; (void)__inc_b_m2_nsdev;
378 35           register PDL_Indx __inc_b_mean_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,8,0)]; (void)__inc_b_mean_n;
379 35           register PDL_Indx __inc_b_signal_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,6,0)]; (void)__inc_b_signal_n;
380 35           register PDL_Indx __inc_b_signal2_nsdev = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,15,0)]; (void)__inc_b_signal2_nsdev;
381 35           register PDL_Indx __inc_b_snr_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,9,0)]; (void)__inc_b_snr_n;
382 35           register PDL_Indx __inc_b_weight_nweight = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,17,0)]; (void)__inc_b_weight_nweight;
383 35           register PDL_Indx __inc_b_weight_sig_nwsdev = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,18,0)]; (void)__inc_b_weight_sig_nwsdev;
384 35           register PDL_Indx __inc_b_weight_sig2_nwsdev = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,19,0)]; (void)__inc_b_weight_sig2_nwsdev;
385 35           register PDL_Indx __inc_b_width_nwidth = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,10,0)]; (void)__inc_b_width_nwidth;
386 35           register PDL_Indx __inc_error_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,1,0)]; (void)__inc_error_n;
387 35           register PDL_Indx __inc_ifirst_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,11,0)]; (void)__inc_ifirst_n;
388 35           register PDL_Indx __inc_ilast_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,12,0)]; (void)__inc_ilast_n;
389 35           register PDL_Indx __inc_index_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,3,0)]; (void)__inc_index_n;
390 35           register PDL_Indx __inc_nelem_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,5,0)]; (void)__inc_nelem_n;
391 35           register PDL_Indx __inc_rc_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,13,0)]; (void)__inc_rc_n;
392 35           register PDL_Indx __inc_signal_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,0,0)]; (void)__inc_signal_n;
393 35           register PDL_Indx __inc_width_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,2,0)]; (void)__inc_width_n;
394             #ifndef PDL_DECLARE_PARAMS_bin_adaptive_snr_1
395             #define PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_TYPE_OP,PDL_PPSYM_OP,PDL_TYPE_PARAM_index,PDL_PPSYM_PARAM_index,PDL_TYPE_PARAM_nbins,PDL_PPSYM_PARAM_nbins,PDL_TYPE_PARAM_nelem,PDL_PPSYM_PARAM_nelem,PDL_TYPE_PARAM_b_signal,PDL_PPSYM_PARAM_b_signal,PDL_TYPE_PARAM_b_error,PDL_PPSYM_PARAM_b_error,PDL_TYPE_PARAM_b_mean,PDL_PPSYM_PARAM_b_mean,PDL_TYPE_PARAM_b_snr,PDL_PPSYM_PARAM_b_snr,PDL_TYPE_PARAM_b_width,PDL_PPSYM_PARAM_b_width,PDL_TYPE_PARAM_ifirst,PDL_PPSYM_PARAM_ifirst,PDL_TYPE_PARAM_ilast,PDL_PPSYM_PARAM_ilast,PDL_TYPE_PARAM_rc,PDL_PPSYM_PARAM_rc,PDL_TYPE_PARAM_b_error2,PDL_PPSYM_PARAM_b_error2,PDL_TYPE_PARAM_b_signal2,PDL_PPSYM_PARAM_b_signal2,PDL_TYPE_PARAM_b_m2,PDL_PPSYM_PARAM_b_m2,PDL_TYPE_PARAM_b_weight,PDL_PPSYM_PARAM_b_weight,PDL_TYPE_PARAM_b_weight_sig,PDL_PPSYM_PARAM_b_weight_sig,PDL_TYPE_PARAM_b_weight_sig2,PDL_PPSYM_PARAM_b_weight_sig2) \
396             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, signal, (__privtrans->pdls[0]), 1, PDL_PPSYM_OP) \
397             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, error, (__privtrans->pdls[1]), 1, PDL_PPSYM_OP) \
398             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, width, (__privtrans->pdls[2]), 1, PDL_PPSYM_OP) \
399             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_index, index, (__privtrans->pdls[3]), 1, PDL_PPSYM_PARAM_index) \
400             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_nbins, nbins, (__privtrans->pdls[4]), 1, PDL_PPSYM_PARAM_nbins) \
401             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_nelem, nelem, (__privtrans->pdls[5]), 1, PDL_PPSYM_PARAM_nelem) \
402             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_signal, b_signal, (__privtrans->pdls[6]), 1, PDL_PPSYM_PARAM_b_signal) \
403             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_error, b_error, (__privtrans->pdls[7]), 1, PDL_PPSYM_PARAM_b_error) \
404             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_mean, b_mean, (__privtrans->pdls[8]), 1, PDL_PPSYM_PARAM_b_mean) \
405             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_snr, b_snr, (__privtrans->pdls[9]), 1, PDL_PPSYM_PARAM_b_snr) \
406             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_width, b_width, (__privtrans->pdls[10]), 1, PDL_PPSYM_PARAM_b_width) \
407             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_ifirst, ifirst, (__privtrans->pdls[11]), 1, PDL_PPSYM_PARAM_ifirst) \
408             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_ilast, ilast, (__privtrans->pdls[12]), 1, PDL_PPSYM_PARAM_ilast) \
409             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_rc, rc, (__privtrans->pdls[13]), 1, PDL_PPSYM_PARAM_rc) \
410             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_error2, b_error2, (__privtrans->pdls[14]), 1, PDL_PPSYM_PARAM_b_error2) \
411             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_signal2, b_signal2, (__privtrans->pdls[15]), 1, PDL_PPSYM_PARAM_b_signal2) \
412             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_m2, b_m2, (__privtrans->pdls[16]), 1, PDL_PPSYM_PARAM_b_m2) \
413             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight, b_weight, (__privtrans->pdls[17]), 1, PDL_PPSYM_PARAM_b_weight) \
414             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight_sig, b_weight_sig, (__privtrans->pdls[18]), 1, PDL_PPSYM_PARAM_b_weight_sig) \
415             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight_sig2, b_weight_sig2, (__privtrans->pdls[19]), 1, PDL_PPSYM_PARAM_b_weight_sig2)
416             #endif
417 35 50         if ( __privtrans->bvalflag ) { /* ** do 'bad' Code ** */
418             #define PDL_BAD_CODE
419             #define PDL_IF_BAD(t,f) t
420 0           switch (__privtrans->__datatype) { /* Start generic switch */
421 0           case PDL_SB: {
422 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_SByte,A,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
423             {
424              
425              
426              
427              
428 0           int flags = __params->optflags;
429              
430 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
431 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
432 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
433 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
434 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
435 0           int fold_last_bin = flags & BIN_ARG_FOLD;
436 0           int set_bad = flags & BIN_ARG_SET_BAD;
437              
438 0           PDL_Indx min_nelem = __params->min_nelem;
439 0           PDL_Indx max_nelem = __params->max_nelem;
440 0           double max_width = __params->max_width;
441 0           double min_width = __params->min_width;
442 0           double min_snr = __params->min_snr;
443              
444             /* simplify the logic below by setting bounds values to their most
445             permissive extremes if they aren't needed. */
446              
447 0 0         if ( max_width == 0 )
448 0           max_width = DBL_MAX;
449              
450 0 0         if ( max_nelem == 0 )
451 0           max_nelem = LONG_MAX;
452              
453 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
454              
455 0           PDL_Indx curind = 0; /* index of current bin */
456              
457             /* Declare bin counters */
458 0           int rc = 0; /* status */
459 0           double bsignal = 0; /* sum of signal */
460 0           double bmean = 0; /* mean of (weighted) signal */
461 0           double bwidth = 0; /* width (if applicable) */
462 0           double bsnr = 0; /* SNR */
463 0           double berror2 = 0; /* sum of error^2 */
464 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
465 0           double bm2 = 0; /* unnormalized variance */
466 0           double bsignal2 = 0; /* sum of signal^2 */
467 0           double bweight = 0; /* sum of 1/error*2 */
468 0           double bweight_sig = 0; /* sum of weight * signal */
469 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
470 0           int bad_error = 0; /* if error2 is not good */
471 0           int lastrc = 0; /* carryover status from previous loop */
472 0           PDL_Indx nin = 0; /* number of elements in the current bin */
473              
474 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
475              
476 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
477             double error;
478             double error2;
479             double width;
480              
481 0           int snr_ok = 0;
482              
483 0 0         if ( have_error ) {
484 0           error = (error_datap)[0+(__inc_error_n*(n))];
485 0           error2 = error * error;
486             }
487              
488 0 0         if ( have_width )
489 0           width = (width_datap)[0+(__inc_width_n*(n))];
490              
491             #ifdef PDL_BAD_CODE
492 0 0         if ( PDL_ISBAD2(signal,signal_badval,A,signal_badval_isnan)
493 0 0         ||
    0          
494 0 0         have_error && PDL_ISBAD2(error,error_badval,A,error_badval_isnan) ) {
    0          
495 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
496 0           continue;
497             }
498             #endif /* PDL_BAD_CODE */
499 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
500              
501            
502 0           bsignal += signal;
503 0           nin += 1;
504              
505             /* calculate error */
506 0 0         if ( error_sdev ) {
507              
508             /* weighted standard deviation */
509 0 0         if ( have_error ) {
510            
511             /* incremental algorithm for possibly weighted standard deviation; see
512             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
513             */
514 0           double _weight = 1 / ( error * error );
515 0           double _sum_weight = bweight += _weight;
516 0           double delta = signal - bmean;
517 0           bmean += delta * _weight / _sum_weight;
518 0           bm2 += _weight * delta * ( signal - bmean );
519              
520 0           bad_error = nin <= 1;
521 0           berror = bad_error
522             ? DBL_MAX
523 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
524            
525             }
526              
527             else {
528            
529             /* incremental algorithm for possibly weighted standard deviation; see
530             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
531             */
532 0           double _weight = 1;
533 0           double _sum_weight = nin;
534 0           double delta = signal - bmean;
535 0           bmean += delta * _weight / _sum_weight;
536 0           bm2 += _weight * delta * ( signal - bmean );
537              
538 0           bad_error = nin <= 1;
539 0           berror = bad_error
540             ? DBL_MAX
541 0 0         : sqrt( bm2 * 1 / (nin - 1) );
542            
543             }
544             }
545              
546 0 0         else if ( error_rss ) {
547            
548             /* incremental algorithm for mean; see
549             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
550             */
551 0           double _error2 = error2;
552 0           double _weight = 1 / ( error * error );
553 0           double delta = signal - bmean;
554              
555 0           bweight += _weight;
556 0           bmean += delta * _weight / bweight;
557 0           berror2 += _error2;
558 0           berror = sqrt( berror2 );
559            
560             }
561              
562 0 0         else if ( error_poisson ) {
563 0           berror = sqrt( nin );
564 0           bmean = bsignal / nin;
565             }
566              
567             else {
568 0           croak( "internal error" );
569             }
570            
571              
572 0           bsnr = bsignal / berror;
573 0           snr_ok = bsnr >= min_snr;
574              
575 0 0         if ( have_width )
576 0           bwidth += width;
577              
578 0 0         if ( nin == 1 )
579 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
580              
581            
582 0           rc |= \
583 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
584 0           | \
585 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
586 0           | \
587             ( nin >= min_nelem \
588 0 0         && bwidth >= min_width \
589 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
590             ; \
591             ;
592              
593 0 0         if ( rc ) {
594 0           rc |= lastrc;
595              
596            
597 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
598 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
599 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
600 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
601            
602 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
603 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
604            
605 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
606 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
607 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
608 0 0         if ( error_sdev ) {
609 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
610 0 0         if ( have_error ) {
611 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
612 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
613 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
614             }
615             else {
616 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
617             }
618             }
619 0 0         else if ( error_rss ) {
620 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
621 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
622             }
623            
624              
625 0           curind++;
626              
627             /* Reset bin counters */
628 0           rc = 0;
629 0           bsignal = 0;
630 0           bmean = 0;
631 0           bwidth = 0;
632 0           bsnr = 0;
633 0           berror2 = 0;
634 0           berror = 0;
635 0           bm2 = 0;
636 0           bsignal2 = 0;
637 0           bweight = 0;
638 0           bweight_sig = 0;
639 0           bweight_sig2 = 0;
640 0           bad_error = 0;
641 0           lastrc = 0;
642 0           nin = 0;
643              
644             }
645              
646 0 0         else if ( snr_ok ) {
647 0           lastrc = BIN_RC_GTMINSN;
648             }
649              
650             else {
651 0           lastrc = 0;
652             }
653             }} /* Close n */
654              
655              
656             /* record last bin if it's not empty */
657 0 0         if ( nin ) {
658              
659             /* needed for SET_RESULTS */
660 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
661              
662 0           rc = 0;
663 0           bad_error = 0;
664              
665             /* a non empty bin means that we didn't meet constraints. fold it into
666             the previous bin if requested & possible. sometimes that will
667             actually lower the S/N of the previous bin; keep going until
668             we can't fold anymore or we get the proper S/N
669             */
670 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
671              
672              
673 0 0         while ( --curind > 0 ) {
674             double tmp;
675 0           int snr_ok = 0;
676 0           PDL_Indx nin_last = nin;
677              
678            
679 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
680 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
681              
682             /* calculate error */
683 0 0         if ( error_sdev ) {
684              
685             /* weighted standard deviation */
686 0 0         if ( have_error ) {
687            
688             /* parallel algorithm (as we're adding bins together) for
689             (possibly) weighted standard deviation; see
690             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
691             */
692 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
693 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
694 0           double mean_b = bmean;
695 0           double weight_a = _weight;
696 0           double weight_b = bweight;
697 0           double weight_x = bweight += weight_a;
698 0           double delta = mean_b - mean_a;
699 0           bmean = mean_a + delta * weight_b / weight_x;
700 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
701 0           + delta * delta * weight_a * weight_b / weight_x;
702              
703 0           bad_error = nin <= 1;
704 0           berror = bad_error
705             ? DBL_MAX
706 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
707            
708             }
709              
710             else {
711            
712             /* parallel algorithm (as we're adding bins together) for
713             (possibly) weighted standard deviation; see
714             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
715             */
716 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
717 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
718 0           double mean_b = bmean;
719 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
720 0           double weight_b = nin_last;
721 0           double weight_x = nin;
722 0           double delta = mean_b - mean_a;
723 0           bmean = mean_a + delta * weight_b / weight_x;
724 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
725 0           + delta * delta * weight_a * weight_b / weight_x;
726              
727 0           bad_error = nin <= 1;
728 0           berror = bad_error
729             ? DBL_MAX
730 0 0         : sqrt( bm2 * 1 / (nin - 1) );
731            
732             }
733             }
734              
735 0 0         else if ( error_rss ) {
736            
737             /* parallel algorithm (as we're adding bins together) for
738             mean; see
739             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
740             */
741 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
742 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
743 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
744 0           double mean_b = bmean;
745 0           double weight_a = _weight;
746 0           double weight_b = bweight;
747 0           double weight_x = bweight += weight_a;
748 0           double delta = mean_b - mean_a;
749 0           bmean = mean_a + delta * weight_b / weight_x;
750              
751 0           berror2 += _error2;
752 0           berror = sqrt( berror2 );
753            
754             }
755              
756 0 0         else if ( error_poisson ) {
757 0           berror = sqrt( nin );
758 0           bmean = bsignal / nin;
759             }
760              
761             else {
762 0           croak( "internal error" );
763             }
764            
765              
766 0           bsnr = bsignal / berror;
767 0           snr_ok = bsnr >= min_snr;
768              
769            
770 0           rc |= \
771 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
772 0           | \
773 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
774 0           | \
775             ( nin >= min_nelem \
776 0 0         && bwidth >= min_width \
777 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
778             ; \
779             ;
780              
781 0 0         if (rc)
782 0           break;
783             }
784              
785             /* fix up index for events initially stuck in folded bins */
786 0           PDL_Indx curind1 = curind+1;
787             PDL_Indx ni;
788              
789 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
790 0 0         ni < __privtrans->ind_sizes[0] ;
791 0           ni++ ) {
792             #ifdef PDL_BAD_CODE
793 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
794             #endif /* PDL_BAD_CODE */
795 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
796             }
797 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
798 0           rc |= BIN_RC_FOLDED;
799             }
800              
801            
802 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
803 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
804 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
805 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
806            
807 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
808 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
809            
810 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
811 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
812 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
813 0 0         if ( error_sdev ) {
814 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
815 0 0         if ( have_error ) {
816 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
817 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
818 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
819             }
820             else {
821 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
822             }
823             }
824 0 0         else if ( error_rss ) {
825 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
826 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
827             }
828            
829             }
830             /* adjust for possibility of last bin being empty */
831 0           (nbins_datap)[0] = curind + ( nin != 0 );
832 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
833             }
834 0           } break;
835 0           case PDL_B: {
836 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Byte,B,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
837             {
838              
839              
840              
841              
842 0           int flags = __params->optflags;
843              
844 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
845 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
846 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
847 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
848 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
849 0           int fold_last_bin = flags & BIN_ARG_FOLD;
850 0           int set_bad = flags & BIN_ARG_SET_BAD;
851              
852 0           PDL_Indx min_nelem = __params->min_nelem;
853 0           PDL_Indx max_nelem = __params->max_nelem;
854 0           double max_width = __params->max_width;
855 0           double min_width = __params->min_width;
856 0           double min_snr = __params->min_snr;
857              
858             /* simplify the logic below by setting bounds values to their most
859             permissive extremes if they aren't needed. */
860              
861 0 0         if ( max_width == 0 )
862 0           max_width = DBL_MAX;
863              
864 0 0         if ( max_nelem == 0 )
865 0           max_nelem = LONG_MAX;
866              
867 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
868              
869 0           PDL_Indx curind = 0; /* index of current bin */
870              
871             /* Declare bin counters */
872 0           int rc = 0; /* status */
873 0           double bsignal = 0; /* sum of signal */
874 0           double bmean = 0; /* mean of (weighted) signal */
875 0           double bwidth = 0; /* width (if applicable) */
876 0           double bsnr = 0; /* SNR */
877 0           double berror2 = 0; /* sum of error^2 */
878 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
879 0           double bm2 = 0; /* unnormalized variance */
880 0           double bsignal2 = 0; /* sum of signal^2 */
881 0           double bweight = 0; /* sum of 1/error*2 */
882 0           double bweight_sig = 0; /* sum of weight * signal */
883 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
884 0           int bad_error = 0; /* if error2 is not good */
885 0           int lastrc = 0; /* carryover status from previous loop */
886 0           PDL_Indx nin = 0; /* number of elements in the current bin */
887              
888 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
889              
890 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
891             double error;
892             double error2;
893             double width;
894              
895 0           int snr_ok = 0;
896              
897 0 0         if ( have_error ) {
898 0           error = (error_datap)[0+(__inc_error_n*(n))];
899 0           error2 = error * error;
900             }
901              
902 0 0         if ( have_width )
903 0           width = (width_datap)[0+(__inc_width_n*(n))];
904              
905             #ifdef PDL_BAD_CODE
906 0 0         if ( PDL_ISBAD2(signal,signal_badval,B,signal_badval_isnan)
907 0 0         ||
    0          
908 0 0         have_error && PDL_ISBAD2(error,error_badval,B,error_badval_isnan) ) {
    0          
909 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
910 0           continue;
911             }
912             #endif /* PDL_BAD_CODE */
913 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
914              
915            
916 0           bsignal += signal;
917 0           nin += 1;
918              
919             /* calculate error */
920 0 0         if ( error_sdev ) {
921              
922             /* weighted standard deviation */
923 0 0         if ( have_error ) {
924            
925             /* incremental algorithm for possibly weighted standard deviation; see
926             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
927             */
928 0           double _weight = 1 / ( error * error );
929 0           double _sum_weight = bweight += _weight;
930 0           double delta = signal - bmean;
931 0           bmean += delta * _weight / _sum_weight;
932 0           bm2 += _weight * delta * ( signal - bmean );
933              
934 0           bad_error = nin <= 1;
935 0           berror = bad_error
936             ? DBL_MAX
937 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
938            
939             }
940              
941             else {
942            
943             /* incremental algorithm for possibly weighted standard deviation; see
944             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
945             */
946 0           double _weight = 1;
947 0           double _sum_weight = nin;
948 0           double delta = signal - bmean;
949 0           bmean += delta * _weight / _sum_weight;
950 0           bm2 += _weight * delta * ( signal - bmean );
951              
952 0           bad_error = nin <= 1;
953 0           berror = bad_error
954             ? DBL_MAX
955 0 0         : sqrt( bm2 * 1 / (nin - 1) );
956            
957             }
958             }
959              
960 0 0         else if ( error_rss ) {
961            
962             /* incremental algorithm for mean; see
963             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
964             */
965 0           double _error2 = error2;
966 0           double _weight = 1 / ( error * error );
967 0           double delta = signal - bmean;
968              
969 0           bweight += _weight;
970 0           bmean += delta * _weight / bweight;
971 0           berror2 += _error2;
972 0           berror = sqrt( berror2 );
973            
974             }
975              
976 0 0         else if ( error_poisson ) {
977 0           berror = sqrt( nin );
978 0           bmean = bsignal / nin;
979             }
980              
981             else {
982 0           croak( "internal error" );
983             }
984            
985              
986 0           bsnr = bsignal / berror;
987 0           snr_ok = bsnr >= min_snr;
988              
989 0 0         if ( have_width )
990 0           bwidth += width;
991              
992 0 0         if ( nin == 1 )
993 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
994              
995            
996 0           rc |= \
997 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
998 0           | \
999 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
1000 0           | \
1001             ( nin >= min_nelem \
1002 0 0         && bwidth >= min_width \
1003 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
1004             ; \
1005             ;
1006              
1007 0 0         if ( rc ) {
1008 0           rc |= lastrc;
1009              
1010            
1011 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
1012 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
1013 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
1014 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
1015            
1016 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
1017 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
1018            
1019 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
1020 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
1021 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
1022 0 0         if ( error_sdev ) {
1023 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
1024 0 0         if ( have_error ) {
1025 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1026 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
1027 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
1028             }
1029             else {
1030 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
1031             }
1032             }
1033 0 0         else if ( error_rss ) {
1034 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
1035 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1036             }
1037            
1038              
1039 0           curind++;
1040              
1041             /* Reset bin counters */
1042 0           rc = 0;
1043 0           bsignal = 0;
1044 0           bmean = 0;
1045 0           bwidth = 0;
1046 0           bsnr = 0;
1047 0           berror2 = 0;
1048 0           berror = 0;
1049 0           bm2 = 0;
1050 0           bsignal2 = 0;
1051 0           bweight = 0;
1052 0           bweight_sig = 0;
1053 0           bweight_sig2 = 0;
1054 0           bad_error = 0;
1055 0           lastrc = 0;
1056 0           nin = 0;
1057              
1058             }
1059              
1060 0 0         else if ( snr_ok ) {
1061 0           lastrc = BIN_RC_GTMINSN;
1062             }
1063              
1064             else {
1065 0           lastrc = 0;
1066             }
1067             }} /* Close n */
1068              
1069              
1070             /* record last bin if it's not empty */
1071 0 0         if ( nin ) {
1072              
1073             /* needed for SET_RESULTS */
1074 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
1075              
1076 0           rc = 0;
1077 0           bad_error = 0;
1078              
1079             /* a non empty bin means that we didn't meet constraints. fold it into
1080             the previous bin if requested & possible. sometimes that will
1081             actually lower the S/N of the previous bin; keep going until
1082             we can't fold anymore or we get the proper S/N
1083             */
1084 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
1085              
1086              
1087 0 0         while ( --curind > 0 ) {
1088             double tmp;
1089 0           int snr_ok = 0;
1090 0           PDL_Indx nin_last = nin;
1091              
1092            
1093 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
1094 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
1095              
1096             /* calculate error */
1097 0 0         if ( error_sdev ) {
1098              
1099             /* weighted standard deviation */
1100 0 0         if ( have_error ) {
1101            
1102             /* parallel algorithm (as we're adding bins together) for
1103             (possibly) weighted standard deviation; see
1104             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1105             */
1106 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1107 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1108 0           double mean_b = bmean;
1109 0           double weight_a = _weight;
1110 0           double weight_b = bweight;
1111 0           double weight_x = bweight += weight_a;
1112 0           double delta = mean_b - mean_a;
1113 0           bmean = mean_a + delta * weight_b / weight_x;
1114 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
1115 0           + delta * delta * weight_a * weight_b / weight_x;
1116              
1117 0           bad_error = nin <= 1;
1118 0           berror = bad_error
1119             ? DBL_MAX
1120 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
1121            
1122             }
1123              
1124             else {
1125            
1126             /* parallel algorithm (as we're adding bins together) for
1127             (possibly) weighted standard deviation; see
1128             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1129             */
1130 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1131 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1132 0           double mean_b = bmean;
1133 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
1134 0           double weight_b = nin_last;
1135 0           double weight_x = nin;
1136 0           double delta = mean_b - mean_a;
1137 0           bmean = mean_a + delta * weight_b / weight_x;
1138 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
1139 0           + delta * delta * weight_a * weight_b / weight_x;
1140              
1141 0           bad_error = nin <= 1;
1142 0           berror = bad_error
1143             ? DBL_MAX
1144 0 0         : sqrt( bm2 * 1 / (nin - 1) );
1145            
1146             }
1147             }
1148              
1149 0 0         else if ( error_rss ) {
1150            
1151             /* parallel algorithm (as we're adding bins together) for
1152             mean; see
1153             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1154             */
1155 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
1156 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1157 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1158 0           double mean_b = bmean;
1159 0           double weight_a = _weight;
1160 0           double weight_b = bweight;
1161 0           double weight_x = bweight += weight_a;
1162 0           double delta = mean_b - mean_a;
1163 0           bmean = mean_a + delta * weight_b / weight_x;
1164              
1165 0           berror2 += _error2;
1166 0           berror = sqrt( berror2 );
1167            
1168             }
1169              
1170 0 0         else if ( error_poisson ) {
1171 0           berror = sqrt( nin );
1172 0           bmean = bsignal / nin;
1173             }
1174              
1175             else {
1176 0           croak( "internal error" );
1177             }
1178            
1179              
1180 0           bsnr = bsignal / berror;
1181 0           snr_ok = bsnr >= min_snr;
1182              
1183            
1184 0           rc |= \
1185 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
1186 0           | \
1187 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
1188 0           | \
1189             ( nin >= min_nelem \
1190 0 0         && bwidth >= min_width \
1191 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
1192             ; \
1193             ;
1194              
1195 0 0         if (rc)
1196 0           break;
1197             }
1198              
1199             /* fix up index for events initially stuck in folded bins */
1200 0           PDL_Indx curind1 = curind+1;
1201             PDL_Indx ni;
1202              
1203 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
1204 0 0         ni < __privtrans->ind_sizes[0] ;
1205 0           ni++ ) {
1206             #ifdef PDL_BAD_CODE
1207 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
1208             #endif /* PDL_BAD_CODE */
1209 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
1210             }
1211 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
1212 0           rc |= BIN_RC_FOLDED;
1213             }
1214              
1215            
1216 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
1217 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
1218 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
1219 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
1220            
1221 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
1222 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
1223            
1224 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
1225 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
1226 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
1227 0 0         if ( error_sdev ) {
1228 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
1229 0 0         if ( have_error ) {
1230 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1231 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
1232 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
1233             }
1234             else {
1235 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
1236             }
1237             }
1238 0 0         else if ( error_rss ) {
1239 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
1240 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1241             }
1242            
1243             }
1244             /* adjust for possibility of last bin being empty */
1245 0           (nbins_datap)[0] = curind + ( nin != 0 );
1246 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
1247             }
1248 0           } break;
1249 0           case PDL_S: {
1250 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Short,S,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
1251             {
1252              
1253              
1254              
1255              
1256 0           int flags = __params->optflags;
1257              
1258 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
1259 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
1260 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
1261 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
1262 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
1263 0           int fold_last_bin = flags & BIN_ARG_FOLD;
1264 0           int set_bad = flags & BIN_ARG_SET_BAD;
1265              
1266 0           PDL_Indx min_nelem = __params->min_nelem;
1267 0           PDL_Indx max_nelem = __params->max_nelem;
1268 0           double max_width = __params->max_width;
1269 0           double min_width = __params->min_width;
1270 0           double min_snr = __params->min_snr;
1271              
1272             /* simplify the logic below by setting bounds values to their most
1273             permissive extremes if they aren't needed. */
1274              
1275 0 0         if ( max_width == 0 )
1276 0           max_width = DBL_MAX;
1277              
1278 0 0         if ( max_nelem == 0 )
1279 0           max_nelem = LONG_MAX;
1280              
1281 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
1282              
1283 0           PDL_Indx curind = 0; /* index of current bin */
1284              
1285             /* Declare bin counters */
1286 0           int rc = 0; /* status */
1287 0           double bsignal = 0; /* sum of signal */
1288 0           double bmean = 0; /* mean of (weighted) signal */
1289 0           double bwidth = 0; /* width (if applicable) */
1290 0           double bsnr = 0; /* SNR */
1291 0           double berror2 = 0; /* sum of error^2 */
1292 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
1293 0           double bm2 = 0; /* unnormalized variance */
1294 0           double bsignal2 = 0; /* sum of signal^2 */
1295 0           double bweight = 0; /* sum of 1/error*2 */
1296 0           double bweight_sig = 0; /* sum of weight * signal */
1297 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
1298 0           int bad_error = 0; /* if error2 is not good */
1299 0           int lastrc = 0; /* carryover status from previous loop */
1300 0           PDL_Indx nin = 0; /* number of elements in the current bin */
1301              
1302 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
1303              
1304 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
1305             double error;
1306             double error2;
1307             double width;
1308              
1309 0           int snr_ok = 0;
1310              
1311 0 0         if ( have_error ) {
1312 0           error = (error_datap)[0+(__inc_error_n*(n))];
1313 0           error2 = error * error;
1314             }
1315              
1316 0 0         if ( have_width )
1317 0           width = (width_datap)[0+(__inc_width_n*(n))];
1318              
1319             #ifdef PDL_BAD_CODE
1320 0 0         if ( PDL_ISBAD2(signal,signal_badval,S,signal_badval_isnan)
1321 0 0         ||
    0          
1322 0 0         have_error && PDL_ISBAD2(error,error_badval,S,error_badval_isnan) ) {
    0          
1323 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
1324 0           continue;
1325             }
1326             #endif /* PDL_BAD_CODE */
1327 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
1328              
1329            
1330 0           bsignal += signal;
1331 0           nin += 1;
1332              
1333             /* calculate error */
1334 0 0         if ( error_sdev ) {
1335              
1336             /* weighted standard deviation */
1337 0 0         if ( have_error ) {
1338            
1339             /* incremental algorithm for possibly weighted standard deviation; see
1340             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1341             */
1342 0           double _weight = 1 / ( error * error );
1343 0           double _sum_weight = bweight += _weight;
1344 0           double delta = signal - bmean;
1345 0           bmean += delta * _weight / _sum_weight;
1346 0           bm2 += _weight * delta * ( signal - bmean );
1347              
1348 0           bad_error = nin <= 1;
1349 0           berror = bad_error
1350             ? DBL_MAX
1351 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
1352            
1353             }
1354              
1355             else {
1356            
1357             /* incremental algorithm for possibly weighted standard deviation; see
1358             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1359             */
1360 0           double _weight = 1;
1361 0           double _sum_weight = nin;
1362 0           double delta = signal - bmean;
1363 0           bmean += delta * _weight / _sum_weight;
1364 0           bm2 += _weight * delta * ( signal - bmean );
1365              
1366 0           bad_error = nin <= 1;
1367 0           berror = bad_error
1368             ? DBL_MAX
1369 0 0         : sqrt( bm2 * 1 / (nin - 1) );
1370            
1371             }
1372             }
1373              
1374 0 0         else if ( error_rss ) {
1375            
1376             /* incremental algorithm for mean; see
1377             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1378             */
1379 0           double _error2 = error2;
1380 0           double _weight = 1 / ( error * error );
1381 0           double delta = signal - bmean;
1382              
1383 0           bweight += _weight;
1384 0           bmean += delta * _weight / bweight;
1385 0           berror2 += _error2;
1386 0           berror = sqrt( berror2 );
1387            
1388             }
1389              
1390 0 0         else if ( error_poisson ) {
1391 0           berror = sqrt( nin );
1392 0           bmean = bsignal / nin;
1393             }
1394              
1395             else {
1396 0           croak( "internal error" );
1397             }
1398            
1399              
1400 0           bsnr = bsignal / berror;
1401 0           snr_ok = bsnr >= min_snr;
1402              
1403 0 0         if ( have_width )
1404 0           bwidth += width;
1405              
1406 0 0         if ( nin == 1 )
1407 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
1408              
1409            
1410 0           rc |= \
1411 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
1412 0           | \
1413 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
1414 0           | \
1415             ( nin >= min_nelem \
1416 0 0         && bwidth >= min_width \
1417 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
1418             ; \
1419             ;
1420              
1421 0 0         if ( rc ) {
1422 0           rc |= lastrc;
1423              
1424            
1425 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
1426 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
1427 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
1428 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
1429            
1430 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
1431 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
1432            
1433 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
1434 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
1435 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
1436 0 0         if ( error_sdev ) {
1437 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
1438 0 0         if ( have_error ) {
1439 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1440 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
1441 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
1442             }
1443             else {
1444 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
1445             }
1446             }
1447 0 0         else if ( error_rss ) {
1448 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
1449 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1450             }
1451            
1452              
1453 0           curind++;
1454              
1455             /* Reset bin counters */
1456 0           rc = 0;
1457 0           bsignal = 0;
1458 0           bmean = 0;
1459 0           bwidth = 0;
1460 0           bsnr = 0;
1461 0           berror2 = 0;
1462 0           berror = 0;
1463 0           bm2 = 0;
1464 0           bsignal2 = 0;
1465 0           bweight = 0;
1466 0           bweight_sig = 0;
1467 0           bweight_sig2 = 0;
1468 0           bad_error = 0;
1469 0           lastrc = 0;
1470 0           nin = 0;
1471              
1472             }
1473              
1474 0 0         else if ( snr_ok ) {
1475 0           lastrc = BIN_RC_GTMINSN;
1476             }
1477              
1478             else {
1479 0           lastrc = 0;
1480             }
1481             }} /* Close n */
1482              
1483              
1484             /* record last bin if it's not empty */
1485 0 0         if ( nin ) {
1486              
1487             /* needed for SET_RESULTS */
1488 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
1489              
1490 0           rc = 0;
1491 0           bad_error = 0;
1492              
1493             /* a non empty bin means that we didn't meet constraints. fold it into
1494             the previous bin if requested & possible. sometimes that will
1495             actually lower the S/N of the previous bin; keep going until
1496             we can't fold anymore or we get the proper S/N
1497             */
1498 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
1499              
1500              
1501 0 0         while ( --curind > 0 ) {
1502             double tmp;
1503 0           int snr_ok = 0;
1504 0           PDL_Indx nin_last = nin;
1505              
1506            
1507 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
1508 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
1509              
1510             /* calculate error */
1511 0 0         if ( error_sdev ) {
1512              
1513             /* weighted standard deviation */
1514 0 0         if ( have_error ) {
1515            
1516             /* parallel algorithm (as we're adding bins together) for
1517             (possibly) weighted standard deviation; see
1518             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1519             */
1520 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1521 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1522 0           double mean_b = bmean;
1523 0           double weight_a = _weight;
1524 0           double weight_b = bweight;
1525 0           double weight_x = bweight += weight_a;
1526 0           double delta = mean_b - mean_a;
1527 0           bmean = mean_a + delta * weight_b / weight_x;
1528 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
1529 0           + delta * delta * weight_a * weight_b / weight_x;
1530              
1531 0           bad_error = nin <= 1;
1532 0           berror = bad_error
1533             ? DBL_MAX
1534 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
1535            
1536             }
1537              
1538             else {
1539            
1540             /* parallel algorithm (as we're adding bins together) for
1541             (possibly) weighted standard deviation; see
1542             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1543             */
1544 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1545 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1546 0           double mean_b = bmean;
1547 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
1548 0           double weight_b = nin_last;
1549 0           double weight_x = nin;
1550 0           double delta = mean_b - mean_a;
1551 0           bmean = mean_a + delta * weight_b / weight_x;
1552 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
1553 0           + delta * delta * weight_a * weight_b / weight_x;
1554              
1555 0           bad_error = nin <= 1;
1556 0           berror = bad_error
1557             ? DBL_MAX
1558 0 0         : sqrt( bm2 * 1 / (nin - 1) );
1559            
1560             }
1561             }
1562              
1563 0 0         else if ( error_rss ) {
1564            
1565             /* parallel algorithm (as we're adding bins together) for
1566             mean; see
1567             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1568             */
1569 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
1570 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1571 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1572 0           double mean_b = bmean;
1573 0           double weight_a = _weight;
1574 0           double weight_b = bweight;
1575 0           double weight_x = bweight += weight_a;
1576 0           double delta = mean_b - mean_a;
1577 0           bmean = mean_a + delta * weight_b / weight_x;
1578              
1579 0           berror2 += _error2;
1580 0           berror = sqrt( berror2 );
1581            
1582             }
1583              
1584 0 0         else if ( error_poisson ) {
1585 0           berror = sqrt( nin );
1586 0           bmean = bsignal / nin;
1587             }
1588              
1589             else {
1590 0           croak( "internal error" );
1591             }
1592            
1593              
1594 0           bsnr = bsignal / berror;
1595 0           snr_ok = bsnr >= min_snr;
1596              
1597            
1598 0           rc |= \
1599 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
1600 0           | \
1601 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
1602 0           | \
1603             ( nin >= min_nelem \
1604 0 0         && bwidth >= min_width \
1605 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
1606             ; \
1607             ;
1608              
1609 0 0         if (rc)
1610 0           break;
1611             }
1612              
1613             /* fix up index for events initially stuck in folded bins */
1614 0           PDL_Indx curind1 = curind+1;
1615             PDL_Indx ni;
1616              
1617 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
1618 0 0         ni < __privtrans->ind_sizes[0] ;
1619 0           ni++ ) {
1620             #ifdef PDL_BAD_CODE
1621 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
1622             #endif /* PDL_BAD_CODE */
1623 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
1624             }
1625 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
1626 0           rc |= BIN_RC_FOLDED;
1627             }
1628              
1629            
1630 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
1631 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
1632 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
1633 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
1634            
1635 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
1636 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
1637            
1638 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
1639 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
1640 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
1641 0 0         if ( error_sdev ) {
1642 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
1643 0 0         if ( have_error ) {
1644 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1645 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
1646 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
1647             }
1648             else {
1649 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
1650             }
1651             }
1652 0 0         else if ( error_rss ) {
1653 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
1654 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1655             }
1656            
1657             }
1658             /* adjust for possibility of last bin being empty */
1659 0           (nbins_datap)[0] = curind + ( nin != 0 );
1660 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
1661             }
1662 0           } break;
1663 0           case PDL_US: {
1664 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Ushort,U,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
1665             {
1666              
1667              
1668              
1669              
1670 0           int flags = __params->optflags;
1671              
1672 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
1673 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
1674 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
1675 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
1676 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
1677 0           int fold_last_bin = flags & BIN_ARG_FOLD;
1678 0           int set_bad = flags & BIN_ARG_SET_BAD;
1679              
1680 0           PDL_Indx min_nelem = __params->min_nelem;
1681 0           PDL_Indx max_nelem = __params->max_nelem;
1682 0           double max_width = __params->max_width;
1683 0           double min_width = __params->min_width;
1684 0           double min_snr = __params->min_snr;
1685              
1686             /* simplify the logic below by setting bounds values to their most
1687             permissive extremes if they aren't needed. */
1688              
1689 0 0         if ( max_width == 0 )
1690 0           max_width = DBL_MAX;
1691              
1692 0 0         if ( max_nelem == 0 )
1693 0           max_nelem = LONG_MAX;
1694              
1695 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
1696              
1697 0           PDL_Indx curind = 0; /* index of current bin */
1698              
1699             /* Declare bin counters */
1700 0           int rc = 0; /* status */
1701 0           double bsignal = 0; /* sum of signal */
1702 0           double bmean = 0; /* mean of (weighted) signal */
1703 0           double bwidth = 0; /* width (if applicable) */
1704 0           double bsnr = 0; /* SNR */
1705 0           double berror2 = 0; /* sum of error^2 */
1706 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
1707 0           double bm2 = 0; /* unnormalized variance */
1708 0           double bsignal2 = 0; /* sum of signal^2 */
1709 0           double bweight = 0; /* sum of 1/error*2 */
1710 0           double bweight_sig = 0; /* sum of weight * signal */
1711 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
1712 0           int bad_error = 0; /* if error2 is not good */
1713 0           int lastrc = 0; /* carryover status from previous loop */
1714 0           PDL_Indx nin = 0; /* number of elements in the current bin */
1715              
1716 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
1717              
1718 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
1719             double error;
1720             double error2;
1721             double width;
1722              
1723 0           int snr_ok = 0;
1724              
1725 0 0         if ( have_error ) {
1726 0           error = (error_datap)[0+(__inc_error_n*(n))];
1727 0           error2 = error * error;
1728             }
1729              
1730 0 0         if ( have_width )
1731 0           width = (width_datap)[0+(__inc_width_n*(n))];
1732              
1733             #ifdef PDL_BAD_CODE
1734 0 0         if ( PDL_ISBAD2(signal,signal_badval,U,signal_badval_isnan)
1735 0 0         ||
    0          
1736 0 0         have_error && PDL_ISBAD2(error,error_badval,U,error_badval_isnan) ) {
    0          
1737 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
1738 0           continue;
1739             }
1740             #endif /* PDL_BAD_CODE */
1741 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
1742              
1743            
1744 0           bsignal += signal;
1745 0           nin += 1;
1746              
1747             /* calculate error */
1748 0 0         if ( error_sdev ) {
1749              
1750             /* weighted standard deviation */
1751 0 0         if ( have_error ) {
1752            
1753             /* incremental algorithm for possibly weighted standard deviation; see
1754             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1755             */
1756 0           double _weight = 1 / ( error * error );
1757 0           double _sum_weight = bweight += _weight;
1758 0           double delta = signal - bmean;
1759 0           bmean += delta * _weight / _sum_weight;
1760 0           bm2 += _weight * delta * ( signal - bmean );
1761              
1762 0           bad_error = nin <= 1;
1763 0           berror = bad_error
1764             ? DBL_MAX
1765 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
1766            
1767             }
1768              
1769             else {
1770            
1771             /* incremental algorithm for possibly weighted standard deviation; see
1772             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1773             */
1774 0           double _weight = 1;
1775 0           double _sum_weight = nin;
1776 0           double delta = signal - bmean;
1777 0           bmean += delta * _weight / _sum_weight;
1778 0           bm2 += _weight * delta * ( signal - bmean );
1779              
1780 0           bad_error = nin <= 1;
1781 0           berror = bad_error
1782             ? DBL_MAX
1783 0 0         : sqrt( bm2 * 1 / (nin - 1) );
1784            
1785             }
1786             }
1787              
1788 0 0         else if ( error_rss ) {
1789            
1790             /* incremental algorithm for mean; see
1791             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1792             */
1793 0           double _error2 = error2;
1794 0           double _weight = 1 / ( error * error );
1795 0           double delta = signal - bmean;
1796              
1797 0           bweight += _weight;
1798 0           bmean += delta * _weight / bweight;
1799 0           berror2 += _error2;
1800 0           berror = sqrt( berror2 );
1801            
1802             }
1803              
1804 0 0         else if ( error_poisson ) {
1805 0           berror = sqrt( nin );
1806 0           bmean = bsignal / nin;
1807             }
1808              
1809             else {
1810 0           croak( "internal error" );
1811             }
1812            
1813              
1814 0           bsnr = bsignal / berror;
1815 0           snr_ok = bsnr >= min_snr;
1816              
1817 0 0         if ( have_width )
1818 0           bwidth += width;
1819              
1820 0 0         if ( nin == 1 )
1821 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
1822              
1823            
1824 0           rc |= \
1825 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
1826 0           | \
1827 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
1828 0           | \
1829             ( nin >= min_nelem \
1830 0 0         && bwidth >= min_width \
1831 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
1832             ; \
1833             ;
1834              
1835 0 0         if ( rc ) {
1836 0           rc |= lastrc;
1837              
1838            
1839 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
1840 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
1841 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
1842 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
1843            
1844 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
1845 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
1846            
1847 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
1848 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
1849 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
1850 0 0         if ( error_sdev ) {
1851 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
1852 0 0         if ( have_error ) {
1853 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1854 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
1855 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
1856             }
1857             else {
1858 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
1859             }
1860             }
1861 0 0         else if ( error_rss ) {
1862 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
1863 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
1864             }
1865            
1866              
1867 0           curind++;
1868              
1869             /* Reset bin counters */
1870 0           rc = 0;
1871 0           bsignal = 0;
1872 0           bmean = 0;
1873 0           bwidth = 0;
1874 0           bsnr = 0;
1875 0           berror2 = 0;
1876 0           berror = 0;
1877 0           bm2 = 0;
1878 0           bsignal2 = 0;
1879 0           bweight = 0;
1880 0           bweight_sig = 0;
1881 0           bweight_sig2 = 0;
1882 0           bad_error = 0;
1883 0           lastrc = 0;
1884 0           nin = 0;
1885              
1886             }
1887              
1888 0 0         else if ( snr_ok ) {
1889 0           lastrc = BIN_RC_GTMINSN;
1890             }
1891              
1892             else {
1893 0           lastrc = 0;
1894             }
1895             }} /* Close n */
1896              
1897              
1898             /* record last bin if it's not empty */
1899 0 0         if ( nin ) {
1900              
1901             /* needed for SET_RESULTS */
1902 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
1903              
1904 0           rc = 0;
1905 0           bad_error = 0;
1906              
1907             /* a non empty bin means that we didn't meet constraints. fold it into
1908             the previous bin if requested & possible. sometimes that will
1909             actually lower the S/N of the previous bin; keep going until
1910             we can't fold anymore or we get the proper S/N
1911             */
1912 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
1913              
1914              
1915 0 0         while ( --curind > 0 ) {
1916             double tmp;
1917 0           int snr_ok = 0;
1918 0           PDL_Indx nin_last = nin;
1919              
1920            
1921 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
1922 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
1923              
1924             /* calculate error */
1925 0 0         if ( error_sdev ) {
1926              
1927             /* weighted standard deviation */
1928 0 0         if ( have_error ) {
1929            
1930             /* parallel algorithm (as we're adding bins together) for
1931             (possibly) weighted standard deviation; see
1932             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1933             */
1934 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1935 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1936 0           double mean_b = bmean;
1937 0           double weight_a = _weight;
1938 0           double weight_b = bweight;
1939 0           double weight_x = bweight += weight_a;
1940 0           double delta = mean_b - mean_a;
1941 0           bmean = mean_a + delta * weight_b / weight_x;
1942 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
1943 0           + delta * delta * weight_a * weight_b / weight_x;
1944              
1945 0           bad_error = nin <= 1;
1946 0           berror = bad_error
1947             ? DBL_MAX
1948 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
1949            
1950             }
1951              
1952             else {
1953            
1954             /* parallel algorithm (as we're adding bins together) for
1955             (possibly) weighted standard deviation; see
1956             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1957             */
1958 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1959 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1960 0           double mean_b = bmean;
1961 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
1962 0           double weight_b = nin_last;
1963 0           double weight_x = nin;
1964 0           double delta = mean_b - mean_a;
1965 0           bmean = mean_a + delta * weight_b / weight_x;
1966 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
1967 0           + delta * delta * weight_a * weight_b / weight_x;
1968              
1969 0           bad_error = nin <= 1;
1970 0           berror = bad_error
1971             ? DBL_MAX
1972 0 0         : sqrt( bm2 * 1 / (nin - 1) );
1973            
1974             }
1975             }
1976              
1977 0 0         else if ( error_rss ) {
1978            
1979             /* parallel algorithm (as we're adding bins together) for
1980             mean; see
1981             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1982             */
1983 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
1984 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
1985 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
1986 0           double mean_b = bmean;
1987 0           double weight_a = _weight;
1988 0           double weight_b = bweight;
1989 0           double weight_x = bweight += weight_a;
1990 0           double delta = mean_b - mean_a;
1991 0           bmean = mean_a + delta * weight_b / weight_x;
1992              
1993 0           berror2 += _error2;
1994 0           berror = sqrt( berror2 );
1995            
1996             }
1997              
1998 0 0         else if ( error_poisson ) {
1999 0           berror = sqrt( nin );
2000 0           bmean = bsignal / nin;
2001             }
2002              
2003             else {
2004 0           croak( "internal error" );
2005             }
2006            
2007              
2008 0           bsnr = bsignal / berror;
2009 0           snr_ok = bsnr >= min_snr;
2010              
2011            
2012 0           rc |= \
2013 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
2014 0           | \
2015 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
2016 0           | \
2017             ( nin >= min_nelem \
2018 0 0         && bwidth >= min_width \
2019 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
2020             ; \
2021             ;
2022              
2023 0 0         if (rc)
2024 0           break;
2025             }
2026              
2027             /* fix up index for events initially stuck in folded bins */
2028 0           PDL_Indx curind1 = curind+1;
2029             PDL_Indx ni;
2030              
2031 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
2032 0 0         ni < __privtrans->ind_sizes[0] ;
2033 0           ni++ ) {
2034             #ifdef PDL_BAD_CODE
2035 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
2036             #endif /* PDL_BAD_CODE */
2037 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
2038             }
2039 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2040 0           rc |= BIN_RC_FOLDED;
2041             }
2042              
2043            
2044 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
2045 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
2046 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
2047 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
2048            
2049 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
2050 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
2051            
2052 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
2053 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
2054 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2055 0 0         if ( error_sdev ) {
2056 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
2057 0 0         if ( have_error ) {
2058 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2059 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
2060 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
2061             }
2062             else {
2063 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
2064             }
2065             }
2066 0 0         else if ( error_rss ) {
2067 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
2068 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2069             }
2070            
2071             }
2072             /* adjust for possibility of last bin being empty */
2073 0           (nbins_datap)[0] = curind + ( nin != 0 );
2074 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
2075             }
2076 0           } break;
2077 0           case PDL_L: {
2078 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Long,L,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
2079             {
2080              
2081              
2082              
2083              
2084 0           int flags = __params->optflags;
2085              
2086 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
2087 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
2088 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
2089 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
2090 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
2091 0           int fold_last_bin = flags & BIN_ARG_FOLD;
2092 0           int set_bad = flags & BIN_ARG_SET_BAD;
2093              
2094 0           PDL_Indx min_nelem = __params->min_nelem;
2095 0           PDL_Indx max_nelem = __params->max_nelem;
2096 0           double max_width = __params->max_width;
2097 0           double min_width = __params->min_width;
2098 0           double min_snr = __params->min_snr;
2099              
2100             /* simplify the logic below by setting bounds values to their most
2101             permissive extremes if they aren't needed. */
2102              
2103 0 0         if ( max_width == 0 )
2104 0           max_width = DBL_MAX;
2105              
2106 0 0         if ( max_nelem == 0 )
2107 0           max_nelem = LONG_MAX;
2108              
2109 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
2110              
2111 0           PDL_Indx curind = 0; /* index of current bin */
2112              
2113             /* Declare bin counters */
2114 0           int rc = 0; /* status */
2115 0           double bsignal = 0; /* sum of signal */
2116 0           double bmean = 0; /* mean of (weighted) signal */
2117 0           double bwidth = 0; /* width (if applicable) */
2118 0           double bsnr = 0; /* SNR */
2119 0           double berror2 = 0; /* sum of error^2 */
2120 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
2121 0           double bm2 = 0; /* unnormalized variance */
2122 0           double bsignal2 = 0; /* sum of signal^2 */
2123 0           double bweight = 0; /* sum of 1/error*2 */
2124 0           double bweight_sig = 0; /* sum of weight * signal */
2125 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
2126 0           int bad_error = 0; /* if error2 is not good */
2127 0           int lastrc = 0; /* carryover status from previous loop */
2128 0           PDL_Indx nin = 0; /* number of elements in the current bin */
2129              
2130 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
2131              
2132 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
2133             double error;
2134             double error2;
2135             double width;
2136              
2137 0           int snr_ok = 0;
2138              
2139 0 0         if ( have_error ) {
2140 0           error = (error_datap)[0+(__inc_error_n*(n))];
2141 0           error2 = error * error;
2142             }
2143              
2144 0 0         if ( have_width )
2145 0           width = (width_datap)[0+(__inc_width_n*(n))];
2146              
2147             #ifdef PDL_BAD_CODE
2148 0 0         if ( PDL_ISBAD2(signal,signal_badval,L,signal_badval_isnan)
2149 0 0         ||
    0          
2150 0 0         have_error && PDL_ISBAD2(error,error_badval,L,error_badval_isnan) ) {
    0          
2151 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
2152 0           continue;
2153             }
2154             #endif /* PDL_BAD_CODE */
2155 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
2156              
2157            
2158 0           bsignal += signal;
2159 0           nin += 1;
2160              
2161             /* calculate error */
2162 0 0         if ( error_sdev ) {
2163              
2164             /* weighted standard deviation */
2165 0 0         if ( have_error ) {
2166            
2167             /* incremental algorithm for possibly weighted standard deviation; see
2168             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2169             */
2170 0           double _weight = 1 / ( error * error );
2171 0           double _sum_weight = bweight += _weight;
2172 0           double delta = signal - bmean;
2173 0           bmean += delta * _weight / _sum_weight;
2174 0           bm2 += _weight * delta * ( signal - bmean );
2175              
2176 0           bad_error = nin <= 1;
2177 0           berror = bad_error
2178             ? DBL_MAX
2179 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
2180            
2181             }
2182              
2183             else {
2184            
2185             /* incremental algorithm for possibly weighted standard deviation; see
2186             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2187             */
2188 0           double _weight = 1;
2189 0           double _sum_weight = nin;
2190 0           double delta = signal - bmean;
2191 0           bmean += delta * _weight / _sum_weight;
2192 0           bm2 += _weight * delta * ( signal - bmean );
2193              
2194 0           bad_error = nin <= 1;
2195 0           berror = bad_error
2196             ? DBL_MAX
2197 0 0         : sqrt( bm2 * 1 / (nin - 1) );
2198            
2199             }
2200             }
2201              
2202 0 0         else if ( error_rss ) {
2203            
2204             /* incremental algorithm for mean; see
2205             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2206             */
2207 0           double _error2 = error2;
2208 0           double _weight = 1 / ( error * error );
2209 0           double delta = signal - bmean;
2210              
2211 0           bweight += _weight;
2212 0           bmean += delta * _weight / bweight;
2213 0           berror2 += _error2;
2214 0           berror = sqrt( berror2 );
2215            
2216             }
2217              
2218 0 0         else if ( error_poisson ) {
2219 0           berror = sqrt( nin );
2220 0           bmean = bsignal / nin;
2221             }
2222              
2223             else {
2224 0           croak( "internal error" );
2225             }
2226            
2227              
2228 0           bsnr = bsignal / berror;
2229 0           snr_ok = bsnr >= min_snr;
2230              
2231 0 0         if ( have_width )
2232 0           bwidth += width;
2233              
2234 0 0         if ( nin == 1 )
2235 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
2236              
2237            
2238 0           rc |= \
2239 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
2240 0           | \
2241 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
2242 0           | \
2243             ( nin >= min_nelem \
2244 0 0         && bwidth >= min_width \
2245 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
2246             ; \
2247             ;
2248              
2249 0 0         if ( rc ) {
2250 0           rc |= lastrc;
2251              
2252            
2253 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
2254 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
2255 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
2256 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
2257            
2258 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
2259 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
2260            
2261 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
2262 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
2263 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2264 0 0         if ( error_sdev ) {
2265 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
2266 0 0         if ( have_error ) {
2267 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2268 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
2269 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
2270             }
2271             else {
2272 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
2273             }
2274             }
2275 0 0         else if ( error_rss ) {
2276 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
2277 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2278             }
2279            
2280              
2281 0           curind++;
2282              
2283             /* Reset bin counters */
2284 0           rc = 0;
2285 0           bsignal = 0;
2286 0           bmean = 0;
2287 0           bwidth = 0;
2288 0           bsnr = 0;
2289 0           berror2 = 0;
2290 0           berror = 0;
2291 0           bm2 = 0;
2292 0           bsignal2 = 0;
2293 0           bweight = 0;
2294 0           bweight_sig = 0;
2295 0           bweight_sig2 = 0;
2296 0           bad_error = 0;
2297 0           lastrc = 0;
2298 0           nin = 0;
2299              
2300             }
2301              
2302 0 0         else if ( snr_ok ) {
2303 0           lastrc = BIN_RC_GTMINSN;
2304             }
2305              
2306             else {
2307 0           lastrc = 0;
2308             }
2309             }} /* Close n */
2310              
2311              
2312             /* record last bin if it's not empty */
2313 0 0         if ( nin ) {
2314              
2315             /* needed for SET_RESULTS */
2316 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
2317              
2318 0           rc = 0;
2319 0           bad_error = 0;
2320              
2321             /* a non empty bin means that we didn't meet constraints. fold it into
2322             the previous bin if requested & possible. sometimes that will
2323             actually lower the S/N of the previous bin; keep going until
2324             we can't fold anymore or we get the proper S/N
2325             */
2326 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
2327              
2328              
2329 0 0         while ( --curind > 0 ) {
2330             double tmp;
2331 0           int snr_ok = 0;
2332 0           PDL_Indx nin_last = nin;
2333              
2334            
2335 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
2336 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
2337              
2338             /* calculate error */
2339 0 0         if ( error_sdev ) {
2340              
2341             /* weighted standard deviation */
2342 0 0         if ( have_error ) {
2343            
2344             /* parallel algorithm (as we're adding bins together) for
2345             (possibly) weighted standard deviation; see
2346             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2347             */
2348 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
2349 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
2350 0           double mean_b = bmean;
2351 0           double weight_a = _weight;
2352 0           double weight_b = bweight;
2353 0           double weight_x = bweight += weight_a;
2354 0           double delta = mean_b - mean_a;
2355 0           bmean = mean_a + delta * weight_b / weight_x;
2356 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
2357 0           + delta * delta * weight_a * weight_b / weight_x;
2358              
2359 0           bad_error = nin <= 1;
2360 0           berror = bad_error
2361             ? DBL_MAX
2362 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
2363            
2364             }
2365              
2366             else {
2367            
2368             /* parallel algorithm (as we're adding bins together) for
2369             (possibly) weighted standard deviation; see
2370             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2371             */
2372 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
2373 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
2374 0           double mean_b = bmean;
2375 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
2376 0           double weight_b = nin_last;
2377 0           double weight_x = nin;
2378 0           double delta = mean_b - mean_a;
2379 0           bmean = mean_a + delta * weight_b / weight_x;
2380 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
2381 0           + delta * delta * weight_a * weight_b / weight_x;
2382              
2383 0           bad_error = nin <= 1;
2384 0           berror = bad_error
2385             ? DBL_MAX
2386 0 0         : sqrt( bm2 * 1 / (nin - 1) );
2387            
2388             }
2389             }
2390              
2391 0 0         else if ( error_rss ) {
2392            
2393             /* parallel algorithm (as we're adding bins together) for
2394             mean; see
2395             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2396             */
2397 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
2398 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
2399 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
2400 0           double mean_b = bmean;
2401 0           double weight_a = _weight;
2402 0           double weight_b = bweight;
2403 0           double weight_x = bweight += weight_a;
2404 0           double delta = mean_b - mean_a;
2405 0           bmean = mean_a + delta * weight_b / weight_x;
2406              
2407 0           berror2 += _error2;
2408 0           berror = sqrt( berror2 );
2409            
2410             }
2411              
2412 0 0         else if ( error_poisson ) {
2413 0           berror = sqrt( nin );
2414 0           bmean = bsignal / nin;
2415             }
2416              
2417             else {
2418 0           croak( "internal error" );
2419             }
2420            
2421              
2422 0           bsnr = bsignal / berror;
2423 0           snr_ok = bsnr >= min_snr;
2424              
2425            
2426 0           rc |= \
2427 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
2428 0           | \
2429 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
2430 0           | \
2431             ( nin >= min_nelem \
2432 0 0         && bwidth >= min_width \
2433 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
2434             ; \
2435             ;
2436              
2437 0 0         if (rc)
2438 0           break;
2439             }
2440              
2441             /* fix up index for events initially stuck in folded bins */
2442 0           PDL_Indx curind1 = curind+1;
2443             PDL_Indx ni;
2444              
2445 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
2446 0 0         ni < __privtrans->ind_sizes[0] ;
2447 0           ni++ ) {
2448             #ifdef PDL_BAD_CODE
2449 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
2450             #endif /* PDL_BAD_CODE */
2451 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
2452             }
2453 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2454 0           rc |= BIN_RC_FOLDED;
2455             }
2456              
2457            
2458 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
2459 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
2460 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
2461 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
2462            
2463 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
2464 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
2465            
2466 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
2467 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
2468 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2469 0 0         if ( error_sdev ) {
2470 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
2471 0 0         if ( have_error ) {
2472 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2473 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
2474 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
2475             }
2476             else {
2477 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
2478             }
2479             }
2480 0 0         else if ( error_rss ) {
2481 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
2482 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2483             }
2484            
2485             }
2486             /* adjust for possibility of last bin being empty */
2487 0           (nbins_datap)[0] = curind + ( nin != 0 );
2488 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
2489             }
2490 0           } break;
2491 0           case PDL_UL: {
2492 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_ULong,K,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
2493             {
2494              
2495              
2496              
2497              
2498 0           int flags = __params->optflags;
2499              
2500 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
2501 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
2502 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
2503 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
2504 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
2505 0           int fold_last_bin = flags & BIN_ARG_FOLD;
2506 0           int set_bad = flags & BIN_ARG_SET_BAD;
2507              
2508 0           PDL_Indx min_nelem = __params->min_nelem;
2509 0           PDL_Indx max_nelem = __params->max_nelem;
2510 0           double max_width = __params->max_width;
2511 0           double min_width = __params->min_width;
2512 0           double min_snr = __params->min_snr;
2513              
2514             /* simplify the logic below by setting bounds values to their most
2515             permissive extremes if they aren't needed. */
2516              
2517 0 0         if ( max_width == 0 )
2518 0           max_width = DBL_MAX;
2519              
2520 0 0         if ( max_nelem == 0 )
2521 0           max_nelem = LONG_MAX;
2522              
2523 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
2524              
2525 0           PDL_Indx curind = 0; /* index of current bin */
2526              
2527             /* Declare bin counters */
2528 0           int rc = 0; /* status */
2529 0           double bsignal = 0; /* sum of signal */
2530 0           double bmean = 0; /* mean of (weighted) signal */
2531 0           double bwidth = 0; /* width (if applicable) */
2532 0           double bsnr = 0; /* SNR */
2533 0           double berror2 = 0; /* sum of error^2 */
2534 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
2535 0           double bm2 = 0; /* unnormalized variance */
2536 0           double bsignal2 = 0; /* sum of signal^2 */
2537 0           double bweight = 0; /* sum of 1/error*2 */
2538 0           double bweight_sig = 0; /* sum of weight * signal */
2539 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
2540 0           int bad_error = 0; /* if error2 is not good */
2541 0           int lastrc = 0; /* carryover status from previous loop */
2542 0           PDL_Indx nin = 0; /* number of elements in the current bin */
2543              
2544 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
2545              
2546 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
2547             double error;
2548             double error2;
2549             double width;
2550              
2551 0           int snr_ok = 0;
2552              
2553 0 0         if ( have_error ) {
2554 0           error = (error_datap)[0+(__inc_error_n*(n))];
2555 0           error2 = error * error;
2556             }
2557              
2558 0 0         if ( have_width )
2559 0           width = (width_datap)[0+(__inc_width_n*(n))];
2560              
2561             #ifdef PDL_BAD_CODE
2562 0 0         if ( PDL_ISBAD2(signal,signal_badval,K,signal_badval_isnan)
2563 0 0         ||
    0          
2564 0 0         have_error && PDL_ISBAD2(error,error_badval,K,error_badval_isnan) ) {
    0          
2565 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
2566 0           continue;
2567             }
2568             #endif /* PDL_BAD_CODE */
2569 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
2570              
2571            
2572 0           bsignal += signal;
2573 0           nin += 1;
2574              
2575             /* calculate error */
2576 0 0         if ( error_sdev ) {
2577              
2578             /* weighted standard deviation */
2579 0 0         if ( have_error ) {
2580            
2581             /* incremental algorithm for possibly weighted standard deviation; see
2582             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2583             */
2584 0           double _weight = 1 / ( error * error );
2585 0           double _sum_weight = bweight += _weight;
2586 0           double delta = signal - bmean;
2587 0           bmean += delta * _weight / _sum_weight;
2588 0           bm2 += _weight * delta * ( signal - bmean );
2589              
2590 0           bad_error = nin <= 1;
2591 0           berror = bad_error
2592             ? DBL_MAX
2593 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
2594            
2595             }
2596              
2597             else {
2598            
2599             /* incremental algorithm for possibly weighted standard deviation; see
2600             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2601             */
2602 0           double _weight = 1;
2603 0           double _sum_weight = nin;
2604 0           double delta = signal - bmean;
2605 0           bmean += delta * _weight / _sum_weight;
2606 0           bm2 += _weight * delta * ( signal - bmean );
2607              
2608 0           bad_error = nin <= 1;
2609 0           berror = bad_error
2610             ? DBL_MAX
2611 0 0         : sqrt( bm2 * 1 / (nin - 1) );
2612            
2613             }
2614             }
2615              
2616 0 0         else if ( error_rss ) {
2617            
2618             /* incremental algorithm for mean; see
2619             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2620             */
2621 0           double _error2 = error2;
2622 0           double _weight = 1 / ( error * error );
2623 0           double delta = signal - bmean;
2624              
2625 0           bweight += _weight;
2626 0           bmean += delta * _weight / bweight;
2627 0           berror2 += _error2;
2628 0           berror = sqrt( berror2 );
2629            
2630             }
2631              
2632 0 0         else if ( error_poisson ) {
2633 0           berror = sqrt( nin );
2634 0           bmean = bsignal / nin;
2635             }
2636              
2637             else {
2638 0           croak( "internal error" );
2639             }
2640            
2641              
2642 0           bsnr = bsignal / berror;
2643 0           snr_ok = bsnr >= min_snr;
2644              
2645 0 0         if ( have_width )
2646 0           bwidth += width;
2647              
2648 0 0         if ( nin == 1 )
2649 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
2650              
2651            
2652 0           rc |= \
2653 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
2654 0           | \
2655 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
2656 0           | \
2657             ( nin >= min_nelem \
2658 0 0         && bwidth >= min_width \
2659 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
2660             ; \
2661             ;
2662              
2663 0 0         if ( rc ) {
2664 0           rc |= lastrc;
2665              
2666            
2667 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
2668 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
2669 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
2670 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
2671            
2672 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
2673 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
2674            
2675 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
2676 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
2677 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2678 0 0         if ( error_sdev ) {
2679 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
2680 0 0         if ( have_error ) {
2681 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2682 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
2683 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
2684             }
2685             else {
2686 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
2687             }
2688             }
2689 0 0         else if ( error_rss ) {
2690 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
2691 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2692             }
2693            
2694              
2695 0           curind++;
2696              
2697             /* Reset bin counters */
2698 0           rc = 0;
2699 0           bsignal = 0;
2700 0           bmean = 0;
2701 0           bwidth = 0;
2702 0           bsnr = 0;
2703 0           berror2 = 0;
2704 0           berror = 0;
2705 0           bm2 = 0;
2706 0           bsignal2 = 0;
2707 0           bweight = 0;
2708 0           bweight_sig = 0;
2709 0           bweight_sig2 = 0;
2710 0           bad_error = 0;
2711 0           lastrc = 0;
2712 0           nin = 0;
2713              
2714             }
2715              
2716 0 0         else if ( snr_ok ) {
2717 0           lastrc = BIN_RC_GTMINSN;
2718             }
2719              
2720             else {
2721 0           lastrc = 0;
2722             }
2723             }} /* Close n */
2724              
2725              
2726             /* record last bin if it's not empty */
2727 0 0         if ( nin ) {
2728              
2729             /* needed for SET_RESULTS */
2730 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
2731              
2732 0           rc = 0;
2733 0           bad_error = 0;
2734              
2735             /* a non empty bin means that we didn't meet constraints. fold it into
2736             the previous bin if requested & possible. sometimes that will
2737             actually lower the S/N of the previous bin; keep going until
2738             we can't fold anymore or we get the proper S/N
2739             */
2740 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
2741              
2742              
2743 0 0         while ( --curind > 0 ) {
2744             double tmp;
2745 0           int snr_ok = 0;
2746 0           PDL_Indx nin_last = nin;
2747              
2748            
2749 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
2750 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
2751              
2752             /* calculate error */
2753 0 0         if ( error_sdev ) {
2754              
2755             /* weighted standard deviation */
2756 0 0         if ( have_error ) {
2757            
2758             /* parallel algorithm (as we're adding bins together) for
2759             (possibly) weighted standard deviation; see
2760             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2761             */
2762 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
2763 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
2764 0           double mean_b = bmean;
2765 0           double weight_a = _weight;
2766 0           double weight_b = bweight;
2767 0           double weight_x = bweight += weight_a;
2768 0           double delta = mean_b - mean_a;
2769 0           bmean = mean_a + delta * weight_b / weight_x;
2770 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
2771 0           + delta * delta * weight_a * weight_b / weight_x;
2772              
2773 0           bad_error = nin <= 1;
2774 0           berror = bad_error
2775             ? DBL_MAX
2776 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
2777            
2778             }
2779              
2780             else {
2781            
2782             /* parallel algorithm (as we're adding bins together) for
2783             (possibly) weighted standard deviation; see
2784             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2785             */
2786 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
2787 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
2788 0           double mean_b = bmean;
2789 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
2790 0           double weight_b = nin_last;
2791 0           double weight_x = nin;
2792 0           double delta = mean_b - mean_a;
2793 0           bmean = mean_a + delta * weight_b / weight_x;
2794 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
2795 0           + delta * delta * weight_a * weight_b / weight_x;
2796              
2797 0           bad_error = nin <= 1;
2798 0           berror = bad_error
2799             ? DBL_MAX
2800 0 0         : sqrt( bm2 * 1 / (nin - 1) );
2801            
2802             }
2803             }
2804              
2805 0 0         else if ( error_rss ) {
2806            
2807             /* parallel algorithm (as we're adding bins together) for
2808             mean; see
2809             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2810             */
2811 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
2812 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
2813 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
2814 0           double mean_b = bmean;
2815 0           double weight_a = _weight;
2816 0           double weight_b = bweight;
2817 0           double weight_x = bweight += weight_a;
2818 0           double delta = mean_b - mean_a;
2819 0           bmean = mean_a + delta * weight_b / weight_x;
2820              
2821 0           berror2 += _error2;
2822 0           berror = sqrt( berror2 );
2823            
2824             }
2825              
2826 0 0         else if ( error_poisson ) {
2827 0           berror = sqrt( nin );
2828 0           bmean = bsignal / nin;
2829             }
2830              
2831             else {
2832 0           croak( "internal error" );
2833             }
2834            
2835              
2836 0           bsnr = bsignal / berror;
2837 0           snr_ok = bsnr >= min_snr;
2838              
2839            
2840 0           rc |= \
2841 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
2842 0           | \
2843 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
2844 0           | \
2845             ( nin >= min_nelem \
2846 0 0         && bwidth >= min_width \
2847 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
2848             ; \
2849             ;
2850              
2851 0 0         if (rc)
2852 0           break;
2853             }
2854              
2855             /* fix up index for events initially stuck in folded bins */
2856 0           PDL_Indx curind1 = curind+1;
2857             PDL_Indx ni;
2858              
2859 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
2860 0 0         ni < __privtrans->ind_sizes[0] ;
2861 0           ni++ ) {
2862             #ifdef PDL_BAD_CODE
2863 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
2864             #endif /* PDL_BAD_CODE */
2865 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
2866             }
2867 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2868 0           rc |= BIN_RC_FOLDED;
2869             }
2870              
2871            
2872 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
2873 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
2874 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
2875 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
2876            
2877 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
2878 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
2879            
2880 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
2881 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
2882 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
2883 0 0         if ( error_sdev ) {
2884 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
2885 0 0         if ( have_error ) {
2886 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2887 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
2888 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
2889             }
2890             else {
2891 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
2892             }
2893             }
2894 0 0         else if ( error_rss ) {
2895 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
2896 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
2897             }
2898            
2899             }
2900             /* adjust for possibility of last bin being empty */
2901 0           (nbins_datap)[0] = curind + ( nin != 0 );
2902 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
2903             }
2904 0           } break;
2905 0           case PDL_IND: {
2906 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
2907             {
2908              
2909              
2910              
2911              
2912 0           int flags = __params->optflags;
2913              
2914 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
2915 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
2916 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
2917 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
2918 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
2919 0           int fold_last_bin = flags & BIN_ARG_FOLD;
2920 0           int set_bad = flags & BIN_ARG_SET_BAD;
2921              
2922 0           PDL_Indx min_nelem = __params->min_nelem;
2923 0           PDL_Indx max_nelem = __params->max_nelem;
2924 0           double max_width = __params->max_width;
2925 0           double min_width = __params->min_width;
2926 0           double min_snr = __params->min_snr;
2927              
2928             /* simplify the logic below by setting bounds values to their most
2929             permissive extremes if they aren't needed. */
2930              
2931 0 0         if ( max_width == 0 )
2932 0           max_width = DBL_MAX;
2933              
2934 0 0         if ( max_nelem == 0 )
2935 0           max_nelem = LONG_MAX;
2936              
2937 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
2938              
2939 0           PDL_Indx curind = 0; /* index of current bin */
2940              
2941             /* Declare bin counters */
2942 0           int rc = 0; /* status */
2943 0           double bsignal = 0; /* sum of signal */
2944 0           double bmean = 0; /* mean of (weighted) signal */
2945 0           double bwidth = 0; /* width (if applicable) */
2946 0           double bsnr = 0; /* SNR */
2947 0           double berror2 = 0; /* sum of error^2 */
2948 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
2949 0           double bm2 = 0; /* unnormalized variance */
2950 0           double bsignal2 = 0; /* sum of signal^2 */
2951 0           double bweight = 0; /* sum of 1/error*2 */
2952 0           double bweight_sig = 0; /* sum of weight * signal */
2953 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
2954 0           int bad_error = 0; /* if error2 is not good */
2955 0           int lastrc = 0; /* carryover status from previous loop */
2956 0           PDL_Indx nin = 0; /* number of elements in the current bin */
2957              
2958 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
2959              
2960 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
2961             double error;
2962             double error2;
2963             double width;
2964              
2965 0           int snr_ok = 0;
2966              
2967 0 0         if ( have_error ) {
2968 0           error = (error_datap)[0+(__inc_error_n*(n))];
2969 0           error2 = error * error;
2970             }
2971              
2972 0 0         if ( have_width )
2973 0           width = (width_datap)[0+(__inc_width_n*(n))];
2974              
2975             #ifdef PDL_BAD_CODE
2976 0 0         if ( PDL_ISBAD2(signal,signal_badval,N,signal_badval_isnan)
2977 0 0         ||
    0          
2978 0 0         have_error && PDL_ISBAD2(error,error_badval,N,error_badval_isnan) ) {
    0          
2979 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
2980 0           continue;
2981             }
2982             #endif /* PDL_BAD_CODE */
2983 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
2984              
2985            
2986 0           bsignal += signal;
2987 0           nin += 1;
2988              
2989             /* calculate error */
2990 0 0         if ( error_sdev ) {
2991              
2992             /* weighted standard deviation */
2993 0 0         if ( have_error ) {
2994            
2995             /* incremental algorithm for possibly weighted standard deviation; see
2996             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2997             */
2998 0           double _weight = 1 / ( error * error );
2999 0           double _sum_weight = bweight += _weight;
3000 0           double delta = signal - bmean;
3001 0           bmean += delta * _weight / _sum_weight;
3002 0           bm2 += _weight * delta * ( signal - bmean );
3003              
3004 0           bad_error = nin <= 1;
3005 0           berror = bad_error
3006             ? DBL_MAX
3007 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
3008            
3009             }
3010              
3011             else {
3012            
3013             /* incremental algorithm for possibly weighted standard deviation; see
3014             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3015             */
3016 0           double _weight = 1;
3017 0           double _sum_weight = nin;
3018 0           double delta = signal - bmean;
3019 0           bmean += delta * _weight / _sum_weight;
3020 0           bm2 += _weight * delta * ( signal - bmean );
3021              
3022 0           bad_error = nin <= 1;
3023 0           berror = bad_error
3024             ? DBL_MAX
3025 0 0         : sqrt( bm2 * 1 / (nin - 1) );
3026            
3027             }
3028             }
3029              
3030 0 0         else if ( error_rss ) {
3031            
3032             /* incremental algorithm for mean; see
3033             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3034             */
3035 0           double _error2 = error2;
3036 0           double _weight = 1 / ( error * error );
3037 0           double delta = signal - bmean;
3038              
3039 0           bweight += _weight;
3040 0           bmean += delta * _weight / bweight;
3041 0           berror2 += _error2;
3042 0           berror = sqrt( berror2 );
3043            
3044             }
3045              
3046 0 0         else if ( error_poisson ) {
3047 0           berror = sqrt( nin );
3048 0           bmean = bsignal / nin;
3049             }
3050              
3051             else {
3052 0           croak( "internal error" );
3053             }
3054            
3055              
3056 0           bsnr = bsignal / berror;
3057 0           snr_ok = bsnr >= min_snr;
3058              
3059 0 0         if ( have_width )
3060 0           bwidth += width;
3061              
3062 0 0         if ( nin == 1 )
3063 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
3064              
3065            
3066 0           rc |= \
3067 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
3068 0           | \
3069 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
3070 0           | \
3071             ( nin >= min_nelem \
3072 0 0         && bwidth >= min_width \
3073 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
3074             ; \
3075             ;
3076              
3077 0 0         if ( rc ) {
3078 0           rc |= lastrc;
3079              
3080            
3081 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
3082 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
3083 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
3084 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
3085            
3086 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
3087 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
3088            
3089 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
3090 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
3091 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
3092 0 0         if ( error_sdev ) {
3093 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
3094 0 0         if ( have_error ) {
3095 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3096 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
3097 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
3098             }
3099             else {
3100 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
3101             }
3102             }
3103 0 0         else if ( error_rss ) {
3104 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
3105 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3106             }
3107            
3108              
3109 0           curind++;
3110              
3111             /* Reset bin counters */
3112 0           rc = 0;
3113 0           bsignal = 0;
3114 0           bmean = 0;
3115 0           bwidth = 0;
3116 0           bsnr = 0;
3117 0           berror2 = 0;
3118 0           berror = 0;
3119 0           bm2 = 0;
3120 0           bsignal2 = 0;
3121 0           bweight = 0;
3122 0           bweight_sig = 0;
3123 0           bweight_sig2 = 0;
3124 0           bad_error = 0;
3125 0           lastrc = 0;
3126 0           nin = 0;
3127              
3128             }
3129              
3130 0 0         else if ( snr_ok ) {
3131 0           lastrc = BIN_RC_GTMINSN;
3132             }
3133              
3134             else {
3135 0           lastrc = 0;
3136             }
3137             }} /* Close n */
3138              
3139              
3140             /* record last bin if it's not empty */
3141 0 0         if ( nin ) {
3142              
3143             /* needed for SET_RESULTS */
3144 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
3145              
3146 0           rc = 0;
3147 0           bad_error = 0;
3148              
3149             /* a non empty bin means that we didn't meet constraints. fold it into
3150             the previous bin if requested & possible. sometimes that will
3151             actually lower the S/N of the previous bin; keep going until
3152             we can't fold anymore or we get the proper S/N
3153             */
3154 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
3155              
3156              
3157 0 0         while ( --curind > 0 ) {
3158             double tmp;
3159 0           int snr_ok = 0;
3160 0           PDL_Indx nin_last = nin;
3161              
3162            
3163 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
3164 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
3165              
3166             /* calculate error */
3167 0 0         if ( error_sdev ) {
3168              
3169             /* weighted standard deviation */
3170 0 0         if ( have_error ) {
3171            
3172             /* parallel algorithm (as we're adding bins together) for
3173             (possibly) weighted standard deviation; see
3174             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3175             */
3176 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
3177 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
3178 0           double mean_b = bmean;
3179 0           double weight_a = _weight;
3180 0           double weight_b = bweight;
3181 0           double weight_x = bweight += weight_a;
3182 0           double delta = mean_b - mean_a;
3183 0           bmean = mean_a + delta * weight_b / weight_x;
3184 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
3185 0           + delta * delta * weight_a * weight_b / weight_x;
3186              
3187 0           bad_error = nin <= 1;
3188 0           berror = bad_error
3189             ? DBL_MAX
3190 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
3191            
3192             }
3193              
3194             else {
3195            
3196             /* parallel algorithm (as we're adding bins together) for
3197             (possibly) weighted standard deviation; see
3198             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3199             */
3200 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
3201 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
3202 0           double mean_b = bmean;
3203 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
3204 0           double weight_b = nin_last;
3205 0           double weight_x = nin;
3206 0           double delta = mean_b - mean_a;
3207 0           bmean = mean_a + delta * weight_b / weight_x;
3208 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
3209 0           + delta * delta * weight_a * weight_b / weight_x;
3210              
3211 0           bad_error = nin <= 1;
3212 0           berror = bad_error
3213             ? DBL_MAX
3214 0 0         : sqrt( bm2 * 1 / (nin - 1) );
3215            
3216             }
3217             }
3218              
3219 0 0         else if ( error_rss ) {
3220            
3221             /* parallel algorithm (as we're adding bins together) for
3222             mean; see
3223             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3224             */
3225 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
3226 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
3227 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
3228 0           double mean_b = bmean;
3229 0           double weight_a = _weight;
3230 0           double weight_b = bweight;
3231 0           double weight_x = bweight += weight_a;
3232 0           double delta = mean_b - mean_a;
3233 0           bmean = mean_a + delta * weight_b / weight_x;
3234              
3235 0           berror2 += _error2;
3236 0           berror = sqrt( berror2 );
3237            
3238             }
3239              
3240 0 0         else if ( error_poisson ) {
3241 0           berror = sqrt( nin );
3242 0           bmean = bsignal / nin;
3243             }
3244              
3245             else {
3246 0           croak( "internal error" );
3247             }
3248            
3249              
3250 0           bsnr = bsignal / berror;
3251 0           snr_ok = bsnr >= min_snr;
3252              
3253            
3254 0           rc |= \
3255 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
3256 0           | \
3257 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
3258 0           | \
3259             ( nin >= min_nelem \
3260 0 0         && bwidth >= min_width \
3261 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
3262             ; \
3263             ;
3264              
3265 0 0         if (rc)
3266 0           break;
3267             }
3268              
3269             /* fix up index for events initially stuck in folded bins */
3270 0           PDL_Indx curind1 = curind+1;
3271             PDL_Indx ni;
3272              
3273 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
3274 0 0         ni < __privtrans->ind_sizes[0] ;
3275 0           ni++ ) {
3276             #ifdef PDL_BAD_CODE
3277 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
3278             #endif /* PDL_BAD_CODE */
3279 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
3280             }
3281 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
3282 0           rc |= BIN_RC_FOLDED;
3283             }
3284              
3285            
3286 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
3287 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
3288 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
3289 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
3290            
3291 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
3292 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
3293            
3294 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
3295 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
3296 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
3297 0 0         if ( error_sdev ) {
3298 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
3299 0 0         if ( have_error ) {
3300 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3301 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
3302 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
3303             }
3304             else {
3305 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
3306             }
3307             }
3308 0 0         else if ( error_rss ) {
3309 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
3310 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3311             }
3312            
3313             }
3314             /* adjust for possibility of last bin being empty */
3315 0           (nbins_datap)[0] = curind + ( nin != 0 );
3316 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
3317             }
3318 0           } break;
3319 0           case PDL_ULL: {
3320 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_ULongLong,P,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
3321             {
3322              
3323              
3324              
3325              
3326 0           int flags = __params->optflags;
3327              
3328 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
3329 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
3330 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
3331 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
3332 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
3333 0           int fold_last_bin = flags & BIN_ARG_FOLD;
3334 0           int set_bad = flags & BIN_ARG_SET_BAD;
3335              
3336 0           PDL_Indx min_nelem = __params->min_nelem;
3337 0           PDL_Indx max_nelem = __params->max_nelem;
3338 0           double max_width = __params->max_width;
3339 0           double min_width = __params->min_width;
3340 0           double min_snr = __params->min_snr;
3341              
3342             /* simplify the logic below by setting bounds values to their most
3343             permissive extremes if they aren't needed. */
3344              
3345 0 0         if ( max_width == 0 )
3346 0           max_width = DBL_MAX;
3347              
3348 0 0         if ( max_nelem == 0 )
3349 0           max_nelem = LONG_MAX;
3350              
3351 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
3352              
3353 0           PDL_Indx curind = 0; /* index of current bin */
3354              
3355             /* Declare bin counters */
3356 0           int rc = 0; /* status */
3357 0           double bsignal = 0; /* sum of signal */
3358 0           double bmean = 0; /* mean of (weighted) signal */
3359 0           double bwidth = 0; /* width (if applicable) */
3360 0           double bsnr = 0; /* SNR */
3361 0           double berror2 = 0; /* sum of error^2 */
3362 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
3363 0           double bm2 = 0; /* unnormalized variance */
3364 0           double bsignal2 = 0; /* sum of signal^2 */
3365 0           double bweight = 0; /* sum of 1/error*2 */
3366 0           double bweight_sig = 0; /* sum of weight * signal */
3367 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
3368 0           int bad_error = 0; /* if error2 is not good */
3369 0           int lastrc = 0; /* carryover status from previous loop */
3370 0           PDL_Indx nin = 0; /* number of elements in the current bin */
3371              
3372 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
3373              
3374 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
3375             double error;
3376             double error2;
3377             double width;
3378              
3379 0           int snr_ok = 0;
3380              
3381 0 0         if ( have_error ) {
3382 0           error = (error_datap)[0+(__inc_error_n*(n))];
3383 0           error2 = error * error;
3384             }
3385              
3386 0 0         if ( have_width )
3387 0           width = (width_datap)[0+(__inc_width_n*(n))];
3388              
3389             #ifdef PDL_BAD_CODE
3390 0 0         if ( PDL_ISBAD2(signal,signal_badval,P,signal_badval_isnan)
3391 0 0         ||
    0          
3392 0 0         have_error && PDL_ISBAD2(error,error_badval,P,error_badval_isnan) ) {
    0          
3393 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
3394 0           continue;
3395             }
3396             #endif /* PDL_BAD_CODE */
3397 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
3398              
3399            
3400 0           bsignal += signal;
3401 0           nin += 1;
3402              
3403             /* calculate error */
3404 0 0         if ( error_sdev ) {
3405              
3406             /* weighted standard deviation */
3407 0 0         if ( have_error ) {
3408            
3409             /* incremental algorithm for possibly weighted standard deviation; see
3410             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3411             */
3412 0           double _weight = 1 / ( error * error );
3413 0           double _sum_weight = bweight += _weight;
3414 0           double delta = signal - bmean;
3415 0           bmean += delta * _weight / _sum_weight;
3416 0           bm2 += _weight * delta * ( signal - bmean );
3417              
3418 0           bad_error = nin <= 1;
3419 0           berror = bad_error
3420             ? DBL_MAX
3421 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
3422            
3423             }
3424              
3425             else {
3426            
3427             /* incremental algorithm for possibly weighted standard deviation; see
3428             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3429             */
3430 0           double _weight = 1;
3431 0           double _sum_weight = nin;
3432 0           double delta = signal - bmean;
3433 0           bmean += delta * _weight / _sum_weight;
3434 0           bm2 += _weight * delta * ( signal - bmean );
3435              
3436 0           bad_error = nin <= 1;
3437 0           berror = bad_error
3438             ? DBL_MAX
3439 0 0         : sqrt( bm2 * 1 / (nin - 1) );
3440            
3441             }
3442             }
3443              
3444 0 0         else if ( error_rss ) {
3445            
3446             /* incremental algorithm for mean; see
3447             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3448             */
3449 0           double _error2 = error2;
3450 0           double _weight = 1 / ( error * error );
3451 0           double delta = signal - bmean;
3452              
3453 0           bweight += _weight;
3454 0           bmean += delta * _weight / bweight;
3455 0           berror2 += _error2;
3456 0           berror = sqrt( berror2 );
3457            
3458             }
3459              
3460 0 0         else if ( error_poisson ) {
3461 0           berror = sqrt( nin );
3462 0           bmean = bsignal / nin;
3463             }
3464              
3465             else {
3466 0           croak( "internal error" );
3467             }
3468            
3469              
3470 0           bsnr = bsignal / berror;
3471 0           snr_ok = bsnr >= min_snr;
3472              
3473 0 0         if ( have_width )
3474 0           bwidth += width;
3475              
3476 0 0         if ( nin == 1 )
3477 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
3478              
3479            
3480 0           rc |= \
3481 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
3482 0           | \
3483 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
3484 0           | \
3485             ( nin >= min_nelem \
3486 0 0         && bwidth >= min_width \
3487 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
3488             ; \
3489             ;
3490              
3491 0 0         if ( rc ) {
3492 0           rc |= lastrc;
3493              
3494            
3495 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
3496 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
3497 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
3498 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
3499            
3500 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
3501 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
3502            
3503 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
3504 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
3505 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
3506 0 0         if ( error_sdev ) {
3507 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
3508 0 0         if ( have_error ) {
3509 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3510 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
3511 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
3512             }
3513             else {
3514 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
3515             }
3516             }
3517 0 0         else if ( error_rss ) {
3518 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
3519 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3520             }
3521            
3522              
3523 0           curind++;
3524              
3525             /* Reset bin counters */
3526 0           rc = 0;
3527 0           bsignal = 0;
3528 0           bmean = 0;
3529 0           bwidth = 0;
3530 0           bsnr = 0;
3531 0           berror2 = 0;
3532 0           berror = 0;
3533 0           bm2 = 0;
3534 0           bsignal2 = 0;
3535 0           bweight = 0;
3536 0           bweight_sig = 0;
3537 0           bweight_sig2 = 0;
3538 0           bad_error = 0;
3539 0           lastrc = 0;
3540 0           nin = 0;
3541              
3542             }
3543              
3544 0 0         else if ( snr_ok ) {
3545 0           lastrc = BIN_RC_GTMINSN;
3546             }
3547              
3548             else {
3549 0           lastrc = 0;
3550             }
3551             }} /* Close n */
3552              
3553              
3554             /* record last bin if it's not empty */
3555 0 0         if ( nin ) {
3556              
3557             /* needed for SET_RESULTS */
3558 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
3559              
3560 0           rc = 0;
3561 0           bad_error = 0;
3562              
3563             /* a non empty bin means that we didn't meet constraints. fold it into
3564             the previous bin if requested & possible. sometimes that will
3565             actually lower the S/N of the previous bin; keep going until
3566             we can't fold anymore or we get the proper S/N
3567             */
3568 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
3569              
3570              
3571 0 0         while ( --curind > 0 ) {
3572             double tmp;
3573 0           int snr_ok = 0;
3574 0           PDL_Indx nin_last = nin;
3575              
3576            
3577 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
3578 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
3579              
3580             /* calculate error */
3581 0 0         if ( error_sdev ) {
3582              
3583             /* weighted standard deviation */
3584 0 0         if ( have_error ) {
3585            
3586             /* parallel algorithm (as we're adding bins together) for
3587             (possibly) weighted standard deviation; see
3588             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3589             */
3590 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
3591 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
3592 0           double mean_b = bmean;
3593 0           double weight_a = _weight;
3594 0           double weight_b = bweight;
3595 0           double weight_x = bweight += weight_a;
3596 0           double delta = mean_b - mean_a;
3597 0           bmean = mean_a + delta * weight_b / weight_x;
3598 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
3599 0           + delta * delta * weight_a * weight_b / weight_x;
3600              
3601 0           bad_error = nin <= 1;
3602 0           berror = bad_error
3603             ? DBL_MAX
3604 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
3605            
3606             }
3607              
3608             else {
3609            
3610             /* parallel algorithm (as we're adding bins together) for
3611             (possibly) weighted standard deviation; see
3612             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3613             */
3614 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
3615 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
3616 0           double mean_b = bmean;
3617 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
3618 0           double weight_b = nin_last;
3619 0           double weight_x = nin;
3620 0           double delta = mean_b - mean_a;
3621 0           bmean = mean_a + delta * weight_b / weight_x;
3622 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
3623 0           + delta * delta * weight_a * weight_b / weight_x;
3624              
3625 0           bad_error = nin <= 1;
3626 0           berror = bad_error
3627             ? DBL_MAX
3628 0 0         : sqrt( bm2 * 1 / (nin - 1) );
3629            
3630             }
3631             }
3632              
3633 0 0         else if ( error_rss ) {
3634            
3635             /* parallel algorithm (as we're adding bins together) for
3636             mean; see
3637             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3638             */
3639 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
3640 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
3641 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
3642 0           double mean_b = bmean;
3643 0           double weight_a = _weight;
3644 0           double weight_b = bweight;
3645 0           double weight_x = bweight += weight_a;
3646 0           double delta = mean_b - mean_a;
3647 0           bmean = mean_a + delta * weight_b / weight_x;
3648              
3649 0           berror2 += _error2;
3650 0           berror = sqrt( berror2 );
3651            
3652             }
3653              
3654 0 0         else if ( error_poisson ) {
3655 0           berror = sqrt( nin );
3656 0           bmean = bsignal / nin;
3657             }
3658              
3659             else {
3660 0           croak( "internal error" );
3661             }
3662            
3663              
3664 0           bsnr = bsignal / berror;
3665 0           snr_ok = bsnr >= min_snr;
3666              
3667            
3668 0           rc |= \
3669 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
3670 0           | \
3671 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
3672 0           | \
3673             ( nin >= min_nelem \
3674 0 0         && bwidth >= min_width \
3675 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
3676             ; \
3677             ;
3678              
3679 0 0         if (rc)
3680 0           break;
3681             }
3682              
3683             /* fix up index for events initially stuck in folded bins */
3684 0           PDL_Indx curind1 = curind+1;
3685             PDL_Indx ni;
3686              
3687 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
3688 0 0         ni < __privtrans->ind_sizes[0] ;
3689 0           ni++ ) {
3690             #ifdef PDL_BAD_CODE
3691 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
3692             #endif /* PDL_BAD_CODE */
3693 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
3694             }
3695 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
3696 0           rc |= BIN_RC_FOLDED;
3697             }
3698              
3699            
3700 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
3701 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
3702 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
3703 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
3704            
3705 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
3706 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
3707            
3708 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
3709 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
3710 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
3711 0 0         if ( error_sdev ) {
3712 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
3713 0 0         if ( have_error ) {
3714 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3715 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
3716 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
3717             }
3718             else {
3719 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
3720             }
3721             }
3722 0 0         else if ( error_rss ) {
3723 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
3724 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3725             }
3726            
3727             }
3728             /* adjust for possibility of last bin being empty */
3729 0           (nbins_datap)[0] = curind + ( nin != 0 );
3730 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
3731             }
3732 0           } break;
3733 0           case PDL_LL: {
3734 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_LongLong,Q,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
3735             {
3736              
3737              
3738              
3739              
3740 0           int flags = __params->optflags;
3741              
3742 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
3743 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
3744 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
3745 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
3746 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
3747 0           int fold_last_bin = flags & BIN_ARG_FOLD;
3748 0           int set_bad = flags & BIN_ARG_SET_BAD;
3749              
3750 0           PDL_Indx min_nelem = __params->min_nelem;
3751 0           PDL_Indx max_nelem = __params->max_nelem;
3752 0           double max_width = __params->max_width;
3753 0           double min_width = __params->min_width;
3754 0           double min_snr = __params->min_snr;
3755              
3756             /* simplify the logic below by setting bounds values to their most
3757             permissive extremes if they aren't needed. */
3758              
3759 0 0         if ( max_width == 0 )
3760 0           max_width = DBL_MAX;
3761              
3762 0 0         if ( max_nelem == 0 )
3763 0           max_nelem = LONG_MAX;
3764              
3765 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
3766              
3767 0           PDL_Indx curind = 0; /* index of current bin */
3768              
3769             /* Declare bin counters */
3770 0           int rc = 0; /* status */
3771 0           double bsignal = 0; /* sum of signal */
3772 0           double bmean = 0; /* mean of (weighted) signal */
3773 0           double bwidth = 0; /* width (if applicable) */
3774 0           double bsnr = 0; /* SNR */
3775 0           double berror2 = 0; /* sum of error^2 */
3776 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
3777 0           double bm2 = 0; /* unnormalized variance */
3778 0           double bsignal2 = 0; /* sum of signal^2 */
3779 0           double bweight = 0; /* sum of 1/error*2 */
3780 0           double bweight_sig = 0; /* sum of weight * signal */
3781 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
3782 0           int bad_error = 0; /* if error2 is not good */
3783 0           int lastrc = 0; /* carryover status from previous loop */
3784 0           PDL_Indx nin = 0; /* number of elements in the current bin */
3785              
3786 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
3787              
3788 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
3789             double error;
3790             double error2;
3791             double width;
3792              
3793 0           int snr_ok = 0;
3794              
3795 0 0         if ( have_error ) {
3796 0           error = (error_datap)[0+(__inc_error_n*(n))];
3797 0           error2 = error * error;
3798             }
3799              
3800 0 0         if ( have_width )
3801 0           width = (width_datap)[0+(__inc_width_n*(n))];
3802              
3803             #ifdef PDL_BAD_CODE
3804 0 0         if ( PDL_ISBAD2(signal,signal_badval,Q,signal_badval_isnan)
3805 0 0         ||
    0          
3806 0 0         have_error && PDL_ISBAD2(error,error_badval,Q,error_badval_isnan) ) {
    0          
3807 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
3808 0           continue;
3809             }
3810             #endif /* PDL_BAD_CODE */
3811 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
3812              
3813            
3814 0           bsignal += signal;
3815 0           nin += 1;
3816              
3817             /* calculate error */
3818 0 0         if ( error_sdev ) {
3819              
3820             /* weighted standard deviation */
3821 0 0         if ( have_error ) {
3822            
3823             /* incremental algorithm for possibly weighted standard deviation; see
3824             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3825             */
3826 0           double _weight = 1 / ( error * error );
3827 0           double _sum_weight = bweight += _weight;
3828 0           double delta = signal - bmean;
3829 0           bmean += delta * _weight / _sum_weight;
3830 0           bm2 += _weight * delta * ( signal - bmean );
3831              
3832 0           bad_error = nin <= 1;
3833 0           berror = bad_error
3834             ? DBL_MAX
3835 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
3836            
3837             }
3838              
3839             else {
3840            
3841             /* incremental algorithm for possibly weighted standard deviation; see
3842             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3843             */
3844 0           double _weight = 1;
3845 0           double _sum_weight = nin;
3846 0           double delta = signal - bmean;
3847 0           bmean += delta * _weight / _sum_weight;
3848 0           bm2 += _weight * delta * ( signal - bmean );
3849              
3850 0           bad_error = nin <= 1;
3851 0           berror = bad_error
3852             ? DBL_MAX
3853 0 0         : sqrt( bm2 * 1 / (nin - 1) );
3854            
3855             }
3856             }
3857              
3858 0 0         else if ( error_rss ) {
3859            
3860             /* incremental algorithm for mean; see
3861             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3862             */
3863 0           double _error2 = error2;
3864 0           double _weight = 1 / ( error * error );
3865 0           double delta = signal - bmean;
3866              
3867 0           bweight += _weight;
3868 0           bmean += delta * _weight / bweight;
3869 0           berror2 += _error2;
3870 0           berror = sqrt( berror2 );
3871            
3872             }
3873              
3874 0 0         else if ( error_poisson ) {
3875 0           berror = sqrt( nin );
3876 0           bmean = bsignal / nin;
3877             }
3878              
3879             else {
3880 0           croak( "internal error" );
3881             }
3882            
3883              
3884 0           bsnr = bsignal / berror;
3885 0           snr_ok = bsnr >= min_snr;
3886              
3887 0 0         if ( have_width )
3888 0           bwidth += width;
3889              
3890 0 0         if ( nin == 1 )
3891 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
3892              
3893            
3894 0           rc |= \
3895 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
3896 0           | \
3897 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
3898 0           | \
3899             ( nin >= min_nelem \
3900 0 0         && bwidth >= min_width \
3901 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
3902             ; \
3903             ;
3904              
3905 0 0         if ( rc ) {
3906 0           rc |= lastrc;
3907              
3908            
3909 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
3910 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
3911 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
3912 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
3913            
3914 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
3915 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
3916            
3917 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
3918 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
3919 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
3920 0 0         if ( error_sdev ) {
3921 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
3922 0 0         if ( have_error ) {
3923 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3924 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
3925 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
3926             }
3927             else {
3928 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
3929             }
3930             }
3931 0 0         else if ( error_rss ) {
3932 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
3933 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
3934             }
3935            
3936              
3937 0           curind++;
3938              
3939             /* Reset bin counters */
3940 0           rc = 0;
3941 0           bsignal = 0;
3942 0           bmean = 0;
3943 0           bwidth = 0;
3944 0           bsnr = 0;
3945 0           berror2 = 0;
3946 0           berror = 0;
3947 0           bm2 = 0;
3948 0           bsignal2 = 0;
3949 0           bweight = 0;
3950 0           bweight_sig = 0;
3951 0           bweight_sig2 = 0;
3952 0           bad_error = 0;
3953 0           lastrc = 0;
3954 0           nin = 0;
3955              
3956             }
3957              
3958 0 0         else if ( snr_ok ) {
3959 0           lastrc = BIN_RC_GTMINSN;
3960             }
3961              
3962             else {
3963 0           lastrc = 0;
3964             }
3965             }} /* Close n */
3966              
3967              
3968             /* record last bin if it's not empty */
3969 0 0         if ( nin ) {
3970              
3971             /* needed for SET_RESULTS */
3972 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
3973              
3974 0           rc = 0;
3975 0           bad_error = 0;
3976              
3977             /* a non empty bin means that we didn't meet constraints. fold it into
3978             the previous bin if requested & possible. sometimes that will
3979             actually lower the S/N of the previous bin; keep going until
3980             we can't fold anymore or we get the proper S/N
3981             */
3982 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
3983              
3984              
3985 0 0         while ( --curind > 0 ) {
3986             double tmp;
3987 0           int snr_ok = 0;
3988 0           PDL_Indx nin_last = nin;
3989              
3990            
3991 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
3992 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
3993              
3994             /* calculate error */
3995 0 0         if ( error_sdev ) {
3996              
3997             /* weighted standard deviation */
3998 0 0         if ( have_error ) {
3999            
4000             /* parallel algorithm (as we're adding bins together) for
4001             (possibly) weighted standard deviation; see
4002             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4003             */
4004 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4005 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4006 0           double mean_b = bmean;
4007 0           double weight_a = _weight;
4008 0           double weight_b = bweight;
4009 0           double weight_x = bweight += weight_a;
4010 0           double delta = mean_b - mean_a;
4011 0           bmean = mean_a + delta * weight_b / weight_x;
4012 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
4013 0           + delta * delta * weight_a * weight_b / weight_x;
4014              
4015 0           bad_error = nin <= 1;
4016 0           berror = bad_error
4017             ? DBL_MAX
4018 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
4019            
4020             }
4021              
4022             else {
4023            
4024             /* parallel algorithm (as we're adding bins together) for
4025             (possibly) weighted standard deviation; see
4026             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4027             */
4028 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4029 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4030 0           double mean_b = bmean;
4031 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
4032 0           double weight_b = nin_last;
4033 0           double weight_x = nin;
4034 0           double delta = mean_b - mean_a;
4035 0           bmean = mean_a + delta * weight_b / weight_x;
4036 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
4037 0           + delta * delta * weight_a * weight_b / weight_x;
4038              
4039 0           bad_error = nin <= 1;
4040 0           berror = bad_error
4041             ? DBL_MAX
4042 0 0         : sqrt( bm2 * 1 / (nin - 1) );
4043            
4044             }
4045             }
4046              
4047 0 0         else if ( error_rss ) {
4048            
4049             /* parallel algorithm (as we're adding bins together) for
4050             mean; see
4051             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4052             */
4053 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
4054 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4055 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4056 0           double mean_b = bmean;
4057 0           double weight_a = _weight;
4058 0           double weight_b = bweight;
4059 0           double weight_x = bweight += weight_a;
4060 0           double delta = mean_b - mean_a;
4061 0           bmean = mean_a + delta * weight_b / weight_x;
4062              
4063 0           berror2 += _error2;
4064 0           berror = sqrt( berror2 );
4065            
4066             }
4067              
4068 0 0         else if ( error_poisson ) {
4069 0           berror = sqrt( nin );
4070 0           bmean = bsignal / nin;
4071             }
4072              
4073             else {
4074 0           croak( "internal error" );
4075             }
4076            
4077              
4078 0           bsnr = bsignal / berror;
4079 0           snr_ok = bsnr >= min_snr;
4080              
4081            
4082 0           rc |= \
4083 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
4084 0           | \
4085 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
4086 0           | \
4087             ( nin >= min_nelem \
4088 0 0         && bwidth >= min_width \
4089 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
4090             ; \
4091             ;
4092              
4093 0 0         if (rc)
4094 0           break;
4095             }
4096              
4097             /* fix up index for events initially stuck in folded bins */
4098 0           PDL_Indx curind1 = curind+1;
4099             PDL_Indx ni;
4100              
4101 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
4102 0 0         ni < __privtrans->ind_sizes[0] ;
4103 0           ni++ ) {
4104             #ifdef PDL_BAD_CODE
4105 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
4106             #endif /* PDL_BAD_CODE */
4107 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
4108             }
4109 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4110 0           rc |= BIN_RC_FOLDED;
4111             }
4112              
4113            
4114 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
4115 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
4116 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
4117 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
4118            
4119 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
4120 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
4121            
4122 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
4123 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
4124 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4125 0 0         if ( error_sdev ) {
4126 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
4127 0 0         if ( have_error ) {
4128 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4129 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
4130 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
4131             }
4132             else {
4133 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
4134             }
4135             }
4136 0 0         else if ( error_rss ) {
4137 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
4138 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4139             }
4140            
4141             }
4142             /* adjust for possibility of last bin being empty */
4143 0           (nbins_datap)[0] = curind + ( nin != 0 );
4144 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
4145             }
4146 0           } break;
4147 0           case PDL_F: {
4148 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Float,F,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
4149             {
4150              
4151              
4152              
4153              
4154 0           int flags = __params->optflags;
4155              
4156 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
4157 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
4158 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
4159 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
4160 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
4161 0           int fold_last_bin = flags & BIN_ARG_FOLD;
4162 0           int set_bad = flags & BIN_ARG_SET_BAD;
4163              
4164 0           PDL_Indx min_nelem = __params->min_nelem;
4165 0           PDL_Indx max_nelem = __params->max_nelem;
4166 0           double max_width = __params->max_width;
4167 0           double min_width = __params->min_width;
4168 0           double min_snr = __params->min_snr;
4169              
4170             /* simplify the logic below by setting bounds values to their most
4171             permissive extremes if they aren't needed. */
4172              
4173 0 0         if ( max_width == 0 )
4174 0           max_width = DBL_MAX;
4175              
4176 0 0         if ( max_nelem == 0 )
4177 0           max_nelem = LONG_MAX;
4178              
4179 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
4180              
4181 0           PDL_Indx curind = 0; /* index of current bin */
4182              
4183             /* Declare bin counters */
4184 0           int rc = 0; /* status */
4185 0           double bsignal = 0; /* sum of signal */
4186 0           double bmean = 0; /* mean of (weighted) signal */
4187 0           double bwidth = 0; /* width (if applicable) */
4188 0           double bsnr = 0; /* SNR */
4189 0           double berror2 = 0; /* sum of error^2 */
4190 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
4191 0           double bm2 = 0; /* unnormalized variance */
4192 0           double bsignal2 = 0; /* sum of signal^2 */
4193 0           double bweight = 0; /* sum of 1/error*2 */
4194 0           double bweight_sig = 0; /* sum of weight * signal */
4195 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
4196 0           int bad_error = 0; /* if error2 is not good */
4197 0           int lastrc = 0; /* carryover status from previous loop */
4198 0           PDL_Indx nin = 0; /* number of elements in the current bin */
4199              
4200 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
4201              
4202 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
4203             double error;
4204             double error2;
4205             double width;
4206              
4207 0           int snr_ok = 0;
4208              
4209 0 0         if ( have_error ) {
4210 0           error = (error_datap)[0+(__inc_error_n*(n))];
4211 0           error2 = error * error;
4212             }
4213              
4214 0 0         if ( have_width )
4215 0           width = (width_datap)[0+(__inc_width_n*(n))];
4216              
4217             #ifdef PDL_BAD_CODE
4218 0 0         if ( PDL_ISBAD2(signal,signal_badval,F,signal_badval_isnan)
    0          
    0          
4219 0 0         ||
    0          
4220 0 0         have_error && PDL_ISBAD2(error,error_badval,F,error_badval_isnan) ) {
    0          
4221 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
4222 0           continue;
4223             }
4224             #endif /* PDL_BAD_CODE */
4225 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
4226              
4227            
4228 0           bsignal += signal;
4229 0           nin += 1;
4230              
4231             /* calculate error */
4232 0 0         if ( error_sdev ) {
4233              
4234             /* weighted standard deviation */
4235 0 0         if ( have_error ) {
4236            
4237             /* incremental algorithm for possibly weighted standard deviation; see
4238             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4239             */
4240 0           double _weight = 1 / ( error * error );
4241 0           double _sum_weight = bweight += _weight;
4242 0           double delta = signal - bmean;
4243 0           bmean += delta * _weight / _sum_weight;
4244 0           bm2 += _weight * delta * ( signal - bmean );
4245              
4246 0           bad_error = nin <= 1;
4247 0           berror = bad_error
4248             ? DBL_MAX
4249 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
4250            
4251             }
4252              
4253             else {
4254            
4255             /* incremental algorithm for possibly weighted standard deviation; see
4256             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4257             */
4258 0           double _weight = 1;
4259 0           double _sum_weight = nin;
4260 0           double delta = signal - bmean;
4261 0           bmean += delta * _weight / _sum_weight;
4262 0           bm2 += _weight * delta * ( signal - bmean );
4263              
4264 0           bad_error = nin <= 1;
4265 0           berror = bad_error
4266             ? DBL_MAX
4267 0 0         : sqrt( bm2 * 1 / (nin - 1) );
4268            
4269             }
4270             }
4271              
4272 0 0         else if ( error_rss ) {
4273            
4274             /* incremental algorithm for mean; see
4275             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4276             */
4277 0           double _error2 = error2;
4278 0           double _weight = 1 / ( error * error );
4279 0           double delta = signal - bmean;
4280              
4281 0           bweight += _weight;
4282 0           bmean += delta * _weight / bweight;
4283 0           berror2 += _error2;
4284 0           berror = sqrt( berror2 );
4285            
4286             }
4287              
4288 0 0         else if ( error_poisson ) {
4289 0           berror = sqrt( nin );
4290 0           bmean = bsignal / nin;
4291             }
4292              
4293             else {
4294 0           croak( "internal error" );
4295             }
4296            
4297              
4298 0           bsnr = bsignal / berror;
4299 0           snr_ok = bsnr >= min_snr;
4300              
4301 0 0         if ( have_width )
4302 0           bwidth += width;
4303              
4304 0 0         if ( nin == 1 )
4305 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
4306              
4307            
4308 0           rc |= \
4309 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
4310 0           | \
4311 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
4312 0           | \
4313             ( nin >= min_nelem \
4314 0 0         && bwidth >= min_width \
4315 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
4316             ; \
4317             ;
4318              
4319 0 0         if ( rc ) {
4320 0           rc |= lastrc;
4321              
4322            
4323 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
4324 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
4325 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
4326 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
4327            
4328 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
4329 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
4330            
4331 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
4332 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
4333 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4334 0 0         if ( error_sdev ) {
4335 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
4336 0 0         if ( have_error ) {
4337 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4338 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
4339 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
4340             }
4341             else {
4342 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
4343             }
4344             }
4345 0 0         else if ( error_rss ) {
4346 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
4347 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4348             }
4349            
4350              
4351 0           curind++;
4352              
4353             /* Reset bin counters */
4354 0           rc = 0;
4355 0           bsignal = 0;
4356 0           bmean = 0;
4357 0           bwidth = 0;
4358 0           bsnr = 0;
4359 0           berror2 = 0;
4360 0           berror = 0;
4361 0           bm2 = 0;
4362 0           bsignal2 = 0;
4363 0           bweight = 0;
4364 0           bweight_sig = 0;
4365 0           bweight_sig2 = 0;
4366 0           bad_error = 0;
4367 0           lastrc = 0;
4368 0           nin = 0;
4369              
4370             }
4371              
4372 0 0         else if ( snr_ok ) {
4373 0           lastrc = BIN_RC_GTMINSN;
4374             }
4375              
4376             else {
4377 0           lastrc = 0;
4378             }
4379             }} /* Close n */
4380              
4381              
4382             /* record last bin if it's not empty */
4383 0 0         if ( nin ) {
4384              
4385             /* needed for SET_RESULTS */
4386 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
4387              
4388 0           rc = 0;
4389 0           bad_error = 0;
4390              
4391             /* a non empty bin means that we didn't meet constraints. fold it into
4392             the previous bin if requested & possible. sometimes that will
4393             actually lower the S/N of the previous bin; keep going until
4394             we can't fold anymore or we get the proper S/N
4395             */
4396 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
4397              
4398              
4399 0 0         while ( --curind > 0 ) {
4400             double tmp;
4401 0           int snr_ok = 0;
4402 0           PDL_Indx nin_last = nin;
4403              
4404            
4405 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
4406 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
4407              
4408             /* calculate error */
4409 0 0         if ( error_sdev ) {
4410              
4411             /* weighted standard deviation */
4412 0 0         if ( have_error ) {
4413            
4414             /* parallel algorithm (as we're adding bins together) for
4415             (possibly) weighted standard deviation; see
4416             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4417             */
4418 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4419 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4420 0           double mean_b = bmean;
4421 0           double weight_a = _weight;
4422 0           double weight_b = bweight;
4423 0           double weight_x = bweight += weight_a;
4424 0           double delta = mean_b - mean_a;
4425 0           bmean = mean_a + delta * weight_b / weight_x;
4426 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
4427 0           + delta * delta * weight_a * weight_b / weight_x;
4428              
4429 0           bad_error = nin <= 1;
4430 0           berror = bad_error
4431             ? DBL_MAX
4432 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
4433            
4434             }
4435              
4436             else {
4437            
4438             /* parallel algorithm (as we're adding bins together) for
4439             (possibly) weighted standard deviation; see
4440             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4441             */
4442 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4443 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4444 0           double mean_b = bmean;
4445 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
4446 0           double weight_b = nin_last;
4447 0           double weight_x = nin;
4448 0           double delta = mean_b - mean_a;
4449 0           bmean = mean_a + delta * weight_b / weight_x;
4450 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
4451 0           + delta * delta * weight_a * weight_b / weight_x;
4452              
4453 0           bad_error = nin <= 1;
4454 0           berror = bad_error
4455             ? DBL_MAX
4456 0 0         : sqrt( bm2 * 1 / (nin - 1) );
4457            
4458             }
4459             }
4460              
4461 0 0         else if ( error_rss ) {
4462            
4463             /* parallel algorithm (as we're adding bins together) for
4464             mean; see
4465             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4466             */
4467 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
4468 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4469 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4470 0           double mean_b = bmean;
4471 0           double weight_a = _weight;
4472 0           double weight_b = bweight;
4473 0           double weight_x = bweight += weight_a;
4474 0           double delta = mean_b - mean_a;
4475 0           bmean = mean_a + delta * weight_b / weight_x;
4476              
4477 0           berror2 += _error2;
4478 0           berror = sqrt( berror2 );
4479            
4480             }
4481              
4482 0 0         else if ( error_poisson ) {
4483 0           berror = sqrt( nin );
4484 0           bmean = bsignal / nin;
4485             }
4486              
4487             else {
4488 0           croak( "internal error" );
4489             }
4490            
4491              
4492 0           bsnr = bsignal / berror;
4493 0           snr_ok = bsnr >= min_snr;
4494              
4495            
4496 0           rc |= \
4497 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
4498 0           | \
4499 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
4500 0           | \
4501             ( nin >= min_nelem \
4502 0 0         && bwidth >= min_width \
4503 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
4504             ; \
4505             ;
4506              
4507 0 0         if (rc)
4508 0           break;
4509             }
4510              
4511             /* fix up index for events initially stuck in folded bins */
4512 0           PDL_Indx curind1 = curind+1;
4513             PDL_Indx ni;
4514              
4515 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
4516 0 0         ni < __privtrans->ind_sizes[0] ;
4517 0           ni++ ) {
4518             #ifdef PDL_BAD_CODE
4519 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
4520             #endif /* PDL_BAD_CODE */
4521 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
4522             }
4523 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4524 0           rc |= BIN_RC_FOLDED;
4525             }
4526              
4527            
4528 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
4529 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
4530 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
4531 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
4532            
4533 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
4534 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
4535            
4536 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
4537 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
4538 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4539 0 0         if ( error_sdev ) {
4540 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
4541 0 0         if ( have_error ) {
4542 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4543 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
4544 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
4545             }
4546             else {
4547 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
4548             }
4549             }
4550 0 0         else if ( error_rss ) {
4551 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
4552 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4553             }
4554            
4555             }
4556             /* adjust for possibility of last bin being empty */
4557 0           (nbins_datap)[0] = curind + ( nin != 0 );
4558 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
4559             }
4560 0           } break;
4561 0           case PDL_D: {
4562 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
4563             {
4564              
4565              
4566              
4567              
4568 0           int flags = __params->optflags;
4569              
4570 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
4571 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
4572 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
4573 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
4574 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
4575 0           int fold_last_bin = flags & BIN_ARG_FOLD;
4576 0           int set_bad = flags & BIN_ARG_SET_BAD;
4577              
4578 0           PDL_Indx min_nelem = __params->min_nelem;
4579 0           PDL_Indx max_nelem = __params->max_nelem;
4580 0           double max_width = __params->max_width;
4581 0           double min_width = __params->min_width;
4582 0           double min_snr = __params->min_snr;
4583              
4584             /* simplify the logic below by setting bounds values to their most
4585             permissive extremes if they aren't needed. */
4586              
4587 0 0         if ( max_width == 0 )
4588 0           max_width = DBL_MAX;
4589              
4590 0 0         if ( max_nelem == 0 )
4591 0           max_nelem = LONG_MAX;
4592              
4593 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
4594              
4595 0           PDL_Indx curind = 0; /* index of current bin */
4596              
4597             /* Declare bin counters */
4598 0           int rc = 0; /* status */
4599 0           double bsignal = 0; /* sum of signal */
4600 0           double bmean = 0; /* mean of (weighted) signal */
4601 0           double bwidth = 0; /* width (if applicable) */
4602 0           double bsnr = 0; /* SNR */
4603 0           double berror2 = 0; /* sum of error^2 */
4604 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
4605 0           double bm2 = 0; /* unnormalized variance */
4606 0           double bsignal2 = 0; /* sum of signal^2 */
4607 0           double bweight = 0; /* sum of 1/error*2 */
4608 0           double bweight_sig = 0; /* sum of weight * signal */
4609 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
4610 0           int bad_error = 0; /* if error2 is not good */
4611 0           int lastrc = 0; /* carryover status from previous loop */
4612 0           PDL_Indx nin = 0; /* number of elements in the current bin */
4613              
4614 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
4615              
4616 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
4617             double error;
4618             double error2;
4619             double width;
4620              
4621 0           int snr_ok = 0;
4622              
4623 0 0         if ( have_error ) {
4624 0           error = (error_datap)[0+(__inc_error_n*(n))];
4625 0           error2 = error * error;
4626             }
4627              
4628 0 0         if ( have_width )
4629 0           width = (width_datap)[0+(__inc_width_n*(n))];
4630              
4631             #ifdef PDL_BAD_CODE
4632 0 0         if ( PDL_ISBAD2(signal,signal_badval,D,signal_badval_isnan)
    0          
    0          
4633 0 0         ||
    0          
4634 0 0         have_error && PDL_ISBAD2(error,error_badval,D,error_badval_isnan) ) {
    0          
4635 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
4636 0           continue;
4637             }
4638             #endif /* PDL_BAD_CODE */
4639 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
4640              
4641            
4642 0           bsignal += signal;
4643 0           nin += 1;
4644              
4645             /* calculate error */
4646 0 0         if ( error_sdev ) {
4647              
4648             /* weighted standard deviation */
4649 0 0         if ( have_error ) {
4650            
4651             /* incremental algorithm for possibly weighted standard deviation; see
4652             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4653             */
4654 0           double _weight = 1 / ( error * error );
4655 0           double _sum_weight = bweight += _weight;
4656 0           double delta = signal - bmean;
4657 0           bmean += delta * _weight / _sum_weight;
4658 0           bm2 += _weight * delta * ( signal - bmean );
4659              
4660 0           bad_error = nin <= 1;
4661 0           berror = bad_error
4662             ? DBL_MAX
4663 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
4664            
4665             }
4666              
4667             else {
4668            
4669             /* incremental algorithm for possibly weighted standard deviation; see
4670             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4671             */
4672 0           double _weight = 1;
4673 0           double _sum_weight = nin;
4674 0           double delta = signal - bmean;
4675 0           bmean += delta * _weight / _sum_weight;
4676 0           bm2 += _weight * delta * ( signal - bmean );
4677              
4678 0           bad_error = nin <= 1;
4679 0           berror = bad_error
4680             ? DBL_MAX
4681 0 0         : sqrt( bm2 * 1 / (nin - 1) );
4682            
4683             }
4684             }
4685              
4686 0 0         else if ( error_rss ) {
4687            
4688             /* incremental algorithm for mean; see
4689             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4690             */
4691 0           double _error2 = error2;
4692 0           double _weight = 1 / ( error * error );
4693 0           double delta = signal - bmean;
4694              
4695 0           bweight += _weight;
4696 0           bmean += delta * _weight / bweight;
4697 0           berror2 += _error2;
4698 0           berror = sqrt( berror2 );
4699            
4700             }
4701              
4702 0 0         else if ( error_poisson ) {
4703 0           berror = sqrt( nin );
4704 0           bmean = bsignal / nin;
4705             }
4706              
4707             else {
4708 0           croak( "internal error" );
4709             }
4710            
4711              
4712 0           bsnr = bsignal / berror;
4713 0           snr_ok = bsnr >= min_snr;
4714              
4715 0 0         if ( have_width )
4716 0           bwidth += width;
4717              
4718 0 0         if ( nin == 1 )
4719 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
4720              
4721            
4722 0           rc |= \
4723 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
4724 0           | \
4725 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
4726 0           | \
4727             ( nin >= min_nelem \
4728 0 0         && bwidth >= min_width \
4729 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
4730             ; \
4731             ;
4732              
4733 0 0         if ( rc ) {
4734 0           rc |= lastrc;
4735              
4736            
4737 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
4738 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
4739 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
4740 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
4741            
4742 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
4743 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
4744            
4745 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
4746 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
4747 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4748 0 0         if ( error_sdev ) {
4749 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
4750 0 0         if ( have_error ) {
4751 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4752 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
4753 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
4754             }
4755             else {
4756 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
4757             }
4758             }
4759 0 0         else if ( error_rss ) {
4760 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
4761 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4762             }
4763            
4764              
4765 0           curind++;
4766              
4767             /* Reset bin counters */
4768 0           rc = 0;
4769 0           bsignal = 0;
4770 0           bmean = 0;
4771 0           bwidth = 0;
4772 0           bsnr = 0;
4773 0           berror2 = 0;
4774 0           berror = 0;
4775 0           bm2 = 0;
4776 0           bsignal2 = 0;
4777 0           bweight = 0;
4778 0           bweight_sig = 0;
4779 0           bweight_sig2 = 0;
4780 0           bad_error = 0;
4781 0           lastrc = 0;
4782 0           nin = 0;
4783              
4784             }
4785              
4786 0 0         else if ( snr_ok ) {
4787 0           lastrc = BIN_RC_GTMINSN;
4788             }
4789              
4790             else {
4791 0           lastrc = 0;
4792             }
4793             }} /* Close n */
4794              
4795              
4796             /* record last bin if it's not empty */
4797 0 0         if ( nin ) {
4798              
4799             /* needed for SET_RESULTS */
4800 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
4801              
4802 0           rc = 0;
4803 0           bad_error = 0;
4804              
4805             /* a non empty bin means that we didn't meet constraints. fold it into
4806             the previous bin if requested & possible. sometimes that will
4807             actually lower the S/N of the previous bin; keep going until
4808             we can't fold anymore or we get the proper S/N
4809             */
4810 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
4811              
4812              
4813 0 0         while ( --curind > 0 ) {
4814             double tmp;
4815 0           int snr_ok = 0;
4816 0           PDL_Indx nin_last = nin;
4817              
4818            
4819 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
4820 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
4821              
4822             /* calculate error */
4823 0 0         if ( error_sdev ) {
4824              
4825             /* weighted standard deviation */
4826 0 0         if ( have_error ) {
4827            
4828             /* parallel algorithm (as we're adding bins together) for
4829             (possibly) weighted standard deviation; see
4830             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4831             */
4832 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4833 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4834 0           double mean_b = bmean;
4835 0           double weight_a = _weight;
4836 0           double weight_b = bweight;
4837 0           double weight_x = bweight += weight_a;
4838 0           double delta = mean_b - mean_a;
4839 0           bmean = mean_a + delta * weight_b / weight_x;
4840 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
4841 0           + delta * delta * weight_a * weight_b / weight_x;
4842              
4843 0           bad_error = nin <= 1;
4844 0           berror = bad_error
4845             ? DBL_MAX
4846 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
4847            
4848             }
4849              
4850             else {
4851            
4852             /* parallel algorithm (as we're adding bins together) for
4853             (possibly) weighted standard deviation; see
4854             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4855             */
4856 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4857 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4858 0           double mean_b = bmean;
4859 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
4860 0           double weight_b = nin_last;
4861 0           double weight_x = nin;
4862 0           double delta = mean_b - mean_a;
4863 0           bmean = mean_a + delta * weight_b / weight_x;
4864 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
4865 0           + delta * delta * weight_a * weight_b / weight_x;
4866              
4867 0           bad_error = nin <= 1;
4868 0           berror = bad_error
4869             ? DBL_MAX
4870 0 0         : sqrt( bm2 * 1 / (nin - 1) );
4871            
4872             }
4873             }
4874              
4875 0 0         else if ( error_rss ) {
4876            
4877             /* parallel algorithm (as we're adding bins together) for
4878             mean; see
4879             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
4880             */
4881 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
4882 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
4883 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
4884 0           double mean_b = bmean;
4885 0           double weight_a = _weight;
4886 0           double weight_b = bweight;
4887 0           double weight_x = bweight += weight_a;
4888 0           double delta = mean_b - mean_a;
4889 0           bmean = mean_a + delta * weight_b / weight_x;
4890              
4891 0           berror2 += _error2;
4892 0           berror = sqrt( berror2 );
4893            
4894             }
4895              
4896 0 0         else if ( error_poisson ) {
4897 0           berror = sqrt( nin );
4898 0           bmean = bsignal / nin;
4899             }
4900              
4901             else {
4902 0           croak( "internal error" );
4903             }
4904            
4905              
4906 0           bsnr = bsignal / berror;
4907 0           snr_ok = bsnr >= min_snr;
4908              
4909            
4910 0           rc |= \
4911 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
4912 0           | \
4913 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
4914 0           | \
4915             ( nin >= min_nelem \
4916 0 0         && bwidth >= min_width \
4917 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
4918             ; \
4919             ;
4920              
4921 0 0         if (rc)
4922 0           break;
4923             }
4924              
4925             /* fix up index for events initially stuck in folded bins */
4926 0           PDL_Indx curind1 = curind+1;
4927             PDL_Indx ni;
4928              
4929 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
4930 0 0         ni < __privtrans->ind_sizes[0] ;
4931 0           ni++ ) {
4932             #ifdef PDL_BAD_CODE
4933 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
4934             #endif /* PDL_BAD_CODE */
4935 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
4936             }
4937 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4938 0           rc |= BIN_RC_FOLDED;
4939             }
4940              
4941            
4942 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
4943 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
4944 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
4945 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
4946            
4947 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
4948 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
4949            
4950 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
4951 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
4952 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
4953 0 0         if ( error_sdev ) {
4954 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
4955 0 0         if ( have_error ) {
4956 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4957 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
4958 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
4959             }
4960             else {
4961 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
4962             }
4963             }
4964 0 0         else if ( error_rss ) {
4965 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
4966 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
4967             }
4968            
4969             }
4970             /* adjust for possibility of last bin being empty */
4971 0           (nbins_datap)[0] = curind + ( nin != 0 );
4972 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
4973             }
4974 0           } break;
4975 0           case PDL_LD: {
4976 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_LDouble,E,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
4977             {
4978              
4979              
4980              
4981              
4982 0           int flags = __params->optflags;
4983              
4984 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
4985 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
4986 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
4987 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
4988 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
4989 0           int fold_last_bin = flags & BIN_ARG_FOLD;
4990 0           int set_bad = flags & BIN_ARG_SET_BAD;
4991              
4992 0           PDL_Indx min_nelem = __params->min_nelem;
4993 0           PDL_Indx max_nelem = __params->max_nelem;
4994 0           double max_width = __params->max_width;
4995 0           double min_width = __params->min_width;
4996 0           double min_snr = __params->min_snr;
4997              
4998             /* simplify the logic below by setting bounds values to their most
4999             permissive extremes if they aren't needed. */
5000              
5001 0 0         if ( max_width == 0 )
5002 0           max_width = DBL_MAX;
5003              
5004 0 0         if ( max_nelem == 0 )
5005 0           max_nelem = LONG_MAX;
5006              
5007 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
5008              
5009 0           PDL_Indx curind = 0; /* index of current bin */
5010              
5011             /* Declare bin counters */
5012 0           int rc = 0; /* status */
5013 0           double bsignal = 0; /* sum of signal */
5014 0           double bmean = 0; /* mean of (weighted) signal */
5015 0           double bwidth = 0; /* width (if applicable) */
5016 0           double bsnr = 0; /* SNR */
5017 0           double berror2 = 0; /* sum of error^2 */
5018 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
5019 0           double bm2 = 0; /* unnormalized variance */
5020 0           double bsignal2 = 0; /* sum of signal^2 */
5021 0           double bweight = 0; /* sum of 1/error*2 */
5022 0           double bweight_sig = 0; /* sum of weight * signal */
5023 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
5024 0           int bad_error = 0; /* if error2 is not good */
5025 0           int lastrc = 0; /* carryover status from previous loop */
5026 0           PDL_Indx nin = 0; /* number of elements in the current bin */
5027              
5028 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
5029              
5030 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
5031             double error;
5032             double error2;
5033             double width;
5034              
5035 0           int snr_ok = 0;
5036              
5037 0 0         if ( have_error ) {
5038 0           error = (error_datap)[0+(__inc_error_n*(n))];
5039 0           error2 = error * error;
5040             }
5041              
5042 0 0         if ( have_width )
5043 0           width = (width_datap)[0+(__inc_width_n*(n))];
5044              
5045             #ifdef PDL_BAD_CODE
5046 0 0         if ( PDL_ISBAD2(signal,signal_badval,E,signal_badval_isnan)
    0          
    0          
5047 0 0         ||
    0          
5048 0 0         have_error && PDL_ISBAD2(error,error_badval,E,error_badval_isnan) ) {
    0          
5049 0           (index_datap)[0+(__inc_index_n*(n))]=index_badval;
5050 0           continue;
5051             }
5052             #endif /* PDL_BAD_CODE */
5053 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
5054              
5055            
5056 0           bsignal += signal;
5057 0           nin += 1;
5058              
5059             /* calculate error */
5060 0 0         if ( error_sdev ) {
5061              
5062             /* weighted standard deviation */
5063 0 0         if ( have_error ) {
5064            
5065             /* incremental algorithm for possibly weighted standard deviation; see
5066             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
5067             */
5068 0           double _weight = 1 / ( error * error );
5069 0           double _sum_weight = bweight += _weight;
5070 0           double delta = signal - bmean;
5071 0           bmean += delta * _weight / _sum_weight;
5072 0           bm2 += _weight * delta * ( signal - bmean );
5073              
5074 0           bad_error = nin <= 1;
5075 0           berror = bad_error
5076             ? DBL_MAX
5077 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
5078            
5079             }
5080              
5081             else {
5082            
5083             /* incremental algorithm for possibly weighted standard deviation; see
5084             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
5085             */
5086 0           double _weight = 1;
5087 0           double _sum_weight = nin;
5088 0           double delta = signal - bmean;
5089 0           bmean += delta * _weight / _sum_weight;
5090 0           bm2 += _weight * delta * ( signal - bmean );
5091              
5092 0           bad_error = nin <= 1;
5093 0           berror = bad_error
5094             ? DBL_MAX
5095 0 0         : sqrt( bm2 * 1 / (nin - 1) );
5096            
5097             }
5098             }
5099              
5100 0 0         else if ( error_rss ) {
5101            
5102             /* incremental algorithm for mean; see
5103             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
5104             */
5105 0           double _error2 = error2;
5106 0           double _weight = 1 / ( error * error );
5107 0           double delta = signal - bmean;
5108              
5109 0           bweight += _weight;
5110 0           bmean += delta * _weight / bweight;
5111 0           berror2 += _error2;
5112 0           berror = sqrt( berror2 );
5113            
5114             }
5115              
5116 0 0         else if ( error_poisson ) {
5117 0           berror = sqrt( nin );
5118 0           bmean = bsignal / nin;
5119             }
5120              
5121             else {
5122 0           croak( "internal error" );
5123             }
5124            
5125              
5126 0           bsnr = bsignal / berror;
5127 0           snr_ok = bsnr >= min_snr;
5128              
5129 0 0         if ( have_width )
5130 0           bwidth += width;
5131              
5132 0 0         if ( nin == 1 )
5133 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
5134              
5135            
5136 0           rc |= \
5137 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
5138 0           | \
5139 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
5140 0           | \
5141             ( nin >= min_nelem \
5142 0 0         && bwidth >= min_width \
5143 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
5144             ; \
5145             ;
5146              
5147 0 0         if ( rc ) {
5148 0           rc |= lastrc;
5149              
5150            
5151 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
5152 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
5153 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
5154 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
5155            
5156 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
5157 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
5158            
5159 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
5160 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
5161 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
5162 0 0         if ( error_sdev ) {
5163 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
5164 0 0         if ( have_error ) {
5165 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5166 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
5167 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
5168             }
5169             else {
5170 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
5171             }
5172             }
5173 0 0         else if ( error_rss ) {
5174 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
5175 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5176             }
5177            
5178              
5179 0           curind++;
5180              
5181             /* Reset bin counters */
5182 0           rc = 0;
5183 0           bsignal = 0;
5184 0           bmean = 0;
5185 0           bwidth = 0;
5186 0           bsnr = 0;
5187 0           berror2 = 0;
5188 0           berror = 0;
5189 0           bm2 = 0;
5190 0           bsignal2 = 0;
5191 0           bweight = 0;
5192 0           bweight_sig = 0;
5193 0           bweight_sig2 = 0;
5194 0           bad_error = 0;
5195 0           lastrc = 0;
5196 0           nin = 0;
5197              
5198             }
5199              
5200 0 0         else if ( snr_ok ) {
5201 0           lastrc = BIN_RC_GTMINSN;
5202             }
5203              
5204             else {
5205 0           lastrc = 0;
5206             }
5207             }} /* Close n */
5208              
5209              
5210             /* record last bin if it's not empty */
5211 0 0         if ( nin ) {
5212              
5213             /* needed for SET_RESULTS */
5214 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
5215              
5216 0           rc = 0;
5217 0           bad_error = 0;
5218              
5219             /* a non empty bin means that we didn't meet constraints. fold it into
5220             the previous bin if requested & possible. sometimes that will
5221             actually lower the S/N of the previous bin; keep going until
5222             we can't fold anymore or we get the proper S/N
5223             */
5224 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
5225              
5226              
5227 0 0         while ( --curind > 0 ) {
5228             double tmp;
5229 0           int snr_ok = 0;
5230 0           PDL_Indx nin_last = nin;
5231              
5232            
5233 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
5234 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
5235              
5236             /* calculate error */
5237 0 0         if ( error_sdev ) {
5238              
5239             /* weighted standard deviation */
5240 0 0         if ( have_error ) {
5241            
5242             /* parallel algorithm (as we're adding bins together) for
5243             (possibly) weighted standard deviation; see
5244             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
5245             */
5246 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
5247 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
5248 0           double mean_b = bmean;
5249 0           double weight_a = _weight;
5250 0           double weight_b = bweight;
5251 0           double weight_x = bweight += weight_a;
5252 0           double delta = mean_b - mean_a;
5253 0           bmean = mean_a + delta * weight_b / weight_x;
5254 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
5255 0           + delta * delta * weight_a * weight_b / weight_x;
5256              
5257 0           bad_error = nin <= 1;
5258 0           berror = bad_error
5259             ? DBL_MAX
5260 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
5261            
5262             }
5263              
5264             else {
5265            
5266             /* parallel algorithm (as we're adding bins together) for
5267             (possibly) weighted standard deviation; see
5268             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
5269             */
5270 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
5271 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
5272 0           double mean_b = bmean;
5273 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
5274 0           double weight_b = nin_last;
5275 0           double weight_x = nin;
5276 0           double delta = mean_b - mean_a;
5277 0           bmean = mean_a + delta * weight_b / weight_x;
5278 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
5279 0           + delta * delta * weight_a * weight_b / weight_x;
5280              
5281 0           bad_error = nin <= 1;
5282 0           berror = bad_error
5283             ? DBL_MAX
5284 0 0         : sqrt( bm2 * 1 / (nin - 1) );
5285            
5286             }
5287             }
5288              
5289 0 0         else if ( error_rss ) {
5290            
5291             /* parallel algorithm (as we're adding bins together) for
5292             mean; see
5293             https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
5294             */
5295 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
5296 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
5297 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
5298 0           double mean_b = bmean;
5299 0           double weight_a = _weight;
5300 0           double weight_b = bweight;
5301 0           double weight_x = bweight += weight_a;
5302 0           double delta = mean_b - mean_a;
5303 0           bmean = mean_a + delta * weight_b / weight_x;
5304              
5305 0           berror2 += _error2;
5306 0           berror = sqrt( berror2 );
5307            
5308             }
5309              
5310 0 0         else if ( error_poisson ) {
5311 0           berror = sqrt( nin );
5312 0           bmean = bsignal / nin;
5313             }
5314              
5315             else {
5316 0           croak( "internal error" );
5317             }
5318            
5319              
5320 0           bsnr = bsignal / berror;
5321 0           snr_ok = bsnr >= min_snr;
5322              
5323            
5324 0           rc |= \
5325 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
5326 0           | \
5327 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
5328 0           | \
5329             ( nin >= min_nelem \
5330 0 0         && bwidth >= min_width \
5331 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
5332             ; \
5333             ;
5334              
5335 0 0         if (rc)
5336 0           break;
5337             }
5338              
5339             /* fix up index for events initially stuck in folded bins */
5340 0           PDL_Indx curind1 = curind+1;
5341             PDL_Indx ni;
5342              
5343 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
5344 0 0         ni < __privtrans->ind_sizes[0] ;
5345 0           ni++ ) {
5346             #ifdef PDL_BAD_CODE
5347 0 0         if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
    0          
5348             #endif /* PDL_BAD_CODE */
5349 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
5350             }
5351 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
5352 0           rc |= BIN_RC_FOLDED;
5353             }
5354              
5355            
5356 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
5357 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
5358 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
5359 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
5360            
5361 0 0         if ( set_bad && bad_error ) { (b_error_datap)[0+(__inc_b_error_n*(curind))]=b_error_badval; }
    0          
5362 0           else { (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror; }
5363            
5364 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
5365 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
5366 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
5367 0 0         if ( error_sdev ) {
5368 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
5369 0 0         if ( have_error ) {
5370 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5371 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
5372 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
5373             }
5374             else {
5375 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
5376             }
5377             }
5378 0 0         else if ( error_rss ) {
5379 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
5380 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5381             }
5382            
5383             }
5384             /* adjust for possibility of last bin being empty */
5385 0           (nbins_datap)[0] = curind + ( nin != 0 );
5386 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
5387             }
5388 0           } break;
5389 0           default: return PDL->make_error(PDL_EUSERERROR, "PP INTERNAL ERROR in bin_adaptive_snr: unhandled datatype(%d), only handles (ABSULKNPQFDE)! PLEASE MAKE A BUG REPORT\n", __privtrans->__datatype);
5390             }
5391             #undef PDL_BAD_CODE
5392             #undef PDL_IF_BAD
5393             } else { /* ** else do 'good' Code ** */
5394             #define PDL_IF_BAD(t,f) f
5395 35           switch (__privtrans->__datatype) { /* Start generic switch */
5396 0           case PDL_SB: {
5397 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_SByte,A,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
5398             {
5399              
5400              
5401              
5402              
5403 0           int flags = __params->optflags;
5404              
5405 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
5406 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
5407 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
5408 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
5409 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
5410 0           int fold_last_bin = flags & BIN_ARG_FOLD;
5411 0           int set_bad = flags & BIN_ARG_SET_BAD;
5412              
5413 0           PDL_Indx min_nelem = __params->min_nelem;
5414 0           PDL_Indx max_nelem = __params->max_nelem;
5415 0           double max_width = __params->max_width;
5416 0           double min_width = __params->min_width;
5417 0           double min_snr = __params->min_snr;
5418              
5419             /* simplify the logic below by setting bounds values to their most
5420             permissive extremes if they aren't needed. */
5421              
5422 0 0         if ( max_width == 0 )
5423 0           max_width = DBL_MAX;
5424              
5425 0 0         if ( max_nelem == 0 )
5426 0           max_nelem = LONG_MAX;
5427              
5428 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
5429              
5430 0           PDL_Indx curind = 0; /* index of current bin */
5431              
5432             /* Declare bin counters */
5433 0           int rc = 0; /* status */
5434 0           double bsignal = 0; /* sum of signal */
5435 0           double bmean = 0; /* mean of (weighted) signal */
5436 0           double bwidth = 0; /* width (if applicable) */
5437 0           double bsnr = 0; /* SNR */
5438 0           double berror2 = 0; /* sum of error^2 */
5439 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
5440 0           double bm2 = 0; /* unnormalized variance */
5441 0           double bsignal2 = 0; /* sum of signal^2 */
5442 0           double bweight = 0; /* sum of 1/error*2 */
5443 0           double bweight_sig = 0; /* sum of weight * signal */
5444 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
5445 0           int bad_error = 0; /* if error2 is not good */
5446 0           int lastrc = 0; /* carryover status from previous loop */
5447 0           PDL_Indx nin = 0; /* number of elements in the current bin */
5448              
5449 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
5450              
5451 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
5452             double error;
5453             double error2;
5454             double width;
5455              
5456 0           int snr_ok = 0;
5457              
5458 0 0         if ( have_error ) {
5459 0           error = (error_datap)[0+(__inc_error_n*(n))];
5460 0           error2 = error * error;
5461             }
5462              
5463 0 0         if ( have_width )
5464 0           width = (width_datap)[0+(__inc_width_n*(n))];
5465              
5466             #ifdef PDL_BAD_CODE
5467             if ( PDL_ISBAD2(signal,signal_badval,A,signal_badval_isnan)
5468             ||
5469             have_error && PDL_ISBAD2(error,error_badval,A,error_badval_isnan) ) {
5470             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
5471             continue;
5472             }
5473             #endif /* PDL_BAD_CODE */
5474 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
5475              
5476            
5477 0           bsignal += signal;
5478 0           nin += 1;
5479              
5480             /* calculate error */
5481 0 0         if ( error_sdev ) {
5482              
5483             /* weighted standard deviation */
5484 0 0         if ( have_error ) {
5485            
5486             /* incremental algorithm for possibly weighted standard deviation; see
5487             https: */
5488 0           double _weight = 1 / ( error * error );
5489 0           double _sum_weight = bweight += _weight;
5490 0           double delta = signal - bmean;
5491 0           bmean += delta * _weight / _sum_weight;
5492 0           bm2 += _weight * delta * ( signal - bmean );
5493              
5494 0           bad_error = nin <= 1;
5495 0           berror = bad_error
5496             ? DBL_MAX
5497 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
5498            
5499             }
5500              
5501             else {
5502            
5503             /* incremental algorithm for possibly weighted standard deviation; see
5504             https: */
5505 0           double _weight = 1;
5506 0           double _sum_weight = nin;
5507 0           double delta = signal - bmean;
5508 0           bmean += delta * _weight / _sum_weight;
5509 0           bm2 += _weight * delta * ( signal - bmean );
5510              
5511 0           bad_error = nin <= 1;
5512 0           berror = bad_error
5513             ? DBL_MAX
5514 0 0         : sqrt( bm2 * 1 / (nin - 1) );
5515            
5516             }
5517             }
5518              
5519 0 0         else if ( error_rss ) {
5520            
5521             /* incremental algorithm for mean; see
5522             https: */
5523 0           double _error2 = error2;
5524 0           double _weight = 1 / ( error * error );
5525 0           double delta = signal - bmean;
5526              
5527 0           bweight += _weight;
5528 0           bmean += delta * _weight / bweight;
5529 0           berror2 += _error2;
5530 0           berror = sqrt( berror2 );
5531            
5532             }
5533              
5534 0 0         else if ( error_poisson ) {
5535 0           berror = sqrt( nin );
5536 0           bmean = bsignal / nin;
5537             }
5538              
5539             else {
5540 0           croak( "internal error" );
5541             }
5542            
5543              
5544 0           bsnr = bsignal / berror;
5545 0           snr_ok = bsnr >= min_snr;
5546              
5547 0 0         if ( have_width )
5548 0           bwidth += width;
5549              
5550 0 0         if ( nin == 1 )
5551 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
5552              
5553            
5554 0           rc |= \
5555 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
5556 0           | \
5557 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
5558 0           | \
5559             ( nin >= min_nelem \
5560 0 0         && bwidth >= min_width \
5561 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
5562             ; \
5563             ;
5564              
5565 0 0         if ( rc ) {
5566 0           rc |= lastrc;
5567              
5568            
5569 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
5570 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
5571 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
5572 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
5573            
5574 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
5575            
5576 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
5577 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
5578 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
5579 0 0         if ( error_sdev ) {
5580 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
5581 0 0         if ( have_error ) {
5582 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5583 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
5584 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
5585             }
5586             else {
5587 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
5588             }
5589             }
5590 0 0         else if ( error_rss ) {
5591 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
5592 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5593             }
5594            
5595              
5596 0           curind++;
5597              
5598             /* Reset bin counters */
5599 0           rc = 0;
5600 0           bsignal = 0;
5601 0           bmean = 0;
5602 0           bwidth = 0;
5603 0           bsnr = 0;
5604 0           berror2 = 0;
5605 0           berror = 0;
5606 0           bm2 = 0;
5607 0           bsignal2 = 0;
5608 0           bweight = 0;
5609 0           bweight_sig = 0;
5610 0           bweight_sig2 = 0;
5611 0           bad_error = 0;
5612 0           lastrc = 0;
5613 0           nin = 0;
5614              
5615             }
5616              
5617 0 0         else if ( snr_ok ) {
5618 0           lastrc = BIN_RC_GTMINSN;
5619             }
5620              
5621             else {
5622 0           lastrc = 0;
5623             }
5624             }} /* Close n */
5625              
5626              
5627             /* record last bin if it's not empty */
5628 0 0         if ( nin ) {
5629              
5630             /* needed for SET_RESULTS */
5631 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
5632              
5633 0           rc = 0;
5634 0           bad_error = 0;
5635              
5636             /* a non empty bin means that we didn't meet constraints. fold it into
5637             the previous bin if requested & possible. sometimes that will
5638             actually lower the S/N of the previous bin; keep going until
5639             we can't fold anymore or we get the proper S/N
5640             */
5641 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
5642              
5643              
5644 0 0         while ( --curind > 0 ) {
5645             double tmp;
5646 0           int snr_ok = 0;
5647 0           PDL_Indx nin_last = nin;
5648              
5649            
5650 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
5651 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
5652              
5653             /* calculate error */
5654 0 0         if ( error_sdev ) {
5655              
5656             /* weighted standard deviation */
5657 0 0         if ( have_error ) {
5658            
5659             /* parallel algorithm (as we're adding bins together) for
5660             (possibly) weighted standard deviation; see
5661             https: */
5662 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
5663 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
5664 0           double mean_b = bmean;
5665 0           double weight_a = _weight;
5666 0           double weight_b = bweight;
5667 0           double weight_x = bweight += weight_a;
5668 0           double delta = mean_b - mean_a;
5669 0           bmean = mean_a + delta * weight_b / weight_x;
5670 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
5671 0           + delta * delta * weight_a * weight_b / weight_x;
5672              
5673 0           bad_error = nin <= 1;
5674 0           berror = bad_error
5675             ? DBL_MAX
5676 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
5677            
5678             }
5679              
5680             else {
5681            
5682             /* parallel algorithm (as we're adding bins together) for
5683             (possibly) weighted standard deviation; see
5684             https: */
5685 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
5686 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
5687 0           double mean_b = bmean;
5688 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
5689 0           double weight_b = nin_last;
5690 0           double weight_x = nin;
5691 0           double delta = mean_b - mean_a;
5692 0           bmean = mean_a + delta * weight_b / weight_x;
5693 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
5694 0           + delta * delta * weight_a * weight_b / weight_x;
5695              
5696 0           bad_error = nin <= 1;
5697 0           berror = bad_error
5698             ? DBL_MAX
5699 0 0         : sqrt( bm2 * 1 / (nin - 1) );
5700            
5701             }
5702             }
5703              
5704 0 0         else if ( error_rss ) {
5705            
5706             /* parallel algorithm (as we're adding bins together) for
5707             mean; see
5708             https: */
5709 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
5710 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
5711 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
5712 0           double mean_b = bmean;
5713 0           double weight_a = _weight;
5714 0           double weight_b = bweight;
5715 0           double weight_x = bweight += weight_a;
5716 0           double delta = mean_b - mean_a;
5717 0           bmean = mean_a + delta * weight_b / weight_x;
5718              
5719 0           berror2 += _error2;
5720 0           berror = sqrt( berror2 );
5721            
5722             }
5723              
5724 0 0         else if ( error_poisson ) {
5725 0           berror = sqrt( nin );
5726 0           bmean = bsignal / nin;
5727             }
5728              
5729             else {
5730 0           croak( "internal error" );
5731             }
5732            
5733              
5734 0           bsnr = bsignal / berror;
5735 0           snr_ok = bsnr >= min_snr;
5736              
5737            
5738 0           rc |= \
5739 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
5740 0           | \
5741 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
5742 0           | \
5743             ( nin >= min_nelem \
5744 0 0         && bwidth >= min_width \
5745 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
5746             ; \
5747             ;
5748              
5749 0 0         if (rc)
5750 0           break;
5751             }
5752              
5753             /* fix up index for events initially stuck in folded bins */
5754 0           PDL_Indx curind1 = curind+1;
5755             PDL_Indx ni;
5756              
5757 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
5758 0 0         ni < __privtrans->ind_sizes[0] ;
5759 0           ni++ ) {
5760             #ifdef PDL_BAD_CODE
5761             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
5762             #endif /* PDL_BAD_CODE */
5763 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
5764             }
5765 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
5766 0           rc |= BIN_RC_FOLDED;
5767             }
5768              
5769            
5770 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
5771 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
5772 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
5773 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
5774            
5775 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
5776            
5777 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
5778 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
5779 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
5780 0 0         if ( error_sdev ) {
5781 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
5782 0 0         if ( have_error ) {
5783 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5784 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
5785 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
5786             }
5787             else {
5788 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
5789             }
5790             }
5791 0 0         else if ( error_rss ) {
5792 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
5793 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5794             }
5795            
5796             }
5797             /* adjust for possibility of last bin being empty */
5798 0           (nbins_datap)[0] = curind + ( nin != 0 );
5799 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
5800             }
5801 0           } break;
5802 0           case PDL_B: {
5803 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Byte,B,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
5804             {
5805              
5806              
5807              
5808              
5809 0           int flags = __params->optflags;
5810              
5811 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
5812 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
5813 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
5814 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
5815 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
5816 0           int fold_last_bin = flags & BIN_ARG_FOLD;
5817 0           int set_bad = flags & BIN_ARG_SET_BAD;
5818              
5819 0           PDL_Indx min_nelem = __params->min_nelem;
5820 0           PDL_Indx max_nelem = __params->max_nelem;
5821 0           double max_width = __params->max_width;
5822 0           double min_width = __params->min_width;
5823 0           double min_snr = __params->min_snr;
5824              
5825             /* simplify the logic below by setting bounds values to their most
5826             permissive extremes if they aren't needed. */
5827              
5828 0 0         if ( max_width == 0 )
5829 0           max_width = DBL_MAX;
5830              
5831 0 0         if ( max_nelem == 0 )
5832 0           max_nelem = LONG_MAX;
5833              
5834 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
5835              
5836 0           PDL_Indx curind = 0; /* index of current bin */
5837              
5838             /* Declare bin counters */
5839 0           int rc = 0; /* status */
5840 0           double bsignal = 0; /* sum of signal */
5841 0           double bmean = 0; /* mean of (weighted) signal */
5842 0           double bwidth = 0; /* width (if applicable) */
5843 0           double bsnr = 0; /* SNR */
5844 0           double berror2 = 0; /* sum of error^2 */
5845 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
5846 0           double bm2 = 0; /* unnormalized variance */
5847 0           double bsignal2 = 0; /* sum of signal^2 */
5848 0           double bweight = 0; /* sum of 1/error*2 */
5849 0           double bweight_sig = 0; /* sum of weight * signal */
5850 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
5851 0           int bad_error = 0; /* if error2 is not good */
5852 0           int lastrc = 0; /* carryover status from previous loop */
5853 0           PDL_Indx nin = 0; /* number of elements in the current bin */
5854              
5855 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
5856              
5857 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
5858             double error;
5859             double error2;
5860             double width;
5861              
5862 0           int snr_ok = 0;
5863              
5864 0 0         if ( have_error ) {
5865 0           error = (error_datap)[0+(__inc_error_n*(n))];
5866 0           error2 = error * error;
5867             }
5868              
5869 0 0         if ( have_width )
5870 0           width = (width_datap)[0+(__inc_width_n*(n))];
5871              
5872             #ifdef PDL_BAD_CODE
5873             if ( PDL_ISBAD2(signal,signal_badval,B,signal_badval_isnan)
5874             ||
5875             have_error && PDL_ISBAD2(error,error_badval,B,error_badval_isnan) ) {
5876             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
5877             continue;
5878             }
5879             #endif /* PDL_BAD_CODE */
5880 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
5881              
5882            
5883 0           bsignal += signal;
5884 0           nin += 1;
5885              
5886             /* calculate error */
5887 0 0         if ( error_sdev ) {
5888              
5889             /* weighted standard deviation */
5890 0 0         if ( have_error ) {
5891            
5892             /* incremental algorithm for possibly weighted standard deviation; see
5893             https: */
5894 0           double _weight = 1 / ( error * error );
5895 0           double _sum_weight = bweight += _weight;
5896 0           double delta = signal - bmean;
5897 0           bmean += delta * _weight / _sum_weight;
5898 0           bm2 += _weight * delta * ( signal - bmean );
5899              
5900 0           bad_error = nin <= 1;
5901 0           berror = bad_error
5902             ? DBL_MAX
5903 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
5904            
5905             }
5906              
5907             else {
5908            
5909             /* incremental algorithm for possibly weighted standard deviation; see
5910             https: */
5911 0           double _weight = 1;
5912 0           double _sum_weight = nin;
5913 0           double delta = signal - bmean;
5914 0           bmean += delta * _weight / _sum_weight;
5915 0           bm2 += _weight * delta * ( signal - bmean );
5916              
5917 0           bad_error = nin <= 1;
5918 0           berror = bad_error
5919             ? DBL_MAX
5920 0 0         : sqrt( bm2 * 1 / (nin - 1) );
5921            
5922             }
5923             }
5924              
5925 0 0         else if ( error_rss ) {
5926            
5927             /* incremental algorithm for mean; see
5928             https: */
5929 0           double _error2 = error2;
5930 0           double _weight = 1 / ( error * error );
5931 0           double delta = signal - bmean;
5932              
5933 0           bweight += _weight;
5934 0           bmean += delta * _weight / bweight;
5935 0           berror2 += _error2;
5936 0           berror = sqrt( berror2 );
5937            
5938             }
5939              
5940 0 0         else if ( error_poisson ) {
5941 0           berror = sqrt( nin );
5942 0           bmean = bsignal / nin;
5943             }
5944              
5945             else {
5946 0           croak( "internal error" );
5947             }
5948            
5949              
5950 0           bsnr = bsignal / berror;
5951 0           snr_ok = bsnr >= min_snr;
5952              
5953 0 0         if ( have_width )
5954 0           bwidth += width;
5955              
5956 0 0         if ( nin == 1 )
5957 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
5958              
5959            
5960 0           rc |= \
5961 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
5962 0           | \
5963 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
5964 0           | \
5965             ( nin >= min_nelem \
5966 0 0         && bwidth >= min_width \
5967 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
5968             ; \
5969             ;
5970              
5971 0 0         if ( rc ) {
5972 0           rc |= lastrc;
5973              
5974            
5975 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
5976 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
5977 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
5978 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
5979            
5980 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
5981            
5982 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
5983 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
5984 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
5985 0 0         if ( error_sdev ) {
5986 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
5987 0 0         if ( have_error ) {
5988 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5989 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
5990 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
5991             }
5992             else {
5993 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
5994             }
5995             }
5996 0 0         else if ( error_rss ) {
5997 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
5998 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
5999             }
6000            
6001              
6002 0           curind++;
6003              
6004             /* Reset bin counters */
6005 0           rc = 0;
6006 0           bsignal = 0;
6007 0           bmean = 0;
6008 0           bwidth = 0;
6009 0           bsnr = 0;
6010 0           berror2 = 0;
6011 0           berror = 0;
6012 0           bm2 = 0;
6013 0           bsignal2 = 0;
6014 0           bweight = 0;
6015 0           bweight_sig = 0;
6016 0           bweight_sig2 = 0;
6017 0           bad_error = 0;
6018 0           lastrc = 0;
6019 0           nin = 0;
6020              
6021             }
6022              
6023 0 0         else if ( snr_ok ) {
6024 0           lastrc = BIN_RC_GTMINSN;
6025             }
6026              
6027             else {
6028 0           lastrc = 0;
6029             }
6030             }} /* Close n */
6031              
6032              
6033             /* record last bin if it's not empty */
6034 0 0         if ( nin ) {
6035              
6036             /* needed for SET_RESULTS */
6037 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
6038              
6039 0           rc = 0;
6040 0           bad_error = 0;
6041              
6042             /* a non empty bin means that we didn't meet constraints. fold it into
6043             the previous bin if requested & possible. sometimes that will
6044             actually lower the S/N of the previous bin; keep going until
6045             we can't fold anymore or we get the proper S/N
6046             */
6047 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
6048              
6049              
6050 0 0         while ( --curind > 0 ) {
6051             double tmp;
6052 0           int snr_ok = 0;
6053 0           PDL_Indx nin_last = nin;
6054              
6055            
6056 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
6057 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
6058              
6059             /* calculate error */
6060 0 0         if ( error_sdev ) {
6061              
6062             /* weighted standard deviation */
6063 0 0         if ( have_error ) {
6064            
6065             /* parallel algorithm (as we're adding bins together) for
6066             (possibly) weighted standard deviation; see
6067             https: */
6068 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6069 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6070 0           double mean_b = bmean;
6071 0           double weight_a = _weight;
6072 0           double weight_b = bweight;
6073 0           double weight_x = bweight += weight_a;
6074 0           double delta = mean_b - mean_a;
6075 0           bmean = mean_a + delta * weight_b / weight_x;
6076 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
6077 0           + delta * delta * weight_a * weight_b / weight_x;
6078              
6079 0           bad_error = nin <= 1;
6080 0           berror = bad_error
6081             ? DBL_MAX
6082 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
6083            
6084             }
6085              
6086             else {
6087            
6088             /* parallel algorithm (as we're adding bins together) for
6089             (possibly) weighted standard deviation; see
6090             https: */
6091 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6092 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6093 0           double mean_b = bmean;
6094 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
6095 0           double weight_b = nin_last;
6096 0           double weight_x = nin;
6097 0           double delta = mean_b - mean_a;
6098 0           bmean = mean_a + delta * weight_b / weight_x;
6099 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
6100 0           + delta * delta * weight_a * weight_b / weight_x;
6101              
6102 0           bad_error = nin <= 1;
6103 0           berror = bad_error
6104             ? DBL_MAX
6105 0 0         : sqrt( bm2 * 1 / (nin - 1) );
6106            
6107             }
6108             }
6109              
6110 0 0         else if ( error_rss ) {
6111            
6112             /* parallel algorithm (as we're adding bins together) for
6113             mean; see
6114             https: */
6115 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
6116 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6117 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6118 0           double mean_b = bmean;
6119 0           double weight_a = _weight;
6120 0           double weight_b = bweight;
6121 0           double weight_x = bweight += weight_a;
6122 0           double delta = mean_b - mean_a;
6123 0           bmean = mean_a + delta * weight_b / weight_x;
6124              
6125 0           berror2 += _error2;
6126 0           berror = sqrt( berror2 );
6127            
6128             }
6129              
6130 0 0         else if ( error_poisson ) {
6131 0           berror = sqrt( nin );
6132 0           bmean = bsignal / nin;
6133             }
6134              
6135             else {
6136 0           croak( "internal error" );
6137             }
6138            
6139              
6140 0           bsnr = bsignal / berror;
6141 0           snr_ok = bsnr >= min_snr;
6142              
6143            
6144 0           rc |= \
6145 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
6146 0           | \
6147 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
6148 0           | \
6149             ( nin >= min_nelem \
6150 0 0         && bwidth >= min_width \
6151 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
6152             ; \
6153             ;
6154              
6155 0 0         if (rc)
6156 0           break;
6157             }
6158              
6159             /* fix up index for events initially stuck in folded bins */
6160 0           PDL_Indx curind1 = curind+1;
6161             PDL_Indx ni;
6162              
6163 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
6164 0 0         ni < __privtrans->ind_sizes[0] ;
6165 0           ni++ ) {
6166             #ifdef PDL_BAD_CODE
6167             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
6168             #endif /* PDL_BAD_CODE */
6169 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
6170             }
6171 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6172 0           rc |= BIN_RC_FOLDED;
6173             }
6174              
6175            
6176 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
6177 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
6178 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
6179 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
6180            
6181 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
6182            
6183 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
6184 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
6185 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6186 0 0         if ( error_sdev ) {
6187 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
6188 0 0         if ( have_error ) {
6189 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6190 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
6191 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
6192             }
6193             else {
6194 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
6195             }
6196             }
6197 0 0         else if ( error_rss ) {
6198 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
6199 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6200             }
6201            
6202             }
6203             /* adjust for possibility of last bin being empty */
6204 0           (nbins_datap)[0] = curind + ( nin != 0 );
6205 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
6206             }
6207 0           } break;
6208 0           case PDL_S: {
6209 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Short,S,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
6210             {
6211              
6212              
6213              
6214              
6215 0           int flags = __params->optflags;
6216              
6217 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
6218 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
6219 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
6220 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
6221 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
6222 0           int fold_last_bin = flags & BIN_ARG_FOLD;
6223 0           int set_bad = flags & BIN_ARG_SET_BAD;
6224              
6225 0           PDL_Indx min_nelem = __params->min_nelem;
6226 0           PDL_Indx max_nelem = __params->max_nelem;
6227 0           double max_width = __params->max_width;
6228 0           double min_width = __params->min_width;
6229 0           double min_snr = __params->min_snr;
6230              
6231             /* simplify the logic below by setting bounds values to their most
6232             permissive extremes if they aren't needed. */
6233              
6234 0 0         if ( max_width == 0 )
6235 0           max_width = DBL_MAX;
6236              
6237 0 0         if ( max_nelem == 0 )
6238 0           max_nelem = LONG_MAX;
6239              
6240 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
6241              
6242 0           PDL_Indx curind = 0; /* index of current bin */
6243              
6244             /* Declare bin counters */
6245 0           int rc = 0; /* status */
6246 0           double bsignal = 0; /* sum of signal */
6247 0           double bmean = 0; /* mean of (weighted) signal */
6248 0           double bwidth = 0; /* width (if applicable) */
6249 0           double bsnr = 0; /* SNR */
6250 0           double berror2 = 0; /* sum of error^2 */
6251 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
6252 0           double bm2 = 0; /* unnormalized variance */
6253 0           double bsignal2 = 0; /* sum of signal^2 */
6254 0           double bweight = 0; /* sum of 1/error*2 */
6255 0           double bweight_sig = 0; /* sum of weight * signal */
6256 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
6257 0           int bad_error = 0; /* if error2 is not good */
6258 0           int lastrc = 0; /* carryover status from previous loop */
6259 0           PDL_Indx nin = 0; /* number of elements in the current bin */
6260              
6261 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
6262              
6263 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
6264             double error;
6265             double error2;
6266             double width;
6267              
6268 0           int snr_ok = 0;
6269              
6270 0 0         if ( have_error ) {
6271 0           error = (error_datap)[0+(__inc_error_n*(n))];
6272 0           error2 = error * error;
6273             }
6274              
6275 0 0         if ( have_width )
6276 0           width = (width_datap)[0+(__inc_width_n*(n))];
6277              
6278             #ifdef PDL_BAD_CODE
6279             if ( PDL_ISBAD2(signal,signal_badval,S,signal_badval_isnan)
6280             ||
6281             have_error && PDL_ISBAD2(error,error_badval,S,error_badval_isnan) ) {
6282             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
6283             continue;
6284             }
6285             #endif /* PDL_BAD_CODE */
6286 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
6287              
6288            
6289 0           bsignal += signal;
6290 0           nin += 1;
6291              
6292             /* calculate error */
6293 0 0         if ( error_sdev ) {
6294              
6295             /* weighted standard deviation */
6296 0 0         if ( have_error ) {
6297            
6298             /* incremental algorithm for possibly weighted standard deviation; see
6299             https: */
6300 0           double _weight = 1 / ( error * error );
6301 0           double _sum_weight = bweight += _weight;
6302 0           double delta = signal - bmean;
6303 0           bmean += delta * _weight / _sum_weight;
6304 0           bm2 += _weight * delta * ( signal - bmean );
6305              
6306 0           bad_error = nin <= 1;
6307 0           berror = bad_error
6308             ? DBL_MAX
6309 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
6310            
6311             }
6312              
6313             else {
6314            
6315             /* incremental algorithm for possibly weighted standard deviation; see
6316             https: */
6317 0           double _weight = 1;
6318 0           double _sum_weight = nin;
6319 0           double delta = signal - bmean;
6320 0           bmean += delta * _weight / _sum_weight;
6321 0           bm2 += _weight * delta * ( signal - bmean );
6322              
6323 0           bad_error = nin <= 1;
6324 0           berror = bad_error
6325             ? DBL_MAX
6326 0 0         : sqrt( bm2 * 1 / (nin - 1) );
6327            
6328             }
6329             }
6330              
6331 0 0         else if ( error_rss ) {
6332            
6333             /* incremental algorithm for mean; see
6334             https: */
6335 0           double _error2 = error2;
6336 0           double _weight = 1 / ( error * error );
6337 0           double delta = signal - bmean;
6338              
6339 0           bweight += _weight;
6340 0           bmean += delta * _weight / bweight;
6341 0           berror2 += _error2;
6342 0           berror = sqrt( berror2 );
6343            
6344             }
6345              
6346 0 0         else if ( error_poisson ) {
6347 0           berror = sqrt( nin );
6348 0           bmean = bsignal / nin;
6349             }
6350              
6351             else {
6352 0           croak( "internal error" );
6353             }
6354            
6355              
6356 0           bsnr = bsignal / berror;
6357 0           snr_ok = bsnr >= min_snr;
6358              
6359 0 0         if ( have_width )
6360 0           bwidth += width;
6361              
6362 0 0         if ( nin == 1 )
6363 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
6364              
6365            
6366 0           rc |= \
6367 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
6368 0           | \
6369 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
6370 0           | \
6371             ( nin >= min_nelem \
6372 0 0         && bwidth >= min_width \
6373 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
6374             ; \
6375             ;
6376              
6377 0 0         if ( rc ) {
6378 0           rc |= lastrc;
6379              
6380            
6381 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
6382 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
6383 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
6384 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
6385            
6386 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
6387            
6388 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
6389 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
6390 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6391 0 0         if ( error_sdev ) {
6392 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
6393 0 0         if ( have_error ) {
6394 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6395 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
6396 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
6397             }
6398             else {
6399 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
6400             }
6401             }
6402 0 0         else if ( error_rss ) {
6403 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
6404 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6405             }
6406            
6407              
6408 0           curind++;
6409              
6410             /* Reset bin counters */
6411 0           rc = 0;
6412 0           bsignal = 0;
6413 0           bmean = 0;
6414 0           bwidth = 0;
6415 0           bsnr = 0;
6416 0           berror2 = 0;
6417 0           berror = 0;
6418 0           bm2 = 0;
6419 0           bsignal2 = 0;
6420 0           bweight = 0;
6421 0           bweight_sig = 0;
6422 0           bweight_sig2 = 0;
6423 0           bad_error = 0;
6424 0           lastrc = 0;
6425 0           nin = 0;
6426              
6427             }
6428              
6429 0 0         else if ( snr_ok ) {
6430 0           lastrc = BIN_RC_GTMINSN;
6431             }
6432              
6433             else {
6434 0           lastrc = 0;
6435             }
6436             }} /* Close n */
6437              
6438              
6439             /* record last bin if it's not empty */
6440 0 0         if ( nin ) {
6441              
6442             /* needed for SET_RESULTS */
6443 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
6444              
6445 0           rc = 0;
6446 0           bad_error = 0;
6447              
6448             /* a non empty bin means that we didn't meet constraints. fold it into
6449             the previous bin if requested & possible. sometimes that will
6450             actually lower the S/N of the previous bin; keep going until
6451             we can't fold anymore or we get the proper S/N
6452             */
6453 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
6454              
6455              
6456 0 0         while ( --curind > 0 ) {
6457             double tmp;
6458 0           int snr_ok = 0;
6459 0           PDL_Indx nin_last = nin;
6460              
6461            
6462 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
6463 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
6464              
6465             /* calculate error */
6466 0 0         if ( error_sdev ) {
6467              
6468             /* weighted standard deviation */
6469 0 0         if ( have_error ) {
6470            
6471             /* parallel algorithm (as we're adding bins together) for
6472             (possibly) weighted standard deviation; see
6473             https: */
6474 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6475 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6476 0           double mean_b = bmean;
6477 0           double weight_a = _weight;
6478 0           double weight_b = bweight;
6479 0           double weight_x = bweight += weight_a;
6480 0           double delta = mean_b - mean_a;
6481 0           bmean = mean_a + delta * weight_b / weight_x;
6482 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
6483 0           + delta * delta * weight_a * weight_b / weight_x;
6484              
6485 0           bad_error = nin <= 1;
6486 0           berror = bad_error
6487             ? DBL_MAX
6488 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
6489            
6490             }
6491              
6492             else {
6493            
6494             /* parallel algorithm (as we're adding bins together) for
6495             (possibly) weighted standard deviation; see
6496             https: */
6497 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6498 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6499 0           double mean_b = bmean;
6500 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
6501 0           double weight_b = nin_last;
6502 0           double weight_x = nin;
6503 0           double delta = mean_b - mean_a;
6504 0           bmean = mean_a + delta * weight_b / weight_x;
6505 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
6506 0           + delta * delta * weight_a * weight_b / weight_x;
6507              
6508 0           bad_error = nin <= 1;
6509 0           berror = bad_error
6510             ? DBL_MAX
6511 0 0         : sqrt( bm2 * 1 / (nin - 1) );
6512            
6513             }
6514             }
6515              
6516 0 0         else if ( error_rss ) {
6517            
6518             /* parallel algorithm (as we're adding bins together) for
6519             mean; see
6520             https: */
6521 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
6522 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6523 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6524 0           double mean_b = bmean;
6525 0           double weight_a = _weight;
6526 0           double weight_b = bweight;
6527 0           double weight_x = bweight += weight_a;
6528 0           double delta = mean_b - mean_a;
6529 0           bmean = mean_a + delta * weight_b / weight_x;
6530              
6531 0           berror2 += _error2;
6532 0           berror = sqrt( berror2 );
6533            
6534             }
6535              
6536 0 0         else if ( error_poisson ) {
6537 0           berror = sqrt( nin );
6538 0           bmean = bsignal / nin;
6539             }
6540              
6541             else {
6542 0           croak( "internal error" );
6543             }
6544            
6545              
6546 0           bsnr = bsignal / berror;
6547 0           snr_ok = bsnr >= min_snr;
6548              
6549            
6550 0           rc |= \
6551 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
6552 0           | \
6553 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
6554 0           | \
6555             ( nin >= min_nelem \
6556 0 0         && bwidth >= min_width \
6557 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
6558             ; \
6559             ;
6560              
6561 0 0         if (rc)
6562 0           break;
6563             }
6564              
6565             /* fix up index for events initially stuck in folded bins */
6566 0           PDL_Indx curind1 = curind+1;
6567             PDL_Indx ni;
6568              
6569 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
6570 0 0         ni < __privtrans->ind_sizes[0] ;
6571 0           ni++ ) {
6572             #ifdef PDL_BAD_CODE
6573             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
6574             #endif /* PDL_BAD_CODE */
6575 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
6576             }
6577 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6578 0           rc |= BIN_RC_FOLDED;
6579             }
6580              
6581            
6582 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
6583 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
6584 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
6585 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
6586            
6587 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
6588            
6589 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
6590 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
6591 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6592 0 0         if ( error_sdev ) {
6593 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
6594 0 0         if ( have_error ) {
6595 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6596 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
6597 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
6598             }
6599             else {
6600 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
6601             }
6602             }
6603 0 0         else if ( error_rss ) {
6604 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
6605 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6606             }
6607            
6608             }
6609             /* adjust for possibility of last bin being empty */
6610 0           (nbins_datap)[0] = curind + ( nin != 0 );
6611 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
6612             }
6613 0           } break;
6614 0           case PDL_US: {
6615 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Ushort,U,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
6616             {
6617              
6618              
6619              
6620              
6621 0           int flags = __params->optflags;
6622              
6623 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
6624 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
6625 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
6626 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
6627 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
6628 0           int fold_last_bin = flags & BIN_ARG_FOLD;
6629 0           int set_bad = flags & BIN_ARG_SET_BAD;
6630              
6631 0           PDL_Indx min_nelem = __params->min_nelem;
6632 0           PDL_Indx max_nelem = __params->max_nelem;
6633 0           double max_width = __params->max_width;
6634 0           double min_width = __params->min_width;
6635 0           double min_snr = __params->min_snr;
6636              
6637             /* simplify the logic below by setting bounds values to their most
6638             permissive extremes if they aren't needed. */
6639              
6640 0 0         if ( max_width == 0 )
6641 0           max_width = DBL_MAX;
6642              
6643 0 0         if ( max_nelem == 0 )
6644 0           max_nelem = LONG_MAX;
6645              
6646 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
6647              
6648 0           PDL_Indx curind = 0; /* index of current bin */
6649              
6650             /* Declare bin counters */
6651 0           int rc = 0; /* status */
6652 0           double bsignal = 0; /* sum of signal */
6653 0           double bmean = 0; /* mean of (weighted) signal */
6654 0           double bwidth = 0; /* width (if applicable) */
6655 0           double bsnr = 0; /* SNR */
6656 0           double berror2 = 0; /* sum of error^2 */
6657 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
6658 0           double bm2 = 0; /* unnormalized variance */
6659 0           double bsignal2 = 0; /* sum of signal^2 */
6660 0           double bweight = 0; /* sum of 1/error*2 */
6661 0           double bweight_sig = 0; /* sum of weight * signal */
6662 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
6663 0           int bad_error = 0; /* if error2 is not good */
6664 0           int lastrc = 0; /* carryover status from previous loop */
6665 0           PDL_Indx nin = 0; /* number of elements in the current bin */
6666              
6667 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
6668              
6669 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
6670             double error;
6671             double error2;
6672             double width;
6673              
6674 0           int snr_ok = 0;
6675              
6676 0 0         if ( have_error ) {
6677 0           error = (error_datap)[0+(__inc_error_n*(n))];
6678 0           error2 = error * error;
6679             }
6680              
6681 0 0         if ( have_width )
6682 0           width = (width_datap)[0+(__inc_width_n*(n))];
6683              
6684             #ifdef PDL_BAD_CODE
6685             if ( PDL_ISBAD2(signal,signal_badval,U,signal_badval_isnan)
6686             ||
6687             have_error && PDL_ISBAD2(error,error_badval,U,error_badval_isnan) ) {
6688             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
6689             continue;
6690             }
6691             #endif /* PDL_BAD_CODE */
6692 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
6693              
6694            
6695 0           bsignal += signal;
6696 0           nin += 1;
6697              
6698             /* calculate error */
6699 0 0         if ( error_sdev ) {
6700              
6701             /* weighted standard deviation */
6702 0 0         if ( have_error ) {
6703            
6704             /* incremental algorithm for possibly weighted standard deviation; see
6705             https: */
6706 0           double _weight = 1 / ( error * error );
6707 0           double _sum_weight = bweight += _weight;
6708 0           double delta = signal - bmean;
6709 0           bmean += delta * _weight / _sum_weight;
6710 0           bm2 += _weight * delta * ( signal - bmean );
6711              
6712 0           bad_error = nin <= 1;
6713 0           berror = bad_error
6714             ? DBL_MAX
6715 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
6716            
6717             }
6718              
6719             else {
6720            
6721             /* incremental algorithm for possibly weighted standard deviation; see
6722             https: */
6723 0           double _weight = 1;
6724 0           double _sum_weight = nin;
6725 0           double delta = signal - bmean;
6726 0           bmean += delta * _weight / _sum_weight;
6727 0           bm2 += _weight * delta * ( signal - bmean );
6728              
6729 0           bad_error = nin <= 1;
6730 0           berror = bad_error
6731             ? DBL_MAX
6732 0 0         : sqrt( bm2 * 1 / (nin - 1) );
6733            
6734             }
6735             }
6736              
6737 0 0         else if ( error_rss ) {
6738            
6739             /* incremental algorithm for mean; see
6740             https: */
6741 0           double _error2 = error2;
6742 0           double _weight = 1 / ( error * error );
6743 0           double delta = signal - bmean;
6744              
6745 0           bweight += _weight;
6746 0           bmean += delta * _weight / bweight;
6747 0           berror2 += _error2;
6748 0           berror = sqrt( berror2 );
6749            
6750             }
6751              
6752 0 0         else if ( error_poisson ) {
6753 0           berror = sqrt( nin );
6754 0           bmean = bsignal / nin;
6755             }
6756              
6757             else {
6758 0           croak( "internal error" );
6759             }
6760            
6761              
6762 0           bsnr = bsignal / berror;
6763 0           snr_ok = bsnr >= min_snr;
6764              
6765 0 0         if ( have_width )
6766 0           bwidth += width;
6767              
6768 0 0         if ( nin == 1 )
6769 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
6770              
6771            
6772 0           rc |= \
6773 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
6774 0           | \
6775 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
6776 0           | \
6777             ( nin >= min_nelem \
6778 0 0         && bwidth >= min_width \
6779 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
6780             ; \
6781             ;
6782              
6783 0 0         if ( rc ) {
6784 0           rc |= lastrc;
6785              
6786            
6787 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
6788 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
6789 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
6790 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
6791            
6792 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
6793            
6794 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
6795 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
6796 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6797 0 0         if ( error_sdev ) {
6798 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
6799 0 0         if ( have_error ) {
6800 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6801 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
6802 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
6803             }
6804             else {
6805 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
6806             }
6807             }
6808 0 0         else if ( error_rss ) {
6809 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
6810 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
6811             }
6812            
6813              
6814 0           curind++;
6815              
6816             /* Reset bin counters */
6817 0           rc = 0;
6818 0           bsignal = 0;
6819 0           bmean = 0;
6820 0           bwidth = 0;
6821 0           bsnr = 0;
6822 0           berror2 = 0;
6823 0           berror = 0;
6824 0           bm2 = 0;
6825 0           bsignal2 = 0;
6826 0           bweight = 0;
6827 0           bweight_sig = 0;
6828 0           bweight_sig2 = 0;
6829 0           bad_error = 0;
6830 0           lastrc = 0;
6831 0           nin = 0;
6832              
6833             }
6834              
6835 0 0         else if ( snr_ok ) {
6836 0           lastrc = BIN_RC_GTMINSN;
6837             }
6838              
6839             else {
6840 0           lastrc = 0;
6841             }
6842             }} /* Close n */
6843              
6844              
6845             /* record last bin if it's not empty */
6846 0 0         if ( nin ) {
6847              
6848             /* needed for SET_RESULTS */
6849 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
6850              
6851 0           rc = 0;
6852 0           bad_error = 0;
6853              
6854             /* a non empty bin means that we didn't meet constraints. fold it into
6855             the previous bin if requested & possible. sometimes that will
6856             actually lower the S/N of the previous bin; keep going until
6857             we can't fold anymore or we get the proper S/N
6858             */
6859 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
6860              
6861              
6862 0 0         while ( --curind > 0 ) {
6863             double tmp;
6864 0           int snr_ok = 0;
6865 0           PDL_Indx nin_last = nin;
6866              
6867            
6868 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
6869 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
6870              
6871             /* calculate error */
6872 0 0         if ( error_sdev ) {
6873              
6874             /* weighted standard deviation */
6875 0 0         if ( have_error ) {
6876            
6877             /* parallel algorithm (as we're adding bins together) for
6878             (possibly) weighted standard deviation; see
6879             https: */
6880 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6881 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6882 0           double mean_b = bmean;
6883 0           double weight_a = _weight;
6884 0           double weight_b = bweight;
6885 0           double weight_x = bweight += weight_a;
6886 0           double delta = mean_b - mean_a;
6887 0           bmean = mean_a + delta * weight_b / weight_x;
6888 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
6889 0           + delta * delta * weight_a * weight_b / weight_x;
6890              
6891 0           bad_error = nin <= 1;
6892 0           berror = bad_error
6893             ? DBL_MAX
6894 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
6895            
6896             }
6897              
6898             else {
6899            
6900             /* parallel algorithm (as we're adding bins together) for
6901             (possibly) weighted standard deviation; see
6902             https: */
6903 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6904 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6905 0           double mean_b = bmean;
6906 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
6907 0           double weight_b = nin_last;
6908 0           double weight_x = nin;
6909 0           double delta = mean_b - mean_a;
6910 0           bmean = mean_a + delta * weight_b / weight_x;
6911 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
6912 0           + delta * delta * weight_a * weight_b / weight_x;
6913              
6914 0           bad_error = nin <= 1;
6915 0           berror = bad_error
6916             ? DBL_MAX
6917 0 0         : sqrt( bm2 * 1 / (nin - 1) );
6918            
6919             }
6920             }
6921              
6922 0 0         else if ( error_rss ) {
6923            
6924             /* parallel algorithm (as we're adding bins together) for
6925             mean; see
6926             https: */
6927 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
6928 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
6929 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
6930 0           double mean_b = bmean;
6931 0           double weight_a = _weight;
6932 0           double weight_b = bweight;
6933 0           double weight_x = bweight += weight_a;
6934 0           double delta = mean_b - mean_a;
6935 0           bmean = mean_a + delta * weight_b / weight_x;
6936              
6937 0           berror2 += _error2;
6938 0           berror = sqrt( berror2 );
6939            
6940             }
6941              
6942 0 0         else if ( error_poisson ) {
6943 0           berror = sqrt( nin );
6944 0           bmean = bsignal / nin;
6945             }
6946              
6947             else {
6948 0           croak( "internal error" );
6949             }
6950            
6951              
6952 0           bsnr = bsignal / berror;
6953 0           snr_ok = bsnr >= min_snr;
6954              
6955            
6956 0           rc |= \
6957 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
6958 0           | \
6959 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
6960 0           | \
6961             ( nin >= min_nelem \
6962 0 0         && bwidth >= min_width \
6963 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
6964             ; \
6965             ;
6966              
6967 0 0         if (rc)
6968 0           break;
6969             }
6970              
6971             /* fix up index for events initially stuck in folded bins */
6972 0           PDL_Indx curind1 = curind+1;
6973             PDL_Indx ni;
6974              
6975 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
6976 0 0         ni < __privtrans->ind_sizes[0] ;
6977 0           ni++ ) {
6978             #ifdef PDL_BAD_CODE
6979             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
6980             #endif /* PDL_BAD_CODE */
6981 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
6982             }
6983 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6984 0           rc |= BIN_RC_FOLDED;
6985             }
6986              
6987            
6988 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
6989 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
6990 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
6991 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
6992            
6993 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
6994            
6995 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
6996 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
6997 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
6998 0 0         if ( error_sdev ) {
6999 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
7000 0 0         if ( have_error ) {
7001 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7002 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
7003 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
7004             }
7005             else {
7006 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
7007             }
7008             }
7009 0 0         else if ( error_rss ) {
7010 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
7011 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7012             }
7013            
7014             }
7015             /* adjust for possibility of last bin being empty */
7016 0           (nbins_datap)[0] = curind + ( nin != 0 );
7017 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
7018             }
7019 0           } break;
7020 0           case PDL_L: {
7021 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Long,L,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
7022             {
7023              
7024              
7025              
7026              
7027 0           int flags = __params->optflags;
7028              
7029 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
7030 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
7031 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
7032 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
7033 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
7034 0           int fold_last_bin = flags & BIN_ARG_FOLD;
7035 0           int set_bad = flags & BIN_ARG_SET_BAD;
7036              
7037 0           PDL_Indx min_nelem = __params->min_nelem;
7038 0           PDL_Indx max_nelem = __params->max_nelem;
7039 0           double max_width = __params->max_width;
7040 0           double min_width = __params->min_width;
7041 0           double min_snr = __params->min_snr;
7042              
7043             /* simplify the logic below by setting bounds values to their most
7044             permissive extremes if they aren't needed. */
7045              
7046 0 0         if ( max_width == 0 )
7047 0           max_width = DBL_MAX;
7048              
7049 0 0         if ( max_nelem == 0 )
7050 0           max_nelem = LONG_MAX;
7051              
7052 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
7053              
7054 0           PDL_Indx curind = 0; /* index of current bin */
7055              
7056             /* Declare bin counters */
7057 0           int rc = 0; /* status */
7058 0           double bsignal = 0; /* sum of signal */
7059 0           double bmean = 0; /* mean of (weighted) signal */
7060 0           double bwidth = 0; /* width (if applicable) */
7061 0           double bsnr = 0; /* SNR */
7062 0           double berror2 = 0; /* sum of error^2 */
7063 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
7064 0           double bm2 = 0; /* unnormalized variance */
7065 0           double bsignal2 = 0; /* sum of signal^2 */
7066 0           double bweight = 0; /* sum of 1/error*2 */
7067 0           double bweight_sig = 0; /* sum of weight * signal */
7068 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
7069 0           int bad_error = 0; /* if error2 is not good */
7070 0           int lastrc = 0; /* carryover status from previous loop */
7071 0           PDL_Indx nin = 0; /* number of elements in the current bin */
7072              
7073 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
7074              
7075 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
7076             double error;
7077             double error2;
7078             double width;
7079              
7080 0           int snr_ok = 0;
7081              
7082 0 0         if ( have_error ) {
7083 0           error = (error_datap)[0+(__inc_error_n*(n))];
7084 0           error2 = error * error;
7085             }
7086              
7087 0 0         if ( have_width )
7088 0           width = (width_datap)[0+(__inc_width_n*(n))];
7089              
7090             #ifdef PDL_BAD_CODE
7091             if ( PDL_ISBAD2(signal,signal_badval,L,signal_badval_isnan)
7092             ||
7093             have_error && PDL_ISBAD2(error,error_badval,L,error_badval_isnan) ) {
7094             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
7095             continue;
7096             }
7097             #endif /* PDL_BAD_CODE */
7098 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
7099              
7100            
7101 0           bsignal += signal;
7102 0           nin += 1;
7103              
7104             /* calculate error */
7105 0 0         if ( error_sdev ) {
7106              
7107             /* weighted standard deviation */
7108 0 0         if ( have_error ) {
7109            
7110             /* incremental algorithm for possibly weighted standard deviation; see
7111             https: */
7112 0           double _weight = 1 / ( error * error );
7113 0           double _sum_weight = bweight += _weight;
7114 0           double delta = signal - bmean;
7115 0           bmean += delta * _weight / _sum_weight;
7116 0           bm2 += _weight * delta * ( signal - bmean );
7117              
7118 0           bad_error = nin <= 1;
7119 0           berror = bad_error
7120             ? DBL_MAX
7121 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
7122            
7123             }
7124              
7125             else {
7126            
7127             /* incremental algorithm for possibly weighted standard deviation; see
7128             https: */
7129 0           double _weight = 1;
7130 0           double _sum_weight = nin;
7131 0           double delta = signal - bmean;
7132 0           bmean += delta * _weight / _sum_weight;
7133 0           bm2 += _weight * delta * ( signal - bmean );
7134              
7135 0           bad_error = nin <= 1;
7136 0           berror = bad_error
7137             ? DBL_MAX
7138 0 0         : sqrt( bm2 * 1 / (nin - 1) );
7139            
7140             }
7141             }
7142              
7143 0 0         else if ( error_rss ) {
7144            
7145             /* incremental algorithm for mean; see
7146             https: */
7147 0           double _error2 = error2;
7148 0           double _weight = 1 / ( error * error );
7149 0           double delta = signal - bmean;
7150              
7151 0           bweight += _weight;
7152 0           bmean += delta * _weight / bweight;
7153 0           berror2 += _error2;
7154 0           berror = sqrt( berror2 );
7155            
7156             }
7157              
7158 0 0         else if ( error_poisson ) {
7159 0           berror = sqrt( nin );
7160 0           bmean = bsignal / nin;
7161             }
7162              
7163             else {
7164 0           croak( "internal error" );
7165             }
7166            
7167              
7168 0           bsnr = bsignal / berror;
7169 0           snr_ok = bsnr >= min_snr;
7170              
7171 0 0         if ( have_width )
7172 0           bwidth += width;
7173              
7174 0 0         if ( nin == 1 )
7175 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
7176              
7177            
7178 0           rc |= \
7179 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
7180 0           | \
7181 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
7182 0           | \
7183             ( nin >= min_nelem \
7184 0 0         && bwidth >= min_width \
7185 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
7186             ; \
7187             ;
7188              
7189 0 0         if ( rc ) {
7190 0           rc |= lastrc;
7191              
7192            
7193 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
7194 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
7195 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
7196 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
7197            
7198 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
7199            
7200 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
7201 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
7202 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
7203 0 0         if ( error_sdev ) {
7204 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
7205 0 0         if ( have_error ) {
7206 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7207 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
7208 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
7209             }
7210             else {
7211 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
7212             }
7213             }
7214 0 0         else if ( error_rss ) {
7215 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
7216 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7217             }
7218            
7219              
7220 0           curind++;
7221              
7222             /* Reset bin counters */
7223 0           rc = 0;
7224 0           bsignal = 0;
7225 0           bmean = 0;
7226 0           bwidth = 0;
7227 0           bsnr = 0;
7228 0           berror2 = 0;
7229 0           berror = 0;
7230 0           bm2 = 0;
7231 0           bsignal2 = 0;
7232 0           bweight = 0;
7233 0           bweight_sig = 0;
7234 0           bweight_sig2 = 0;
7235 0           bad_error = 0;
7236 0           lastrc = 0;
7237 0           nin = 0;
7238              
7239             }
7240              
7241 0 0         else if ( snr_ok ) {
7242 0           lastrc = BIN_RC_GTMINSN;
7243             }
7244              
7245             else {
7246 0           lastrc = 0;
7247             }
7248             }} /* Close n */
7249              
7250              
7251             /* record last bin if it's not empty */
7252 0 0         if ( nin ) {
7253              
7254             /* needed for SET_RESULTS */
7255 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
7256              
7257 0           rc = 0;
7258 0           bad_error = 0;
7259              
7260             /* a non empty bin means that we didn't meet constraints. fold it into
7261             the previous bin if requested & possible. sometimes that will
7262             actually lower the S/N of the previous bin; keep going until
7263             we can't fold anymore or we get the proper S/N
7264             */
7265 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
7266              
7267              
7268 0 0         while ( --curind > 0 ) {
7269             double tmp;
7270 0           int snr_ok = 0;
7271 0           PDL_Indx nin_last = nin;
7272              
7273            
7274 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
7275 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
7276              
7277             /* calculate error */
7278 0 0         if ( error_sdev ) {
7279              
7280             /* weighted standard deviation */
7281 0 0         if ( have_error ) {
7282            
7283             /* parallel algorithm (as we're adding bins together) for
7284             (possibly) weighted standard deviation; see
7285             https: */
7286 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
7287 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
7288 0           double mean_b = bmean;
7289 0           double weight_a = _weight;
7290 0           double weight_b = bweight;
7291 0           double weight_x = bweight += weight_a;
7292 0           double delta = mean_b - mean_a;
7293 0           bmean = mean_a + delta * weight_b / weight_x;
7294 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
7295 0           + delta * delta * weight_a * weight_b / weight_x;
7296              
7297 0           bad_error = nin <= 1;
7298 0           berror = bad_error
7299             ? DBL_MAX
7300 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
7301            
7302             }
7303              
7304             else {
7305            
7306             /* parallel algorithm (as we're adding bins together) for
7307             (possibly) weighted standard deviation; see
7308             https: */
7309 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
7310 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
7311 0           double mean_b = bmean;
7312 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
7313 0           double weight_b = nin_last;
7314 0           double weight_x = nin;
7315 0           double delta = mean_b - mean_a;
7316 0           bmean = mean_a + delta * weight_b / weight_x;
7317 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
7318 0           + delta * delta * weight_a * weight_b / weight_x;
7319              
7320 0           bad_error = nin <= 1;
7321 0           berror = bad_error
7322             ? DBL_MAX
7323 0 0         : sqrt( bm2 * 1 / (nin - 1) );
7324            
7325             }
7326             }
7327              
7328 0 0         else if ( error_rss ) {
7329            
7330             /* parallel algorithm (as we're adding bins together) for
7331             mean; see
7332             https: */
7333 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
7334 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
7335 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
7336 0           double mean_b = bmean;
7337 0           double weight_a = _weight;
7338 0           double weight_b = bweight;
7339 0           double weight_x = bweight += weight_a;
7340 0           double delta = mean_b - mean_a;
7341 0           bmean = mean_a + delta * weight_b / weight_x;
7342              
7343 0           berror2 += _error2;
7344 0           berror = sqrt( berror2 );
7345            
7346             }
7347              
7348 0 0         else if ( error_poisson ) {
7349 0           berror = sqrt( nin );
7350 0           bmean = bsignal / nin;
7351             }
7352              
7353             else {
7354 0           croak( "internal error" );
7355             }
7356            
7357              
7358 0           bsnr = bsignal / berror;
7359 0           snr_ok = bsnr >= min_snr;
7360              
7361            
7362 0           rc |= \
7363 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
7364 0           | \
7365 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
7366 0           | \
7367             ( nin >= min_nelem \
7368 0 0         && bwidth >= min_width \
7369 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
7370             ; \
7371             ;
7372              
7373 0 0         if (rc)
7374 0           break;
7375             }
7376              
7377             /* fix up index for events initially stuck in folded bins */
7378 0           PDL_Indx curind1 = curind+1;
7379             PDL_Indx ni;
7380              
7381 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
7382 0 0         ni < __privtrans->ind_sizes[0] ;
7383 0           ni++ ) {
7384             #ifdef PDL_BAD_CODE
7385             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
7386             #endif /* PDL_BAD_CODE */
7387 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
7388             }
7389 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
7390 0           rc |= BIN_RC_FOLDED;
7391             }
7392              
7393            
7394 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
7395 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
7396 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
7397 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
7398            
7399 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
7400            
7401 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
7402 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
7403 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
7404 0 0         if ( error_sdev ) {
7405 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
7406 0 0         if ( have_error ) {
7407 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7408 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
7409 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
7410             }
7411             else {
7412 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
7413             }
7414             }
7415 0 0         else if ( error_rss ) {
7416 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
7417 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7418             }
7419            
7420             }
7421             /* adjust for possibility of last bin being empty */
7422 0           (nbins_datap)[0] = curind + ( nin != 0 );
7423 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
7424             }
7425 0           } break;
7426 0           case PDL_UL: {
7427 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_ULong,K,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
7428             {
7429              
7430              
7431              
7432              
7433 0           int flags = __params->optflags;
7434              
7435 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
7436 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
7437 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
7438 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
7439 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
7440 0           int fold_last_bin = flags & BIN_ARG_FOLD;
7441 0           int set_bad = flags & BIN_ARG_SET_BAD;
7442              
7443 0           PDL_Indx min_nelem = __params->min_nelem;
7444 0           PDL_Indx max_nelem = __params->max_nelem;
7445 0           double max_width = __params->max_width;
7446 0           double min_width = __params->min_width;
7447 0           double min_snr = __params->min_snr;
7448              
7449             /* simplify the logic below by setting bounds values to their most
7450             permissive extremes if they aren't needed. */
7451              
7452 0 0         if ( max_width == 0 )
7453 0           max_width = DBL_MAX;
7454              
7455 0 0         if ( max_nelem == 0 )
7456 0           max_nelem = LONG_MAX;
7457              
7458 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
7459              
7460 0           PDL_Indx curind = 0; /* index of current bin */
7461              
7462             /* Declare bin counters */
7463 0           int rc = 0; /* status */
7464 0           double bsignal = 0; /* sum of signal */
7465 0           double bmean = 0; /* mean of (weighted) signal */
7466 0           double bwidth = 0; /* width (if applicable) */
7467 0           double bsnr = 0; /* SNR */
7468 0           double berror2 = 0; /* sum of error^2 */
7469 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
7470 0           double bm2 = 0; /* unnormalized variance */
7471 0           double bsignal2 = 0; /* sum of signal^2 */
7472 0           double bweight = 0; /* sum of 1/error*2 */
7473 0           double bweight_sig = 0; /* sum of weight * signal */
7474 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
7475 0           int bad_error = 0; /* if error2 is not good */
7476 0           int lastrc = 0; /* carryover status from previous loop */
7477 0           PDL_Indx nin = 0; /* number of elements in the current bin */
7478              
7479 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
7480              
7481 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
7482             double error;
7483             double error2;
7484             double width;
7485              
7486 0           int snr_ok = 0;
7487              
7488 0 0         if ( have_error ) {
7489 0           error = (error_datap)[0+(__inc_error_n*(n))];
7490 0           error2 = error * error;
7491             }
7492              
7493 0 0         if ( have_width )
7494 0           width = (width_datap)[0+(__inc_width_n*(n))];
7495              
7496             #ifdef PDL_BAD_CODE
7497             if ( PDL_ISBAD2(signal,signal_badval,K,signal_badval_isnan)
7498             ||
7499             have_error && PDL_ISBAD2(error,error_badval,K,error_badval_isnan) ) {
7500             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
7501             continue;
7502             }
7503             #endif /* PDL_BAD_CODE */
7504 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
7505              
7506            
7507 0           bsignal += signal;
7508 0           nin += 1;
7509              
7510             /* calculate error */
7511 0 0         if ( error_sdev ) {
7512              
7513             /* weighted standard deviation */
7514 0 0         if ( have_error ) {
7515            
7516             /* incremental algorithm for possibly weighted standard deviation; see
7517             https: */
7518 0           double _weight = 1 / ( error * error );
7519 0           double _sum_weight = bweight += _weight;
7520 0           double delta = signal - bmean;
7521 0           bmean += delta * _weight / _sum_weight;
7522 0           bm2 += _weight * delta * ( signal - bmean );
7523              
7524 0           bad_error = nin <= 1;
7525 0           berror = bad_error
7526             ? DBL_MAX
7527 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
7528            
7529             }
7530              
7531             else {
7532            
7533             /* incremental algorithm for possibly weighted standard deviation; see
7534             https: */
7535 0           double _weight = 1;
7536 0           double _sum_weight = nin;
7537 0           double delta = signal - bmean;
7538 0           bmean += delta * _weight / _sum_weight;
7539 0           bm2 += _weight * delta * ( signal - bmean );
7540              
7541 0           bad_error = nin <= 1;
7542 0           berror = bad_error
7543             ? DBL_MAX
7544 0 0         : sqrt( bm2 * 1 / (nin - 1) );
7545            
7546             }
7547             }
7548              
7549 0 0         else if ( error_rss ) {
7550            
7551             /* incremental algorithm for mean; see
7552             https: */
7553 0           double _error2 = error2;
7554 0           double _weight = 1 / ( error * error );
7555 0           double delta = signal - bmean;
7556              
7557 0           bweight += _weight;
7558 0           bmean += delta * _weight / bweight;
7559 0           berror2 += _error2;
7560 0           berror = sqrt( berror2 );
7561            
7562             }
7563              
7564 0 0         else if ( error_poisson ) {
7565 0           berror = sqrt( nin );
7566 0           bmean = bsignal / nin;
7567             }
7568              
7569             else {
7570 0           croak( "internal error" );
7571             }
7572            
7573              
7574 0           bsnr = bsignal / berror;
7575 0           snr_ok = bsnr >= min_snr;
7576              
7577 0 0         if ( have_width )
7578 0           bwidth += width;
7579              
7580 0 0         if ( nin == 1 )
7581 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
7582              
7583            
7584 0           rc |= \
7585 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
7586 0           | \
7587 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
7588 0           | \
7589             ( nin >= min_nelem \
7590 0 0         && bwidth >= min_width \
7591 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
7592             ; \
7593             ;
7594              
7595 0 0         if ( rc ) {
7596 0           rc |= lastrc;
7597              
7598            
7599 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
7600 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
7601 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
7602 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
7603            
7604 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
7605            
7606 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
7607 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
7608 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
7609 0 0         if ( error_sdev ) {
7610 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
7611 0 0         if ( have_error ) {
7612 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7613 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
7614 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
7615             }
7616             else {
7617 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
7618             }
7619             }
7620 0 0         else if ( error_rss ) {
7621 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
7622 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7623             }
7624            
7625              
7626 0           curind++;
7627              
7628             /* Reset bin counters */
7629 0           rc = 0;
7630 0           bsignal = 0;
7631 0           bmean = 0;
7632 0           bwidth = 0;
7633 0           bsnr = 0;
7634 0           berror2 = 0;
7635 0           berror = 0;
7636 0           bm2 = 0;
7637 0           bsignal2 = 0;
7638 0           bweight = 0;
7639 0           bweight_sig = 0;
7640 0           bweight_sig2 = 0;
7641 0           bad_error = 0;
7642 0           lastrc = 0;
7643 0           nin = 0;
7644              
7645             }
7646              
7647 0 0         else if ( snr_ok ) {
7648 0           lastrc = BIN_RC_GTMINSN;
7649             }
7650              
7651             else {
7652 0           lastrc = 0;
7653             }
7654             }} /* Close n */
7655              
7656              
7657             /* record last bin if it's not empty */
7658 0 0         if ( nin ) {
7659              
7660             /* needed for SET_RESULTS */
7661 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
7662              
7663 0           rc = 0;
7664 0           bad_error = 0;
7665              
7666             /* a non empty bin means that we didn't meet constraints. fold it into
7667             the previous bin if requested & possible. sometimes that will
7668             actually lower the S/N of the previous bin; keep going until
7669             we can't fold anymore or we get the proper S/N
7670             */
7671 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
7672              
7673              
7674 0 0         while ( --curind > 0 ) {
7675             double tmp;
7676 0           int snr_ok = 0;
7677 0           PDL_Indx nin_last = nin;
7678              
7679            
7680 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
7681 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
7682              
7683             /* calculate error */
7684 0 0         if ( error_sdev ) {
7685              
7686             /* weighted standard deviation */
7687 0 0         if ( have_error ) {
7688            
7689             /* parallel algorithm (as we're adding bins together) for
7690             (possibly) weighted standard deviation; see
7691             https: */
7692 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
7693 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
7694 0           double mean_b = bmean;
7695 0           double weight_a = _weight;
7696 0           double weight_b = bweight;
7697 0           double weight_x = bweight += weight_a;
7698 0           double delta = mean_b - mean_a;
7699 0           bmean = mean_a + delta * weight_b / weight_x;
7700 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
7701 0           + delta * delta * weight_a * weight_b / weight_x;
7702              
7703 0           bad_error = nin <= 1;
7704 0           berror = bad_error
7705             ? DBL_MAX
7706 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
7707            
7708             }
7709              
7710             else {
7711            
7712             /* parallel algorithm (as we're adding bins together) for
7713             (possibly) weighted standard deviation; see
7714             https: */
7715 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
7716 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
7717 0           double mean_b = bmean;
7718 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
7719 0           double weight_b = nin_last;
7720 0           double weight_x = nin;
7721 0           double delta = mean_b - mean_a;
7722 0           bmean = mean_a + delta * weight_b / weight_x;
7723 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
7724 0           + delta * delta * weight_a * weight_b / weight_x;
7725              
7726 0           bad_error = nin <= 1;
7727 0           berror = bad_error
7728             ? DBL_MAX
7729 0 0         : sqrt( bm2 * 1 / (nin - 1) );
7730            
7731             }
7732             }
7733              
7734 0 0         else if ( error_rss ) {
7735            
7736             /* parallel algorithm (as we're adding bins together) for
7737             mean; see
7738             https: */
7739 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
7740 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
7741 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
7742 0           double mean_b = bmean;
7743 0           double weight_a = _weight;
7744 0           double weight_b = bweight;
7745 0           double weight_x = bweight += weight_a;
7746 0           double delta = mean_b - mean_a;
7747 0           bmean = mean_a + delta * weight_b / weight_x;
7748              
7749 0           berror2 += _error2;
7750 0           berror = sqrt( berror2 );
7751            
7752             }
7753              
7754 0 0         else if ( error_poisson ) {
7755 0           berror = sqrt( nin );
7756 0           bmean = bsignal / nin;
7757             }
7758              
7759             else {
7760 0           croak( "internal error" );
7761             }
7762            
7763              
7764 0           bsnr = bsignal / berror;
7765 0           snr_ok = bsnr >= min_snr;
7766              
7767            
7768 0           rc |= \
7769 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
7770 0           | \
7771 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
7772 0           | \
7773             ( nin >= min_nelem \
7774 0 0         && bwidth >= min_width \
7775 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
7776             ; \
7777             ;
7778              
7779 0 0         if (rc)
7780 0           break;
7781             }
7782              
7783             /* fix up index for events initially stuck in folded bins */
7784 0           PDL_Indx curind1 = curind+1;
7785             PDL_Indx ni;
7786              
7787 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
7788 0 0         ni < __privtrans->ind_sizes[0] ;
7789 0           ni++ ) {
7790             #ifdef PDL_BAD_CODE
7791             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
7792             #endif /* PDL_BAD_CODE */
7793 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
7794             }
7795 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
7796 0           rc |= BIN_RC_FOLDED;
7797             }
7798              
7799            
7800 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
7801 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
7802 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
7803 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
7804            
7805 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
7806            
7807 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
7808 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
7809 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
7810 0 0         if ( error_sdev ) {
7811 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
7812 0 0         if ( have_error ) {
7813 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7814 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
7815 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
7816             }
7817             else {
7818 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
7819             }
7820             }
7821 0 0         else if ( error_rss ) {
7822 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
7823 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
7824             }
7825            
7826             }
7827             /* adjust for possibility of last bin being empty */
7828 0           (nbins_datap)[0] = curind + ( nin != 0 );
7829 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
7830             }
7831 0           } break;
7832 0           case PDL_IND: {
7833 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
7834             {
7835              
7836              
7837              
7838              
7839 0           int flags = __params->optflags;
7840              
7841 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
7842 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
7843 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
7844 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
7845 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
7846 0           int fold_last_bin = flags & BIN_ARG_FOLD;
7847 0           int set_bad = flags & BIN_ARG_SET_BAD;
7848              
7849 0           PDL_Indx min_nelem = __params->min_nelem;
7850 0           PDL_Indx max_nelem = __params->max_nelem;
7851 0           double max_width = __params->max_width;
7852 0           double min_width = __params->min_width;
7853 0           double min_snr = __params->min_snr;
7854              
7855             /* simplify the logic below by setting bounds values to their most
7856             permissive extremes if they aren't needed. */
7857              
7858 0 0         if ( max_width == 0 )
7859 0           max_width = DBL_MAX;
7860              
7861 0 0         if ( max_nelem == 0 )
7862 0           max_nelem = LONG_MAX;
7863              
7864 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
7865              
7866 0           PDL_Indx curind = 0; /* index of current bin */
7867              
7868             /* Declare bin counters */
7869 0           int rc = 0; /* status */
7870 0           double bsignal = 0; /* sum of signal */
7871 0           double bmean = 0; /* mean of (weighted) signal */
7872 0           double bwidth = 0; /* width (if applicable) */
7873 0           double bsnr = 0; /* SNR */
7874 0           double berror2 = 0; /* sum of error^2 */
7875 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
7876 0           double bm2 = 0; /* unnormalized variance */
7877 0           double bsignal2 = 0; /* sum of signal^2 */
7878 0           double bweight = 0; /* sum of 1/error*2 */
7879 0           double bweight_sig = 0; /* sum of weight * signal */
7880 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
7881 0           int bad_error = 0; /* if error2 is not good */
7882 0           int lastrc = 0; /* carryover status from previous loop */
7883 0           PDL_Indx nin = 0; /* number of elements in the current bin */
7884              
7885 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
7886              
7887 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
7888             double error;
7889             double error2;
7890             double width;
7891              
7892 0           int snr_ok = 0;
7893              
7894 0 0         if ( have_error ) {
7895 0           error = (error_datap)[0+(__inc_error_n*(n))];
7896 0           error2 = error * error;
7897             }
7898              
7899 0 0         if ( have_width )
7900 0           width = (width_datap)[0+(__inc_width_n*(n))];
7901              
7902             #ifdef PDL_BAD_CODE
7903             if ( PDL_ISBAD2(signal,signal_badval,N,signal_badval_isnan)
7904             ||
7905             have_error && PDL_ISBAD2(error,error_badval,N,error_badval_isnan) ) {
7906             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
7907             continue;
7908             }
7909             #endif /* PDL_BAD_CODE */
7910 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
7911              
7912            
7913 0           bsignal += signal;
7914 0           nin += 1;
7915              
7916             /* calculate error */
7917 0 0         if ( error_sdev ) {
7918              
7919             /* weighted standard deviation */
7920 0 0         if ( have_error ) {
7921            
7922             /* incremental algorithm for possibly weighted standard deviation; see
7923             https: */
7924 0           double _weight = 1 / ( error * error );
7925 0           double _sum_weight = bweight += _weight;
7926 0           double delta = signal - bmean;
7927 0           bmean += delta * _weight / _sum_weight;
7928 0           bm2 += _weight * delta * ( signal - bmean );
7929              
7930 0           bad_error = nin <= 1;
7931 0           berror = bad_error
7932             ? DBL_MAX
7933 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
7934            
7935             }
7936              
7937             else {
7938            
7939             /* incremental algorithm for possibly weighted standard deviation; see
7940             https: */
7941 0           double _weight = 1;
7942 0           double _sum_weight = nin;
7943 0           double delta = signal - bmean;
7944 0           bmean += delta * _weight / _sum_weight;
7945 0           bm2 += _weight * delta * ( signal - bmean );
7946              
7947 0           bad_error = nin <= 1;
7948 0           berror = bad_error
7949             ? DBL_MAX
7950 0 0         : sqrt( bm2 * 1 / (nin - 1) );
7951            
7952             }
7953             }
7954              
7955 0 0         else if ( error_rss ) {
7956            
7957             /* incremental algorithm for mean; see
7958             https: */
7959 0           double _error2 = error2;
7960 0           double _weight = 1 / ( error * error );
7961 0           double delta = signal - bmean;
7962              
7963 0           bweight += _weight;
7964 0           bmean += delta * _weight / bweight;
7965 0           berror2 += _error2;
7966 0           berror = sqrt( berror2 );
7967            
7968             }
7969              
7970 0 0         else if ( error_poisson ) {
7971 0           berror = sqrt( nin );
7972 0           bmean = bsignal / nin;
7973             }
7974              
7975             else {
7976 0           croak( "internal error" );
7977             }
7978            
7979              
7980 0           bsnr = bsignal / berror;
7981 0           snr_ok = bsnr >= min_snr;
7982              
7983 0 0         if ( have_width )
7984 0           bwidth += width;
7985              
7986 0 0         if ( nin == 1 )
7987 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
7988              
7989            
7990 0           rc |= \
7991 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
7992 0           | \
7993 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
7994 0           | \
7995             ( nin >= min_nelem \
7996 0 0         && bwidth >= min_width \
7997 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
7998             ; \
7999             ;
8000              
8001 0 0         if ( rc ) {
8002 0           rc |= lastrc;
8003              
8004            
8005 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
8006 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
8007 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
8008 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
8009            
8010 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
8011            
8012 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
8013 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
8014 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
8015 0 0         if ( error_sdev ) {
8016 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
8017 0 0         if ( have_error ) {
8018 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8019 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
8020 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
8021             }
8022             else {
8023 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
8024             }
8025             }
8026 0 0         else if ( error_rss ) {
8027 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
8028 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8029             }
8030            
8031              
8032 0           curind++;
8033              
8034             /* Reset bin counters */
8035 0           rc = 0;
8036 0           bsignal = 0;
8037 0           bmean = 0;
8038 0           bwidth = 0;
8039 0           bsnr = 0;
8040 0           berror2 = 0;
8041 0           berror = 0;
8042 0           bm2 = 0;
8043 0           bsignal2 = 0;
8044 0           bweight = 0;
8045 0           bweight_sig = 0;
8046 0           bweight_sig2 = 0;
8047 0           bad_error = 0;
8048 0           lastrc = 0;
8049 0           nin = 0;
8050              
8051             }
8052              
8053 0 0         else if ( snr_ok ) {
8054 0           lastrc = BIN_RC_GTMINSN;
8055             }
8056              
8057             else {
8058 0           lastrc = 0;
8059             }
8060             }} /* Close n */
8061              
8062              
8063             /* record last bin if it's not empty */
8064 0 0         if ( nin ) {
8065              
8066             /* needed for SET_RESULTS */
8067 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
8068              
8069 0           rc = 0;
8070 0           bad_error = 0;
8071              
8072             /* a non empty bin means that we didn't meet constraints. fold it into
8073             the previous bin if requested & possible. sometimes that will
8074             actually lower the S/N of the previous bin; keep going until
8075             we can't fold anymore or we get the proper S/N
8076             */
8077 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
8078              
8079              
8080 0 0         while ( --curind > 0 ) {
8081             double tmp;
8082 0           int snr_ok = 0;
8083 0           PDL_Indx nin_last = nin;
8084              
8085            
8086 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
8087 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
8088              
8089             /* calculate error */
8090 0 0         if ( error_sdev ) {
8091              
8092             /* weighted standard deviation */
8093 0 0         if ( have_error ) {
8094            
8095             /* parallel algorithm (as we're adding bins together) for
8096             (possibly) weighted standard deviation; see
8097             https: */
8098 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8099 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8100 0           double mean_b = bmean;
8101 0           double weight_a = _weight;
8102 0           double weight_b = bweight;
8103 0           double weight_x = bweight += weight_a;
8104 0           double delta = mean_b - mean_a;
8105 0           bmean = mean_a + delta * weight_b / weight_x;
8106 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
8107 0           + delta * delta * weight_a * weight_b / weight_x;
8108              
8109 0           bad_error = nin <= 1;
8110 0           berror = bad_error
8111             ? DBL_MAX
8112 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
8113            
8114             }
8115              
8116             else {
8117            
8118             /* parallel algorithm (as we're adding bins together) for
8119             (possibly) weighted standard deviation; see
8120             https: */
8121 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8122 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8123 0           double mean_b = bmean;
8124 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
8125 0           double weight_b = nin_last;
8126 0           double weight_x = nin;
8127 0           double delta = mean_b - mean_a;
8128 0           bmean = mean_a + delta * weight_b / weight_x;
8129 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
8130 0           + delta * delta * weight_a * weight_b / weight_x;
8131              
8132 0           bad_error = nin <= 1;
8133 0           berror = bad_error
8134             ? DBL_MAX
8135 0 0         : sqrt( bm2 * 1 / (nin - 1) );
8136            
8137             }
8138             }
8139              
8140 0 0         else if ( error_rss ) {
8141            
8142             /* parallel algorithm (as we're adding bins together) for
8143             mean; see
8144             https: */
8145 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
8146 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8147 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8148 0           double mean_b = bmean;
8149 0           double weight_a = _weight;
8150 0           double weight_b = bweight;
8151 0           double weight_x = bweight += weight_a;
8152 0           double delta = mean_b - mean_a;
8153 0           bmean = mean_a + delta * weight_b / weight_x;
8154              
8155 0           berror2 += _error2;
8156 0           berror = sqrt( berror2 );
8157            
8158             }
8159              
8160 0 0         else if ( error_poisson ) {
8161 0           berror = sqrt( nin );
8162 0           bmean = bsignal / nin;
8163             }
8164              
8165             else {
8166 0           croak( "internal error" );
8167             }
8168            
8169              
8170 0           bsnr = bsignal / berror;
8171 0           snr_ok = bsnr >= min_snr;
8172              
8173            
8174 0           rc |= \
8175 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
8176 0           | \
8177 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
8178 0           | \
8179             ( nin >= min_nelem \
8180 0 0         && bwidth >= min_width \
8181 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
8182             ; \
8183             ;
8184              
8185 0 0         if (rc)
8186 0           break;
8187             }
8188              
8189             /* fix up index for events initially stuck in folded bins */
8190 0           PDL_Indx curind1 = curind+1;
8191             PDL_Indx ni;
8192              
8193 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
8194 0 0         ni < __privtrans->ind_sizes[0] ;
8195 0           ni++ ) {
8196             #ifdef PDL_BAD_CODE
8197             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
8198             #endif /* PDL_BAD_CODE */
8199 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
8200             }
8201 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
8202 0           rc |= BIN_RC_FOLDED;
8203             }
8204              
8205            
8206 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
8207 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
8208 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
8209 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
8210            
8211 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
8212            
8213 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
8214 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
8215 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
8216 0 0         if ( error_sdev ) {
8217 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
8218 0 0         if ( have_error ) {
8219 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8220 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
8221 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
8222             }
8223             else {
8224 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
8225             }
8226             }
8227 0 0         else if ( error_rss ) {
8228 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
8229 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8230             }
8231            
8232             }
8233             /* adjust for possibility of last bin being empty */
8234 0           (nbins_datap)[0] = curind + ( nin != 0 );
8235 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
8236             }
8237 0           } break;
8238 0           case PDL_ULL: {
8239 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_ULongLong,P,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
8240             {
8241              
8242              
8243              
8244              
8245 0           int flags = __params->optflags;
8246              
8247 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
8248 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
8249 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
8250 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
8251 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
8252 0           int fold_last_bin = flags & BIN_ARG_FOLD;
8253 0           int set_bad = flags & BIN_ARG_SET_BAD;
8254              
8255 0           PDL_Indx min_nelem = __params->min_nelem;
8256 0           PDL_Indx max_nelem = __params->max_nelem;
8257 0           double max_width = __params->max_width;
8258 0           double min_width = __params->min_width;
8259 0           double min_snr = __params->min_snr;
8260              
8261             /* simplify the logic below by setting bounds values to their most
8262             permissive extremes if they aren't needed. */
8263              
8264 0 0         if ( max_width == 0 )
8265 0           max_width = DBL_MAX;
8266              
8267 0 0         if ( max_nelem == 0 )
8268 0           max_nelem = LONG_MAX;
8269              
8270 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
8271              
8272 0           PDL_Indx curind = 0; /* index of current bin */
8273              
8274             /* Declare bin counters */
8275 0           int rc = 0; /* status */
8276 0           double bsignal = 0; /* sum of signal */
8277 0           double bmean = 0; /* mean of (weighted) signal */
8278 0           double bwidth = 0; /* width (if applicable) */
8279 0           double bsnr = 0; /* SNR */
8280 0           double berror2 = 0; /* sum of error^2 */
8281 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
8282 0           double bm2 = 0; /* unnormalized variance */
8283 0           double bsignal2 = 0; /* sum of signal^2 */
8284 0           double bweight = 0; /* sum of 1/error*2 */
8285 0           double bweight_sig = 0; /* sum of weight * signal */
8286 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
8287 0           int bad_error = 0; /* if error2 is not good */
8288 0           int lastrc = 0; /* carryover status from previous loop */
8289 0           PDL_Indx nin = 0; /* number of elements in the current bin */
8290              
8291 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
8292              
8293 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
8294             double error;
8295             double error2;
8296             double width;
8297              
8298 0           int snr_ok = 0;
8299              
8300 0 0         if ( have_error ) {
8301 0           error = (error_datap)[0+(__inc_error_n*(n))];
8302 0           error2 = error * error;
8303             }
8304              
8305 0 0         if ( have_width )
8306 0           width = (width_datap)[0+(__inc_width_n*(n))];
8307              
8308             #ifdef PDL_BAD_CODE
8309             if ( PDL_ISBAD2(signal,signal_badval,P,signal_badval_isnan)
8310             ||
8311             have_error && PDL_ISBAD2(error,error_badval,P,error_badval_isnan) ) {
8312             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
8313             continue;
8314             }
8315             #endif /* PDL_BAD_CODE */
8316 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
8317              
8318            
8319 0           bsignal += signal;
8320 0           nin += 1;
8321              
8322             /* calculate error */
8323 0 0         if ( error_sdev ) {
8324              
8325             /* weighted standard deviation */
8326 0 0         if ( have_error ) {
8327            
8328             /* incremental algorithm for possibly weighted standard deviation; see
8329             https: */
8330 0           double _weight = 1 / ( error * error );
8331 0           double _sum_weight = bweight += _weight;
8332 0           double delta = signal - bmean;
8333 0           bmean += delta * _weight / _sum_weight;
8334 0           bm2 += _weight * delta * ( signal - bmean );
8335              
8336 0           bad_error = nin <= 1;
8337 0           berror = bad_error
8338             ? DBL_MAX
8339 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
8340            
8341             }
8342              
8343             else {
8344            
8345             /* incremental algorithm for possibly weighted standard deviation; see
8346             https: */
8347 0           double _weight = 1;
8348 0           double _sum_weight = nin;
8349 0           double delta = signal - bmean;
8350 0           bmean += delta * _weight / _sum_weight;
8351 0           bm2 += _weight * delta * ( signal - bmean );
8352              
8353 0           bad_error = nin <= 1;
8354 0           berror = bad_error
8355             ? DBL_MAX
8356 0 0         : sqrt( bm2 * 1 / (nin - 1) );
8357            
8358             }
8359             }
8360              
8361 0 0         else if ( error_rss ) {
8362            
8363             /* incremental algorithm for mean; see
8364             https: */
8365 0           double _error2 = error2;
8366 0           double _weight = 1 / ( error * error );
8367 0           double delta = signal - bmean;
8368              
8369 0           bweight += _weight;
8370 0           bmean += delta * _weight / bweight;
8371 0           berror2 += _error2;
8372 0           berror = sqrt( berror2 );
8373            
8374             }
8375              
8376 0 0         else if ( error_poisson ) {
8377 0           berror = sqrt( nin );
8378 0           bmean = bsignal / nin;
8379             }
8380              
8381             else {
8382 0           croak( "internal error" );
8383             }
8384            
8385              
8386 0           bsnr = bsignal / berror;
8387 0           snr_ok = bsnr >= min_snr;
8388              
8389 0 0         if ( have_width )
8390 0           bwidth += width;
8391              
8392 0 0         if ( nin == 1 )
8393 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
8394              
8395            
8396 0           rc |= \
8397 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
8398 0           | \
8399 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
8400 0           | \
8401             ( nin >= min_nelem \
8402 0 0         && bwidth >= min_width \
8403 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
8404             ; \
8405             ;
8406              
8407 0 0         if ( rc ) {
8408 0           rc |= lastrc;
8409              
8410            
8411 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
8412 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
8413 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
8414 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
8415            
8416 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
8417            
8418 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
8419 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
8420 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
8421 0 0         if ( error_sdev ) {
8422 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
8423 0 0         if ( have_error ) {
8424 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8425 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
8426 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
8427             }
8428             else {
8429 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
8430             }
8431             }
8432 0 0         else if ( error_rss ) {
8433 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
8434 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8435             }
8436            
8437              
8438 0           curind++;
8439              
8440             /* Reset bin counters */
8441 0           rc = 0;
8442 0           bsignal = 0;
8443 0           bmean = 0;
8444 0           bwidth = 0;
8445 0           bsnr = 0;
8446 0           berror2 = 0;
8447 0           berror = 0;
8448 0           bm2 = 0;
8449 0           bsignal2 = 0;
8450 0           bweight = 0;
8451 0           bweight_sig = 0;
8452 0           bweight_sig2 = 0;
8453 0           bad_error = 0;
8454 0           lastrc = 0;
8455 0           nin = 0;
8456              
8457             }
8458              
8459 0 0         else if ( snr_ok ) {
8460 0           lastrc = BIN_RC_GTMINSN;
8461             }
8462              
8463             else {
8464 0           lastrc = 0;
8465             }
8466             }} /* Close n */
8467              
8468              
8469             /* record last bin if it's not empty */
8470 0 0         if ( nin ) {
8471              
8472             /* needed for SET_RESULTS */
8473 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
8474              
8475 0           rc = 0;
8476 0           bad_error = 0;
8477              
8478             /* a non empty bin means that we didn't meet constraints. fold it into
8479             the previous bin if requested & possible. sometimes that will
8480             actually lower the S/N of the previous bin; keep going until
8481             we can't fold anymore or we get the proper S/N
8482             */
8483 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
8484              
8485              
8486 0 0         while ( --curind > 0 ) {
8487             double tmp;
8488 0           int snr_ok = 0;
8489 0           PDL_Indx nin_last = nin;
8490              
8491            
8492 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
8493 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
8494              
8495             /* calculate error */
8496 0 0         if ( error_sdev ) {
8497              
8498             /* weighted standard deviation */
8499 0 0         if ( have_error ) {
8500            
8501             /* parallel algorithm (as we're adding bins together) for
8502             (possibly) weighted standard deviation; see
8503             https: */
8504 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8505 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8506 0           double mean_b = bmean;
8507 0           double weight_a = _weight;
8508 0           double weight_b = bweight;
8509 0           double weight_x = bweight += weight_a;
8510 0           double delta = mean_b - mean_a;
8511 0           bmean = mean_a + delta * weight_b / weight_x;
8512 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
8513 0           + delta * delta * weight_a * weight_b / weight_x;
8514              
8515 0           bad_error = nin <= 1;
8516 0           berror = bad_error
8517             ? DBL_MAX
8518 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
8519            
8520             }
8521              
8522             else {
8523            
8524             /* parallel algorithm (as we're adding bins together) for
8525             (possibly) weighted standard deviation; see
8526             https: */
8527 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8528 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8529 0           double mean_b = bmean;
8530 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
8531 0           double weight_b = nin_last;
8532 0           double weight_x = nin;
8533 0           double delta = mean_b - mean_a;
8534 0           bmean = mean_a + delta * weight_b / weight_x;
8535 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
8536 0           + delta * delta * weight_a * weight_b / weight_x;
8537              
8538 0           bad_error = nin <= 1;
8539 0           berror = bad_error
8540             ? DBL_MAX
8541 0 0         : sqrt( bm2 * 1 / (nin - 1) );
8542            
8543             }
8544             }
8545              
8546 0 0         else if ( error_rss ) {
8547            
8548             /* parallel algorithm (as we're adding bins together) for
8549             mean; see
8550             https: */
8551 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
8552 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8553 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8554 0           double mean_b = bmean;
8555 0           double weight_a = _weight;
8556 0           double weight_b = bweight;
8557 0           double weight_x = bweight += weight_a;
8558 0           double delta = mean_b - mean_a;
8559 0           bmean = mean_a + delta * weight_b / weight_x;
8560              
8561 0           berror2 += _error2;
8562 0           berror = sqrt( berror2 );
8563            
8564             }
8565              
8566 0 0         else if ( error_poisson ) {
8567 0           berror = sqrt( nin );
8568 0           bmean = bsignal / nin;
8569             }
8570              
8571             else {
8572 0           croak( "internal error" );
8573             }
8574            
8575              
8576 0           bsnr = bsignal / berror;
8577 0           snr_ok = bsnr >= min_snr;
8578              
8579            
8580 0           rc |= \
8581 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
8582 0           | \
8583 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
8584 0           | \
8585             ( nin >= min_nelem \
8586 0 0         && bwidth >= min_width \
8587 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
8588             ; \
8589             ;
8590              
8591 0 0         if (rc)
8592 0           break;
8593             }
8594              
8595             /* fix up index for events initially stuck in folded bins */
8596 0           PDL_Indx curind1 = curind+1;
8597             PDL_Indx ni;
8598              
8599 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
8600 0 0         ni < __privtrans->ind_sizes[0] ;
8601 0           ni++ ) {
8602             #ifdef PDL_BAD_CODE
8603             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
8604             #endif /* PDL_BAD_CODE */
8605 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
8606             }
8607 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
8608 0           rc |= BIN_RC_FOLDED;
8609             }
8610              
8611            
8612 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
8613 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
8614 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
8615 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
8616            
8617 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
8618            
8619 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
8620 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
8621 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
8622 0 0         if ( error_sdev ) {
8623 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
8624 0 0         if ( have_error ) {
8625 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8626 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
8627 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
8628             }
8629             else {
8630 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
8631             }
8632             }
8633 0 0         else if ( error_rss ) {
8634 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
8635 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8636             }
8637            
8638             }
8639             /* adjust for possibility of last bin being empty */
8640 0           (nbins_datap)[0] = curind + ( nin != 0 );
8641 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
8642             }
8643 0           } break;
8644 0           case PDL_LL: {
8645 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_LongLong,Q,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
8646             {
8647              
8648              
8649              
8650              
8651 0           int flags = __params->optflags;
8652              
8653 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
8654 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
8655 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
8656 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
8657 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
8658 0           int fold_last_bin = flags & BIN_ARG_FOLD;
8659 0           int set_bad = flags & BIN_ARG_SET_BAD;
8660              
8661 0           PDL_Indx min_nelem = __params->min_nelem;
8662 0           PDL_Indx max_nelem = __params->max_nelem;
8663 0           double max_width = __params->max_width;
8664 0           double min_width = __params->min_width;
8665 0           double min_snr = __params->min_snr;
8666              
8667             /* simplify the logic below by setting bounds values to their most
8668             permissive extremes if they aren't needed. */
8669              
8670 0 0         if ( max_width == 0 )
8671 0           max_width = DBL_MAX;
8672              
8673 0 0         if ( max_nelem == 0 )
8674 0           max_nelem = LONG_MAX;
8675              
8676 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
8677              
8678 0           PDL_Indx curind = 0; /* index of current bin */
8679              
8680             /* Declare bin counters */
8681 0           int rc = 0; /* status */
8682 0           double bsignal = 0; /* sum of signal */
8683 0           double bmean = 0; /* mean of (weighted) signal */
8684 0           double bwidth = 0; /* width (if applicable) */
8685 0           double bsnr = 0; /* SNR */
8686 0           double berror2 = 0; /* sum of error^2 */
8687 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
8688 0           double bm2 = 0; /* unnormalized variance */
8689 0           double bsignal2 = 0; /* sum of signal^2 */
8690 0           double bweight = 0; /* sum of 1/error*2 */
8691 0           double bweight_sig = 0; /* sum of weight * signal */
8692 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
8693 0           int bad_error = 0; /* if error2 is not good */
8694 0           int lastrc = 0; /* carryover status from previous loop */
8695 0           PDL_Indx nin = 0; /* number of elements in the current bin */
8696              
8697 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
8698              
8699 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
8700             double error;
8701             double error2;
8702             double width;
8703              
8704 0           int snr_ok = 0;
8705              
8706 0 0         if ( have_error ) {
8707 0           error = (error_datap)[0+(__inc_error_n*(n))];
8708 0           error2 = error * error;
8709             }
8710              
8711 0 0         if ( have_width )
8712 0           width = (width_datap)[0+(__inc_width_n*(n))];
8713              
8714             #ifdef PDL_BAD_CODE
8715             if ( PDL_ISBAD2(signal,signal_badval,Q,signal_badval_isnan)
8716             ||
8717             have_error && PDL_ISBAD2(error,error_badval,Q,error_badval_isnan) ) {
8718             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
8719             continue;
8720             }
8721             #endif /* PDL_BAD_CODE */
8722 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
8723              
8724            
8725 0           bsignal += signal;
8726 0           nin += 1;
8727              
8728             /* calculate error */
8729 0 0         if ( error_sdev ) {
8730              
8731             /* weighted standard deviation */
8732 0 0         if ( have_error ) {
8733            
8734             /* incremental algorithm for possibly weighted standard deviation; see
8735             https: */
8736 0           double _weight = 1 / ( error * error );
8737 0           double _sum_weight = bweight += _weight;
8738 0           double delta = signal - bmean;
8739 0           bmean += delta * _weight / _sum_weight;
8740 0           bm2 += _weight * delta * ( signal - bmean );
8741              
8742 0           bad_error = nin <= 1;
8743 0           berror = bad_error
8744             ? DBL_MAX
8745 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
8746            
8747             }
8748              
8749             else {
8750            
8751             /* incremental algorithm for possibly weighted standard deviation; see
8752             https: */
8753 0           double _weight = 1;
8754 0           double _sum_weight = nin;
8755 0           double delta = signal - bmean;
8756 0           bmean += delta * _weight / _sum_weight;
8757 0           bm2 += _weight * delta * ( signal - bmean );
8758              
8759 0           bad_error = nin <= 1;
8760 0           berror = bad_error
8761             ? DBL_MAX
8762 0 0         : sqrt( bm2 * 1 / (nin - 1) );
8763            
8764             }
8765             }
8766              
8767 0 0         else if ( error_rss ) {
8768            
8769             /* incremental algorithm for mean; see
8770             https: */
8771 0           double _error2 = error2;
8772 0           double _weight = 1 / ( error * error );
8773 0           double delta = signal - bmean;
8774              
8775 0           bweight += _weight;
8776 0           bmean += delta * _weight / bweight;
8777 0           berror2 += _error2;
8778 0           berror = sqrt( berror2 );
8779            
8780             }
8781              
8782 0 0         else if ( error_poisson ) {
8783 0           berror = sqrt( nin );
8784 0           bmean = bsignal / nin;
8785             }
8786              
8787             else {
8788 0           croak( "internal error" );
8789             }
8790            
8791              
8792 0           bsnr = bsignal / berror;
8793 0           snr_ok = bsnr >= min_snr;
8794              
8795 0 0         if ( have_width )
8796 0           bwidth += width;
8797              
8798 0 0         if ( nin == 1 )
8799 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
8800              
8801            
8802 0           rc |= \
8803 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
8804 0           | \
8805 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
8806 0           | \
8807             ( nin >= min_nelem \
8808 0 0         && bwidth >= min_width \
8809 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
8810             ; \
8811             ;
8812              
8813 0 0         if ( rc ) {
8814 0           rc |= lastrc;
8815              
8816            
8817 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
8818 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
8819 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
8820 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
8821            
8822 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
8823            
8824 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
8825 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
8826 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
8827 0 0         if ( error_sdev ) {
8828 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
8829 0 0         if ( have_error ) {
8830 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8831 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
8832 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
8833             }
8834             else {
8835 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
8836             }
8837             }
8838 0 0         else if ( error_rss ) {
8839 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
8840 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
8841             }
8842            
8843              
8844 0           curind++;
8845              
8846             /* Reset bin counters */
8847 0           rc = 0;
8848 0           bsignal = 0;
8849 0           bmean = 0;
8850 0           bwidth = 0;
8851 0           bsnr = 0;
8852 0           berror2 = 0;
8853 0           berror = 0;
8854 0           bm2 = 0;
8855 0           bsignal2 = 0;
8856 0           bweight = 0;
8857 0           bweight_sig = 0;
8858 0           bweight_sig2 = 0;
8859 0           bad_error = 0;
8860 0           lastrc = 0;
8861 0           nin = 0;
8862              
8863             }
8864              
8865 0 0         else if ( snr_ok ) {
8866 0           lastrc = BIN_RC_GTMINSN;
8867             }
8868              
8869             else {
8870 0           lastrc = 0;
8871             }
8872             }} /* Close n */
8873              
8874              
8875             /* record last bin if it's not empty */
8876 0 0         if ( nin ) {
8877              
8878             /* needed for SET_RESULTS */
8879 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
8880              
8881 0           rc = 0;
8882 0           bad_error = 0;
8883              
8884             /* a non empty bin means that we didn't meet constraints. fold it into
8885             the previous bin if requested & possible. sometimes that will
8886             actually lower the S/N of the previous bin; keep going until
8887             we can't fold anymore or we get the proper S/N
8888             */
8889 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
8890              
8891              
8892 0 0         while ( --curind > 0 ) {
8893             double tmp;
8894 0           int snr_ok = 0;
8895 0           PDL_Indx nin_last = nin;
8896              
8897            
8898 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
8899 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
8900              
8901             /* calculate error */
8902 0 0         if ( error_sdev ) {
8903              
8904             /* weighted standard deviation */
8905 0 0         if ( have_error ) {
8906            
8907             /* parallel algorithm (as we're adding bins together) for
8908             (possibly) weighted standard deviation; see
8909             https: */
8910 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8911 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8912 0           double mean_b = bmean;
8913 0           double weight_a = _weight;
8914 0           double weight_b = bweight;
8915 0           double weight_x = bweight += weight_a;
8916 0           double delta = mean_b - mean_a;
8917 0           bmean = mean_a + delta * weight_b / weight_x;
8918 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
8919 0           + delta * delta * weight_a * weight_b / weight_x;
8920              
8921 0           bad_error = nin <= 1;
8922 0           berror = bad_error
8923             ? DBL_MAX
8924 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
8925            
8926             }
8927              
8928             else {
8929            
8930             /* parallel algorithm (as we're adding bins together) for
8931             (possibly) weighted standard deviation; see
8932             https: */
8933 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8934 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8935 0           double mean_b = bmean;
8936 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
8937 0           double weight_b = nin_last;
8938 0           double weight_x = nin;
8939 0           double delta = mean_b - mean_a;
8940 0           bmean = mean_a + delta * weight_b / weight_x;
8941 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
8942 0           + delta * delta * weight_a * weight_b / weight_x;
8943              
8944 0           bad_error = nin <= 1;
8945 0           berror = bad_error
8946             ? DBL_MAX
8947 0 0         : sqrt( bm2 * 1 / (nin - 1) );
8948            
8949             }
8950             }
8951              
8952 0 0         else if ( error_rss ) {
8953            
8954             /* parallel algorithm (as we're adding bins together) for
8955             mean; see
8956             https: */
8957 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
8958 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
8959 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
8960 0           double mean_b = bmean;
8961 0           double weight_a = _weight;
8962 0           double weight_b = bweight;
8963 0           double weight_x = bweight += weight_a;
8964 0           double delta = mean_b - mean_a;
8965 0           bmean = mean_a + delta * weight_b / weight_x;
8966              
8967 0           berror2 += _error2;
8968 0           berror = sqrt( berror2 );
8969            
8970             }
8971              
8972 0 0         else if ( error_poisson ) {
8973 0           berror = sqrt( nin );
8974 0           bmean = bsignal / nin;
8975             }
8976              
8977             else {
8978 0           croak( "internal error" );
8979             }
8980            
8981              
8982 0           bsnr = bsignal / berror;
8983 0           snr_ok = bsnr >= min_snr;
8984              
8985            
8986 0           rc |= \
8987 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
8988 0           | \
8989 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
8990 0           | \
8991             ( nin >= min_nelem \
8992 0 0         && bwidth >= min_width \
8993 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
8994             ; \
8995             ;
8996              
8997 0 0         if (rc)
8998 0           break;
8999             }
9000              
9001             /* fix up index for events initially stuck in folded bins */
9002 0           PDL_Indx curind1 = curind+1;
9003             PDL_Indx ni;
9004              
9005 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
9006 0 0         ni < __privtrans->ind_sizes[0] ;
9007 0           ni++ ) {
9008             #ifdef PDL_BAD_CODE
9009             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
9010             #endif /* PDL_BAD_CODE */
9011 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
9012             }
9013 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9014 0           rc |= BIN_RC_FOLDED;
9015             }
9016              
9017            
9018 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
9019 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
9020 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
9021 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
9022            
9023 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
9024            
9025 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
9026 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
9027 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9028 0 0         if ( error_sdev ) {
9029 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
9030 0 0         if ( have_error ) {
9031 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9032 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
9033 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
9034             }
9035             else {
9036 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
9037             }
9038             }
9039 0 0         else if ( error_rss ) {
9040 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
9041 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9042             }
9043            
9044             }
9045             /* adjust for possibility of last bin being empty */
9046 0           (nbins_datap)[0] = curind + ( nin != 0 );
9047 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
9048             }
9049 0           } break;
9050 0           case PDL_F: {
9051 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Float,F,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
9052             {
9053              
9054              
9055              
9056              
9057 0           int flags = __params->optflags;
9058              
9059 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
9060 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
9061 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
9062 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
9063 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
9064 0           int fold_last_bin = flags & BIN_ARG_FOLD;
9065 0           int set_bad = flags & BIN_ARG_SET_BAD;
9066              
9067 0           PDL_Indx min_nelem = __params->min_nelem;
9068 0           PDL_Indx max_nelem = __params->max_nelem;
9069 0           double max_width = __params->max_width;
9070 0           double min_width = __params->min_width;
9071 0           double min_snr = __params->min_snr;
9072              
9073             /* simplify the logic below by setting bounds values to their most
9074             permissive extremes if they aren't needed. */
9075              
9076 0 0         if ( max_width == 0 )
9077 0           max_width = DBL_MAX;
9078              
9079 0 0         if ( max_nelem == 0 )
9080 0           max_nelem = LONG_MAX;
9081              
9082 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
9083              
9084 0           PDL_Indx curind = 0; /* index of current bin */
9085              
9086             /* Declare bin counters */
9087 0           int rc = 0; /* status */
9088 0           double bsignal = 0; /* sum of signal */
9089 0           double bmean = 0; /* mean of (weighted) signal */
9090 0           double bwidth = 0; /* width (if applicable) */
9091 0           double bsnr = 0; /* SNR */
9092 0           double berror2 = 0; /* sum of error^2 */
9093 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
9094 0           double bm2 = 0; /* unnormalized variance */
9095 0           double bsignal2 = 0; /* sum of signal^2 */
9096 0           double bweight = 0; /* sum of 1/error*2 */
9097 0           double bweight_sig = 0; /* sum of weight * signal */
9098 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
9099 0           int bad_error = 0; /* if error2 is not good */
9100 0           int lastrc = 0; /* carryover status from previous loop */
9101 0           PDL_Indx nin = 0; /* number of elements in the current bin */
9102              
9103 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
9104              
9105 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
9106             double error;
9107             double error2;
9108             double width;
9109              
9110 0           int snr_ok = 0;
9111              
9112 0 0         if ( have_error ) {
9113 0           error = (error_datap)[0+(__inc_error_n*(n))];
9114 0           error2 = error * error;
9115             }
9116              
9117 0 0         if ( have_width )
9118 0           width = (width_datap)[0+(__inc_width_n*(n))];
9119              
9120             #ifdef PDL_BAD_CODE
9121             if ( PDL_ISBAD2(signal,signal_badval,F,signal_badval_isnan)
9122             ||
9123             have_error && PDL_ISBAD2(error,error_badval,F,error_badval_isnan) ) {
9124             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
9125             continue;
9126             }
9127             #endif /* PDL_BAD_CODE */
9128 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
9129              
9130            
9131 0           bsignal += signal;
9132 0           nin += 1;
9133              
9134             /* calculate error */
9135 0 0         if ( error_sdev ) {
9136              
9137             /* weighted standard deviation */
9138 0 0         if ( have_error ) {
9139            
9140             /* incremental algorithm for possibly weighted standard deviation; see
9141             https: */
9142 0           double _weight = 1 / ( error * error );
9143 0           double _sum_weight = bweight += _weight;
9144 0           double delta = signal - bmean;
9145 0           bmean += delta * _weight / _sum_weight;
9146 0           bm2 += _weight * delta * ( signal - bmean );
9147              
9148 0           bad_error = nin <= 1;
9149 0           berror = bad_error
9150             ? DBL_MAX
9151 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
9152            
9153             }
9154              
9155             else {
9156            
9157             /* incremental algorithm for possibly weighted standard deviation; see
9158             https: */
9159 0           double _weight = 1;
9160 0           double _sum_weight = nin;
9161 0           double delta = signal - bmean;
9162 0           bmean += delta * _weight / _sum_weight;
9163 0           bm2 += _weight * delta * ( signal - bmean );
9164              
9165 0           bad_error = nin <= 1;
9166 0           berror = bad_error
9167             ? DBL_MAX
9168 0 0         : sqrt( bm2 * 1 / (nin - 1) );
9169            
9170             }
9171             }
9172              
9173 0 0         else if ( error_rss ) {
9174            
9175             /* incremental algorithm for mean; see
9176             https: */
9177 0           double _error2 = error2;
9178 0           double _weight = 1 / ( error * error );
9179 0           double delta = signal - bmean;
9180              
9181 0           bweight += _weight;
9182 0           bmean += delta * _weight / bweight;
9183 0           berror2 += _error2;
9184 0           berror = sqrt( berror2 );
9185            
9186             }
9187              
9188 0 0         else if ( error_poisson ) {
9189 0           berror = sqrt( nin );
9190 0           bmean = bsignal / nin;
9191             }
9192              
9193             else {
9194 0           croak( "internal error" );
9195             }
9196            
9197              
9198 0           bsnr = bsignal / berror;
9199 0           snr_ok = bsnr >= min_snr;
9200              
9201 0 0         if ( have_width )
9202 0           bwidth += width;
9203              
9204 0 0         if ( nin == 1 )
9205 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
9206              
9207            
9208 0           rc |= \
9209 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
9210 0           | \
9211 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
9212 0           | \
9213             ( nin >= min_nelem \
9214 0 0         && bwidth >= min_width \
9215 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
9216             ; \
9217             ;
9218              
9219 0 0         if ( rc ) {
9220 0           rc |= lastrc;
9221              
9222            
9223 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
9224 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
9225 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
9226 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
9227            
9228 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
9229            
9230 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
9231 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
9232 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9233 0 0         if ( error_sdev ) {
9234 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
9235 0 0         if ( have_error ) {
9236 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9237 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
9238 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
9239             }
9240             else {
9241 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
9242             }
9243             }
9244 0 0         else if ( error_rss ) {
9245 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
9246 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9247             }
9248            
9249              
9250 0           curind++;
9251              
9252             /* Reset bin counters */
9253 0           rc = 0;
9254 0           bsignal = 0;
9255 0           bmean = 0;
9256 0           bwidth = 0;
9257 0           bsnr = 0;
9258 0           berror2 = 0;
9259 0           berror = 0;
9260 0           bm2 = 0;
9261 0           bsignal2 = 0;
9262 0           bweight = 0;
9263 0           bweight_sig = 0;
9264 0           bweight_sig2 = 0;
9265 0           bad_error = 0;
9266 0           lastrc = 0;
9267 0           nin = 0;
9268              
9269             }
9270              
9271 0 0         else if ( snr_ok ) {
9272 0           lastrc = BIN_RC_GTMINSN;
9273             }
9274              
9275             else {
9276 0           lastrc = 0;
9277             }
9278             }} /* Close n */
9279              
9280              
9281             /* record last bin if it's not empty */
9282 0 0         if ( nin ) {
9283              
9284             /* needed for SET_RESULTS */
9285 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
9286              
9287 0           rc = 0;
9288 0           bad_error = 0;
9289              
9290             /* a non empty bin means that we didn't meet constraints. fold it into
9291             the previous bin if requested & possible. sometimes that will
9292             actually lower the S/N of the previous bin; keep going until
9293             we can't fold anymore or we get the proper S/N
9294             */
9295 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
9296              
9297              
9298 0 0         while ( --curind > 0 ) {
9299             double tmp;
9300 0           int snr_ok = 0;
9301 0           PDL_Indx nin_last = nin;
9302              
9303            
9304 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
9305 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
9306              
9307             /* calculate error */
9308 0 0         if ( error_sdev ) {
9309              
9310             /* weighted standard deviation */
9311 0 0         if ( have_error ) {
9312            
9313             /* parallel algorithm (as we're adding bins together) for
9314             (possibly) weighted standard deviation; see
9315             https: */
9316 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
9317 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
9318 0           double mean_b = bmean;
9319 0           double weight_a = _weight;
9320 0           double weight_b = bweight;
9321 0           double weight_x = bweight += weight_a;
9322 0           double delta = mean_b - mean_a;
9323 0           bmean = mean_a + delta * weight_b / weight_x;
9324 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
9325 0           + delta * delta * weight_a * weight_b / weight_x;
9326              
9327 0           bad_error = nin <= 1;
9328 0           berror = bad_error
9329             ? DBL_MAX
9330 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
9331            
9332             }
9333              
9334             else {
9335            
9336             /* parallel algorithm (as we're adding bins together) for
9337             (possibly) weighted standard deviation; see
9338             https: */
9339 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
9340 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
9341 0           double mean_b = bmean;
9342 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
9343 0           double weight_b = nin_last;
9344 0           double weight_x = nin;
9345 0           double delta = mean_b - mean_a;
9346 0           bmean = mean_a + delta * weight_b / weight_x;
9347 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
9348 0           + delta * delta * weight_a * weight_b / weight_x;
9349              
9350 0           bad_error = nin <= 1;
9351 0           berror = bad_error
9352             ? DBL_MAX
9353 0 0         : sqrt( bm2 * 1 / (nin - 1) );
9354            
9355             }
9356             }
9357              
9358 0 0         else if ( error_rss ) {
9359            
9360             /* parallel algorithm (as we're adding bins together) for
9361             mean; see
9362             https: */
9363 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
9364 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
9365 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
9366 0           double mean_b = bmean;
9367 0           double weight_a = _weight;
9368 0           double weight_b = bweight;
9369 0           double weight_x = bweight += weight_a;
9370 0           double delta = mean_b - mean_a;
9371 0           bmean = mean_a + delta * weight_b / weight_x;
9372              
9373 0           berror2 += _error2;
9374 0           berror = sqrt( berror2 );
9375            
9376             }
9377              
9378 0 0         else if ( error_poisson ) {
9379 0           berror = sqrt( nin );
9380 0           bmean = bsignal / nin;
9381             }
9382              
9383             else {
9384 0           croak( "internal error" );
9385             }
9386            
9387              
9388 0           bsnr = bsignal / berror;
9389 0           snr_ok = bsnr >= min_snr;
9390              
9391            
9392 0           rc |= \
9393 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
9394 0           | \
9395 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
9396 0           | \
9397             ( nin >= min_nelem \
9398 0 0         && bwidth >= min_width \
9399 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
9400             ; \
9401             ;
9402              
9403 0 0         if (rc)
9404 0           break;
9405             }
9406              
9407             /* fix up index for events initially stuck in folded bins */
9408 0           PDL_Indx curind1 = curind+1;
9409             PDL_Indx ni;
9410              
9411 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
9412 0 0         ni < __privtrans->ind_sizes[0] ;
9413 0           ni++ ) {
9414             #ifdef PDL_BAD_CODE
9415             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
9416             #endif /* PDL_BAD_CODE */
9417 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
9418             }
9419 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9420 0           rc |= BIN_RC_FOLDED;
9421             }
9422              
9423            
9424 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
9425 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
9426 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
9427 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
9428            
9429 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
9430            
9431 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
9432 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
9433 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9434 0 0         if ( error_sdev ) {
9435 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
9436 0 0         if ( have_error ) {
9437 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9438 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
9439 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
9440             }
9441             else {
9442 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
9443             }
9444             }
9445 0 0         else if ( error_rss ) {
9446 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
9447 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9448             }
9449            
9450             }
9451             /* adjust for possibility of last bin being empty */
9452 0           (nbins_datap)[0] = curind + ( nin != 0 );
9453 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
9454             }
9455 0           } break;
9456 35           case PDL_D: {
9457 35 50         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    100          
    50          
    50          
9458             {
9459              
9460              
9461              
9462              
9463 35           int flags = __params->optflags;
9464              
9465 35           int have_width = flags & BIN_ARG_HAVE_WIDTH;
9466 35           int have_error = flags & BIN_ARG_HAVE_ERROR;
9467 35           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
9468 35           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
9469 35           int error_rss = flags & BIN_ARG_ERROR_RSS;
9470 35           int fold_last_bin = flags & BIN_ARG_FOLD;
9471 35           int set_bad = flags & BIN_ARG_SET_BAD;
9472              
9473 35           PDL_Indx min_nelem = __params->min_nelem;
9474 35           PDL_Indx max_nelem = __params->max_nelem;
9475 35           double max_width = __params->max_width;
9476 35           double min_width = __params->min_width;
9477 35           double min_snr = __params->min_snr;
9478              
9479             /* simplify the logic below by setting bounds values to their most
9480             permissive extremes if they aren't needed. */
9481              
9482 35 100         if ( max_width == 0 )
9483 26           max_width = DBL_MAX;
9484              
9485 35 100         if ( max_nelem == 0 )
9486 17           max_nelem = LONG_MAX;
9487              
9488 140 50         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    50          
    50          
    50          
    50          
    100          
    100          
9489              
9490 35           PDL_Indx curind = 0; /* index of current bin */
9491              
9492             /* Declare bin counters */
9493 35           int rc = 0; /* status */
9494 35           double bsignal = 0; /* sum of signal */
9495 35           double bmean = 0; /* mean of (weighted) signal */
9496 35           double bwidth = 0; /* width (if applicable) */
9497 35           double bsnr = 0; /* SNR */
9498 35           double berror2 = 0; /* sum of error^2 */
9499 35           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
9500 35           double bm2 = 0; /* unnormalized variance */
9501 35           double bsignal2 = 0; /* sum of signal^2 */
9502 35           double bweight = 0; /* sum of 1/error*2 */
9503 35           double bweight_sig = 0; /* sum of weight * signal */
9504 35           double bweight_sig2 = 0; /* sum of weight * signal**2 */
9505 35           int bad_error = 0; /* if error2 is not good */
9506 35           int lastrc = 0; /* carryover status from previous loop */
9507 35           PDL_Indx nin = 0; /* number of elements in the current bin */
9508              
9509 25145 100         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
9510              
9511 25110           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
9512             double error;
9513             double error2;
9514             double width;
9515              
9516 25110           int snr_ok = 0;
9517              
9518 25110 100         if ( have_error ) {
9519 14106           error = (error_datap)[0+(__inc_error_n*(n))];
9520 14106           error2 = error * error;
9521             }
9522              
9523 25110 100         if ( have_width )
9524 8040           width = (width_datap)[0+(__inc_width_n*(n))];
9525              
9526             #ifdef PDL_BAD_CODE
9527             if ( PDL_ISBAD2(signal,signal_badval,D,signal_badval_isnan)
9528             ||
9529             have_error && PDL_ISBAD2(error,error_badval,D,error_badval_isnan) ) {
9530             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
9531             continue;
9532             }
9533             #endif /* PDL_BAD_CODE */
9534 25110           (index_datap)[0+(__inc_index_n*(n))] = curind;
9535              
9536            
9537 25110           bsignal += signal;
9538 25110           nin += 1;
9539              
9540             /* calculate error */
9541 25110 100         if ( error_sdev ) {
9542              
9543             /* weighted standard deviation */
9544 13006 100         if ( have_error ) {
9545            
9546             /* incremental algorithm for possibly weighted standard deviation; see
9547             https: */
9548 4004           double _weight = 1 / ( error * error );
9549 4004           double _sum_weight = bweight += _weight;
9550 4004           double delta = signal - bmean;
9551 4004           bmean += delta * _weight / _sum_weight;
9552 4004           bm2 += _weight * delta * ( signal - bmean );
9553              
9554 4004           bad_error = nin <= 1;
9555 4004           berror = bad_error
9556             ? DBL_MAX
9557 4004 100         : sqrt( bm2 * nin / bweight / (nin - 1) );
9558            
9559             }
9560              
9561             else {
9562            
9563             /* incremental algorithm for possibly weighted standard deviation; see
9564             https: */
9565 9002           double _weight = 1;
9566 9002           double _sum_weight = nin;
9567 9002           double delta = signal - bmean;
9568 9002           bmean += delta * _weight / _sum_weight;
9569 9002           bm2 += _weight * delta * ( signal - bmean );
9570              
9571 9002           bad_error = nin <= 1;
9572 9002           berror = bad_error
9573             ? DBL_MAX
9574 9002 100         : sqrt( bm2 * 1 / (nin - 1) );
9575            
9576             }
9577             }
9578              
9579 12104 100         else if ( error_rss ) {
9580            
9581             /* incremental algorithm for mean; see
9582             https: */
9583 10102           double _error2 = error2;
9584 10102           double _weight = 1 / ( error * error );
9585 10102           double delta = signal - bmean;
9586              
9587 10102           bweight += _weight;
9588 10102           bmean += delta * _weight / bweight;
9589 10102           berror2 += _error2;
9590 10102           berror = sqrt( berror2 );
9591            
9592             }
9593              
9594 2002 50         else if ( error_poisson ) {
9595 2002           berror = sqrt( nin );
9596 2002           bmean = bsignal / nin;
9597             }
9598              
9599             else {
9600 0           croak( "internal error" );
9601             }
9602            
9603              
9604 25110           bsnr = bsignal / berror;
9605 25110           snr_ok = bsnr >= min_snr;
9606              
9607 25110 100         if ( have_width )
9608 8040           bwidth += width;
9609              
9610 25110 100         if ( nin == 1 )
9611 7206           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
9612              
9613            
9614 25110           rc |= \
9615 25110 100         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
9616 25110           | \
9617 25110 100         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
9618 25110           | \
9619             ( nin >= min_nelem \
9620 15180 100         && bwidth >= min_width \
9621 40290 100         && snr_ok ? BIN_RC_OK : 0 ) \
    100          
9622             ; \
9623             ;
9624              
9625 25110 100         if ( rc ) {
9626 7183           rc |= lastrc;
9627              
9628            
9629 7183           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
9630 7183           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
9631 7183           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
9632 7183 100         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
9633            
9634 7183           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
9635            
9636 7183           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
9637 7183           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
9638 7183           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9639 7183 100         if ( error_sdev ) {
9640 3551           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
9641 3551 100         if ( have_error ) {
9642 400           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9643 400           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
9644 400           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
9645             }
9646             else {
9647 3151           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
9648             }
9649             }
9650 3632 100         else if ( error_rss ) {
9651 3432           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
9652 3432           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9653             }
9654            
9655              
9656 7183           curind++;
9657              
9658             /* Reset bin counters */
9659 7183           rc = 0;
9660 7183           bsignal = 0;
9661 7183           bmean = 0;
9662 7183           bwidth = 0;
9663 7183           bsnr = 0;
9664 7183           berror2 = 0;
9665 7183           berror = 0;
9666 7183           bm2 = 0;
9667 7183           bsignal2 = 0;
9668 7183           bweight = 0;
9669 7183           bweight_sig = 0;
9670 7183           bweight_sig2 = 0;
9671 7183           bad_error = 0;
9672 7183           lastrc = 0;
9673 7183           nin = 0;
9674              
9675             }
9676              
9677 17927 100         else if ( snr_ok ) {
9678 27           lastrc = BIN_RC_GTMINSN;
9679             }
9680              
9681             else {
9682 17900           lastrc = 0;
9683             }
9684             }} /* Close n */
9685              
9686              
9687             /* record last bin if it's not empty */
9688 35 100         if ( nin ) {
9689              
9690             /* needed for SET_RESULTS */
9691 23           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
9692              
9693 23           rc = 0;
9694 23           bad_error = 0;
9695              
9696             /* a non empty bin means that we didn't meet constraints. fold it into
9697             the previous bin if requested & possible. sometimes that will
9698             actually lower the S/N of the previous bin; keep going until
9699             we can't fold anymore or we get the proper S/N
9700             */
9701 23 100         if ( fold_last_bin && curind > 0 ) {
    50          
9702              
9703              
9704 7 50         while ( --curind > 0 ) {
9705             double tmp;
9706 7           int snr_ok = 0;
9707 7           PDL_Indx nin_last = nin;
9708              
9709            
9710 7           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
9711 7           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
9712              
9713             /* calculate error */
9714 7 100         if ( error_sdev ) {
9715              
9716             /* weighted standard deviation */
9717 3 100         if ( have_error ) {
9718            
9719             /* parallel algorithm (as we're adding bins together) for
9720             (possibly) weighted standard deviation; see
9721             https: */
9722 2           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
9723 2           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
9724 2           double mean_b = bmean;
9725 2           double weight_a = _weight;
9726 2           double weight_b = bweight;
9727 2           double weight_x = bweight += weight_a;
9728 2           double delta = mean_b - mean_a;
9729 2           bmean = mean_a + delta * weight_b / weight_x;
9730 2           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
9731 2           + delta * delta * weight_a * weight_b / weight_x;
9732              
9733 2           bad_error = nin <= 1;
9734 2           berror = bad_error
9735             ? DBL_MAX
9736 2 50         : sqrt( bm2 * nin / bweight / (nin - 1) );
9737            
9738             }
9739              
9740             else {
9741            
9742             /* parallel algorithm (as we're adding bins together) for
9743             (possibly) weighted standard deviation; see
9744             https: */
9745 1           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
9746 1           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
9747 1           double mean_b = bmean;
9748 1           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
9749 1           double weight_b = nin_last;
9750 1           double weight_x = nin;
9751 1           double delta = mean_b - mean_a;
9752 1           bmean = mean_a + delta * weight_b / weight_x;
9753 1           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
9754 1           + delta * delta * weight_a * weight_b / weight_x;
9755              
9756 1           bad_error = nin <= 1;
9757 1           berror = bad_error
9758             ? DBL_MAX
9759 1 50         : sqrt( bm2 * 1 / (nin - 1) );
9760            
9761             }
9762             }
9763              
9764 4 100         else if ( error_rss ) {
9765            
9766             /* parallel algorithm (as we're adding bins together) for
9767             mean; see
9768             https: */
9769 3           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
9770 3           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
9771 3           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
9772 3           double mean_b = bmean;
9773 3           double weight_a = _weight;
9774 3           double weight_b = bweight;
9775 3           double weight_x = bweight += weight_a;
9776 3           double delta = mean_b - mean_a;
9777 3           bmean = mean_a + delta * weight_b / weight_x;
9778              
9779 3           berror2 += _error2;
9780 3           berror = sqrt( berror2 );
9781            
9782             }
9783              
9784 1 50         else if ( error_poisson ) {
9785 1           berror = sqrt( nin );
9786 1           bmean = bsignal / nin;
9787             }
9788              
9789             else {
9790 0           croak( "internal error" );
9791             }
9792            
9793              
9794 7           bsnr = bsignal / berror;
9795 7           snr_ok = bsnr >= min_snr;
9796              
9797            
9798 7           rc |= \
9799 7 100         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
9800 7           | \
9801 7 50         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
9802 7           | \
9803             ( nin >= min_nelem \
9804 7 50         && bwidth >= min_width \
9805 14 50         && snr_ok ? BIN_RC_OK : 0 ) \
    100          
9806             ; \
9807             ;
9808              
9809 7 50         if (rc)
9810 7           break;
9811             }
9812              
9813             /* fix up index for events initially stuck in folded bins */
9814 7           PDL_Indx curind1 = curind+1;
9815             PDL_Indx ni;
9816              
9817 7           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
9818 21 100         ni < __privtrans->ind_sizes[0] ;
9819 14           ni++ ) {
9820             #ifdef PDL_BAD_CODE
9821             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
9822             #endif /* PDL_BAD_CODE */
9823 14           (index_datap)[0+(__inc_index_n*(ni))] = curind;
9824             }
9825 7           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9826 7           rc |= BIN_RC_FOLDED;
9827             }
9828              
9829            
9830 23           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
9831 23           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
9832 23           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
9833 23 100         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
9834            
9835 23           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
9836            
9837 23           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
9838 23           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
9839 23           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
9840 23 100         if ( error_sdev ) {
9841 8           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
9842 8 100         if ( have_error ) {
9843 2           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9844 2           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
9845 2           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
9846             }
9847             else {
9848 6           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
9849             }
9850             }
9851 15 100         else if ( error_rss ) {
9852 14           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
9853 14           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
9854             }
9855            
9856             }
9857             /* adjust for possibility of last bin being empty */
9858 35           (nbins_datap)[0] = curind + ( nin != 0 );
9859 35 50         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    50          
9860             }
9861 35           } break;
9862 0           case PDL_LD: {
9863 0 0         PDL_DECLARE_PARAMS_bin_adaptive_snr_1(PDL_LDouble,E,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Long,L,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
9864             {
9865              
9866              
9867              
9868              
9869 0           int flags = __params->optflags;
9870              
9871 0           int have_width = flags & BIN_ARG_HAVE_WIDTH;
9872 0           int have_error = flags & BIN_ARG_HAVE_ERROR;
9873 0           int error_sdev = flags & BIN_ARG_ERROR_SDEV;
9874 0           int error_poisson = flags & BIN_ARG_ERROR_POISSON;
9875 0           int error_rss = flags & BIN_ARG_ERROR_RSS;
9876 0           int fold_last_bin = flags & BIN_ARG_FOLD;
9877 0           int set_bad = flags & BIN_ARG_SET_BAD;
9878              
9879 0           PDL_Indx min_nelem = __params->min_nelem;
9880 0           PDL_Indx max_nelem = __params->max_nelem;
9881 0           double max_width = __params->max_width;
9882 0           double min_width = __params->min_width;
9883 0           double min_snr = __params->min_snr;
9884              
9885             /* simplify the logic below by setting bounds values to their most
9886             permissive extremes if they aren't needed. */
9887              
9888 0 0         if ( max_width == 0 )
9889 0           max_width = DBL_MAX;
9890              
9891 0 0         if ( max_nelem == 0 )
9892 0           max_nelem = LONG_MAX;
9893              
9894 0 0         PDL_BROADCASTLOOP_START_bin_adaptive_snr_readdata
    0          
    0          
    0          
    0          
    0          
    0          
9895              
9896 0           PDL_Indx curind = 0; /* index of current bin */
9897              
9898             /* Declare bin counters */
9899 0           int rc = 0; /* status */
9900 0           double bsignal = 0; /* sum of signal */
9901 0           double bmean = 0; /* mean of (weighted) signal */
9902 0           double bwidth = 0; /* width (if applicable) */
9903 0           double bsnr = 0; /* SNR */
9904 0           double berror2 = 0; /* sum of error^2 */
9905 0           double berror = 0; /* sqrt( berror2 ) or DBL_MAX */
9906 0           double bm2 = 0; /* unnormalized variance */
9907 0           double bsignal2 = 0; /* sum of signal^2 */
9908 0           double bweight = 0; /* sum of 1/error*2 */
9909 0           double bweight_sig = 0; /* sum of weight * signal */
9910 0           double bweight_sig2 = 0; /* sum of weight * signal**2 */
9911 0           int bad_error = 0; /* if error2 is not good */
9912 0           int lastrc = 0; /* carryover status from previous loop */
9913 0           PDL_Indx nin = 0; /* number of elements in the current bin */
9914              
9915 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
9916              
9917 0           double signal = (signal_datap)[0+(__inc_signal_n*(n))];
9918             double error;
9919             double error2;
9920             double width;
9921              
9922 0           int snr_ok = 0;
9923              
9924 0 0         if ( have_error ) {
9925 0           error = (error_datap)[0+(__inc_error_n*(n))];
9926 0           error2 = error * error;
9927             }
9928              
9929 0 0         if ( have_width )
9930 0           width = (width_datap)[0+(__inc_width_n*(n))];
9931              
9932             #ifdef PDL_BAD_CODE
9933             if ( PDL_ISBAD2(signal,signal_badval,E,signal_badval_isnan)
9934             ||
9935             have_error && PDL_ISBAD2(error,error_badval,E,error_badval_isnan) ) {
9936             (index_datap)[0+(__inc_index_n*(n))]=index_badval;
9937             continue;
9938             }
9939             #endif /* PDL_BAD_CODE */
9940 0           (index_datap)[0+(__inc_index_n*(n))] = curind;
9941              
9942            
9943 0           bsignal += signal;
9944 0           nin += 1;
9945              
9946             /* calculate error */
9947 0 0         if ( error_sdev ) {
9948              
9949             /* weighted standard deviation */
9950 0 0         if ( have_error ) {
9951            
9952             /* incremental algorithm for possibly weighted standard deviation; see
9953             https: */
9954 0           double _weight = 1 / ( error * error );
9955 0           double _sum_weight = bweight += _weight;
9956 0           double delta = signal - bmean;
9957 0           bmean += delta * _weight / _sum_weight;
9958 0           bm2 += _weight * delta * ( signal - bmean );
9959              
9960 0           bad_error = nin <= 1;
9961 0           berror = bad_error
9962             ? DBL_MAX
9963 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
9964            
9965             }
9966              
9967             else {
9968            
9969             /* incremental algorithm for possibly weighted standard deviation; see
9970             https: */
9971 0           double _weight = 1;
9972 0           double _sum_weight = nin;
9973 0           double delta = signal - bmean;
9974 0           bmean += delta * _weight / _sum_weight;
9975 0           bm2 += _weight * delta * ( signal - bmean );
9976              
9977 0           bad_error = nin <= 1;
9978 0           berror = bad_error
9979             ? DBL_MAX
9980 0 0         : sqrt( bm2 * 1 / (nin - 1) );
9981            
9982             }
9983             }
9984              
9985 0 0         else if ( error_rss ) {
9986            
9987             /* incremental algorithm for mean; see
9988             https: */
9989 0           double _error2 = error2;
9990 0           double _weight = 1 / ( error * error );
9991 0           double delta = signal - bmean;
9992              
9993 0           bweight += _weight;
9994 0           bmean += delta * _weight / bweight;
9995 0           berror2 += _error2;
9996 0           berror = sqrt( berror2 );
9997            
9998             }
9999              
10000 0 0         else if ( error_poisson ) {
10001 0           berror = sqrt( nin );
10002 0           bmean = bsignal / nin;
10003             }
10004              
10005             else {
10006 0           croak( "internal error" );
10007             }
10008            
10009              
10010 0           bsnr = bsignal / berror;
10011 0           snr_ok = bsnr >= min_snr;
10012              
10013 0 0         if ( have_width )
10014 0           bwidth += width;
10015              
10016 0 0         if ( nin == 1 )
10017 0           (ifirst_datap)[0+(__inc_ifirst_n*(curind))] = n;
10018              
10019            
10020 0           rc |= \
10021 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
10022 0           | \
10023 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
10024 0           | \
10025             ( nin >= min_nelem \
10026 0 0         && bwidth >= min_width \
10027 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
10028             ; \
10029             ;
10030              
10031 0 0         if ( rc ) {
10032 0           rc |= lastrc;
10033              
10034            
10035 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
10036 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
10037 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
10038 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
10039            
10040 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
10041            
10042 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
10043 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
10044 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
10045 0 0         if ( error_sdev ) {
10046 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
10047 0 0         if ( have_error ) {
10048 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
10049 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
10050 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
10051             }
10052             else {
10053 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
10054             }
10055             }
10056 0 0         else if ( error_rss ) {
10057 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
10058 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
10059             }
10060            
10061              
10062 0           curind++;
10063              
10064             /* Reset bin counters */
10065 0           rc = 0;
10066 0           bsignal = 0;
10067 0           bmean = 0;
10068 0           bwidth = 0;
10069 0           bsnr = 0;
10070 0           berror2 = 0;
10071 0           berror = 0;
10072 0           bm2 = 0;
10073 0           bsignal2 = 0;
10074 0           bweight = 0;
10075 0           bweight_sig = 0;
10076 0           bweight_sig2 = 0;
10077 0           bad_error = 0;
10078 0           lastrc = 0;
10079 0           nin = 0;
10080              
10081             }
10082              
10083 0 0         else if ( snr_ok ) {
10084 0           lastrc = BIN_RC_GTMINSN;
10085             }
10086              
10087             else {
10088 0           lastrc = 0;
10089             }
10090             }} /* Close n */
10091              
10092              
10093             /* record last bin if it's not empty */
10094 0 0         if ( nin ) {
10095              
10096             /* needed for SET_RESULTS */
10097 0           PDL_Indx n = __privtrans->ind_sizes[0] - 1;
10098              
10099 0           rc = 0;
10100 0           bad_error = 0;
10101              
10102             /* a non empty bin means that we didn't meet constraints. fold it into
10103             the previous bin if requested & possible. sometimes that will
10104             actually lower the S/N of the previous bin; keep going until
10105             we can't fold anymore or we get the proper S/N
10106             */
10107 0 0         if ( fold_last_bin && curind > 0 ) {
    0          
10108              
10109              
10110 0 0         while ( --curind > 0 ) {
10111             double tmp;
10112 0           int snr_ok = 0;
10113 0           PDL_Indx nin_last = nin;
10114              
10115            
10116 0           bsignal += (b_signal_datap)[0+(__inc_b_signal_n*(curind))];
10117 0           nin += (nelem_datap)[0+(__inc_nelem_n*(curind))];
10118              
10119             /* calculate error */
10120 0 0         if ( error_sdev ) {
10121              
10122             /* weighted standard deviation */
10123 0 0         if ( have_error ) {
10124            
10125             /* parallel algorithm (as we're adding bins together) for
10126             (possibly) weighted standard deviation; see
10127             https: */
10128 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
10129 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
10130 0           double mean_b = bmean;
10131 0           double weight_a = _weight;
10132 0           double weight_b = bweight;
10133 0           double weight_x = bweight += weight_a;
10134 0           double delta = mean_b - mean_a;
10135 0           bmean = mean_a + delta * weight_b / weight_x;
10136 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
10137 0           + delta * delta * weight_a * weight_b / weight_x;
10138              
10139 0           bad_error = nin <= 1;
10140 0           berror = bad_error
10141             ? DBL_MAX
10142 0 0         : sqrt( bm2 * nin / bweight / (nin - 1) );
10143            
10144             }
10145              
10146             else {
10147            
10148             /* parallel algorithm (as we're adding bins together) for
10149             (possibly) weighted standard deviation; see
10150             https: */
10151 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
10152 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
10153 0           double mean_b = bmean;
10154 0           double weight_a = (nelem_datap)[0+(__inc_nelem_n*(curind))];
10155 0           double weight_b = nin_last;
10156 0           double weight_x = nin;
10157 0           double delta = mean_b - mean_a;
10158 0           bmean = mean_a + delta * weight_b / weight_x;
10159 0           bm2 += (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))]
10160 0           + delta * delta * weight_a * weight_b / weight_x;
10161              
10162 0           bad_error = nin <= 1;
10163 0           berror = bad_error
10164             ? DBL_MAX
10165 0 0         : sqrt( bm2 * 1 / (nin - 1) );
10166            
10167             }
10168             }
10169              
10170 0 0         else if ( error_rss ) {
10171            
10172             /* parallel algorithm (as we're adding bins together) for
10173             mean; see
10174             https: */
10175 0           double _error2 = (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))];
10176 0           double _weight = (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))];
10177 0           double mean_a = (b_mean_datap)[0+(__inc_b_mean_n*(curind))];
10178 0           double mean_b = bmean;
10179 0           double weight_a = _weight;
10180 0           double weight_b = bweight;
10181 0           double weight_x = bweight += weight_a;
10182 0           double delta = mean_b - mean_a;
10183 0           bmean = mean_a + delta * weight_b / weight_x;
10184              
10185 0           berror2 += _error2;
10186 0           berror = sqrt( berror2 );
10187            
10188             }
10189              
10190 0 0         else if ( error_poisson ) {
10191 0           berror = sqrt( nin );
10192 0           bmean = bsignal / nin;
10193             }
10194              
10195             else {
10196 0           croak( "internal error" );
10197             }
10198            
10199              
10200 0           bsnr = bsignal / berror;
10201 0           snr_ok = bsnr >= min_snr;
10202              
10203            
10204 0           rc |= \
10205 0 0         ( nin >= max_nelem ? BIN_RC_GENMAX : 0 ) \
10206 0           | \
10207 0 0         ( bwidth >= max_width ? BIN_RC_GEWMAX : 0 ) \
10208 0           | \
10209             ( nin >= min_nelem \
10210 0 0         && bwidth >= min_width \
10211 0 0         && snr_ok ? BIN_RC_OK : 0 ) \
    0          
10212             ; \
10213             ;
10214              
10215 0 0         if (rc)
10216 0           break;
10217             }
10218              
10219             /* fix up index for events initially stuck in folded bins */
10220 0           PDL_Indx curind1 = curind+1;
10221             PDL_Indx ni;
10222              
10223 0           for ( ni = (ifirst_datap)[0+(__inc_ifirst_n*(curind1))] ;
10224 0 0         ni < __privtrans->ind_sizes[0] ;
10225 0           ni++ ) {
10226             #ifdef PDL_BAD_CODE
10227             if ( !PDL_ISBAD2((index_datap)[0+(__inc_index_n*(ni))],index_badval,N,index_badval_isnan) )
10228             #endif /* PDL_BAD_CODE */
10229 0           (index_datap)[0+(__inc_index_n*(ni))] = curind;
10230             }
10231 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
10232 0           rc |= BIN_RC_FOLDED;
10233             }
10234              
10235            
10236 0           (rc_datap)[0+(__inc_rc_n*(curind))] = rc;
10237 0           (b_signal_datap)[0+(__inc_b_signal_n*(curind))] = bsignal;
10238 0           (b_mean_datap)[0+(__inc_b_mean_n*(curind))] = bmean;
10239 0 0         if ( have_width ) (b_width_datap)[0+(__inc_b_width_nwidth*(curind))] = bwidth;
10240            
10241 0           (b_error_datap)[0+(__inc_b_error_n*(curind))] = berror;
10242            
10243 0           (b_snr_datap)[0+(__inc_b_snr_n*(curind))] = bsnr;
10244 0           (nelem_datap)[0+(__inc_nelem_n*(curind))] = nin;
10245 0           (ilast_datap)[0+(__inc_ilast_n*(curind))] = n;
10246 0 0         if ( error_sdev ) {
10247 0           (b_m2_datap)[0+(__inc_b_m2_nsdev*(curind))] = bm2;
10248 0 0         if ( have_error ) {
10249 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
10250 0           (b_weight_sig_datap)[0+(__inc_b_weight_sig_nwsdev*(curind))] = bweight_sig;
10251 0           (b_weight_sig2_datap)[0+(__inc_b_weight_sig2_nwsdev*(curind))] = bweight_sig2;
10252             }
10253             else {
10254 0           (b_signal2_datap)[0+(__inc_b_signal2_nsdev*(curind))] = bsignal2;
10255             }
10256             }
10257 0 0         else if ( error_rss ) {
10258 0           (b_error2_datap)[0+(__inc_b_error2_nrss*(curind))] = berror2;
10259 0           (b_weight_datap)[0+(__inc_b_weight_nweight*(curind))] = bweight;
10260             }
10261            
10262             }
10263             /* adjust for possibility of last bin being empty */
10264 0           (nbins_datap)[0] = curind + ( nin != 0 );
10265 0 0         PDL_BROADCASTLOOP_END_bin_adaptive_snr_readdata
    0          
10266             }
10267 0           } break;
10268 0           default: return PDL->make_error(PDL_EUSERERROR, "PP INTERNAL ERROR in bin_adaptive_snr: unhandled datatype(%d), only handles (ABSULKNPQFDE)! PLEASE MAKE A BUG REPORT\n", __privtrans->__datatype);
10269             }
10270             #undef PDL_IF_BAD
10271             }
10272 35           return PDL_err;
10273             }
10274              
10275             static pdl_datatypes pdl_bin_adaptive_snr_vtable_gentypes[] = { PDL_SB, PDL_B, PDL_S, PDL_US, PDL_L, PDL_UL, PDL_IND, PDL_ULL, PDL_LL, PDL_F, PDL_D, PDL_LD, -1 };
10276             static PDL_Indx pdl_bin_adaptive_snr_vtable_realdims[] = { 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
10277             static char *pdl_bin_adaptive_snr_vtable_parnames[] = { "signal","error","width","index","nbins","nelem","b_signal","b_error","b_mean","b_snr","b_width","ifirst","ilast","rc","b_error2","b_signal2","b_m2","b_weight","b_weight_sig","b_weight_sig2" };
10278             static short pdl_bin_adaptive_snr_vtable_parflags[] = {
10279             0,
10280             0,
10281             0,
10282             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10283             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10284             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10285             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10286             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10287             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10288             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10289             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10290             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10291             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10292             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10293             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10294             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10295             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10296             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10297             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
10298             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE
10299             };
10300             static pdl_datatypes pdl_bin_adaptive_snr_vtable_partypes[] = { -1, -1, -1, PDL_IND, PDL_IND, PDL_IND, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D, PDL_IND, PDL_IND, PDL_L, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D };
10301             static PDL_Indx pdl_bin_adaptive_snr_vtable_realdims_starts[] = { 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
10302             static PDL_Indx pdl_bin_adaptive_snr_vtable_realdims_ind_ids[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 2, 2, 3, 5, 5 };
10303             static char *pdl_bin_adaptive_snr_vtable_indnames[] = { "n","nrss","nsdev","nweight","nwidth","nwsdev" };
10304             pdl_transvtable pdl_bin_adaptive_snr_vtable = {
10305             PDL_TRANS_DO_BROADCAST|PDL_TRANS_BADPROCESS, 0, pdl_bin_adaptive_snr_vtable_gentypes, 3, 20, NULL /*CORE21*/,
10306             pdl_bin_adaptive_snr_vtable_realdims, pdl_bin_adaptive_snr_vtable_parnames,
10307             pdl_bin_adaptive_snr_vtable_parflags, pdl_bin_adaptive_snr_vtable_partypes,
10308             pdl_bin_adaptive_snr_vtable_realdims_starts, pdl_bin_adaptive_snr_vtable_realdims_ind_ids, 19,
10309             6, pdl_bin_adaptive_snr_vtable_indnames,
10310             pdl_bin_adaptive_snr_redodims, pdl_bin_adaptive_snr_readdata, NULL,
10311             NULL,
10312             sizeof(pdl_params_bin_adaptive_snr),"CXC::PDL::Bin1D::bin_adaptive_snr"
10313             };
10314              
10315              
10316 35           pdl_error pdl_run_bin_adaptive_snr(pdl *signal,pdl *error,pdl *width,pdl *index,pdl *nbins,pdl *nelem,pdl *b_signal,pdl *b_error,pdl *b_mean,pdl *b_snr,pdl *b_width,pdl *ifirst,pdl *ilast,pdl *rc,unsigned long optflags,double min_snr,PDL_Indx min_nelem,PDL_Indx max_nelem,double min_width,double max_width) {
10317 35           pdl_error PDL_err = {0, NULL, 0};
10318 35 50         if (!PDL) return (pdl_error){PDL_EFATAL, "PDL core struct is NULL, can't continue",0};
10319 35           pdl_trans *__privtrans = PDL->create_trans(&pdl_bin_adaptive_snr_vtable);
10320 35 50         if (!__privtrans) return PDL->make_error_simple(PDL_EFATAL, "Couldn't create trans");
10321 35           pdl_params_bin_adaptive_snr *__params = __privtrans->params;
10322 35           __privtrans->pdls[0] = signal;
10323 35           __privtrans->pdls[1] = error;
10324 35           __privtrans->pdls[2] = width;
10325 35           __privtrans->pdls[3] = index;
10326 35           __privtrans->pdls[4] = nbins;
10327 35           __privtrans->pdls[5] = nelem;
10328 35           __privtrans->pdls[6] = b_signal;
10329 35           __privtrans->pdls[7] = b_error;
10330 35           __privtrans->pdls[8] = b_mean;
10331 35           __privtrans->pdls[9] = b_snr;
10332 35           __privtrans->pdls[10] = b_width;
10333 35           __privtrans->pdls[11] = ifirst;
10334 35           __privtrans->pdls[12] = ilast;
10335 35           __privtrans->pdls[13] = rc;
10336 35 50         PDL_RETERROR(PDL_err, PDL->type_coerce(__privtrans));
10337 35           (__params->optflags) = (optflags); /* CType.get_copy */
10338 35           (__params->min_snr) = (min_snr); /* CType.get_copy */
10339 35           (__params->min_nelem) = (min_nelem); /* CType.get_copy */
10340 35           (__params->max_nelem) = (max_nelem); /* CType.get_copy */
10341 35           (__params->min_width) = (min_width); /* CType.get_copy */
10342 35           (__params->max_width) = (max_width); /* CType.get_copy */
10343 35 50         PDL_RETERROR(PDL_err, PDL->make_trans_mutual(__privtrans));
10344 35           return PDL_err;
10345             }
10346              
10347             #line 1846 "lib/PDL/PP.pm"
10348             typedef struct pdl_params_bin_on_index {
10349             #line 10350 "Bin1D.xs"
10350             unsigned long optflags;
10351             PDL_Indx nbins_max;
10352             } pdl_params_bin_on_index;
10353              
10354              
10355             #line 1857 "lib/PDL/PP.pm"
10356             pdl_error pdl_bin_on_index_redodims(pdl_trans *__privtrans) {
10357             pdl_error PDL_err = {0, NULL, 0};
10358             #line 10359 "Bin1D.xs"
10359 50           pdl_params_bin_on_index *__params = __privtrans->params; (void)__params;
10360             #ifndef PDL_DECLARE_PARAMS_bin_on_index_0
10361             #define PDL_DECLARE_PARAMS_bin_on_index_0(PDL_TYPE_OP,PDL_PPSYM_OP,PDL_TYPE_PARAM_index,PDL_PPSYM_PARAM_index,PDL_TYPE_PARAM_imin,PDL_PPSYM_PARAM_imin,PDL_TYPE_PARAM_nbins,PDL_PPSYM_PARAM_nbins,PDL_TYPE_PARAM_b_count,PDL_PPSYM_PARAM_b_count,PDL_TYPE_PARAM_b_data,PDL_PPSYM_PARAM_b_data,PDL_TYPE_PARAM_b_weight,PDL_PPSYM_PARAM_b_weight,PDL_TYPE_PARAM_b_weight2,PDL_PPSYM_PARAM_b_weight2,PDL_TYPE_PARAM_b_mean,PDL_PPSYM_PARAM_b_mean,PDL_TYPE_PARAM_b_dmin,PDL_PPSYM_PARAM_b_dmin,PDL_TYPE_PARAM_b_dmax,PDL_PPSYM_PARAM_b_dmax,PDL_TYPE_PARAM_b_dev2,PDL_PPSYM_PARAM_b_dev2,PDL_TYPE_PARAM_b_data_error,PDL_PPSYM_PARAM_b_data_error,PDL_TYPE_PARAM_b_weight_error,PDL_PPSYM_PARAM_b_weight_error,PDL_TYPE_PARAM_b_weight2_error,PDL_PPSYM_PARAM_b_weight2_error) \
10362             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, data, (__privtrans->pdls[0]), 0, PDL_PPSYM_OP) \
10363             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_index, index, (__privtrans->pdls[1]), 0, PDL_PPSYM_PARAM_index) \
10364             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, weight, (__privtrans->pdls[2]), 0, PDL_PPSYM_OP) \
10365             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_imin, imin, (__privtrans->pdls[3]), 0, PDL_PPSYM_PARAM_imin) \
10366             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_nbins, nbins, (__privtrans->pdls[4]), 0, PDL_PPSYM_PARAM_nbins) \
10367             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_count, b_count, (__privtrans->pdls[5]), 0, PDL_PPSYM_PARAM_b_count) \
10368             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_data, b_data, (__privtrans->pdls[6]), 0, PDL_PPSYM_PARAM_b_data) \
10369             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight, b_weight, (__privtrans->pdls[7]), 0, PDL_PPSYM_PARAM_b_weight) \
10370             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight2, b_weight2, (__privtrans->pdls[8]), 0, PDL_PPSYM_PARAM_b_weight2) \
10371             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_mean, b_mean, (__privtrans->pdls[9]), 0, PDL_PPSYM_PARAM_b_mean) \
10372             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_dmin, b_dmin, (__privtrans->pdls[10]), 0, PDL_PPSYM_PARAM_b_dmin) \
10373             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_dmax, b_dmax, (__privtrans->pdls[11]), 0, PDL_PPSYM_PARAM_b_dmax) \
10374             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_dev2, b_dev2, (__privtrans->pdls[12]), 0, PDL_PPSYM_PARAM_b_dev2) \
10375             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_data_error, b_data_error, (__privtrans->pdls[13]), 0, PDL_PPSYM_PARAM_b_data_error) \
10376             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight_error, b_weight_error, (__privtrans->pdls[14]), 0, PDL_PPSYM_PARAM_b_weight_error) \
10377             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight2_error, b_weight2_error, (__privtrans->pdls[15]), 0, PDL_PPSYM_PARAM_b_weight2_error)
10378             #endif
10379             #define PDL_IF_BAD(t,f) f
10380 50           switch (__privtrans->__datatype) { /* Start generic switch */
10381 0           case PDL_SB: {
10382 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_SByte,A,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10383 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10384 0           } break;
10385 0           case PDL_B: {
10386 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_Byte,B,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10387 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10388 0           } break;
10389 0           case PDL_S: {
10390 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_Short,S,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10391 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10392 0           } break;
10393 0           case PDL_US: {
10394 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_Ushort,U,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10395 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10396 0           } break;
10397 0           case PDL_L: {
10398 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_Long,L,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10399 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10400 0           } break;
10401 0           case PDL_UL: {
10402 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_ULong,K,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10403 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10404 0           } break;
10405 0           case PDL_IND: {
10406 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10407 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10408 0           } break;
10409 0           case PDL_ULL: {
10410 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_ULongLong,P,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10411 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10412 0           } break;
10413 0           case PDL_LL: {
10414 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_LongLong,Q,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10415 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10416 0           } break;
10417 0           case PDL_F: {
10418 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_Float,F,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10419 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10420 0           } break;
10421 50           case PDL_D: {
10422 50 50         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
10423 50 100         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10424 50           } break;
10425 0           case PDL_LD: {
10426 0 0         PDL_DECLARE_PARAMS_bin_on_index_0(PDL_LDouble,E,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10427 0 0         { __privtrans->ind_sizes[1] = __params->nbins_max + (__params->optflags & BIN_ARG_SAVE_OOB ? 2 : 0 ); }
10428 0           } break;
10429 0           default: return PDL->make_error(PDL_EUSERERROR, "PP INTERNAL ERROR in bin_on_index: unhandled datatype(%d), only handles (ABSULKNPQFDE)! PLEASE MAKE A BUG REPORT\n", __privtrans->__datatype);
10430             }
10431             #undef PDL_IF_BAD
10432              
10433 50 50         PDL_RETERROR(PDL_err, PDL->redodims_default(__privtrans));
10434 50           return PDL_err;
10435             }
10436              
10437              
10438             #line 1857 "lib/PDL/PP.pm"
10439             pdl_error pdl_bin_on_index_readdata(pdl_trans *__privtrans) {
10440             pdl_error PDL_err = {0, NULL, 0};
10441             #line 10442 "Bin1D.xs"
10442 50           pdl_params_bin_on_index *__params = __privtrans->params; (void)__params;
10443 50           register PDL_Indx __n_size = __privtrans->ind_sizes[0];
10444 50           register PDL_Indx __nb_size = __privtrans->ind_sizes[1];
10445 50 50         if (!__privtrans->broadcast.incs) return PDL->make_error(PDL_EUSERERROR, "Error in bin_on_index:" "broadcast.incs NULL");
10446             /* broadcastloop declarations */
10447             int __brcloopval;
10448             register PDL_Indx __tind0,__tind1; /* counters along dim */
10449 50           register PDL_Indx __tnpdls = __privtrans->broadcast.npdls;
10450             /* dims here are how many steps along those dims */
10451 50           register PDL_Indx __tinc0_data = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,0,0);
10452 50           register PDL_Indx __tinc0_index = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,1,0);
10453 50           register PDL_Indx __tinc0_weight = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,2,0);
10454 50           register PDL_Indx __tinc0_imin = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,3,0);
10455 50           register PDL_Indx __tinc0_nbins = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,4,0);
10456 50           register PDL_Indx __tinc0_b_count = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,5,0);
10457 50           register PDL_Indx __tinc0_b_data = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,6,0);
10458 50           register PDL_Indx __tinc0_b_weight = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,7,0);
10459 50           register PDL_Indx __tinc0_b_weight2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,8,0);
10460 50           register PDL_Indx __tinc0_b_mean = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,9,0);
10461 50           register PDL_Indx __tinc0_b_dmin = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,10,0);
10462 50           register PDL_Indx __tinc0_b_dmax = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,11,0);
10463 50           register PDL_Indx __tinc0_b_dev2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,12,0);
10464 50           register PDL_Indx __tinc0_b_data_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,13,0);
10465 50           register PDL_Indx __tinc0_b_weight_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,14,0);
10466 50           register PDL_Indx __tinc0_b_weight2_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,15,0);
10467 50           register PDL_Indx __tinc1_data = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,0,1);
10468 50           register PDL_Indx __tinc1_index = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,1,1);
10469 50           register PDL_Indx __tinc1_weight = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,2,1);
10470 50           register PDL_Indx __tinc1_imin = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,3,1);
10471 50           register PDL_Indx __tinc1_nbins = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,4,1);
10472 50           register PDL_Indx __tinc1_b_count = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,5,1);
10473 50           register PDL_Indx __tinc1_b_data = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,6,1);
10474 50           register PDL_Indx __tinc1_b_weight = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,7,1);
10475 50           register PDL_Indx __tinc1_b_weight2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,8,1);
10476 50           register PDL_Indx __tinc1_b_mean = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,9,1);
10477 50           register PDL_Indx __tinc1_b_dmin = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,10,1);
10478 50           register PDL_Indx __tinc1_b_dmax = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,11,1);
10479 50           register PDL_Indx __tinc1_b_dev2 = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,12,1);
10480 50           register PDL_Indx __tinc1_b_data_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,13,1);
10481 50           register PDL_Indx __tinc1_b_weight_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,14,1);
10482 50           register PDL_Indx __tinc1_b_weight2_error = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,15,1);
10483             #define PDL_BROADCASTLOOP_START_bin_on_index_readdata PDL_BROADCASTLOOP_START( \
10484             readdata, \
10485             __privtrans->broadcast, \
10486             __privtrans->vtable, \
10487             data_datap += __offsp[0]; \
10488             index_datap += __offsp[1]; \
10489             weight_datap += __offsp[2]; \
10490             imin_datap += __offsp[3]; \
10491             nbins_datap += __offsp[4]; \
10492             b_count_datap += __offsp[5]; \
10493             b_data_datap += __offsp[6]; \
10494             b_weight_datap += __offsp[7]; \
10495             b_weight2_datap += __offsp[8]; \
10496             b_mean_datap += __offsp[9]; \
10497             b_dmin_datap += __offsp[10]; \
10498             b_dmax_datap += __offsp[11]; \
10499             b_dev2_datap += __offsp[12]; \
10500             b_data_error_datap += __offsp[13]; \
10501             b_weight_error_datap += __offsp[14]; \
10502             b_weight2_error_datap += __offsp[15]; \
10503             , \
10504             ( ,data_datap += __tinc1_data - __tinc0_data * __tdims0 \
10505             ,index_datap += __tinc1_index - __tinc0_index * __tdims0 \
10506             ,weight_datap += __tinc1_weight - __tinc0_weight * __tdims0 \
10507             ,imin_datap += __tinc1_imin - __tinc0_imin * __tdims0 \
10508             ,nbins_datap += __tinc1_nbins - __tinc0_nbins * __tdims0 \
10509             ,b_count_datap += __tinc1_b_count - __tinc0_b_count * __tdims0 \
10510             ,b_data_datap += __tinc1_b_data - __tinc0_b_data * __tdims0 \
10511             ,b_weight_datap += __tinc1_b_weight - __tinc0_b_weight * __tdims0 \
10512             ,b_weight2_datap += __tinc1_b_weight2 - __tinc0_b_weight2 * __tdims0 \
10513             ,b_mean_datap += __tinc1_b_mean - __tinc0_b_mean * __tdims0 \
10514             ,b_dmin_datap += __tinc1_b_dmin - __tinc0_b_dmin * __tdims0 \
10515             ,b_dmax_datap += __tinc1_b_dmax - __tinc0_b_dmax * __tdims0 \
10516             ,b_dev2_datap += __tinc1_b_dev2 - __tinc0_b_dev2 * __tdims0 \
10517             ,b_data_error_datap += __tinc1_b_data_error - __tinc0_b_data_error * __tdims0 \
10518             ,b_weight_error_datap += __tinc1_b_weight_error - __tinc0_b_weight_error * __tdims0 \
10519             ,b_weight2_error_datap += __tinc1_b_weight2_error - __tinc0_b_weight2_error * __tdims0 \
10520             ), \
10521             ( ,data_datap += __tinc0_data \
10522             ,index_datap += __tinc0_index \
10523             ,weight_datap += __tinc0_weight \
10524             ,imin_datap += __tinc0_imin \
10525             ,nbins_datap += __tinc0_nbins \
10526             ,b_count_datap += __tinc0_b_count \
10527             ,b_data_datap += __tinc0_b_data \
10528             ,b_weight_datap += __tinc0_b_weight \
10529             ,b_weight2_datap += __tinc0_b_weight2 \
10530             ,b_mean_datap += __tinc0_b_mean \
10531             ,b_dmin_datap += __tinc0_b_dmin \
10532             ,b_dmax_datap += __tinc0_b_dmax \
10533             ,b_dev2_datap += __tinc0_b_dev2 \
10534             ,b_data_error_datap += __tinc0_b_data_error \
10535             ,b_weight_error_datap += __tinc0_b_weight_error \
10536             ,b_weight2_error_datap += __tinc0_b_weight2_error \
10537             ) \
10538             )
10539             #define PDL_BROADCASTLOOP_END_bin_on_index_readdata PDL_BROADCASTLOOP_END( \
10540             __privtrans->broadcast, \
10541             data_datap -= __tinc1_data * __tdims1 + __offsp[0]; \
10542             index_datap -= __tinc1_index * __tdims1 + __offsp[1]; \
10543             weight_datap -= __tinc1_weight * __tdims1 + __offsp[2]; \
10544             imin_datap -= __tinc1_imin * __tdims1 + __offsp[3]; \
10545             nbins_datap -= __tinc1_nbins * __tdims1 + __offsp[4]; \
10546             b_count_datap -= __tinc1_b_count * __tdims1 + __offsp[5]; \
10547             b_data_datap -= __tinc1_b_data * __tdims1 + __offsp[6]; \
10548             b_weight_datap -= __tinc1_b_weight * __tdims1 + __offsp[7]; \
10549             b_weight2_datap -= __tinc1_b_weight2 * __tdims1 + __offsp[8]; \
10550             b_mean_datap -= __tinc1_b_mean * __tdims1 + __offsp[9]; \
10551             b_dmin_datap -= __tinc1_b_dmin * __tdims1 + __offsp[10]; \
10552             b_dmax_datap -= __tinc1_b_dmax * __tdims1 + __offsp[11]; \
10553             b_dev2_datap -= __tinc1_b_dev2 * __tdims1 + __offsp[12]; \
10554             b_data_error_datap -= __tinc1_b_data_error * __tdims1 + __offsp[13]; \
10555             b_weight_error_datap -= __tinc1_b_weight_error * __tdims1 + __offsp[14]; \
10556             b_weight2_error_datap -= __tinc1_b_weight2_error * __tdims1 + __offsp[15]; \
10557             )
10558 50           register PDL_Indx __inc_b_count_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,5,0)]; (void)__inc_b_count_nb;
10559 50           register PDL_Indx __inc_b_data_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,6,0)]; (void)__inc_b_data_nb;
10560 50           register PDL_Indx __inc_b_data_error_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,13,0)]; (void)__inc_b_data_error_nb;
10561 50           register PDL_Indx __inc_b_dev2_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,12,0)]; (void)__inc_b_dev2_nb;
10562 50           register PDL_Indx __inc_b_dmax_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,11,0)]; (void)__inc_b_dmax_nb;
10563 50           register PDL_Indx __inc_b_dmin_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,10,0)]; (void)__inc_b_dmin_nb;
10564 50           register PDL_Indx __inc_b_mean_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,9,0)]; (void)__inc_b_mean_nb;
10565 50           register PDL_Indx __inc_b_weight_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,7,0)]; (void)__inc_b_weight_nb;
10566 50           register PDL_Indx __inc_b_weight2_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,8,0)]; (void)__inc_b_weight2_nb;
10567 50           register PDL_Indx __inc_b_weight2_error_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,15,0)]; (void)__inc_b_weight2_error_nb;
10568 50           register PDL_Indx __inc_b_weight_error_nb = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,14,0)]; (void)__inc_b_weight_error_nb;
10569 50           register PDL_Indx __inc_data_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,0,0)]; (void)__inc_data_n;
10570 50           register PDL_Indx __inc_imin_nt = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,3,0)]; (void)__inc_imin_nt;
10571 50           register PDL_Indx __inc_index_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,1,0)]; (void)__inc_index_n;
10572 50           register PDL_Indx __inc_nbins_nt = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,4,0)]; (void)__inc_nbins_nt;
10573 50           register PDL_Indx __inc_weight_n = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,2,0)]; (void)__inc_weight_n;
10574             #ifndef PDL_DECLARE_PARAMS_bin_on_index_1
10575             #define PDL_DECLARE_PARAMS_bin_on_index_1(PDL_TYPE_OP,PDL_PPSYM_OP,PDL_TYPE_PARAM_index,PDL_PPSYM_PARAM_index,PDL_TYPE_PARAM_imin,PDL_PPSYM_PARAM_imin,PDL_TYPE_PARAM_nbins,PDL_PPSYM_PARAM_nbins,PDL_TYPE_PARAM_b_count,PDL_PPSYM_PARAM_b_count,PDL_TYPE_PARAM_b_data,PDL_PPSYM_PARAM_b_data,PDL_TYPE_PARAM_b_weight,PDL_PPSYM_PARAM_b_weight,PDL_TYPE_PARAM_b_weight2,PDL_PPSYM_PARAM_b_weight2,PDL_TYPE_PARAM_b_mean,PDL_PPSYM_PARAM_b_mean,PDL_TYPE_PARAM_b_dmin,PDL_PPSYM_PARAM_b_dmin,PDL_TYPE_PARAM_b_dmax,PDL_PPSYM_PARAM_b_dmax,PDL_TYPE_PARAM_b_dev2,PDL_PPSYM_PARAM_b_dev2,PDL_TYPE_PARAM_b_data_error,PDL_PPSYM_PARAM_b_data_error,PDL_TYPE_PARAM_b_weight_error,PDL_PPSYM_PARAM_b_weight_error,PDL_TYPE_PARAM_b_weight2_error,PDL_PPSYM_PARAM_b_weight2_error) \
10576             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, data, (__privtrans->pdls[0]), 1, PDL_PPSYM_OP) \
10577             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_index, index, (__privtrans->pdls[1]), 1, PDL_PPSYM_PARAM_index) \
10578             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_OP, weight, (__privtrans->pdls[2]), 1, PDL_PPSYM_OP) \
10579             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_imin, imin, (__privtrans->pdls[3]), 1, PDL_PPSYM_PARAM_imin) \
10580             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_nbins, nbins, (__privtrans->pdls[4]), 1, PDL_PPSYM_PARAM_nbins) \
10581             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_count, b_count, (__privtrans->pdls[5]), 1, PDL_PPSYM_PARAM_b_count) \
10582             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_data, b_data, (__privtrans->pdls[6]), 1, PDL_PPSYM_PARAM_b_data) \
10583             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight, b_weight, (__privtrans->pdls[7]), 1, PDL_PPSYM_PARAM_b_weight) \
10584             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight2, b_weight2, (__privtrans->pdls[8]), 1, PDL_PPSYM_PARAM_b_weight2) \
10585             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_mean, b_mean, (__privtrans->pdls[9]), 1, PDL_PPSYM_PARAM_b_mean) \
10586             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_dmin, b_dmin, (__privtrans->pdls[10]), 1, PDL_PPSYM_PARAM_b_dmin) \
10587             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_dmax, b_dmax, (__privtrans->pdls[11]), 1, PDL_PPSYM_PARAM_b_dmax) \
10588             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_dev2, b_dev2, (__privtrans->pdls[12]), 1, PDL_PPSYM_PARAM_b_dev2) \
10589             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_data_error, b_data_error, (__privtrans->pdls[13]), 1, PDL_PPSYM_PARAM_b_data_error) \
10590             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight_error, b_weight_error, (__privtrans->pdls[14]), 1, PDL_PPSYM_PARAM_b_weight_error) \
10591             PDL_DECLARE_PARAMETER_BADVAL(PDL_TYPE_PARAM_b_weight2_error, b_weight2_error, (__privtrans->pdls[15]), 1, PDL_PPSYM_PARAM_b_weight2_error)
10592             #endif
10593 50 50         if ( __privtrans->bvalflag ) { /* ** do 'bad' Code ** */
10594             #define PDL_BAD_CODE
10595             #define PDL_IF_BAD(t,f) t
10596 0           switch (__privtrans->__datatype) { /* Start generic switch */
10597 0           case PDL_SB: {
10598 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_SByte,A,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10599             {
10600              
10601              
10602              
10603              
10604 0           int flags = __params->optflags;
10605              
10606 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
10607 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
10608              
10609 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
10610              
10611 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
10612 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
10613 0           PDL_Indx imax = imin + nbins - 1;
10614              
10615             /* if first bin is reserved for oob, need to
10616             offset imin to ensure that non-oob data start
10617             at second bin */
10618 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
10619 0           imin -= 1;
10620              
10621             /* intialize output and temp bin data one at a time to
10622             avoid trashing the cache */
10623            
10624             /* initialize Kahan Summation for b_data */
10625 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
10626 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
10627             ;
10628 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
10629 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
10630 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
10631              
10632 0 0         if ( have_weight ) {
10633            
10634             /* initialize Kahan Summation for b_weight */
10635 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
10636 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
10637             ;
10638            
10639             /* initialize Kahan Summation for b_weight2 */
10640 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
10641 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
10642             ;
10643             }
10644              
10645             /* if we could preset min & max to the initial value in a bin,
10646             we could shave off a comparison. Unfortunately, we can't
10647             do that, as we can't know apriori which is the first
10648             element in a bin. */
10649 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
10650 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
10651              
10652 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
10653              
10654             PDL_Indx count;
10655 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
10656 0           double data = (data_datap)[0+(__inc_data_n*(n))];
10657             double weight;
10658              
10659 0           PDL_Indx oob_low =
10660 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
10661 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
10662 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
10663 0 0         : -1; /* error */
10664              
10665 0           PDL_Indx oob_high =
10666 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
10667 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
10668 0 0         : -1; /* error */
10669              
10670              
10671             #ifdef PDL_BAD_CODE
10672 0 0         if ( PDL_ISBAD2(data,data_badval,A,data_badval_isnan)
10673 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
10674             ) {
10675 0           continue;
10676             }
10677             #endif /* PDL_BAD_CODE */
10678              
10679 0 0         if ( have_weight ) {
10680 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
10681              
10682             #ifdef PDL_BAD_CODE
10683 0 0         if ( PDL_ISBAD2(weight,weight_badval,A,weight_badval_isnan) )
    0          
10684 0           continue;
10685             #endif /* PDL_BAD_CODE */
10686             }
10687              
10688              
10689 0 0         if ( idx < imin ) {
10690 0 0         if ( save_oob ) idx = oob_low;
10691 0           else continue;
10692             }
10693 0 0         else if ( idx > imax ) {
10694 0 0         if ( save_oob ) idx = oob_high;
10695 0           else continue;
10696             }
10697             else {
10698 0           idx -= imin;
10699             }
10700              
10701 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
10702             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
10703              
10704 0 0         if ( have_weight ){
10705             {
10706             /* Kahan Summation increment for b_data */
10707 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
10708             /* use temporaries to minimize access of piddle arrays */
10709 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
10710 0           double tsum = temp + y;
10711 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
10712 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
10713             }
10714             ;
10715             }
10716             else {
10717             {
10718             /* Kahan Summation increment for b_data */
10719 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
10720             /* use temporaries to minimize access of piddle arrays */
10721 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
10722 0           double tsum = temp + y;
10723 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
10724 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
10725             }
10726             ;
10727             }
10728              
10729 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
10730 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
10731              
10732             {
10733 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
10734             double d_mean;
10735              
10736 0 0         if ( have_weight ) {
10737 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
10738              
10739             {
10740             /* Kahan Summation increment for b_weight */
10741 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
10742             /* use temporaries to minimize access of piddle arrays */
10743 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
10744 0           double tsum = temp + y;
10745 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
10746 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
10747             }
10748             ;
10749             {
10750             /* Kahan Summation increment for b_weight2 */
10751 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
10752             /* use temporaries to minimize access of piddle arrays */
10753 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
10754 0           double tsum = temp + y;
10755 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
10756 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
10757             }
10758             ;
10759              
10760 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
10761              
10762 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
10763 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
10764             }
10765              
10766             else {
10767 0           d_mean = ( data - prev_mean ) / count;
10768 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
10769 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
10770             }
10771             }
10772             }} /* Close n */
10773              
10774             {
10775 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
10776             }
10777             ;
10778              
10779             /* if there were no data in the bin, set some derived values to BAD_VAL */
10780            
10781             {
10782             /* if any are bad */
10783 0           int set_bad = 0;
10784 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
10785              
10786 0 0         if ( set_bad ) {
10787 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
10788              
10789 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
10790 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
10791              
10792 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
10793 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
10794              
10795 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
10796 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
10797             }
10798             }
10799            
10800              
10801 0 0         if ( have_weight ) {
10802             {
10803 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
10804             }
10805             ;
10806             {
10807 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
10808             }
10809             ;
10810             }
10811              
10812 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
10813             }
10814 0           } break;
10815 0           case PDL_B: {
10816 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Byte,B,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
10817             {
10818              
10819              
10820              
10821              
10822 0           int flags = __params->optflags;
10823              
10824 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
10825 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
10826              
10827 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
10828              
10829 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
10830 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
10831 0           PDL_Indx imax = imin + nbins - 1;
10832              
10833             /* if first bin is reserved for oob, need to
10834             offset imin to ensure that non-oob data start
10835             at second bin */
10836 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
10837 0           imin -= 1;
10838              
10839             /* intialize output and temp bin data one at a time to
10840             avoid trashing the cache */
10841            
10842             /* initialize Kahan Summation for b_data */
10843 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
10844 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
10845             ;
10846 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
10847 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
10848 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
10849              
10850 0 0         if ( have_weight ) {
10851            
10852             /* initialize Kahan Summation for b_weight */
10853 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
10854 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
10855             ;
10856            
10857             /* initialize Kahan Summation for b_weight2 */
10858 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
10859 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
10860             ;
10861             }
10862              
10863             /* if we could preset min & max to the initial value in a bin,
10864             we could shave off a comparison. Unfortunately, we can't
10865             do that, as we can't know apriori which is the first
10866             element in a bin. */
10867 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
10868 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
10869              
10870 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
10871              
10872             PDL_Indx count;
10873 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
10874 0           double data = (data_datap)[0+(__inc_data_n*(n))];
10875             double weight;
10876              
10877 0           PDL_Indx oob_low =
10878 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
10879 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
10880 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
10881 0 0         : -1; /* error */
10882              
10883 0           PDL_Indx oob_high =
10884 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
10885 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
10886 0 0         : -1; /* error */
10887              
10888              
10889             #ifdef PDL_BAD_CODE
10890 0 0         if ( PDL_ISBAD2(data,data_badval,B,data_badval_isnan)
10891 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
10892             ) {
10893 0           continue;
10894             }
10895             #endif /* PDL_BAD_CODE */
10896              
10897 0 0         if ( have_weight ) {
10898 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
10899              
10900             #ifdef PDL_BAD_CODE
10901 0 0         if ( PDL_ISBAD2(weight,weight_badval,B,weight_badval_isnan) )
    0          
10902 0           continue;
10903             #endif /* PDL_BAD_CODE */
10904             }
10905              
10906              
10907 0 0         if ( idx < imin ) {
10908 0 0         if ( save_oob ) idx = oob_low;
10909 0           else continue;
10910             }
10911 0 0         else if ( idx > imax ) {
10912 0 0         if ( save_oob ) idx = oob_high;
10913 0           else continue;
10914             }
10915             else {
10916 0           idx -= imin;
10917             }
10918              
10919 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
10920             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
10921              
10922 0 0         if ( have_weight ){
10923             {
10924             /* Kahan Summation increment for b_data */
10925 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
10926             /* use temporaries to minimize access of piddle arrays */
10927 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
10928 0           double tsum = temp + y;
10929 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
10930 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
10931             }
10932             ;
10933             }
10934             else {
10935             {
10936             /* Kahan Summation increment for b_data */
10937 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
10938             /* use temporaries to minimize access of piddle arrays */
10939 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
10940 0           double tsum = temp + y;
10941 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
10942 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
10943             }
10944             ;
10945             }
10946              
10947 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
10948 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
10949              
10950             {
10951 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
10952             double d_mean;
10953              
10954 0 0         if ( have_weight ) {
10955 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
10956              
10957             {
10958             /* Kahan Summation increment for b_weight */
10959 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
10960             /* use temporaries to minimize access of piddle arrays */
10961 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
10962 0           double tsum = temp + y;
10963 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
10964 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
10965             }
10966             ;
10967             {
10968             /* Kahan Summation increment for b_weight2 */
10969 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
10970             /* use temporaries to minimize access of piddle arrays */
10971 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
10972 0           double tsum = temp + y;
10973 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
10974 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
10975             }
10976             ;
10977              
10978 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
10979              
10980 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
10981 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
10982             }
10983              
10984             else {
10985 0           d_mean = ( data - prev_mean ) / count;
10986 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
10987 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
10988             }
10989             }
10990             }} /* Close n */
10991              
10992             {
10993 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
10994             }
10995             ;
10996              
10997             /* if there were no data in the bin, set some derived values to BAD_VAL */
10998            
10999             {
11000             /* if any are bad */
11001 0           int set_bad = 0;
11002 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
11003              
11004 0 0         if ( set_bad ) {
11005 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
11006              
11007 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
11008 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
11009              
11010 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
11011 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
11012              
11013 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
11014 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
11015             }
11016             }
11017            
11018              
11019 0 0         if ( have_weight ) {
11020             {
11021 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
11022             }
11023             ;
11024             {
11025 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
11026             }
11027             ;
11028             }
11029              
11030 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
11031             }
11032 0           } break;
11033 0           case PDL_S: {
11034 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Short,S,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
11035             {
11036              
11037              
11038              
11039              
11040 0           int flags = __params->optflags;
11041              
11042 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
11043 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
11044              
11045 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
11046              
11047 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
11048 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
11049 0           PDL_Indx imax = imin + nbins - 1;
11050              
11051             /* if first bin is reserved for oob, need to
11052             offset imin to ensure that non-oob data start
11053             at second bin */
11054 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
11055 0           imin -= 1;
11056              
11057             /* intialize output and temp bin data one at a time to
11058             avoid trashing the cache */
11059            
11060             /* initialize Kahan Summation for b_data */
11061 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
11062 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
11063             ;
11064 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
11065 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
11066 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
11067              
11068 0 0         if ( have_weight ) {
11069            
11070             /* initialize Kahan Summation for b_weight */
11071 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
11072 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
11073             ;
11074            
11075             /* initialize Kahan Summation for b_weight2 */
11076 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
11077 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
11078             ;
11079             }
11080              
11081             /* if we could preset min & max to the initial value in a bin,
11082             we could shave off a comparison. Unfortunately, we can't
11083             do that, as we can't know apriori which is the first
11084             element in a bin. */
11085 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
11086 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
11087              
11088 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
11089              
11090             PDL_Indx count;
11091 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
11092 0           double data = (data_datap)[0+(__inc_data_n*(n))];
11093             double weight;
11094              
11095 0           PDL_Indx oob_low =
11096 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
11097 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
11098 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
11099 0 0         : -1; /* error */
11100              
11101 0           PDL_Indx oob_high =
11102 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
11103 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
11104 0 0         : -1; /* error */
11105              
11106              
11107             #ifdef PDL_BAD_CODE
11108 0 0         if ( PDL_ISBAD2(data,data_badval,S,data_badval_isnan)
11109 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
11110             ) {
11111 0           continue;
11112             }
11113             #endif /* PDL_BAD_CODE */
11114              
11115 0 0         if ( have_weight ) {
11116 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
11117              
11118             #ifdef PDL_BAD_CODE
11119 0 0         if ( PDL_ISBAD2(weight,weight_badval,S,weight_badval_isnan) )
    0          
11120 0           continue;
11121             #endif /* PDL_BAD_CODE */
11122             }
11123              
11124              
11125 0 0         if ( idx < imin ) {
11126 0 0         if ( save_oob ) idx = oob_low;
11127 0           else continue;
11128             }
11129 0 0         else if ( idx > imax ) {
11130 0 0         if ( save_oob ) idx = oob_high;
11131 0           else continue;
11132             }
11133             else {
11134 0           idx -= imin;
11135             }
11136              
11137 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
11138             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
11139              
11140 0 0         if ( have_weight ){
11141             {
11142             /* Kahan Summation increment for b_data */
11143 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11144             /* use temporaries to minimize access of piddle arrays */
11145 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11146 0           double tsum = temp + y;
11147 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11148 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11149             }
11150             ;
11151             }
11152             else {
11153             {
11154             /* Kahan Summation increment for b_data */
11155 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11156             /* use temporaries to minimize access of piddle arrays */
11157 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11158 0           double tsum = temp + y;
11159 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11160 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11161             }
11162             ;
11163             }
11164              
11165 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
11166 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
11167              
11168             {
11169 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
11170             double d_mean;
11171              
11172 0 0         if ( have_weight ) {
11173 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11174              
11175             {
11176             /* Kahan Summation increment for b_weight */
11177 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11178             /* use temporaries to minimize access of piddle arrays */
11179 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
11180 0           double tsum = temp + y;
11181 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
11182 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
11183             }
11184             ;
11185             {
11186             /* Kahan Summation increment for b_weight2 */
11187 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
11188             /* use temporaries to minimize access of piddle arrays */
11189 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
11190 0           double tsum = temp + y;
11191 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
11192 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
11193             }
11194             ;
11195              
11196 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11197              
11198 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11199 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
11200             }
11201              
11202             else {
11203 0           d_mean = ( data - prev_mean ) / count;
11204 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11205 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
11206             }
11207             }
11208             }} /* Close n */
11209              
11210             {
11211 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
11212             }
11213             ;
11214              
11215             /* if there were no data in the bin, set some derived values to BAD_VAL */
11216            
11217             {
11218             /* if any are bad */
11219 0           int set_bad = 0;
11220 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
11221              
11222 0 0         if ( set_bad ) {
11223 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
11224              
11225 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
11226 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
11227              
11228 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
11229 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
11230              
11231 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
11232 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
11233             }
11234             }
11235            
11236              
11237 0 0         if ( have_weight ) {
11238             {
11239 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
11240             }
11241             ;
11242             {
11243 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
11244             }
11245             ;
11246             }
11247              
11248 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
11249             }
11250 0           } break;
11251 0           case PDL_US: {
11252 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Ushort,U,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
11253             {
11254              
11255              
11256              
11257              
11258 0           int flags = __params->optflags;
11259              
11260 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
11261 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
11262              
11263 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
11264              
11265 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
11266 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
11267 0           PDL_Indx imax = imin + nbins - 1;
11268              
11269             /* if first bin is reserved for oob, need to
11270             offset imin to ensure that non-oob data start
11271             at second bin */
11272 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
11273 0           imin -= 1;
11274              
11275             /* intialize output and temp bin data one at a time to
11276             avoid trashing the cache */
11277            
11278             /* initialize Kahan Summation for b_data */
11279 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
11280 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
11281             ;
11282 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
11283 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
11284 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
11285              
11286 0 0         if ( have_weight ) {
11287            
11288             /* initialize Kahan Summation for b_weight */
11289 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
11290 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
11291             ;
11292            
11293             /* initialize Kahan Summation for b_weight2 */
11294 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
11295 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
11296             ;
11297             }
11298              
11299             /* if we could preset min & max to the initial value in a bin,
11300             we could shave off a comparison. Unfortunately, we can't
11301             do that, as we can't know apriori which is the first
11302             element in a bin. */
11303 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
11304 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
11305              
11306 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
11307              
11308             PDL_Indx count;
11309 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
11310 0           double data = (data_datap)[0+(__inc_data_n*(n))];
11311             double weight;
11312              
11313 0           PDL_Indx oob_low =
11314 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
11315 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
11316 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
11317 0 0         : -1; /* error */
11318              
11319 0           PDL_Indx oob_high =
11320 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
11321 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
11322 0 0         : -1; /* error */
11323              
11324              
11325             #ifdef PDL_BAD_CODE
11326 0 0         if ( PDL_ISBAD2(data,data_badval,U,data_badval_isnan)
11327 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
11328             ) {
11329 0           continue;
11330             }
11331             #endif /* PDL_BAD_CODE */
11332              
11333 0 0         if ( have_weight ) {
11334 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
11335              
11336             #ifdef PDL_BAD_CODE
11337 0 0         if ( PDL_ISBAD2(weight,weight_badval,U,weight_badval_isnan) )
    0          
11338 0           continue;
11339             #endif /* PDL_BAD_CODE */
11340             }
11341              
11342              
11343 0 0         if ( idx < imin ) {
11344 0 0         if ( save_oob ) idx = oob_low;
11345 0           else continue;
11346             }
11347 0 0         else if ( idx > imax ) {
11348 0 0         if ( save_oob ) idx = oob_high;
11349 0           else continue;
11350             }
11351             else {
11352 0           idx -= imin;
11353             }
11354              
11355 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
11356             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
11357              
11358 0 0         if ( have_weight ){
11359             {
11360             /* Kahan Summation increment for b_data */
11361 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11362             /* use temporaries to minimize access of piddle arrays */
11363 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11364 0           double tsum = temp + y;
11365 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11366 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11367             }
11368             ;
11369             }
11370             else {
11371             {
11372             /* Kahan Summation increment for b_data */
11373 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11374             /* use temporaries to minimize access of piddle arrays */
11375 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11376 0           double tsum = temp + y;
11377 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11378 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11379             }
11380             ;
11381             }
11382              
11383 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
11384 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
11385              
11386             {
11387 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
11388             double d_mean;
11389              
11390 0 0         if ( have_weight ) {
11391 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11392              
11393             {
11394             /* Kahan Summation increment for b_weight */
11395 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11396             /* use temporaries to minimize access of piddle arrays */
11397 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
11398 0           double tsum = temp + y;
11399 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
11400 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
11401             }
11402             ;
11403             {
11404             /* Kahan Summation increment for b_weight2 */
11405 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
11406             /* use temporaries to minimize access of piddle arrays */
11407 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
11408 0           double tsum = temp + y;
11409 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
11410 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
11411             }
11412             ;
11413              
11414 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11415              
11416 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11417 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
11418             }
11419              
11420             else {
11421 0           d_mean = ( data - prev_mean ) / count;
11422 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11423 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
11424             }
11425             }
11426             }} /* Close n */
11427              
11428             {
11429 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
11430             }
11431             ;
11432              
11433             /* if there were no data in the bin, set some derived values to BAD_VAL */
11434            
11435             {
11436             /* if any are bad */
11437 0           int set_bad = 0;
11438 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
11439              
11440 0 0         if ( set_bad ) {
11441 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
11442              
11443 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
11444 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
11445              
11446 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
11447 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
11448              
11449 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
11450 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
11451             }
11452             }
11453            
11454              
11455 0 0         if ( have_weight ) {
11456             {
11457 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
11458             }
11459             ;
11460             {
11461 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
11462             }
11463             ;
11464             }
11465              
11466 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
11467             }
11468 0           } break;
11469 0           case PDL_L: {
11470 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Long,L,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
11471             {
11472              
11473              
11474              
11475              
11476 0           int flags = __params->optflags;
11477              
11478 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
11479 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
11480              
11481 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
11482              
11483 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
11484 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
11485 0           PDL_Indx imax = imin + nbins - 1;
11486              
11487             /* if first bin is reserved for oob, need to
11488             offset imin to ensure that non-oob data start
11489             at second bin */
11490 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
11491 0           imin -= 1;
11492              
11493             /* intialize output and temp bin data one at a time to
11494             avoid trashing the cache */
11495            
11496             /* initialize Kahan Summation for b_data */
11497 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
11498 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
11499             ;
11500 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
11501 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
11502 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
11503              
11504 0 0         if ( have_weight ) {
11505            
11506             /* initialize Kahan Summation for b_weight */
11507 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
11508 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
11509             ;
11510            
11511             /* initialize Kahan Summation for b_weight2 */
11512 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
11513 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
11514             ;
11515             }
11516              
11517             /* if we could preset min & max to the initial value in a bin,
11518             we could shave off a comparison. Unfortunately, we can't
11519             do that, as we can't know apriori which is the first
11520             element in a bin. */
11521 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
11522 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
11523              
11524 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
11525              
11526             PDL_Indx count;
11527 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
11528 0           double data = (data_datap)[0+(__inc_data_n*(n))];
11529             double weight;
11530              
11531 0           PDL_Indx oob_low =
11532 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
11533 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
11534 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
11535 0 0         : -1; /* error */
11536              
11537 0           PDL_Indx oob_high =
11538 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
11539 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
11540 0 0         : -1; /* error */
11541              
11542              
11543             #ifdef PDL_BAD_CODE
11544 0 0         if ( PDL_ISBAD2(data,data_badval,L,data_badval_isnan)
11545 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
11546             ) {
11547 0           continue;
11548             }
11549             #endif /* PDL_BAD_CODE */
11550              
11551 0 0         if ( have_weight ) {
11552 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
11553              
11554             #ifdef PDL_BAD_CODE
11555 0 0         if ( PDL_ISBAD2(weight,weight_badval,L,weight_badval_isnan) )
    0          
11556 0           continue;
11557             #endif /* PDL_BAD_CODE */
11558             }
11559              
11560              
11561 0 0         if ( idx < imin ) {
11562 0 0         if ( save_oob ) idx = oob_low;
11563 0           else continue;
11564             }
11565 0 0         else if ( idx > imax ) {
11566 0 0         if ( save_oob ) idx = oob_high;
11567 0           else continue;
11568             }
11569             else {
11570 0           idx -= imin;
11571             }
11572              
11573 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
11574             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
11575              
11576 0 0         if ( have_weight ){
11577             {
11578             /* Kahan Summation increment for b_data */
11579 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11580             /* use temporaries to minimize access of piddle arrays */
11581 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11582 0           double tsum = temp + y;
11583 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11584 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11585             }
11586             ;
11587             }
11588             else {
11589             {
11590             /* Kahan Summation increment for b_data */
11591 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11592             /* use temporaries to minimize access of piddle arrays */
11593 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11594 0           double tsum = temp + y;
11595 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11596 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11597             }
11598             ;
11599             }
11600              
11601 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
11602 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
11603              
11604             {
11605 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
11606             double d_mean;
11607              
11608 0 0         if ( have_weight ) {
11609 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11610              
11611             {
11612             /* Kahan Summation increment for b_weight */
11613 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11614             /* use temporaries to minimize access of piddle arrays */
11615 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
11616 0           double tsum = temp + y;
11617 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
11618 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
11619             }
11620             ;
11621             {
11622             /* Kahan Summation increment for b_weight2 */
11623 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
11624             /* use temporaries to minimize access of piddle arrays */
11625 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
11626 0           double tsum = temp + y;
11627 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
11628 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
11629             }
11630             ;
11631              
11632 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11633              
11634 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11635 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
11636             }
11637              
11638             else {
11639 0           d_mean = ( data - prev_mean ) / count;
11640 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11641 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
11642             }
11643             }
11644             }} /* Close n */
11645              
11646             {
11647 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
11648             }
11649             ;
11650              
11651             /* if there were no data in the bin, set some derived values to BAD_VAL */
11652            
11653             {
11654             /* if any are bad */
11655 0           int set_bad = 0;
11656 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
11657              
11658 0 0         if ( set_bad ) {
11659 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
11660              
11661 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
11662 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
11663              
11664 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
11665 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
11666              
11667 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
11668 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
11669             }
11670             }
11671            
11672              
11673 0 0         if ( have_weight ) {
11674             {
11675 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
11676             }
11677             ;
11678             {
11679 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
11680             }
11681             ;
11682             }
11683              
11684 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
11685             }
11686 0           } break;
11687 0           case PDL_UL: {
11688 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_ULong,K,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
11689             {
11690              
11691              
11692              
11693              
11694 0           int flags = __params->optflags;
11695              
11696 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
11697 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
11698              
11699 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
11700              
11701 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
11702 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
11703 0           PDL_Indx imax = imin + nbins - 1;
11704              
11705             /* if first bin is reserved for oob, need to
11706             offset imin to ensure that non-oob data start
11707             at second bin */
11708 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
11709 0           imin -= 1;
11710              
11711             /* intialize output and temp bin data one at a time to
11712             avoid trashing the cache */
11713            
11714             /* initialize Kahan Summation for b_data */
11715 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
11716 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
11717             ;
11718 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
11719 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
11720 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
11721              
11722 0 0         if ( have_weight ) {
11723            
11724             /* initialize Kahan Summation for b_weight */
11725 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
11726 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
11727             ;
11728            
11729             /* initialize Kahan Summation for b_weight2 */
11730 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
11731 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
11732             ;
11733             }
11734              
11735             /* if we could preset min & max to the initial value in a bin,
11736             we could shave off a comparison. Unfortunately, we can't
11737             do that, as we can't know apriori which is the first
11738             element in a bin. */
11739 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
11740 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
11741              
11742 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
11743              
11744             PDL_Indx count;
11745 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
11746 0           double data = (data_datap)[0+(__inc_data_n*(n))];
11747             double weight;
11748              
11749 0           PDL_Indx oob_low =
11750 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
11751 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
11752 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
11753 0 0         : -1; /* error */
11754              
11755 0           PDL_Indx oob_high =
11756 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
11757 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
11758 0 0         : -1; /* error */
11759              
11760              
11761             #ifdef PDL_BAD_CODE
11762 0 0         if ( PDL_ISBAD2(data,data_badval,K,data_badval_isnan)
11763 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
11764             ) {
11765 0           continue;
11766             }
11767             #endif /* PDL_BAD_CODE */
11768              
11769 0 0         if ( have_weight ) {
11770 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
11771              
11772             #ifdef PDL_BAD_CODE
11773 0 0         if ( PDL_ISBAD2(weight,weight_badval,K,weight_badval_isnan) )
    0          
11774 0           continue;
11775             #endif /* PDL_BAD_CODE */
11776             }
11777              
11778              
11779 0 0         if ( idx < imin ) {
11780 0 0         if ( save_oob ) idx = oob_low;
11781 0           else continue;
11782             }
11783 0 0         else if ( idx > imax ) {
11784 0 0         if ( save_oob ) idx = oob_high;
11785 0           else continue;
11786             }
11787             else {
11788 0           idx -= imin;
11789             }
11790              
11791 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
11792             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
11793              
11794 0 0         if ( have_weight ){
11795             {
11796             /* Kahan Summation increment for b_data */
11797 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11798             /* use temporaries to minimize access of piddle arrays */
11799 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11800 0           double tsum = temp + y;
11801 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11802 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11803             }
11804             ;
11805             }
11806             else {
11807             {
11808             /* Kahan Summation increment for b_data */
11809 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
11810             /* use temporaries to minimize access of piddle arrays */
11811 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
11812 0           double tsum = temp + y;
11813 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
11814 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
11815             }
11816             ;
11817             }
11818              
11819 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
11820 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
11821              
11822             {
11823 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
11824             double d_mean;
11825              
11826 0 0         if ( have_weight ) {
11827 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11828              
11829             {
11830             /* Kahan Summation increment for b_weight */
11831 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11832             /* use temporaries to minimize access of piddle arrays */
11833 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
11834 0           double tsum = temp + y;
11835 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
11836 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
11837             }
11838             ;
11839             {
11840             /* Kahan Summation increment for b_weight2 */
11841 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
11842             /* use temporaries to minimize access of piddle arrays */
11843 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
11844 0           double tsum = temp + y;
11845 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
11846 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
11847             }
11848             ;
11849              
11850 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
11851              
11852 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11853 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
11854             }
11855              
11856             else {
11857 0           d_mean = ( data - prev_mean ) / count;
11858 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
11859 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
11860             }
11861             }
11862             }} /* Close n */
11863              
11864             {
11865 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
11866             }
11867             ;
11868              
11869             /* if there were no data in the bin, set some derived values to BAD_VAL */
11870            
11871             {
11872             /* if any are bad */
11873 0           int set_bad = 0;
11874 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
11875              
11876 0 0         if ( set_bad ) {
11877 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
11878              
11879 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
11880 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
11881              
11882 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
11883 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
11884              
11885 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
11886 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
11887             }
11888             }
11889            
11890              
11891 0 0         if ( have_weight ) {
11892             {
11893 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
11894             }
11895             ;
11896             {
11897 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
11898             }
11899             ;
11900             }
11901              
11902 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
11903             }
11904 0           } break;
11905 0           case PDL_IND: {
11906 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
11907             {
11908              
11909              
11910              
11911              
11912 0           int flags = __params->optflags;
11913              
11914 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
11915 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
11916              
11917 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
11918              
11919 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
11920 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
11921 0           PDL_Indx imax = imin + nbins - 1;
11922              
11923             /* if first bin is reserved for oob, need to
11924             offset imin to ensure that non-oob data start
11925             at second bin */
11926 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
11927 0           imin -= 1;
11928              
11929             /* intialize output and temp bin data one at a time to
11930             avoid trashing the cache */
11931            
11932             /* initialize Kahan Summation for b_data */
11933 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
11934 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
11935             ;
11936 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
11937 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
11938 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
11939              
11940 0 0         if ( have_weight ) {
11941            
11942             /* initialize Kahan Summation for b_weight */
11943 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
11944 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
11945             ;
11946            
11947             /* initialize Kahan Summation for b_weight2 */
11948 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
11949 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
11950             ;
11951             }
11952              
11953             /* if we could preset min & max to the initial value in a bin,
11954             we could shave off a comparison. Unfortunately, we can't
11955             do that, as we can't know apriori which is the first
11956             element in a bin. */
11957 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
11958 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
11959              
11960 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
11961              
11962             PDL_Indx count;
11963 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
11964 0           double data = (data_datap)[0+(__inc_data_n*(n))];
11965             double weight;
11966              
11967 0           PDL_Indx oob_low =
11968 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
11969 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
11970 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
11971 0 0         : -1; /* error */
11972              
11973 0           PDL_Indx oob_high =
11974 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
11975 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
11976 0 0         : -1; /* error */
11977              
11978              
11979             #ifdef PDL_BAD_CODE
11980 0 0         if ( PDL_ISBAD2(data,data_badval,N,data_badval_isnan)
11981 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
11982             ) {
11983 0           continue;
11984             }
11985             #endif /* PDL_BAD_CODE */
11986              
11987 0 0         if ( have_weight ) {
11988 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
11989              
11990             #ifdef PDL_BAD_CODE
11991 0 0         if ( PDL_ISBAD2(weight,weight_badval,N,weight_badval_isnan) )
    0          
11992 0           continue;
11993             #endif /* PDL_BAD_CODE */
11994             }
11995              
11996              
11997 0 0         if ( idx < imin ) {
11998 0 0         if ( save_oob ) idx = oob_low;
11999 0           else continue;
12000             }
12001 0 0         else if ( idx > imax ) {
12002 0 0         if ( save_oob ) idx = oob_high;
12003 0           else continue;
12004             }
12005             else {
12006 0           idx -= imin;
12007             }
12008              
12009 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
12010             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
12011              
12012 0 0         if ( have_weight ){
12013             {
12014             /* Kahan Summation increment for b_data */
12015 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12016             /* use temporaries to minimize access of piddle arrays */
12017 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12018 0           double tsum = temp + y;
12019 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12020 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12021             }
12022             ;
12023             }
12024             else {
12025             {
12026             /* Kahan Summation increment for b_data */
12027 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12028             /* use temporaries to minimize access of piddle arrays */
12029 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12030 0           double tsum = temp + y;
12031 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12032 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12033             }
12034             ;
12035             }
12036              
12037 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
12038 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
12039              
12040             {
12041 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
12042             double d_mean;
12043              
12044 0 0         if ( have_weight ) {
12045 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12046              
12047             {
12048             /* Kahan Summation increment for b_weight */
12049 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12050             /* use temporaries to minimize access of piddle arrays */
12051 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
12052 0           double tsum = temp + y;
12053 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
12054 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
12055             }
12056             ;
12057             {
12058             /* Kahan Summation increment for b_weight2 */
12059 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
12060             /* use temporaries to minimize access of piddle arrays */
12061 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
12062 0           double tsum = temp + y;
12063 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
12064 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
12065             }
12066             ;
12067              
12068 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12069              
12070 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12071 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
12072             }
12073              
12074             else {
12075 0           d_mean = ( data - prev_mean ) / count;
12076 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12077 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
12078             }
12079             }
12080             }} /* Close n */
12081              
12082             {
12083 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
12084             }
12085             ;
12086              
12087             /* if there were no data in the bin, set some derived values to BAD_VAL */
12088            
12089             {
12090             /* if any are bad */
12091 0           int set_bad = 0;
12092 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
12093              
12094 0 0         if ( set_bad ) {
12095 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
12096              
12097 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
12098 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
12099              
12100 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
12101 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
12102              
12103 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
12104 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
12105             }
12106             }
12107            
12108              
12109 0 0         if ( have_weight ) {
12110             {
12111 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
12112             }
12113             ;
12114             {
12115 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
12116             }
12117             ;
12118             }
12119              
12120 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
12121             }
12122 0           } break;
12123 0           case PDL_ULL: {
12124 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_ULongLong,P,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
12125             {
12126              
12127              
12128              
12129              
12130 0           int flags = __params->optflags;
12131              
12132 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
12133 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
12134              
12135 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
12136              
12137 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
12138 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
12139 0           PDL_Indx imax = imin + nbins - 1;
12140              
12141             /* if first bin is reserved for oob, need to
12142             offset imin to ensure that non-oob data start
12143             at second bin */
12144 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
12145 0           imin -= 1;
12146              
12147             /* intialize output and temp bin data one at a time to
12148             avoid trashing the cache */
12149            
12150             /* initialize Kahan Summation for b_data */
12151 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
12152 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
12153             ;
12154 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
12155 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
12156 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
12157              
12158 0 0         if ( have_weight ) {
12159            
12160             /* initialize Kahan Summation for b_weight */
12161 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
12162 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
12163             ;
12164            
12165             /* initialize Kahan Summation for b_weight2 */
12166 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
12167 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
12168             ;
12169             }
12170              
12171             /* if we could preset min & max to the initial value in a bin,
12172             we could shave off a comparison. Unfortunately, we can't
12173             do that, as we can't know apriori which is the first
12174             element in a bin. */
12175 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
12176 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
12177              
12178 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
12179              
12180             PDL_Indx count;
12181 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
12182 0           double data = (data_datap)[0+(__inc_data_n*(n))];
12183             double weight;
12184              
12185 0           PDL_Indx oob_low =
12186 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
12187 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
12188 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
12189 0 0         : -1; /* error */
12190              
12191 0           PDL_Indx oob_high =
12192 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
12193 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
12194 0 0         : -1; /* error */
12195              
12196              
12197             #ifdef PDL_BAD_CODE
12198 0 0         if ( PDL_ISBAD2(data,data_badval,P,data_badval_isnan)
12199 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
12200             ) {
12201 0           continue;
12202             }
12203             #endif /* PDL_BAD_CODE */
12204              
12205 0 0         if ( have_weight ) {
12206 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
12207              
12208             #ifdef PDL_BAD_CODE
12209 0 0         if ( PDL_ISBAD2(weight,weight_badval,P,weight_badval_isnan) )
    0          
12210 0           continue;
12211             #endif /* PDL_BAD_CODE */
12212             }
12213              
12214              
12215 0 0         if ( idx < imin ) {
12216 0 0         if ( save_oob ) idx = oob_low;
12217 0           else continue;
12218             }
12219 0 0         else if ( idx > imax ) {
12220 0 0         if ( save_oob ) idx = oob_high;
12221 0           else continue;
12222             }
12223             else {
12224 0           idx -= imin;
12225             }
12226              
12227 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
12228             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
12229              
12230 0 0         if ( have_weight ){
12231             {
12232             /* Kahan Summation increment for b_data */
12233 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12234             /* use temporaries to minimize access of piddle arrays */
12235 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12236 0           double tsum = temp + y;
12237 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12238 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12239             }
12240             ;
12241             }
12242             else {
12243             {
12244             /* Kahan Summation increment for b_data */
12245 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12246             /* use temporaries to minimize access of piddle arrays */
12247 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12248 0           double tsum = temp + y;
12249 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12250 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12251             }
12252             ;
12253             }
12254              
12255 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
12256 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
12257              
12258             {
12259 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
12260             double d_mean;
12261              
12262 0 0         if ( have_weight ) {
12263 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12264              
12265             {
12266             /* Kahan Summation increment for b_weight */
12267 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12268             /* use temporaries to minimize access of piddle arrays */
12269 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
12270 0           double tsum = temp + y;
12271 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
12272 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
12273             }
12274             ;
12275             {
12276             /* Kahan Summation increment for b_weight2 */
12277 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
12278             /* use temporaries to minimize access of piddle arrays */
12279 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
12280 0           double tsum = temp + y;
12281 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
12282 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
12283             }
12284             ;
12285              
12286 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12287              
12288 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12289 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
12290             }
12291              
12292             else {
12293 0           d_mean = ( data - prev_mean ) / count;
12294 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12295 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
12296             }
12297             }
12298             }} /* Close n */
12299              
12300             {
12301 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
12302             }
12303             ;
12304              
12305             /* if there were no data in the bin, set some derived values to BAD_VAL */
12306            
12307             {
12308             /* if any are bad */
12309 0           int set_bad = 0;
12310 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
12311              
12312 0 0         if ( set_bad ) {
12313 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
12314              
12315 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
12316 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
12317              
12318 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
12319 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
12320              
12321 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
12322 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
12323             }
12324             }
12325            
12326              
12327 0 0         if ( have_weight ) {
12328             {
12329 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
12330             }
12331             ;
12332             {
12333 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
12334             }
12335             ;
12336             }
12337              
12338 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
12339             }
12340 0           } break;
12341 0           case PDL_LL: {
12342 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_LongLong,Q,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
12343             {
12344              
12345              
12346              
12347              
12348 0           int flags = __params->optflags;
12349              
12350 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
12351 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
12352              
12353 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
12354              
12355 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
12356 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
12357 0           PDL_Indx imax = imin + nbins - 1;
12358              
12359             /* if first bin is reserved for oob, need to
12360             offset imin to ensure that non-oob data start
12361             at second bin */
12362 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
12363 0           imin -= 1;
12364              
12365             /* intialize output and temp bin data one at a time to
12366             avoid trashing the cache */
12367            
12368             /* initialize Kahan Summation for b_data */
12369 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
12370 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
12371             ;
12372 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
12373 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
12374 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
12375              
12376 0 0         if ( have_weight ) {
12377            
12378             /* initialize Kahan Summation for b_weight */
12379 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
12380 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
12381             ;
12382            
12383             /* initialize Kahan Summation for b_weight2 */
12384 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
12385 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
12386             ;
12387             }
12388              
12389             /* if we could preset min & max to the initial value in a bin,
12390             we could shave off a comparison. Unfortunately, we can't
12391             do that, as we can't know apriori which is the first
12392             element in a bin. */
12393 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
12394 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
12395              
12396 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
12397              
12398             PDL_Indx count;
12399 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
12400 0           double data = (data_datap)[0+(__inc_data_n*(n))];
12401             double weight;
12402              
12403 0           PDL_Indx oob_low =
12404 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
12405 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
12406 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
12407 0 0         : -1; /* error */
12408              
12409 0           PDL_Indx oob_high =
12410 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
12411 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
12412 0 0         : -1; /* error */
12413              
12414              
12415             #ifdef PDL_BAD_CODE
12416 0 0         if ( PDL_ISBAD2(data,data_badval,Q,data_badval_isnan)
12417 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
12418             ) {
12419 0           continue;
12420             }
12421             #endif /* PDL_BAD_CODE */
12422              
12423 0 0         if ( have_weight ) {
12424 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
12425              
12426             #ifdef PDL_BAD_CODE
12427 0 0         if ( PDL_ISBAD2(weight,weight_badval,Q,weight_badval_isnan) )
    0          
12428 0           continue;
12429             #endif /* PDL_BAD_CODE */
12430             }
12431              
12432              
12433 0 0         if ( idx < imin ) {
12434 0 0         if ( save_oob ) idx = oob_low;
12435 0           else continue;
12436             }
12437 0 0         else if ( idx > imax ) {
12438 0 0         if ( save_oob ) idx = oob_high;
12439 0           else continue;
12440             }
12441             else {
12442 0           idx -= imin;
12443             }
12444              
12445 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
12446             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
12447              
12448 0 0         if ( have_weight ){
12449             {
12450             /* Kahan Summation increment for b_data */
12451 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12452             /* use temporaries to minimize access of piddle arrays */
12453 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12454 0           double tsum = temp + y;
12455 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12456 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12457             }
12458             ;
12459             }
12460             else {
12461             {
12462             /* Kahan Summation increment for b_data */
12463 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12464             /* use temporaries to minimize access of piddle arrays */
12465 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12466 0           double tsum = temp + y;
12467 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12468 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12469             }
12470             ;
12471             }
12472              
12473 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
12474 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
12475              
12476             {
12477 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
12478             double d_mean;
12479              
12480 0 0         if ( have_weight ) {
12481 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12482              
12483             {
12484             /* Kahan Summation increment for b_weight */
12485 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12486             /* use temporaries to minimize access of piddle arrays */
12487 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
12488 0           double tsum = temp + y;
12489 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
12490 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
12491             }
12492             ;
12493             {
12494             /* Kahan Summation increment for b_weight2 */
12495 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
12496             /* use temporaries to minimize access of piddle arrays */
12497 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
12498 0           double tsum = temp + y;
12499 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
12500 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
12501             }
12502             ;
12503              
12504 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12505              
12506 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12507 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
12508             }
12509              
12510             else {
12511 0           d_mean = ( data - prev_mean ) / count;
12512 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12513 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
12514             }
12515             }
12516             }} /* Close n */
12517              
12518             {
12519 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
12520             }
12521             ;
12522              
12523             /* if there were no data in the bin, set some derived values to BAD_VAL */
12524            
12525             {
12526             /* if any are bad */
12527 0           int set_bad = 0;
12528 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
12529              
12530 0 0         if ( set_bad ) {
12531 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
12532              
12533 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
12534 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
12535              
12536 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
12537 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
12538              
12539 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
12540 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
12541             }
12542             }
12543            
12544              
12545 0 0         if ( have_weight ) {
12546             {
12547 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
12548             }
12549             ;
12550             {
12551 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
12552             }
12553             ;
12554             }
12555              
12556 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
12557             }
12558 0           } break;
12559 0           case PDL_F: {
12560 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Float,F,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
12561             {
12562              
12563              
12564              
12565              
12566 0           int flags = __params->optflags;
12567              
12568 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
12569 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
12570              
12571 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
12572              
12573 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
12574 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
12575 0           PDL_Indx imax = imin + nbins - 1;
12576              
12577             /* if first bin is reserved for oob, need to
12578             offset imin to ensure that non-oob data start
12579             at second bin */
12580 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
12581 0           imin -= 1;
12582              
12583             /* intialize output and temp bin data one at a time to
12584             avoid trashing the cache */
12585            
12586             /* initialize Kahan Summation for b_data */
12587 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
12588 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
12589             ;
12590 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
12591 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
12592 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
12593              
12594 0 0         if ( have_weight ) {
12595            
12596             /* initialize Kahan Summation for b_weight */
12597 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
12598 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
12599             ;
12600            
12601             /* initialize Kahan Summation for b_weight2 */
12602 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
12603 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
12604             ;
12605             }
12606              
12607             /* if we could preset min & max to the initial value in a bin,
12608             we could shave off a comparison. Unfortunately, we can't
12609             do that, as we can't know apriori which is the first
12610             element in a bin. */
12611 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
12612 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
12613              
12614 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
12615              
12616             PDL_Indx count;
12617 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
12618 0           double data = (data_datap)[0+(__inc_data_n*(n))];
12619             double weight;
12620              
12621 0           PDL_Indx oob_low =
12622 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
12623 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
12624 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
12625 0 0         : -1; /* error */
12626              
12627 0           PDL_Indx oob_high =
12628 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
12629 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
12630 0 0         : -1; /* error */
12631              
12632              
12633             #ifdef PDL_BAD_CODE
12634 0 0         if ( PDL_ISBAD2(data,data_badval,F,data_badval_isnan)
    0          
12635 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
12636             ) {
12637 0           continue;
12638             }
12639             #endif /* PDL_BAD_CODE */
12640              
12641 0 0         if ( have_weight ) {
12642 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
12643              
12644             #ifdef PDL_BAD_CODE
12645 0 0         if ( PDL_ISBAD2(weight,weight_badval,F,weight_badval_isnan) )
    0          
12646 0           continue;
12647             #endif /* PDL_BAD_CODE */
12648             }
12649              
12650              
12651 0 0         if ( idx < imin ) {
12652 0 0         if ( save_oob ) idx = oob_low;
12653 0           else continue;
12654             }
12655 0 0         else if ( idx > imax ) {
12656 0 0         if ( save_oob ) idx = oob_high;
12657 0           else continue;
12658             }
12659             else {
12660 0           idx -= imin;
12661             }
12662              
12663 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
12664             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
12665              
12666 0 0         if ( have_weight ){
12667             {
12668             /* Kahan Summation increment for b_data */
12669 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12670             /* use temporaries to minimize access of piddle arrays */
12671 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12672 0           double tsum = temp + y;
12673 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12674 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12675             }
12676             ;
12677             }
12678             else {
12679             {
12680             /* Kahan Summation increment for b_data */
12681 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12682             /* use temporaries to minimize access of piddle arrays */
12683 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12684 0           double tsum = temp + y;
12685 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12686 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12687             }
12688             ;
12689             }
12690              
12691 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
12692 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
12693              
12694             {
12695 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
12696             double d_mean;
12697              
12698 0 0         if ( have_weight ) {
12699 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12700              
12701             {
12702             /* Kahan Summation increment for b_weight */
12703 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12704             /* use temporaries to minimize access of piddle arrays */
12705 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
12706 0           double tsum = temp + y;
12707 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
12708 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
12709             }
12710             ;
12711             {
12712             /* Kahan Summation increment for b_weight2 */
12713 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
12714             /* use temporaries to minimize access of piddle arrays */
12715 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
12716 0           double tsum = temp + y;
12717 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
12718 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
12719             }
12720             ;
12721              
12722 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12723              
12724 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12725 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
12726             }
12727              
12728             else {
12729 0           d_mean = ( data - prev_mean ) / count;
12730 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12731 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
12732             }
12733             }
12734             }} /* Close n */
12735              
12736             {
12737 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
12738             }
12739             ;
12740              
12741             /* if there were no data in the bin, set some derived values to BAD_VAL */
12742            
12743             {
12744             /* if any are bad */
12745 0           int set_bad = 0;
12746 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
12747              
12748 0 0         if ( set_bad ) {
12749 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
12750              
12751 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
12752 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
12753              
12754 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
12755 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
12756              
12757 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
12758 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
12759             }
12760             }
12761            
12762              
12763 0 0         if ( have_weight ) {
12764             {
12765 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
12766             }
12767             ;
12768             {
12769 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
12770             }
12771             ;
12772             }
12773              
12774 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
12775             }
12776 0           } break;
12777 0           case PDL_D: {
12778 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
12779             {
12780              
12781              
12782              
12783              
12784 0           int flags = __params->optflags;
12785              
12786 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
12787 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
12788              
12789 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
12790              
12791 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
12792 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
12793 0           PDL_Indx imax = imin + nbins - 1;
12794              
12795             /* if first bin is reserved for oob, need to
12796             offset imin to ensure that non-oob data start
12797             at second bin */
12798 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
12799 0           imin -= 1;
12800              
12801             /* intialize output and temp bin data one at a time to
12802             avoid trashing the cache */
12803            
12804             /* initialize Kahan Summation for b_data */
12805 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
12806 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
12807             ;
12808 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
12809 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
12810 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
12811              
12812 0 0         if ( have_weight ) {
12813            
12814             /* initialize Kahan Summation for b_weight */
12815 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
12816 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
12817             ;
12818            
12819             /* initialize Kahan Summation for b_weight2 */
12820 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
12821 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
12822             ;
12823             }
12824              
12825             /* if we could preset min & max to the initial value in a bin,
12826             we could shave off a comparison. Unfortunately, we can't
12827             do that, as we can't know apriori which is the first
12828             element in a bin. */
12829 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
12830 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
12831              
12832 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
12833              
12834             PDL_Indx count;
12835 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
12836 0           double data = (data_datap)[0+(__inc_data_n*(n))];
12837             double weight;
12838              
12839 0           PDL_Indx oob_low =
12840 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
12841 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
12842 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
12843 0 0         : -1; /* error */
12844              
12845 0           PDL_Indx oob_high =
12846 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
12847 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
12848 0 0         : -1; /* error */
12849              
12850              
12851             #ifdef PDL_BAD_CODE
12852 0 0         if ( PDL_ISBAD2(data,data_badval,D,data_badval_isnan)
    0          
12853 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
12854             ) {
12855 0           continue;
12856             }
12857             #endif /* PDL_BAD_CODE */
12858              
12859 0 0         if ( have_weight ) {
12860 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
12861              
12862             #ifdef PDL_BAD_CODE
12863 0 0         if ( PDL_ISBAD2(weight,weight_badval,D,weight_badval_isnan) )
    0          
12864 0           continue;
12865             #endif /* PDL_BAD_CODE */
12866             }
12867              
12868              
12869 0 0         if ( idx < imin ) {
12870 0 0         if ( save_oob ) idx = oob_low;
12871 0           else continue;
12872             }
12873 0 0         else if ( idx > imax ) {
12874 0 0         if ( save_oob ) idx = oob_high;
12875 0           else continue;
12876             }
12877             else {
12878 0           idx -= imin;
12879             }
12880              
12881 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
12882             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
12883              
12884 0 0         if ( have_weight ){
12885             {
12886             /* Kahan Summation increment for b_data */
12887 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12888             /* use temporaries to minimize access of piddle arrays */
12889 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12890 0           double tsum = temp + y;
12891 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12892 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12893             }
12894             ;
12895             }
12896             else {
12897             {
12898             /* Kahan Summation increment for b_data */
12899 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
12900             /* use temporaries to minimize access of piddle arrays */
12901 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
12902 0           double tsum = temp + y;
12903 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
12904 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
12905             }
12906             ;
12907             }
12908              
12909 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
12910 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
12911              
12912             {
12913 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
12914             double d_mean;
12915              
12916 0 0         if ( have_weight ) {
12917 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12918              
12919             {
12920             /* Kahan Summation increment for b_weight */
12921 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12922             /* use temporaries to minimize access of piddle arrays */
12923 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
12924 0           double tsum = temp + y;
12925 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
12926 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
12927             }
12928             ;
12929             {
12930             /* Kahan Summation increment for b_weight2 */
12931 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
12932             /* use temporaries to minimize access of piddle arrays */
12933 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
12934 0           double tsum = temp + y;
12935 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
12936 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
12937             }
12938             ;
12939              
12940 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
12941              
12942 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12943 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
12944             }
12945              
12946             else {
12947 0           d_mean = ( data - prev_mean ) / count;
12948 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
12949 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
12950             }
12951             }
12952             }} /* Close n */
12953              
12954             {
12955 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
12956             }
12957             ;
12958              
12959             /* if there were no data in the bin, set some derived values to BAD_VAL */
12960            
12961             {
12962             /* if any are bad */
12963 0           int set_bad = 0;
12964 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
12965              
12966 0 0         if ( set_bad ) {
12967 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
12968              
12969 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
12970 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
12971              
12972 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
12973 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
12974              
12975 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
12976 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
12977             }
12978             }
12979            
12980              
12981 0 0         if ( have_weight ) {
12982             {
12983 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
12984             }
12985             ;
12986             {
12987 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
12988             }
12989             ;
12990             }
12991              
12992 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
12993             }
12994 0           } break;
12995 0           case PDL_LD: {
12996 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_LDouble,E,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
12997             {
12998              
12999              
13000              
13001              
13002 0           int flags = __params->optflags;
13003              
13004 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
13005 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
13006              
13007 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
13008              
13009 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
13010 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
13011 0           PDL_Indx imax = imin + nbins - 1;
13012              
13013             /* if first bin is reserved for oob, need to
13014             offset imin to ensure that non-oob data start
13015             at second bin */
13016 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
13017 0           imin -= 1;
13018              
13019             /* intialize output and temp bin data one at a time to
13020             avoid trashing the cache */
13021            
13022             /* initialize Kahan Summation for b_data */
13023 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
13024 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
13025             ;
13026 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
13027 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
13028 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
13029              
13030 0 0         if ( have_weight ) {
13031            
13032             /* initialize Kahan Summation for b_weight */
13033 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
13034 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
13035             ;
13036            
13037             /* initialize Kahan Summation for b_weight2 */
13038 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
13039 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
13040             ;
13041             }
13042              
13043             /* if we could preset min & max to the initial value in a bin,
13044             we could shave off a comparison. Unfortunately, we can't
13045             do that, as we can't know apriori which is the first
13046             element in a bin. */
13047 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
13048 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
13049              
13050 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
13051              
13052             PDL_Indx count;
13053 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
13054 0           double data = (data_datap)[0+(__inc_data_n*(n))];
13055             double weight;
13056              
13057 0           PDL_Indx oob_low =
13058 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
13059 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
13060 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
13061 0 0         : -1; /* error */
13062              
13063 0           PDL_Indx oob_high =
13064 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
13065 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
13066 0 0         : -1; /* error */
13067              
13068              
13069             #ifdef PDL_BAD_CODE
13070 0 0         if ( PDL_ISBAD2(data,data_badval,E,data_badval_isnan)
    0          
13071 0 0         || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
    0          
    0          
13072             ) {
13073 0           continue;
13074             }
13075             #endif /* PDL_BAD_CODE */
13076              
13077 0 0         if ( have_weight ) {
13078 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
13079              
13080             #ifdef PDL_BAD_CODE
13081 0 0         if ( PDL_ISBAD2(weight,weight_badval,E,weight_badval_isnan) )
    0          
13082 0           continue;
13083             #endif /* PDL_BAD_CODE */
13084             }
13085              
13086              
13087 0 0         if ( idx < imin ) {
13088 0 0         if ( save_oob ) idx = oob_low;
13089 0           else continue;
13090             }
13091 0 0         else if ( idx > imax ) {
13092 0 0         if ( save_oob ) idx = oob_high;
13093 0           else continue;
13094             }
13095             else {
13096 0           idx -= imin;
13097             }
13098              
13099 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
13100             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
13101              
13102 0 0         if ( have_weight ){
13103             {
13104             /* Kahan Summation increment for b_data */
13105 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13106             /* use temporaries to minimize access of piddle arrays */
13107 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13108 0           double tsum = temp + y;
13109 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13110 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13111             }
13112             ;
13113             }
13114             else {
13115             {
13116             /* Kahan Summation increment for b_data */
13117 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13118             /* use temporaries to minimize access of piddle arrays */
13119 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13120 0           double tsum = temp + y;
13121 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13122 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13123             }
13124             ;
13125             }
13126              
13127 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
13128 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
13129              
13130             {
13131 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
13132             double d_mean;
13133              
13134 0 0         if ( have_weight ) {
13135 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13136              
13137             {
13138             /* Kahan Summation increment for b_weight */
13139 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13140             /* use temporaries to minimize access of piddle arrays */
13141 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
13142 0           double tsum = temp + y;
13143 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
13144 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
13145             }
13146             ;
13147             {
13148             /* Kahan Summation increment for b_weight2 */
13149 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
13150             /* use temporaries to minimize access of piddle arrays */
13151 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
13152 0           double tsum = temp + y;
13153 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
13154 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
13155             }
13156             ;
13157              
13158 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13159              
13160 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13161 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
13162             }
13163              
13164             else {
13165 0           d_mean = ( data - prev_mean ) / count;
13166 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13167 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
13168             }
13169             }
13170             }} /* Close n */
13171              
13172             {
13173 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
13174             }
13175             ;
13176              
13177             /* if there were no data in the bin, set some derived values to BAD_VAL */
13178            
13179             {
13180             /* if any are bad */
13181 0           int set_bad = 0;
13182 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
13183              
13184 0 0         if ( set_bad ) {
13185 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
13186              
13187 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
13188 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
13189              
13190 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
13191 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
13192              
13193 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
13194 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
13195             }
13196             }
13197            
13198              
13199 0 0         if ( have_weight ) {
13200             {
13201 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
13202             }
13203             ;
13204             {
13205 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
13206             }
13207             ;
13208             }
13209              
13210 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
13211             }
13212 0           } break;
13213 0           default: return PDL->make_error(PDL_EUSERERROR, "PP INTERNAL ERROR in bin_on_index: unhandled datatype(%d), only handles (ABSULKNPQFDE)! PLEASE MAKE A BUG REPORT\n", __privtrans->__datatype);
13214             }
13215             #undef PDL_BAD_CODE
13216             #undef PDL_IF_BAD
13217             } else { /* ** else do 'good' Code ** */
13218             #define PDL_IF_BAD(t,f) f
13219 50           switch (__privtrans->__datatype) { /* Start generic switch */
13220 0           case PDL_SB: {
13221 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_SByte,A,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
13222             {
13223              
13224              
13225              
13226              
13227 0           int flags = __params->optflags;
13228              
13229 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
13230 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
13231              
13232 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
13233              
13234 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
13235 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
13236 0           PDL_Indx imax = imin + nbins - 1;
13237              
13238             /* if first bin is reserved for oob, need to
13239             offset imin to ensure that non-oob data start
13240             at second bin */
13241 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
13242 0           imin -= 1;
13243              
13244             /* intialize output and temp bin data one at a time to
13245             avoid trashing the cache */
13246            
13247             /* initialize Kahan Summation for b_data */
13248 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
13249 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
13250             ;
13251 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
13252 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
13253 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
13254              
13255 0 0         if ( have_weight ) {
13256            
13257             /* initialize Kahan Summation for b_weight */
13258 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
13259 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
13260             ;
13261            
13262             /* initialize Kahan Summation for b_weight2 */
13263 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
13264 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
13265             ;
13266             }
13267              
13268             /* if we could preset min & max to the initial value in a bin,
13269             we could shave off a comparison. Unfortunately, we can't
13270             do that, as we can't know apriori which is the first
13271             element in a bin. */
13272 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
13273 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
13274              
13275 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
13276              
13277             PDL_Indx count;
13278 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
13279 0           double data = (data_datap)[0+(__inc_data_n*(n))];
13280             double weight;
13281              
13282 0           PDL_Indx oob_low =
13283 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
13284 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
13285 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
13286 0 0         : -1; /* error */
13287              
13288 0           PDL_Indx oob_high =
13289 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
13290 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
13291 0 0         : -1; /* error */
13292              
13293              
13294             #ifdef PDL_BAD_CODE
13295             if ( PDL_ISBAD2(data,data_badval,A,data_badval_isnan)
13296             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
13297             ) {
13298             continue;
13299             }
13300             #endif /* PDL_BAD_CODE */
13301              
13302 0 0         if ( have_weight ) {
13303 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
13304              
13305             #ifdef PDL_BAD_CODE
13306             if ( PDL_ISBAD2(weight,weight_badval,A,weight_badval_isnan) )
13307             continue;
13308             #endif /* PDL_BAD_CODE */
13309             }
13310              
13311              
13312 0 0         if ( idx < imin ) {
13313 0 0         if ( save_oob ) idx = oob_low;
13314 0           else continue;
13315             }
13316 0 0         else if ( idx > imax ) {
13317 0 0         if ( save_oob ) idx = oob_high;
13318 0           else continue;
13319             }
13320             else {
13321 0           idx -= imin;
13322             }
13323              
13324 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
13325             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
13326              
13327 0 0         if ( have_weight ){
13328             {
13329             /* Kahan Summation increment for b_data */
13330 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13331             /* use temporaries to minimize access of piddle arrays */
13332 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13333 0           double tsum = temp + y;
13334 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13335 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13336             }
13337             ;
13338             }
13339             else {
13340             {
13341             /* Kahan Summation increment for b_data */
13342 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13343             /* use temporaries to minimize access of piddle arrays */
13344 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13345 0           double tsum = temp + y;
13346 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13347 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13348             }
13349             ;
13350             }
13351              
13352 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
13353 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
13354              
13355             {
13356 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
13357             double d_mean;
13358              
13359 0 0         if ( have_weight ) {
13360 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13361              
13362             {
13363             /* Kahan Summation increment for b_weight */
13364 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13365             /* use temporaries to minimize access of piddle arrays */
13366 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
13367 0           double tsum = temp + y;
13368 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
13369 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
13370             }
13371             ;
13372             {
13373             /* Kahan Summation increment for b_weight2 */
13374 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
13375             /* use temporaries to minimize access of piddle arrays */
13376 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
13377 0           double tsum = temp + y;
13378 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
13379 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
13380             }
13381             ;
13382              
13383 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13384              
13385 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13386 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
13387             }
13388              
13389             else {
13390 0           d_mean = ( data - prev_mean ) / count;
13391 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13392 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
13393             }
13394             }
13395             }} /* Close n */
13396              
13397             {
13398 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
13399             }
13400             ;
13401              
13402             /* if there were no data in the bin, set some derived values to BAD_VAL */
13403            
13404             {
13405             /* if any are bad */
13406 0           int set_bad = 0;
13407 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
13408              
13409 0 0         if ( set_bad ) {
13410 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
13411              
13412 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
13413 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
13414              
13415 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
13416 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
13417              
13418 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
13419 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
13420             }
13421             }
13422            
13423              
13424 0 0         if ( have_weight ) {
13425             {
13426 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
13427             }
13428             ;
13429             {
13430 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
13431             }
13432             ;
13433             }
13434              
13435 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
13436             }
13437 0           } break;
13438 0           case PDL_B: {
13439 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Byte,B,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
13440             {
13441              
13442              
13443              
13444              
13445 0           int flags = __params->optflags;
13446              
13447 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
13448 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
13449              
13450 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
13451              
13452 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
13453 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
13454 0           PDL_Indx imax = imin + nbins - 1;
13455              
13456             /* if first bin is reserved for oob, need to
13457             offset imin to ensure that non-oob data start
13458             at second bin */
13459 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
13460 0           imin -= 1;
13461              
13462             /* intialize output and temp bin data one at a time to
13463             avoid trashing the cache */
13464            
13465             /* initialize Kahan Summation for b_data */
13466 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
13467 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
13468             ;
13469 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
13470 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
13471 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
13472              
13473 0 0         if ( have_weight ) {
13474            
13475             /* initialize Kahan Summation for b_weight */
13476 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
13477 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
13478             ;
13479            
13480             /* initialize Kahan Summation for b_weight2 */
13481 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
13482 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
13483             ;
13484             }
13485              
13486             /* if we could preset min & max to the initial value in a bin,
13487             we could shave off a comparison. Unfortunately, we can't
13488             do that, as we can't know apriori which is the first
13489             element in a bin. */
13490 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
13491 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
13492              
13493 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
13494              
13495             PDL_Indx count;
13496 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
13497 0           double data = (data_datap)[0+(__inc_data_n*(n))];
13498             double weight;
13499              
13500 0           PDL_Indx oob_low =
13501 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
13502 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
13503 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
13504 0 0         : -1; /* error */
13505              
13506 0           PDL_Indx oob_high =
13507 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
13508 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
13509 0 0         : -1; /* error */
13510              
13511              
13512             #ifdef PDL_BAD_CODE
13513             if ( PDL_ISBAD2(data,data_badval,B,data_badval_isnan)
13514             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
13515             ) {
13516             continue;
13517             }
13518             #endif /* PDL_BAD_CODE */
13519              
13520 0 0         if ( have_weight ) {
13521 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
13522              
13523             #ifdef PDL_BAD_CODE
13524             if ( PDL_ISBAD2(weight,weight_badval,B,weight_badval_isnan) )
13525             continue;
13526             #endif /* PDL_BAD_CODE */
13527             }
13528              
13529              
13530 0 0         if ( idx < imin ) {
13531 0 0         if ( save_oob ) idx = oob_low;
13532 0           else continue;
13533             }
13534 0 0         else if ( idx > imax ) {
13535 0 0         if ( save_oob ) idx = oob_high;
13536 0           else continue;
13537             }
13538             else {
13539 0           idx -= imin;
13540             }
13541              
13542 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
13543             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
13544              
13545 0 0         if ( have_weight ){
13546             {
13547             /* Kahan Summation increment for b_data */
13548 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13549             /* use temporaries to minimize access of piddle arrays */
13550 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13551 0           double tsum = temp + y;
13552 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13553 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13554             }
13555             ;
13556             }
13557             else {
13558             {
13559             /* Kahan Summation increment for b_data */
13560 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13561             /* use temporaries to minimize access of piddle arrays */
13562 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13563 0           double tsum = temp + y;
13564 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13565 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13566             }
13567             ;
13568             }
13569              
13570 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
13571 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
13572              
13573             {
13574 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
13575             double d_mean;
13576              
13577 0 0         if ( have_weight ) {
13578 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13579              
13580             {
13581             /* Kahan Summation increment for b_weight */
13582 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13583             /* use temporaries to minimize access of piddle arrays */
13584 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
13585 0           double tsum = temp + y;
13586 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
13587 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
13588             }
13589             ;
13590             {
13591             /* Kahan Summation increment for b_weight2 */
13592 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
13593             /* use temporaries to minimize access of piddle arrays */
13594 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
13595 0           double tsum = temp + y;
13596 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
13597 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
13598             }
13599             ;
13600              
13601 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13602              
13603 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13604 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
13605             }
13606              
13607             else {
13608 0           d_mean = ( data - prev_mean ) / count;
13609 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13610 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
13611             }
13612             }
13613             }} /* Close n */
13614              
13615             {
13616 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
13617             }
13618             ;
13619              
13620             /* if there were no data in the bin, set some derived values to BAD_VAL */
13621            
13622             {
13623             /* if any are bad */
13624 0           int set_bad = 0;
13625 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
13626              
13627 0 0         if ( set_bad ) {
13628 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
13629              
13630 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
13631 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
13632              
13633 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
13634 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
13635              
13636 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
13637 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
13638             }
13639             }
13640            
13641              
13642 0 0         if ( have_weight ) {
13643             {
13644 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
13645             }
13646             ;
13647             {
13648 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
13649             }
13650             ;
13651             }
13652              
13653 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
13654             }
13655 0           } break;
13656 0           case PDL_S: {
13657 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Short,S,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
13658             {
13659              
13660              
13661              
13662              
13663 0           int flags = __params->optflags;
13664              
13665 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
13666 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
13667              
13668 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
13669              
13670 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
13671 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
13672 0           PDL_Indx imax = imin + nbins - 1;
13673              
13674             /* if first bin is reserved for oob, need to
13675             offset imin to ensure that non-oob data start
13676             at second bin */
13677 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
13678 0           imin -= 1;
13679              
13680             /* intialize output and temp bin data one at a time to
13681             avoid trashing the cache */
13682            
13683             /* initialize Kahan Summation for b_data */
13684 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
13685 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
13686             ;
13687 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
13688 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
13689 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
13690              
13691 0 0         if ( have_weight ) {
13692            
13693             /* initialize Kahan Summation for b_weight */
13694 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
13695 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
13696             ;
13697            
13698             /* initialize Kahan Summation for b_weight2 */
13699 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
13700 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
13701             ;
13702             }
13703              
13704             /* if we could preset min & max to the initial value in a bin,
13705             we could shave off a comparison. Unfortunately, we can't
13706             do that, as we can't know apriori which is the first
13707             element in a bin. */
13708 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
13709 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
13710              
13711 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
13712              
13713             PDL_Indx count;
13714 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
13715 0           double data = (data_datap)[0+(__inc_data_n*(n))];
13716             double weight;
13717              
13718 0           PDL_Indx oob_low =
13719 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
13720 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
13721 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
13722 0 0         : -1; /* error */
13723              
13724 0           PDL_Indx oob_high =
13725 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
13726 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
13727 0 0         : -1; /* error */
13728              
13729              
13730             #ifdef PDL_BAD_CODE
13731             if ( PDL_ISBAD2(data,data_badval,S,data_badval_isnan)
13732             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
13733             ) {
13734             continue;
13735             }
13736             #endif /* PDL_BAD_CODE */
13737              
13738 0 0         if ( have_weight ) {
13739 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
13740              
13741             #ifdef PDL_BAD_CODE
13742             if ( PDL_ISBAD2(weight,weight_badval,S,weight_badval_isnan) )
13743             continue;
13744             #endif /* PDL_BAD_CODE */
13745             }
13746              
13747              
13748 0 0         if ( idx < imin ) {
13749 0 0         if ( save_oob ) idx = oob_low;
13750 0           else continue;
13751             }
13752 0 0         else if ( idx > imax ) {
13753 0 0         if ( save_oob ) idx = oob_high;
13754 0           else continue;
13755             }
13756             else {
13757 0           idx -= imin;
13758             }
13759              
13760 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
13761             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
13762              
13763 0 0         if ( have_weight ){
13764             {
13765             /* Kahan Summation increment for b_data */
13766 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13767             /* use temporaries to minimize access of piddle arrays */
13768 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13769 0           double tsum = temp + y;
13770 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13771 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13772             }
13773             ;
13774             }
13775             else {
13776             {
13777             /* Kahan Summation increment for b_data */
13778 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13779             /* use temporaries to minimize access of piddle arrays */
13780 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13781 0           double tsum = temp + y;
13782 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13783 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13784             }
13785             ;
13786             }
13787              
13788 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
13789 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
13790              
13791             {
13792 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
13793             double d_mean;
13794              
13795 0 0         if ( have_weight ) {
13796 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13797              
13798             {
13799             /* Kahan Summation increment for b_weight */
13800 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13801             /* use temporaries to minimize access of piddle arrays */
13802 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
13803 0           double tsum = temp + y;
13804 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
13805 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
13806             }
13807             ;
13808             {
13809             /* Kahan Summation increment for b_weight2 */
13810 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
13811             /* use temporaries to minimize access of piddle arrays */
13812 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
13813 0           double tsum = temp + y;
13814 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
13815 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
13816             }
13817             ;
13818              
13819 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
13820              
13821 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13822 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
13823             }
13824              
13825             else {
13826 0           d_mean = ( data - prev_mean ) / count;
13827 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
13828 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
13829             }
13830             }
13831             }} /* Close n */
13832              
13833             {
13834 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
13835             }
13836             ;
13837              
13838             /* if there were no data in the bin, set some derived values to BAD_VAL */
13839            
13840             {
13841             /* if any are bad */
13842 0           int set_bad = 0;
13843 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
13844              
13845 0 0         if ( set_bad ) {
13846 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
13847              
13848 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
13849 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
13850              
13851 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
13852 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
13853              
13854 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
13855 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
13856             }
13857             }
13858            
13859              
13860 0 0         if ( have_weight ) {
13861             {
13862 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
13863             }
13864             ;
13865             {
13866 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
13867             }
13868             ;
13869             }
13870              
13871 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
13872             }
13873 0           } break;
13874 0           case PDL_US: {
13875 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Ushort,U,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
13876             {
13877              
13878              
13879              
13880              
13881 0           int flags = __params->optflags;
13882              
13883 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
13884 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
13885              
13886 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
13887              
13888 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
13889 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
13890 0           PDL_Indx imax = imin + nbins - 1;
13891              
13892             /* if first bin is reserved for oob, need to
13893             offset imin to ensure that non-oob data start
13894             at second bin */
13895 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
13896 0           imin -= 1;
13897              
13898             /* intialize output and temp bin data one at a time to
13899             avoid trashing the cache */
13900            
13901             /* initialize Kahan Summation for b_data */
13902 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
13903 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
13904             ;
13905 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
13906 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
13907 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
13908              
13909 0 0         if ( have_weight ) {
13910            
13911             /* initialize Kahan Summation for b_weight */
13912 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
13913 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
13914             ;
13915            
13916             /* initialize Kahan Summation for b_weight2 */
13917 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
13918 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
13919             ;
13920             }
13921              
13922             /* if we could preset min & max to the initial value in a bin,
13923             we could shave off a comparison. Unfortunately, we can't
13924             do that, as we can't know apriori which is the first
13925             element in a bin. */
13926 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
13927 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
13928              
13929 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
13930              
13931             PDL_Indx count;
13932 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
13933 0           double data = (data_datap)[0+(__inc_data_n*(n))];
13934             double weight;
13935              
13936 0           PDL_Indx oob_low =
13937 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
13938 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
13939 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
13940 0 0         : -1; /* error */
13941              
13942 0           PDL_Indx oob_high =
13943 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
13944 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
13945 0 0         : -1; /* error */
13946              
13947              
13948             #ifdef PDL_BAD_CODE
13949             if ( PDL_ISBAD2(data,data_badval,U,data_badval_isnan)
13950             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
13951             ) {
13952             continue;
13953             }
13954             #endif /* PDL_BAD_CODE */
13955              
13956 0 0         if ( have_weight ) {
13957 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
13958              
13959             #ifdef PDL_BAD_CODE
13960             if ( PDL_ISBAD2(weight,weight_badval,U,weight_badval_isnan) )
13961             continue;
13962             #endif /* PDL_BAD_CODE */
13963             }
13964              
13965              
13966 0 0         if ( idx < imin ) {
13967 0 0         if ( save_oob ) idx = oob_low;
13968 0           else continue;
13969             }
13970 0 0         else if ( idx > imax ) {
13971 0 0         if ( save_oob ) idx = oob_high;
13972 0           else continue;
13973             }
13974             else {
13975 0           idx -= imin;
13976             }
13977              
13978 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
13979             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
13980              
13981 0 0         if ( have_weight ){
13982             {
13983             /* Kahan Summation increment for b_data */
13984 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13985             /* use temporaries to minimize access of piddle arrays */
13986 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13987 0           double tsum = temp + y;
13988 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
13989 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
13990             }
13991             ;
13992             }
13993             else {
13994             {
13995             /* Kahan Summation increment for b_data */
13996 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
13997             /* use temporaries to minimize access of piddle arrays */
13998 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
13999 0           double tsum = temp + y;
14000 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14001 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14002             }
14003             ;
14004             }
14005              
14006 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
14007 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
14008              
14009             {
14010 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
14011             double d_mean;
14012              
14013 0 0         if ( have_weight ) {
14014 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14015              
14016             {
14017             /* Kahan Summation increment for b_weight */
14018 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14019             /* use temporaries to minimize access of piddle arrays */
14020 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
14021 0           double tsum = temp + y;
14022 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
14023 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
14024             }
14025             ;
14026             {
14027             /* Kahan Summation increment for b_weight2 */
14028 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
14029             /* use temporaries to minimize access of piddle arrays */
14030 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
14031 0           double tsum = temp + y;
14032 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
14033 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
14034             }
14035             ;
14036              
14037 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14038              
14039 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14040 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
14041             }
14042              
14043             else {
14044 0           d_mean = ( data - prev_mean ) / count;
14045 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14046 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
14047             }
14048             }
14049             }} /* Close n */
14050              
14051             {
14052 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
14053             }
14054             ;
14055              
14056             /* if there were no data in the bin, set some derived values to BAD_VAL */
14057            
14058             {
14059             /* if any are bad */
14060 0           int set_bad = 0;
14061 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
14062              
14063 0 0         if ( set_bad ) {
14064 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
14065              
14066 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
14067 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
14068              
14069 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
14070 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
14071              
14072 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
14073 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
14074             }
14075             }
14076            
14077              
14078 0 0         if ( have_weight ) {
14079             {
14080 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
14081             }
14082             ;
14083             {
14084 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
14085             }
14086             ;
14087             }
14088              
14089 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
14090             }
14091 0           } break;
14092 0           case PDL_L: {
14093 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Long,L,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
14094             {
14095              
14096              
14097              
14098              
14099 0           int flags = __params->optflags;
14100              
14101 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
14102 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
14103              
14104 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
14105              
14106 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
14107 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
14108 0           PDL_Indx imax = imin + nbins - 1;
14109              
14110             /* if first bin is reserved for oob, need to
14111             offset imin to ensure that non-oob data start
14112             at second bin */
14113 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
14114 0           imin -= 1;
14115              
14116             /* intialize output and temp bin data one at a time to
14117             avoid trashing the cache */
14118            
14119             /* initialize Kahan Summation for b_data */
14120 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
14121 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
14122             ;
14123 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
14124 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
14125 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
14126              
14127 0 0         if ( have_weight ) {
14128            
14129             /* initialize Kahan Summation for b_weight */
14130 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
14131 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
14132             ;
14133            
14134             /* initialize Kahan Summation for b_weight2 */
14135 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
14136 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
14137             ;
14138             }
14139              
14140             /* if we could preset min & max to the initial value in a bin,
14141             we could shave off a comparison. Unfortunately, we can't
14142             do that, as we can't know apriori which is the first
14143             element in a bin. */
14144 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
14145 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
14146              
14147 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
14148              
14149             PDL_Indx count;
14150 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
14151 0           double data = (data_datap)[0+(__inc_data_n*(n))];
14152             double weight;
14153              
14154 0           PDL_Indx oob_low =
14155 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
14156 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
14157 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
14158 0 0         : -1; /* error */
14159              
14160 0           PDL_Indx oob_high =
14161 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
14162 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
14163 0 0         : -1; /* error */
14164              
14165              
14166             #ifdef PDL_BAD_CODE
14167             if ( PDL_ISBAD2(data,data_badval,L,data_badval_isnan)
14168             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
14169             ) {
14170             continue;
14171             }
14172             #endif /* PDL_BAD_CODE */
14173              
14174 0 0         if ( have_weight ) {
14175 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
14176              
14177             #ifdef PDL_BAD_CODE
14178             if ( PDL_ISBAD2(weight,weight_badval,L,weight_badval_isnan) )
14179             continue;
14180             #endif /* PDL_BAD_CODE */
14181             }
14182              
14183              
14184 0 0         if ( idx < imin ) {
14185 0 0         if ( save_oob ) idx = oob_low;
14186 0           else continue;
14187             }
14188 0 0         else if ( idx > imax ) {
14189 0 0         if ( save_oob ) idx = oob_high;
14190 0           else continue;
14191             }
14192             else {
14193 0           idx -= imin;
14194             }
14195              
14196 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
14197             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
14198              
14199 0 0         if ( have_weight ){
14200             {
14201             /* Kahan Summation increment for b_data */
14202 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14203             /* use temporaries to minimize access of piddle arrays */
14204 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14205 0           double tsum = temp + y;
14206 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14207 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14208             }
14209             ;
14210             }
14211             else {
14212             {
14213             /* Kahan Summation increment for b_data */
14214 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14215             /* use temporaries to minimize access of piddle arrays */
14216 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14217 0           double tsum = temp + y;
14218 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14219 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14220             }
14221             ;
14222             }
14223              
14224 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
14225 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
14226              
14227             {
14228 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
14229             double d_mean;
14230              
14231 0 0         if ( have_weight ) {
14232 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14233              
14234             {
14235             /* Kahan Summation increment for b_weight */
14236 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14237             /* use temporaries to minimize access of piddle arrays */
14238 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
14239 0           double tsum = temp + y;
14240 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
14241 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
14242             }
14243             ;
14244             {
14245             /* Kahan Summation increment for b_weight2 */
14246 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
14247             /* use temporaries to minimize access of piddle arrays */
14248 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
14249 0           double tsum = temp + y;
14250 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
14251 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
14252             }
14253             ;
14254              
14255 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14256              
14257 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14258 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
14259             }
14260              
14261             else {
14262 0           d_mean = ( data - prev_mean ) / count;
14263 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14264 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
14265             }
14266             }
14267             }} /* Close n */
14268              
14269             {
14270 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
14271             }
14272             ;
14273              
14274             /* if there were no data in the bin, set some derived values to BAD_VAL */
14275            
14276             {
14277             /* if any are bad */
14278 0           int set_bad = 0;
14279 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
14280              
14281 0 0         if ( set_bad ) {
14282 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
14283              
14284 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
14285 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
14286              
14287 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
14288 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
14289              
14290 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
14291 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
14292             }
14293             }
14294            
14295              
14296 0 0         if ( have_weight ) {
14297             {
14298 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
14299             }
14300             ;
14301             {
14302 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
14303             }
14304             ;
14305             }
14306              
14307 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
14308             }
14309 0           } break;
14310 0           case PDL_UL: {
14311 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_ULong,K,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
14312             {
14313              
14314              
14315              
14316              
14317 0           int flags = __params->optflags;
14318              
14319 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
14320 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
14321              
14322 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
14323              
14324 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
14325 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
14326 0           PDL_Indx imax = imin + nbins - 1;
14327              
14328             /* if first bin is reserved for oob, need to
14329             offset imin to ensure that non-oob data start
14330             at second bin */
14331 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
14332 0           imin -= 1;
14333              
14334             /* intialize output and temp bin data one at a time to
14335             avoid trashing the cache */
14336            
14337             /* initialize Kahan Summation for b_data */
14338 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
14339 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
14340             ;
14341 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
14342 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
14343 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
14344              
14345 0 0         if ( have_weight ) {
14346            
14347             /* initialize Kahan Summation for b_weight */
14348 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
14349 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
14350             ;
14351            
14352             /* initialize Kahan Summation for b_weight2 */
14353 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
14354 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
14355             ;
14356             }
14357              
14358             /* if we could preset min & max to the initial value in a bin,
14359             we could shave off a comparison. Unfortunately, we can't
14360             do that, as we can't know apriori which is the first
14361             element in a bin. */
14362 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
14363 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
14364              
14365 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
14366              
14367             PDL_Indx count;
14368 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
14369 0           double data = (data_datap)[0+(__inc_data_n*(n))];
14370             double weight;
14371              
14372 0           PDL_Indx oob_low =
14373 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
14374 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
14375 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
14376 0 0         : -1; /* error */
14377              
14378 0           PDL_Indx oob_high =
14379 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
14380 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
14381 0 0         : -1; /* error */
14382              
14383              
14384             #ifdef PDL_BAD_CODE
14385             if ( PDL_ISBAD2(data,data_badval,K,data_badval_isnan)
14386             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
14387             ) {
14388             continue;
14389             }
14390             #endif /* PDL_BAD_CODE */
14391              
14392 0 0         if ( have_weight ) {
14393 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
14394              
14395             #ifdef PDL_BAD_CODE
14396             if ( PDL_ISBAD2(weight,weight_badval,K,weight_badval_isnan) )
14397             continue;
14398             #endif /* PDL_BAD_CODE */
14399             }
14400              
14401              
14402 0 0         if ( idx < imin ) {
14403 0 0         if ( save_oob ) idx = oob_low;
14404 0           else continue;
14405             }
14406 0 0         else if ( idx > imax ) {
14407 0 0         if ( save_oob ) idx = oob_high;
14408 0           else continue;
14409             }
14410             else {
14411 0           idx -= imin;
14412             }
14413              
14414 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
14415             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
14416              
14417 0 0         if ( have_weight ){
14418             {
14419             /* Kahan Summation increment for b_data */
14420 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14421             /* use temporaries to minimize access of piddle arrays */
14422 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14423 0           double tsum = temp + y;
14424 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14425 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14426             }
14427             ;
14428             }
14429             else {
14430             {
14431             /* Kahan Summation increment for b_data */
14432 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14433             /* use temporaries to minimize access of piddle arrays */
14434 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14435 0           double tsum = temp + y;
14436 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14437 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14438             }
14439             ;
14440             }
14441              
14442 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
14443 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
14444              
14445             {
14446 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
14447             double d_mean;
14448              
14449 0 0         if ( have_weight ) {
14450 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14451              
14452             {
14453             /* Kahan Summation increment for b_weight */
14454 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14455             /* use temporaries to minimize access of piddle arrays */
14456 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
14457 0           double tsum = temp + y;
14458 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
14459 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
14460             }
14461             ;
14462             {
14463             /* Kahan Summation increment for b_weight2 */
14464 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
14465             /* use temporaries to minimize access of piddle arrays */
14466 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
14467 0           double tsum = temp + y;
14468 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
14469 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
14470             }
14471             ;
14472              
14473 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14474              
14475 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14476 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
14477             }
14478              
14479             else {
14480 0           d_mean = ( data - prev_mean ) / count;
14481 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14482 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
14483             }
14484             }
14485             }} /* Close n */
14486              
14487             {
14488 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
14489             }
14490             ;
14491              
14492             /* if there were no data in the bin, set some derived values to BAD_VAL */
14493            
14494             {
14495             /* if any are bad */
14496 0           int set_bad = 0;
14497 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
14498              
14499 0 0         if ( set_bad ) {
14500 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
14501              
14502 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
14503 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
14504              
14505 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
14506 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
14507              
14508 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
14509 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
14510             }
14511             }
14512            
14513              
14514 0 0         if ( have_weight ) {
14515             {
14516 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
14517             }
14518             ;
14519             {
14520 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
14521             }
14522             ;
14523             }
14524              
14525 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
14526             }
14527 0           } break;
14528 0           case PDL_IND: {
14529 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
14530             {
14531              
14532              
14533              
14534              
14535 0           int flags = __params->optflags;
14536              
14537 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
14538 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
14539              
14540 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
14541              
14542 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
14543 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
14544 0           PDL_Indx imax = imin + nbins - 1;
14545              
14546             /* if first bin is reserved for oob, need to
14547             offset imin to ensure that non-oob data start
14548             at second bin */
14549 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
14550 0           imin -= 1;
14551              
14552             /* intialize output and temp bin data one at a time to
14553             avoid trashing the cache */
14554            
14555             /* initialize Kahan Summation for b_data */
14556 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
14557 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
14558             ;
14559 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
14560 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
14561 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
14562              
14563 0 0         if ( have_weight ) {
14564            
14565             /* initialize Kahan Summation for b_weight */
14566 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
14567 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
14568             ;
14569            
14570             /* initialize Kahan Summation for b_weight2 */
14571 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
14572 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
14573             ;
14574             }
14575              
14576             /* if we could preset min & max to the initial value in a bin,
14577             we could shave off a comparison. Unfortunately, we can't
14578             do that, as we can't know apriori which is the first
14579             element in a bin. */
14580 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
14581 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
14582              
14583 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
14584              
14585             PDL_Indx count;
14586 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
14587 0           double data = (data_datap)[0+(__inc_data_n*(n))];
14588             double weight;
14589              
14590 0           PDL_Indx oob_low =
14591 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
14592 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
14593 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
14594 0 0         : -1; /* error */
14595              
14596 0           PDL_Indx oob_high =
14597 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
14598 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
14599 0 0         : -1; /* error */
14600              
14601              
14602             #ifdef PDL_BAD_CODE
14603             if ( PDL_ISBAD2(data,data_badval,N,data_badval_isnan)
14604             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
14605             ) {
14606             continue;
14607             }
14608             #endif /* PDL_BAD_CODE */
14609              
14610 0 0         if ( have_weight ) {
14611 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
14612              
14613             #ifdef PDL_BAD_CODE
14614             if ( PDL_ISBAD2(weight,weight_badval,N,weight_badval_isnan) )
14615             continue;
14616             #endif /* PDL_BAD_CODE */
14617             }
14618              
14619              
14620 0 0         if ( idx < imin ) {
14621 0 0         if ( save_oob ) idx = oob_low;
14622 0           else continue;
14623             }
14624 0 0         else if ( idx > imax ) {
14625 0 0         if ( save_oob ) idx = oob_high;
14626 0           else continue;
14627             }
14628             else {
14629 0           idx -= imin;
14630             }
14631              
14632 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
14633             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
14634              
14635 0 0         if ( have_weight ){
14636             {
14637             /* Kahan Summation increment for b_data */
14638 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14639             /* use temporaries to minimize access of piddle arrays */
14640 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14641 0           double tsum = temp + y;
14642 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14643 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14644             }
14645             ;
14646             }
14647             else {
14648             {
14649             /* Kahan Summation increment for b_data */
14650 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14651             /* use temporaries to minimize access of piddle arrays */
14652 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14653 0           double tsum = temp + y;
14654 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14655 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14656             }
14657             ;
14658             }
14659              
14660 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
14661 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
14662              
14663             {
14664 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
14665             double d_mean;
14666              
14667 0 0         if ( have_weight ) {
14668 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14669              
14670             {
14671             /* Kahan Summation increment for b_weight */
14672 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14673             /* use temporaries to minimize access of piddle arrays */
14674 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
14675 0           double tsum = temp + y;
14676 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
14677 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
14678             }
14679             ;
14680             {
14681             /* Kahan Summation increment for b_weight2 */
14682 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
14683             /* use temporaries to minimize access of piddle arrays */
14684 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
14685 0           double tsum = temp + y;
14686 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
14687 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
14688             }
14689             ;
14690              
14691 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14692              
14693 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14694 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
14695             }
14696              
14697             else {
14698 0           d_mean = ( data - prev_mean ) / count;
14699 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14700 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
14701             }
14702             }
14703             }} /* Close n */
14704              
14705             {
14706 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
14707             }
14708             ;
14709              
14710             /* if there were no data in the bin, set some derived values to BAD_VAL */
14711            
14712             {
14713             /* if any are bad */
14714 0           int set_bad = 0;
14715 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
14716              
14717 0 0         if ( set_bad ) {
14718 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
14719              
14720 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
14721 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
14722              
14723 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
14724 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
14725              
14726 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
14727 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
14728             }
14729             }
14730            
14731              
14732 0 0         if ( have_weight ) {
14733             {
14734 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
14735             }
14736             ;
14737             {
14738 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
14739             }
14740             ;
14741             }
14742              
14743 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
14744             }
14745 0           } break;
14746 0           case PDL_ULL: {
14747 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_ULongLong,P,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
14748             {
14749              
14750              
14751              
14752              
14753 0           int flags = __params->optflags;
14754              
14755 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
14756 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
14757              
14758 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
14759              
14760 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
14761 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
14762 0           PDL_Indx imax = imin + nbins - 1;
14763              
14764             /* if first bin is reserved for oob, need to
14765             offset imin to ensure that non-oob data start
14766             at second bin */
14767 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
14768 0           imin -= 1;
14769              
14770             /* intialize output and temp bin data one at a time to
14771             avoid trashing the cache */
14772            
14773             /* initialize Kahan Summation for b_data */
14774 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
14775 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
14776             ;
14777 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
14778 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
14779 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
14780              
14781 0 0         if ( have_weight ) {
14782            
14783             /* initialize Kahan Summation for b_weight */
14784 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
14785 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
14786             ;
14787            
14788             /* initialize Kahan Summation for b_weight2 */
14789 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
14790 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
14791             ;
14792             }
14793              
14794             /* if we could preset min & max to the initial value in a bin,
14795             we could shave off a comparison. Unfortunately, we can't
14796             do that, as we can't know apriori which is the first
14797             element in a bin. */
14798 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
14799 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
14800              
14801 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
14802              
14803             PDL_Indx count;
14804 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
14805 0           double data = (data_datap)[0+(__inc_data_n*(n))];
14806             double weight;
14807              
14808 0           PDL_Indx oob_low =
14809 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
14810 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
14811 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
14812 0 0         : -1; /* error */
14813              
14814 0           PDL_Indx oob_high =
14815 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
14816 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
14817 0 0         : -1; /* error */
14818              
14819              
14820             #ifdef PDL_BAD_CODE
14821             if ( PDL_ISBAD2(data,data_badval,P,data_badval_isnan)
14822             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
14823             ) {
14824             continue;
14825             }
14826             #endif /* PDL_BAD_CODE */
14827              
14828 0 0         if ( have_weight ) {
14829 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
14830              
14831             #ifdef PDL_BAD_CODE
14832             if ( PDL_ISBAD2(weight,weight_badval,P,weight_badval_isnan) )
14833             continue;
14834             #endif /* PDL_BAD_CODE */
14835             }
14836              
14837              
14838 0 0         if ( idx < imin ) {
14839 0 0         if ( save_oob ) idx = oob_low;
14840 0           else continue;
14841             }
14842 0 0         else if ( idx > imax ) {
14843 0 0         if ( save_oob ) idx = oob_high;
14844 0           else continue;
14845             }
14846             else {
14847 0           idx -= imin;
14848             }
14849              
14850 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
14851             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
14852              
14853 0 0         if ( have_weight ){
14854             {
14855             /* Kahan Summation increment for b_data */
14856 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14857             /* use temporaries to minimize access of piddle arrays */
14858 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14859 0           double tsum = temp + y;
14860 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14861 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14862             }
14863             ;
14864             }
14865             else {
14866             {
14867             /* Kahan Summation increment for b_data */
14868 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
14869             /* use temporaries to minimize access of piddle arrays */
14870 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
14871 0           double tsum = temp + y;
14872 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
14873 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
14874             }
14875             ;
14876             }
14877              
14878 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
14879 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
14880              
14881             {
14882 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
14883             double d_mean;
14884              
14885 0 0         if ( have_weight ) {
14886 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14887              
14888             {
14889             /* Kahan Summation increment for b_weight */
14890 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14891             /* use temporaries to minimize access of piddle arrays */
14892 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
14893 0           double tsum = temp + y;
14894 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
14895 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
14896             }
14897             ;
14898             {
14899             /* Kahan Summation increment for b_weight2 */
14900 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
14901             /* use temporaries to minimize access of piddle arrays */
14902 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
14903 0           double tsum = temp + y;
14904 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
14905 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
14906             }
14907             ;
14908              
14909 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
14910              
14911 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14912 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
14913             }
14914              
14915             else {
14916 0           d_mean = ( data - prev_mean ) / count;
14917 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
14918 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
14919             }
14920             }
14921             }} /* Close n */
14922              
14923             {
14924 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
14925             }
14926             ;
14927              
14928             /* if there were no data in the bin, set some derived values to BAD_VAL */
14929            
14930             {
14931             /* if any are bad */
14932 0           int set_bad = 0;
14933 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
14934              
14935 0 0         if ( set_bad ) {
14936 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
14937              
14938 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
14939 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
14940              
14941 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
14942 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
14943              
14944 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
14945 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
14946             }
14947             }
14948            
14949              
14950 0 0         if ( have_weight ) {
14951             {
14952 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
14953             }
14954             ;
14955             {
14956 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
14957             }
14958             ;
14959             }
14960              
14961 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
14962             }
14963 0           } break;
14964 0           case PDL_LL: {
14965 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_LongLong,Q,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
14966             {
14967              
14968              
14969              
14970              
14971 0           int flags = __params->optflags;
14972              
14973 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
14974 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
14975              
14976 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
14977              
14978 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
14979 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
14980 0           PDL_Indx imax = imin + nbins - 1;
14981              
14982             /* if first bin is reserved for oob, need to
14983             offset imin to ensure that non-oob data start
14984             at second bin */
14985 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
14986 0           imin -= 1;
14987              
14988             /* intialize output and temp bin data one at a time to
14989             avoid trashing the cache */
14990            
14991             /* initialize Kahan Summation for b_data */
14992 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
14993 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
14994             ;
14995 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
14996 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
14997 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
14998              
14999 0 0         if ( have_weight ) {
15000            
15001             /* initialize Kahan Summation for b_weight */
15002 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
15003 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
15004             ;
15005            
15006             /* initialize Kahan Summation for b_weight2 */
15007 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
15008 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
15009             ;
15010             }
15011              
15012             /* if we could preset min & max to the initial value in a bin,
15013             we could shave off a comparison. Unfortunately, we can't
15014             do that, as we can't know apriori which is the first
15015             element in a bin. */
15016 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
15017 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
15018              
15019 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
15020              
15021             PDL_Indx count;
15022 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
15023 0           double data = (data_datap)[0+(__inc_data_n*(n))];
15024             double weight;
15025              
15026 0           PDL_Indx oob_low =
15027 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
15028 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
15029 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
15030 0 0         : -1; /* error */
15031              
15032 0           PDL_Indx oob_high =
15033 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
15034 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
15035 0 0         : -1; /* error */
15036              
15037              
15038             #ifdef PDL_BAD_CODE
15039             if ( PDL_ISBAD2(data,data_badval,Q,data_badval_isnan)
15040             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
15041             ) {
15042             continue;
15043             }
15044             #endif /* PDL_BAD_CODE */
15045              
15046 0 0         if ( have_weight ) {
15047 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
15048              
15049             #ifdef PDL_BAD_CODE
15050             if ( PDL_ISBAD2(weight,weight_badval,Q,weight_badval_isnan) )
15051             continue;
15052             #endif /* PDL_BAD_CODE */
15053             }
15054              
15055              
15056 0 0         if ( idx < imin ) {
15057 0 0         if ( save_oob ) idx = oob_low;
15058 0           else continue;
15059             }
15060 0 0         else if ( idx > imax ) {
15061 0 0         if ( save_oob ) idx = oob_high;
15062 0           else continue;
15063             }
15064             else {
15065 0           idx -= imin;
15066             }
15067              
15068 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
15069             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
15070              
15071 0 0         if ( have_weight ){
15072             {
15073             /* Kahan Summation increment for b_data */
15074 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15075             /* use temporaries to minimize access of piddle arrays */
15076 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15077 0           double tsum = temp + y;
15078 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15079 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15080             }
15081             ;
15082             }
15083             else {
15084             {
15085             /* Kahan Summation increment for b_data */
15086 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15087             /* use temporaries to minimize access of piddle arrays */
15088 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15089 0           double tsum = temp + y;
15090 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15091 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15092             }
15093             ;
15094             }
15095              
15096 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
15097 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
15098              
15099             {
15100 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
15101             double d_mean;
15102              
15103 0 0         if ( have_weight ) {
15104 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15105              
15106             {
15107             /* Kahan Summation increment for b_weight */
15108 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15109             /* use temporaries to minimize access of piddle arrays */
15110 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
15111 0           double tsum = temp + y;
15112 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
15113 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
15114             }
15115             ;
15116             {
15117             /* Kahan Summation increment for b_weight2 */
15118 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
15119             /* use temporaries to minimize access of piddle arrays */
15120 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
15121 0           double tsum = temp + y;
15122 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
15123 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
15124             }
15125             ;
15126              
15127 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15128              
15129 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15130 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
15131             }
15132              
15133             else {
15134 0           d_mean = ( data - prev_mean ) / count;
15135 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15136 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
15137             }
15138             }
15139             }} /* Close n */
15140              
15141             {
15142 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
15143             }
15144             ;
15145              
15146             /* if there were no data in the bin, set some derived values to BAD_VAL */
15147            
15148             {
15149             /* if any are bad */
15150 0           int set_bad = 0;
15151 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
15152              
15153 0 0         if ( set_bad ) {
15154 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
15155              
15156 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
15157 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
15158              
15159 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
15160 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
15161              
15162 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
15163 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
15164             }
15165             }
15166            
15167              
15168 0 0         if ( have_weight ) {
15169             {
15170 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
15171             }
15172             ;
15173             {
15174 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
15175             }
15176             ;
15177             }
15178              
15179 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
15180             }
15181 0           } break;
15182 0           case PDL_F: {
15183 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Float,F,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
15184             {
15185              
15186              
15187              
15188              
15189 0           int flags = __params->optflags;
15190              
15191 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
15192 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
15193              
15194 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
15195              
15196 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
15197 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
15198 0           PDL_Indx imax = imin + nbins - 1;
15199              
15200             /* if first bin is reserved for oob, need to
15201             offset imin to ensure that non-oob data start
15202             at second bin */
15203 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
15204 0           imin -= 1;
15205              
15206             /* intialize output and temp bin data one at a time to
15207             avoid trashing the cache */
15208            
15209             /* initialize Kahan Summation for b_data */
15210 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
15211 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
15212             ;
15213 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
15214 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
15215 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
15216              
15217 0 0         if ( have_weight ) {
15218            
15219             /* initialize Kahan Summation for b_weight */
15220 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
15221 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
15222             ;
15223            
15224             /* initialize Kahan Summation for b_weight2 */
15225 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
15226 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
15227             ;
15228             }
15229              
15230             /* if we could preset min & max to the initial value in a bin,
15231             we could shave off a comparison. Unfortunately, we can't
15232             do that, as we can't know apriori which is the first
15233             element in a bin. */
15234 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
15235 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
15236              
15237 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
15238              
15239             PDL_Indx count;
15240 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
15241 0           double data = (data_datap)[0+(__inc_data_n*(n))];
15242             double weight;
15243              
15244 0           PDL_Indx oob_low =
15245 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
15246 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
15247 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
15248 0 0         : -1; /* error */
15249              
15250 0           PDL_Indx oob_high =
15251 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
15252 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
15253 0 0         : -1; /* error */
15254              
15255              
15256             #ifdef PDL_BAD_CODE
15257             if ( PDL_ISBAD2(data,data_badval,F,data_badval_isnan)
15258             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
15259             ) {
15260             continue;
15261             }
15262             #endif /* PDL_BAD_CODE */
15263              
15264 0 0         if ( have_weight ) {
15265 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
15266              
15267             #ifdef PDL_BAD_CODE
15268             if ( PDL_ISBAD2(weight,weight_badval,F,weight_badval_isnan) )
15269             continue;
15270             #endif /* PDL_BAD_CODE */
15271             }
15272              
15273              
15274 0 0         if ( idx < imin ) {
15275 0 0         if ( save_oob ) idx = oob_low;
15276 0           else continue;
15277             }
15278 0 0         else if ( idx > imax ) {
15279 0 0         if ( save_oob ) idx = oob_high;
15280 0           else continue;
15281             }
15282             else {
15283 0           idx -= imin;
15284             }
15285              
15286 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
15287             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
15288              
15289 0 0         if ( have_weight ){
15290             {
15291             /* Kahan Summation increment for b_data */
15292 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15293             /* use temporaries to minimize access of piddle arrays */
15294 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15295 0           double tsum = temp + y;
15296 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15297 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15298             }
15299             ;
15300             }
15301             else {
15302             {
15303             /* Kahan Summation increment for b_data */
15304 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15305             /* use temporaries to minimize access of piddle arrays */
15306 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15307 0           double tsum = temp + y;
15308 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15309 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15310             }
15311             ;
15312             }
15313              
15314 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
15315 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
15316              
15317             {
15318 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
15319             double d_mean;
15320              
15321 0 0         if ( have_weight ) {
15322 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15323              
15324             {
15325             /* Kahan Summation increment for b_weight */
15326 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15327             /* use temporaries to minimize access of piddle arrays */
15328 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
15329 0           double tsum = temp + y;
15330 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
15331 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
15332             }
15333             ;
15334             {
15335             /* Kahan Summation increment for b_weight2 */
15336 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
15337             /* use temporaries to minimize access of piddle arrays */
15338 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
15339 0           double tsum = temp + y;
15340 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
15341 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
15342             }
15343             ;
15344              
15345 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15346              
15347 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15348 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
15349             }
15350              
15351             else {
15352 0           d_mean = ( data - prev_mean ) / count;
15353 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15354 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
15355             }
15356             }
15357             }} /* Close n */
15358              
15359             {
15360 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
15361             }
15362             ;
15363              
15364             /* if there were no data in the bin, set some derived values to BAD_VAL */
15365            
15366             {
15367             /* if any are bad */
15368 0           int set_bad = 0;
15369 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
15370              
15371 0 0         if ( set_bad ) {
15372 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
15373              
15374 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
15375 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
15376              
15377 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
15378 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
15379              
15380 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
15381 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
15382             }
15383             }
15384            
15385              
15386 0 0         if ( have_weight ) {
15387             {
15388 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
15389             }
15390             ;
15391             {
15392 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
15393             }
15394             ;
15395             }
15396              
15397 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
15398             }
15399 0           } break;
15400 50           case PDL_D: {
15401 50 50         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_Double,D,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
15402             {
15403              
15404              
15405              
15406              
15407 50           int flags = __params->optflags;
15408              
15409 50           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
15410 50           int save_oob = flags & BIN_ARG_SAVE_OOB;
15411              
15412 550 50         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    50          
    50          
    50          
    50          
    100          
    100          
15413              
15414 300           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
15415 300           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
15416 300           PDL_Indx imax = imin + nbins - 1;
15417              
15418             /* if first bin is reserved for oob, need to
15419             offset imin to ensure that non-oob data start
15420             at second bin */
15421 300 100         if ( save_oob & BIN_ARG_SHIFT_IMIN )
15422 48           imin -= 1;
15423              
15424             /* intialize output and temp bin data one at a time to
15425             avoid trashing the cache */
15426            
15427             /* initialize Kahan Summation for b_data */
15428 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
15429 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
15430             ;
15431 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
15432 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
15433 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
15434              
15435 300 50         if ( have_weight ) {
15436            
15437             /* initialize Kahan Summation for b_weight */
15438 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
15439 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
15440             ;
15441            
15442             /* initialize Kahan Summation for b_weight2 */
15443 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
15444 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
15445             ;
15446             }
15447              
15448             /* if we could preset min & max to the initial value in a bin,
15449             we could shave off a comparison. Unfortunately, we can't
15450             do that, as we can't know apriori which is the first
15451             element in a bin. */
15452 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
15453 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
15454              
15455 300300 100         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
15456              
15457             PDL_Indx count;
15458 300000           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
15459 300000           double data = (data_datap)[0+(__inc_data_n*(n))];
15460             double weight;
15461              
15462 300000           PDL_Indx oob_low =
15463 300000           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
15464 552000 100         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
15465 480000 100         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
15466 228000 100         : -1; /* error */
15467              
15468 300000           PDL_Indx oob_high =
15469 348000           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
15470 552000 100         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
15471 252000 100         : -1; /* error */
15472              
15473              
15474             #ifdef PDL_BAD_CODE
15475             if ( PDL_ISBAD2(data,data_badval,D,data_badval_isnan)
15476             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
15477             ) {
15478             continue;
15479             }
15480             #endif /* PDL_BAD_CODE */
15481              
15482 300000 50         if ( have_weight ) {
15483 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
15484              
15485             #ifdef PDL_BAD_CODE
15486             if ( PDL_ISBAD2(weight,weight_badval,D,weight_badval_isnan) )
15487             continue;
15488             #endif /* PDL_BAD_CODE */
15489             }
15490              
15491              
15492 300000 100         if ( idx < imin ) {
15493 5540 50         if ( save_oob ) idx = oob_low;
15494 0           else continue;
15495             }
15496 294460 100         else if ( idx > imax ) {
15497 11214 50         if ( save_oob ) idx = oob_high;
15498 0           else continue;
15499             }
15500             else {
15501 283246           idx -= imin;
15502             }
15503              
15504 300000           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
15505             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
15506              
15507 300000 50         if ( have_weight ){
15508             {
15509             /* Kahan Summation increment for b_data */
15510 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15511             /* use temporaries to minimize access of piddle arrays */
15512 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15513 0           double tsum = temp + y;
15514 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15515 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15516             }
15517             ;
15518             }
15519             else {
15520             {
15521             /* Kahan Summation increment for b_data */
15522 300000           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15523             /* use temporaries to minimize access of piddle arrays */
15524 300000           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15525 300000           double tsum = temp + y;
15526 300000           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15527 300000           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15528             }
15529             ;
15530             }
15531              
15532 300000 100         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
15533 300000 100         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
15534              
15535             {
15536 300000           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
15537             double d_mean;
15538              
15539 300000 50         if ( have_weight ) {
15540 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15541              
15542             {
15543             /* Kahan Summation increment for b_weight */
15544 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15545             /* use temporaries to minimize access of piddle arrays */
15546 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
15547 0           double tsum = temp + y;
15548 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
15549 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
15550             }
15551             ;
15552             {
15553             /* Kahan Summation increment for b_weight2 */
15554 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
15555             /* use temporaries to minimize access of piddle arrays */
15556 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
15557 0           double tsum = temp + y;
15558 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
15559 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
15560             }
15561             ;
15562              
15563 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15564              
15565 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15566 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
15567             }
15568              
15569             else {
15570 300000           d_mean = ( data - prev_mean ) / count;
15571 300000           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15572 300000           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
15573             }
15574             }
15575             }} /* Close n */
15576              
15577             {
15578 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
15579             }
15580             ;
15581              
15582             /* if there were no data in the bin, set some derived values to BAD_VAL */
15583            
15584             {
15585             /* if any are bad */
15586 300           int set_bad = 0;
15587 8496 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    100          
15588              
15589 300 100         if ( set_bad ) {
15590 68           __privtrans->pdls[10]->state |= PDL_BADVAL;
15591              
15592 2844 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    100          
15593 68           __privtrans->pdls[11]->state |= PDL_BADVAL;
15594              
15595 2844 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    100          
15596 68           __privtrans->pdls[9]->state |= PDL_BADVAL;
15597              
15598 2844 100         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    100          
15599 68           __privtrans->pdls[12]->state |= PDL_BADVAL;
15600             }
15601             }
15602            
15603              
15604 300 50         if ( have_weight ) {
15605             {
15606 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
15607             }
15608             ;
15609             {
15610 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
15611             }
15612             ;
15613             }
15614              
15615 50 50         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    50          
15616             }
15617 50           } break;
15618 0           case PDL_LD: {
15619 0 0         PDL_DECLARE_PARAMS_bin_on_index_1(PDL_LDouble,E,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Indx,N,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D,PDL_Double,D)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
15620             {
15621              
15622              
15623              
15624              
15625 0           int flags = __params->optflags;
15626              
15627 0           int have_weight = flags & BIN_ARG_HAVE_WEIGHT;
15628 0           int save_oob = flags & BIN_ARG_SAVE_OOB;
15629              
15630 0 0         PDL_BROADCASTLOOP_START_bin_on_index_readdata
    0          
    0          
    0          
    0          
    0          
    0          
15631              
15632 0           PDL_Indx nbins = (nbins_datap)[0+(__inc_nbins_nt*(0))];
15633 0           PDL_Indx imin = (imin_datap)[0+(__inc_imin_nt*(0))];
15634 0           PDL_Indx imax = imin + nbins - 1;
15635              
15636             /* if first bin is reserved for oob, need to
15637             offset imin to ensure that non-oob data start
15638             at second bin */
15639 0 0         if ( save_oob & BIN_ARG_SHIFT_IMIN )
15640 0           imin -= 1;
15641              
15642             /* intialize output and temp bin data one at a time to
15643             avoid trashing the cache */
15644            
15645             /* initialize Kahan Summation for b_data */
15646 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] = 0.0; }} /* Close nb */
15647 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))] = 0.0; }} /* Close nb */
15648             ;
15649 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_count_datap)[0+(__inc_b_count_nb*(nb))] = 0; }} /* Close nb */
15650 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))] = 0; }} /* Close nb */
15651 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))] = 0; }} /* Close nb */
15652              
15653 0 0         if ( have_weight ) {
15654            
15655             /* initialize Kahan Summation for b_weight */
15656 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] = 0.0; }} /* Close nb */
15657 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))] = 0.0; }} /* Close nb */
15658             ;
15659            
15660             /* initialize Kahan Summation for b_weight2 */
15661 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] = 0.0; }} /* Close nb */
15662 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))] = 0.0; }} /* Close nb */
15663             ;
15664             }
15665              
15666             /* if we could preset min & max to the initial value in a bin,
15667             we could shave off a comparison. Unfortunately, we can't
15668             do that, as we can't know apriori which is the first
15669             element in a bin. */
15670 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))] = DBL_MAX; }} /* Close nb */
15671 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))] = -DBL_MAX; }} /* Close nb */
15672              
15673 0 0         {/* Open n */ PDL_EXPAND2(register PDL_Indx n=0, __n_stop=(__n_size)); for(; n<__n_stop; n+=1) {
15674              
15675             PDL_Indx count;
15676 0           PDL_Indx idx = (index_datap)[0+(__inc_index_n*(n))];
15677 0           double data = (data_datap)[0+(__inc_data_n*(n))];
15678             double weight;
15679              
15680 0           PDL_Indx oob_low =
15681 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_START_NBINS ) ? 0
15682 0 0         : save_oob & (BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 2
15683 0 0         : save_oob & (BIN_ARG_SAVE_OOB_NBINS ) ? nbins
15684 0 0         : -1; /* error */
15685              
15686 0           PDL_Indx oob_high =
15687 0           save_oob & (BIN_ARG_SAVE_OOB_START_END | BIN_ARG_SAVE_OOB_END ) ? __privtrans->ind_sizes[1] - 1
15688 0 0         : save_oob & (BIN_ARG_SAVE_OOB_START_NBINS | BIN_ARG_SAVE_OOB_NBINS ) ? nbins + 1
15689 0 0         : -1; /* error */
15690              
15691              
15692             #ifdef PDL_BAD_CODE
15693             if ( PDL_ISBAD2(data,data_badval,E,data_badval_isnan)
15694             || PDL_ISBAD2(idx,index_badval,N,index_badval_isnan)
15695             ) {
15696             continue;
15697             }
15698             #endif /* PDL_BAD_CODE */
15699              
15700 0 0         if ( have_weight ) {
15701 0           weight = (weight_datap)[0+(__inc_weight_n*(n))];
15702              
15703             #ifdef PDL_BAD_CODE
15704             if ( PDL_ISBAD2(weight,weight_badval,E,weight_badval_isnan) )
15705             continue;
15706             #endif /* PDL_BAD_CODE */
15707             }
15708              
15709              
15710 0 0         if ( idx < imin ) {
15711 0 0         if ( save_oob ) idx = oob_low;
15712 0           else continue;
15713             }
15714 0 0         else if ( idx > imax ) {
15715 0 0         if ( save_oob ) idx = oob_high;
15716 0           else continue;
15717             }
15718             else {
15719 0           idx -= imin;
15720             }
15721              
15722 0           count = ++(b_count_datap)[0+(__inc_b_count_nb*(idx))];
15723             /* (b_data_datap)[0+(__inc_b_data_nb*(idx))] += data; */
15724              
15725 0 0         if ( have_weight ){
15726             {
15727             /* Kahan Summation increment for b_data */
15728 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15729             /* use temporaries to minimize access of piddle arrays */
15730 0           double y = (data * weight) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15731 0           double tsum = temp + y;
15732 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15733 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15734             }
15735             ;
15736             }
15737             else {
15738             {
15739             /* Kahan Summation increment for b_data */
15740 0           double temp = (b_data_datap)[0+(__inc_b_data_nb*(idx))];
15741             /* use temporaries to minimize access of piddle arrays */
15742 0           double y = (data) + (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))];
15743 0           double tsum = temp + y;
15744 0           (b_data_datap)[0+(__inc_b_data_nb*(idx))] = tsum;
15745 0           (b_data_error_datap)[0+(__inc_b_data_error_nb*(idx))] = ( temp - tsum ) + y;
15746             }
15747             ;
15748             }
15749              
15750 0 0         if ( data < (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] ) (b_dmin_datap)[0+(__inc_b_dmin_nb*(idx))] = data;
15751 0 0         if ( data > (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] ) (b_dmax_datap)[0+(__inc_b_dmax_nb*(idx))] = data;
15752              
15753             {
15754 0           double prev_mean = (b_mean_datap)[0+(__inc_b_mean_nb*(idx))];
15755             double d_mean;
15756              
15757 0 0         if ( have_weight ) {
15758 0           double prev_sum_weight = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15759              
15760             {
15761             /* Kahan Summation increment for b_weight */
15762 0           double temp = (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15763             /* use temporaries to minimize access of piddle arrays */
15764 0           double y = (weight) + (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))];
15765 0           double tsum = temp + y;
15766 0           (b_weight_datap)[0+(__inc_b_weight_nb*(idx))] = tsum;
15767 0           (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(idx))] = ( temp - tsum ) + y;
15768             }
15769             ;
15770             {
15771             /* Kahan Summation increment for b_weight2 */
15772 0           double temp = (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))];
15773             /* use temporaries to minimize access of piddle arrays */
15774 0           double y = (weight * weight) + (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))];
15775 0           double tsum = temp + y;
15776 0           (b_weight2_datap)[0+(__inc_b_weight2_nb*(idx))] = tsum;
15777 0           (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(idx))] = ( temp - tsum ) + y;
15778             }
15779             ;
15780              
15781 0           d_mean = weight * ( data - prev_mean ) / (b_weight_datap)[0+(__inc_b_weight_nb*(idx))];
15782              
15783 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15784 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += d_mean * ( data - prev_mean ) * prev_sum_weight;
15785             }
15786              
15787             else {
15788 0           d_mean = ( data - prev_mean ) / count;
15789 0           (b_mean_datap)[0+(__inc_b_mean_nb*(idx))] += d_mean;
15790 0           (b_dev2_datap)[0+(__inc_b_dev2_nb*(idx))] += ( (count - 1) * ( data - prev_mean ) ) * d_mean;
15791             }
15792             }
15793             }} /* Close n */
15794              
15795             {
15796 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_data_datap)[0+(__inc_b_data_nb*(nb))] += (b_data_error_datap)[0+(__inc_b_data_error_nb*(nb))]; }} /* Close nb */
15797             }
15798             ;
15799              
15800             /* if there were no data in the bin, set some derived values to BAD_VAL */
15801            
15802             {
15803             /* if any are bad */
15804 0           int set_bad = 0;
15805 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmin_datap)[0+(__inc_b_dmin_nb*(nb))]=b_dmin_badval; set_bad = 1;} }} /* Close nb */
    0          
15806              
15807 0 0         if ( set_bad ) {
15808 0           __privtrans->pdls[10]->state |= PDL_BADVAL;
15809              
15810 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dmax_datap)[0+(__inc_b_dmax_nb*(nb))]=b_dmax_badval; } }} /* Close nb */
    0          
15811 0           __privtrans->pdls[11]->state |= PDL_BADVAL;
15812              
15813 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_mean_datap)[0+(__inc_b_mean_nb*(nb))]=b_mean_badval; } }} /* Close nb */
    0          
15814 0           __privtrans->pdls[9]->state |= PDL_BADVAL;
15815              
15816 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { if ( (b_count_datap)[0+(__inc_b_count_nb*(nb))] == 0 ) { (b_dev2_datap)[0+(__inc_b_dev2_nb*(nb))]=b_dev2_badval; } }} /* Close nb */
    0          
15817 0           __privtrans->pdls[12]->state |= PDL_BADVAL;
15818             }
15819             }
15820            
15821              
15822 0 0         if ( have_weight ) {
15823             {
15824 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight_datap)[0+(__inc_b_weight_nb*(nb))] += (b_weight_error_datap)[0+(__inc_b_weight_error_nb*(nb))]; }} /* Close nb */
15825             }
15826             ;
15827             {
15828 0 0         {/* Open nb */ PDL_EXPAND2(register PDL_Indx nb=0, __nb_stop=(__nb_size)); for(; nb<__nb_stop; nb+=1) { (b_weight2_datap)[0+(__inc_b_weight2_nb*(nb))] += (b_weight2_error_datap)[0+(__inc_b_weight2_error_nb*(nb))]; }} /* Close nb */
15829             }
15830             ;
15831             }
15832              
15833 0 0         PDL_BROADCASTLOOP_END_bin_on_index_readdata
    0          
15834             }
15835 0           } break;
15836 0           default: return PDL->make_error(PDL_EUSERERROR, "PP INTERNAL ERROR in bin_on_index: unhandled datatype(%d), only handles (ABSULKNPQFDE)! PLEASE MAKE A BUG REPORT\n", __privtrans->__datatype);
15837             }
15838             #undef PDL_IF_BAD
15839             }
15840 50           return PDL_err;
15841             }
15842              
15843             static pdl_datatypes pdl_bin_on_index_vtable_gentypes[] = { PDL_SB, PDL_B, PDL_S, PDL_US, PDL_L, PDL_UL, PDL_IND, PDL_ULL, PDL_LL, PDL_F, PDL_D, PDL_LD, -1 };
15844             static PDL_Indx pdl_bin_on_index_vtable_realdims[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
15845             static char *pdl_bin_on_index_vtable_parnames[] = { "data","index","weight","imin","nbins","b_count","b_data","b_weight","b_weight2","b_mean","b_dmin","b_dmax","b_dev2","b_data_error","b_weight_error","b_weight2_error" };
15846             static short pdl_bin_on_index_vtable_parflags[] = {
15847             0,
15848             PDL_PARAM_ISTYPED,
15849             0,
15850             PDL_PARAM_ISTYPED,
15851             PDL_PARAM_ISTYPED,
15852             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15853             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15854             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15855             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15856             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15857             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15858             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15859             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15860             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15861             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE,
15862             PDL_PARAM_ISCREAT|PDL_PARAM_ISTEMP|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE
15863             };
15864             static pdl_datatypes pdl_bin_on_index_vtable_partypes[] = { -1, PDL_IND, -1, PDL_IND, PDL_IND, PDL_IND, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D, PDL_D };
15865             static PDL_Indx pdl_bin_on_index_vtable_realdims_starts[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
15866             static PDL_Indx pdl_bin_on_index_vtable_realdims_ind_ids[] = { 0, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
15867             static char *pdl_bin_on_index_vtable_indnames[] = { "n","nb","nt" };
15868             pdl_transvtable pdl_bin_on_index_vtable = {
15869             PDL_TRANS_DO_BROADCAST|PDL_TRANS_BADPROCESS, 0, pdl_bin_on_index_vtable_gentypes, 5, 16, NULL /*CORE21*/,
15870             pdl_bin_on_index_vtable_realdims, pdl_bin_on_index_vtable_parnames,
15871             pdl_bin_on_index_vtable_parflags, pdl_bin_on_index_vtable_partypes,
15872             pdl_bin_on_index_vtable_realdims_starts, pdl_bin_on_index_vtable_realdims_ind_ids, 16,
15873             3, pdl_bin_on_index_vtable_indnames,
15874             pdl_bin_on_index_redodims, pdl_bin_on_index_readdata, NULL,
15875             NULL,
15876             sizeof(pdl_params_bin_on_index),"CXC::PDL::Bin1D::bin_on_index"
15877             };
15878              
15879              
15880 50           pdl_error pdl_run_bin_on_index(pdl *data,pdl *index,pdl *weight,pdl *imin,pdl *nbins,pdl *b_count,pdl *b_data,pdl *b_weight,pdl *b_weight2,pdl *b_mean,pdl *b_dmin,pdl *b_dmax,pdl *b_dev2,unsigned long optflags,PDL_Indx nbins_max) {
15881 50           pdl_error PDL_err = {0, NULL, 0};
15882 50 50         if (!PDL) return (pdl_error){PDL_EFATAL, "PDL core struct is NULL, can't continue",0};
15883 50           pdl_trans *__privtrans = PDL->create_trans(&pdl_bin_on_index_vtable);
15884 50 50         if (!__privtrans) return PDL->make_error_simple(PDL_EFATAL, "Couldn't create trans");
15885 50           pdl_params_bin_on_index *__params = __privtrans->params;
15886 50           __privtrans->pdls[0] = data;
15887 50           __privtrans->pdls[1] = index;
15888 50           __privtrans->pdls[2] = weight;
15889 50           __privtrans->pdls[3] = imin;
15890 50           __privtrans->pdls[4] = nbins;
15891 50           __privtrans->pdls[5] = b_count;
15892 50           __privtrans->pdls[6] = b_data;
15893 50           __privtrans->pdls[7] = b_weight;
15894 50           __privtrans->pdls[8] = b_weight2;
15895 50           __privtrans->pdls[9] = b_mean;
15896 50           __privtrans->pdls[10] = b_dmin;
15897 50           __privtrans->pdls[11] = b_dmax;
15898 50           __privtrans->pdls[12] = b_dev2;
15899 50 50         PDL_RETERROR(PDL_err, PDL->type_coerce(__privtrans));
15900 50           (__params->optflags) = (optflags); /* CType.get_copy */
15901 50           (__params->nbins_max) = (nbins_max); /* CType.get_copy */
15902 50 50         PDL_RETERROR(PDL_err, PDL->make_trans_mutual(__privtrans));
15903 50           return PDL_err;
15904             }
15905              
15906             #line 449 "lib/PDL/PP.pm"
15907             #include "pdlperl.h"
15908             #line 15909 "Bin1D.xs"
15909              
15910             Core* PDL = NULL; /* Structure hold core C functions */
15911              
15912             MODULE = CXC::PDL::Bin1D PACKAGE = CXC::PDL::Bin1D PREFIX=pdl_run_
15913              
15914             PROTOTYPES: DISABLE
15915              
15916              
15917             void
15918             _bin_adaptive_snr_int(signal,error,width,index,nbins,nelem,b_signal,b_error,b_mean,b_snr,b_width,ifirst,ilast,rc,optflags,min_snr,min_nelem,max_nelem,min_width,max_width)
15919             pdl *signal
15920             pdl *error
15921             pdl *width
15922             pdl *index
15923             pdl *nbins
15924             pdl *nelem
15925             pdl *b_signal
15926             pdl *b_error
15927             pdl *b_mean
15928             pdl *b_snr
15929             pdl *b_width
15930             pdl *ifirst
15931             pdl *ilast
15932             pdl *rc
15933             unsigned long optflags
15934             double min_snr
15935             PDL_Indx min_nelem
15936             PDL_Indx max_nelem
15937             double min_width
15938             double max_width
15939             CODE:
15940 35           PDL->barf_if_error(pdl_run_bin_adaptive_snr(signal,error,width,index,nbins,nelem,b_signal,b_error,b_mean,b_snr,b_width,ifirst,ilast,rc,optflags,min_snr,min_nelem,max_nelem,min_width,max_width));
15941              
15942             void
15943             _bin_on_index_int(data,index,weight,imin,nbins,b_count,b_data,b_weight,b_weight2,b_mean,b_dmin,b_dmax,b_dev2,optflags,nbins_max)
15944             pdl *data
15945             pdl *index
15946             pdl *weight
15947             pdl *imin
15948             pdl *nbins
15949             pdl *b_count
15950             pdl *b_data
15951             pdl *b_weight
15952             pdl *b_weight2
15953             pdl *b_mean
15954             pdl *b_dmin
15955             pdl *b_dmax
15956             pdl *b_dev2
15957             unsigned long optflags
15958             PDL_Indx nbins_max
15959             CODE:
15960 50           PDL->barf_if_error(pdl_run_bin_on_index(data,index,weight,imin,nbins,b_count,b_data,b_weight,b_weight2,b_mean,b_dmin,b_dmax,b_dev2,optflags,nbins_max));
15961              
15962              
15963             #line 483 "lib/PDL/PP.pm"
15964             BOOT:
15965             /* Get pointer to structure of core shared C routines */
15966             /* make sure PDL::Core is loaded */
15967             #line 15968 "Bin1D.xs"
15968 4           perl_require_pv ("PDL/Core.pm"); /* make sure PDL::Core is loaded */
15969             #ifndef aTHX_
15970             #define aTHX_
15971             #endif
15972 4 50         if (SvTRUE (ERRSV)) Perl_croak(aTHX_ "%s",SvPV_nolen (ERRSV));
    50          
    0          
15973 4           SV* CoreSV = perl_get_sv("PDL::SHARE",FALSE); /* var with core structure */
15974 4 50         if (!CoreSV)
15975 0           Perl_croak(aTHX_ "We require the PDL::Core module, which was not found");
15976 4 50         if (!(PDL = INT2PTR(Core*,SvIV( CoreSV )))) /* Core* value */
15977 0           Perl_croak(aTHX_ "Got NULL pointer for PDL");
15978 4 50         if (PDL->Version != PDL_CORE_VERSION)
15979 0           Perl_croak(aTHX_ "[PDL->Version: %ld PDL_CORE_VERSION: %ld XS_VERSION: %s] CXC::PDL::Bin1D needs to be recompiled against the newly installed PDL", (long int)PDL->Version, (long int)PDL_CORE_VERSION, XS_VERSION);