Ejemplo n.º 1
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>
        /// Calculates derivation of i - th function
        /// </summary>
        /// <param name="tree">Object tree</param>
        /// <param name="i">Function number</param>
        /// <param name="variableName">Name of variable</param>
        /// <returns>The derivation</returns>
        static private ObjectFormulaTree Derivation(ObjectFormulaTree tree, int i, string variableName)
        {
            Double            a  = 0;
            ObjectFormulaTree f1 = tree[0].Derivation(variableName);

            if (ZeroPerformer.IsZero(f1))
            {
                return(ElementaryRealConstant.RealZero);
            }
            ObjectFormulaTree        fd        = functionDerivations[i];
            ObjectFormulaTree        f0        = InsertVariable(fd, tree[0]);
            IObjectOperation         operation = new ElementaryBinaryOperation('*', new object[] { a, a });
            List <ObjectFormulaTree> children  = new List <ObjectFormulaTree>();

            children.Add(f0);
            children.Add(f1);
            return(new ObjectFormulaTree(operation, children));
        }
Ejemplo n.º 3
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>
 ObjectFormulaTree IDerivationOperation.Derivation(ObjectFormulaTree tree, string variableName)
 {
     bool[] b = new bool[] { false, false };
     if ((symbol == '+') | (symbol == '-')) // "+" or "-" operation
     {
         IObjectOperation op = new ElementaryBinaryOperation(symbol,
                                                             new object[] { tree[0].ReturnType, tree[1].ReturnType });
         List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();
         for (int i = 0; i < tree.Count; i++)
         {
             ObjectFormulaTree t = tree[i].Derivation(variableName);
             b[i] = ZeroPerformer.IsZero(t);
             l.Add(t);
         }
         if (b[0])
         {
             if (b[1])
             {
                 return(ElementaryRealConstant.RealZero);
             }
             if (symbol == '+')
             {
                 return(l[1]);
             }
             List <ObjectFormulaTree> ll = new List <ObjectFormulaTree>();
             ll.Add(l[1]);
             return(new ObjectFormulaTree(new ElementaryFunctionOperation('-'), ll));
         }
         if (b[1])
         {
             return(l[0]);
         }
         return(new ObjectFormulaTree(op, l));
     }
     ObjectFormulaTree[] der = new ObjectFormulaTree[2];
     for (int i = 0; i < 2; i++)
     {
         der[i] = tree[i].Derivation(variableName);
         b[i]   = ZeroPerformer.IsZero(der[i]);
     }
     if (symbol == '*') // "*" - operation
     {
         List <ObjectFormulaTree> list = new List <ObjectFormulaTree>();
         for (int i = 0; i < 2; i++)
         {
             List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();
             l.Add(tree[i]);
             l.Add(der[1 - i]);
             ElementaryBinaryOperation o = new ElementaryBinaryOperation('*',
                                                                         new object[] { l[0].ReturnType, l[1].ReturnType });
             list.Add(new ObjectFormulaTree(o, l));
         }
         if (b[0] & b[1])
         {
             return(ElementaryRealConstant.RealZero);
         }
         for (int i = 0; i < b.Length; i++)
         {
             if (b[i])
             {
                 return(list[i]);
             }
         }
         ElementaryBinaryOperation op = new ElementaryBinaryOperation('+',
                                                                      new object[] { list[0].ReturnType, list[1].ReturnType });
         return(new ObjectFormulaTree(op, list));
     }
     return(null);
 }
Ejemplo n.º 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)
        {
            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));
        }