-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBigMathExpression.java
More file actions
353 lines (306 loc) · 17.5 KB
/
BigMathExpression.java
File metadata and controls
353 lines (306 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright 2023 gbemirojiboye.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parser;
import java.util.*;
import java.math.*;
import static parser.Variable.*;
import static parser.Number.*;
import static parser.Operator.*;
import static parser.methods.Method.*;
import util.FunctionManager;
/**
* Base class for handling calculations in BigDecimal
* @author gbemirojiboye
*/
public class BigMathExpression extends MathExpression {
public BigMathExpression(String expression) throws InputMismatchException {
super(expression);
if (isHasInbuiltFunctions() || isHasLogicOperators() || isHasListReturningOperators()
|| isHasPreNumberOperators() || isHasNumberReturningNonUserDefinedFunctions()
|| isHasPermOrCombOperators() || isHasRemainderOperators() || isHasPostNumberOperators()) {
setCorrectFunction(false);
throw new InputMismatchException("Functions not supported yet!");
} // end if
// Check if the user defined functions do not contain any function whose bigmath
// operation is yet undefined by ParserNG
if(recursiveHasContrabandFunction(this)){
setCorrectFunction(false);
throw new InputMismatchException("User defined function calls unsupported inbuilt function!");
}
}
/**
* This function scans a MathExpression for functions that ParserNG cannot
* evaluate using big math yet.
* At the moment, this includes all functions that are not simple algebraic
* functions.
*
* @return true if it finds any contraband function
*/
private boolean recursiveHasContrabandFunction(MathExpression expression) {
// Check if the user defined functions do not contain any function whose big math
// operation is yet undefined by ParserNG
if (expression.hasFunctions) {
int sz = scanner.size();
for (int i = 0; i < sz; i++) {
String token = scanner.get(i);
if (i + 1 < sz) {
String nextToken = scanner.get(i + 1);
if (isOpeningBracket(nextToken)) {
if(isInBuiltMethod(token)){
return true;//contraband found
}
Function f = FunctionManager.getFunction(token);
if (f != null) {
if(f.getType() != Function.ALGEBRAIC){
return true;//contraband found
}
MathExpression me = f.getMathExpression();
if(me.hasInbuiltFunctions){
return true;
}else if(me.hasUserDefinedFunctions){
return recursiveHasContrabandFunction(me);
}
}
}
}
} // end for
}
return false;//Yay, no contraband function found
}
/**
* used by the main parser solve to figure out SBP portions of a
* multi-bracketed expression (MBP)
*
* @param list a list of scanner tokens of a maths expression
* @return the solution to a SBP maths expression
*/
@Override
protected List<String> solve(List<String> list) {
//correct the anomaly: [ (,-,number....,) ]
// turn it into: [ (,,-number........,) ]
//The double commas show that there exists an empty location in between the 2 commas
if (list.get(0).equals("(") && list.get(1).equals(Operator.MINUS) && isNumber(list.get(2))) {
list.set(1, "");
//if the number is negative,make it positive
if (list.get(2).substring(0, 1).equals(Operator.MINUS)) {
list.set(2, list.get(2).substring(1));
} //if the number is positive,make it negative
else {
list.set(2, Operator.MINUS + list.get(2));
}
}
//Create a collection to serve as a garbage collector for the empty memory
//locations and other unwanted locations created in the processing collection
ArrayList<String> real = new ArrayList<String>();
//insert an empty string in it so that we can use it to remove empty spaces from the processing collection.
real.add("");
real.add("(");
real.add(")");
list.removeAll(real);
if (isHasPowerOperators()) {
/*Deals with powers.Handles the primary power operator e.g in 3^sin3^4.This is necessary at this stage to dis-allow operations like sinA^Bfrom giving the result:(sinA)^B
instead of sin(A^B).
Also instructs the software to multiply any 2 numbers in consecutive positions in the vector.
This is important in distinguishing between functions such as sinAB and sinA*B.Note:sinAB=sin(A*B),while sinA*B=B*sinA.
*/
for (int i = 0; i < list.size(); i++) {
try {
if (!list.get(i - 1).equals("Infinity") && !list.get(i + 1).equals("Infinity")) {
if (list.get(i).equals("^") && isNumber(list.get(i - 1)) && isNumber(list.get(i + 1))) {
BigDecimal lhs = new BigDecimal(list.get(i - 1));
String rhs = list.get(i + 1);
list.set(i + 1, String.valueOf(lhs.pow(Integer.valueOf(rhs), MathContext.DECIMAL128)));
list.set(i - 1, "");
list.set(i, "");
}//end if
}//end if
else if (list.get(i - 1).equals("Infinity") && !list.get(i + 1).equals("Infinity")) {
if (Double.valueOf(list.get(i + 1)) > 1) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i + 1)) == 1) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i + 1)) < 1 && Double.valueOf(list.get(i + 1)) > 0) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i + 1)) < 1 && Double.valueOf(list.get(i + 1)) == 0) {
list.set(i + 1, "1.0");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i + 1)) < 1 && Double.valueOf(list.get(i + 1)) < 0) {
list.set(i + 1, "0.0");
list.set(i - 1, "");
list.set(i, "");
}
} else if (!list.get(i - 1).equals("Infinity") && list.get(i + 1).equals("Infinity")) {
if (Double.valueOf(list.get(i - 1)) > 1) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i - 1)) == 1) {
list.set(i + 1, "1.0");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i - 1)) < 1 && Double.valueOf(list.get(i - 1)) > 0) {
list.set(i + 1, "0.0");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i - 1)) < 1 && Double.valueOf(list.get(i - 1)) == 0) {
list.set(i + 1, "0.0");
list.set(i - 1, "");
list.set(i, "");
} else if (Double.valueOf(list.get(i - 1)) < 1 && Double.valueOf(list.get(i - 1)) < 0) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
}//end else if
}//end else if
else if (list.get(i - 1).equals("Infinity") && list.get(i + 1).equals("Infinity")) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
}
}//end try
catch (NumberFormatException numerror) {
} catch (NullPointerException nullerror) {
} catch (IndexOutOfBoundsException inderror) {
}
}//end for
list.removeAll(real);
}//end if
list.removeAll(real);
boolean skip = false;
if (isHasMulOrDivOperators()) {
for (int i = 0; i < list.size(); i++) {
try {
if (list.get(i).equals(Operator.MULTIPLY)) {
if (!list.get(i - 1).equals("Infinity") && !list.get(i + 1).equals("Infinity")) {
BigDecimal lhs = new BigDecimal(list.get(i - 1));
BigDecimal rhs = new BigDecimal(list.get(i + 1));
list.set(i + 1, String.valueOf(lhs.multiply(rhs, MathContext.DECIMAL128)));
list.set(i - 1, "");
list.set(i, "");
skip = true;
} else if (list.get(i - 1).equals("Infinity") && !list.get(i + 1).equals("Infinity")) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
skip = true;
} else if (!list.get(i - 1).equals("Infinity") && list.get(i + 1).equals("Infinity")) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
skip = true;
} else if (list.get(i - 1).equals("Infinity") && list.get(i + 1).equals("Infinity")) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
skip = true;
}
}//end if
else if (list.get(i).equals(Operator.DIVIDE)) {
if (!list.get(i - 1).equals("Infinity") && !list.get(i + 1).equals("Infinity")) {
BigDecimal lhs = new BigDecimal(list.get(i - 1));
BigDecimal rhs = new BigDecimal(list.get(i + 1));
list.set(i + 1, String.valueOf(lhs.divide(rhs, MathContext.DECIMAL128)));
list.set(i - 1, "");
list.set(i, "");
skip = true;
} else if (list.get(i - 1).equals("Infinity") && !list.get(i + 1).equals("Infinity")) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
skip = true;
} else if (!list.get(i - 1).equals("Infinity") && list.get(i + 1).equals("Infinity")) {
list.set(i + 1, "0.0");
list.set(i - 1, "");
list.set(i, "");
skip = true;
} else if (list.get(i - 1).equals("Infinity") && list.get(i + 1).equals("Infinity")) {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
skip = true;
}
}//end else if
}//end try
catch (NullPointerException nolan) {
}//end catch
catch (NumberFormatException numerr) {
}//end catch
catch (IndexOutOfBoundsException inderr) {
}//end catch
}//end for
list.removeAll(real);
}//end if
if (isHasPlusOrMinusOperators()) {
//Handles the subtraction and addition operators
for (int i = 0; i < list.size(); i++) {
try {
if (list.get(i).equals(Operator.PLUS) || list.get(i).equals(Operator.MINUS)) {
if (!list.get(i - 1).equals("Infinity") && !list.get(i + 1).equals("Infinity")) {
if (list.get(i).equals(Operator.PLUS) && isNumber(list.get(i - 1)) && isNumber(list.get(i + 1))) {
BigDecimal lhs = new BigDecimal(list.get(i - 1));
BigDecimal rhs = new BigDecimal(list.get(i + 1));
list.set(i + 1, String.valueOf(lhs.add(rhs, MathContext.DECIMAL128)));
list.set(i - 1, "");
list.set(i, "");
}//end else
else if (list.get(i).equals(Operator.MINUS) && isNumber(list.get(i - 1)) && isNumber(list.get(i + 1))) {
BigDecimal lhs = new BigDecimal(list.get(i - 1));
BigDecimal rhs = new BigDecimal(list.get(i + 1));
list.set(i + 1, String.valueOf(lhs.subtract(rhs, MathContext.DECIMAL128)));
list.set(i - 1, "");
list.set(i, "");
}//end else if
}//end if
else {
list.set(i + 1, "Infinity");
list.set(i - 1, "");
list.set(i, "");
}
}
}//end try
catch (NullPointerException nolerr) {
}//end catch
catch (NumberFormatException numerr) {
}//end catch
catch (IndexOutOfBoundsException inderr) {
}//end catch
}//end for
}//end if
real.add("(");
real.add(")");
list.removeAll(real);
if (list.size() != 1) {
this.correctFunction = false;
}//end if
//Now de-list or un-package the input.If all goes well the list should have only its first memory location occupied.
return list;
}//end method solve
public static void main(String[] args) {
MathExpression me = new MathExpression("a,v,d=2/3;b=3;f=3ab;p(x)=x^3+5*x^2-4*x+1;p(9);");
System.out.println(me.solve());
BigMathExpression bme = new BigMathExpression("x=2;h(x)=5*x^2+x+2*x-sin(x);h(3);");
System.out.println(bme.solve());
MathExpression bm = new MathExpression("x=9;f(x)=3*x^2+sin(x^2);f(6)<3");
System.out.println(bm.solve());
}
}