Skip to content

Commit

Permalink
finished derivatives + fixed division bugs for the release
Browse files Browse the repository at this point in the history
  • Loading branch information
Tran-Antoine committed Mar 21, 2019
1 parent 2d83114 commit 6e63527
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
import java.util.List;
import java.util.Map;

public class Sum extends BinaryOperationHandler {
public class Adder extends BinaryOperationHandler {

private static final Sum INSTANCE = new Sum();
private static final Adder INSTANCE = new Adder();

@Override
public String operate(String a, String b) {
LOGGER.info("Sum process of {} |+| {}: \n", a, b);
LOGGER.info("Adder process of {} |+| {}: \n", a, b);
List<String> monomials = ExpressionUtils.toMonomials(a);
monomials.addAll(ExpressionUtils.toMonomials(b));
LOGGER.info("Monomials : {}", monomials);
String result = monomialSum(monomials, false);
LOGGER.info("---> Sum result of {} |+| {}: {}", a, b, result);
LOGGER.info("---> Adder result of {} |+| {}: {}", a, b, result);
return result;
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public String outFormat(String origin) {
return FormatterFactory.removeMultiplicationSigns(origin);
}

public static Sum getInstance() {
public static Adder getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
/**
* Does not work yet
*/
public class Derivative {
public class Differentiator {

private static final Derivative INSTANCE = new Derivative();
private static final Differentiator INSTANCE = new Differentiator();
private static final QuaternaryOperationSign[] PROCEDURAL_OPERATIONS;
private static final Logger LOGGER = LoggerFactory.getLogger(Derivative.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Differentiator.class);

static {
PROCEDURAL_OPERATIONS = new QuaternaryOperationSign[]{
Expand Down Expand Up @@ -84,10 +84,11 @@ public static String differentiateElement(String element) {
if(element.length() == 1)
return "1";

return MathUtils.diffPow(String.valueOf(element.charAt(0)), null, element.substring(2), null);
String[] parts = element.split("\\^", 2);
return MathUtils.diffPow(parts[0], differentiateElement(parts[0]), parts[1], null);
}

public static Derivative getInstance() {
public static Differentiator getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@
import java.math.MathContext;
import java.util.List;

public class Division extends BinaryOperationHandler {
public class Divider extends BinaryOperationHandler {

private static final Division INSTANCE = new Division();
private static final Divider INSTANCE = new Divider();
private static final MathContext CONTEXT = new MathContext(120);

@Override
protected String operate(String a, String b) {
LOGGER.info("Divider process of {} |/| {}: \n", a, b);

// Avoids division by zero error.
if(a.equals(b))
return "1";

LOGGER.info("Division process of {} |/| {}: \n", a, b);

if (ExpressionUtils.isANumber(a) && ExpressionUtils.isANumber(b)) {
return numericalDivision(a, b);
}

String unsignedB = b.substring(1);
if(unsignedB.contains("+") || unsignedB.contains("-")) {
LOGGER.error("Unable to calculate a division, the denominator being a polynomial. Returns a/b");
return a+"/"+b;
}

List<String> numMonomials = ExpressionUtils.toMonomials(a);
int index = 0;
for(String numMonomial : numMonomials) {
Expand Down Expand Up @@ -131,7 +137,7 @@ private boolean proceedForVarDivision(String num, String den, int i, int j, List
nPow = nPow.isEmpty() ? "1" : nPow;
dPow = dPow.isEmpty() ? "1" : dPow;

String subResult = Subtraction.getInstance().rawOperate(nPow, dPow);
String subResult = Subtractor.getInstance().rawOperate(nPow, dPow);

if(ExpressionUtils.isANumber(subResult)) {
float subNumericResult = Float.parseFloat(subResult);
Expand All @@ -153,7 +159,7 @@ private String assembleFactors(List<String> factors) {
BUILDER.append(1);
for(String factor : factors) {
if(factor != null && !factor.equals("1") && !factor.equals("1.0")) {
BUILDER.replace(0, BUILDER.length(), Multiplication.getInstance().simpleMult(BUILDER.toString(), factor));
BUILDER.replace(0, BUILDER.length(), Multiplicator.getInstance().simpleMult(BUILDER.toString(), factor));
}
}
return BUILDER.toString();
Expand Down Expand Up @@ -197,7 +203,7 @@ public String outFormat(String origin) {
return FormatterFactory.removeMultiplicationSigns(origin);
}

public static Division getInstance() {
public static Divider getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import java.math.MathContext;
import java.util.List;

public class Multiplication extends BinaryOperationHandler {
public class Multiplicator extends BinaryOperationHandler {

private static final Multiplication INSTANCE = new Multiplication();
private static final Multiplicator INSTANCE = new Multiplicator();
private static final MathContext CONTEXT = new MathContext(120);
@Override
protected String operate(String a, String b) {
Expand All @@ -23,7 +23,7 @@ protected String operate(String a, String b) {
LOGGER.info("Trigonometry result : {}", result);
return result;
}
LOGGER.info("Multiplication process of {} |*| {}: \n", a, b);
LOGGER.info("Multiplicator process of {} |*| {}: \n", a, b);

List<String> aMonomials = ExpressionUtils.toMonomials(a);
List<String> bMonomials = ExpressionUtils.toMonomials(b);
Expand All @@ -33,7 +33,7 @@ protected String operate(String a, String b) {

for (String part : aMonomials) {
for (String part2 : bMonomials) {
LOGGER.error("Treating simple mult : {} |*| {}", part, part2);
LOGGER.debug("Treating simple mult : {} |*| {}", part, part2);
String result = simpleMult(part, part2);
LOGGER.error("Result of simple mult between {} and {} : {}", part, part2, result);
boolean first = part.equals(aMonomials.get(0)) && part2.equals(bMonomials.get(0));
Expand All @@ -46,7 +46,7 @@ protected String operate(String a, String b) {
}
String unReducedResult = builder.toString();
LOGGER.info("FINAL RESULT : {}", unReducedResult);
String finalResult = Sum.getInstance().operate(unReducedResult, "");
String finalResult = Adder.getInstance().operate(unReducedResult, "");
LOGGER.error("- Result of mult {} |*| {} : {}", a, b, finalResult);
return finalResult;
}
Expand All @@ -64,7 +64,7 @@ public String simpleMult(String a, String b) {

String concatenated = a + "*" + b;
String originalVars = ExpressionUtils.toVariables(concatenated);
LOGGER.error("Variables of {} and {} : {}", a, b, originalVars);
LOGGER.debug("Variables of {} and {} : {}", a, b, originalVars);
a = ExpressionUtils.toNumericValue(a);
b = ExpressionUtils.toNumericValue(b);

Expand Down Expand Up @@ -104,7 +104,7 @@ public String outFormat(String origin) {
return FormatterFactory.removeMultiplicationSigns(origin);
}

public static Multiplication getInstance() {
public static Multiplicator getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import net.akami.mask.utils.FormatterFactory;
import net.akami.mask.utils.MathUtils;

public class Pow extends BinaryOperationHandler {
public class PowCalculator extends BinaryOperationHandler {

private static final Pow INSTANCE = new Pow();
private static final PowCalculator INSTANCE = new PowCalculator();

@Override
public String operate(String a, String b) {
LOGGER.info("Pow operation process between {} and {} : \n", a, b);
LOGGER.info("PowCalculator operation process between {} and {} : \n", a, b);

String aVars = ExpressionUtils.toVariables(a);
String bVars = ExpressionUtils.toVariables(b);
Expand All @@ -25,7 +25,7 @@ public String operate(String a, String b) {
// If pow value is too high, there is no point in developing the entire expression
if (bVars.length() != 0 || (powValue = Float.parseFloat(b)) > 199 ||
(aVars.length() != 0 && powValue % 1 != 0)) {
LOGGER.info("Pow value contains variables or pow value is greater than 199. Returns a^b");
LOGGER.info("PowCalculator value contains variables or pow value is greater than 199. Returns a^b");
a = ExpressionUtils.isReduced(a) ? a : "(" + a + ")";
b = ExpressionUtils.isReduced(b) ? b : "(" + b + ")";
return a + "^" + b;
Expand All @@ -35,7 +35,7 @@ public String operate(String a, String b) {
StringBuilder builder = new StringBuilder();
builder.append(a);
for (int i = 1; i < powValue; i++) {
builder.replace(0, builder.length(), Multiplication.getInstance().rawOperate(builder.toString(), a));
builder.replace(0, builder.length(), Multiplicator.getInstance().rawOperate(builder.toString(), a));
LOGGER.info("{} steps left. Currently : {}", powValue - i - 1, builder.toString());
}
return builder.toString();
Expand All @@ -51,7 +51,7 @@ public String outFormat(String origin) {
return MathUtils.cutSignificantZero(FormatterFactory.removeMultiplicationSigns(origin));
}

public static Pow getInstance() {
public static PowCalculator getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import java.util.List;

public class Subtraction extends BinaryOperationHandler {
public class Subtractor extends BinaryOperationHandler {

private static final Subtraction INSTANCE = new Subtraction();
private static final Subtractor INSTANCE = new Subtractor();

@Override
protected String operate(String a, String b) {
LOGGER.info("Subtraction process of {} |-| {}: \n", a, b);
LOGGER.info("Subtractor process of {} |-| {}: \n", a, b);

List<String> monomials = ExpressionUtils.toMonomials(a);
List<String> bMonomials = ExpressionUtils.toMonomials(b);
Expand All @@ -29,7 +29,7 @@ protected String operate(String a, String b) {
}
}
monomials.addAll(bMonomials);
return Sum.getInstance().monomialSum(monomials, true);
return Adder.getInstance().monomialSum(monomials, true);
}

@Override
Expand All @@ -42,7 +42,7 @@ public String outFormat(String origin) {
return FormatterFactory.removeMultiplicationSigns(origin);
}

public static Subtraction getInstance() {
public static Subtractor getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public String compute(String a, String b) {
return binaryFunction.compute(a, b);
}


public static BinaryOperationSign getBySign(char sign) {
for(BinaryOperationSign operation : values()) {
if(operation.sign == sign) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/akami/mask/tree/FormatterTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ protected void evalBranch(Branch self) {
if(ExpressionUtils.hasHigherPriority(String.valueOf(self.getOperation()), right))
right = addRequiredBrackets(right);

if(ExpressionUtils.hasHigherPriority(String.valueOf(self.getOperation()), left))
left = addRequiredBrackets(left);
// TODO

char operation = self.getOperation();

if(operation == '*') {
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/net/akami/mask/utils/FormatterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import static net.akami.mask.utils.ExpressionUtils.MATH_SIGNS;
import static net.akami.mask.utils.ExpressionUtils.VARIABLES;
import static net.akami.mask.utils.ExpressionUtils.NUMBERS;

public class FormatterFactory {

Expand Down Expand Up @@ -60,12 +61,6 @@ public static String formatTrigonometry(String origin) {

// TODO : remove 1's in front of variables, remove useless brackets
public static String formatForVisual(String origin) {
/*origin = origin
.replaceAll("\\(\\((.*?)\\)(\\*@|@)\\)", "sin\\($1\\)")
.replaceAll("\\(\\((.*?)\\)(\\*#|#)\\)", "cos\\($1\\)")
.replaceAll("\\(\\((.*?)\\)(\\*§|§)\\)", "tan\\($1\\)");
return removeMultiplicationSigns(origin);*/
BinaryTree tree = new FormatterTree(origin);
return tree.merge();
}
Expand Down Expand Up @@ -108,21 +103,20 @@ public static String addMultiplicationSigns(String self, boolean addForTrigonome
BUILDER.append(c);
}
}
LOGGER.error("Converted : " + self + " to " + BUILDER.toString());
LOGGER.debug("Converted : " + self + " to " + BUILDER.toString());
return BUILDER.toString();
}

public static String addAllCoefficients(String origin) {

clearBuilder();
for(int i = 0; i<origin.length(); i++) {
for(int i = 0; i < origin.length(); i++) {
String current = String.valueOf(origin.charAt(i));
String previous = i == 0 ? "$" : String.valueOf(origin.charAt(i-1));

if(VARIABLES.contains(current) && (ExpressionUtils.MATH_SIGNS.contains(previous) || i== 0)) {
String next = i == origin.length()-1 ? "$" : String.valueOf(origin.charAt(i+1));
if(VARIABLES.contains(current) && (MATH_SIGNS.contains(previous) || i == 0) && !NUMBERS.contains(next)) {
BUILDER.append('1');
}

BUILDER.append(current);
}
return BUILDER.toString();
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/net/akami/mask/utils/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public class MathUtils {
private static final MathContext TRIGO_CONTEXT = new MathContext(4);

public static String sum(String a, String b) {
return Sum.getInstance().rawOperate(a, b);
return Adder.getInstance().rawOperate(a, b);
}
public static String sum(List<String> monomials) {
return Sum.getInstance().monomialSum(monomials, true);
return Adder.getInstance().monomialSum(monomials, true);
}
public static String subtract(String a, String b) {
return Subtraction.getInstance().rawOperate(a, b);
return Subtractor.getInstance().rawOperate(a, b);
}
public static String mult(String a, String b) {
return Multiplication.getInstance().rawOperate(a, b);
return Multiplicator.getInstance().rawOperate(a, b);
}
public static String divide(String a, String b) {
return Division.getInstance().rawOperate(a, b);
return Divider.getInstance().rawOperate(a, b);
}
public static String diffSum(String a, String altA, String b, String altB) {
return sum(altA, altB);
Expand All @@ -53,9 +53,13 @@ else if(!ExpressionUtils.isReduced(subtractResult))
subtractResult = "^("+subtractResult+')';
else
subtractResult = '^'+subtractResult;
b = ExpressionUtils.isReduced(b) || ExpressionUtils.areEdgesBracketsConnected(b, true) ? b+"*" : "("+b+")*";
if(!ExpressionUtils.isReduced(a))

if(!ExpressionUtils.isReduced(a) && !ExpressionUtils.areEdgesBracketsConnected(a, false))
a = '(' + a + ')';
if(!ExpressionUtils.isReduced(b) && !ExpressionUtils.areEdgesBracketsConnected(b, false))
b = '(' + b + ")*";
else
b += '*';

if(altA.equals("1"))
altA = "";
Expand All @@ -78,7 +82,7 @@ public static String breakNumericalFraction(String self) {

// TODO : optimize : use other method to chain multiplications
public static String pow(String a, String b) {
return Pow.getInstance().rawOperate(a, b);
return PowCalculator.getInstance().rawOperate(a, b);
}

public static String sin(String a) {
Expand Down Expand Up @@ -120,7 +124,7 @@ public static String highPow(String a, String b) {
float powValue;

if (bVars.length() != 0 || (powValue = Float.parseFloat(b)) > 199) {
LOGGER.info("Pow value contains variables or pow value is greater than 9. Returns a^b");
LOGGER.info("PowCalculator value contains variables or pow value is greater than 9. Returns a^b");
return a + "^" + (ExpressionUtils.isReduced(b) ? b : "(" + b + ")");
}

Expand Down
Loading

0 comments on commit 6e63527

Please sign in to comment.