Example #1
0
 public MathFormulaDrawable(MathFormula formula, IMathSymbolConverter converter)
     : base(formula.Level, formula.Sizes)
 {
     sizes = formula.Sizes;
     for (int i = 0; i < formula.Count; i++)
     {
         MathSymbol s   = formula[i];
         MathSymbol sym = converter.Convert(s);
         sym.Sizes = sizes;
         sym.Append(this);
         sym = Last;
         for (int j = 0; j < s.Count; j++)
         {
             if (j >= sym.Count)
             {
                 break;
             }
             if (s[j] != null)
             {
                 s[j].Sizes = sizes;
                 sym[j]     = new MathFormulaDrawable(s[j], converter);
             }
         }
     }
 }
Example #2
0
 /// <summary>
 /// Wraps elementary functions by brackets for further processing
 /// </summary>
 void WrapFunctions()
 {
     s = First;
     while (s != null)
     {
         if (s.HasChildren)
         {
             for (int i = 0; i < s.Count; i++)
             {
                 s[i].WrapFunctions();
             }
         }
         if (s.SymbolType == (byte)FormulaConstants.Unary | s.SymbolType == (byte)FormulaConstants.Series)
         {
             s1 = s.Next;
             if (s1 == null)
             {
                 return;
             }
             if (!s1.ShouldWrapped | s1 is BracketsSymbol)
             {
                 s = s1;
                 continue;
             }
             s.Append(MathSymbol.Bra);
             s1 = s.Next;
             while (s1 != null)
             {
                 bool       b  = false;
                 MathSymbol ss = s1.Next;
                 if (ss == null)
                 {
                     b = true;
                 }
                 else if (!ss.ShouldWrapped | s1 is BracketsSymbol)
                 {
                     b = true;
                 }
                 if (b)
                 {
                     s1.Append(MathSymbol.Ket);
                     s = s1.Next;
                     break;
                 }
                 MathSymbol s2 = s1;
                 s1 = s1.Next;
                 if (s1 == null)
                 {
                     s2.Append(MathSymbol.Ket);
                     break;
                 }
             }
         }
         s = s.Next;
     }
 }
Example #3
0
        /// <summary>
        /// Inserts object
        /// </summary>
        /// <param name="symbol">Symbol to insert</param>
        /// <returns>Inserted object</returns>
        public MathFormula InsertObject(MathSymbol symbol)
        {
            symbol.Append(this);
            MathSymbol s = this[Count - 1];

            if ((s is BracketsSymbol) | (s is AbsSymbol))
            {
                return(s[0]);
            }
            return(this);
        }
Example #4
0
 /// <summary>
 /// Constructor from prototype
 /// </summary>
 /// <param name="formula">Prototype</param>
 /// <param name="converter">Symbol converter</param>
 public MathFormula(MathFormula formula, IMathSymbolConverter converter)
     : this(formula.Level, formula.Sizes)
 {
     for (int i = 0; i < formula.Count; i++)
     {
         MathSymbol s   = formula[i];
         MathSymbol sym = converter.Convert(s);
         sym.Append(this);
         sym = Last;
         for (int j = 0; j < s.Count; j++)
         {
             if (s[j] != null)
             {
                 sym[j] = new MathFormula(s[j], converter);
             }
         }
     }
 }
        /// <summary>
        /// Creates symbol from fragment of string
        /// </summary>
        /// <param name="f">Formula for setting symbol</param>
        /// <param name="str">The string</param>
        /// <param name="b">Fragment begin</param>
        /// <param name="e">Fragment end</param>
        /// <returns>The symbol</returns>
        public MathSymbol CreateSymbol(MathFormula f, string str, int b, int e)
        {
            char       c  = str[b];
            char       by = str[b + 1];
            char       cb = (char)by;
            MathSymbol s  = ChooseSymbol(str, b, e);

            if (s == null)
            {
                return(null);
            }
            s.Append(f);
            MathSymbol symb = f.Last;
            int        j    = b + Shift;

            if (str[b] == '\u2220')
            {
                for (; j < str.Length; j++)
                {
                    if (str[j] == Separator)
                    {
                        break;
                    }
                }
            }
            if (symb.Count > 0)
            {
                for (int i = 0; i < symb.Count; i++)
                {
                    int m = 0;
                    for (int k = j; k < str.Length; k++)
                    {
                        char bt = str[k];
                        if (k > str.Length - 2)
                        {
                            break;
                        }
                        if (k < str.Length - 1)
                        {
                            if (bt == Separator & str[k + 1] == BeginSeparator)
                            {
                                m++;
                            }
                            else if (bt == Separator & str[k + 1] == EndSeparator)
                            {
                                m--;
                            }
                        }
                        if (m <= 0 & k > j)
                        {
                            if (symb[i] != null)
                            {
                                int bd = (str[j] == Separator) ? 1 : 0;
                                int ed = (str[k] == EndSeparator) ? 1 : 0;
                                if (i > 0)
                                {
                                    //	bd++;
                                    ed--;
                                }
                                MathFormula formula = new MathFormula(symb[i].Level,
                                                                      f.Sizes, str, j + bd + 1, k - ed - 1, this);
                                symb[i] = formula;
                                j       = k + 1;
                                if (str[j] == EndSeparator)
                                {
                                    ++j;
                                }
                                break;
                            }
                        }
                    }
                }
            }
            return(symb);
        }
Example #6
0
        /// <summary>
        /// Reverses digital to symbolic represrntation
        /// </summary>
        private void ReverseNumber()
        {
            MathSymbol s = First;

            while (s != null)
            {
                if (s.SymbolType == (int)FormulaConstants.Number)
                {
                    if (s.Symbol == '?')
                    {
                        List <MathFormula> f = s.Children;
                        double             a = s.DoubleValue;
                        if (a == Math.E | a == Math.PI)
                        {
                            s.Append(new SimpleSymbol((a == Math.E) ? 'e' : 'p'));
                            MathSymbol s1 = s.Next;
                            s.Remove();
                            s1.Children = f;
                            s           = s1.Next;
                            continue;
                        }
                        int  b    = 0;
                        bool intv = false;
                        if (Math.Abs(a - Math.Ceiling(a)) < ROUND_EPS)
                        {
                            b    = (int)Math.Ceiling(a);
                            intv = true;
                        }
                        else if (Math.Abs(a - Math.Floor(a)) < ROUND_EPS)
                        {
                            b    = (int)Math.Floor(a);
                            intv = true;
                        }
                        string str = "";
                        if (intv)
                        {
                            str = b + "";
                        }
                        else
                        {
                            str = a + "";
                        }
                        MathSymbol sOne = s;
                        for (int i = 0; i < str.Length; i++)
                        {
                            MathSymbol symb = (str[i] == MathSymbol.DecimalSep[0]) ?
                                              new BinarySymbol('.') :
                                              new SimpleSymbol(str[i], false, (int)FormulaConstants.Number);
                            sOne.Append(symb);
                            sOne = sOne.Next;
                        }
                        s.Remove();
                        sOne.Children = f;
                        s             = sOne.Next;
                    }
                }
                else
                {
                    s = s.Next;
                }
            }
            s = First;
            while (s != null)
            {
                if (s.HasChildren)
                {
                    List <MathFormula> f = s.Children;
                    if (f != null)
                    {
                        for (int i = 0; i < f.Count; i++)
                        {
                            MathFormula form = f[i] as MathFormula;
                            if (form.Count > 0)
                            {
                                form.ReverseNumber();
                            }
                        }
                    }
                }
                s = s.Next;
            }
        }