-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolver.cs
More file actions
346 lines (305 loc) · 9.31 KB
/
Solver.cs
File metadata and controls
346 lines (305 loc) · 9.31 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
using System;
using System.Collections.Generic;
using MathParser.DataTypes;
using MathParser.DataTypes.DynamicDataTypes;
namespace MathParser
{
/// <summary>
/// Solver.
/// This is the Entry point of the MathParsing liberay.
/// </summary>
/// The other Programmers will use this class as an interface.
public delegate void SolverOnPrintDelegate<T>(T theType); // Internal delegate. Don't mess with them from outside the namespace MATH_PARSER.
public delegate void SolverKeyWordsSyncDelegate<T>(ref T theList); // Internal delgate.
public delegate void SolverMatrixFlagSync(ref string FlagStart, ref string FlagEnd, ref string FlagElementSeperation, ref string FlagRowSeperation);
public class Solver : ISolver
{
string theExpression;
bool saveHistory = false;
//Dictionary<string, MathParserExpression> History;
MathParserExpression solution;
bool Processed = true;
public SolverOnPrintDelegate<Matrix> OnMatixPrint = delegate { };
public SolverOnPrintDelegate<Number> OnNumberPrint = delegate { };
public static SolverKeyWordsSyncDelegate<List<string>> OnKeyWordsSync = null;
public static SolverKeyWordsSyncDelegate<List<string>> On_LeftRight_Function_KeyWords_Sync = null;
public SolverKeyWordsSyncDelegate<Dictionary<string, MathParserExpression>> OnConstantSync = null;
public SolverMatrixFlagSync OnMatrixFlagSync = null;
public static UnitFunctionExtentionDelegte On_Single_Argument_KeyWord_Implement = null;
//public List<string>theKeyWordsList = new List<string>();
//public List<string>
List<string> theUnitFunctionKeyWordsList;
public MathParserExpression getSolution()
{
return solution;
}
public bool isProcessed()
{
return Processed;
}
void KeyWordsList(ref List<string> theKeyWordList)
{
if (theKeyWordList.Count != 0) {
return;
}
theKeyWordList.Add("degToRad");
theKeyWordList.Add("radToDeg");
theKeyWordList.Add("sin");
theKeyWordList.Add("cos");
theKeyWordList.Add("tan");
theKeyWordList.Add("cot");
theKeyWordList.Add("cosec");
theKeyWordList.Add("csc");
theKeyWordList.Add("sec");
theKeyWordList.Add("floor");
theKeyWordList.Add("ceil");
theKeyWordList.Add("abs");
theKeyWordList.Add("sinh");
theKeyWordList.Add("cosh");
theKeyWordList.Add("tanh");
theKeyWordList.Add("ln");
theKeyWordList.Add("log");
theKeyWordList.Add("arcCos");
theKeyWordList.Add("arcSin");
theKeyWordList.Add("arcTan");
theKeyWordList.Add("sqrt");
theKeyWordList.Add("round");
theKeyWordList.Add("√");
theKeyWordList.Add("trunc");
theKeyWordList.Add("rref");
theKeyWordList.Add("ref");
theKeyWordList.Add("det");
theKeyWordList.Add("adj");
theKeyWordList.Add("rank");
theKeyWordList.Add("inv");
theKeyWordList.Add("transp");
theKeyWordList.Add("rootByBisection_polynomial");
theKeyWordList.Add("rootByRFM_polynomial");
theKeyWordList.Add("rootByNRM_polynomial");
theKeyWordList.Add("rootBySM_polynomial");
theKeyWordList.Add("linearFit_XY"); // y = ax + b
theKeyWordList.Add("polynomialFit_XY"); // y = ax^2 + bx + c
theKeyWordList.Add("exponentialFit_XY"); // y = a*e^bx
theKeyWordList.Add("geometricFit_XY"); // y = a*x^b
theKeyWordList.Add("linearFit_ND"); // y = a1x1 + a2x2 + a3x3 + ... + aNxN
theKeyWordList.Add("interpolationByNFIM"); // This function does the Newton Foreard Interpolation.
OnKeyWordsSync?.Invoke(ref theKeyWordList);
theUnitFunctionKeyWordsList = theKeyWordList;
}
void LeftRightList(ref List<string> theList)
{
if (theList.Count != 0) {
return;
}
theList.Add("P");
theList.Add("C");
}
void ConstantList(ref Dictionary<string, MathParserExpression> ConstantList)
{
if (ConstantList.Count != 0) {
return;
}
ConstantList.Add("e", new MathParserExpression(new Number((double)Math.E)));
ConstantList.Add("pi", new MathParserExpression(new Number((double)Math.PI)));
OnConstantSync?.Invoke(ref ConstantList);
}
public Solver()
{
Matrix.staticOnPrint = (Matrix matrix) =>
{
OnMatixPrint?.Invoke(matrix);
};
MatrixBuilder.staticOnFlagSync = (ref string FlagStart, ref string FlagEnd, ref string FlagElementSeperation, ref string FlagRowSeperation) =>
{
OnMatrixFlagSync?.Invoke(ref FlagStart, ref FlagEnd, ref FlagElementSeperation, ref FlagRowSeperation);
};
Number.staticOnPrint = (Number number) =>
{
OnNumberPrint?.Invoke(number);
};
//DigitalLogicSolver.syncKeyWords ();
NonEquation.staticOnKeyWordSync += KeyWordsList;
NonEquation.staticOnConstantSync += ConstantList;
}
~Solver()
{
NonEquation.staticOnKeyWordSync -= KeyWordsList;
NonEquation.staticOnConstantSync -= ConstantList;
}
public Solver(string theExpression)
{
Matrix.staticOnPrint = (Matrix matrix) =>
{
OnMatixPrint?.Invoke(matrix);
};
Number.staticOnPrint = (Number number) =>
{
OnNumberPrint?.Invoke(number);
};
MatrixBuilder.staticOnFlagSync = (ref string FlagStart, ref string FlagEnd, ref string FlagRowSeperation, ref string FlagElementSeperation) =>
{
OnMatrixFlagSync?.Invoke(ref FlagStart, ref FlagEnd, ref FlagRowSeperation, ref FlagElementSeperation);
};
NonEquation.staticOnKeyWordSync += KeyWordsList;
NonEquation.staticOnConstantSync += ConstantList;
this.theExpression = theExpression.Trim();
if (this.theExpression.Contains("`"))
{
Processed = false;
throw new Exception("Invalid symbol \" ` \" in the string cannot proceed");
}
if (this.theExpression.Contains("#"))
{
Processed = false;
throw new Exception("Invalid symbol \" # \" in the string cannot proceed");
}
if (string.IsNullOrEmpty(this.theExpression) || string.IsNullOrWhiteSpace(this.theExpression))
{
Processed = false;
throw new Exception("The given Expression doen not contain any information.");
}
if (theExpression.Contains("E"))
{
this.theExpression = theExpression.Replace ("E", "*10^");
//theExpression = theExpression.Replace ("rref","reducedRowEchelonForm");
}
}
public void setMathExpression(string theExpression)
{
Processed = true;
this.theExpression = theExpression.Trim();
if (this.theExpression.Contains("`"))
{
Processed = false;
throw new Exception("Invalid symbol \" ` \" in the string cannot proceed");
}
if (this.theExpression.Contains("#"))
{
Processed = false;
throw new Exception("Invalid symbol \" # \" in the string cannot proceed");
}
if (string.IsNullOrEmpty(this.theExpression) || string.IsNullOrWhiteSpace(this.theExpression))
{
Processed = false;
throw new Exception("The given Expression doen not contain any information.");
}
if (theExpression.Contains("E"))
{
this.
theExpression = theExpression.Replace ("E", "*10^");
//this.theExpression = theExpression.Replace ("rref","reducedRowEchelonForm");
}
}
public void SaveHistory()
{
saveHistory = true;
Checker.History = new Dictionary<string, MathParserExpression>();
}
public void ClearHistory()
{
Checker.History.Clear();
}
public Dictionary<string, MathParserExpression> getHistory() => Checker.History;
public void Solve()
{
MathParser.StringObserver sol = new StringObserver(theExpression, Checker.History);
if (sol.isProcessed())
{
solution = sol.getSolution();
}
else
{
Processed = false;
throw new Exception("Expression Unsolvable.");
}
solution.Statement = theExpression;
if (saveHistory)
{
if (string.IsNullOrEmpty(solution.Tag) || string.IsNullOrWhiteSpace(solution.Tag))
{
if (solution.Type.Contains("Number"))
{
string name = autoNumberNamer();
solution.Tag = name;
solution.setEntireTag(name);
Checker.History.Add(name, solution);
}
else if (solution.Type.Contains("Matrix"))
{
string name = autoMatrixNamer();
solution.Tag = name;
solution.setEntireTag(name);
Checker.History.Add(name, solution);
}
}
else
{
string name = solution.Tag;
if (Checker.History.ContainsKey(name))
{
// solution.setEntireTag (name);
Checker.History[name] = solution;
}
else
{
// solution.setEntireTag (name);
Checker.History.Add(name, solution);
}
}
}
} // end method for solve
public void PrintSolution()
{
if (solution.Type.Contains("Number"))
{
((MathParser.DataTypes.DynamicDataTypes.Number)solution.Data)?.Print();
}
else if (solution.Type.Contains("Matrix"))
{
((MathParser.DataTypes.DynamicDataTypes.Matrix)solution.Data)?.Print();
}
}
int ncount = 0;
string autoNumberNamer()
{
ncount++;
string name = "n" + ncount;
if (!Checker.History.ContainsKey(name))
{
return name;
}
else
{
return autoNumberNamer();
}
}
int mcount = 0;
string autoMatrixNamer()
{
mcount++;
string name = "m" + mcount;
if (!Checker.History.ContainsKey(name))
{
return name;
}
else
{
return autoMatrixNamer();
}
}
/*public bool theExpressionContainsDigitalLogic()
{
List<string> theDigitalKeyWords = DigitalLogicSolver.getKeyWordsList ();
foreach(var x in theDigitalKeyWords){
if (theExpression.Contains (x)) {
return true;
}
}
return false;
}*/
public List<string> getUnitFunctionList()
{
return theUnitFunctionKeyWordsList;
}
}
}