Negate() public static method

Creates a UnaryExpression that represents an arithmetic negation operation.
Thrown when is null. Thrown when the unary minus operator is not defined for .Type.
public static Negate ( Expression expression ) : UnaryExpression
expression Expression An to set the property equal to.
return UnaryExpression
示例#1
0
        public void Method()
        {
            var expected =
                LinqExpression.Negate(
                    LinqExpression.Default(
                        typeof(bool)),
                    typeof(SampleClass).GetMethod(nameof(SampleClass.StaticFunctionWithArgument)));

            var actual = $@"
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

:s
    :unaryExpressionType xt:Negate ;
    :unaryOperand [
        :defaultType [
            :typeName ""System.Boolean"" ;
        ] ;
    ] ;
    :unaryMethod [
        :memberType [
            :typeName ""GraphEngine.Tests.SampleClass, GraphEngine.Tests"" ;
        ] ;
        :memberName ""StaticFunctionWithArgument"" ;
    ] ;
.
";

            ShouldBe(actual, expected);
        }
示例#2
0
        static E Compile(
            ElementExpression expression,
            Dictionary <string, ParameterExpression> spans,
            Dictionary <string, ParameterExpression> indices)
        {
            switch (expression.ArityKind)
            {
            case ArityKind.Element:
                return(Compile(expression.Element, spans, indices));

            case ArityKind.Unary:
                switch (expression.UnaryKind)
                {
                case UnaryExpressionKind.Exp:
                    throw new NotImplementedException();

                case UnaryExpressionKind.Log:
                    throw new NotImplementedException();

                default:
                    throw new NotSupportedException();
                }

            case ArityKind.Binary:
                switch (expression.BinaryKind)
                {
                case BinaryExpressionKind.Add:
                    return(E.Add(
                               Compile(expression.Expr1, spans, indices),
                               Compile(expression.Expr2, spans, indices)));

                case BinaryExpressionKind.Subtract:
                    return(E.Add(
                               Compile(expression.Expr1, spans, indices),
                               E.Negate(Compile(expression.Expr2, spans, indices))));

                case BinaryExpressionKind.Multiply:
                    return(E.Multiply(
                               Compile(expression.Expr1, spans, indices),
                               Compile(expression.Expr2, spans, indices)));

                case BinaryExpressionKind.Divide:
                    return(E.Divide(
                               Compile(expression.Expr1, spans, indices),
                               Compile(expression.Expr2, spans, indices)));

                default:
                    throw new NotSupportedException();
                }

            case ArityKind.Ternary:
                throw new NotImplementedException();

            default:
                throw new NotSupportedException();
            }
        }
        public void UnaryMethod()
        {
            var expression =
                LinqExpression.Negate(
                    LinqExpression.Default(
                        typeof(bool)),
                    typeof(SampleClass).GetMethod(nameof(SampleClass.StaticFunctionWithArgument)));

            ShouldRoundrip(expression);
        }
        private UnaryExpression UnaryExpression(
            ExpressionType nodeType, System.Type type, JObject obj)
        {
            var operand = this.Prop(obj, "operand", this.Expression);
            var method  = this.Prop(obj, "method", this.Method);

            switch (nodeType)
            {
            case ExpressionType.ArrayLength: return(Expr.ArrayLength(operand));

            case ExpressionType.Convert: return(Expr.Convert(operand, type, method));

            case ExpressionType.ConvertChecked: return(Expr.ConvertChecked(operand, type, method));

            case ExpressionType.Decrement: return(Expr.Decrement(operand, method));

            case ExpressionType.Increment: return(Expr.Increment(operand, method));

            case ExpressionType.IsFalse: return(Expr.IsFalse(operand, method));

            case ExpressionType.IsTrue: return(Expr.IsTrue(operand, method));

            case ExpressionType.Negate: return(Expr.Negate(operand, method));

            case ExpressionType.NegateChecked: return(Expr.NegateChecked(operand, method));

            case ExpressionType.Not: return(Expr.Not(operand, method));

            case ExpressionType.OnesComplement: return(Expr.OnesComplement(operand, method));

            case ExpressionType.PostDecrementAssign: return(Expr.PostDecrementAssign(operand, method));

            case ExpressionType.PostIncrementAssign: return(Expr.PostIncrementAssign(operand, method));

            case ExpressionType.PreDecrementAssign: return(Expr.PreDecrementAssign(operand, method));

            case ExpressionType.PreIncrementAssign: return(Expr.PreIncrementAssign(operand, method));

            case ExpressionType.Quote: return(Expr.Quote(operand));

            case ExpressionType.Throw: return(Expr.Throw(operand, type));

            case ExpressionType.TypeAs: return(Expr.TypeAs(operand, type));

            case ExpressionType.UnaryPlus: return(Expr.UnaryPlus(operand, method));

            case ExpressionType.Unbox: return(Expr.Unbox(operand, type));

            default: throw new NotSupportedException();
            }
        }
示例#5
0
        static E Compile(
            IndexExpression expression,
            Dictionary <string, ParameterExpression> spans,
            Dictionary <string, ParameterExpression> indices)
        {
            switch (expression.ArityKind)
            {
            case IndexExpressionArityKind.Constant:
                return(E.Constant(expression.Constant));

            case IndexExpressionArityKind.Index:
                return(indices[expression.Index.Name]);

            case IndexExpressionArityKind.Element:
                if (expression.Element.Symbol.Shape.Kind != ElementKind.Int32)
                {
                    throw new InvalidOperationException("Integer tensor requires for table lookups.");
                }
                return(Compile(expression.Element, spans, indices));

            case IndexExpressionArityKind.Binary:
                switch (expression.BinaryKind)
                {
                case BinaryExpressionKind.Add:
                    return(E.Add(
                               Compile(expression.Expr1, spans, indices),
                               Compile(expression.Expr2, spans, indices)));

                case BinaryExpressionKind.Subtract:
                    return(E.Add(
                               Compile(expression.Expr1, spans, indices),
                               E.Negate(Compile(expression.Expr2, spans, indices))));

                case BinaryExpressionKind.Multiply:
                    return(E.Multiply(
                               Compile(expression.Expr1, spans, indices),
                               Compile(expression.Expr2, spans, indices)));

                case BinaryExpressionKind.Divide:
                    return(E.Divide(
                               Compile(expression.Expr1, spans, indices),
                               Compile(expression.Expr2, spans, indices)));

                default:
                    throw new NotSupportedException();
                }

            default:
                throw new NotSupportedException();
            }
        }
示例#6
0
        // Solve a system of linear equations
        private static void Solve(CodeGen code, LinqExpr Ab, IEnumerable <LinearCombination> Equations, IEnumerable <Expression> Unknowns)
        {
            LinearCombination[] eqs    = Equations.ToArray();
            Expression[]        deltas = Unknowns.ToArray();

            int M = eqs.Length;
            int N = deltas.Length;

            // Initialize the matrix.
            for (int i = 0; i < M; ++i)
            {
                LinqExpr Abi = code.ReDeclInit <double[]>("Abi", LinqExpr.ArrayAccess(Ab, LinqExpr.Constant(i)));
                for (int x = 0; x < N; ++x)
                {
                    code.Add(LinqExpr.Assign(
                                 LinqExpr.ArrayAccess(Abi, LinqExpr.Constant(x)),
                                 code.Compile(eqs[i][deltas[x]])));
                }
                code.Add(LinqExpr.Assign(
                             LinqExpr.ArrayAccess(Abi, LinqExpr.Constant(N)),
                             code.Compile(eqs[i][1])));
            }

            // Gaussian elimination on this turd.
            //RowReduce(code, Ab, M, N);
            code.Add(LinqExpr.Call(
                         GetMethod <Simulation>("RowReduce", Ab.Type, typeof(int), typeof(int)),
                         Ab,
                         LinqExpr.Constant(M),
                         LinqExpr.Constant(N)));

            // Ab is now upper triangular, solve it.
            for (int j = N - 1; j >= 0; --j)
            {
                LinqExpr _j  = LinqExpr.Constant(j);
                LinqExpr Abj = code.ReDeclInit <double[]>("Abj", LinqExpr.ArrayAccess(Ab, _j));

                LinqExpr r = LinqExpr.ArrayAccess(Abj, LinqExpr.Constant(N));
                for (int ji = j + 1; ji < N; ++ji)
                {
                    r = LinqExpr.Add(r, LinqExpr.Multiply(LinqExpr.ArrayAccess(Abj, LinqExpr.Constant(ji)), code[deltas[ji]]));
                }
                code.DeclInit(deltas[j], LinqExpr.Divide(LinqExpr.Negate(r), LinqExpr.ArrayAccess(Abj, _j)));
            }
        }
示例#7
0
        private Exp MakeUnary(CompilerState state, Node node)
        {
            if (state == null)
            {
                throw new Exception();
            }
            if (node == null)
            {
                throw new Exception();
            }

            var data = node.Token.Data as Tokenizing.OperatorData;

            if (data == null)
            {
                throw new Exception();
            }

            if (node.Children.Count != 1)
            {
                throw new Exception();
            }

            if (data.Operator == Operator.Minus)
            {
                var value = Make(state, node.Children[0]);
                return(Exp.Negate(value));
            }

            if (data.Operator == Operator.LogicalNot)
            {
                var value = Make(state, node.Children[0]);
                return(Exp.Not(ToBoolean(value)));
            }

            throw new Exception();
        }
示例#8
0
 public static Ex Neg(this Ex me) => Ex.Negate(me);
示例#9
0
        protected Expression VisitMathMethodCall([NotNull] MethodCallExpression m)
        {
            switch (m.Method.Name)
            {
            case "Pow": return(Visit(sPower(Expression.Power(m.Arguments[0], m.Arguments[1]))));

            case "Sin":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), MathMethod("Cos", x)));
            }

            case "Cos":
            {
                var x = m.Arguments[0];
                return(Expression.Negate(sMultiply(Visit(x), MathMethod("Sin", x))));
            }

            case "Tan":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), sDivide(1, sPower(MathMethod("Cos", x), 2))));
            }

            case "Asin":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), sDivide(1, MathMethod("Sqrt", sSubtract(1, sPower(x, 2))))));
            }

            case "Acos":
            {
                var x = m.Arguments[0];
                return(Expression.Negate(sMultiply(Visit(x), sDivide(1, MathMethod("Sqrt", sSubtract(1, sPower(x, 2)))))));
            }

            case "Atan":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), sDivide(1, sAdd(1, sPower(x, 2)))));
            }

            case "Sinh":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), MathMethod("Cosh", x)));
            }

            case "Cosh":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), MathMethod("Sinh", x)));
            }

            case "Tanh":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), sDivide(1, sPower(sDivide(1, MathMethod("Tanh", x)), 2))));
            }

            case "Abs":
            {
                var x         = m.Arguments[0];
                var condition = Expression.Condition
                                (
                    Expression.Equal(x, Expression.Constant(0.0)),
                    Expression.Constant(0.0),
                    Expression.Convert(MathMethod("Sign", x), typeof(double))
                                );
                return(sMultiply(Visit(x), condition));
            }

            case "Sign":
            {
                var x         = m.Arguments[0];
                var condition = Expression.Condition
                                (
                    Expression.Equal(x, Expression.Constant(0.0)),
                    Expression.Constant(double.PositiveInfinity),
                    Expression.Constant(0.0)
                                );
                return(sMultiply(Visit(x), condition));
            }

            case "Sqrt":
                return(Visit(sPower(m.Arguments[0],
                                    Expression.Divide(Expression.Constant(1.0), Expression.Constant(2.0)))));

            case "Exp":
            {
                var x = m.Arguments[0];
                return(sMultiply(Visit(x), MathMethod("Exp", x)));
            }

            case "Log":
            {
                var x = m.Arguments[0];
                if (m.Arguments.Count > 1)
                {
                    var a    = m.Arguments[1];
                    var expr = sDivide(MathMethod("Log", x), MathMethod("Log", a));
                    return(Visit(expr));
                }
                var dx = Visit(x);
                return(sDivide(dx, x));
            }

            case "Log10":
            {
                var x    = m.Arguments[0];
                var expr = MathMethod("Log", x, Expression.Constant(10.0));
                return(Visit(expr));
            }

            default:
                throw new NotSupportedException();
            }
        }
示例#10
0
 public static UnaryExpression Negate(Expression expression) => Expression.Negate(expression);
示例#11
0
 public static UnaryExpression Negate(Expression expression, MethodInfo method) => Expression.Negate(expression, method);