-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBraketSolver.cs
More file actions
117 lines (108 loc) · 3.04 KB
/
Copy pathBraketSolver.cs
File metadata and controls
117 lines (108 loc) · 3.04 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
using System;
using System.Collections.Generic;
using MathParser.DataTypes;
using MathParser.DataTypes.DynamicDataTypes;
namespace MathParser
{
/// <summary>
/// Braket solver.
/// This solver is connected to DMAS too... the
/// It Solves the Brakets.
/// </summary>
public class BraketSolver : ISolver
{
Dictionary<string, MathParserExpression> theData;
List<string> ExpressionElements;
MathParserExpression solution;
bool Processed = true;
public MathParserExpression getSolution() => solution;
public bool isProcessed() => Processed;
public BraketSolver (){}
public BraketSolver (List<string>theExpressionElement, Dictionary<string, MathParserExpression>Data)
{
theData = Data;
ExpressionElements = theExpressionElement;
Solve ();
}
void Solve()
{
if (!(ExpressionElements.Contains ("(") || ExpressionElements.Contains (")"))) {
DMASSolver sol = new DMASSolver (ExpressionElements, theData);
sol.Solve ();
if (sol.isProcessed ()) {
solution = sol.getSolution ();
} else {
Processed = false;
throw new MathParserException ("The expression could not be solved");
}
} else {
bool intake = false;
int bstart = 0;
int belements = 1;
List<string> theBraketList = new List<string>();
for (int c = 0; c < ExpressionElements.Count; c++) {
string Element = ExpressionElements [c];
if (!ExpressionElements.Contains ("(") && !ExpressionElements.Contains("-(")) {
break;
}
if (Element == ")") {
intake = false;
DMASSolver sol = new DMASSolver (theBraketList, theData);
sol.Solve ();
if (sol.isProcessed ()) {
MathParserExpression ANS = sol.getSolution ();
if (ExpressionElements [bstart].Contains ("-")) {
if (ANS.Type.Contains ("Number")) {
Number g = ANS.Data;
g = g * new Number ((double)-1);
ANS = new MathParserExpression (g);
} else if (ANS.Type.Contains ("Matrix")) {
Matrix g = ANS.Data;
g = g * -1;
ANS = new MathParserExpression (g);
}
}
string name = autoNamer ();
theData.Add (name, ANS);
ExpressionElements [bstart] = name;
ExpressionElements.RemoveRange (bstart+1, belements+1);
c = -1;
} else {
Processed = false;
throw new MathParserException ("Error");
}
belements = 0;
}
if (intake) {
theBraketList.Add (Element);
belements++;
}
if (Element.Contains("(")) {
theBraketList = new List<string> ();
intake = true;
bstart = c;
belements = 0;
}
}
BraketSolver bsol = new BraketSolver (ExpressionElements, theData);
if (bsol.isProcessed ()) {
solution = bsol.getSolution ();
} else {
Processed = false;
throw new MathParserException ("Error");
}
}
}
int ncount = 0;
string autoNamer()
{
ncount++;
string name = "BODMASS_thinkgy_9pobosx__" + ncount.ToString ();
if (theData.ContainsKey (name)) {
return (autoNamer());
} else {
return name;
}
}
}
}