Ejemplo n.º 1
0
        private static ObjectFormulaTree sumSumInverse(ObjectFormulaTree tree)
        {
            ObjectFormulaTree tre = tree;
            IObjectOperation  op  = tree.Operation;

            if (op is PolySum)
            {
                PolySum ps = op as PolySum;
                tre = sumSumInverse(ps);
                op  = tre.Operation;
            }
            List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();

            for (int i = 0; i < tre.Count; i++)
            {
                l.Add(sumSumInverse(tre[i]));
            }
            if (op is ElementaryBinaryOperation)
            {
                ElementaryBinaryOperation bo = op as ElementaryBinaryOperation;
                char c = bo.Symbol;
                if (c == '+' | c == '-')
                {
                    if (PolyMult.IsZero(l[1]))
                    {
                        return(l[0]);
                    }
                    if (PolyMult.IsZero(l[0]))
                    {
                        return(signed(l[1], c));
                    }
                }
            }
            return(new ObjectFormulaTree(op, l));
        }
Ejemplo n.º 2
0
        private bool reduce()
        {
            List <ObjectFormulaTree> list = new List <ObjectFormulaTree>(l);
            bool b = true;

            foreach (ObjectFormulaTree t in l)
            {
                if (t.Operation is PolyMult)
                {
                    list.Remove(t);
                    PolyMult pm = t.Operation as PolyMult;
                    list.AddRange(pm.l);
                    b = false;
                    continue;
                }
                if (!(t.Operation is ElementaryBinaryOperation))
                {
                    continue;
                }
                ElementaryBinaryOperation op = t.Operation as ElementaryBinaryOperation;
                if (op.Symbol != '*')
                {
                    continue;
                }
                list.Remove(t);
                for (int i = 0; i < 2; i++)
                {
                    list.Add(t);
                    b = false;
                }
            }
            l = list;
            return(b);
        }
 /// <summary>
 /// Calculates derivation tree
 /// </summary>
 /// <param name="tree">Tree to calculate</param>
 /// <param name="variableName">Name of variable</param>
 /// <returns>Tree of derivation</returns>
 static public ObjectFormulaTree Derivation(this ObjectFormulaTree tree, string variableName)
 {
     if (tree.Operation is IDerivationOperation)
     {
         IDerivationOperation op = tree.Operation as IDerivationOperation;
         return(op.Derivation(tree, variableName));
     }
     if (ElementaryBinaryOperation.IsInteger(tree.Operation.ReturnType))
     {
         ElementaryRealConstant op = new ElementaryRealConstant(0);
         return(new ObjectFormulaTree(op, new List <ObjectFormulaTree>()));
     }
     if (tree.Operation is OptionalOperation)
     {
         OptionalOperation op = tree.Operation as OptionalOperation;
         op = op.Clone() as OptionalOperation;
         List <ObjectFormulaTree> list = new List <ObjectFormulaTree>();
         list.Add(tree[0]);
         for (int i = 1; i < 3; i++)
         {
             list.Add(tree[i].Derivation(variableName));
         }
         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">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));
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Accepts operation
 /// </summary>
 /// <param name="type">Argument type</param>
 /// <returns>The operation</returns>
 public IObjectOperation Accept(object type)
 {
     if (!ElementaryBinaryOperation.IsInteger(type))
     {
         return(null);
     }
     this.type = type;
     return(this);
 }
        /// <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));
        }
        /// <summary>
        /// Derivation of square root
        /// </summary>
        /// <param name="tree">Prototype tree</param>
        /// <param name="variableName">Name of variable</param>
        /// <returns>The derivation</returns>
        public static ObjectFormulaTree SquareRootDerivation(ObjectFormulaTree tree, string variableName)
        {
            ObjectFormulaTree        co     = ElementaryRealConstant.GetConstant(0.5);
            ObjectFormulaTree        derNom = tree[0].Derivation(variableName);
            List <ObjectFormulaTree> lnom   = new List <ObjectFormulaTree>()
            {
                co, derNom
            };
            Double a = 0;
            ElementaryBinaryOperation bo = new ElementaryBinaryOperation('*', new object[] { a, a });
            ObjectFormulaTree         tn = new ObjectFormulaTree(bo, lnom);
            List <ObjectFormulaTree>  l  = new List <ObjectFormulaTree>()
            {
                tn, tree
            };

            return(new ObjectFormulaTree(ElementaryFraction.Object, l));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Acceptor of binary operation
        /// </summary>
        /// <param name="typeA">Type of left part</param>
        /// <param name="typeB">Type of right part</param>
        /// <returns>Accepted operation</returns>
        public IObjectOperation Accept(object typeA, object typeB)
        {
            object type = ElementaryBinaryOperation.DetectType(typeA, typeB);

            if (type == null)
            {
                foreach (IBinaryAcceptor acceptor in acceptors)
                {
                    IObjectOperation op = acceptor.Accept(typeA, typeB);
                    if (op != null)
                    {
                        return(op);
                    }
                }
                return(null);
            }
            return(new ElementaryBinaryOperation(symbol, new object[] { typeA, typeB }));
        }
Ejemplo n.º 10
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];
            for (int i = 0; i < 2; i++)
            {
                fc[i] = tree[i];
                fd[i] = fc[i].Derivation(variableName);
            }
            List <ObjectFormulaTree> list    = new List <ObjectFormulaTree>();
            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(fc[1 - i]);
                l.Add(fd[i]);
                IObjectOperation o = new ElementaryBinaryOperation('*', new object[] { a, a });
                nomList.Add(new ObjectFormulaTree(o, l));
            }
            list.Add(new ObjectFormulaTree(nom, nomList));
            List <ObjectFormulaTree> plusList = new List <ObjectFormulaTree>();

            for (int i = 0; i < 2; i++)
            {
                List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();
                l.Add(fc[i]);
                l.Add(new ObjectFormulaTree(new ElementaryRealConstant(2), new List <ObjectFormulaTree>()));
                IObjectOperation o = ElementaryFunctionsCreator.Object.GetPowerOperation(a, a);
                plusList.Add(new ObjectFormulaTree(o, l));
            }
            IObjectOperation         plusOp    = new ElementaryBinaryOperation('+', new object[] { a, a });
            IObjectOperation         denomOp   = new ElementaryRoot();
            List <ObjectFormulaTree> denomList = new List <ObjectFormulaTree>();

            // denomList.Add(new ObjectFormulaTree(plusOp, plusList));
            //list.Add(new ObjectFormulaTree(new ElementaryRoot(), denomList));
            list.Add(new ObjectFormulaTree(plusOp, plusList));
            return(new ObjectFormulaTree(ElementaryFraction.Object, list));
        }
Ejemplo n.º 11
0
        internal static ObjectFormulaTree MultMultReverse(ObjectFormulaTree tree)
        {
            Double a = 0;
            List <ObjectFormulaTree> l  = new List <ObjectFormulaTree>();
            IObjectOperation         op = tree.Operation;
            PolyMult pm = null;

            if (op is PolyMult)
            {
                pm = op as PolyMult;
            }
            for (int i = 0; i < tree.Count; i++)
            {
                l.Add(MultMultReverse(tree[i]));
            }
            if (pm == null)
            {
                return(new ObjectFormulaTree(op, l));
            }
            if (l.Count == 1)
            {
                return(l[0]);
            }
            PolyMult p = new PolyMult();

            if (l.Count == 0)
            {
                ElementaryRealConstant rc = new ElementaryRealConstant(0);
                return(new ObjectFormulaTree(rc, new List <ObjectFormulaTree>()));
            }
            ObjectFormulaTree left = l[0];

            l.RemoveAt(0);
            p.l = l;
            ObjectFormulaTree        right = MultMultReverse(new ObjectFormulaTree(p, l));
            List <ObjectFormulaTree> lll   = new List <ObjectFormulaTree>();

            lll.Add(left);
            lll.Add(right);
            ElementaryBinaryOperation bo = new ElementaryBinaryOperation('*', new object[] { a, a });

            return(new ObjectFormulaTree(bo, lll));
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Accepts operation
 /// </summary>
 /// <param name="type">Argument type</param>
 /// <returns>The operation</returns>
 public IObjectOperation Accept(object type)
 {
     if (!ElementaryBinaryOperation.IsNumber(type))
     {
         if (!addAcceptors.ContainsKey(type))
         {
             return(null);
         }
         List <IOperationAcceptor> l = addAcceptors[type];
         foreach (IOperationAcceptor acc in l)
         {
             IObjectOperation op = acc.Accept(type);
             if (op != null)
             {
                 return(op);
             }
         }
         return(null);
     }
     return(new ElementaryIntegerOperation(symbol, type));
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Acceptor of binary operation
        /// </summary>
        /// <param name="typeA">Type of left part</param>
        /// <param name="typeB">Type of right part</param>
        /// <returns>Accepted operation</returns>
        public IObjectOperation Accept(object typeA, object typeB)
        {
            IObjectOperation op = DateTimeMoreComparator.Object.Accept(typeA, typeB);

            if (op != null)
            {
                if (symbol == '>')
                {
                    return(DateTimeMoreComparator.Object);
                }
                if (symbol == '<')
                {
                    return(DateTimeLessCompatrator.Object);
                }
            }
            if (!ElementaryBinaryOperation.IsNumber(typeA) | !ElementaryBinaryOperation.IsNumber(typeB))
            {
                return(null);
            }
            return(this);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Acceptor of binary operation
 /// </summary>
 /// <param name="typeA">Type of left part</param>
 /// <param name="typeB">Type of right part</param>
 /// <returns>Accepted operation</returns>
 public IObjectOperation Accept(object typeA, object typeB)
 {
     if (!ElementaryBinaryOperation.IsInteger(typeA))
     {
         throw new Exception("Illegal integer argument");
     }
     if ((symbol == '\u2266') | (symbol == '\u2267'))
     {
         if (!ElementaryBinaryOperation.IsNumber(typeB))
         {
             throw new Exception("Illegal integer argument");
         }
         type = typeA;
         return(this);
     }
     if (!typeA.Equals(typeB))
     {
         throw new Exception("Different types of integer");
     }
     type = typeA;
     return(this);
 }
Ejemplo n.º 16
0
        private static ObjectFormulaTree reduce(ObjectFormulaTree tree)
        {
            bool mult = false;

            if (tree.Operation is ElementaryBinaryOperation)
            {
                ElementaryBinaryOperation op = tree.Operation as ElementaryBinaryOperation;
                if (op.Symbol == '*')
                {
                    mult = true;
                }
            }
            if (mult)
            {
                PolyMult pm = new PolyMult();
                for (int i = 0; i < tree.Count; i++)
                {
                    pm.add(tree);
                }
                pm.repeatReduce();
                List <ObjectFormulaTree> list = new List <ObjectFormulaTree>(pm.l);
                pm.l.Clear();
                foreach (ObjectFormulaTree t in list)
                {
                    ObjectFormulaTree r = reduce(t);
                    list.Add(r);
                    pm.add(r);
                }
                return(new ObjectFormulaTree(pm, list));
            }

            List <ObjectFormulaTree> listr = new List <ObjectFormulaTree>();

            for (int i = 0; i < tree.Count; i++)
            {
                listr.Add(reduce(tree[i]));
            }
            return(new ObjectFormulaTree(tree.Operation, listr));
        }
Ejemplo n.º 17
0
        internal static ObjectFormulaTree MultMult(ObjectFormulaTree tree)
        {
            List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();

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

            if (op is ElementaryBinaryOperation)
            {
                ElementaryBinaryOperation bo = op as ElementaryBinaryOperation;
                if (bo.Symbol == '*')
                {
                    List <ObjectFormulaTree> ll = new List <ObjectFormulaTree>();
                    PolyMult pm = new PolyMult();
                    foreach (ObjectFormulaTree t in l)
                    {
                        if (!(t.Operation is PolyMult))
                        {
                            pm.add(t);
                            ll.Add(t);
                            continue;
                        }
                        for (int i = 0; i < t.Count; i++)
                        {
                            pm.add(t[i]);
                            ll.Add(t[i]);
                        }
                    }
                    return(new ObjectFormulaTree(pm, ll));
                }
            }
            return(new ObjectFormulaTree(op, l));
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
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.º 20
0
 /// <summary>
 /// Calculates result of this operation
 /// </summary>
 public object this[object[] x]
 {
     get
     {
         int i = 0;
         if (!ElementaryBinaryOperation.IsNegative(x[0]) & ElementaryBinaryOperation.IsNegative(x[1]))
         {
             i = 1;
         }
         else if (ElementaryBinaryOperation.IsNegative(x[0]) & !ElementaryBinaryOperation.IsNegative(x[1]))
         {
             i = -1;
         }
         if (!ElementaryBinaryOperation.IsInteger64(x[0]) | !ElementaryBinaryOperation.IsInteger64(x[0]))
         {
             double a = Converter.ToDouble(x[0]);
             double b = Converter.ToDouble(x[1]);
             if (a > b)
             {
                 i = 1;
             }
             if (a < b)
             {
                 i = -1;
             }
         }
         else
         {
             long a = Converter.ToInt64(x[0]);
             long b = Converter.ToInt64(x[1]);
             if (a > b)
             {
                 i = 1;
             }
             if (a < b)
             {
                 i = -1;
             }
         }
         if (symbol == '>')
         {
             return(i == 1);
         }
         if (symbol == '<')
         {
             return(i == -1);
         }
         if (symbol == '\u2260')
         {
             return(i != 0);
         }
         if (symbol == '\u2264')
         {
             return(i != 1);
         }
         if (symbol == '\u2265')
         {
             return(i != -1);
         }
         return(false);
     }
 }
Ejemplo n.º 21
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));
        }
Ejemplo n.º 22
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));
        }