Exemple #1
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));
        }
 static StaticExtensionFormulaEditor()
 {
     "orderby".AddAdditionalFormula();
     "where".AddAdditionalFormula();
     "average".AddAdditionalFormula();
     "IndexOf".AddAdditionalFormula();
     MathSymbolFactory.Sizes = new int[] { 15, 11, 9, 6 };
     loadFormulaResources();
     ObjectFormulaTree.Creator =
         new FormulaArrayObjectCreator(ElementaryFunctionsCreator.Object);
     ElementaryFunctionOperation.InitDeri();
     ElementaryIntegerOperation.Prepare();
     ParallelCount = 0;
 }
Exemple #3
0
        /// <summary>
        /// Integral of tree
        /// </summary>
        /// <param name="tree">The tree</param>
        /// <returns>The integral</returns>
        public static double Integral(ObjectFormulaTree tree)
        {
            IObjectOperation op = tree.Operation;

            if (op is IDistribution)
            {
                IDistribution df = op as IDistribution;
                return(df.Integral);
            }
            if (op is ElementaryBinaryOperation)
            {
                ElementaryBinaryOperation ebo = op as ElementaryBinaryOperation;
                char c = ebo.Symbol;
                if (c == '+')
                {
                    return(getSum(tree));
                }
                if (c == '-')
                {
                    return(getDiff(tree));
                }
                if (c == '*')
                {
                    return(getMult(tree));
                }
            }
            if (op is ElementaryFunctionOperation)
            {
                ElementaryFunctionOperation efo = op as ElementaryFunctionOperation;
                char   c = efo.Symbol;
                double a = Integral(tree[0]);
                if (c == '?')
                {
                    return(a);
                }
                if (c == '-')
                {
                    return(-a);
                }
            }
            return(0);
        }
Exemple #4
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(ElementaryFunctionOperation.SquareRootDerivation(tree, variableName));
            }
            IObjectOperation         mainOp   = ElementaryFunctionsCreator.Object.GetPowerOperation(a, a);
            List <ObjectFormulaTree> mainList = new List <ObjectFormulaTree>();

            mainList.Add(tree[0]);
            IObjectOperation         secondOp            = ElementaryFraction.Object;
            List <ObjectFormulaTree> secondList          = new List <ObjectFormulaTree>();
            IObjectOperation         secondFistOperation = new ElementaryRealConstant(1);
            ObjectFormulaTree        secondFirstTree     = new ObjectFormulaTree(secondFistOperation, new List <ObjectFormulaTree>());

            secondList.Add(secondFirstTree);
            secondList.Add(tree[1]);
            ObjectFormulaTree secondTree = new ObjectFormulaTree(secondOp, secondList);

            mainList.Add(secondTree);
            return((new ObjectFormulaTree(mainOp, mainList)).Derivation(variableName));
        }
Exemple #5
0
        /// <summary>
        /// Checks whether tree contains distributions
        /// </summary>
        /// <param name="tree">The tree</param>
        /// <returns>True if contains and false otherwise</returns>
        static bool CheckDelta(ObjectFormulaTree tree)
        {
            int count = 0;

            if (tree == null)
            {
                return(false);
            }
            if (tree.Operation is IDistribution)
            {
                return(true);
            }
            bool b = false;

            for (int i = 0; i < tree.Count; i++)
            {
                ObjectFormulaTree t = tree[i];
                if (t == null)
                {
                    continue;
                }
                b |= CheckDelta(t);
                if (t.Operation is IDistribution)
                {
                    ++count;
                }
            }
            IObjectOperation op = tree.Operation;

            if (b)
            {
                if (op.InputTypes.Length == 2)
                {
                    if (op is ElementaryBinaryOperation)
                    {
                        ElementaryBinaryOperation bop = op as ElementaryBinaryOperation;
                        char s = bop.Symbol;
                        if (s != '+' & s != '-' & s != '*')
                        {
                            throwError();
                        }
                        if (count > 1 & s == '*')
                        {
                            throwError();
                        }
                    }
                    else if (op is ElementaryFunctionOperation)
                    {
                        ElementaryFunctionOperation eop = op as ElementaryFunctionOperation;
                        char c = eop.Symbol;
                        if (c != '!' & c != '-')
                        {
                            throwError();
                        }
                    }
                    else
                    {
                        throwError();
                    }
                }
            }
            return(b | count > 0);
        }
Exemple #6
0
        private static ObjectFormulaTree sumSumInverse(PolySum ps)
        {
            Dictionary <ObjectFormulaTree, bool> d = new Dictionary <ObjectFormulaTree, bool>(ps.summands);
            ObjectFormulaTree first  = null;
            ObjectFormulaTree second = null;
            bool bf = true;

            foreach (ObjectFormulaTree t in d.Keys)
            {
                if (ElementaryFormulaSimplification.IsConst(t))
                {
                    first = t;
                }
            }
            if (first == null)
            {
                if (d.Count > 0)
                {
                    foreach (ObjectFormulaTree t in d.Keys)
                    {
                        first = t;
                        bf    = d[t];
                        break;
                    }
                }
            }
            if (first == null)
            {
                ElementaryRealConstant er = new ElementaryRealConstant(0);
                return(new ObjectFormulaTree(er, new List <ObjectFormulaTree>()));
            }
            d.Remove(first);
            first = sumSumInverse(first);
            if (!bf)
            {
                ElementaryFunctionOperation ef = new ElementaryFunctionOperation('-');
                List <ObjectFormulaTree>    l  = new List <ObjectFormulaTree>();
                l.Add(first);
                first = new ObjectFormulaTree(ef, l);
            }
            if (d.Count == 0)
            {
                return(first);
            }
            ObjectFormulaTree sec = null;

            foreach (ObjectFormulaTree t in d.Keys)
            {
                sec = t;
            }
            bool    kb = d[sec];
            PolySum pp = new PolySum(true);
            Dictionary <ObjectFormulaTree, bool> dd = pp.summands;

            foreach (ObjectFormulaTree t in d.Keys)
            {
                if (PolyMult.IsZero(t))
                {
                    continue;
                }
                bool bl = d[t];
                if (!kb)
                {
                    bl = !bl;
                }
                dd[t] = bl;
            }
            second = sumSumInverse(pp);
            List <ObjectFormulaTree> lr = new List <ObjectFormulaTree>();

            lr.Add(first);
            lr.Add(second);
            char c = kb ? '+' : '-';

            if (PolyMult.IsZero(second))
            {
                return(first);
            }
            Double type = 0;
            ElementaryBinaryOperation eop = new ElementaryBinaryOperation(c, new object[] { type, type });

            return(new ObjectFormulaTree(eop, lr));
        }
Exemple #7
0
        private static ObjectFormulaTree delConst(PolySum ps, ref bool simple)
        {
            Dictionary <ObjectFormulaTree, bool> dd  = ps.summands;
            List <ObjectFormulaTree>             del = new List <ObjectFormulaTree>();
            Dictionary <ObjectFormulaTree, bool> d   = new Dictionary <ObjectFormulaTree, bool>();

            foreach (ObjectFormulaTree ttt in dd.Keys)
            {
                ObjectFormulaTree ts = ElementaryFormulaSimplification.Object.Simplify(ttt);
                if (!PolyMult.IsZero(ts))
                {
                    d[ts] = dd[ttt];
                }
            }
            PolySum p = new PolySum(true);
            Dictionary <ObjectFormulaTree, bool> forms = p.summands;
            double a = 0;
            int    i = 0;

            foreach (ObjectFormulaTree t in d.Keys)
            {
                bool b = d[t];
                ObjectFormulaTree tr = delConst(t, ref simple);
                if (PolyMult.IsZero(tr))
                {
                    simple = false;
                    continue;
                }
                if (ElementaryFormulaSimplification.IsConst(tr))
                {
                    double x = (double)tr.Result;
                    a += b ? x : -x;
                    ++i;
                    if (i > 1)
                    {
                        simple = false;
                    }
                }
                else
                {
                    forms[tr] = d[t];
                }
            }
            if (a != 0)
            {
                ElementaryRealConstant ec = new ElementaryRealConstant(a);
                forms[new ObjectFormulaTree(ec, new List <ObjectFormulaTree>())] = true;
            }
            if (forms.Count == 1)
            {
                foreach (ObjectFormulaTree f in forms.Keys)
                {
                    bool b = forms[f];
                    if (b)
                    {
                        return(f);
                    }
                    ElementaryFunctionOperation op  = new ElementaryFunctionOperation('-');
                    List <ObjectFormulaTree>    lop = new List <ObjectFormulaTree>();
                    lop.Add(f);
                    return(new ObjectFormulaTree(op, lop));
                }
            }
            return(new ObjectFormulaTree(p, new List <ObjectFormulaTree>()));
        }
        /// <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)
        {
            Double a = 0;

            ObjectFormulaTree[] fc = new ObjectFormulaTree[2];
            ObjectFormulaTree[] fd = new ObjectFormulaTree[2];
            bool[] b = new bool[] { false, false };
            for (int i = 0; i < 2; i++)
            {
                fc[i] = tree[i];//.Clone() as ObjectFormulaTree;
                fd[i] = fc[i].Derivation(variableName);
                bool bb = ZeroPerformer.IsZero(fd[i]);
                b[i] = bb;
                if (!bb)
                {
                    // glo = true;
                }
            }
            if (b[1])
            {
                double rs = (double)fc[1].Result;
                if (rs == 1)
                {
                    return(fd[0]);
                }
            }
            List <ObjectFormulaTree> mainList        = new List <ObjectFormulaTree>();
            IObjectOperation         mainOperation   = new ElementaryBinaryOperation('+', new object[] { a, a });
            List <ObjectFormulaTree> firstList       = new List <ObjectFormulaTree>();
            IObjectOperation         firstOperation  = new ElementaryBinaryOperation('*', new object[] { a, a });
            List <ObjectFormulaTree> secondList      = new List <ObjectFormulaTree>();
            IObjectOperation         secondOperation = new ElementaryBinaryOperation('*', new object[] { a, a });
            List <ObjectFormulaTree> firstFirstList  = new List <ObjectFormulaTree>();

            firstFirstList.Add(fc[1]);
            IObjectOperation         firstFirstOperation       = new ElementaryBinaryOperation('*', new object[] { a, a });
            IObjectOperation         firstFirstSecondOperation = new ElementaryPowerOperation(valType, powType);
            List <ObjectFormulaTree> firstFirstSecondList      = new List <ObjectFormulaTree>();

            firstFirstSecondList.Add(fc[0]);
            List <ObjectFormulaTree> firstFirstSecondSecondList      = new List <ObjectFormulaTree>();
            IObjectOperation         firstFirstSecondSecondOperation = new ElementaryBinaryOperation('-', new object[] { a, a });
            ObjectFormulaTree        fcd = fd[1];

            firstFirstSecondSecondList.Add(fc[1]);
            IObjectOperation  firstFirstSecondSecondSecondOperation = new ElementaryRealConstant(1);
            ObjectFormulaTree firstFirstSecondSecondSecondTree      =
                new ObjectFormulaTree(firstFirstSecondSecondSecondOperation, new List <ObjectFormulaTree>());

            firstFirstSecondSecondList.Add(firstFirstSecondSecondSecondTree);
            ObjectFormulaTree firstFirstSecondSecondTree = null;
            bool unityDeg = false;

            if (ZeroPerformer.IsZero(fd[1]))
            {
                double f2d = (double)tree[1].Result - 1;
                if (f2d == 1)
                {
                    unityDeg = true;
                }
                ElementaryRealConstant erc = new ElementaryRealConstant(f2d);
                firstFirstSecondSecondTree = new ObjectFormulaTree(erc, new List <ObjectFormulaTree>());
            }
            else
            {
                firstFirstSecondSecondTree =
                    new ObjectFormulaTree(firstFirstSecondSecondOperation, firstFirstSecondSecondList);
            }
            firstFirstSecondList.Add(firstFirstSecondSecondTree);
            ObjectFormulaTree firstFirstSecondTree = null;

            if (unityDeg)
            {
                firstFirstSecondTree = fc[0];
            }
            else
            {
                firstFirstSecondTree =
                    new ObjectFormulaTree(firstFirstSecondOperation, firstFirstSecondList);
            }
            firstFirstList.Add(firstFirstSecondTree);
            ObjectFormulaTree firstFirstTree = new ObjectFormulaTree(firstFirstOperation, firstFirstList);

            firstList.Add(firstFirstTree);
            firstList.Add(fd[0]);
            ObjectFormulaTree firstTree = new ObjectFormulaTree(firstOperation, firstList);

            mainList.Add(firstTree);

            // Second part

            IObjectOperation         secondFirstOperation      = new ElementaryBinaryOperation('*', new object[] { a, a });
            List <ObjectFormulaTree> secondFirstList           = new List <ObjectFormulaTree>();
            IObjectOperation         secondFirstFirstOperation = new ElementaryFunctionOperation('l');
            List <ObjectFormulaTree> secondFirstFirstList      = new List <ObjectFormulaTree>();

            secondFirstFirstList.Add(fc[0]);//.Clone() as ObjectFormulaTree);
            ObjectFormulaTree secondFirstFirstTree =
                new ObjectFormulaTree(secondFirstFirstOperation, secondFirstFirstList);

            secondFirstList.Add(secondFirstFirstTree);
            secondFirstList.Add(tree);//.Clone() as ObjectFormulaTree);
            ObjectFormulaTree secondFirstTree = new ObjectFormulaTree(secondFirstOperation, secondFirstList);

            secondList.Add(secondFirstTree);
            secondList.Add(fd[1]);
            ObjectFormulaTree secondTree = new ObjectFormulaTree(secondOperation, secondList);

            mainList.Add(secondTree);
            if (b[0] & b[1])
            {
                return(ElementaryRealConstant.RealZero);
            }
            for (int i = 0; i < 2; i++)
            {
                if (b[i])
                {
                    return(mainList[1 - i]);
                }
            }
            return(new ObjectFormulaTree(mainOperation, mainList));
        }