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));
        }
Beispiel #3
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is LessThanEqualTo a &&
            a.Equals(this));
 }
Beispiel #4
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is ConstantNumber num &&
            num.Equals(this));
 }
Beispiel #5
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Not not &&
            not.Equals(this));
 }
Beispiel #6
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Factorial fac &&
            fac.Equals(this));
 }
Beispiel #7
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Variable var &&
            var.Equals(this));
 }
Beispiel #8
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Increment a &&
            a.Equals(this));
 }
Beispiel #9
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is ConstantString str &&
            str.Equals(this));
 }
Beispiel #10
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Sqrt sqrt &&
            sqrt.Equals(this));
 }
Beispiel #11
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Cosine cos &&
            cos.Equals(this));
 }
Beispiel #12
0
 public override bool Equals(BaseExpression? other)
 {
     return other is ErrorExpression a
         && a.Equals(this);
 }
Beispiel #13
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Abs abs &&
            abs.Equals(this));
 }
Beispiel #14
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is GreaterThan a &&
            a.Equals(this));
 }
Beispiel #15
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is ArcSine asin &&
            asin.Equals(this));
 }
Beispiel #16
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is ArcCos acos &&
            acos.Equals(this));
 }
Beispiel #17
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is ArcTan atan &&
            atan.Equals(this));
 }
Beispiel #18
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Divide a &&
            a.Equals(this));
 }
Beispiel #19
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Negate neg &&
            neg.Equals(this));
 }
Beispiel #20
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Bracketed brk &&
            brk.Equals(this));
 }
Beispiel #21
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is PostIncrement post &&
            post.Equals(this));
 }
Beispiel #22
0
 public abstract bool Equals(BaseExpression?other);
Beispiel #23
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Sine sine &&
            sine.Equals(this));
 }
Beispiel #24
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is PreDecrement pre &&
            pre.Equals(this));
 }
Beispiel #25
0
 public override bool Equals(BaseExpression?other)
 {
     return(other is Tangent tan &&
            tan.Equals(this));
 }