Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimann Sum Calculator/Equation Parser #42

Merged
merged 12 commits into from
Oct 2, 2017
11 changes: 11 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="java/distance_formula"/>
<classpathentry kind="src" path="java/Geometry_Triangle_Formulas"/>
<classpathentry kind="src" path="java/Linear_Function_Formulas"/>
<classpathentry kind="src" path="java/nth_root"/>
<classpathentry kind="src" path="java/physics/force"/>
<classpathentry kind="src" path="java/quadratic_formula"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Hacktoberfest-Mathematics</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
138 changes: 138 additions & 0 deletions calculus/reimann_sum/java/Equation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import java.util.HashMap;
import java.util.Stack;

public class Equation
{

private String infixString;
private String tempInfixString;
private HashMap<Character, Integer> operatorValues;

public Equation(String eq)
{
operatorValues = new HashMap<Character, Integer>();
operatorValues.put('+', 0);
operatorValues.put('-', 0);
operatorValues.put('*', 1);
operatorValues.put('/', 1);
operatorValues.put('^', 2);

infixString = convertToInfix(eq);
tempInfixString = infixString;

}

private String convertToInfix(String eq)
{
String output = "";
eq = eq.replaceAll(" ", "");
Stack<Character> operators = new Stack<Character>();
boolean wasNum = false;
for (int i = 0; i < eq.length(); i++)
{
char currentChar = eq.charAt(i);
if (operatorValues.containsKey(currentChar))
{
if(wasNum)
{
output += " ";
}
while ((operators.size() != 0 && operators.peek() != '(')
&& (operatorValues.get(currentChar) <= operatorValues.get(operators.peek())
&& operatorValues.get(currentChar) != 2))
{
output += operators.pop() + " ";
}
operators.push(currentChar);
} else if (currentChar == '(')
{
operators.push(currentChar);
} else if (currentChar == ')')
{
if(wasNum)
{
output += " ";
}
while (operators.size() != 0 && operators.peek() != '(')
{
output += operators.pop() + " ";
}
operators.pop();
} else
{
output += currentChar;
wasNum = true;
}
}
if(wasNum)
{
output += " ";
}
while (operators.size() != 0)
{
output += operators.pop() + " ";
}
return output;
}

public double evaluateEquation(double val)
{
tempInfixString = infixString.replaceAll("x", Double.toString(val));
return this.evaluateEquation();
}

public double evaluateEquation()
{
String[] infixArray = tempInfixString.split(" ");

Stack<Double> operands = new Stack<Double>();
for (int i = 0; i < infixArray.length; i++)
{
String currentElem = infixArray[i];

if (!operatorValues.containsKey(currentElem.charAt(0)))
{
operands.push(Double.parseDouble(infixArray[i]));
} else
{
double op1 = operands.pop();
double op2 = operands.pop();
switch (currentElem.charAt(0))
{
case '+':
{
operands.push(op2 + op1);
break;
}
case '-':
{
operands.push(op2 - op1);
break;
}
case '*':
{
operands.push(op2 * op1);
break;
}
case '/':
{
operands.push(op2 / op1);
break;
}
case '^':
{
operands.push(Math.pow(op2, op1));
break;
}
}
}
}
return operands.pop();
}

@Override
public String toString()
{
return infixString;
}
}
57 changes: 57 additions & 0 deletions calculus/reimann_sum/java/ReimannSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

public class ReimannSum
{
/** Calculate the area underneath the curve using a right Reimann sum
* @param {double} lowerBound - the lowerBound of the Reimann Sum
* @param {double} upperBound - the upper bound of the Reimann Sum
* @param {double} dx - the change in x, essentially how "small" the rectangle is
* @param {Equation} equation - the equation of the curve
* @return {double} sum - the approximate area underneath the curve of equation from lowerBound to upperBound
*/
public static double rightReimannSum(double lowerBound, double upperBound, int divisions, Equation equation)
{
double sum = 0;
double dx = (upperBound - lowerBound) / divisions;
for(double i = lowerBound + dx; i <= upperBound; i += dx)
{
sum += equation.evaluateEquation(i)*dx;
}
return sum;
}

/** Calculate the area underneath the curve using a left Reimann sum
* @param {double} lowerBound - the lowerBound of the Reimann Sum
* @param {double} upperBound - the upper bound of the Reimann Sum
* @param {double} dx - the change in x, essentially how "small" the rectangle is
* @param {Equation} equation - the equation of the curve
* @return {double} sum - the approximate area underneath the curve of equation from lowerBound to upperBound
*/
public static double leftReimannSum(double lowerBound, double upperBound, int divisions, Equation equation)
{
double sum = 0;
double dx = (upperBound - lowerBound) / divisions;
for(double i = lowerBound; i < upperBound; i += dx)
{
sum += equation.evaluateEquation(i)*dx;
}
return sum;
}

/** Calculate the area underneath the curve using a midpoint Reimann sum
* @param {double} lowerBound - the lowerBound of the Reimann Sum
* @param {double} upperBound - the upper bound of the Reimann Sum
* @param {double} dx - the change in x, essentially how "small" the rectangle is
* @param {Equation} equation - the equation of the curve
* @return {double} sum - the approximate area underneath the curve of equation from lowerBound to upperBound
*/
public static double midpointReimannSum(double lowerBound, double upperBound, int divisions, Equation equation)
{
double sum = 0;
double dx = (upperBound - lowerBound) / divisions;
for(double i = lowerBound + dx/2; i <= upperBound; i += dx)
{
sum += equation.evaluateEquation(i)*dx;
}
return sum;
}
}