public class Poly { private String variableName; private long [] coeffs; private long denominator; // Constructors public Poly ( String polyVariableName, long [] polyCoeffs, long polyDenominator ) { variableName = polyVariableName; coeffs = polyCoeffs; denominator = polyDenominator; } // Debug quality printing. Takes a polynomial and displays its coefficents. public String debugPrint () { String returnValue = "("; returnValue = returnValue + coeffs [0] + "*" + variableName + "^0"; for (int i = 1; i < coeffs.length; i++) { returnValue = returnValue + " + " + coeffs [i] + "*" + variableName + "^" + i; }; returnValue = returnValue + ")/"; returnValue = returnValue + denominator; return returnValue; } // Special-case multiplication. Multiplication by a coefficient. public static Poly mulByCoeff(Poly in, long mulBy) { Poly returnValue = new Poly ( in.variableName, new long [in.coeffs.length], in.denominator ); for (int i = 0; i < in.coeffs.length; i++) { returnValue.coeffs[i] = mulBy * in.coeffs [i]; }; returnValue = reduce(returnValue); returnValue = truncate(returnValue); return returnValue; } // Special-case multiplication. Division by a coefficient. public static Poly divByCoeff(Poly in, long divBy) { Poly returnValue = new Poly ( in.variableName, new long [in.coeffs.length], in.denominator ); returnValue.denominator = in.denominator * divBy; returnValue = reduce(returnValue); returnValue = truncate(returnValue); return returnValue; } // Special-case multiplication. Multiplication by "x", variableName public static Poly mulByx(Poly in) { Poly returnValue = new Poly ( in.variableName, new long [in.coeffs.length + 1], in.denominator); returnValue.coeffs[0] = 0; for (int i = 0; i < in.coeffs.length; i++) { returnValue.coeffs[i+1] = in.coeffs[i]; }; returnValue = reduce(returnValue); returnValue = truncate(returnValue); return returnValue; } // Addition of two polynomials public static Poly polyAdd (Poly A, Poly B) { // Determine length of coefficient array int lengthC = 0; if ( A.coeffs.length == B.coeffs.length ) { lengthC = A.coeffs.length; } else if ( A.coeffs.length > B.coeffs.length) { lengthC = A.coeffs.length; } else { lengthC = B.coeffs.length; }; // Determine common denominator long denomC = A.denominator * B.denominator; // Create return value Poly C = new Poly ( A.variableName, new long [lengthC], denomC); // Set all coefficients to 0 for (int i = 0; i < C.coeffs.length; i++) { C.coeffs[i] = 0; }; for (int i = 0; i < A.coeffs.length; i++) { C.coeffs[i] = C.coeffs[i] + B.denominator * A.coeffs[i]; }; for (int i = 0; i < B.coeffs.length; i++) { C.coeffs[i] = C.coeffs[i] + A.denominator * B.coeffs[i]; }; // Tidy up return value. C = reduce(C); C = truncate(C); return C; } // Subtraction of two polynomials public static Poly polyMinus (Poly A, Poly B) { return polyAdd(A, mulByCoeff(B,-1)); } // Multiplication of two polynomials public static Poly polyTimes (Poly A, Poly B) { // Determine length of coefficient array int lengthC = A.coeffs.length * B.coeffs.length; // Determine common denominator long denomC = A.denominator * B.denominator; // Create return value Poly C = new Poly ( A.variableName, new long [lengthC], denomC); // Set all coefficients to 0 for (int i = 0; i < C.coeffs.length; i++) { C.coeffs[i] = 0; }; for ( int i=0; i