diff --git a/src/main/java/com/robototes/math/Equation.java b/src/main/java/com/robototes/math/Equation.java new file mode 100644 index 0000000..a94e027 --- /dev/null +++ b/src/main/java/com/robototes/math/Equation.java @@ -0,0 +1,67 @@ +package com.robototes.math; + +/** + * + * @author OroArmor + * + */ +public class Equation { + private double[] terms; + + /** + * + * @param terms the terms for the equation, currently only doubles are + * supported. terms go from x^0 to x^n. + */ + public Equation(double... terms) { + this.terms = terms; + } + + public double calculate(double x) { + + double t = 1; + double y = 0; + + for (double term : terms) { + y += term * t; + t *= x; + } + + return y; + } + + public double[] getTerms() { + return terms; + } + + @Override + public String toString() { + String equation = ""; + int i = 0; + for (double term : terms) { + equation = term + ((i++ != 0) ? "x^" + (i - 1) + " + " : "") + equation; + } + + return "y = " + equation; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Equation)) { + return false; + } else { + Equation other = (Equation) o; + + if (other.terms.length != this.terms.length) { + return false; + } + + for (int i = 0; i < this.terms.length; i++) { + if (!MathUtils.epsilonEquals(this.terms[i], other.terms[i], MathUtils.EPSILON)) { + return false; + } + } + } + return true; + } +} diff --git a/src/main/java/com/robototes/math/SimpleRegression.java b/src/main/java/com/robototes/math/SimpleRegression.java new file mode 100644 index 0000000..1e8f912 --- /dev/null +++ b/src/main/java/com/robototes/math/SimpleRegression.java @@ -0,0 +1,48 @@ +package com.robototes.math; + +public class SimpleRegression { + + /** + * + * @param pointOne + * @param pointTwo + * @return An equation that goes through all points + */ + public static Equation linearRegression(Vector pointOne, Vector pointTwo) { + + double m = (pointTwo.y - pointOne.y) / (pointTwo.x - pointOne.x); + + double b = pointTwo.y - m * pointTwo.x; + + return new Equation(b, m); + } + + /** + * + * @param pointOne + * @param pointTwo + * @param pointThree + * @return An quadratic equation that goes through the three points. + */ + public static Equation quadraticRegression(Vector pointOne, Vector pointTwo, Vector pointThree) { + // Lagrange polynomial + double bottom1 = (pointOne.x * pointOne.x - (pointTwo.x + pointThree.x) * pointOne.x + + pointTwo.x * pointThree.x); + double bottom2 = (pointTwo.x * pointTwo.x - (pointOne.x + pointThree.x) * pointTwo.x + + pointOne.x * pointThree.x); + double bottom3 = (pointThree.x * pointThree.x - (pointOne.x + pointTwo.x) * pointThree.x + + pointOne.x * pointTwo.x); + + double coeff1 = pointOne.y / bottom1; + double coeff2 = pointTwo.y / bottom2; + double coeff3 = pointThree.y / bottom3; + + double a = +(coeff1 + coeff2 + coeff3); + double b = -((pointTwo.x + pointThree.x) * coeff1 + (pointOne.x + pointThree.x) * coeff2 + + (pointOne.x + pointTwo.x) * coeff3); + double c = +((pointTwo.x * pointThree.x) * coeff1 + (pointOne.x * pointThree.x) * coeff2 + + (pointOne.x * pointTwo.x) * coeff3); + + return new Equation(c, b, a); + } +} diff --git a/src/test/java/com/robototes/math/DoubleRatioTest.java b/src/test/java/com/robototes/math/DoubleRatioTest.java index e8f775d..d844cda 100644 --- a/src/test/java/com/robototes/math/DoubleRatioTest.java +++ b/src/test/java/com/robototes/math/DoubleRatioTest.java @@ -11,7 +11,7 @@ public void testCalculateRatio() { DoubleRatio one = new DoubleRatio(10, "a", "b"); assertEquals("Calculate double ratio", one.calculateRatio(10), 100, MathUtils.EPSILON); - assertEquals("Calculate Double ratio", one.calculateRatio(new Double(0.1)), 1, MathUtils.EPSILON); + assertEquals("Calculate Double ratio", one.calculateRatio(0.1), 1, MathUtils.EPSILON); } @Test @@ -19,8 +19,7 @@ public void testCalculateReverseRatio() { DoubleRatio one = new DoubleRatio(10, "a", "b"); assertEquals("Calculate reverse double ratio", one.calculateReverseRatio(10), 1, MathUtils.EPSILON); - assertEquals("Calculate reverse Double ratio", one.calculateReverseRatio(new Double(1)), 0.1, - MathUtils.EPSILON); + assertEquals("Calculate reverse Double ratio", one.calculateReverseRatio(1), 0.1, MathUtils.EPSILON); } @Test diff --git a/src/test/java/com/robototes/math/EquationTest.java b/src/test/java/com/robototes/math/EquationTest.java new file mode 100644 index 0000000..a5db2f7 --- /dev/null +++ b/src/test/java/com/robototes/math/EquationTest.java @@ -0,0 +1,15 @@ +package com.robototes.math; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class EquationTest { + + @Test + public void testCalculate() { + Equation equation = new Equation(0, 0, 1); // y = x^2 + double x = 3; + assertEquals("Simple equation has correct value", x * x, equation.calculate(x), MathUtils.EPSILON); + } +} diff --git a/src/test/java/com/robototes/math/SimpleRegressionTest.java b/src/test/java/com/robototes/math/SimpleRegressionTest.java new file mode 100644 index 0000000..12ff9b4 --- /dev/null +++ b/src/test/java/com/robototes/math/SimpleRegressionTest.java @@ -0,0 +1,26 @@ +package com.robototes.math; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class SimpleRegressionTest { + + @Test + public void testLinearRegression() { + Vector a = new Vector(0, 0); + Vector b = new Vector(1, 1); + + assertEquals("Linear Regression", new Equation(0, 1), SimpleRegression.linearRegression(a, b)); + } + + @Test + public void testQuadraticRegression() { + Vector a = new Vector(0, 0); + Vector b = new Vector(1, 1); + Vector c = new Vector(2, 4); + + assertEquals("Quadratic Regression", new Equation(0, 0, 1), SimpleRegression.quadraticRegression(a, b, c)); + } + +}