107 const Index_* group_size,
108 Output_* output_means,
109 Output_* output_variances,
111 Index_* valid_group_size)
113 std::fill_n(output_means, num_groups, 0);
114 std::fill_n(output_variances, num_groups, 0);
116 ::tatami_stats::internal::nanable_ifelse<Value_>(
119 std::fill_n(valid_group_size, num_groups, 0);
121 for (Index_ j = 0; j < num; ++j) {
123 if (!std::isnan(x)) {
125 output_means[b] += x;
126 ++valid_group_size[b];
129 internal::finish_means(num_groups, valid_group_size, output_means);
131 for (Index_ j = 0; j < num; ++j) {
133 if (!std::isnan(x)) {
135 auto delta = x - output_means[b];
136 output_variances[b] += delta * delta;
139 internal::finish_variances(num_groups, valid_group_size, output_variances);
142 for (Index_ j = 0; j < num; ++j) {
143 output_means[group[j]] += ptr[j];
145 internal::finish_means(num_groups, group_size, output_means);
147 for (Index_ j = 0; j < num; ++j) {
149 auto delta = ptr[j] - output_means[b];
150 output_variances[b] += delta * delta;
152 internal::finish_variances(num_groups, group_size, output_variances);
196 const Index_* group_size,
197 Output_* output_means,
198 Output_* output_variances,
199 Index_* output_nonzero,
201 Index_* valid_group_size)
203 std::fill_n(output_means, num_groups, 0);
204 std::fill_n(output_nonzero, num_groups, 0);
205 std::fill_n(output_variances, num_groups, 0);
207 ::tatami_stats::internal::nanable_ifelse<Value_>(
210 std::copy_n(group_size, num_groups, valid_group_size);
212 for (Index_ j = 0; j < num_nonzero; ++j) {
214 auto b = group[index[j]];
215 if (!std::isnan(x)) {
216 output_means[b] += x;
217 ++(output_nonzero[b]);
219 --(valid_group_size[b]);
222 internal::finish_means(num_groups, valid_group_size, output_means);
224 for (Index_ j = 0; j < num_nonzero; ++j) {
226 if (!std::isnan(x)) {
227 auto b = group[index[j]];
228 auto delta = x - output_means[b];
229 output_variances[b] += delta * delta;
232 for (
size_t b = 0; b < num_groups; ++b) {
233 output_variances[b] += output_means[b] * output_means[b] * (valid_group_size[b] - output_nonzero[b]);
235 internal::finish_variances(num_groups, valid_group_size, output_variances);
238 for (Index_ j = 0; j < num_nonzero; ++j) {
239 auto b = group[index[j]];
240 output_means[b] += value[j];
243 internal::finish_means(num_groups, group_size, output_means);
245 for (Index_ j = 0; j < num_nonzero; ++j) {
246 auto b = group[index[j]];
247 auto delta = value[j] - output_means[b];
248 output_variances[b] += delta * delta;
250 for (
size_t b = 0; b < num_groups; ++b) {
251 output_variances[b] += output_means[b] * output_means[b] * (group_size[b] - output_nonzero[b]);
253 internal::finish_variances(num_groups, group_size, output_variances);
282 Index_ dim = (row ? p->
nrow() : p->
ncol());
283 Index_ otherdim = (row ? p->
ncol() : p->
nrow());
289 std::vector<Value_> xbuffer(otherdim);
290 std::vector<Index_> ibuffer(otherdim);
292 std::vector<Output_> tmp_means(num_groups);
293 std::vector<Output_> output_variances(num_groups);
294 std::vector<Index_> tmp_nonzero(num_groups);
295 std::vector<Index_> valid_group_size(sopt.
skip_nan ? num_groups : 0);
297 for (Index_ i = 0; i < len; ++i) {
298 auto range = ext->fetch(xbuffer.data(), ibuffer.data());
307 output_variances.data(),
310 valid_group_size.data()
313 for (
size_t g = 0; g < num_groups; ++g) {
314 output[g][i + start] = output_variances[g];
327 std::vector<variances::RunningSparse<Output_, Value_, Index_> > runners;
328 runners.reserve(num_groups);
329 std::vector<LocalOutputBuffer<Output_> > local_var_output;
330 local_var_output.reserve(num_groups);
331 std::vector<std::vector<Output_> > local_mean_output;
332 local_mean_output.reserve(num_groups);
334 for (
size_t g = 0; g < num_groups; ++g) {
335 local_var_output.emplace_back(thread, start, len, output[g]);
336 local_mean_output.emplace_back(len);
337 runners.emplace_back(len, local_mean_output.back().data(), local_var_output.back().data(), sopt.
skip_nan, start);
341 std::vector<Value_> xbuffer(len);
342 std::vector<Index_> ibuffer(len);
344 for (
int i = 0; i < otherdim; ++i) {
345 auto range = ext->fetch(xbuffer.data(), ibuffer.data());
346 runners[group[i]].add(range.value, range.index, range.number);
349 for (
size_t g = 0; g < num_groups; ++g) {
351 local_var_output[g].transfer();
360 std::vector<Value_> xbuffer(otherdim);
361 std::vector<Output_> tmp_means(num_groups);
362 std::vector<Output_> output_variances(num_groups);
363 std::vector<Index_> valid_group_size(sopt.
skip_nan ? num_groups : 0);
365 for (Index_ i = 0; i < len; ++i) {
366 auto ptr = ext->fetch(xbuffer.data());
374 output_variances.data(),
376 valid_group_size.data()
379 for (
size_t g = 0; g < num_groups; ++g) {
380 output[g][i + start] = output_variances[g];
387 std::vector<variances::RunningDense<Output_, Value_, Index_> > runners;
388 runners.reserve(num_groups);
389 std::vector<LocalOutputBuffer<Output_> > local_var_output;
390 local_var_output.reserve(num_groups);
391 std::vector<std::vector<Output_> > local_mean_output;
392 local_mean_output.reserve(num_groups);
394 for (
size_t g = 0; g < num_groups; ++g) {
395 local_var_output.emplace_back(thread, start, len, output[g]);
396 local_mean_output.emplace_back(len);
397 runners.emplace_back(len, local_mean_output.back().data(), local_var_output.back().data(), sopt.
skip_nan);
400 std::vector<Value_> xbuffer(len);
403 for (Index_ i = 0; i < otherdim; ++i) {
404 auto ptr = ext->fetch(xbuffer.data());
405 runners[group[i]].add(ptr);
408 for (
size_t g = 0; g < num_groups; ++g) {
410 local_var_output[g].transfer();