Skip to content

Commit

Permalink
Return whether extremum is minimum or maximum
Browse files Browse the repository at this point in the history
  • Loading branch information
shred committed Nov 27, 2017
1 parent 57f4286 commit f3e471a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,27 @@ 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));
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(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
Expand All @@ -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
Expand Down

0 comments on commit f3e471a

Please sign in to comment.