Expr Power(Expr val, BigInteger pow) { if(pow == BigInteger.One) return val; return val.MatchDefault( @default: x => builder.Power(x, pow), @const: x => Const(BigRational.Pow(x, pow)), mult: args => Mult(args.Select(x => Power(x, pow)).ToExprList()), power: (v, p) => builder.Power(v, pow * p) ); }
public static UnaryExpressionInfo ExtractMultiplyUnaryInfo(Expr expr) { Func<Expr, UnaryExpressionInfo> getDefault = x => GetDefault(BinaryOperation.Multiply, x); Func<BigRational, UnaryExpressionInfo> getConstant = x => Constant(BinaryOperation.Multiply, x); return expr.MatchDefault( getDefault, @const: getConstant ); }
public static UnaryExpressionInfo ExtractAddUnaryInfo(Expr expr) { Func<Expr, UnaryExpressionInfo> getDefault = x => GetDefault(BinaryOperation.Add, x); Func<BigRational, UnaryExpressionInfo> getConstant = x => Constant(BinaryOperation.Add, x); return expr.MatchDefault( getDefault, mult: args => { var headConstant = args.First().AsConst(); if(headConstant.Return(x => x < 0, () => false)) { var exprConstant = ExprExtensions.Const(-headConstant.Value); var tail = GetTail(args); Expr expr2 = (headConstant.Value == BigInteger.MinusOne) ? tail : Builder.Simple.Multiply(exprConstant.Yield().Concat(tail.ToMult()).ToExprList()); return new UnaryExpressionInfo(expr2, BinaryOperationEx.Subtract); } return getDefault(Builder.Simple.Multiply(args.ToExprList())); }, @const: getConstant ); }