예제 #1
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula      f  = FormulaCreator.CreateFormula(tree[0], level, sizes);
            MathFormula      p  = FormulaCreator.CreateFormula(tree[1], (byte)((int)level + 1), sizes);
            IObjectOperation op = tree[0].Operation;

            if (op.IsPowered())
            {
                if (op is ElementaryFunctionOperation)
                {
                    f.First[0] = p;
                }
                else
                {
                    f.Last[0] = p;
                }
                return(f);
            }
            MathFormula form = new MathFormula(level, sizes);
            MathSymbol  s    = new BracketsSymbol();

            s.Append(form);
            form.First[0] = p;
            return(form);
        }
예제 #2
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="operation">Operation of formula creator</param>
        /// <param name="tree">Tree</param>
        /// <param name="level">Level</param>
        /// <param name="sizes">Sizes</param>
        /// <param name="str">Str of binrary</param>
        /// <returns>The formula</returns>
        public static MathFormula CreateFormula(IFormulaCreatorOperation operation, ObjectFormulaTree tree, byte level, int[] sizes, string str)
        {
            MathFormula form = new MathFormula(level, sizes);

            for (int i = 0; i < str.Length + 1; i++)
            {
                IFormulaCreatorOperation op = tree[i].Operation as IFormulaCreatorOperation;
                MathFormula f  = op.CreateFormula(tree[i], level, sizes);
                MathFormula fp = null;
                if (op.OperationPriority < operation.OperationPriority)
                {
                    fp = new MathFormula(level, sizes);
                    MathSymbol s = new BracketsSymbol();
                    s.Append(fp);
                    fp.First[0] = f;
                }
                else
                {
                    fp = f;
                }
                form.Add(fp);
                if (i < str.Length)
                {
                    MathSymbol s = new BinarySymbol(str[i]);
                    s.Append(form);
                }
            }
            return(form);
        }
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);

            for (int i = 0; i < 2; i++)
            {
                IFormulaCreatorOperation op = tree[i].Operation as IFormulaCreatorOperation;
                MathFormula f  = op.CreateFormula(tree[i], level, sizes);
                MathFormula fp = null;
                if (op.OperationPriority < OperationPriority)
                {
                    fp = new MathFormula(level, sizes);
                    MathSymbol s = new BracketsSymbol();
                    s.Append(fp);
                    fp.First[0] = f;
                }
                else
                {
                    fp = f;
                }
                form.Add(fp);
                if (i == 0)
                {
                    MathSymbol s = new BinarySymbol(symbol);
                    s.Append(form);
                }
            }
            return(form);
        }
예제 #4
0
        string IFormulaSaver.Save(MathFormula formula)
        {
            XElement doc = XElement.Parse("<Root/>");
            XElement e   = MathFormula.CreateElement(formula, creator);

            doc.Add(e);
            return(doc + "");
        }
예제 #5
0
 /// <summary>
 /// Constructor of subformula
 /// </summary>
 /// <param name="form">the initial formula</param>
 /// <param name="n1">the index of start symbol</param>
 /// <param name="n2">the index of finish symbol</param>
 public MathFormula(MathFormula form, int n1, int n2)
     : this(form.level, form.sizes)
 {
     for (int i = n1; i <= n2; i++)
     {
         Add(form[i]);
     }
 }
예제 #6
0
        /// <summary>
        /// Loads formula
        /// </summary>
        /// <param name="str">String representation</param>
        /// <returns>The formula</returns>
        public virtual MathFormula Load(string str)
        {
            XElement    doc = XElement.Parse(str);
            MathFormula f   = MathFormula.CreateFormula(doc.GetFirst(), creator);

            f.SetLevel(0x0);
            return(f);
        }
 /// <summary>
 /// Initialization of derivation calculation functions
 /// </summary>
 public static void InitDeri()
 {
     functionDerivations = new ObjectFormulaTree[formStrings.Length];
     for (int i = 0; i < formStrings.Length; i++)
     {
         MathFormula f = MathFormula.FromString(MAX_SIZES, formStrings[i]);
         functionDerivations[i] = ObjectFormulaTree.CreateTree(f.FullTransform(null));
     }
 }
        /// <summary>
        /// Converts formula to string
        /// </summary>
        /// <param name="formula">Formula to convert</param>
        /// <returns>String representation of formula</returns>
        public string Convert(MathFormula formula)
        {
            string s = "";

            for (int i = 0; i < formula.Count; i++)
            {
                s += Convert(formula[i]);
            }
            return(s);
        }
예제 #9
0
        /// <summary>
        /// Creates formula from string
        /// </summary>
        /// <param name="sizes">The sizes</param>
        /// <param name="str">The string</param>
        /// <returns>The formula</returns>
        public static MathFormula FromString(int[] sizes, string str)
        {
            MathFormula f = saver.Load(str);

            f.Sizes = sizes;
            return(f);

            /*          try
             *        {
             *            //string[] s = str.Split(byteSep);
             *            byte[] b = new byte[str.Length];
             *            for (int i = 0; i < b.Length; i++)
             *            {
             *                b[i] = (byte)(str[i]);
             *            }
             *            System.IO.MemoryStream stream = new System.IO.MemoryStream(b);
             *            MathFormula form = formatter.Deserialize(stream) as MathFormula;
             *            form.Post();
             *            form.Sizes = sizes;
             *            return form;
             *        }
             *        catch (Exception eee)
             *        {
             *            string sm = eee.StackTrace;
             *            sm = null;
             *        }
             *        try
             *        {
             *            f = new MathFormula(0, sizes, str, 0, str.Length,
             *                ElementaryFormulaStringConverter.Object);
             *            new ObjectFormulaTree(f.FullTransform(null));
             *            return f;
             *        }
             *        catch (Exception)
             *        {
             *           /* ArrayList list = new ArrayList();
             *            for (int i = 0; i < str.Length; i++)
             *            {
             *                char c = str[i];
             *                byte[] b = new byte[] { (byte)c };
             *                list.Add(b);
             *            }
             *            try
             *            {
             *                MathFormula fo = new MathFormula(0, sizes, list, 0, str.Length);
             *                //new ObjectFormulaTree(fo.FullTransform);
             *                return fo;
             *            }
             *            catch (Exception)
             *            {
             *            }
             *        }
             *        return f;
             */
        }
예제 #10
0
 /// <summary>
 /// Copies this formula
 /// </summary>
 /// <returns>the copy</returns>
 public virtual MathFormula Copy()
 {
     form = new MathFormula(level, sizes);
     for (int i = 0; i < Count; i++)
     {
         MathSymbol s = this[i].Copy();
         s.Parent = form;
         form.Add(s);
     }
     return(form);
 }
예제 #11
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);
            MathSymbol  sym  = new FractionSymbol();

            sym.Append(form);
            for (int i = 0; i < 2; i++)
            {
                form.First[i] = FormulaCreator.CreateFormula(tree[i], level, sizes);
            }
            return(form);
        }
예제 #12
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula          form = new MathFormula(level, sizes);
            BinaryFunctionSymbol atan = new BinaryFunctionSymbol('A', "atan2");

            atan.Append(form);
            for (int i = 0; i < 2; i++)
            {
                form.First[i] = FormulaCreator.CreateFormula(tree[i], level, sizes);
            }
            return(form);
        }
예제 #13
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);
            MathFormula f    = new MathFormula(level, sizes);
            MathSymbol  sym  = new SimpleSymbol('¬', (byte)FormulaConstants.Unary, false, "¬");

            sym.Append(form);
            sym = new BracketsSymbol();
            sym.Append(form);
            form.Last[0] = FormulaCreator.CreateFormula(tree[0], level, sizes);
            return(form);
        }
예제 #14
0
        /// <summary>
        /// Creates element from formula
        /// </summary>
        /// <param name="formula">Formula</param>
        /// <param name="document">Document</param>
        /// <param name="creator">Creator</param>
        /// <returns>Element</returns>
        public static XElement CreateElement(MathFormula formula,
                                             IXmlSymbolCreator creator)
        {
            XElement e = formula.CreateXElement("F");

            for (int i = 0; i < formula.Count; i++)
            {
                XElement el = MathSymbol.CreateElement(formula[i], creator);
                e.Add(el);
            }
            return(e);
        }
예제 #15
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);
            MathSymbol  sym  = new SeriesSymbol(index);

            sym.Append(form);
            sym = new BracketsSymbol();
            sym.Append(form);
            form.Last[0] = FormulaCreator.CreateFormula(tree[0], level, sizes);
            form.Last[1] = new MathFormula((byte)(level + 1), sizes);
            return(form);
        }
예제 #16
0
        /// <summary>
        /// Gets variables of formula
        /// </summary>
        /// <param name="formula">The formula</param>
        /// <returns>String of variables</returns>
        static public string GetVariables(MathFormula formula)
        {
            List <char> s = new List <char>();

            GetVariables(formula, s);
            string str = "";

            foreach (char c in s)
            {
                str += c;
            }
            return(str);
        }
예제 #17
0
        /// <summary>
        /// Full transformation
        /// </summary>
        public MathFormula FullTransform(string prohibited)
        {
            MathFormula f = Copy();

            Zero();
            f.TranNumber();
            f.WrapFunctions();
            f.Wrap(prohibited);
            f.InsertMult(prohibited);
            StringDetector sDetector = new StringDetector(0x0, false, false);

            f = sDetector.Convert(f);
            return(f);
        }
예제 #18
0
        /// <summary>
        /// Creates formula from Xml element
        /// </summary>
        /// <param name="e">Element</param>
        /// <param name="creator">Creator of symbols</param>
        /// <returns>Formula</returns>
        public static MathFormula CreateFormula(XElement e, IXmlSymbolCreator creator)
        {
            MathFormula            f = new MathFormula((byte)0);
            IEnumerable <XElement> l = e.GetElementsByTagName("S");

            foreach (XElement el in l)
            {
                if (el.Parent != e)
                {
                    continue;
                }
                MathSymbol s = MathSymbol.CreateSymbol(el, creator);
                s.AppendWithChildren(f);
            }
            return(f);
        }
예제 #19
0
        /// <summary>
        /// Adds the formula to this formula
        /// </summary>
        /// <param name="formula">the formula to add</param>
        public void Add(MathFormula formula)
        {
            int n = formula.Count;

            for (int i = 0; i < n; i++)
            {
                if (formula[i].Children == null)
                {
                    formula[i].Append(this);
                }
                else
                {
                    symbols.Add(formula[i]);
                    formula[i].Parent = this;
                }
            }
        }
예제 #20
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);
            MathSymbol  sym  = null;

            if (symbol is Char)
            {
                sym = new SimpleSymbol((char)symbol);
            }
            else if (symbol is StringPair)
            {
                StringPair sp = symbol as StringPair;
                sym = new SubscriptedSymbol(sp.First, sp.Second);
            }
            sym.Append(form);
            return(form);
        }
예제 #21
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula f    = FormulaCreator.CreateFormula(tree[0], level, sizes);
            MathFormula form = new MathFormula(level, sizes);
            AbsSymbol   root = new AbsSymbol();

            root.Append(form);
            form.First[0] = f;
            if (arity == 1)
            {
                return(form);
            }
            MathFormula p = FormulaCreator.CreateFormula(tree[1], (byte)((int)level + 1), sizes);

            form.First[1] = p;
            return(form);
        }
예제 #22
0
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form            = new MathFormula(level, sizes);
            IFormulaCreatorOperation op = tree[0].Operation as IFormulaCreatorOperation;
            MathFormula f = op.CreateFormula(tree[0], level, sizes);
            MathSymbol  s = new BracketsSymbol();

            s.Append(form);
            form.First[0] = f;
            if (arity > 1)
            {
                IFormulaCreatorOperation opPower = tree[1].Operation as IFormulaCreatorOperation;
                MathFormula p = opPower.CreateFormula(tree[0], level, sizes);
                form.First[1] = p;
            }
            return(form);
        }
예제 #23
0
 /// <summary>
 /// Resets auxiliary variables
 /// </summary>
 private void Zero()
 {
     s = First;
     while (s != null)
     {
         if (s.HasChildren)
         {
             for (int i = 0; i < s.Count; i++)
             {
                 s[i].Zero();
             }
         }
         s = s.Next;
     }
     s    = null;
     s1   = null;
     form = null;
 }
예제 #24
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);
             }
         }
     }
 }
예제 #25
0
        /// <summary>
        /// Creates tree from formula
        /// </summary>
        /// <param name="formula">The formula</param>
        /// <param name="creator">The creator</param>
        /// <returns>The tree</returns>
        public static ObjectFormulaTree CreateTree(MathFormula formula, IFormulaObjectCreator creator)
        {
            ObjectFormulaTree tree = new ObjectFormulaTree(formula, creator);
            IObjectOperation  op   = tree.Operation;

            if (op is ITreeCreator)
            {
                ITreeCreator      tc = op as ITreeCreator;
                ObjectFormulaTree tp = tc.Tree;
                if (tp != null)
                {
                    return(tp);
                }
            }
            if (op == null)
            {
                return(null);
            }
            return(tree);
        }
        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);
            MathFormula f    = new MathFormula(level, sizes);
            MathSymbol  sym  = null;

            if (symbol == '-')
            {
                sym = new BinarySymbol('-');
            }
            else
            {
                sym = new SimpleSymbol(symbol, false, (byte)FormulaConstants.Unary);
            }
            sym.Append(form);
            sym = new BracketsSymbol();
            sym.Append(form);
            form.Last.Children[0] = FormulaCreator.CreateFormula(tree[0], level, sizes);
            form.Last.Children[1] = new MathFormula((byte)(level + 1), sizes);
            return(form);
        }
예제 #27
0
 /// <summary>
 /// Gets variables of fornula
 /// </summary>
 /// <param name="formula">The formula</param>
 /// <param name="s">List of variables</param>
 static public void GetVariables(MathFormula formula, List <char> s)
 {
     for (int i = 0; i < formula.Count; i++)
     {
         MathSymbol sym = formula[i];
         if (sym.SymbolType == (byte)FormulaConstants.Variable)
         {
             char c = sym.Symbol;
             if ((c != 'e') & (c != '%'))
             {
                 if (!s.Contains(c))
                 {
                     s.Add(c);
                 }
             }
         }
         for (int j = 0; j < sym.Count; j++)
         {
             GetVariables(sym[j], s);
         }
     }
 }
예제 #28
0
        /// <summary>
        /// Overriden load method
        /// </summary>
        /// <param name="str">Source string</param>
        /// <returns>Loaded formula</returns>
        public override MathFormula Load(string str)
        {
            try
            {
                return(base.Load(str));
            }
            catch (Exception)
            {
            }
            List <byte[]> list = new List <byte[]>();

            for (int i = 0; i < str.Length; i++)
            {
                char   c = str[i];
                byte[] b = new byte[] { (byte)c };
                list.Add(b);
            }
            MathFormula fo = new MathFormula(0, MathSymbolFactory.Sizes, list, 0, str.Length);

            //new ObjectFormulaTree(fo.FullTransform);
            return(fo);
        }
예제 #29
0
        MathFormula IFormulaSaver.Load(string str)
        {
            MathFormula f = null;

            try
            {
                return(saver.Load(str));
            }
            catch (Exception ex)
            {
            }
            try
            {
                f = new MathFormula(0, MathSymbolFactory.Sizes, str, 0, str.Length,
                                    ElementaryFormulaStringConverter.Object);
                ObjectFormulaTree.CreateTree(f.FullTransform(null));
                return(f);
            }
            catch (Exception)
            {
                List <byte[]> list = new List <byte[]>();
                for (int i = 0; i < str.Length; i++)
                {
                    char   c = str[i];
                    byte[] b = new byte[] { (byte)c };
                    list.Add(b);
                }
                try
                {
                    MathFormula fo = new MathFormula(0, MathSymbolFactory.Sizes, list, 0, str.Length);
                    return(fo);
                }
                catch (Exception ex)
                {
                }
            }
            return(null);
        }
        /// <summary>
        /// Transformation of properties
        /// </summary>
        /// <param name="formula"></param>
        /// <returns></returns>
        static public MathFormula TransformPropery(this MathFormula formula)
        {
            MathFormula form = new MathFormula(formula.Level, formula.Sizes);
            int         n    = formula.Count;

            for (int i = 0; i < n; i++)
            {
                MathSymbol         s = formula[i].Copy();
                List <MathFormula> c = s.Children;
                if (i < n + 2)
                {
                    MathSymbol p  = formula[i + 1];
                    MathSymbol pn = formula[i + 2];
                    if (p.Symbol == '.')
                    {
                        s = new PropertySymbol(s.String, p.String);
                    }
                }
                s.Parent = form;
                form.Add(s);
            }
            return(form);
        }