Class MathExpression
Simple math expression parser and evaluator
Namespace: ABB.Robotics.Math
Assembly: ABB.Robotics.Math.dll
Syntax
public class MathExpression
Remarks
Literal values should be using '.' as a decimal separator.
Constructors
MathExpression(Double)
Creates a simple MathExpression from the given value
Declaration
public MathExpression(double val)
Parameters
Type | Name | Description |
---|---|---|
Double | val | Value to build expression for |
Remarks
Calling Evaluate() will return the given value
MathExpression(Double, Int32)
Creates a simple MathExpression from the given value with the specified number of decimals
Declaration
public MathExpression(double val, int numDecimals)
Parameters
Type | Name | Description |
---|---|---|
Double | val | Value to build expression for |
Int32 | numDecimals | Maximum number of decimals to use |
Remarks
Calling Evaluate() will return the given value (rounded to the given number of decimals).
MathExpression(String)
Creates a new MathExpression class from a mathematical expression
Declaration
public MathExpression(string expression)
Parameters
Type | Name | Description |
---|---|---|
String | expression |
MathExpression(String, Boolean)
Creates a new MathExpression class from a mathematical expression, optionally converting arguments of trigonometric functions from degrees
Declaration
public MathExpression(string expression, bool convertDegrees)
Parameters
Type | Name | Description |
---|---|---|
String | expression | The mathematical expression |
Boolean | convertDegrees | If true convert the arguments of trigonometric functions from degrees to radians |
MathExpression(String, String[])
This constructor is obsolete.
Declaration
olete("Parameter 'variables' is not needed")]
public MathExpression(string expression, string[] variables)
Parameters
Type | Name | Description |
---|---|---|
String | expression | |
String[] | variables |
Properties
ErrorText
If the expression is invalid (IsValid returns false), this property may return more information why.
Declaration
public string ErrorText { get; }
Property Value
Type | Description |
---|---|
String |
ExpressionString
Gets or sets the source expression string
Declaration
public string ExpressionString { get; set; }
Property Value
Type | Description |
---|---|
String |
IsSimple
Returns True if the expression consists of a single value
Declaration
public bool IsSimple { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsValid
Returns True if the expression is valid and can be evaluated
Declaration
public bool IsValid { get; }
Property Value
Type | Description |
---|---|
Boolean |
VariableNames
Returns the names of variables used in the expression
Declaration
public IEnumerable<string> VariableNames { get; }
Property Value
Type | Description |
---|---|
IEnumerable<String> |
Methods
Evaluate()
Evaluates the expression
Declaration
public double Evaluate()
Returns
Type | Description |
---|---|
Double | The evaluated value of the expression |
Evaluate(IDictionary<String, Double>)
Evaluates the expression, using the passed in dictionary of variable values
Declaration
public double? Evaluate(IDictionary<string, double> variableValues)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<String, Double> | variableValues |
Returns
Type | Description |
---|---|
Nullable<Double> | The evaluated value of the expression |
Remarks
This overload is thread-safe, as long as no other methods on this class are called concurrently.
GetAllowedOperators()
Returns the allowed operators
Declaration
public static string[] GetAllowedOperators()
Returns
Type | Description |
---|---|
String[] |
GetLinearCoefficients(String[])
Returns the linear coefficients, if this expression is linear in the set of given variables. Otherwise it returns null.
Declaration
public double[] GetLinearCoefficients(string[] variables)
Parameters
Type | Name | Description |
---|---|---|
String[] | variables |
Returns
Type | Description |
---|---|
Double[] |
SetVariableValue(String, Double)
Sets the value of a variable, which will be used the next time the expression is evaluated.
Declaration
public void SetVariableValue(string variable, double val)
Parameters
Type | Name | Description |
---|---|---|
String | variable | Name of the variable to set |
Double | val | New value of the variable |
SetVariableValues(Double[])
This method is obsolete. Use SetVariableValue() instead.
Declaration
olete("Use SetVariableValue() instead")]
public void SetVariableValues(double[] values)
Parameters
Type | Name | Description |
---|---|---|
Double[] | values |
TrySetVariableValue(String, Double)
Sets the value of a variable, which will be used the next time the expression is evaluated.
Declaration
public bool TrySetVariableValue(string variable, double val)
Parameters
Type | Name | Description |
---|---|---|
String | variable | Name of the variable to set |
Double | val | New value of the variable |
Returns
Type | Description |
---|---|
Boolean | True if the variable could be set (i.e. if it exists), false otherwise |