-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDMASSolver.cs
More file actions
458 lines (412 loc) · 14.5 KB
/
DMASSolver.cs
File metadata and controls
458 lines (412 loc) · 14.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
using System;
using System.Collections.Generic;
using MathParser.DataTypes;
using MathParser.DataTypes.DynamicDataTypes;
namespace MathParser
{
/// <summary>
/// DMAS solver.
/// This class deals with the solving of the mathematical expression.
/// it follows the CBODMAS Rule of the mathmatics.
/// Command.
/// Brakket.
/// Order.
/// Division.
/// Multiplication.
/// Addition.
/// Subtraction.
/// </summary>
public class DMASSolver : ISolver
{
List<string> Expressions;
Dictionary<string, MathParserExpression> theData;
MathParserExpression solution;
bool Processed = true;
public MathParserExpression getSolution ()
{
return solution;
}
public bool isProcessed() => Processed;
public DMASSolver() { }
public DMASSolver(List<string> theExpression, Dictionary<string, MathParserExpression> TheData)
{
Expressions = theExpression;
theData = TheData;
}
public void Solve()
{
if (Expressions.Count == 0) {
Processed = false;
throw new MathParserException ("No Data to processes");
}
else if (Expressions.Count == 1)
{
if (Expressions[0].Contains("-"))
{
MathParserExpression hold = theData[Expressions[0].Trim("-".ToCharArray())];
if (hold.Type.Contains("Number"))
{
Number ans = new Number((double)-1) * hold.Data;
solution = new MathParserExpression(ans);
}
else if (hold.Type.Contains("Matrix"))
{
Matrix ans = -1 * hold.Data;
solution = new MathParserExpression(ans);
}
}
else {
solution = theData[Expressions[0].Trim("-".ToCharArray())];
}
}
else
{
if(Unit_Intake_Command()){
if(Order()){
if(Division ()){
if (Multiplication ()) {
if (Addition ()) {
solution = new MathParserExpression (theData [Expressions [0].Trim ("-".ToCharArray ())]);
}
}
}
}
}
}
}
public delegate bool OperatorActionDelegate (MathParserExpression lhs, MathParserExpression rhs, Dictionary<string,MathParserExpression>theData, List<string>ExpressionList, string AnswerName, int currentPos);
public static OperatorActionDelegate onAddition; // Delegate for extending the addition operation.
bool Addition()
{
bool done = true;
if (Expressions.Contains ("+"))
{
for (int c = 0; c < Expressions.Count; c++)
{
if (Expressions[c] == "+")
{
MathParserExpression rhs = theData[Expressions[c + 1].Trim("-".ToCharArray())];
MathParserExpression lhs = theData[Expressions[c - 1].Trim("-".ToCharArray())];
if (rhs.Type == lhs.Type) {
if (rhs.Type.Contains ("Number")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * new Number (-1));
Expressions [c - 1].Replace ("-", "");
}
Number ans = rhs.Data + lhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else if (rhs.Type.Contains ("Matrix")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression ((Matrix)rhs.Data * -1);
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression ((Matrix)lhs.Data * -1);
Expressions [c - 1].Replace ("-", "");
}
Matrix mrhs = rhs.Data;
Matrix mlhs = lhs.Data;
if (mrhs.Rows == mlhs.Rows && mrhs.Columns == mlhs.Columns) {
Matrix ans = rhs.Data + lhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else {
Processed = false;
done = false;
throw new MathParserException ("Invalid row and column matrix addition.");
}
} else {
if (onAddition != null && onAddition (lhs, rhs, theData, Expressions, autoNamer (),c)) {
} else {
Processed = false;
done = false;
throw new MathParserException ("Invalid data types addition.");
}
}
} else {
if (onAddition != null && onAddition (lhs, rhs, theData, Expressions, autoNamer (),c)) {
} else {
Processed = false;
done = false;
throw new MathParserException ("Invalid data types addition.");
}
}
Expressions.RemoveRange(c,2);
c = 0;
} // end if
} // end for loop.
}
return done;
} // The function deals with the addition operations.
public static OperatorActionDelegate onMuliplication;
bool Multiplication()
{
bool done = true;
string multiply_cross = new string (((char)215), 1);
if (Expressions.Contains ("*") || Expressions.Contains (multiply_cross)){
for (int c = 0; c < Expressions.Count; c++)
{
if (Expressions[c] == "*" || Expressions[c] == multiply_cross)
{
MathParserExpression rhs = theData[Expressions[c + 1].Trim("-".ToCharArray())
];
MathParserExpression lhs = theData[Expressions[c - 1].Trim("-".ToCharArray())];
if (rhs.Type == lhs.Type) {
if (rhs.Type.Contains ("Number")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * new Number (-1));
Expressions [c - 1].Replace ("-", "");
}
Number ans = lhs.Data * rhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else if (rhs.Type.Contains ("Matrix")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * -1);
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * -1);
Expressions [c - 1].Replace ("-", "");
}
Matrix mrhs = rhs.Data;
Matrix mlhs = lhs.Data;
if (mrhs.Rows == mlhs.Columns) {
Matrix ans = lhs.Data * rhs.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else {
Processed = false;
done = false;
throw new MathParserException ("Invalid matrix multiplication.");
}
} else if (onMuliplication != null && onMuliplication (lhs, rhs, theData, Expressions, autoNamer (),c)) {
} else {
Processed = false;
done = false;
throw new MathParserException ("Invalid multiplication.");
}
} else if (lhs.Type.Contains ("Number") && rhs.Type.Contains ("Matrix")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * -1);
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * new Number(-1));
Expressions [c - 1].Replace ("-", "");
}
Number l = lhs.Data;
Matrix r = rhs.Data;
Matrix ans = (double)l.Data * r;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else if (rhs.Type.Contains ("Number") && lhs.Type.Contains ("Matrix")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number(-1));
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * -1);
Expressions [c - 1].Replace ("-", "");
}
Number r = rhs.Data;
Matrix l = lhs.Data;
Matrix ans = l * (double)r.Data;
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else {
if (onMuliplication != null && onMuliplication (lhs, rhs, theData, Expressions, autoNamer (),c)) {
} else {
Processed = false;
done = false;
throw new MathParserException ("Not a valid multiplication.");
}
}
Expressions.RemoveRange(c, 2);
c = 0;
} // end if
} // end for loop.
}
return done;
} // The function deals with the multiplication operations.
public static OperatorActionDelegate onDivision;
bool Division()
{
bool done = true;
if (Expressions.Contains ("÷") || Expressions.Contains ("/")) {
for (int c = 0; c < Expressions.Count; c++) {
string Element = Expressions [c];
if (Element == "/" || Element == "÷") {
MathParserExpression lhs = theData [Expressions [c - 1].Trim ("-".ToCharArray ())];
MathParserExpression rhs = theData [Expressions [c + 1].Trim ("-".ToCharArray ())];
if (lhs.Type.Contains ("Number") && rhs.Type.Contains ("Number")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number(-1));
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * new Number(-1));
Expressions [c - 1].Replace ("-", "");
}
Number l = lhs.Data;
Number r = rhs.Data;
Number ans = new Number ((double)l.Data /
(double)r.Data);
string name = autoNamer ();
theData.Add (name, new MathParserExpression (ans));
Expressions [c - 1] = name;
} else if (lhs.Type.Contains ("Matrix") && rhs.Type.Contains ("Number")) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number(-1));
Expressions [c + 1].Replace ("-", "");
}
if (Expressions [c - 1].Contains ("-")) {
lhs = new MathParserExpression (lhs.Data * -1);
Expressions [c - 1].Replace ("-", "");
}
Matrix l = lhs.Data;
Number r = rhs.Data;
Matrix ans = l / ((double)r.Data);
string name = autoNamer ();
theData.Add (name, new MathParserExpression (ans));
Expressions [c - 1] = name;
} else {
if (onDivision != null && onDivision (lhs, rhs, theData, Expressions, autoNamer (), c)) {
} else {
Processed = false;
done = false;
throw new MathParserException ("Invalid Division.");
}
}
Expressions.RemoveRange (c,2);
c = 0;
}
}
}
return done;
} // The function deals with the division operation.
public static OperatorActionDelegate onOrder;
bool Order()
{
bool done = true;
if (Expressions.Contains ("^")) {
for (int c = Expressions.Count-1; c >=0; c--) {
string Element = Expressions [c];
if (Element == "^") {
MathParserExpression lhs = theData [Expressions [c - 1].Trim ("-".ToCharArray ())];
MathParserExpression rhs = theData [Expressions [c + 1].Trim ("-".ToCharArray ())];
if (lhs.Type.Contains ("Number") && rhs.Type.Contains ("Number")) {
if (lhs.Data.Data == (double)Math.E) {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
Number ans = new Number (Math.Exp ((double)((Number)rhs.Data).Data));
if (Expressions [c - 1].Contains ("-")) {
ans = ans * new Number (-1);
}
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
} else {
if (Expressions [c + 1].Contains ("-")) {
rhs = new MathParserExpression (rhs.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
Number ans = new Number (Math.Pow ((double)((Number)lhs.Data).Data, (double)((Number)rhs.Data).Data));
if (Expressions [c - 1].Contains ("-")) {
ans = ans * new Number (-1);
}
string name = autoNamer ();
Expressions [c - 1] = name;
theData.Add (name, new MathParserExpression (ans));
}
} else {
if (onOrder != null && onOrder (lhs, rhs, theData, Expressions, autoNamer (), c)) {
} else {
Processed = false;
done = false;
throw new MathParserException ("Invalid base and exponent.");
}
}
Expressions.RemoveRange (c,2);
c = Expressions.Count;
}
}
}
return done;
} // The function deals with the Order/power operation.
bool Unit_Intake_Command() // For the command like sin, cos, tan
{
bool done = true;
if (Expressions.Contains ("`")) {
for (int c = Expressions.Count-1; c >= 0; c--) {
string Element = Expressions [c];
if (Element == "`") {
MathParserExpression RHS = theData [Expressions [c + 1].Trim ("-".ToCharArray ())];
if (Expressions [c + 1].Contains ("-")) {
if (RHS.Type.Contains ("Number")) {
RHS= new MathParserExpression((Number)RHS.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
if (RHS.Type.Contains ("Matrix")) {
RHS = new MathParserExpression((Matrix)RHS.Data * -1);
Expressions [c + 1].Replace ("-", "");
}
}
UnitFunctionSolver ufs = new UnitFunctionSolver (Expressions [c - 1].Trim ('-'), RHS);
if (ufs.isProcessed ()) {
string name = autoNamer ();
MathParserExpression ans = ufs.getSolution ();
if (Expressions [c - 1].Contains ("-")) {
if (ans.Type.Contains ("Number")) {
ans= new MathParserExpression((Number)ans.Data * new Number (-1));
Expressions [c + 1].Replace ("-", "");
}
if (ans.Type.Contains ("Matrix")) {
ans = new MathParserExpression((Matrix)ans.Data * -1);
Expressions [c + 1].Replace ("-", "");
}
}
theData.Add (name, ans);
Expressions [c - 1] = name;
} else {
Processed = false;
done = false;
break;
}
Expressions.RemoveRange (c,2);
c = Expressions.Count;
}
}
}
return done;
}
int NamingInteger = 0;
string autoNamer()
{
NamingInteger++;
string name = "DMASSOLVER_shity_sol___221209_99" + NamingInteger.ToString ();
if (theData.ContainsKey (name)) {
return autoNamer ();
} else {
return name;
}
}
} // end class DMASSolver.
}