本文整理汇总了C#中ConstantExpression类的典型用法代码示例。如果您正苦于以下问题:C# ConstantExpression类的具体用法?C# ConstantExpression怎么用?C# ConstantExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConstantExpression类属于命名空间,在下文中一共展示了ConstantExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OrIsShortcircuit
public void OrIsShortcircuit()
{
IExpression trueexpr = new ConstantExpression(true);
IExpression dividebyzero = new BinaryOperatorExpression(new ConstantExpression(1), new ConstantExpression(0), BinaryOperator.Divide);
Assert.IsTrue((bool)(new BooleanExpression(trueexpr, dividebyzero, BooleanOperator.Or)).Evaluate(null));
}
示例2: Parse
public Node Parse()
{
Expression curExpr = null;
for (; ; ) {
_tokens.MoveNext();
Token t = _tokens.Current;
switch (t.Kind) {
case TokenKinds.EOF:
return curExpr;
case TokenKinds.Operator:
if (curExpr == null) {
throw new SyntaxErrorException("expected value before operator");
}
curExpr = ActionExpression.Operator(((OperatorToken)t).DlrOperator,
curExpr,
(Expression)Parse());
break;
case TokenKinds.Constant:
curExpr = new ConstantExpression(((ConstantToken)t).Value);
break;
}
}
}
示例3: CreateNotExpression
public void CreateNotExpression()
{
IExpression trueexpr = new ConstantExpression(true);
NotExpression expression = new NotExpression(trueexpr);
Assert.AreEqual(trueexpr, expression.Expression);
}
示例4: AndIsShortcircuit
public void AndIsShortcircuit()
{
IExpression falseexpr = new ConstantExpression(false);
IExpression dividebyzero = new BinaryOperatorExpression(new ConstantExpression(1), new ConstantExpression(0), BinaryOperator.Divide);
Assert.IsFalse((bool)(new BooleanExpression(falseexpr, dividebyzero, BooleanOperator.And)).Evaluate(null));
}
示例5: EvaluateNotExpression
public void EvaluateNotExpression()
{
IExpression trueexpr = new ConstantExpression(true);
IExpression falseexpr = new ConstantExpression(false);
Assert.IsFalse((bool)(new NotExpression(trueexpr)).Evaluate(null));
Assert.IsTrue((bool)(new NotExpression(falseexpr)).Evaluate(null));
}
示例6: SetVariable
public void SetVariable()
{
IExpression expression = new ConstantExpression(1);
BindingEnvironment environment = new BindingEnvironment();
ICommand command = new SetCommand("spam", expression);
command.Execute(environment);
Assert.AreEqual(1, environment.GetValue("spam"));
}
示例7: EvaluateOr
public void EvaluateOr()
{
IExpression trueexpr = new ConstantExpression(true);
IExpression falseexpr = new ConstantExpression(false);
Assert.IsTrue((bool)(new BooleanExpression(trueexpr, trueexpr, BooleanOperator.Or)).Evaluate(null));
Assert.IsTrue((bool)(new BooleanExpression(trueexpr, falseexpr, BooleanOperator.Or)).Evaluate(null));
Assert.IsTrue((bool)(new BooleanExpression(falseexpr, trueexpr, BooleanOperator.Or)).Evaluate(null));
Assert.IsFalse((bool)(new BooleanExpression(falseexpr, falseexpr, BooleanOperator.Or)).Evaluate(null));
}
示例8: CreateBooleanExpression
public void CreateBooleanExpression()
{
IExpression trueexpr = new ConstantExpression(true);
IExpression falseexpr = new ConstantExpression(false);
BooleanExpression expression = new BooleanExpression(trueexpr, falseexpr, BooleanOperator.Or);
Assert.AreEqual(trueexpr, expression.Left);
Assert.AreEqual(falseexpr, expression.Right);
Assert.AreEqual(BooleanOperator.Or, expression.Operation);
}
示例9: CreateSetIndexCommand
public void CreateSetIndexCommand()
{
IExpression targetExpression = new ConstantExpression(1);
IExpression indexExpression = new ConstantExpression(2);
IExpression valueExpression = new ConstantExpression(3);
var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);
Assert.AreEqual(targetExpression, command.TargetExpression);
Assert.AreEqual(indexExpression, command.IndexExpression);
Assert.AreEqual(valueExpression, command.Expression);
}
示例10: CreateSetCommand
public void CreateSetCommand()
{
IExpression expression = new ConstantExpression("bar");
SetCommand command = new SetCommand("foo", expression);
Assert.IsNotNull(command);
Assert.IsNotNull(command.Target);
Assert.IsNotNull(command.Expression);
Assert.AreEqual("foo", command.Target);
Assert.AreEqual(expression, command.Expression);
}
示例11: ExecuteSetIndexCommandOnList
public void ExecuteSetIndexCommandOnList()
{
var list = new List<object>() { 1, 2, 2 };
IExpression targetExpression = new ConstantExpression(list);
IExpression indexExpression = new ConstantExpression(2);
IExpression valueExpression = new ConstantExpression(3);
var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);
command.Execute(null);
Assert.AreEqual(3, list[2]);
}
示例12: AConstant_ShouldReturnTheConstantValue
public void AConstant_ShouldReturnTheConstantValue()
{
// Arrange
var expectedValue = 3;
var sut = new ConstantExpression(expectedValue);
// Act
var actual = sut.Eval();
// Assert
Assert.AreEqual(expectedValue, actual);
}
示例13: ExecuteSetIndexCommandOnArray
public void ExecuteSetIndexCommandOnArray()
{
var array = new object[] { 1, 2, 2 };
IExpression targetExpression = new ConstantExpression(array);
IExpression indexExpression = new ConstantExpression(2);
IExpression valueExpression = new ConstantExpression(3);
var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);
command.Execute(null);
Assert.AreEqual(3, array[2]);
}
示例14: ExecuteSetIndexCommandOnDictionary
public void ExecuteSetIndexCommandOnDictionary()
{
var dictionary = new Hashtable();
IExpression targetExpression = new ConstantExpression(dictionary);
IExpression indexExpression = new ConstantExpression("foo");
IExpression valueExpression = new ConstantExpression("bar");
var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);
command.Execute(null);
Assert.AreEqual("bar", dictionary["foo"]);
}
示例15: Differentiate
/// <summary>
/// Performs reverse-mode (adjoint) automatic differentiation
/// </summary>
/// <param name="builder"></param>
/// <param name="dependentVariable"></param>
/// <param name="independentVariables"></param>
public static void Differentiate(BlockExpressionBuilder builder,
out IList<Expression> derivativeExpressions,
VectorParameterExpression dependentVariable,
params VectorParameterExpression[] independentVariables)
{
if (independentVariables.Length == 0)
{
derivativeExpressions = null;
return;
}
var block = builder.ToBlock();
// change the numbering of variables; arguments first, then locals
List<int>[] jLookup;
Function[] f;
// we want a list of functions which can be unary or binary (in principle high order if it makes sense) and a look-up
// each function is associated with a parameter, which is either an argument or a local
// for each function index we return a list of the indices of the functions that reference the parameter
GetFunctions(block, out f, out jLookup);
int N = dependentVariable.Index;
bool[] derivativeRequired; int[] derivativeExpressionIndex;
IdentifyNeccesaryDerivatives(N, jLookup, independentVariables, out derivativeRequired, out derivativeExpressionIndex);
// the list of operations needed to calculate the derivatives (*all* derivatives)
derivativeExpressions = new Expression[independentVariables.Length];
var dxNdx = new Expression[N + 1];
dxNdx[N] = new ConstantExpression<double>(1.0);
for (int i = N - 1; i >= 0; i--)
{
if (!derivativeRequired[i]) continue;
// dxN / dxi
// need to find all operation indices j such that p(j) contains i
// that is, all functions that have xi as an argument (these must therefore have a higher index than i)
VectorParameterExpression total = new ConstantExpression<double>(0);
var xi = f[i].Parameter;
//need sum of dfj/dxi dXN/dxj
foreach (var j in jLookup[i])
{
var fj = f[j];
var dfjdxi = Differentiate(fj, xi, builder); // dfj/dxi
var dXNdxj = dxNdx[j]; // dXN/dxj
var product = builder.AddProductExpression(dfjdxi, dXNdxj);
total = builder.AddAdditionExpression(total, product);
}
dxNdx[i] = total;
int targetIndex = derivativeExpressionIndex[i];
if (targetIndex != -1) derivativeExpressions[targetIndex] = total;
}
}