Skip to content

Commit

Permalink
fixed bugs with high powered expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tran-Antoine committed Mar 7, 2019
1 parent 1732384 commit b2d2f5a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/main/java/net/akami/mask/operation/Multiplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ protected String operate(String a, String b) {
List<String> bMonomials = ExpressionUtils.toMonomials(b);
for(String part : aMonomials)
for(String part2 : bMonomials)
LOGGER.info("To perform : {} times {}", part, part2);
LOGGER.info("Monomials of a : {}", aMonomials);
LOGGER.info("Monomials of b : {}", bMonomials);
LOGGER.error("To perform : {} times {}", part, part2);
LOGGER.error("Monomials of a : {}", aMonomials);
LOGGER.error("Monomials of b : {}", bMonomials);
// We can't use the constant BUILDER, because it is cleared repeatedly inside the loop
StringBuilder builder = new StringBuilder();

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

Expand All @@ -67,13 +67,13 @@ public String simpleMult(String a, String b) {

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

BigDecimal aValue = new BigDecimal(a);
BigDecimal bValue = new BigDecimal(b);
String floatResult = MathUtils.cutSignificantZero(aValue.multiply(bValue, MathContext.DECIMAL64).toString());
String floatResult = MathUtils.cutSignificantZero(aValue.multiply(bValue, MathContext.DECIMAL128).toString());
if (MathUtils.roundPeriodicSeries(floatResult).equals("1")) {
if (!originalVars.isEmpty()) {
return originalVars;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/net/akami/mask/utils/ExpressionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ public static String toVariables(String exp) {
LOGGER.debug("Exponent of {} is now {}", c, variables.get(c));
} else {
String cutPart = result.substring(i+2);
Pattern cutPattern = Pattern.compile("([a-zA-DF-Z0-9]+)|\\(.+\\)");
Pattern cutPattern = Pattern.compile("(1[a-zA-DF-Z])|([a-zA-DF-Z])|([0-9]+)|\\(.+\\)");
Matcher cutMatcher = cutPattern.matcher(cutPart);
String group = null;
if(cutMatcher.find())
group = cutMatcher.group();

LOGGER.debug("Group after {} is : {}", c, group);
LOGGER.debug("--> Result was previously {}", result);
LOGGER.error("Group after {} is : {}", c, group);
LOGGER.error("--> Result was previously {}", result);
result = result.replaceFirst(Pattern.quote(group), group.replaceAll(".", "_"));
LOGGER.debug("--> Result is now : {}", result);
LOGGER.error("--> Result is now : {}", result);
String sumResult = MathUtils.sum(variables.get(c), group);
sumResult = sumResult.matches("[\\da-zA-Z.]+") ? sumResult : "(" + sumResult + ")";
variables.put(c, sumResult);
Expand Down Expand Up @@ -597,7 +597,9 @@ public static String toNumericValue(String exp) {
if(exp.isEmpty())
return "0";
exp = ExpressionUtils.addMultShortcut(exp)
// deletes trigonometry stuff
.replaceAll("\\(.+\\)([@#§])", "")
// deletes variables + their pow value
.replaceAll("[a-zA-DF-Z]\\^(([a-zA-DF-Z0-9^]+)|\\((.+\\)))", "")
.replaceAll("(\\*[a-zA-DF-Z])|[a-zA-DF-Z]", "")
.replaceAll("\\s", "");
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/net/akami/mask/utils/ExpressionUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class ExpressionUtilsTest {

@Test
public void numericValueOfTest() {
Assertions.assertThat(toNumericValue("5.123E10x")).isEqualTo("5.123E10");
Assertions.assertThat(toNumericValue("5x")).isEqualTo("5");
Assertions.assertThat(toNumericValue("0.3xyz")).isEqualTo("0.3");
Assertions.assertThat(toNumericValue("x^2")).isEqualTo("1");
Expand All @@ -33,6 +34,7 @@ public void toMonomialsTest() {

@Test
public void toVariablesTest() {
Assertions.assertThat(toVariables("x^11x")).isEqualTo("x^12");
Assertions.assertThat(toVariables("5x^2y")).isEqualTo("x^2y");
Assertions.assertThat(toVariables("2x")).isEqualTo("x");
Assertions.assertThat(toVariables("x*x")).isEqualTo("x^2");
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/net/akami/mask/utils/MathUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void sumTest() {

@Test
public void multTest() {
Assertions.assertThat(MathUtils.mult("x^11","x")).isEqualTo("x^12");
Assertions.assertThat(MathUtils.mult("x^11","x+2")).isEqualTo("x^12+2x^11");
Assertions.assertThat(MathUtils.mult("x^(y^2)","x^y")).isEqualTo("x^(y^2+y)");
Assertions.assertThat(MathUtils.mult("x^2+2xy+2xz+y^2+2yz+z^2", "x+y+z")).isEqualTo("x^3+3x^2y+3x^2z+3xy^2+6xyz+3xz^2+y^3+3y^2z+3yz^2+z^3");
Assertions.assertThat(MathUtils.mult("3","x")).isEqualTo("3x");
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<Loggers>
# put DEBUG for debug
<Root level="ERROR">
<Root level="OFF">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="STDERR"/>
</Root>
Expand Down

0 comments on commit b2d2f5a

Please sign in to comment.