/** * @param s an iterator containing at least of element * @param inc the increment * @return an iterator whose first element is incremented by inc. */ @SuppressWarnings("unchecked") public static Iterator<Double> inc(Iterator<Double> s, double inc) { return concat(of(s.next() + inc), s); }
/** * @param <T> any type * @param zippedIterator iterator of lists; must not be empty. * The lists are typically but not necessarily * of equal size. * @return list of iterators to be iterated on independently * The result's size is that of the first list; subsequent lists * are truncated if longer and padded with nulls if shorter */ public static <T> List<ImmutableIterator<T>> unzip(Iterator<List<T>> zippedIterator) { List<T> firstColumn = zippedIterator.next(); int n = firstColumn.size(); @SuppressWarnings("unchecked") List<ImmutableIterator<List<T>>> aux = tee(concat(of(firstColumn), zippedIterator), n); List<ImmutableIterator<T>> result = new ArrayList<>(n); for (int i = 0; i < n; i++) { int j = i; // j is effectively final, i is not result.add(j, map(aux.get(j), xs -> j < xs.size() ? xs.get(j) : null)); } return result; }
/** * @param a left border a <= b * @param b right border * @return returns a time series identical with this on [a, b) and undefined otherwise */ public TimeSeries<T, V> subSeries(T a, T b) { if (cmp.compare(a, b) >= 0) { throw new IllegalArgumentException(); } int i = binarySearch(times, a, cmp); i = (0 <= i) ? i : -i - 2; int j = binarySearch(times, b, cmp); j = (0 <= j) ? j - 1 : -j - 2; Iterator<Pair<T, V>> result = fluent(of(pairOf(a, this.apply(a)))) .concat(zip(pairOf(times.subList(i, j + 1).iterator(), values.subList(i, j + 1).iterator()), true), of(pairOf(b, null))); return new TimeSeries<>(result); }
/** * @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)); }
/** * @param timestamps an iterator of (time, value)-pairs. * identical times are discarded but the last. * identical values are discarded but the first. * Timestamps must be non-descending * A timestamp (null, null) will be inserted if missing */ public TimeSeries(Iterator<Pair<T, V>> timestamps) { FluentIterator<Pair<T, V>> aux = fluent(of(pairOf((T) null, (V) null))) .concat(timestamps) .keepLast(Pair::getFirst) .keepFirst(Pair::getSecond); while (aux.hasNext()) { Pair<T, V> current = aux.next(); times.add(current.getFirst()); values.add(current.getSecond()); } }