-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExpressionSolver.cs
692 lines (626 loc) · 21.2 KB
/
ExpressionSolver.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
using System.Collections;
using System.Collections.Generic;
namespace AK
{
/*
ExpressionSolver is licensed under the MIT license.
Copyright (c) 2015 Antti Kuukka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
public class ExpressionSolver
{
private static readonly int MaxCustomFunctionParamCount = 8;
public enum UndefinedVariablePolicy
{
Error,
DefineGlobalVariable,
DefineExpressionLocalVariable
}
public UndefinedVariablePolicy undefinedVariablePolicy;
private static Dictionary<string, double> immutableGlobalConstants = new Dictionary<string, double>()
{
{"e",System.Math.E},
{"pi",System.Math.PI}
};
private Dictionary<string, Variable> globalConstants = new Dictionary<string, Variable>();
private Dictionary<string, CustomFunction> customFuncs = new Dictionary<string, CustomFunction>();
public ExpressionSolver()
{
undefinedVariablePolicy = UndefinedVariablePolicy.Error;
AddCustomFunction("sin", delegate(double p) { return System.Math.Sin(p); },true);
AddCustomFunction("cos", delegate(double p) { return System.Math.Cos(p); },true);
AddCustomFunction("tan", delegate(double p) { return System.Math.Tan(p); },true);
AddCustomFunction("abs", delegate(double p) { return System.Math.Abs(p); },true);
AddCustomFunction("asin", delegate(double p) { return System.Math.Asin(p); },true);
AddCustomFunction("acos", delegate(double p) { return System.Math.Acos(p); },true);
AddCustomFunction("atan", delegate(double p) { return System.Math.Atan(p); },true);
AddCustomFunction("atan2",2, delegate(double[] p) { return System.Math.Atan2(p[0],p[1]); },true);
AddCustomFunction("sqrt", delegate(double p) { return System.Math.Sqrt(p); },true);
AddCustomFunction("sign", delegate(double p) { return System.Math.Sign(p); },true);
AddCustomFunction("floor", delegate(double p) { return System.Math.Floor(p); },true);
AddCustomFunction("ceil", delegate(double p) { return System.Math.Ceiling(p); },true);
AddCustomFunction("min",2, delegate(double[] p) { return System.Math.Min(p[0],p[1]); },true);
AddCustomFunction("max",2, delegate(double[] p) { return System.Math.Max(p[0],p[1]); },true);
AddCustomFunction("sinh", delegate(double p) { return System.Math.Sinh(p); },true);
AddCustomFunction("exp", delegate(double p) { return System.Math.Exp(p); },true);
AddCustomFunction("cosh", delegate(double p) { return System.Math.Cosh(p); },true);
AddCustomFunction("tanh", delegate(double p) { return System.Math.Tanh(p); },true);
AddCustomFunction("log", delegate(double p) { return System.Math.Log(p); },true);
AddCustomFunction("log10", delegate(double p) { return System.Math.Log10(p); },true);
AddCustomFunction("round", delegate(double p) { return System.Math.Round(p); },true);
}
public void AddCustomFunction(string name, int paramCount, System.Func<double[],double> func, bool enableSymbolicationTimeEvaluation = false)
{
if (paramCount>MaxCustomFunctionParamCount)
{
throw new ESTooManyParametersException("Custom functions can have no more than " + MaxCustomFunctionParamCount + " parameters");
}
customFuncs[name] = new CustomFunction(name, paramCount, func, enableSymbolicationTimeEvaluation);
}
public void AddCustomFunction(string name, int paramCount, System.Func<object[],double> func, bool enableSymbolicationTimeEvaluation = false)
{
if (paramCount>MaxCustomFunctionParamCount)
{
throw new ESTooManyParametersException("Custom functions can have no more than " + MaxCustomFunctionParamCount + " parameters");
}
customFuncs[name] = new CustomFunction(name, paramCount, func, enableSymbolicationTimeEvaluation);
}
public void AddCustomFunction(string name, System.Func<double,double> func, bool enableSymbolicationTimeEvaluation = false)
{
customFuncs[name] = new CustomFunction(name, func, enableSymbolicationTimeEvaluation);
}
public void RemoveCustomFunction(string name)
{
customFuncs.Remove (name);
}
public Variable GetGlobalVariable(string name)
{
return globalConstants[name];
}
public Variable SetGlobalVariable(string name, double value)
{
Variable variable;
if (globalConstants.TryGetValue(name, out variable))
{
if (variable.stringValue != null)
{
throw new ESParameterTypeChangedException("Can not change type of existing parameter " + name);
}
variable.value = value;
return variable;
}
else
{
Variable v = new Variable(name,value);
globalConstants.Add (name,v);
return v;
}
}
public Variable SetGlobalVariable(string name, string value)
{
if (value == null)
{
throw new System.ArgumentException("Null is not acceptable string parameter value.");
}
Variable variable;
if (globalConstants.TryGetValue(name, out variable))
{
if (variable.stringValue == null)
{
throw new ESParameterTypeChangedException("Can not change type of existing parameter " + name);
}
variable.stringValue = value;
return variable;
}
else
{
Variable v = new Variable(name,value);
globalConstants.Add (name,v);
return v;
}
}
public bool RemoveGlobalVariable(string name)
{
return globalConstants.Remove(name);
}
public void ClearGlobalVariables()
{
globalConstants.Clear();
}
public double EvaluateExpression(string formula)
{
return SymbolicateExpression(formula,(string[])null).Evaluate();
}
public Expression SymbolicateExpression(string formula, string localVariable)
{
return SymbolicateExpression(formula,new string[1]{localVariable});
}
public Expression SymbolicateExpression(string formula, string[] localVariables = null) {
Expression newExpression = new Expression();
if (localVariables != null)
{
foreach (var localVariableName in localVariables)
{
if (localVariableName[0] == '$')
{
newExpression.SetVariable(localVariableName.Substring(1,localVariableName.Length-1).Trim(),"");
}
else
{
newExpression.SetVariable(localVariableName.Trim(),0.0);
}
}
}
formula = SolverTools.RemoveWhiteSpace(formula);
// Check validity
try
{
ValidityChecker.CheckValidity(formula);
}
catch (System.Exception ex)
{
throw ex;
}
Symbol s = Symbolicate(formula, 0, formula.Length,newExpression);
newExpression.root = s;
return newExpression;
}
static double ParseSymbols(SymbolList syms)
{
bool transformNextValue = false;
double sum = 0;
double curTerm = 0;
SymbolType prevOper = SymbolType.OperatorAdd;
int len = syms.symbols.Count;
var symbolList = syms.symbols;
for (int i=0;i<len;i++)
{
var s = symbolList[i];
switch (s.type)
{
case SymbolType.RealValue:
case SymbolType.SubExpression:
case SymbolType.StringLiteral:
case SymbolType.StringVariable:
{
double value;
if (transformNextValue)
{
var funcSymbol = symbolList[i-1];
switch (funcSymbol.type)
{
case SymbolType.Pow:
{
value = System.Math.Pow(GetSymbolValue(s),GetSymbolValue(symbolList[i+1]));
i++;
break;
}
case SymbolType.FuncCustom:
{
var customFunc = (CustomFunction)funcSymbol.ptr;
if (customFunc.paramCount == 1 && customFunc.func1d != null)
{
value = customFunc.Invoke(GetSymbolValue(s));
}
else if (customFunc.funcmo != null)
{
object[] p = new object[MaxCustomFunctionParamCount];
p[0] = (!s.IsStringType()) ? (object)GetSymbolValue(s) : (object)s.stringValue;
for (int g=1;g<customFunc.paramCount;g++)
{
p[g] = (!symbolList[i+1].IsStringType()) ? (object)GetSymbolValue(symbolList[i+1]) : (object)symbolList[i+1].stringValue;
i++;
}
value = customFunc.Invoke(p);
}
else
{
double[] p = new double[MaxCustomFunctionParamCount];
p[0] = GetSymbolValue(s);
for (int g=1;g<customFunc.paramCount;g++)
{
p[g] = GetSymbolValue(symbolList[i+1]);
i++;
}
value = customFunc.Invoke(p);
}
break;
}
default:
throw new System.Exception("Very unexpected parse error.");
}
transformNextValue = false;
}
else
{
// The value cant be a string because they appear only as parameters to functions
value = GetSymbolValue(s);
}
switch (prevOper)
{
case SymbolType.OperatorMultiply:
curTerm *= value;
break;
case SymbolType.OperatorDivide:
curTerm /= value;
break;
case SymbolType.OperatorAdd:
sum += curTerm;
curTerm = value;
break;
default:
throw new System.Exception("Very unexpected parse error.");
}
prevOper = SymbolType.OperatorMultiply;
break;
}
case SymbolType.OperatorDivide:
case SymbolType.OperatorAdd:
prevOper = s.type;
break;
case SymbolType.Pow:
case SymbolType.FuncCustom:
transformNextValue = true;
break;
default:
throw new System.Exception("Unable to parse symbols.");
}
}
// Remember to add the final term to sum
return sum + curTerm;
}
public static double GetSymbolValue(Symbol s)
{
return (s.type == SymbolType.RealValue) ? (s.ptr == null ? s.value : ((Variable)s.ptr).value ) : ParseSymbols((SymbolList)s.ptr);
}
Symbol SymbolicateValue(string formula, int begin, int end, Expression exp)
{
if (formula[begin] == '+')
{
begin++;
}
// Check for string value
if (formula[begin] == '\'' && formula[end - 1] == '\'')
{
var svalue = formula.Substring(begin+1,end-begin-2).Replace("\\'","'");
return new Symbol(svalue);
}
int depth=0;
for (int k = begin; k < end; k++)
{
if (formula[k]=='(')
depth++;
else if (formula[k]==')')
depth--;
else if (depth == 0 && formula[k] == '^')
{
// Check for small integer powers: they will be done using multiplication instead!
Symbol lhs = Symbolicate(formula,begin,k,exp);
Symbol rhs = Symbolicate(formula,k+1,end,exp);
var newSubExpression = new SymbolList();
if (end-k-1 == 1 && lhs.type == SymbolType.RealValue && formula.Substring(k+1,end-k-1)=="2")
{
// Second power found
newSubExpression.Append(lhs);
newSubExpression.Append(lhs);
}
else if (end-k-1 == 1 && lhs.type == SymbolType.RealValue && formula.Substring(k+1,end-k-1)=="3")
{
// Second power found
newSubExpression.Append(lhs);
newSubExpression.Append(lhs);
newSubExpression.Append(lhs);
}
else
{
newSubExpression.Append(new Symbol(SymbolType.Pow));
newSubExpression.Append(lhs);
newSubExpression.Append(rhs);
}
Symbol newSymbol = new Symbol(SymbolType.SubExpression);
newSymbol.subExpression = newSubExpression;
return newSymbol;
}
}
if (formula[begin] == '(' && formula[end - 1] == ')')
{
var s = Symbolicate(formula, begin + 1, end - 1,exp);
s.Simplify();
return s;
}
double valueAsRealNumber;
if (double.TryParse(formula.Substring(begin,end-begin),out valueAsRealNumber))
{
return new Symbol(valueAsRealNumber);
}
// Check if the value is transformed by a function
if (formula[end-1]==')') {
int i = begin;
while (i < end-1) {
if (formula[i]=='(') {
break;
}
i++;
}
string funcName = formula.Substring(begin,i-begin);
CustomFunction customFunc;
if (customFuncs.TryGetValue(funcName,out customFunc))
{
int requiredParameterCount = customFunc.paramCount;
int foundParameterCount = SolverTools.CountParameters(formula,begin,end);
if (requiredParameterCount == foundParameterCount) {
if (requiredParameterCount == 1) {
SymbolList newSubExpression = new SymbolList();
newSubExpression.Append(new Symbol(customFunc));
newSubExpression.Append(Symbolicate(formula,i+1,end-1,exp));
return new Symbol(newSubExpression);
}
else {
List<SolverTools.IntPair> parameters = SolverTools.ParseParameters(formula,i,end);
SymbolList newSubExpression = new SymbolList();
newSubExpression.Append(new Symbol(customFunc));
for (int k=0;k<requiredParameterCount;k++) {
Symbol p = Symbolicate(formula,parameters[k].first,parameters[k].second,exp);
newSubExpression.Append(p);
}
Symbol newSymbol = new Symbol(SymbolType.SubExpression);
newSymbol.subExpression = newSubExpression;
return newSymbol;
}
}
else
{
throw new ESInvalidParametersException(customFunc.name + " expects " + requiredParameterCount + " parameters, " + foundParameterCount + " given.");
}
}
else
{
throw new ESInvalidFunctionNameException(funcName);
}
}
var valueName = formula.Substring(begin,end-begin);
// Then a local constant specific to our expression
Variable variable;
if (exp.constants.TryGetValue(valueName,out variable)) {
return new Symbol(variable);
}
// Non immutable globals
if (globalConstants.TryGetValue(valueName,out variable)) {
return new Symbol(variable);
}
// Immutable globals
double constDouble;
if (immutableGlobalConstants.TryGetValue(valueName, out constDouble))
{
return new Symbol(constDouble);
}
// Found an unknown value name. Check policy to see what to do.
Variable v = null;
switch (undefinedVariablePolicy)
{
case UndefinedVariablePolicy.DefineExpressionLocalVariable:
v = new Variable(valueName,0);
exp.constants.Add(valueName,v);
return new Symbol(v);
case UndefinedVariablePolicy.DefineGlobalVariable:
v = new Variable(valueName,0);
globalConstants.Add(valueName,v);
return new Symbol(v);
default:
throw new ESUnknownExpressionException(valueName);
}
}
Symbol SymbolicateMonome(string formula, int begin, int end, Expression exp)
{
var symbols = new SymbolList();
int sign = 0;
int i = begin - 1;
int currentTermBegin = begin;
int numValues = 0;
int currentDepth = 0;
double constMultiplier = 1.0;
bool divideNext = false;
bool constMultiplierUsed = false;
for (;;)
{
i++;
if (i == end || (currentDepth == 0 && i > begin && (formula[i] == '*' || formula[i] == '/')))
{
numValues++;
// Unless we are dealing with a monome, symbolicate the term
Symbol newSymbol = SymbolicateValue(formula, formula[currentTermBegin] == '-' ? currentTermBegin + 1 : currentTermBegin, i,exp);
// Check if we can simplify the generated symbol
if (newSymbol.IsImmutableConstant() && newSymbol.IsRealValueType())
{
// Constants are multiplied/divided together
if (divideNext)
constMultiplier /= GetSymbolValue(newSymbol);
else
constMultiplier *= GetSymbolValue(newSymbol);
constMultiplierUsed = true;
}
else
{
if (divideNext)
symbols.Append(new Symbol(SymbolType.OperatorDivide));
newSymbol.Simplify();
symbols.Append(newSymbol);
}
if (i == end) {
break;
}
divideNext = formula[i] == '/';
currentTermBegin = i + 1;
}
else if (formula[i] == '(')
{
currentDepth++;
}
else if (formula[i] == ')')
{
currentDepth--;
}
else if (formula[i] == '-' && currentDepth == 0 && !(i>begin && formula[i-1] == '^') )
{
sign++;
}
}
// If the generated monome has negative number of minus signs, then we append *-1 to end of the list, or if the preceding symbol is constant real number that is part of a monome, we multiply it.
if (sign % 2 == 1)
{
constMultiplier =-constMultiplier;
}
if (constMultiplierUsed || sign % 2 == 1)
{
// Add the const multiplier to the expression
if (symbols.Length>0 && symbols.first.type==SymbolType.OperatorDivide)
{
// Put to the begin of the expression we are building
symbols.symbols.Insert(0,new Symbol(constMultiplier));
}
else if (symbols.Length > 0 && symbols.last.type == SymbolType.SubExpression && symbols.last.IsMonome())
{
// Add inside the last subexpression
SymbolList leftSideExpression = symbols.last.subExpression;
if (leftSideExpression.last.type==SymbolType.RealValue && leftSideExpression.last.IsImmutableConstant())
{
leftSideExpression.SetSymbolAtIndex(leftSideExpression.Length-1,new Symbol(leftSideExpression.last.value*constMultiplier));
}
else
{
leftSideExpression.Append(new Symbol(constMultiplier));
}
}
else
{
// Put to the end of the expression we are building
symbols.Append(new Symbol(constMultiplier));
}
}
// Check if the final monome is just a real number, in which case we don't have to return a subexpression type
if (symbols.Length == 1 && symbols.first.IsImmutableConstant() && symbols.first.IsRealValueType())
{
return symbols.first.type == SymbolType.RealValue ? symbols.first : new Symbol(GetSymbolValue(symbols.first));
}
Symbol s = new Symbol(SymbolType.SubExpression);
s.subExpression = symbols;
s.Simplify();
return s;
}
Symbol Symbolicate(string formula, int begin, int end, Expression exp)
{
var symbols = new SymbolList();
int i = begin - 1;
int currentTermBegin = formula[begin] == '+' ? begin + 1 : begin;
int currentDepth = 0;
for (;;)
{
i++;
if (i == end || (currentDepth == 0 && i > begin && (formula[i - 1] != '*' && formula[i - 1] != '/') && (formula[i] == '+' || formula[i] == '-')))
{
symbols.Append(SymbolicateMonome(formula, currentTermBegin, i,exp));
if (i == end)
{
break;
}
else {
// The sign of the term is included in the next monome only if its minus
currentTermBegin = (formula[i] == '-') ? i : i + 1;
symbols.Append(new Symbol(SymbolType.OperatorAdd));
}
}
else if (formula[i] == '(')
{
currentDepth++;
}
else if (formula[i] == ')')
{
currentDepth--;
}
else if (formula[i] == '^')
{
i = SolverTools.ParseUntilEndOfExponent(formula,i+1,end) - 1;
}
}
// If at this point we only have one real number left, just return it as a simple value.
if (symbols.Length == 1 && symbols.first.type == SymbolType.RealValue)
{
return symbols.first;
}
// We don't have that single expression, but:
// Now that we are here, we have symbol list which consists of only addition operators and value types. This is a great place to sum constant values together!
double constantSum = 0;
bool addedConstants = false;
for (int j = 0; j < symbols.Length; j++)
{
Symbol s = symbols.getSymbol(j);
if (s.IsImmutableConstant() && s.IsRealValueType()) {
constantSum += s.value;
addedConstants = true;
if (j == symbols.Length - 1)
{
// Destroy preceding +
symbols.symbols.RemoveAt (j);
break;
}
symbols.symbols.RemoveAt(j);
symbols.symbols.RemoveAt(j);
j--;
}
else
{
// Skip the following + symbol
j++;
}
}
if (addedConstants)
{
if (symbols.Length > 0 && symbols.getSymbol(symbols.Length - 1).IsRealValueType())
{
symbols.Append(new Symbol(SymbolType.OperatorAdd));
}
symbols.Append(new Symbol(constantSum));
}
// Finally, if the symbolicated sum is just a single real number, even varying, return just a simple symbol
if (symbols.Length == 1 && symbols.getSymbol(0).type == SymbolType.RealValue)
{
Symbol s = symbols.getSymbol(0);
return s;
}
// Optimization: get rid of unnecessary jumps to subexpressions
for (int j=0;j<symbols.Length;j++)
{
var s = symbols.getSymbol(j);
if (s.type==SymbolType.SubExpression)
{
var subExpression = s.subExpression;
int subExpressionLength = subExpression.Length;
s.CopyValuesFrom(subExpression.first);
for (int k=1;k<subExpressionLength;k++)
{
symbols.InsertBefore(j+k,subExpression.getSymbol(k));
}
j += subExpressionLength;
}
}
// We have turned the formula into a subexpression symbol
Symbol returnSymbol = new Symbol(symbols);
returnSymbol.Simplify();
return returnSymbol;
}
}
}