/** * evaluating generalized polynomials: x^a for a real a. * method: use Taylor Expansion: * f(x) = f(1) + (x-1)f'(1) + (x-1)^2 f''(1)/2! + (x-1)^3 f'''(1)/3! +... * x^a = 1 + (x-1).a + (x-1)^2.a.(a-1)/2 + (x-1)^3.a.(a-1).(a-2)/3! + ... */ import java.math.*; public class genpoly extends series { protected int sign = 1; protected Rational base = 0; protected Rational expo = 0; protected long NoTerms = 1; protected Rational valpos = 0.0; protected Rational valneg = 0.0; /** * Parity of int number * * @param N input integer * @return 0 if even, 1 if odd */ public static int parity(int N){ return parity((long)N); } /** * Parity of long number * * @param N input integer * @return 0 if even, 1 if odd */ public static int parity(long N){ long m; m = N/2; if (N != 2*m) return 1; return 0; } /** * Constructor for genpoly Series * * @param e is the exponent of the monomial * @param b is the base of the monomial, M=b^e. */ genpoly(double b, double e){ base = b; expo = e; n = -1; sign = -1; } /** * First initializes the first term of series */ public void first(){ term=1.0; sign=1; n=0; valpos = term; valneg = 0.0; val = valpos - valneg; } /** * Advances to the next term in the series. */ public void nextTerm(){ n++; term = term*(base-1)*(expo-n+1)/n; if (term>0) valpos = valpos + term; else valneg = valneg + term; val = val + sign*term; // inaccurate way //val = valpos - valneg; // accurate way //System.out.println("n=" + n + ", sign=" + sign + ", term=" +term); } /** * Returns the value of the mth term in series * * @param m the index of a term * @return the mth term */ public double termAt(int m){ first(); for (int i=0; i