/// <summary> /// Creates a matching for type cast expressions. /// </summary> /// <param name="srcType">source type</param> /// <param name="dstType">destination type</param> public Matching Cast(Type srcType, TypeDescriptor dstType) { var cast = IntrinsicFunctions.Cast((Expression)null, srcType, dstType); Matching newm = new Matching(); newm._func = e => e.NodeEquals(cast) && this.Match(e.Children.ElementAt(0)); newm._gen = () => newm._expr == null? IntrinsicFunctions.Cast(_gen(), srcType, dstType) : newm._expr; return(newm); }
public Expression TransformFunction(FunctionCall expr) { if (expr.Callee is IntrinsicFunction) { IntrinsicFunction ifun = (IntrinsicFunction)expr.Callee; switch (ifun.Action) { case IntrinsicFunction.EAction.Sin: { Expression x = expr.Arguments[0]; FunctionCall f = IntrinsicFunctions.Cos(x); return(f * der(x)); } case IntrinsicFunction.EAction.Cos: { Expression x = expr.Arguments[0]; FunctionCall f = IntrinsicFunctions.Sin(x); return(-f *der(x)); } case IntrinsicFunction.EAction.Sqrt: { return(der(expr.Arguments[0]) / expr); } case IntrinsicFunction.EAction.Sign: return(SpecialConstant.ScalarZero); case IntrinsicFunction.EAction.GetArrayElement: throw new ArgumentException("Expression is not differentiatable"); default: throw new NotImplementedException(); } } else { throw new NotImplementedException("Only intrinsic functions can be derived"); } }
private static Expression ToExpression(object arg) { if (arg is Expression) { var ex = (Expression)arg; if (ex.ResultType.CILType.Equals(typeof(string))) { return(ex); } else { return(IntrinsicFunctions.Cast(ex, ex.ResultType.CILType, typeof(string))); } } else if (arg is ILiteral) { return(ToExpression(new LiteralReference((ILiteral)arg))); } else { return(LiteralReference.CreateConstant(arg.ToString())); } }
/// <summary> /// Emits a call to output a textual diagnostic message. /// </summary> /// <param name="builder">algorithm builder</param> /// <param name="args">output arguments</param> public static void ReportLine(this IAlgorithmBuilder builder, params object[] args) { var arg = Expression.Concat(args.Select(_ => ToExpression(_)).ToArray()); builder.Call(IntrinsicFunctions.ReportLine(arg), arg); }