Skip to content

Commit 403d5e1

Browse files
committed
fixed final bugs for release
1 parent b2d2f5a commit 403d5e1

File tree

8 files changed

+11
-194
lines changed

8 files changed

+11
-194
lines changed

Diff for: src/main/java/net/akami/mask/operation/Derivative.java

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
1111

12+
/**
13+
* Does not work yet
14+
*/
1215
public class Derivative {
1316

1417
private static final Derivative INSTANCE = new Derivative();

Diff for: src/main/java/net/akami/mask/operation/Sum.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.HashMap;
1010
import java.util.List;
1111
import java.util.Map;
12-
import java.util.regex.Pattern;
1312

1413
public class Sum extends BinaryOperationHandler {
1514

@@ -90,7 +89,7 @@ public String monomialSum(List<String> monomials, boolean needsFormatting) {
9089
if (rest.startsWith("+") || rest.startsWith("-")) {
9190
BUILDER.append(rest);
9291
} else {
93-
BUILDER.append("+" + rest);
92+
BUILDER.append('+').append(rest);
9493
}
9594
}
9695
String result = BUILDER.toString();
@@ -145,12 +144,6 @@ private void fillMonomialList(String monomial, int i, List<String> initialMonomi
145144
* 2x + 2x + 3x
146145
* -> Compatible unknown part : x. Hence, initialMonomial is 2x, others contains 2x and 3x, it removes
147146
* the three monomials to the initial list, and adds 7x to the final list
148-
* @param initialMonomial
149-
* @param vars
150-
* @param index
151-
* @param others
152-
* @param initialMonomials
153-
* @param finalMonomials
154147
*/
155148
private void replaceMonomialsByResult(String initialMonomial, String vars, int index, Map<BigDecimal, Integer> others,
156149
List<String> initialMonomials, List<String> finalMonomials) {

Diff for: src/main/java/net/akami/mask/utils/ExpressionUtils.java

+1-147
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ExpressionUtils {
1616
private static final String DELETE_NON_VARIABLES = "[\\d.+\\-/*()^]+";
1717
public static final String MATH_SIGNS = "+-*/^()";
1818
public static final String NUMBERS = "0123456789";
19-
// 'E' deliberately missing
19+
// 'E' deliberately missing, because it corresponds to "*10^x"
2020
public static final String VARIABLES = "abcdefghijklmnopqrstuvwxyzABCDFGHIJKLMNOPQRSTUVWXYZ";
2121
public static final String TRIGONOMETRY_SHORTCUTS = "@#§";
2222
public static List<String> toMonomials(String self) {
@@ -50,84 +50,6 @@ public static List<String> toMonomials(String self) {
5050
return monomials;
5151
}
5252

53-
public static String toVariablesUnused(String exp) {
54-
55-
String[] unSortedVars = keepEachCharacterOnce(exp.replaceAll(DELETE_NON_VARIABLES, "")).split("");
56-
if (unSortedVars.length == 0)
57-
return "";
58-
59-
Arrays.sort(unSortedVars);
60-
String vars = String.join("", unSortedVars);
61-
62-
LOGGER.debug("Looking for the variables of the sequence : {}", exp);
63-
List<String> finalVars = new ArrayList<>();
64-
StringBuilder builtExpression = new StringBuilder();
65-
builtExpression.append(exp);
66-
clearBuilder();
67-
// Going through the variables
68-
for (int i = 0; i < vars.length(); i++) {
69-
int partsAmount = 0;
70-
char var = vars.charAt(i);
71-
72-
LOGGER.debug("Treating {}", var);
73-
74-
// can happen if 'x^y', the y has been transformed to '$', meaning it wasn't a variable but an sequence.
75-
if (!builtExpression.toString().contains(String.valueOf(var))) {
76-
LOGGER.debug("Variable {} not found. Skipping.", var);
77-
continue;
78-
}
79-
80-
for (int j = 0; j < builtExpression.length(); j++) {
81-
82-
char c1 = builtExpression.charAt(j);
83-
// The treated variable has been found
84-
if (var == c1) {
85-
partsAmount++;
86-
LOGGER.debug("Identical variable found at index {} : {}", j, c1);
87-
// If now powers after the variable
88-
if (j + 2 > builtExpression.length() || builtExpression.charAt(j + 1) != '^') {
89-
BUILDER.append("+1");
90-
LOGGER.debug("No power found. Adds 1");
91-
} else {
92-
SequenceCalculationResult sequenceResult = groupAfter(j + 1, exp);
93-
String exponent = sequenceResult.sequence;
94-
String valuelessSequence = exponent.replaceAll(".", "\\$");
95-
builtExpression.replace(sequenceResult.start, sequenceResult.end, valuelessSequence);
96-
BUILDER.append("+").append(exponent);
97-
LOGGER.debug("Power found. Adds {}", exponent);
98-
}
99-
builtExpression.setCharAt(j, '$');
100-
LOGGER.debug("Sequence : {}", builtExpression.toString());
101-
LOGGER.debug("Exponent : {}", BUILDER.toString());
102-
}
103-
}
104-
if (BUILDER.charAt(0) == '+')
105-
BUILDER.deleteCharAt(0);
106-
107-
if (partsAmount > 1) {
108-
LOGGER.debug("Several parts forming the power : {}. Needs extra monomialSum", BUILDER.toString());
109-
String reducedExponent = MathUtils.sum(BUILDER.toString(), "");
110-
clearBuilder();
111-
BUILDER.append(reducedExponent);
112-
}
113-
String exponent = BUILDER.toString();
114-
if (!isReduced(exponent)) {
115-
exponent = "(" + exponent + ")";
116-
}
117-
LOGGER.debug("Final sequence calculation of {} : {}", var, exponent);
118-
if (exponent.equals("1")) {
119-
finalVars.add(String.valueOf(var));
120-
} else {
121-
finalVars.add(var + "^" + exponent);
122-
}
123-
clearBuilder();
124-
}
125-
String result = String.join("", finalVars);
126-
127-
LOGGER.debug("Result of toVariables(): {}", result);
128-
return result;
129-
}
130-
13153
public static String toVariables(String exp) {
13254
Pattern pattern = Pattern.compile("(([a-zA-DF-Z]\\^(\\(.+\\)|([0-9.]+)|[a-zA-DF-Z]))|[a-zA-DF-Z])+");
13355
Matcher matcher = pattern.matcher(exp);
@@ -232,34 +154,6 @@ public static SequenceCalculationResult groupAfter(int index, String exp) {
232154
LOGGER.debug("Result of group at index {} for {} : {}", index, exp, result.sequence);
233155
return result;
234156
}
235-
public static SequenceCalculationResult groupBefore(int index, String exp) {
236-
if(index > 0 && exp.charAt(index-1) == ')') {
237-
return bracketSequenceBefore(index, exp);
238-
}
239-
LOGGER.debug("No bracket sequence after char at index {}", index);
240-
SequenceCalculationResult result = new SequenceCalculationResult();
241-
for (int i = index - 1; i >= 0; i--) {
242-
char c = exp.charAt(i);
243-
244-
if ("+-*/^".contains(String.valueOf(c))) {
245-
LOGGER.debug("Found an operator : {} at index {}. Stopped", c, i);
246-
result.sequence = exp.substring(i, index);
247-
result.start = i;
248-
result.end = index;
249-
break;
250-
}
251-
252-
if(i == 0) {
253-
LOGGER.debug("Reached the end of the string.");
254-
result.sequence = exp.substring(0, index);
255-
result.start = 0;
256-
result.end = index;
257-
}
258-
}
259-
removeBrackets(result, exp, index, false);
260-
LOGGER.debug("Result of group at index {} for {} : {}", index, exp, result.sequence);
261-
return result;
262-
}
263157

264158
public static SequenceCalculationResult bracketSequenceAfter(int index, String exp) {
265159

@@ -290,46 +184,6 @@ public static SequenceCalculationResult bracketSequenceAfter(int index, String e
290184
return result;
291185
}
292186

293-
public static SequenceCalculationResult bracketSequenceBefore(int index, String exp) {
294-
SequenceCalculationResult result = new SequenceCalculationResult();
295-
296-
int closingBrackets = 0;
297-
for (int i = index - 1; i >= 0; i--) {
298-
char c = exp.charAt(i);
299-
300-
if (c == '(') {
301-
if(closingBrackets <= 0) {
302-
LOGGER.debug("Found the closing bracket");
303-
result.start = i+1;
304-
result.sequence = exp.substring(i+1, index-1);
305-
result.end = index-1;
306-
break;
307-
} else {
308-
closingBrackets--;
309-
}
310-
}
311-
if(c == ')' && i != index+1) {
312-
closingBrackets++;
313-
}
314-
}
315-
316-
//removeBrackets(result, exp, index, false);
317-
LOGGER.debug("Sequence {} after pow at index {} found : {}", exp, index, result.sequence);
318-
return result;
319-
}
320-
321-
private static void removeBrackets(SequenceCalculationResult result, String exp, int index, boolean leftToR) {
322-
if (result.sequence == null) {
323-
LOGGER.debug("Sequence null");
324-
result.sequence = leftToR ? exp.substring(index + 1) : exp.substring(0, index);
325-
result.start = leftToR ? index : 0;
326-
result.end = leftToR ? exp.length() : index;
327-
}
328-
while(areEdgesBracketsConnected(result.sequence)) {
329-
result.sequence = result.sequence.substring(1, result.sequence.length()-1);
330-
}
331-
}
332-
333187
public static String keepEachCharacterOnce(String self) {
334188
List<String> chars = new ArrayList<>();
335189
for (char c : self.toCharArray()) {

Diff for: src/main/java/net/akami/mask/utils/FormatterFactory.java

-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
public class FormatterFactory {
99

10-
private static final StringBuilder BUILDER = new StringBuilder();
1110
private static final Logger LOGGER = LoggerFactory.getLogger(FormatterFactory.class);
1211

1312
public static String removeFractions(String origin) {
@@ -57,8 +56,4 @@ public static String formatForVisual(String origin) {
5756

5857
return ExpressionUtils.addMultShortcut(origin);
5958
}
60-
61-
private static void clearBuilder() {
62-
BUILDER.delete(0, BUILDER.length());
63-
}
6459
}

Diff for: src/main/java/net/akami/mask/utils/MathUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public static String diffDivide(String a, String altA, String b, String altB) {
4848
public static String diffPow(String a, String altA, String b, String altB) {
4949
String subtractResult = subtract(b, "1");
5050
subtractResult = subtractResult.equals("1") ? "" : "^("+subtractResult+")";
51-
return "("+mult(b, a)+")"+subtractResult;
51+
b = ExpressionUtils.isReduced(b) || ExpressionUtils.areEdgesBracketsConnected(b) ? b+"*" : "("+b+")*";
52+
return b+a+subtractResult;
5253
}
5354

5455
public static String breakNumericalFraction(String self) {

Diff for: src/test/java/net/akami/mask/core/MainTester.java

-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package net.akami.mask.core;
22

3-
import net.akami.mask.math.MaskExpression;
4-
import net.akami.mask.operation.MaskOperator;
53
import net.akami.mask.utils.ReducerFactory;
64

75
import java.util.Scanner;
@@ -10,21 +8,6 @@ public class MainTester {
108

119
public static void main(String... args) {
1210

13-
//System.out.println("...............................");
14-
//System.out.println(MathUtils.monomialSum("x^10", ""));
15-
//System.out.println(MathUtils.highPow("x+1", "3"));
16-
//operator.imageFor(MaskExpression.TEMP, false, res -> System.out.println(operator.asInt(res)), "1");
17-
18-
//System.out.println(MathUtils.simpleMult("5*x", "6*x"));
19-
//System.out.println(MathUtils.pow("x+y+3", "3"));
20-
//System.out.println(ReducerFactory.reduce("(x+y+3)^3"));
21-
//System.out.println(MathUtils.pow("x+1", "2"));
22-
//MaskExpression exp = new MaskExpression("3x^2 + 6xy + 3");
23-
//System.out.println(MaskOperator.begin().differentiate(exp, MaskExpression.TEMP, 'x',true).asExpression());
24-
25-
//System.out.println(MathUtils.monomialSum("xx", "x^2"));
26-
//MaskExpression exp = new MaskExpression("5x + 3y");
27-
//System.out.println(MaskOperator.begin(exp).differentiate('y').asExpression());
2811
Scanner sc = new Scanner(System.in);
2912
String expression;
3013

@@ -36,17 +19,5 @@ public static void main(String... args) {
3619
System.out.println("Calculations ended after "+deltaTime+" seconds");
3720
System.out.println("Next expression to reduce : ");
3821
}
39-
40-
//System.out.println(MathUtils.monomialSum(Arrays.asList("5", "-4", "-x", "3y", "4x")));
41-
//System.out.println(MathUtils.subtract("5x", "3+x+2"));
42-
//System.out.println(ReducerFactory.reduce("5+2"));
43-
//System.out.println(ReducerFactory.reduce("3*((x+2y)*2 - 8z)"));
44-
//System.out.println(ReducerFactory.reduce("((1+2)*3)*4"));
45-
//System.out.println(ReducerFactory.reduce("(5x+4x)*7y"));
46-
//System.out.println(ReducerFactory.reduce("(5x+3y)*3"));
47-
//System.out.println(ReducerFactory.reduce("5x*y"));
48-
//for(int i = 0; i<5000; i++)
49-
//System.out.println(ReducerFactory.reduce("((2+7)^(23-19)-((3+9)*10))^3"));
50-
//System.out.println(MathUtils.mult("5x+3y+8z+9w", "2x+3y+12z+4w"));
5122
}
5223
}

Diff for: src/test/java/net/akami/mask/operation/DerivativeTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public void diffElementTest() {
1111
Assertions.assertThat(Derivative.differentiateElement("a")).isEqualTo("1");
1212
Assertions.assertThat(Derivative.differentiateElement("3")).isEqualTo("0");
1313
Assertions.assertThat(Derivative.differentiateElement("-1324")).isEqualTo("0");
14-
Assertions.assertThat(Derivative.differentiateElement("x^2")).isEqualTo("(2x)");
15-
Assertions.assertThat(Derivative.differentiateElement("x^(y+1)")).isEqualTo("(xy+x)^(y)");
14+
Assertions.assertThat(Derivative.differentiateElement("x^2")).isEqualTo("2*x");
15+
Assertions.assertThat(Derivative.differentiateElement("x^(y+1)")).isEqualTo("(y+1)*x^(y)");
1616

1717
}
1818
}

Diff for: src/test/java/net/akami/mask/utils/MathUtilsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void diffDivideTest() {
7272
@Test
7373
public void diffPowTest() {
7474
QuaternaryMathOperation sum = MathUtils::diffPow;
75-
Assertions.assertThat(sum.compute("3x", "3", "3", "0")).isEqualTo("(9x)^(2)");
76-
Assertions.assertThat(sum.compute("-5x", "-5", "x+1", "1")).isEqualTo("(-5x^2-5x)^(x)");
75+
Assertions.assertThat(sum.compute("3x", "3", "3", "0")).isEqualTo("3*3x^(2)");
76+
Assertions.assertThat(sum.compute("-5x", "-5", "x+1", "1")).isEqualTo("(x+1)*-5x^(x)");
7777
}
7878
}

0 commit comments

Comments
 (0)