Beispiel #1
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);
        }
Beispiel #2
0
        static internal bool IsZero(ObjectFormulaTree tree)
        {
            IObjectOperation op = tree.Operation;

            if (op is ElementaryRealConstant)
            {
                ElementaryRealConstant ec = op as ElementaryRealConstant;
                double a = ec.Value;
                return(a == 0);
            }
            if (!(op is PolyMult))
            {
                return(false);
            }
            PolyMult pm = op as PolyMult;

            for (int i = 0; i < tree.Count; i++)
            {
                if (IsZero(tree[i]))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #3
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));
        }
Beispiel #4
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));
        }
        /// <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));
        }
        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);
        }
Beispiel #7
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));
        }
Beispiel #8
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));
        }
Beispiel #9
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));
        }
Beispiel #10
0
        static internal ObjectFormulaTree simplify(ObjectFormulaTree tree, out bool simple)
        {
            //bool s = true;
            simple = true;
            List <ObjectFormulaTree> l = new List <ObjectFormulaTree>();

            for (int i = 0; i < tree.Count; i++)
            {
                ObjectFormulaTree t = simplifyRecursive(tree[i]);
                l.Add(t);
            }
            if (!(tree.Operation is PolyMult))
            {
                return(new ObjectFormulaTree(tree.Operation, l));
            }
            List <ObjectFormulaTree> consts = new List <ObjectFormulaTree>();
            List <ObjectFormulaTree> forms  = new List <ObjectFormulaTree>();

            for (int i = 0; i < tree.Count; i++)
            {
                if (ElementaryFormulaSimplification.IsConst(tree[i]))
                {
                    consts.Add(tree[i]);
                    continue;
                }
                forms.Add(tree[i]);
            }
            double a = 1;

            foreach (ObjectFormulaTree t in consts)
            {
                double x = (double)t.Result;
                a *= x;
            }
            if (a == 0)
            {
                ElementaryRealConstant x = new ElementaryRealConstant(0);
                simple = true;
                return(new ObjectFormulaTree(x, new List <ObjectFormulaTree>()));
            }
            if (a == 1)
            {
                PolyMult pm = new PolyMult();
                foreach (ObjectFormulaTree t in forms)
                {
                    pm.add(t);
                }
                if (consts.Count > 0)
                {
                    simple = false;
                }
                return(new ObjectFormulaTree(pm, forms));
            }
            if (consts.Count <= 1)
            {
                PolyMult pm = new PolyMult();
                pm.l = l;
                return(new ObjectFormulaTree(tree.Operation, l));
            }
            simple = false;
            ElementaryRealConstant   xx = new ElementaryRealConstant(a);
            ObjectFormulaTree        yy = new ObjectFormulaTree(xx, new List <ObjectFormulaTree>());
            List <ObjectFormulaTree> ll = new List <ObjectFormulaTree>();

            ll.Add(yy);
            ll.AddRange(forms);
            PolyMult pmm = new PolyMult();

            pmm.l = new List <ObjectFormulaTree>(ll);
            return(new ObjectFormulaTree(pmm, ll));
        }
Beispiel #11
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));
        }
Beispiel #12
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>()));
        }