-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMathExpr.cs
More file actions
238 lines (204 loc) · 11.3 KB
/
MathExpr.cs
File metadata and controls
238 lines (204 loc) · 11.3 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
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
using StringMath.Expressions;
using System;
using System.Collections.Generic;
using System.Linq;
namespace StringMath
{
/// <summary>A mathematical expression.</summary>
public class MathExpr
{
private readonly EvaluateExpression _evaluator;
private readonly VariablesCollection _variables = new VariablesCollection();
private IReadOnlyCollection<string>? _localVariables;
private IReadOnlyCollection<string>? _allVariables;
private double? _cachedResult;
internal IExpression Expression { get; }
/// <summary>The <see cref="IMathContext"/> in which this expression is evaluated.</summary>
public IMathContext Context { get; }
/// <summary>A collection of variable names excluding globals from <see cref="Variables"/>.</summary>
public IReadOnlyCollection<string> LocalVariables => _localVariables ??= Variables.Where(x => !VariablesCollection.Default.Contains(x)).ToList();
/// <summary>A collection of variable names including globals extracted from the <see cref="Text"/>.</summary>
public IReadOnlyCollection<string> Variables
{
get
{
if (_allVariables == null)
{
var extractor = new ExtractVariables();
extractor.Visit(Expression);
_allVariables = extractor.Variables;
}
return _allVariables;
}
}
/// <summary>Constructs a <see cref="MathExpr"/> from a string.</summary>
/// <param name="text">The math expression.</param>
public MathExpr(string text) : this(text, new MathContext(MathContext.Default))
{
}
/// <summary>Constructs a <see cref="MathExpr"/> from a string.</summary>
/// <param name="text">The math expression.</param>
/// <param name="context">The <see cref="IMathContext"/> in which this expression is evaluated.</param>
public MathExpr(string text, IMathContext context) : this(text.Parse(context), context)
{
}
/// <summary>Constructs a <see cref="MathExpr"/> from an expression tree.</summary>
/// <param name="expression">The expression tree.</param>
/// <param name="context">The <see cref="IMathContext"/> in which this expression is evaluated.</param>
internal MathExpr(IExpression expression, IMathContext context)
{
expression.EnsureNotNull(nameof(expression));
context.EnsureNotNull(nameof(context));
Context = context;
Expression = expression;
_evaluator = new EvaluateExpression(Context, _variables);
}
/// <summary>The result of the expression.</summary>
/// <remarks>The variables used in the expression must be set before getting the result.</remarks>
public double Result
{
get
{
if (!_cachedResult.HasValue)
{
var result = (ConstantExpression)_evaluator.Visit(Expression);
_cachedResult = result.Value;
}
return _cachedResult.Value;
}
}
/// <summary>Creates a string representation of the current expression.</summary>
public string Text => Expression.ToString(Context);
/// <summary>Substitutes the variable with the given value.</summary>
/// <param name="name">The name of the variable.</param>
/// <param name="value">The new value.</param>
public MathExpr Substitute(string name, double value)
{
if (VariablesCollection.Default.TryGetValue(name, out _))
{
throw MathException.ReadonlyVariable(name);
}
if (LocalVariables.Contains(name))
{
_cachedResult = null;
_variables[name] = value;
}
else
{
throw MathException.MissingVariable(name);
}
return this;
}
/// <summary>Compiles a <see cref="MathExpr"/> into a delegate.</summary>
/// <returns>A type safe delegate.</returns>
public Func<double> Compile()
{
var exp = new CompileExpression(_variables).Compile<Func<IMathContext, double>>(Expression);
return () => exp(Context);
}
/// <summary>Compiles a <see cref="MathExpr"/> into a delegate.</summary>
/// <returns>A type safe delegate.</returns>
public Func<double, double> Compile(string var)
{
var exp = new CompileExpression(_variables).Compile<Func<IMathContext, double, double>>(Expression, var);
return (double x) => exp(Context, x);
}
/// <summary>Compiles a <see cref="MathExpr"/> into a delegate.</summary>
/// <returns>A type safe delegate.</returns>
public Func<double, double, double> Compile(string var1, string var2)
{
var exp = new CompileExpression(_variables).Compile<Func<IMathContext, double, double, double>>(Expression, var1, var2);
return (x, y) => exp(Context, x, y);
}
/// <summary>Compiles a <see cref="MathExpr"/> into a delegate.</summary>
/// <returns>A type safe delegate.</returns>
public Func<double, double, double, double> Compile(string var1, string var2, string var3)
{
var exp = new CompileExpression(_variables).Compile<Func<IMathContext, double, double, double, double>>(Expression, var1, var2, var3);
return (x, y, z) => exp(Context, x, y, z);
}
/// <summary>Compiles a <see cref="MathExpr"/> into a delegate.</summary>
/// <returns>A type safe delegate.</returns>
public Func<double, double, double, double, double> Compile(string var1, string var2, string var3, string var4)
{
var exp = new CompileExpression(_variables).Compile<Func<IMathContext, double, double, double, double, double>>(Expression, var1, var2, var3, var4);
return (x, y, z, w) => exp(Context, x, y, z, w);
}
/// <summary>Compiles a <see cref="MathExpr"/> into a delegate.</summary>
/// <returns>A type safe delegate.</returns>
public Func<double, double, double, double, double, double> Compile(string var1, string var2, string var3, string var4, string var5)
{
var exp = new CompileExpression(_variables).Compile<Func<IMathContext, double, double, double, double, double, double>>(Expression, var1, var2, var3, var4, var5);
return (x, y, z, w, q) => exp(Context, x, y, z, w, q);
}
/// <summary>Converts a string to a <see cref="MathExpr"/>.</summary>
/// <param name="value">The value to convert.</param>
public static implicit operator MathExpr(string value) => new MathExpr(value);
/// <summary>Evaluates a <see cref="MathExpr"/>.</summary>
/// <param name="expression"></param>
public static implicit operator double(MathExpr expression) => expression.Result;
/// <inheritdoc cref="Substitute(string, double)" />
public double this[string name]
{
set => Substitute(name, value);
}
/// <summary>Add a new binary operator or overwrite an existing operator implementation.</summary>
/// <param name="name">The operator's string representation.</param>
/// <param name="operation">The operation to execute for this operator.</param>
/// <param name="precedence"><see cref="Precedence.UserDefined"/> precedence by default.</param>
/// <returns>The current math expression.</returns>
/// <remarks>Operators are inherited from <see cref="AddOperator(string, Func{double, double, double}, Precedence?)"/>.</remarks>
public MathExpr SetOperator(string name, Func<double, double, double> operation, Precedence? precedence = default)
{
_cachedResult = null;
Context.RegisterBinary(name, operation, precedence);
return this;
}
/// <summary>Add a new unary operator or overwrite an existing operator implementation. <see cref="Precedence"/> is always <see cref="Precedence.Prefix" />.</summary>
/// <param name="name">The operator's string representation.</param>
/// <param name="operation">The operation to execute for this operator.</param>
/// <returns>The current math expression.</returns>
/// <remarks>Operators are inherited from <see cref="AddOperator(string, Func{double, double})"/>.</remarks>
public MathExpr SetOperator(string name, Func<double, double> operation)
{
_cachedResult = null;
Context.RegisterUnary(name, operation);
return this;
}
/// <summary>Adds a new function or overwrites and existing one.</summary>
/// <param name="name">The name of the function.</param>
/// <param name="body">The code to execute for this function.</param>
/// <returns>The current math expression.</returns>
public MathExpr SetFunction(string name, Func<double[], double> body)
{
_cachedResult = null;
Context.RegisterFunction(name, body);
return this;
}
/// <summary>Add a new binary operator or overwrite an existing operator implementation.</summary>
/// <param name="name">The operator's string representation.</param>
/// <param name="operation">The operation to execute for this operator.</param>
/// <param name="precedence"><see cref="Precedence.UserDefined"/> precedence by default.</param>
/// <remarks>Operators will be available in all <see cref="MathExpr"/> expressions.</remarks>
public static void AddOperator(string name, Func<double, double, double> operation, Precedence? precedence = default)
=> MathContext.Default.RegisterBinary(name, operation, precedence);
/// <summary>Add a new unary operator or overwrite an existing operator implementation. <see cref="Precedence"/> is always <see cref="Precedence.Prefix" />.</summary>
/// <param name="name">The operator's string representation.</param>
/// <param name="operation">The operation to execute for this operator.</param>
/// <returns>The current math expression.</returns>
/// <remarks>Operators will be available in all <see cref="MathExpr"/> expressions. Operators are inherited from <see cref="AddOperator(string, Func{double, double})"/>.</remarks>
public static void AddOperator(string name, Func<double, double> operation)
=> MathContext.Default.RegisterUnary(name, operation);
/// <summary>Adds a new function or overwrites and existing one.</summary>
/// <param name="name">The name of the function.</param>
/// <param name="body">The code to execute for this function.</param>
public static void AddFunction(string name, Func<double[], double> body)
=> MathContext.Default.RegisterFunction(name, body);
/// <inheritdoc cref="Substitute(string, double)"/>
/// <remarks>Variables will be available in all <see cref="MathExpr"/> expressions.</remarks>
public static void AddVariable(string name, double value)
=> VariablesCollection.Default[name] = value;
/// <inheritdoc cref="Text"/>
public override string ToString() => Text;
}
}