package com.github.davidmoten.rx;

import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;

public final class Maths {

    private Maths() {
        // prevent instantiation
    }

    public static Observable<Double> solveWithNewtonsMethod(final Func1<Double, Double> f,
            final Func1<Double, Double> dfdx, double x0) {
        return Observable.range(0, Integer.MAX_VALUE).scan(x0,
                new Func2<Double, Integer, Double>() {

                    @Override
                    public Double call(Double xn, Integer n) {
                        return xn - f.call(xn) / dfdx.call(xn);
                    }
                });
    }

    public static Observable<Double> solveWithNewtonsMethod(final Func1<Double, Double> f,
            final double x0, final double h) {
        return Observable.range(0, Integer.MAX_VALUE).scan(x0,
                new Func2<Double, Integer, Double>() {

                    @Override
                    public Double call(Double xn, Integer n) {
                        return xn - f.call(xn) * 2 * h / (f.call(xn + h) - f.call(xn - h));
                    }
                });
    }

}

@@ -367,42 +367,20 @@
  Obs
-ervable.range(0, Integer.MAX_VALUE
+.repeating(1
 ).sc
@@ -433,33 +433,32 @@
 er, Double%3E() %7B%0A
-%0A
                 
@@ -797,78 +797,36 @@
     
-return Observable.range(0, Integer.MAX_VALUE).scan(x0,%0A               
+Func1%3CDouble, Double%3E dfdx =
  new
@@ -826,33 +826,33 @@
 x = new Func
-2
+1
 %3CDouble,
  Integer, Do
@@ -835,33 +835,24 @@
 unc1%3CDouble,
- Integer,
  Double%3E() %7B
@@ -852,23 +852,16 @@
 le%3E() %7B%0A
-%0A      
         
@@ -856,34 +856,32 @@
 ) %7B%0A            
-  
 @Override%0A      
@@ -870,32 +870,24 @@
   @Override%0A
-        
             
@@ -913,37 +913,20 @@
 Double x
-n, Integer n) %7B%0A     
+) %7B%0A
         
@@ -937,43 +937,14 @@
     
-   
 return
- xn - f.call(xn) * 2 * h /
  (f.
@@ -953,15 +953,14 @@
 ll(x
-n
  + h
-)
  
+)
 - f.
@@ -969,15 +969,18 @@
 ll(x
-n - h))
+-h))/2.0/h
 ;%0A  
@@ -993,35 +993,61 @@
     
-        %7D%0A                %7D
+%7D%7D;%0A        return solveWithNewtonsMethod(f, dfdx, x0
 );%0A 

refactor newtons