Skip to content

Instantly share code, notes, and snippets.

@icedraco
Created November 27, 2014 11:27
Show Gist options
  • Save icedraco/ab454283547d1c4e3c2f to your computer and use it in GitHub Desktop.
Save icedraco/ab454283547d1c4e3c2f to your computer and use it in GitHub Desktop.
A generator/diagnostics class for the polynom validator. It was written in order to test a polynom calculator assignment at the university...
/**
* Polynom validator diagnostics class
*
* This class is responsible for checking the polynom validator, bringing up
* potentially suspicious polynoms that have either passed or failed, but
* possibly shouldn't have.
*
* Since the probability to hit those is very small, out of 1000 tests, the
* user will only have to check a few of those to make sure those random
* polynom strings are, indeed, valid (or invalid).
*
* @author IceDragon <[email protected]>
*/
class PolynomDiagnostics {
// How many tests of each (rational/real) should we perform?
public final static int NUM_TESTS = 1000;
/*** PolynomStringGenerator Sub-Class ************************************/
/**
* This singleton class is responsible for generating polynom
* terms/elements/components, as well as entire polynom strings that are
* built from them.
*
* @author IceDragon <[email protected]>
*/
static class PolynomStringGenerator {
// How big a coefficient can get? (|coeff| <= MAX_COEFFICIENT)
public final static int MAX_COEFFICIENT = 10000;
// How big an exponent power can be (x^p : |p| <= MAX_POWER)
public final static int MAX_POWER = 50;
// How many elements (at most) can a polynom string have?
public final static int MAX_POLYNOM_ELEMENTS = 1000;
// What are the changes to get a fraction (a/b as opposed to just a)
public final static double CHANCE_FRACTION = 0.7; // Chance to get a fraction (and not a whole)
// What are the chances to get a negative element?
public final static double CHANCE_NEGATIVE = 0.5; // Chance to get a negative coefficient
/**
* Returns a polynom string with randomly-generated elements. The
* exponents are very unlikely to be in a climbing order, so these are
* very likely to be invalid.
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
* @return Polynom string
*/
public static String getRandomPolynom(boolean isRational) {
return getRandomPolynom(isRational, 1, MAX_POLYNOM_ELEMENTS);
}
/**
* Returns a polynom string with randomly-generated elements. The
* exponents are very unlikely to be in a climbing order, so these are
* very likely to be invalid.
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
* @param minIterations [Default: 1]
* Minimal amount of elements within polynom
* @param maxIterations [Default: MAX_POLYNOM_ELEMENTS]
* Maximal amount of elements within polynom
* @return Polynom string
*/
public static String getRandomPolynom(boolean isRational, int minIterations, int maxIterations) {
int iter = randInt(minIterations, maxIterations) - 1;
StringBuilder sb = new StringBuilder(getRandomTerm(isRational));
while (iter > 0) {
String element = getRandomTerm(isRational);
if (element.charAt(0) != '-')
sb.append('+');
sb.append(element);
iter--;
}
return sb.toString();
}
/**
* Returns a polynom string with randomly-generated elements, where the
* exponents appear in a climbing order (i.e.: x^2, x^6, x^20, ...)
*
* These polynoms are supposed to be valid and accepted by the system!
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
* @return Polynom string
*/
public static String getRandomClimbingPolynom(boolean isRational) {
return getRandomClimbingPolynom(isRational, 1, MAX_POWER);
}
/**
* Returns a polynom string with randomly-generated elements, where the
* exponents appear in a climbing order (i.e.: x^2, x^6, x^20, ...)
*
* These polynoms are supposed to be valid and accepted by the system!
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
* @param minPower [Default: 1] Minimal exponent power
* @param maxPower [Default: MAX_POWER] Maximal exponent power
* @return Polynom string
*/
public static String getRandomClimbingPolynom(boolean isRational, int minPower, int maxPower) {
StringBuilder sb = new StringBuilder(getRandomTerm(isRational, minPower, minPower));
int current_power = minPower;
while (current_power < maxPower) {
current_power += randInt(1,maxPower - current_power);
String element = getRandomTerm(isRational, current_power, current_power);
if (element.charAt(0) != '-')
sb.append('+');
sb.append(element);
}
return sb.toString();
}
/**
* Returns a random polynom term in either Rational field or Real:
* Example (Q): a/bx^p where |a| <= MAX_COEFFICIENT
* Example (R): ax^p where |a| <= MAX_COEFFICIENT
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
* @return Polynom term/element/component string
*/
public static String getRandomTerm(boolean isRational) {
return getRandomTerm(isRational, 1, MAX_POWER);
}
/**
* Returns a random polynom term in either Rational field or Real:
* Example (Q): a/bx^p where |a| <= MAX_COEFFICIENT
* Example (R): ax^p where |a| <= MAX_COEFFICIENT
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
* @param minPower [Default: 1]
* @param maxPower [Default: MAX_POWER]
* @return Polynom term/element/component string
*/
public static String getRandomTerm(boolean isRational, int minPower, int maxPower) {
return isRational
? getRandomTermQ(0, MAX_COEFFICIENT, minPower, maxPower)
: getRandomTermR(0, MAX_COEFFICIENT, minPower, maxPower);
}
/**
* Returns a random polynom term in the Rational (Q) field
* Example: a/bx^p or ax^p
*
* @param minCoeff [Default: 0] Minimal coefficient value
* @param maxCoeff [Default: MAX_COEFFICIENT] Maximal coefficient value
* @param minPower [Default: 1] Minimal exponent power
* @param maxPower [Default: MAX_POWER] Maximal exponent power
* @return Polynom term/element/component string
*/
public static String getRandomTermQ(int minCoeff, int maxCoeff, int minPower, int maxPower) {
int numerator = randInt(minCoeff, maxCoeff);
int denominator = randInt(minCoeff, maxCoeff);
int power = randInt(minPower, maxPower);
String denom = Math.random() <= CHANCE_FRACTION
? "/" + denominator
: "";
if (Math.random() <= CHANCE_NEGATIVE)
numerator = -numerator;
return "" + numerator + denom +"x^"+ power;
}
/**
* Returns a random polynom term in the Real (R) field
* Example: ax^p
*
* @param minCoeff [Default: 0] Minimal coefficient value
* @param maxCoeff [Default: MAX_COEFFICIENT] Maximal coefficient value
* @param minPower [Default: 1] Minimal exponent power
* @param maxPower [Default: MAX_POWER] Maximal exponent power
* @return Polynom term/element/component string
*/
public static String getRandomTermR(double minCoeff, double maxCoeff, int minPower, int maxPower) {
int power = randInt(minPower, maxPower);
double coeff = minCoeff == maxCoeff
? minCoeff
: minCoeff + Math.random()*(maxCoeff-minCoeff);
if (Math.random() <= CHANCE_NEGATIVE)
coeff = -coeff;
return "" + coeff +"x^"+ power;
}
}
/*************************************************************************/
/**
* PROGRAM ENTRY POINT
* @param args
*/
public static void main(String[] args) {
// Check valid polynoms
for (int i = 0; i < NUM_TESTS; i++) {
checkValidPolynom(true);
checkValidPolynom(false);
}
// Check invalid polynoms
for (int i = 0; i < NUM_TESTS; i++) {
checkInvalidPolynom(true);
checkInvalidPolynom(false);
}
}
/**
* Performs a single check against a randomly selected valid polynom.
* Valid polynoms have a climbing exponents as opposed to ranomly-placed
* ones.
*
* If a randomly-generated polynom was found invalid (shouldn't happen),
* the user will be notified over the console with the following message:
* INVALID: <polynom string>
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
*/
public static void checkValidPolynom(boolean isRational) {
String ps = PolynomStringGenerator.getRandomClimbingPolynom(isRational);
//System.out.println("CHECK: "+ ps);
if (!Polynom.isValid(ps, isRational ? Field.Q : Field.R))
System.out.println("INVALID: "+ ps);
//else
// System.out.println("VALID: "+ ps);
}
/**
* Performs a single check against a randomly selected polynom - polenom
* which has a very high probability to be invalid.
*
* If a randomly-generated polynom was found valid (MIGHT happen), the user
* will be notified over the console with the following message:
* VALID: <polynom string>
*
* @param isRational TRUE = Rational (Q) / FALSE = Real (R)
*/
public static void checkInvalidPolynom(boolean isRational) {
String ps = PolynomStringGenerator.getRandomPolynom(isRational);
//System.out.println("CHECK: "+ ps);
if (Polynom.isValid(ps, isRational ? Field.Q : Field.R))
System.out.println("VALID: "+ ps);
//else
// System.out.println("INVALID: "+ ps);
}
/**
* Integer randomizer for convenience sake...
*
* @param min Minimal integer we can get
* @param max Maximal integer we can get
* @return Random number from min to max (inclusive)
*/
protected static int randInt(int min, int max) {
return (int)(Math.random()*(max-min+1)+min);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment