From f3e471adc838ead7b0266de233702ae34a86cc8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Mon, 27 Nov 2017 22:35:43 +0100 Subject: [PATCH] Return whether extremum is minimum or maximum --- .../suncalc/util/QuadraticInterpolation.java | 12 ++++++++++++ .../suncalc/util/QuadraticInterpolationTest.java | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/shredzone/commons/suncalc/util/QuadraticInterpolation.java b/src/main/java/org/shredzone/commons/suncalc/util/QuadraticInterpolation.java index 6e9c6ba..d23e37f 100644 --- a/src/main/java/org/shredzone/commons/suncalc/util/QuadraticInterpolation.java +++ b/src/main/java/org/shredzone/commons/suncalc/util/QuadraticInterpolation.java @@ -25,6 +25,7 @@ public class QuadraticInterpolation { private final double root1; private final double root2; private final int nRoot; + private final boolean maximum; /** * Creates a new quadratic equation. @@ -43,6 +44,7 @@ public QuadraticInterpolation(double yMinus, double y0, double yPlus) { xe = -b / (2.0 * a); ye = (a * xe + b) * xe + c; + maximum = a < 0.0; double dis = b * b - 4.0 * a * c; int rootCount = 0; @@ -112,4 +114,14 @@ public int getNumberOfRoots() { return nRoot; } + /** + * Returns whether the extremum is a minimum or a maximum. + * + * @return {@code true}: Extremum at xe is a maximum. {@code false}: Extremum at xe is + * a minimum. + */ + public boolean isMaximum() { + return maximum; + } + } diff --git a/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java b/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java index c6db248..24143a8 100644 --- a/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/util/QuadraticInterpolationTest.java @@ -26,7 +26,7 @@ public class QuadraticInterpolationTest { private static final double ERROR = 0.001; @Test - public void testTwoRoots() { + public void testTwoRootsAndMinimum() { QuadraticInterpolation qi = new QuadraticInterpolation(1.0, -1.0, 1.0); assertThat(qi.getNumberOfRoots(), is(2)); @@ -34,6 +34,19 @@ public void testTwoRoots() { assertThat(qi.getRoot2(), is(closeTo( 0.707, ERROR))); assertThat(qi.getXe(), is(closeTo( 0.0, ERROR))); assertThat(qi.getYe(), is(closeTo(-1.0, ERROR))); + assertThat(qi.isMaximum(), is(false)); + } + + @Test + public void testTwoRootsAndMaximum() { + QuadraticInterpolation qi = new QuadraticInterpolation(-1.0, 1.0, -1.0); + + assertThat(qi.getNumberOfRoots(), is(2)); + assertThat(qi.getRoot1(), is(closeTo(-0.707, ERROR))); + assertThat(qi.getRoot2(), is(closeTo( 0.707, ERROR))); + assertThat(qi.getXe(), is(closeTo(0.0, ERROR))); + assertThat(qi.getYe(), is(closeTo(1.0, ERROR))); + assertThat(qi.isMaximum(), is(true)); } @Test @@ -44,6 +57,7 @@ public void testOneRoot() { assertThat(qi.getRoot1(), is(closeTo( 0.0, ERROR))); assertThat(qi.getXe(), is(closeTo( 1.5, ERROR))); assertThat(qi.getYe(), is(closeTo(-1.125, ERROR))); + assertThat(qi.isMaximum(), is(false)); } @Test