private static BaseExpression?SmallestExponents(Number value) { var bestLength = int.MaxValue; BaseExpression?best = null; void Submit(BaseExpression exp) { if (exp.StaticEvaluate().Number != value) { return; } var length = exp.ToString().Length; if (length < bestLength) { bestLength = length; best = exp; } } for (var idx = 1; idx < 320; idx++) { var b = idx / 10m; try { var log = Math.Log((double)value, (double)b); if (double.IsNaN(log)) { continue; } var exp = new Exponent(new ConstantNumber((Number)b), new ConstantNumber((Number)log)); Submit(exp); var integral = new Exponent(new ConstantNumber((Number)b), new ConstantNumber((Number)(int)Math.Round(log))); var integralV = integral.StaticEvaluate().Number; var integralE = value - integralV; Submit(new Add(integral, new ConstantNumber(integralE))); } catch (OverflowException) { // If the calculation overflows then don't submit this case } } return(best); }
protected override BaseExpression Visit(ConstantNumber con) { if (con.Value.ToString().Length <= 3) { return(base.Visit(con)); } var replacements = new[] { SmallestExponents(con.Value), BestFraction(con.Value), }; var shortestLength = int.MaxValue; BaseExpression?shortest = null; foreach (var replacement in replacements) { if (replacement == null) { continue; } var l = replacement.ToString().Length; if (l < shortestLength) { shortestLength = l; shortest = replacement; } } if (shortest != null && shortestLength < con.ToString().Length) { return(base.Visit(shortest)); } return(base.Visit(con)); }
public override bool Equals(BaseExpression?other) { return(other is LessThanEqualTo a && a.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is ConstantNumber num && num.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Not not && not.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Factorial fac && fac.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Variable var && var.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Increment a && a.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is ConstantString str && str.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Sqrt sqrt && sqrt.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Cosine cos && cos.Equals(this)); }
public override bool Equals(BaseExpression? other) { return other is ErrorExpression a && a.Equals(this); }
public override bool Equals(BaseExpression?other) { return(other is Abs abs && abs.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is GreaterThan a && a.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is ArcSine asin && asin.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is ArcCos acos && acos.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is ArcTan atan && atan.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Divide a && a.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Negate neg && neg.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Bracketed brk && brk.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is PostIncrement post && post.Equals(this)); }
public abstract bool Equals(BaseExpression?other);
public override bool Equals(BaseExpression?other) { return(other is Sine sine && sine.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is PreDecrement pre && pre.Equals(this)); }
public override bool Equals(BaseExpression?other) { return(other is Tangent tan && tan.Equals(this)); }