-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
355 lines (306 loc) · 13.7 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Linq;
using JA.Expressions;
using System.Text.Json.Serialization;
namespace JA
{
static class Program
{
static int testIndex = 0;
static void Main(string[] args)
{
ParseExpressionDemo();
CompileExpressionDemo();
SimplifyExpressionDemo();
AssignExpressionDemo();
SystemOfEquationsDemo();
CalculusExprDemo();
CalculusDerivativeDemo();
CalculusSolveDemo();
CompileArrayDemo();
CompileMatrixDemo();
}
private static void CalculusExprDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
VariableExpr x = "x", y="y";
var f_input = "((x-1)*(x+1))/((x)^2+1)";
Console.WriteLine($"input: {f_input}");
var f = Expr.Parse(f_input).GetFunction("f");
Console.WriteLine(f);
var fx = f.CompileArg1();
Console.WriteLine($"f(0.5)={fx(0.5)}");
Console.WriteLine("Partial Derivative");
var df = f.PartialDerivative(x);
Console.WriteLine($"df/dx: {df}");
Console.WriteLine("Total Derivative");
var fp = f.TotalDerivative();
Console.WriteLine(fp);
Console.WriteLine();
var w_input = "x^2 + 2*x*y + x/y";
Console.WriteLine($"input: {w_input}");
var w = Expr.Parse(w_input).GetFunction("w", "x", "y");
Console.WriteLine(w);
Console.WriteLine("Patial Derivatives");
var wx = w.PartialDerivative(x);
Console.WriteLine($"dw/dx: {wx}");
var wy = w.PartialDerivative(y);
Console.WriteLine($"dw/dy: {wy}");
Console.WriteLine("Total Derivative");
Console.WriteLine($"Set xp=v, yp=3");
var wp = w.TotalDerivative("wp", ("x", "v"), ("y", 3.0));
Console.WriteLine(wp);
Console.WriteLine();
}
static void ParseExpressionDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
Console.WriteLine("Evaluate Expression");
var text = "1/t^2*(1-exp(-pi*t))/(1-t^2)";
var expr = Expr.Parse(text);
Console.WriteLine(expr);
Console.WriteLine($"Symbols: {string.Join(",", expr.GetSymbols())}");
Console.WriteLine($"Rank:{expr.Rank}");
Console.WriteLine($"{"t",-12} {"expr",-12}");
for (int i = 1; i <= 10; i++)
{
var t = 0.10 * i;
var x = expr.Eval(("t", t));
Console.WriteLine($"{t,-12:g4} {x,-12:g4}");
}
Console.WriteLine();
}
static void CompileExpressionDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
Console.WriteLine("Compile Expression");
var text = "1/t^2*(1-exp(-pi*t))/(1-t^2)";
var expr = Expr.Parse(text);
var fun = new Function("f", expr, "t");
Console.WriteLine(fun);
Console.WriteLine($"Rank:{fun.Rank}");
var fq = fun.Compile<FArg1>();
Console.WriteLine($"{"t",-12} {"f(t)",-12}");
for (int i = 1; i <= 10; i++)
{
var t = 0.10 * i;
var x = fq(t);
Console.WriteLine($"{t,-12:g4} {x,-12:g4}");
}
Console.WriteLine();
}
static void SimplifyExpressionDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
VariableExpr x = "x", y = "y";
//double a = 3, b = 0.25;
Expr a = "a=3", b = "b=0.25";
Console.WriteLine($"{a}={(double)a}, {b}={(double)b}, {x}, {y}");
Console.WriteLine();
int index = 0;
Console.WriteLine($"{index,3}. definition = result");
Console.WriteLine($"{++index,3}. ({a}+x)+({b}+y) = {(a+x)+(b+y)}");
Console.WriteLine($"{++index,3}. ({a}+x)-({b}+y) = {(a+x)-(b+y)}");
Console.WriteLine($"{++index,3}. ({a}-x)+({b}+y) = {(a-x)+(b+y)}");
Console.WriteLine($"{++index,3}. ({a}+x)-({b}-y) = {(a+x)-(b-y)}");
Console.WriteLine($"{++index,3}. ({a}-x)+({b}-y) = {(a-x)+(b-y)}");
Console.WriteLine($"{++index,3}. ({a}-x)-({b}-y) = {(a-x)-(b-y)}");
Console.WriteLine($"{++index,3}. ({a}*x)+({b}*y) = {(a*x)+(b*y)}");
Console.WriteLine($"{++index,3}. ({a}*x)-({b}*y) = {(a*x)-(b*y)}");
Console.WriteLine($"{++index,3}. ({a}/x)+({b}*y) = {(a/x)+(b*y)}");
Console.WriteLine($"{++index,3}. ({a}*x)-({b}/y) = {(a*x)-(b/y)}");
Console.WriteLine($"{++index,3}. ({a}/x)+({b}/y) = {(a/x)+(b/y)}");
Console.WriteLine($"{++index,3}. ({a}/x)-({b}/y) = {(a/x)-(b/y)}");
Console.WriteLine($"{++index,3}. ({a}+x)*({b}+y) = {(a+x)*(b+y)}");
Console.WriteLine($"{++index,3}. ({a}+x)/({b}+y) = {(a+x)/(b+y)}");
Console.WriteLine($"{++index,3}. ({a}-x)*({b}+y) = {(a-x)*(b+y)}");
Console.WriteLine($"{++index,3}. ({a}+x)/({b}-y) = {(a+x)/(b-y)}");
Console.WriteLine($"{++index,3}. ({a}-x)*({b}-y) = {(a-x)*(b-y)}");
Console.WriteLine($"{++index,3}. ({a}-x)/({b}-y) = {(a-x)/(b-y)}");
Console.WriteLine($"{++index,3}. ({a}*x)*({b}*y) = {(a*x)*(b*y)}");
Console.WriteLine($"{++index,3}. ({a}*x)/({b}*y) = {(a*x)/(b*y)}");
Console.WriteLine($"{++index,3}. ({a}/x)*({b}*y) = {(a/x)*(b*y)}");
Console.WriteLine($"{++index,3}. ({a}*x)/({b}/y) = {(a*x)/(b/y)}");
Console.WriteLine($"{++index,3}. ({a}/x)*({b}/y) = {(a/x)*(b/y)}");
Console.WriteLine($"{++index,3}. ({a}/x)/({b}/y) = {(a/x)/(b/y)}");
Console.WriteLine();
}
static void CompileArrayDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
Console.WriteLine("Array Expression");
var input = "abs([(1-t)^3,3*(1-t)^2*t,3*t^2*(1-t),t^3])";
var expr = Expr.Parse(input);
Console.WriteLine($"input: {input}");
var y = expr.Eval(("t", 0.5));
var fun = new Function("f", expr, "t");
Console.WriteLine(fun);
var f = fun.Compile<QArg1>();
Console.WriteLine($"{"t",-12} {"f(t)",-12}");
for (int i = 0; i <= 10; i++)
{
var t = 0.10 * i;
var x = f(t);
Console.WriteLine($"{t,-12:g4} {x,-18:g4}");
}
// TODO: How to implement vector functions?
input = "dot([x,2*y],[4-x,-1+y])";
Console.WriteLine($"input: {input}");
expr = Expr.Parse(input);
var f2 = new Function("f", expr, "x", "y");
Console.WriteLine(f2);
input = "dot(r_, r_) - outer(r_, r_)";
Console.WriteLine($"f(r_) = {input}");
Console.WriteLine("r_=[x,y,z]");
input = input.Replace("r_", "[x,y,z]");
expr = Expr.Parse(input);
var f3 = new Function("f", expr, "x", "y", "z");
Console.WriteLine(f3);
Console.WriteLine();
}
static void CompileMatrixDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
Console.WriteLine("Matrix Expression");
var tt = Expr.Variable("t");
var expr = Expr.Matrix( new Expr[][] {
new Expr[] { 1/(1+tt^2), 1-(tt^2)/(1+tt^2) },
new Expr[] { tt/(1+tt^2), -1/(1+tt^2) }});
var fexpr = new Function("f", expr, "t");
Console.WriteLine(fexpr);
var f = fexpr.Compile<QArg1>();
Console.WriteLine($"{"t",-12} {"f(t)"}");
for (int i = 0; i <= 10; i++)
{
var t = 0.10 * i;
var y = f(t);
Console.WriteLine($"{t,-12:g4} {y:g4}");
}
var fp = fexpr.PartialDerivative(tt);
Console.WriteLine(fp);
Console.WriteLine("Solve a 2×2 system of equations.");
var A = Expr.Parse("[[7,t],[-t,3]]");
var b = Expr.Parse("[4,-1]");
Console.WriteLine($"Coefficient Matrix, A={A}");
Console.WriteLine($"Constant Vector, b={b}");
var x = Expr.Solve(A, b);
Console.WriteLine($"Solution Vector, x={x}");
var r = b - A*x;
Console.WriteLine($"Residual Vector, b-A*x={r}");
Console.WriteLine("Check residual for t=0..1");
Console.WriteLine($"{"t",-12} {"residual"}");
for (int i = 0; i <= 10; i++)
{
var t = 0.10 * i;
var rval = r.Eval(("t",t));
Console.WriteLine($"{t,-12:g4} {rval:g8}");
}
Console.WriteLine();
}
static void CalculusDerivativeDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
VariableExpr x = "x";
foreach (var op in KnownUnaryDictionary.Defined)
{
var f = Expr.Unary(op, x);
var fp = f.PartialDerivative(x);
Console.WriteLine($"d/dx({f}) = {fp}");
}
Console.WriteLine();
}
static void CalculusSolveDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
Console.WriteLine("Define a function and solve using Newton-Raphon.");
Expr a = 7, b= 3;
Expr x = "x";
Function f = ( a*Expr.Sin(x)-b*x ).GetFunction("f");
Console.WriteLine(f);
Console.WriteLine("Find solution, such that f(x)=0");
Scalar init = 3.0;
Console.WriteLine($"Initial guess, x={init}");
Scalar sol = (Scalar)f.NewtonRaphson(init, 0.0);
var fx = f.CompileArg1();
Console.WriteLine($"x={sol}, f(x)={fx(sol)}");
Console.WriteLine();
}
static void AssignExpressionDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
Expr.ClearParameters();
var input = "a+b = (2*a+2*b)/2";
var ex_1 = Expr.Parse(input);
Console.WriteLine(ex_1);
var e_1 = ex_1.Eval(("a",1), ("b",3) );
Console.WriteLine($"Eval = {e_1}");
Console.WriteLine("Use = for assignment of constants.");
VariableExpr a = "a", b= "b", c="c";
Expr lhs = Expr.Array(a,b,c);
Expr rhs = "[1,2,3]";
Console.WriteLine($"{lhs}={rhs}");
Expr ex_2 = Expr.Assign(lhs, rhs);
Console.WriteLine(ex_2);
input = "(a+b)*x-c";
var f = Expr.Parse(input).GetFunction("f", "x");
Console.WriteLine(f);
var fx = f.CompileArg1();
foreach (var x in new[] { -1.0, 0.0, 1.0 })
{
Console.WriteLine($"f({x})={fx(x)}");
}
Console.WriteLine();
}
static void SystemOfEquationsDemo()
{
Console.WriteLine($"*** DEMO [{++testIndex}] : {GetMethodName()} ***");
Expr.ClearParameters();
Console.WriteLine("Define a 3×3 system of equations.");
var system = Expr.Parse("[2*x-y+3*z=15, x + 3*z/2 = 3, x+3*y = 1]");
var vars = new[] { "x", "y", "z" };
Console.WriteLine(system);
Console.WriteLine();
if (system.ExtractLinearSystem(vars, out Matrix A, out Vector b))
{
Console.WriteLine($"Unknowns: {string.Join(",", vars)}");
Console.WriteLine();
ShowMatrix("Coefficient Matrix A=", A);
Console.WriteLine();
ShowVector("Constant Vector b=", b);
var x = A.Solve(b);
ShowVector("Solution Vector x=", x);
var r = b-A*x;
ShowVector("Residual Vector r=", r);
}
else
{
Console.WriteLine("Something went wrong, could not extract linear system from equations above.");
}
Console.WriteLine();
}
static void ShowVector(string title, Vector x, string formatting = "g4", int columnWidth = 6)
{
Console.WriteLine(title);
for (int i = 0; i < x.Size; i++)
{
Console.WriteLine($"| {x.Elements[i].ToString(formatting).PadLeft(columnWidth)} |");
}
}
static void ShowMatrix(string title, Matrix A, string formatting = "g4", int columnWidth = 6)
{
Console.WriteLine(title);
for (int i = 0; i < A.Rows; i++)
{
Console.WriteLine($"| {string.Join(" ", A.Elements[i].Select(x=>x.ToString(formatting).PadLeft(columnWidth)))} |");
}
}
static string GetMethodName([CallerMemberName] string name = null)
{
return name;
}
}
}