MathIterators { 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)); } private  MathIterators(); static Comparator<Double> weakDoubleComparator(double epsilon); static UnaryOperator<Double> increment(Double delta); static UnaryOperator<Double> times(Double factor); static InfiniteIterator<Double> random(); static InfiniteIterator<Double> arithmeticSeries(double start, double delta); static InfiniteIterator<Double> geometricSeries(double start, double factor); static ImmutableIterator<Double> exp(); static ImmutableIterator<Double> sin(); static ImmutableIterator<Double> cos(); static ImmutableIterator<Double> add(Iterator<Double> s, double inc); @SuppressWarnings("unchecked") static Iterator<Double> inc(Iterator<Double> s, double inc); static ImmutableIterator<Double> add(Iterator<Double> s, Iterator<Double> t); static ImmutableIterator<Double> multiply(Iterator<Double> s, double factor); static ImmutableIterator<Double> multiply(Iterator<Double> s, Iterator<Double> t); static ImmutableIterator<Double> square(Iterator<Double> t); static InfiniteIterator<Double> inverse(Iterator<Double> t); static ImmutableIterator<Double> divide(Iterator<Double> s, Iterator<Double> t); static UnaryOperator<Double> polynom(Iterator<Double> t, int dim); static UnaryOperator<Double> polynom(Iterator<Double> t); static Iterator<Double> compose(Iterator<Double> t, Iterator<Double> s, int dim); static Iterator<Double> compose(Iterator<Double> t, Iterator<Double> s);  }
@Test public void testCompose() { Iterator<Double> t = of(1.0, 2.0, 3.0); Iterator<Double> s = of(5.0, 7.0); Iterator<Double> r = compose(t, s, 5); BinaryOperator<Double> acc = (a, b) -> a + b; double sum = reduce(r, acc); assertEquals(457.0, sum, 0); t = of(1.0, 2.0, 3.0); s = of(0.0); r = compose(t, s, 5); sum = reduce(r, acc); assertEquals(1.0, sum, 0); t = of(1.0, 2.0, 3.0); s = of(1.0); r = compose(t, s, 5); sum = reduce(r, acc); assertEquals(6.0, sum, 0); t = of(1.0, 1.0, 1.0); s = of(1.0, 1.0); r = compose(t, s, 5); sum = reduce(r, acc); assertEquals(7.0, sum, 0); t = of(1.0, 0.0); s = of(0.0, 1.0); r = compose(t, s); sum = reduce(r, acc); assertEquals(1.0, sum, 0); t = of(0.0, 1.0, 2.0); s = of(0.0); r = compose(t, s); sum = reduce(r, acc); assertEquals(0.0, sum, 0); s = of(0.0, 1.0, 2.0); t = of(0.0); r = compose(t, s); sum = reduce(r, acc); assertEquals(0.0, sum, 0); s = of(0.0, 1.0, 2.0); t = of(0.0, 1.0); r = compose(t, s); sum = reduce(r, acc); assertEquals(3.0, sum, 0); }