Exemplo n.º 1
0
 public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
 {
     string L = this.LeftExpression.ToString(Format, VariableFormat, ConstantFormat);
     string R = this.RightExpression.ToString(Format, VariableFormat, ConstantFormat);
     if (Format == ExpressionStringFormat.ParseFriendly) return "(" + L + " - " + R + ")";
     return L + " - " + R;
 }
Exemplo n.º 2
0
        public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
        {
            string N = this.Numerator.ToString(Format, VariableFormat, ConstantFormat);
            string D = this.Denominator.ToString(Format, VariableFormat, ConstantFormat);

            if (Format == ExpressionStringFormat.Default)
            {
                return N + " / " + D;
            }
            else if (Format == ExpressionStringFormat.LaTeX)
            {
                return @"\frac{" + N + "}{" + D + "}";
            }
            else if (Format == ExpressionStringFormat.ParseFriendly)
            {
                return "(" + N + " / " + D + ")";
            }
            else
            {
                throw new Exceptions.ExpressionParsingException("Unable to convert product to string - Invalid ExpressionStringConstantFormat");
            }
        }
Exemplo n.º 3
0
        public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
        {
            //if (this.Val == (1.0 / 2.0)) return "½";
            //if (this.Val == (1.0 / 3.0)) return "⅓";
            //if (this.Val == (1.0 / 4.0)) return "¼";
            //if (this.Val == (1.0 / 8.0)) return "⅛";
            //if (this.Val == (2.0 / 3.0)) return "⅔";
            //if (this.Val == (3.0 / 4.0)) return "¾";
            //if (this.Val == (3.0 / 8.0)) return "⅜";
            //if (this.Val == (5.0 / 8.0)) return "⅝";
            //if (this.Val == (7.0 / 8.0)) return "⅞";

            if (Format == ExpressionStringFormat.Default)
            {
                string buildstring = "";
                if (this.Negated) buildstring += "-";
                if (this.InnerValue == (1.0 / 2)) return buildstring + "½";
                if (this.InnerValue == (1.0 / 3)) return buildstring + "⅓";
                if (this.InnerValue == (2.0 / 3)) return buildstring + "⅔";
                if (this.InnerValue == (1.0 / 4)) return buildstring + "¼";
                if (this.InnerValue == (3.0 / 4)) return buildstring + "¾";
                return buildstring + "(" + this.Numerator + "/" + this.Denominator + ")";
            }
            else
            {
                string buildstring = "";
                if (this.Negated) buildstring += "-";
                return buildstring + @"\frac{" + this.Numerator + "}{" + this.Denominator + "}";
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Obtains the string representation of this function.
 /// </summary>
 public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
 {
     string N = this.Symbol; //FINISH ME - We need to get names right!
     string A = String.Join(", ", this.Variables.ConvertAll(x => x.ToString(Format, VariableFormat, ConstantFormat)));
     if (Format == ExpressionStringFormat.Default)
     {
         return N + "(" + A + ")";
     }
     else if (Format == ExpressionStringFormat.LaTeX)
     {
         return N + "(" + A + ")";
     }
     else if (Format == ExpressionStringFormat.ParseFriendly)
     {
         return N + "[" + A + "]";
     }
     else
     {
         throw new Exceptions.ExpressionParsingException("Unable to convert function to string - Invalid ExpressionStringFormat");
     }
 }
Exemplo n.º 5
0
        public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
        {

            string LS = this.LeftExpression.ToString(Format, VariableFormat, ConstantFormat);
            string RS = this.RightExpression.ToString(Format, VariableFormat, ConstantFormat);

            if (Format == ExpressionStringFormat.Default)
            {
                //Special Formats
                if ((this.LeftExpression is Value) && (this.LeftExpression as Value).InnerValue == -1) return "-" + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).InnerValue == -1) return "-" + LS;
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && (this.RightExpression is Constant)) return LS + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && (this.LeftExpression is Constant)) return RS + LS;
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && (this.RightExpression is Exponentiation)) return LS + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && (this.LeftExpression is Exponentiation)) return RS + LS;
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && (this.RightExpression is Sum)) return LS + "(" + RS + ")";
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && (this.LeftExpression is Sum)) return RS + "(" + LS + ")";
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && (this.RightExpression is Difference)) return LS + "(" + RS + ")";
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && (this.LeftExpression is Difference)) return RS + "(" + LS + ")";
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && !(this.RightExpression is Value)) return LS + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && !(this.LeftExpression is Value)) return RS + LS;
                //else if (!(this.LeftExpression is Value) && !(this.RightExpression is Value)) return LS + RS;

                //Otherwise, just return the default:
                return LS + " * " + RS;
            }
            else if (Format == ExpressionStringFormat.LaTeX)
            {
                //Special Formats
                if ((this.LeftExpression is Value) && (this.LeftExpression as Value).InnerValue == -1) return "-" + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).InnerValue == -1) return "-" + LS;
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && (this.RightExpression is Variable)) return LS + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && (this.LeftExpression is Variable)) return RS + LS;
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && (this.RightExpression is Constant)) return LS + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && (this.LeftExpression is Constant)) return RS + LS;
                else if ((this.LeftExpression is Value) && (this.LeftExpression as Value).IsInteger() && (this.RightExpression is Exponentiation)) return LS + RS;
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).IsInteger() && (this.LeftExpression is Exponentiation)) return RS + LS;

                //Otherwise, just return the default:
                return LS + @" \times " + RS;
            }
            else if (Format == ExpressionStringFormat.ParseFriendly)
            {
                if ((this.LeftExpression is Value) && (this.LeftExpression as Value).InnerValue == -1) return "-(" + RS + ")";
                else if ((this.RightExpression is Value) && (this.RightExpression as Value).InnerValue == -1) return "-(" + LS + ")";
                return "(" + LS + " * " + RS + ")";
            }
            else
            {
                throw new Exceptions.ExpressionParsingException("Unable to convert product to string - Invalid ExpressionStringConstantFormat");
            }
        }
Exemplo n.º 6
0
        public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
        {
            string B = this.Base.ToString(Format, VariableFormat, ConstantFormat);
            string E = this.Exponent.ToString(Format, VariableFormat, ConstantFormat);

            if (Format == ExpressionStringFormat.Default)
            {
                return B + "^" + E;
            }
            else if (Format == ExpressionStringFormat.LaTeX)
            {
                return B + "^{" + E + "}";
            }
            else if (Format == ExpressionStringFormat.ParseFriendly)
            {
                return "(" + B + " ^ " + E + ")";
            }
            else
            {
                throw new Exceptions.ExpressionParsingException("Unable to convert exponentiation to string - Invalid ExpressionStringFormat");
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets an operand (variable/constant) defined in this scope by its matching string representation.
        /// Returns first match and null if no operand was found.
        /// </summary>
        /// <param name="MatchString">The string an operand's string must match to be selected</param>
        /// <param name="VF">The string format variables are converted to for comparison</param>
        /// <param name="CF">The string format constants are converted to for comparison</param>
        public Irreducible Get(string MatchString, ExpressionStringVariableFormat VF, ExpressionStringConstantFormat CF)
        {
            //Firstly, lets check for the default variables and constants
            if (MatchString == "x") return Variable.x;
            if (MatchString == "y") return Variable.y;
            if (MatchString == "z") return Variable.z;
            if (MatchString == "C") return Constant.C;
            if (MatchString == "K") return Constant.K;
            if (MatchString == "pi") return Constant.pi;
            if (MatchString == "e") return Constant.e;
            if (MatchString == "Infinity" || MatchString == "PositiveInfinity") return Constant.PositiveInfinity;
            if (MatchString == "NegativeInfinity") return Constant.NegativeInfinity;
            if (MatchString == "i") return ImaginaryUnit.i;

            //Lets combine our variables and constants into one list
            List<Irreducible> CheckList = new List<Irreducible>();
            if (this.Variables != null && this.Variables.Count > 0) CheckList.AddRange(this.Variables.Values);
            if (this.Constants != null && this.Constants.Count > 0) CheckList.AddRange(this.Constants.Values);
            CheckList.TrimExcess();

            //Now lets search through everything for matches via default representation
            if (CheckList.Count < 1) return null;
            for (int i = 0; i < CheckList.Count; i++)
            {
                if (CheckList[i].ToString(ExpressionStringFormat.ParseFriendly, VF, CF) == MatchString) return CheckList[i];
            }

            //If we couldn't find anything
            return null;
        }
Exemplo n.º 8
0
 /// <summary>
 /// Obtains the string representation of this mathematical expression with specific formatting options.
 /// </summary>
 /// <param name="Format">The general format to use for the output</param>
 /// <param name="VariableFormat">The format to use for variables in the output</param>
 /// <param name="ConstantFormat">The format to use for constants and values in the output</param>
 public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
 {
     if (ConstantFormat == ExpressionStringConstantFormat.Default)
     {
         if (this.Symbol != string.Empty)
         {
             if (this.Subscript != string.Empty)
             {
                 if (Format == ExpressionStringFormat.Default || Format == ExpressionStringFormat.ParseFriendly)
                 {
                     return this.Symbol + "_[" + this.Subscript + "]";
                 }
                 else if (Format == ExpressionStringFormat.LaTeX)
                 {
                     return this.Symbol + "_{" + this.Subscript + "}";
                 }
                 else
                 {
                     throw new Exceptions.ExpressionParsingException("Unable to convert constant to string - Invalid ExpressionStringFormat");
                 }
             }
             else
             {
                 return this.Symbol;
             }
         }
         else
         {
             if (this.HasDescreteValue)
             {
                 return this.Value.ToString(Format, VariableFormat, ConstantFormat);
             }
             else
             {
                 return "\"[UNKNOWN CONSTANT]\"";
             }
         }
     }
     else if (ConstantFormat == ExpressionStringConstantFormat.Numeric)
     {
         if (this.HasDescreteValue)
         {
             return this.Value.ToString(Format, VariableFormat, ConstantFormat);
         }
         else
         {
             if (this.Symbol != string.Empty)
             {
                 if (this.Subscript != string.Empty)
                 {
                     if (Format == ExpressionStringFormat.Default || Format == ExpressionStringFormat.ParseFriendly)
                     {
                         return this.Symbol + "_[" + this.Subscript + "]";
                     }
                     else if (Format == ExpressionStringFormat.LaTeX)
                     {
                         return this.Symbol + "_{" + this.Subscript + "}";
                     }
                     else
                     {
                         throw new Exceptions.ExpressionParsingException("Unable to convert constant to string - Invalid ExpressionStringFormat");
                     }
                 }
                 else
                 {
                     return this.Symbol;
                 }
             }
             else
             {
                 return "\"[UNKNOWN CONSTANT]\"";
             }
         }
     }
     else if (ConstantFormat == ExpressionStringConstantFormat.Symbolic)
     {
         if (this.Symbol != string.Empty)
         {
             if (this.Subscript != string.Empty)
             {
                 if (Format == ExpressionStringFormat.Default || Format == ExpressionStringFormat.ParseFriendly)
                 {
                     return this.Symbol + "_[" + this.Subscript + "]";
                 }
                 else if (Format == ExpressionStringFormat.LaTeX)
                 {
                     return this.Symbol + "_{" + this.Subscript + "}";
                 }
                 else
                 {
                     throw new Exceptions.ExpressionParsingException("Unable to convert constant to string - Invalid ExpressionStringFormat");
                 }
             }
             else
             {
                 return this.Symbol;
             }
         }
         else
         {
             return "C";
         }
     }
     else
     {
         throw new Exceptions.ExpressionParsingException("Unable to convert constant to string - Invalid ExpressionStringConstantFormat");
     }
 }
 public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
 {
     string BuildString = "(" + this.ChildExpressions[0].ToString(Format, VariableFormat, ConstantFormat);
     for (int i = 1; i < this.ChildExpressions.Count; i++)
     {
         BuildString += " * " + this.ChildExpressions[i].ToString(Format, VariableFormat, ConstantFormat);
     }
     return BuildString + ")";
 }
Exemplo n.º 10
0
 public override string ToString(ExpressionStringFormat Format, ExpressionStringVariableFormat VariableFormat, ExpressionStringConstantFormat ConstantFormat)
 {
     return "i";
 }