/// <summary> /// Perform an add on two expression values. a+b /// </summary> /// <param name="a">The first argument.</param> /// <param name="b">The second argument.</param> /// <returns> /// The result of adding two numbers. Concat for strings. If one is a /// string, the other is converted to string. If no string, then if /// one is float, both are converted to int. /// </returns> public static ExpressionValue Add(ExpressionValue a, ExpressionValue b) { if (a.IsString || b.IsString) { return new ExpressionValue(a.ToStringValue() + b.ToStringValue()); } if (a.IsInt && b.IsInt) { return new ExpressionValue(a.ToIntValue() + b.ToIntValue()); } return new ExpressionValue(a.ToFloatValue() + b.ToFloatValue()); }
/// <summary> /// Construct the program node. /// </summary> /// <param name="theOwner">The owner of the node.</param> /// <param name="theTemplate">The opcode that this node is based on.</param> /// <param name="theArgs">The child nodes to this node.</param> public ProgramNode(EncogProgram theOwner, IProgramExtensionTemplate theTemplate, ITreeNode[] theArgs) { _owner = theOwner; _data = new ExpressionValue[theTemplate.DataSize]; _template = theTemplate; AddChildNodes(theArgs); for (int i = 0; i < _data.Length; i++) { _data[i] = new ExpressionValue((long) 0); } }
/// <summary> /// Perform a division on two expression values. a/b An Encog division by /// zero exception can occur. If one param is a float, the other is converted /// to a float. /// </summary> /// <param name="a">The first argument, must be numeric.</param> /// <param name="b">The second argument, must be numeric.</param> /// <returns> The result of the operation.</returns> public static ExpressionValue Div(ExpressionValue a, ExpressionValue b) { if (a.IsInt && b.IsInt) { long i = b.ToIntValue(); if (i == 0) { throw new DivisionByZeroError(); } return new ExpressionValue(a.ToIntValue()/i); } double denom = b.ToFloatValue(); if (Math.Abs(denom) < EncogFramework.DefaultDoubleEqual) { throw new DivisionByZeroError(); } return new ExpressionValue(a.ToFloatValue()/denom); }
/// <summary> /// Set a variable value by name. /// </summary> /// <param name="name">The variable name.</param> /// <param name="value">The value.</param> public void SetVariable(String name, ExpressionValue value) { lock (this) { if (_varMap.ContainsKey(name)) { int index = _varMap[name]; _variables[index] = value; } else { _varMap[name] = _variables.Count; _variables.Add(value); } } }
/// <summary> /// Set a variable floating point value by index. /// </summary> /// <param name="index">The index.</param> /// <param name="value">The value.</param> public void SetVariable(int index, double value) { _variables[index] = new ExpressionValue(value); }
/// <summary> /// Construct a expression based on an expression. /// </summary> /// <param name="other">The value to construct.</param> public ExpressionValue(ExpressionValue other) { switch (_expressionType = other._expressionType) { case EPLValueType.BooleanType: _boolValue = other._boolValue; _floatValue = 0; _stringValue = null; _intValue = 0; _enumType = -1; break; case EPLValueType.FloatingType: _floatValue = other._floatValue; _boolValue = false; _stringValue = null; _intValue = 0; _enumType = -1; break; case EPLValueType.IntType: _intValue = other._intValue; _boolValue = false; _floatValue = 0; _stringValue = null; _enumType = -1; break; case EPLValueType.StringType: _stringValue = other._stringValue; _boolValue = false; _floatValue = 0; _intValue = 0; _enumType = -1; break; case EPLValueType.EnumType: _intValue = other._intValue; _boolValue = false; _floatValue = 0; _stringValue = null; _enumType = other._enumType; break; default: throw new EARuntimeError("Unsupported type."); } }
/// <summary> /// Perform a subtract on two expression values. a-b If one param is a float, /// the other is converted to a float. /// </summary> /// <param name="a">The first argument, must be numeric.</param> /// <param name="b">The second argument, must be numeric.</param> /// <returns>The result of the operation.</returns> public static ExpressionValue Sub(ExpressionValue a, ExpressionValue b) { if (a.IsInt && b.IsInt) { return new ExpressionValue(a.ToIntValue() - b.ToIntValue()); } return new ExpressionValue(a.ToFloatValue() - b.ToFloatValue()); }
/// <summary> /// Perform a protected div on two expression values. a/b Division by zero /// results in 1. /// </summary> /// <param name="a">The first argument, must be numeric.</param> /// <param name="b">The second argument, must be numeric.</param> /// <returns>The result of the operation.</returns> public static ExpressionValue ProtectedDiv(ExpressionValue a, ExpressionValue b) { if (a.IsInt && b.IsInt) { long i = b.ToIntValue(); if (i == 0) { return new ExpressionValue(1); } return new ExpressionValue(a.ToIntValue()/i); } double denom = b.ToFloatValue(); if (Math.Abs(denom) < EncogFramework.DefaultDoubleEqual) { return new ExpressionValue(1); } return new ExpressionValue(a.ToFloatValue()/denom); }
/// <summary> /// Perform a protected div on two expression values. a/b If one param is a /// float, the other is converted to a float. /// </summary> /// <param name="a">The first argument, must be numeric.</param> /// <param name="b">The second argument, must be numeric.</param> /// <returns>The result of the operation.</returns> public static ExpressionValue Pow(ExpressionValue a, ExpressionValue b) { if (a.IsInt && b.IsInt) { return new ExpressionValue(Math.Pow(a.ToIntValue(), b.ToIntValue())); } return new ExpressionValue(Math.Pow(a.ToFloatValue(), b.ToFloatValue())); }
/// <summary> /// Perform a non-equal on two expressions. Booleans, ints and strings must /// exactly non-equal. Floating point must be non-equal within the default /// Encog tolerance. /// </summary> /// <param name="a">The first parameter to check.</param> /// <param name="b">The second parameter to check.</param> /// <returns>True/false.</returns> public static ExpressionValue Notequ(ExpressionValue a, ExpressionValue b) { if (a.ExprType == EPLValueType.BooleanType) { return new ExpressionValue(a.ToBooleanValue() != b.ToBooleanValue()); } if (a.ExprType == EPLValueType.EnumType) { return new ExpressionValue(a.ToIntValue() != b.ToIntValue() && a.EnumType == b.EnumType); } if (a.ExprType == EPLValueType.StringType) { return new ExpressionValue(!a.ToStringValue().Equals( b.ToStringValue())); } double diff = Math.Abs(a.ToFloatValue() - b.ToFloatValue()); return new ExpressionValue(diff > EncogFramework.DefaultDoubleEqual); }