@Test public void testQuadraticFunctionRoots() { List<Double> roots; Assert.assertTrue(SumatraMath.quadraticFunctionRoots(0, 0, 1).isEmpty()); roots = SumatraMath.quadraticFunctionRoots(0, 2, 1); Assert.assertTrue(roots.size() == 1); Assert.assertEquals(-0.5, roots.get(0), 1e-6); roots = SumatraMath.quadraticFunctionRoots(2, 2, 1); Assert.assertTrue(roots.isEmpty()); roots = SumatraMath.quadraticFunctionRoots(1, 2, 1); Assert.assertTrue(roots.size() == 1); Assert.assertEquals(-1.0, roots.get(0), 1e-6); roots = SumatraMath.quadraticFunctionRoots(1, 6, 5); Assert.assertTrue(roots.size() == 2); Assert.assertEquals(-1.0, roots.get(0), 1e-6); Assert.assertEquals(-5.0, roots.get(1), 1e-6); }
public static List<Double> quadraticFunctionRoots(final double a, final double b, final double c) { List<Double> roots = new ArrayList<>(); if (isEqual(a, 0.0)) { if (isEqual(b, 0.0)) { return roots; } double x1 = -c / b; roots.add(x1); return roots; } double p = b / a; double q = c / a; double d = (p * p) - (4.0 * q); if (d < 0.0) { } else if (d > 0.0) { double pHalf = p * 0.5; double x1 = -pHalf + SumatraMath.sqrt((pHalf * pHalf) - q); double x2 = -pHalf - SumatraMath.sqrt((pHalf * pHalf) - q); roots.add(x1); roots.add(x2); } else { double x1 = -p * 0.5; roots.add(x1); } return roots; }
SumatraMath { public static List<Double> quadraticFunctionRoots(final double a, final double b, final double c) { List<Double> roots = new ArrayList<>(); if (isEqual(a, 0.0)) { if (isEqual(b, 0.0)) { return roots; } double x1 = -c / b; roots.add(x1); return roots; } double p = b / a; double q = c / a; double d = (p * p) - (4.0 * q); if (d < 0.0) { } else if (d > 0.0) { double pHalf = p * 0.5; double x1 = -pHalf + SumatraMath.sqrt((pHalf * pHalf) - q); double x2 = -pHalf - SumatraMath.sqrt((pHalf * pHalf) - q); roots.add(x1); roots.add(x2); } else { double x1 = -p * 0.5; roots.add(x1); } return roots; } }
SumatraMath { public static List<Double> quadraticFunctionRoots(final double a, final double b, final double c) { List<Double> roots = new ArrayList<>(); if (isEqual(a, 0.0)) { if (isEqual(b, 0.0)) { return roots; } double x1 = -c / b; roots.add(x1); return roots; } double p = b / a; double q = c / a; double d = (p * p) - (4.0 * q); if (d < 0.0) { } else if (d > 0.0) { double pHalf = p * 0.5; double x1 = -pHalf + SumatraMath.sqrt((pHalf * pHalf) - q); double x2 = -pHalf - SumatraMath.sqrt((pHalf * pHalf) - q); roots.add(x1); roots.add(x2); } else { double x1 = -p * 0.5; roots.add(x1); } return roots; } private  SumatraMath(); }
SumatraMath { public static List<Double> quadraticFunctionRoots(final double a, final double b, final double c) { List<Double> roots = new ArrayList<>(); if (isEqual(a, 0.0)) { if (isEqual(b, 0.0)) { return roots; } double x1 = -c / b; roots.add(x1); return roots; } double p = b / a; double q = c / a; double d = (p * p) - (4.0 * q); if (d < 0.0) { } else if (d > 0.0) { double pHalf = p * 0.5; double x1 = -pHalf + SumatraMath.sqrt((pHalf * pHalf) - q); double x2 = -pHalf - SumatraMath.sqrt((pHalf * pHalf) - q); roots.add(x1); roots.add(x2); } else { double x1 = -p * 0.5; roots.add(x1); } return roots; } private  SumatraMath(); static double sqrt(final double number); static double square(final double x); static double min(final double... values); static double max(final double... values); static boolean hasDigitsAfterDecimalPoint(final double number); static boolean isEqual(final double a, final double b, final double tolerance); static boolean isEqual(final double a, final double b); static boolean isBetween(final double x, final double min, final double max); static boolean isZero(final double v); static double relative(final double value, final double from, final double to); static double cap(final double value, final double min, final double max); static List<Double> quadraticFunctionRoots(final double a, final double b, final double c); static List<Double> cubicFunctionRoots(final double a3, final double b2, final double c1, final double d0); static double getEqualTol(); static double acos(double x); static double asin(double x); static double atan2(double y, double x); static double cos(double angle); static double sin(double angle); static double tan(double angle); static List<Double> evenDistribution1D(double min, double max, int n); }
SumatraMath { public static List<Double> quadraticFunctionRoots(final double a, final double b, final double c) { List<Double> roots = new ArrayList<>(); if (isEqual(a, 0.0)) { if (isEqual(b, 0.0)) { return roots; } double x1 = -c / b; roots.add(x1); return roots; } double p = b / a; double q = c / a; double d = (p * p) - (4.0 * q); if (d < 0.0) { } else if (d > 0.0) { double pHalf = p * 0.5; double x1 = -pHalf + SumatraMath.sqrt((pHalf * pHalf) - q); double x2 = -pHalf - SumatraMath.sqrt((pHalf * pHalf) - q); roots.add(x1); roots.add(x2); } else { double x1 = -p * 0.5; roots.add(x1); } return roots; } private  SumatraMath(); static double sqrt(final double number); static double square(final double x); static double min(final double... values); static double max(final double... values); static boolean hasDigitsAfterDecimalPoint(final double number); static boolean isEqual(final double a, final double b, final double tolerance); static boolean isEqual(final double a, final double b); static boolean isBetween(final double x, final double min, final double max); static boolean isZero(final double v); static double relative(final double value, final double from, final double to); static double cap(final double value, final double min, final double max); static List<Double> quadraticFunctionRoots(final double a, final double b, final double c); static List<Double> cubicFunctionRoots(final double a3, final double b2, final double c1, final double d0); static double getEqualTol(); static double acos(double x); static double asin(double x); static double atan2(double y, double x); static double cos(double angle); static double sin(double angle); static double tan(double angle); static List<Double> evenDistribution1D(double min, double max, int n);  }