/*
 * Zorbage: an Algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2019 Barry DeZonia
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package nom.bdezonia.zorbage.algorithm;

import nom.bdezonia.zorbage.function.Function2;
import nom.bdezonia.zorbage.procedure.Procedure2;
import nom.bdezonia.zorbage.type.algebra.Addition;
import nom.bdezonia.zorbage.type.algebra.Algebra;
import nom.bdezonia.zorbage.type.algebra.Invertible;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class NewtonsMethod<T extends Algebra<T,U> & Addition<U> & Invertible<U>, U>
	implements Function2<Boolean,U,U>
{
	// x1 = x0 - f(x0) / f'(x0)
	// iterate

	private final T alg;
	private final Procedure2<U,U> f;
	private final Derivative<T,U> fPrime;
	private final long maxIters;

	/**
	 * 
	 * @param alg
	 * @param f
	 * @param delta
	 * @param maxIters
	 */
	public NewtonsMethod(T alg, Procedure2<U,U> f, U delta, long maxIters) {
		this.alg = alg;
		this.f = f;
		this.fPrime = new Derivative<T,U>(alg, f, delta);
		this.maxIters = maxIters;
		if (maxIters < 1)
			throw new IllegalArgumentException("number of iterations must be > 0");
	}
	
	// TODO: detect convergence status. return true if converged and false if not.

	@Override
	public Boolean call(U guess, U result) {
		U tmp = alg.construct(guess);
		U n = alg.construct();
		U d = alg.construct();
		U correction = alg.construct();
		for (long i = 0; i < maxIters; i++) {
			f.call(tmp, n);
			fPrime.call(tmp, d);
			alg.divide().call(n, d, correction);
			alg.subtract().call(tmp, correction, tmp);
		}
		alg.assign().call(tmp, result);
		return true;
	}
}

@@ -1664,16 +1664,67 @@
 lgebra;%0A
+import nom.bdezonia.zorbage.type.algebra.Infinite;%0A
 import n
@@ -1888,16 +1888,30 @@
 tible%3CU%3E
+ & Infinite%3CU%3E
 , U%3E%0A%09im
@@ -2090,22 +2090,16 @@
 private 
-final 
 long max
@@ -2474,90 +2474,231 @@
 %0A%09%7D%0A
-%09
 %0A%09
-// TODO: detect convergence status. return true if converged and false if not.%0A
+public long getMaxIters() %7B%0A%09%09return maxIters;%0A%09%7D%0A%09%0A%09public void setMaxIters(long maxIters) %7B%0A%09%09this.maxIters = maxIters;%0A%09%09if (maxIters %3C 1)%0A%09%09%09throw new IllegalArgumentException(%22number of iterations must be %3E 0%22);%0A%09%7D%0A%09
 %0A%09@O
@@ -2929,27 +2929,125 @@
 %0A%09%09%09
-fPrime.call(tmp, d)
+if (alg.isInfinite().call(n))%0A%09%09%09%09return false;%0A%09%09%09fPrime.call(tmp, d);%0A%09%09%09if (alg.isZero().call(d))%0A%09%09%09%09return false
 ;%0A%09%09

Put divergence tests into NewtonsMethod class