-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathOld Code for differentiating.txt
289 lines (204 loc) · 10.2 KB
/
Old Code for differentiating.txt
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
public static class SymbolicOperations
{
private class DifferentiableExpression
{
public DifferentiableExpression Next;
public Token DifferentialGroupToken;
public string DifferentialResult;
public Token Operation;
}
/// <summary>
/// Differentiate the expression based on the required variable.
/// </summary>
/// <param name="equation"></param>
/// <param name="variable"></param>
/// <returns></returns>
public static string Diff(string equation, string variable)
{
Token tokens = TokenizeExpression(equation);
//now all premitive are tokenized
// to differentiate we should separate + and -
int ix = 0; //this is the index in the discovered tokens
DifferentiableExpression RootDE = new DifferentiableExpression();
DifferentiableExpression CurrentDE = RootDE;
Token Group = new Token();
while (ix < tokens.Count)
{
if (
tokens[ix].TokenClassType != typeof(PlusToken) &&
tokens[ix].TokenClassType != typeof(MinusToken)
)
{
Group.AppendSubToken(tokens[ix]);
}
else
{
//grouping done, put this group in the list.
CurrentDE.DifferentialGroupToken = Group;
CurrentDE.DifferentialResult = DiffPart(Group, variable);
if (ix < tokens.Count)
{
CurrentDE.Operation = tokens[ix]; //the current positive or negative token
CurrentDE.Next = new DifferentiableExpression();
CurrentDE = CurrentDE.Next;
Group = new Token();
}
}
ix++;
}
CurrentDE.DifferentialGroupToken = Group;
CurrentDE.DifferentialResult = DiffPart(Group, variable);
// make another pass to form the result.
CurrentDE = RootDE;
string result = string.Empty;
while (CurrentDE != null)
{
if (string.IsNullOrEmpty(CurrentDE.DifferentialResult))
{
result += CurrentDE.DifferentialGroupToken.TokenValue;
}
else
{
result += CurrentDE.DifferentialResult;
}
if (CurrentDE.Operation != null) result += CurrentDE.Operation.TokenValue;
CurrentDE = CurrentDE.Next;
}
return result;
}
/// <summary>
/// Take token {which doesn't contain any + or -}
/// and then differentiate it.
/// </summary>
/// <param name="part"></param>
/// <param name="variable"></param>
/// <returns></returns>
private static string DiffPart(Token part, string variable)
{
//the code will go back and forward to adjust the derivation.
int ix = 0;
while (ix < part.Count)
{
if (part[ix].TokenValue.Equals(variable, StringComparison.OrdinalIgnoreCase))
{
//check if it has ^ token after it
string PowerPart = string.Empty;
if (ix < part.Count)
{
//there is still tokens to consume
if (part[ix + 1].TokenClassType == typeof(CaretToken))
{
PowerPart = part[ix + 2].TokenValue;
}
//check the value before the variable
string CoeffecientPart = string.Empty;
if (ix > 1)
{
CoeffecientPart = part[ix - 2].TokenValue;
}
double NumericalPowerPart;
double NumericalCoeffecientPart;
if (double.TryParse(PowerPart, out NumericalPowerPart))
{
//succeed
if (double.TryParse(CoeffecientPart, out NumericalCoeffecientPart))
{
// a*x^b
// a=a*b
double NewNumericalCoeffecient = NumericalPowerPart * NumericalCoeffecientPart;
// b=b-1
double NewNumericalPowerPart = NumericalPowerPart - 1;
// replace a*x^b with a*b*x^b-1
string result = NewNumericalCoeffecient.ToString(CultureInfo.InvariantCulture);
result += part[ix - 1].TokenValue;
result += part[ix].TokenValue;
if (NewNumericalPowerPart > 1)
{
result += part[ix + 1].TokenValue;
result += NewNumericalPowerPart.ToString(CultureInfo.InvariantCulture);
}
else
{
//omit the zero power.
}
return result;
}
}
}
else
{
//no there are not
}
}
ix++;
}
return string.Empty;
}
public static string compute(string expression)
{
Token tokens = TokenizeExpression(expression);
//suppose that we have x*x
// then the output is x^2
// suppose that we have 3*x*x*x
// then the output is 3*x^3
// suppose we have 4*a*3*x^4+5*sin(x)-2*(x^4-3*x^2)
// then the ouput is 12*a*x^4 +5*sin(x)-2*(x^4-3*x^2)
// this means LEAVE the brackets.
// make the multiplications and divisions first
// leave the brackets as it is
// in every term
// 1- inspect every word
// 2- inspect every number
// make the same as the calculation of numbers but instead
// make the expression tree to mix numbers and words.
throw new Exception();
}
private static Token TokenizeExpression(string expression)
{
var tokens = Token.ParseText(expression);
tokens = tokens.MergeTokens(new MultipleSpaceToken());
#region Conditions
tokens = tokens.MergeTokens(new WhenStatementToken());
tokens = tokens.MergeTokens(new OtherwiseStatementToken());
tokens = tokens.MergeTokens(new AndStatementToken());
tokens = tokens.MergeTokens(new OrStatementToken());
tokens = tokens.MergeTokens(new EqualityToken());
tokens = tokens.MergeTokens(new InEqualityToken());
tokens = tokens.MergeTokens(new LessThanOrEqualToken());
tokens = tokens.MergeTokens(new GreaterThanOrEqualToken());
#endregion
tokens = tokens.MergeTokens(new WordToken()); //discover words
tokens = tokens.MergeTokens(new NumberToken()); //discover the numbers
//tokens = tokens.MergeTokens<UnitToken>();
//tokens = tokens.MergeTokens(new UnitizedNumberToken()); //discover the unitized numbers
tokens = tokens.MergeTokens<TensorProductToken>();
tokens = tokens.MergeTokens(new NameSpaceToken());
tokens = tokens.MergeTokens(new NameSpaceAndValueToken());
tokens = tokens.MergeTokens<FunctionValueToken>();
tokens = tokens.MergeTokensInGroups(
new ParenthesisGroupToken(), // group (--()-) parenthesis
new SquareBracketsGroupToken(), // [[][][]]
new CurlyBracketGroupToken() // {{}}{}
);
tokens = tokens.MergeTokens<MagnitudeToken>();
tokens = tokens.MergeTokens<AbsoluteToken>();
tokens = tokens.RemoveSpaceTokens(); //remove all spaces
tokens = tokens.DiscoverQsCalls(StringComparer.OrdinalIgnoreCase,
new string[] { "When", "Otherwise", "And", "Or" }
);
return tokens;
}
}
/// <summary>
///A test for Diff
///</summary>
[TestMethod()]
public void DiffTest()
{
string equation = "3*x^2+4*x^3";
string variable = "x";
string expected = "6*x+12*x^2";
string actual;
actual = SymbolicOperations.Diff(equation, variable);
Assert.AreEqual(expected, actual);
}