Click here to Skip to main content
15,892,480 members
Articles / Programming Languages / C#

Universal Framework for Science and Engineering - Part 4: Space elevator

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
14 Aug 20066 min read 36.6K   2.2K   37  
An article on framework applications to the space elevator.
using System;
using System.Collections.Generic;
using System.Text;

namespace FormulaEditor
{
    class PolySum : IObjectOperation
    {
        Dictionary<ObjectFormulaTree, bool> summands = new Dictionary<ObjectFormulaTree, bool>();
        bool positive;
        internal PolySum(bool positive)
        {
            this.positive = positive;
        }


        #region IObjectOperation Members

        int IObjectOperation.Arity
        {
            get { return summands.Count; }
        }

        object IObjectOperation.this[object[] x]
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        object IObjectOperation.ReturnType
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        bool IObjectOperation.IsPowered
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        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);
        }

        private static ObjectFormulaTree delConst(ObjectFormulaTree tree, ref bool simple)
        {
            IObjectOperation op = tree.Operation;
            if (op is PolySum)
            {
                PolySum ps = op as PolySum;
                return delConst(ps, ref simple);
            }
            List<ObjectFormulaTree> l = new List<ObjectFormulaTree>();
            for (int i = 0; i < tree.Count; i++)
            {
                l.Add(delConst(tree[i], ref simple));
            }
            if ((op is ElementaryBrackets) | (op is ElementaryRoot) | (op is ElementaryPowerOperation))
            {
                if (l.Count == 2)
                {
                    ObjectFormulaTree t = l[1];
                    if (ElementaryFormulaSimplification.IsConst(t))
                    {
                        double a = (double) t.Result;
                        if (a == 1)
                        {
                            simple = false;
                            return l[0];
                        }
                    }
                }
            }
            return new ObjectFormulaTree(op, l);
        }


        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>());
        }

        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, type);
            return new ObjectFormulaTree(eop, lr);
        }

        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);
        }


        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);
        }
        
        internal static ObjectFormulaTree signed(ObjectFormulaTree tree, char sign)
        {
            return signed(tree, sign == '+');
        }

        internal static ObjectFormulaTree Simplify(ObjectFormulaTree tree, ref bool simple)
        {
            ObjectFormulaTree t = sumSum(tree);
            t = delConst(t, ref simple);
            return sumSumInverse(t);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions