Skip to content

Commit

Permalink
Adding in regression to the library
Browse files Browse the repository at this point in the history
  • Loading branch information
s-oronae authored and s-oronae committed Jan 8, 2020
1 parent b2b5096 commit 22ec097
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 3 deletions.
67 changes: 67 additions & 0 deletions src/main/java/com/robototes/math/Equation.java
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;
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/robototes/math/SimpleRegression.java
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);
}
}
5 changes: 2 additions & 3 deletions src/test/java/com/robototes/math/DoubleRatioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ 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
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
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/robototes/math/EquationTest.java
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 src/test/java/com/robototes/math/SimpleRegressionTest.java
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));
}

}

0 comments on commit 22ec097

Please sign in to comment.