Click here to Skip to main content
15,897,519 members
Articles / Programming Languages / C#

Universal Framework for Science and Engineering - Part 2: Regression

Rate me:
Please Sign up or sign in to vote.
4.77/5 (19 votes)
11 Jul 20067 min read 51.2K   5K   76  
An article on universal scalable engineering framework applications.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace FormulaEditor
{
    /// <summary>
    /// Elementary power operation
    /// </summary>
    public class ElementaryPowerOperation : IObjectOperation, ICloneable, IDerivationOperation,
        IFormulaCreatorOperation
    {

        /// <summary>
        /// Type of operation
        /// </summary>
        private const Double a = 0;

        /// <summary>
        /// Type of value
        /// </summary>
        private object valType;

        /// <summary>
        /// Type of pow
        /// </summary>
        private object powType;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="valType"></param>
        /// <param name="powType"></param>
        public ElementaryPowerOperation(object valType, object powType)
        {
            this.valType = valType;
            this.powType = powType;
        }


        /// <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 f = FormulaCreator.CreateFormula(tree[0], level, sizes);
            MathFormula p = FormulaCreator.CreateFormula(tree[1], (byte)((int)level + 1), sizes);
            IObjectOperation op = tree[0].Operation;
            if (op.IsPowered)
            {
                if (op is ElementaryFunctionOperation)
                {
                    f.First[0] = p;
                }
                else
                {
                    f.Last[0] = p;
                }
                return f;
            }
            MathFormula form = new MathFormula(level, sizes);
            MathSymbol s = new BracketsSymbol();
            s.Append(form);
            form.First[0] = p;
            return form;
        }

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



        /// <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> mainList = new List<ObjectFormulaTree>();
            IObjectOperation mainOperation = new ElementaryBinaryOperation('+', a);
            List<ObjectFormulaTree> firstList = new List<ObjectFormulaTree>();
            IObjectOperation firstOperation = new ElementaryBinaryOperation('*', a);
            List<ObjectFormulaTree> secondList = new List<ObjectFormulaTree>();
            IObjectOperation secondOperation = new ElementaryBinaryOperation('*', a);
            List<ObjectFormulaTree> firstFirstList = new List<ObjectFormulaTree>();
            firstFirstList.Add(fc[1]);
            IObjectOperation firstFirstOperation = new ElementaryBinaryOperation('*', 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('-', a);
            firstFirstSecondSecondList.Add(fc[1]);
            IObjectOperation firstFirstSecondSecondSecondOperation = new ElementaryRealConstant(1);
            ObjectFormulaTree firstFirstSecondSecondSecondTree =
                new ObjectFormulaTree(firstFirstSecondSecondSecondOperation, new List<ObjectFormulaTree>());
            firstFirstSecondSecondList.Add(firstFirstSecondSecondSecondTree);
            ObjectFormulaTree firstFirstSecondSecondTree =
                new ObjectFormulaTree(firstFirstSecondSecondOperation, firstFirstSecondSecondList);
            firstFirstSecondList.Add(firstFirstSecondSecondTree);
            ObjectFormulaTree 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('*', 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);
            return new ObjectFormulaTree(mainOperation, mainList);
        }



        /// <summary>
        /// ICloneable interface implementation
        /// </summary>
        /// <returns>Clone of itself</returns>
        public object Clone()
        {
            return new ElementaryPowerOperation(valType, powType);
        }

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

        /// <summary>
        /// Calculates result of this operation
        /// </summary>
        public object this[object[] x]
        {
            get
            {
                return calculate(x);
            }
        }

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

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

        /// <summary>
        /// Calculates itself
        /// </summary>
        /// <param name="x">Arguments</param>
        /// <returns>Result of calculation</returns>
        private double calculate(object[] x)
        {
            double a = (double)x[0];
            double b = (double)x[1];
            return Math.Pow(a, b);
        }
    }
}

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