/// <summary>
        /// Checks whether a tree has a fiction
        /// </summary>
        /// <param name="tree">The tree</param>
        /// <returns>True if operation has fictoin</returns>
        static public bool HasFiction(this ObjectFormulaTree tree)
        {
            if (tree == null)
            {
                return(true);
            }
            IObjectOperation op = tree.Operation;

            if (op == null)
            {
                return(true);
            }
            if (StaticExtensionBaseTypes.HasAttributeBT <Attributes.FictionAttribute>(op))
            {
                return(true);
            }
            for (int i = 0; i < tree.Count; i++)
            {
                if (tree[i].HasFiction())
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #2
0
        MathFormula IFormulaCreatorOperation.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 < (int)ElementaryOperationPriorities.Divide)
                {
                    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);
        }
예제 #3
0
        /// <summary>
        /// Gets unary indexes of tree
        /// </summary>
        /// <param name="tree">The tree</param>
        /// <returns>List of indexes</returns>
        public static List <int> GetUnaryIndexes(ObjectFormulaTree tree)
        {
            List <int> l = new List <int>();

            GetUnaryIndexes(tree, l);
            l.Sort();
            return(l);
        }
 /// <summary>
 /// Gets conditional value of tree
 /// </summary>
 /// <param name="tree">The tree</param>
 /// <returns>Conditional value</returns>
 public static object GetValue(ObjectFormulaTree tree)
 {
     if (!IsConst(tree))
     {
         return(null);
     }
     return(tree.Result);
 }
예제 #5
0
 /// <summary>
 /// Creates distribution from formula tree
 /// </summary>
 /// <param name="tree">The tree</param>
 /// <returns>The distribution</returns>
 static public IDistribution GetDistribution(ObjectFormulaTree tree)
 {
     if (!CheckDelta(tree))
     {
         return(null);
     }
     return(new FormulaDistribution(tree));
 }
예제 #6
0
        /// <summary>
        /// Calculates derivation
        /// </summary>
        /// <param name="tree">The function for derivation calculation</param>
        /// <param name="variableName">Derivation string</param>
        /// <returns>The derivation</returns>
        public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string variableName)
        {
            Double a = 0;

            bool[] b = new bool[] { false, false };
            ObjectFormulaTree[] fc = new ObjectFormulaTree[2];
            ObjectFormulaTree[] fd = new ObjectFormulaTree[2];
            for (int i = 0; i < 2; i++)
            {
                fc[i] = tree[1 - i];
                fd[i] = fc[i].Derivation(variableName);
                b[i]  = ZeroPerformer.IsZero(fd[i]);
            }
            if (b[0] & b[1])
            {
                return(ElementaryRealConstant.RealZero);
            }
            IObjectOperation         nom     = new ElementaryBinaryOperation('-', new object[] { a, a });
            List <ObjectFormulaTree> nomList = new List <ObjectFormulaTree>();

            for (int i = 0; i < 2; i++)
            {
                List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();
                l.Add(fd[1 - i]); //.Clone() as ObjectFormulaTree);
                l.Add(fc[i]);     //.Clone() as ObjectFormulaTree);
                IObjectOperation o = new ElementaryBinaryOperation('*', new object[] { a, a });
                nomList.Add(new ObjectFormulaTree(o, l));
            }
            List <ObjectFormulaTree> list = new List <ObjectFormulaTree>();

            if (b[0] | b[1])
            {
                for (int i = 0; i < 2; i++)
                {
                    if (b[i])
                    {
                        List <ObjectFormulaTree> lt = new List <ObjectFormulaTree>();
                        lt.Add(nomList[i]);
                        list.Add(new ObjectFormulaTree(new ElementaryFunctionOperation('-'), lt));
                    }
                }
            }
            else
            {
                list.Add(new ObjectFormulaTree(nom, nomList));
            }
            IObjectOperation         square     = ElementaryFunctionsCreator.Object.GetPowerOperation(a, a);
            List <ObjectFormulaTree> squareList = new List <ObjectFormulaTree>();

            squareList.Add(fc[0]);
            squareList.Add(new ObjectFormulaTree(new ElementaryRealConstant(2), new List <ObjectFormulaTree>()));
            list.Add(new ObjectFormulaTree(square, squareList));
            if (list.Count != 2)
            {
                // list = list;
            }
            return(new ObjectFormulaTree(new ElementaryFraction(), list));
        }
 /// <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>
        /// Simplfies tree
        /// </summary>
        /// <param name="tree">Simplfied tree</param>
        /// <returns>Simplfication result</returns>
        public override ObjectFormulaTree Simplify(ObjectFormulaTree tree)
        {
            ObjectFormulaTree t = tree;//.Clone() as ObjectFormulaTree;

            t = simplify(t);
            t = PolyMult.MultMult(t);
            t = simplify(t);
            return(PolyMult.MultMultReverse(t));
        }
예제 #9
0
        private static ObjectFormulaTree sumSum(ObjectFormulaTree tree)
        {
            List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();

            for (int i = 0; i < tree.Count; i++)
            {
                ObjectFormulaTree t = tree[i];
                // t = ElementaryFormulaSimplification.Object.Simplify(t);
                l.Add(sumSum(t));
            }
            IObjectOperation op = tree.Operation;

            if (op is ElementaryBinaryOperation)
            {
                ElementaryBinaryOperation bo = op as ElementaryBinaryOperation;
                char    c  = bo.Symbol;
                PolySum ps = new PolySum(true);
                if (c == '+' | c == '-')
                {
                    bool    b = c == '+';
                    PolySum p = new PolySum(true);
                    Dictionary <ObjectFormulaTree, bool> dic = p.summands;
                    for (int i = 0; i < l.Count; i++)
                    {
                        ObjectFormulaTree t = l[i];
                        if (PolyMult.IsZero(t))
                        {
                            continue;
                        }
                        bool kb = true;
                        if (i > 0)
                        {
                            kb = b;
                        }
                        IObjectOperation oo = t.Operation;
                        if (oo is PolySum)
                        {
                            PolySum pss = oo as PolySum;
                            Dictionary <ObjectFormulaTree, bool> d = pss.summands;
                            foreach (ObjectFormulaTree tr in d.Keys)
                            {
                                bool rb = d[tr];
                                if (!kb)
                                {
                                    rb = !rb;
                                }
                                dic[tr] = rb;
                            }
                            continue;
                        }
                        dic[t] = kb;
                    }
                    return(new ObjectFormulaTree(p, new List <ObjectFormulaTree>()));
                }
            }
            return(new ObjectFormulaTree(op, l));
        }
예제 #10
0
        /// <summary>
        /// Checks whether tree is zero
        /// </summary>
        /// <param name="tree">The tree</param>
        /// <returns>True in case of zero and false otherwise</returns>
        static public bool IsZero(ObjectFormulaTree tree)
        {
            IObjectOperation op = tree.Operation;

            if (op is ISupportsZero)
            {
                ISupportsZero sz = op as ISupportsZero;
                return(sz.IsZero(tree));
            }
            return(false);
        }
예제 #11
0
            internal Variable(IObjectOperation variable, ObjectFormulaTree[] children)
            {
                this.variable = variable;
                this.children = children;
                type          = children[0].ReturnType;
                types         = new object[] { type };
                ArrayReturnType art = children[0].ReturnType as ArrayReturnType;

                retType = art.ElementType;
                tree    = Convert(children[1]);
            }
예제 #12
0
        internal static ObjectFormulaTree signed(ObjectFormulaTree tree, bool sign)
        {
            if (sign)
            {
                return(tree);
            }
            ElementaryFunctionOperation eo = new ElementaryFunctionOperation('-');
            List <ObjectFormulaTree>    l  = new List <ObjectFormulaTree>();

            l.Add(tree);
            return(new ObjectFormulaTree(eo, l));
        }
예제 #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);
            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);
        }
예제 #14
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);
        }
예제 #15
0
        /// <summary>
        /// Calculates derivation
        /// </summary>
        /// <param name="tree">The function for derivation calculation</param>
        /// <param name="variableName">Name of variable</param>
        /// <returns>The derivation</returns>
        public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string variableName)
        {
            if (arity == 1)
            {
                return(tree[0].Derivation(variableName));
            }
            IDerivationOperation pow =
                ElementaryFunctionsCreator.Object.GetPowerOperation(tree[0].ReturnType, tree[1].ReturnType)
                as IDerivationOperation;

            return(pow.Derivation(tree, variableName));
        }
예제 #16
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);
        }
예제 #17
0
 private void Normalize()
 {
     if (operation is ElementaryBrackets)
     {
         ObjectFormulaTree tr = children[0];
         children  = tr.children;
         operation = tr.Operation;
         y         = tr.y;
         result    = tr.result;
         tag       = tr.tag;
     }
 }
예제 #18
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);
        }
        /// <summary>
        /// Calculates derivation
        /// </summary>
        /// <param name="tree">The function for derivation calculation</param>
        /// <param name="s">Derivation string</param>
        /// <returns>The derivation</returns>
        public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string s)
        {
            int i = 0;

            if (symbol == '-')
            {
                i = functionDerivations.Length - 1;
            }
            else
            {
                i = MathSymbol.FUNCTIONS.IndexOf(symbol);
            }
            return(Derivation(tree, i, s));
        }
예제 #20
0
        static internal ObjectFormulaTree reduceTree(ObjectFormulaTree tree, bool simple)
        {
            bool b;

            simple = true;
            ObjectFormulaTree t  = reduce(tree);
            ObjectFormulaTree st = simplify(t, out b);

            if (!b)
            {
                simple = false;
            }
            return(st);
        }
예제 #21
0
 private static void AddUnique(ObjectFormulaTree tree, List <ObjectFormulaTree> l)
 {
     foreach (ObjectFormulaTree t in tree.children)
     {
         if (l.Contains(t))
         {
             continue;
         }
         AddUnique(t, l);
     }
     if (!l.Contains(tree))
     {
         l.Add(tree);
     }
 }
예제 #22
0
        /// <summary>
        /// Resets distributons of tree
        /// </summary>
        /// <param name="tree">The tree</param>
        static public void Reset(ObjectFormulaTree tree)
        {
            IObjectOperation op = tree.Operation;

            if (op is IDistribution)
            {
                IDistribution d = op as IDistribution;
                d.Reset();
                return;
            }
            for (int i = 0; i < tree.Count; i++)
            {
                Reset(tree[i]);
            }
        }
예제 #23
0
        /// <summary>
        /// Gets integral of multiplication
        /// </summary>
        /// <param name="tree">The tree</param>
        /// <returns>The integral</returns>
        private static double getMult(ObjectFormulaTree tree)
        {
            double a = Integral(tree[0]);
            double b = Integral(tree[1]);

            if (a != 0)
            {
                return(a * (double)tree[1].Result);
            }
            if (b != 0)
            {
                return(b * (double)tree[0].Result);
            }
            return(0);
        }
        private ObjectFormulaTree simplify(ObjectFormulaTree tree)
        {
            ObjectFormulaTree t = tree;

            while (true)
            {
                bool b;
                t = PolyMult.simplify(t, out b);
                t = PolySum.Simplify(t, ref b);
                if (b)
                {
                    break;
                }
            }
            return(t);
        }
예제 #25
0
        static private ObjectFormulaTree simplifyRecursive(ObjectFormulaTree tree)
        {
            ObjectFormulaTree t = tree;

            while (true)
            {
                bool b;
                t = simplify(t, out b);
                // t = PolyMult.Simplify(t, ref b);
                if (b)
                {
                    break;
                }
            }
            return(t);
        }
예제 #26
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);
        }
예제 #27
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);
        }
예제 #28
0
        /// <summary>
        /// Inserts formula instead variable
        /// </summary>
        /// <param name="tree">Prototype</param>
        /// <param name="insert">Inerted formula</param>
        /// <returns>Result of the operation</returns>
        private static ObjectFormulaTree insertVariable(ObjectFormulaTree tree, ObjectFormulaTree insert)
        {
            Double a = 0;

            if (tree.Operation is ElementaryObjectVariable & tree.ReturnType.Equals(a))
            {
                return(insert);//.Clone() as ObjectFormulaTree;
            }
            List <ObjectFormulaTree> children = new List <ObjectFormulaTree>();

            for (int i = 0; i < tree.Count; i++)
            {
                ObjectFormulaTree t = tree[i];//.Clone() as ObjectFormulaTree;
                children.Add(insertVariable(t, insert));
            }
            return(new ObjectFormulaTree(tree.Operation, children));
        }
예제 #29
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);
        }
        /// <summary>
        /// Checks whether tree is const
        /// </summary>
        /// <param name="tree">The tree</param>
        /// <returns>True in case of const and false otherwise</returns>
        public static bool IsConst(ObjectFormulaTree tree)
        {
            for (int i = 0; i < tree.Count; i++)
            {
                if (!IsConst(tree[i]))
                {
                    return(false);
                }
            }
            IObjectOperation op = tree.Operation;

            if (op is ElementaryRealConstant)
            {
                return(true);
            }
            return(false);
        }