Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#

Integration: Mechanics + Hydraulics + Navigation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (46 votes)
3 Feb 2011CPOL21 min read 61.5K   6.1K   88  
Sample of integration of branches of engineering.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

using BaseTypes.Interfaces;

using FormulaEditor.Interfaces;

namespace FormulaEditor
{
    /// <summary>
    /// Elementary root
    /// </summary>
    public class ElementaryAtan2 : IMultiVariableOperationAcceptor, IMultiVariableOperation,
        IDerivationOperation, IFormulaCreatorOperation
    {

        /// <summary>
        /// Singleton
        /// </summary>
        public static readonly ElementaryAtan2 Object = new ElementaryAtan2();


        /// <summary>
        /// Return type
        /// </summary>
        private const Double a = 0;


        /// <summary>
        /// The arity
        /// </summary>
        private int arity = 2;


        /// <summary>
        /// Constructor
        /// </summary>
        private ElementaryAtan2()
        {
        }

        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);
            BinaryFunctionSymbol atan = new BinaryFunctionSymbol('A', "atan2");
            atan.Append(form);
            for (int i = 0; i < 2; i++)
            {
                form.First[i] = FormulaCreator.CreateFormula(tree[i], level, sizes);
            }
            return form;
        }


        /// <summary>
        /// Operation priority
        /// </summary>
        public int OperationPriority
        {
            get
            {
                return (int)ElementaryOperationPriorities.Function;
            }
        }



        /// <summary>
        /// Calculates derivation
        /// </summary>
        /// <param name="tree">The function for derivation calculation</param>
        /// <param name="s">Derivation string</param>
        /// <returns>The derivation</returns>
        public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string s)
        {
            Double a = 0;
            ObjectFormulaTree[] fc = new ObjectFormulaTree[2];
            ObjectFormulaTree[] fd = new ObjectFormulaTree[2];
            for (int i = 0; i < 2; i++)
            {
                fc[i] = tree[i];//.Clone() as ObjectFormulaTree;
                fd[i] = DerivationPerformer.Derivation(fc[i], s);
            }
            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]);//.Clone() as ObjectFormulaTree);
                l.Add(fd[i]);//.Clone() as ObjectFormulaTree);
                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));
            return new ObjectFormulaTree(ElementaryFraction.Object, list);
        }




        /// <summary>
        /// Arity of this operation
        /// </summary>
        public int Arity
        {
            get
            {
                return arity;
            }
        }

        /// <summary>
        /// Calculates result of this operation
        /// </summary>
        public object this[object[] x]
        {
            get
            {
                double a = (double)x[0];
                double b = (double)x[1];
                return Math.Atan2(a, b);
            }
        }

        /// <summary>
        /// Return type
        /// </summary>
        public object ReturnType
        {
            get
            {
                return a;
            }
        }

        /// <summary>
        /// The "is powered" sign
        /// </summary>
        public bool IsPowered
        {
            get
            {
                return false;
            }
        }



        /// <summary>
        /// Accepts operation
        /// </summary>
        /// <param name="type">Argument type</param>
        /// <returns>The operation</returns>
        public IObjectOperation Accept(object type)
        {
            if (type.Equals(a))
            {
                return this;
            }
            return null;
        }

        /// <summary>
        /// Accepts operation
        /// </summary>
        /// <param name="types">Types of variables</param>
        /// <returns>Accepted operation</returns>
        public IObjectOperation Accept(object[] types)
        {
            for (int i = 0; i < types.Length; i++)
            {
                if (!types[i].Equals(a))
                {
                    return null;
                }
            }
            arity = types.Length;
            return this;
        }


        /// <summary>
        /// Accepts operation
        /// </summary>
        /// <param name="symbol">The symbol</param>
        /// <returns>Accepted operation</returns>
        public IMultiVariableOperation AcceptOperation(MathSymbol symbol)
        {
            if (symbol is BinaryFunctionSymbol)
            {
                return this;
            }
            return null;
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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