/** * @param f accumulator: the operator to be applied * @return the result */ public T reduce(BinaryOperator<T> f) { return Iterators.reduce(this, f); }
/** * @param f the accumulator function * @param start the start value * @param <V> any type * @return the accumulated result */ public <V> V reduce(BiFunction<V, T, V> f, V start) { return Iterators.reduce(this, f, start); } }
@Override public Double apply(Double x) { BinaryOperator<Double> horner = (a, b) -> a * x + b; return reduce(reverse(rt), horner, 0.0); } };
/** * @param t the first iterator * @param s the second iterator * @param dim the dimension * @return the coefficients of polynom(t) o polynom(s), thus * polynom(compose(t, s)) = compose(polynom(t), polynom(s)) */ public static Iterator<Double> compose(Iterator<Double> t, Iterator<Double> s, int dim) { List<Double> rt = take(t, dim); List<Double> rs = take(s, dim); BiFunction<Iterator<Double>, Double, Iterator<Double>> horner = (Iterator<Double> a, Double b) -> inc(multiply(a, rs.iterator()), b); return reduce(reverse(rt), horner, of(0.0)); }
/** * This method merges n time series into one according to the following strategy: * The timestamps are merged; the resulting sequence is the union of the given ones. * On each timestamp, the new value is computed by reducing the given operator to * the vector of values at that time stamp. * Typical operators are min, max, sum, avg. * * @param input an iterable of time series to be merged * @param op the operator to be applied * @param <T> type of timestamps * @param <V> type of values * @return iterator of merged timeValuePairs */ public static <T extends Comparable<T>, V> Iterator<Pair<T, V>> merge(Iterable<Iterator<Pair<T, V>>> input, BinaryOperator<V> op) { return map(merge(input), (Pair<T, List<V>> p) -> pairOf(p.getFirst(), reduce(asIterator(p.getSecond()), weakBinaryOperator(op)))); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @param op an operator reducing the value list * @return a new time series unioning all time stamps given, * the value being the result of the reducing by op (e.g. min, max, avg) */ public static <T extends Comparable<T>, V> TimeSeries<T, V> merge(Iterable<TimeSeries<T, V>> ts, BinaryOperator<V> op) { BinaryOperator<V> wop = weakBinaryOperator(op); Iterator<Pair<T, List<V>>> aux1 = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); Iterator<Pair<T, V>> aux2 = map(aux1, (Pair<T, List<V>> p) -> pairOf(p.getFirst(), reduce(asIterator(p.getSecond()), wop))); return new TimeSeries<>(aux2); }