@@ -16,7 +16,7 @@ public class ExpressionUtils {
16
16
private static final String DELETE_NON_VARIABLES = "[\\ d.+\\ -/*()^]+" ;
17
17
public static final String MATH_SIGNS = "+-*/^()" ;
18
18
public static final String NUMBERS = "0123456789" ;
19
- // 'E' deliberately missing
19
+ // 'E' deliberately missing, because it corresponds to "*10^x"
20
20
public static final String VARIABLES = "abcdefghijklmnopqrstuvwxyzABCDFGHIJKLMNOPQRSTUVWXYZ" ;
21
21
public static final String TRIGONOMETRY_SHORTCUTS = "@#§" ;
22
22
public static List <String > toMonomials (String self ) {
@@ -50,84 +50,6 @@ public static List<String> toMonomials(String self) {
50
50
return monomials ;
51
51
}
52
52
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
-
131
53
public static String toVariables (String exp ) {
132
54
Pattern pattern = Pattern .compile ("(([a-zA-DF-Z]\\ ^(\\ (.+\\ )|([0-9.]+)|[a-zA-DF-Z]))|[a-zA-DF-Z])+" );
133
55
Matcher matcher = pattern .matcher (exp );
@@ -232,34 +154,6 @@ public static SequenceCalculationResult groupAfter(int index, String exp) {
232
154
LOGGER .debug ("Result of group at index {} for {} : {}" , index , exp , result .sequence );
233
155
return result ;
234
156
}
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
- }
263
157
264
158
public static SequenceCalculationResult bracketSequenceAfter (int index , String exp ) {
265
159
@@ -290,46 +184,6 @@ public static SequenceCalculationResult bracketSequenceAfter(int index, String e
290
184
return result ;
291
185
}
292
186
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
-
333
187
public static String keepEachCharacterOnce (String self ) {
334
188
List <String > chars = new ArrayList <>();
335
189
for (char c : self .toCharArray ()) {
0 commit comments