/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2017 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.type.algebra.Group;
import nom.bdezonia.zorbage.type.algebra.Invertible;
import nom.bdezonia.zorbage.type.algebra.Multiplication;
import nom.bdezonia.zorbage.type.algebra.Unity;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class Power {

	private Power() {}
	
	/**
	 * 
	 * @param group
	 * @param power
	 * @param a
	 * @param b
	 */
	public static <T extends Group<T,U> & Multiplication<U> & Unity<U> & Invertible<U>, U>
		void compute(T group, int power, U a, U b)
	{
		if (power == 0) {
			U tmp = group.construct();
			if (group.isEqual(a,tmp))
				throw new IllegalArgumentException("0^0 is not a number");
			group.unity(tmp);
			group.assign(tmp, b);
			return;
		}
		// else power != 0

		pow(group, power, a, b);
	}
	
	private static <T extends Group<T,U> & Multiplication<U> & Unity<U> & Invertible<U>, U>
		void pow(T group, int pow, U a, U b)
	{
		if (pow < 0) {
			U invA = group.construct();
			group.invert(a, invA);
			pow(group, pow, invA, b);
		}
		else if (pow == 0) {
			group.unity(b);
		}
		else if (pow == 1) {
			group.assign(a, b);
		}
		else {
			U tmp = group.construct();
			int halfPow = pow >>> 1;
			pow(group, halfPow, a, tmp);
			group.multiply(tmp, tmp, tmp);
			if (pow != (halfPow << 1))
				group.multiply(tmp, a, tmp);
			group.assign(tmp, b);
		}
	}
}

@@ -2196,171 +2196,8 @@
  0%0A%0A
-%09%09pow(group, power, a, b);%0A%09%7D%0A%09%0A%09private static %3CT extends Group%3CT,U%3E & Multiplication%3CU%3E & Unity%3CU%3E & Invertible%3CU%3E, U%3E%0A%09%09void pow(T group, int pow, U a, U b)%0A%09%7B%0A
 %09%09if
@@ -2201,16 +2201,18 @@
 %09if (pow
+er
  %3C 0) %7B%0A
@@ -2271,27 +2271,38 @@
 vA);%0A%09%09%09
-p
+P
 ow
+erI.compute
 (group, 
 pow, inv
@@ -2293,19 +2293,22 @@
 (group, 
+-
 pow
+er
 , invA, 
@@ -2325,322 +2325,47 @@
 else
- if (pow == 0) %7B%0A%09%09%09group.unity(b);%0A%09%09%7D%0A%09%09else if (pow == 1) %7B%0A%09%09%09group.assign(a, b);%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09U tmp = group.construct();%0A%09%09%09int halfPow = pow %3E%3E%3E 1;%0A%09%09%09pow(group, halfPow, a, tmp);%0A%09%09%09group.multiply(tmp, tmp, tmp);%0A%09%09%09if (pow != (halfPow %3C%3C 1))%0A%09%09%09%09group.multiply(tmp, a, tmp);%0A%09%09%09group.assign(tmp, b);%0A%09%09%7D
+%0A%09%09%09PowerI.compute(group, power, a, b);
 %0A%09%7D%0A

Simplify Power by utilizing PowerI