/** * @param n the number of elements to be read * @return a list containing the first n elements of t * or all elements of t if there are not more than n. */ public List<T> take(int n) { return Iterators.take(this, n); }
/** * @param t an iterator yielding the coefficients in ascending order: * a0, a1, a2, .. * may be finite or infinite * @param dim the desired dimension. Mandatory iff t is infinite * @return a polynom defined on doubles. */ public static UnaryOperator<Double> polynom(Iterator<Double> t, int dim) { return new UnaryOperator<Double>() { private List<Double> rt = take(t, dim); @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)); }