23 #include "valuevector.hpp"
26 #include "globals.hpp"
37 while (ringsz < window) ringsz <<= 1;
38 vector<T> ring(ringsz);
40 for (idx_type c=0; c<a.v.size(); c++) {
45 for (idx_type r=0; r<a.dim[0]; ++r) {
46 T outvalue = ring[r & (ringsz-1)];
47 if (!std::isnan((*a.v[c])[r])) {
49 ring[r & (ringsz - 1)] = (*a.v[c])[r];
52 if (r >= window && !std::isnan(outvalue)) {
57 setv(a.getcol(c), r, mean / window);
59 setv(a.getcol(c), r, Global::ZNAN);
76 auto minima =
new std::pair<T, idx_type>[a.dim[0]];
78 for (idx_type c=0; c<a.v.size(); c++) {
79 idx_type nbv = 0, front = 0, back = 0;
80 for (idx_type r=0; r<a.dim[0]; ++r) {
81 if (!std::isnan((*a.v[c])[r])) {
84 while (back > front && minima[back-1].first >= (*a.v[c])[r]) --back;
85 minima[back].first = (*a.v[c])[r];
86 minima[back].second = r;
90 if (r >= window && !std::isnan((*a.v[c])[r-window])) {
97 while (minima[front].second <= r-window) ++front;
99 setv(a.getcol(c), r, minima[front].first);
101 setv(a.getcol(c), r, Global::ZNAN);
119 auto maxima =
new std::pair<T, idx_type>[a.dim[0]];
121 for (idx_type c=0; c<a.v.size(); c++) {
122 idx_type nbv = 0, front = 0, back = 0;
123 for (idx_type r=0; r<a.dim[0]; ++r) {
124 if (!std::isnan((*a.v[c])[r])) {
127 while (back > front && maxima[back-1].first <= (*a.v[c])[r]) --back;
128 maxima[back].first = (*a.v[c])[r];
129 maxima[back].second = r;
133 if (r >= window && !std::isnan((*a.v[c])[r-window])) {
136 if (nbv >= nbvalid) {
140 while (maxima[front].second <= r-window) ++front;
142 setv(a.getcol(c), r, maxima[front].first);
144 setv(a.getcol(c), r, Global::ZNAN);
163 while (ringsz < window) ringsz <<= 1;
164 vector<T> ring(ringsz);
165 vector<T> ring2(ringsz);
168 for (idx_type c=0; c<a.v.size(); c++) {
172 for (idx_type r=0; r<a.dim[0]; ++r) {
173 T outvalue = ring[r & (ringsz-1)];
174 T outvalue2 = ring2[r & (ringsz-1)];
175 if (!std::isnan((*a.v[c])[r])) {
177 ring[r & (ringsz - 1)] = (*a.v[c])[r];
178 sum2 += (*a.v[c])[r] * (*a.v[c])[r];
179 ring2[r & (ringsz - 1)] = (*a.v[c])[r] * (*a.v[c])[r];
182 if (r >= window && !std::isnan(outvalue)) {
187 if (nbv >= nbvalid) {
188 setv(a.getcol(c), r, (sum2 - sum*sum / nbv) / (nbv - 1));
189 if ((*a.v[c])[r] < 0) {
190 setv(a.getcol(c), r, 0.0);
193 setv(a.getcol(c), r, Global::ZNAN);
203 for (idx_type c=0; c<a.v.size(); c++) {
205 for (idx_type r=1; r<a.dim[0]; ++r) {
206 if (std::isnan((*a.v[c])[r])) {
210 setv(a.getcol(c), r, (r - last_idx <=
static_cast<idx_type
>(n)) ? (*a.v[c])[r-1] : Global::ZNAN);
223 for (idx_type c=0; c<a.v.size(); c++) {
224 for (idx_type r=a.dim[0]-1; r >=
static_cast<idx_type
>(n); --r) {
225 setv(a.getcol(c), r, (*a.v[c])[r - n]);
227 for (idx_type r=0; r<static_cast<idx_type>(n); ++r) {
228 setv(a.getcol(c), r, Global::ZNAN);
233 for (idx_type c=0; c<a.v.size(); c++) {
234 for (idx_type r=0; r<a.dim[0] + n; ++r) {
235 setv(a.getcol(c), r, (*a.v[c])[r - n]);
237 for (idx_type r=a.dim[0]+n; r<a.dim[0]; ++r) {
238 setv(a.getcol(c), r, Global::ZNAN);
250 for (idx_type c=0; c<a.v.size(); c++) {
251 for (idx_type r=a.dim[0]-n; r<a.dim[0]; ++r) {
252 v[r-(a.dim[0]-n)] = (*a.v[c])[r];
254 for (idx_type r=a.dim[0]-1; r >=
static_cast<idx_type
>(n); --r) {
255 setv(a.getcol(c), r, (*a.v[c])[r - n]);
257 for (idx_type r=0; r<static_cast<idx_type>(n); ++r) {
258 setv(a.getcol(c), r, v[r]);
264 for (idx_type c=0; c<a.v.size(); c++) {
265 for (idx_type r=0; r<static_cast<idx_type>(-n); ++r) {
268 for (idx_type r=0; r<a.dim[0] + n; ++r) {
269 setv(a.getcol(c), r, (*a.v[c])[r - n]);
271 for (idx_type r=a.dim[0]+n; r<a.dim[0]; ++r) {
272 setv(a.getcol(c), r, v[r - (a.dim[0]+n)]);
283 for (idx_type c=0; c<a.v.size(); c++) {
284 for (idx_type r=a.dim[0]-1; r >=
static_cast<idx_type
>(n); --r) {
285 setv(a.getcol(c), r, (*a.v[c])[r] - (*a.v[c])[r - n]);
287 for (idx_type r=0; r<static_cast<idx_type>(n); ++r) {
288 setv(a.getcol(c), r, Global::ZNAN);
293 for (idx_type c=0; c<a.v.size(); c++) {
294 for (idx_type r=0; r<a.dim[0] + n; ++r) {
295 setv(a.getcol(c), r, (*a.v[c])[r] - (*a.v[c])[r - n]);
297 for (idx_type r=a.dim[0]+n; r<a.dim[0]; ++r) {
298 setv(a.getcol(c), r, Global::ZNAN);
325 while (ringsz < window) ringsz <<= 1;
326 vector<T> ring_x(ringsz);
327 vector<T> ring_y(ringsz);
328 vector<T> ring_xy(ringsz);
330 if (!(x.getdim().size() && y.getdim().size() && (x.getdim(0) == y.getdim(0)))) {
331 throw std::range_error(
"invalid dimensions");
338 const idx_type nrows = y.getdim(0);
340 for (idx_type colx=0; colx < x.ncols(); ++colx) {
341 for (idx_type coly=0; coly < y.ncols(); ++coly) {
343 T sum_x = 0, sum_y = 0, sum_xy = 0;
345 for (idx_type r=0; r<nrows; ++r) {
346 T outvalue_x = ring_x[r & (ringsz-1)];
347 T outvalue_y = ring_y[r & (ringsz-1)];
348 T outvalue_xy = ring_xy[r & (ringsz-1)];
349 if (!std::isnan(x.getcol(colx)[r]) && !std::isnan(y.getcol(coly)[r])) {
350 sum_x += x.getcol(colx)[r];
351 ring_x[r & (ringsz - 1)] = x.getcol(colx)[r];
352 sum_y += y.getcol(coly)[r];
353 ring_y[r & (ringsz - 1)] = y.getcol(coly)[r];
354 sum_xy += x.getcol(colx)[r] * y.getcol(coly)[r];
355 ring_xy[r & (ringsz - 1)] = x.getcol(colx)[r] * y.getcol(coly)[r];
358 if (r >= window && !std::isnan(outvalue_x) && !std::isnan(outvalue_y)) {
361 sum_xy -= outvalue_xy;
364 if (nbv >= nbvalid) {
365 z.getcol(zcol).push_back((sum_xy - sum_x*sum_y / nbv) / (nbv - 1));
367 z.getcol(zcol).push_back(Global::ZNAN);
374 z.names[0]->resize(nrows);
375 setv(z.dim, 0, nrows);
381 template<
typename T,
typename F>
384 for (idx_type c=0; c < a.ncols(); c++) {
385 for (idx_type r=1; r < a.getcol(c).size(); ++r) {
386 setv(a.getcol(c), r, F()(a.getcol(c)[r], a.getcol(c)[r-1]));
391 for (idx_type c=0; c < a.ncols(); c++) {
392 for (idx_type r=a.getcol(c).size()-1; r >= 1 ; --r) {
393 setv(a.getcol(c), r-1, F()(a.getcol(c)[r-1], a.getcol(c)[r]));
402 for (idx_type c=0; c < a.ncols(); c++) {
403 auto iter_start = a.getcol(c).begin();
404 auto iter_end = a.getcol(c).end();
405 for (arr::idx_type j=0; j<a.getcol(c).size() / 2; j++) {
406 std::iter_swap(iter_start++, --iter_end);