-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrixBuilder.cs
More file actions
211 lines (195 loc) · 5.96 KB
/
MatrixBuilder.cs
File metadata and controls
211 lines (195 loc) · 5.96 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
using System;
using MathParser.DataTypes.DynamicDataTypes;
using MathParser.DataTypes;
using System.Collections.Generic;
namespace MathParser
{
/// <summary>
/// Matrix builder.
/// This class actuall makes the matrix out of the given string,
/// If it is in correct syntax.
/// The flags for the matrix structure are defined as default to be "[", "Space", ";", "]".
/// They can be definded by the programmer externally by the static and on static delegates 'OnSyncFlags()' and 'staticOnSyncFlag()'.
/// The non-static one gets the highest precendence.
///
/// The parsing is defind by default to convert the elements into the double.
/// But the parsing methods can be defined by the programmer externally, by the delegate methods which are
/// static and non-static. The non-static one gets the highest precedence.
/// The delegate is OnParse() and staticOnParse();
///
/// How to use:
///
/// MatrixBuilder builder = new MatrixBuilder("[2 2 ; 2 2 ]");
///
/// builder.OnSyncFlag += (ref string FlagStart,ref string FlagEnd, ref string FlagElementSeperation, ref string FlagRowSeperation) =>
/// {
/// // Do Something like;
/// FlagStart = "[";
/// FlagEnd = "]";
/// //....
/// };
///
/// builder.SyncFlags();
/// builder.Parse();
/// Matrix mat = builder.getMatrix();
///
/// //....
///
/// </summary>
public class MatrixBuilder : ISolver
{
string givenExpression;
DataTypes.DynamicDataTypes.Matrix theMatrix;
bool Processed = true;
public bool isProcessed() => Processed;
string FlagStart = "[", FlagEnd = "]", FlagElementSeperation = " ", FlagRowSeperation = ";";
public delegate void FlagSyncerDelegate(ref string FlagStart,ref string FlagEnd, ref string FlagElementSeperation, ref string FlagRowSeperation);
public FlagSyncerDelegate OnFlagSync;
public static FlagSyncerDelegate staticOnFlagSync;
public delegate dynamic ParseAlgorithm(string matrixElementString, ref bool Successfully_processed);
public static ParseAlgorithm staticOnParse;
public ParseAlgorithm OnParse;
public MathParserExpression getSolution() {
MathParserExpression result = new MathParserExpression(theMatrix);
return result;
}
public Matrix getMatrix()
{
return theMatrix;
}
void DefaultFlagSetter()
{
FlagStart = "[";
FlagEnd = "]";
FlagRowSeperation = ";";
FlagElementSeperation = " ";
}
public void SyncFlags()
{
if (OnFlagSync != null)
{
OnFlagSync(ref FlagStart, ref FlagEnd, ref FlagElementSeperation, ref FlagRowSeperation);
}
else if (staticOnFlagSync != null)
{
staticOnFlagSync(ref FlagStart, ref FlagEnd, ref FlagElementSeperation, ref FlagRowSeperation);
}
else
{
DefaultFlagSetter();
}
}
public MatrixBuilder(){}
public MatrixBuilder(string String_To_Parse) {
givenExpression = String_To_Parse.Trim();
}
// Checks the correct syntax of the given Matrix.
private bool MatrixSyntaxChecker()
{
bool MatrixAlright = true;
if (givenExpression.Contains(FlagStart) && givenExpression.Contains(FlagEnd))
{
if (givenExpression.StartsWith(FlagStart) && givenExpression.EndsWith(FlagEnd))
{
int lenghtS = givenExpression.Replace(FlagStart, "").Length;
int lenghtE = givenExpression.Replace(FlagEnd, "").Length;
if ((lenghtE == givenExpression.Length - 1) && (lenghtE == givenExpression.Length - 1))
{ }
else {
MatrixAlright = false;
throw new Exception("Invalid Matrix Syntax.");
}
}
else {
MatrixAlright = false;
throw (new Exception("Invalid Matrix Syntax. The given string does not contain the correct sequences of \'{FlagStart}\' and \'{FlagEnd}\'."));
}
}
else {
MatrixAlright = false;
throw (new Exception($"Invalid Matrix Syntax. The given string does not contain \'{FlagStart}\' in the Start and \'{FlagEnd}\' in the End."));
}
return MatrixAlright;
}
// End matrix syntax checker.
public void Parse() // Parse method.
{
bool proceed = MatrixSyntaxChecker();
if (!proceed)
{
Processed = false;
}
else {
string matExpression = givenExpression.TrimStart(FlagStart.ToCharArray());
matExpression = matExpression.TrimEnd(FlagEnd.ToCharArray());
matExpression = matExpression.Trim();
List<string> theStringRows = new List<string>(matExpression.Split(FlagRowSeperation.ToCharArray()));
theStringRows.Remove("");
int rows = theStringRows.Count;
int cols = 0;
List<List<string>> theMatrixElements = new List<List<string>>();
foreach (string row in theStringRows)
{
List<string> dumyList = new List<string>(row.Trim().Split(FlagElementSeperation.ToCharArray()));
dumyList.Remove("");
if (dumyList.Count > cols)
{
cols = dumyList.Count;
}
theMatrixElements.Add(dumyList);
}
theMatrix = new Matrix(rows, cols, "");
int irow = 0, icol = 0;
foreach (List<string> row in theMatrixElements)
{
icol = 0;
foreach (string element in row)
{
if (OnParse != null)
{
dynamic mElement = OnParse(element.Trim(), ref Processed);
theMatrix[irow, icol] = mElement;
}
else if (staticOnParse != null)
{
dynamic mElement = staticOnParse(element.Trim(), ref Processed);
theMatrix[irow, icol] = mElement;
}
else
{
NonEquation sol = new NonEquation (element.Trim ());
sol.History = Checker.History;
sol.Solve();
Number a;
if (sol.isProcessed ()) {
MathParserExpression ans = sol.getSolution ();
a = ans.Data;
} else {
Processed = false;
throw new MathParserException ("Invalid Entry.");
}
theMatrix[irow, icol] = (double)a.Data;
Processed = true;
}
if (!Processed)
{
Processed = false;
break;
}
icol++;
}
if (!Processed)
{
Processed = false;
break;
}
irow++;
}
if (!Processed)
{
theMatrix = new Matrix(1, 1);
}
}
}
}
}