-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/java/com/robototes/math/SimpleRegressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |