Click here to Skip to main content
15,894,539 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.8K   6.1K   88  
Sample of integration of branches of engineering.
using System;
using System.Collections.Generic;
using System.Text;

using BaseTypes;
using BaseTypes.Interfaces;

using FormulaEditor.Interfaces;

namespace FormulaEditor.CSharp
{
    public class CSharpExtendedCodeCreator : CSharpCodeCreator
    {

        #region Fields

        new public static readonly ICodeCreator CodeCreator = new CSharpExtendedCodeCreator(null);

        #endregion


        #region Ctor

        private CSharpExtendedCodeCreator(ObjectFormulaTree[] trees)
            : base(trees)
        {
        }

        #endregion

        #region Overriden

        public override ICodeCreator Create(ObjectFormulaTree[] trees)
        {
           return new CSharpExtendedCodeCreator(trees);
        }

        public override IList<string> CreateCode(ObjectFormulaTree tree, string ret, string[] parameters, out IList<string> variables, out IList<string> initializers)
        {
            IObjectOperation op = tree.Operation;
            if (op is RealMatrixMultiplication)
            {
                return CreateMultiplication(tree, ret, parameters, out variables, out initializers);
            }
            if (op is ScalarProduct)
            {
                return CreateScalarProduct(tree, ret, parameters, out variables, out initializers);
            }
            return base.CreateCode(tree, ret, parameters, out variables, out initializers);
        }

        #endregion

        #region Members

        List<string> CreateMultiplication(ObjectFormulaTree tree, string ret, string[] parameters, out IList<string> variables, out IList<string> initializers)
        {
            variables = new List<string>();
            initializers = new List<string>();
            List<string> mult = new List<string>();
            ArrayReturnType[] t = new ArrayReturnType[tree.Count];
            for (int i = 0; i < t.Length; i++)
            {
                t[i] = tree[i].ReturnType as ArrayReturnType;
            }
            int n = t[0].Dimension[0];
            int m = t[1].Dimension[1];
            int l = t[0].Dimension[1];
            string p1 = parameters[0];
            string p2 = parameters[1];
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++)
            {
                string si = "[" + i + ",";
                string ret1 = ret + si;
                string par1 = parameters[0] + si;
                for (int j = 0; j < m; j++)
                {
                    sb.Append(ret1);
                    sb.Append(j);
                    sb.Append("] = ");
                    string sj = j + "]";
                    for (int k = 0; k < l; k++)
                    {
                        sb.Append(par1);
                        sb.Append(k);
                        sb.Append("] * ");
                        sb.Append(p2);
                        sb.Append("[");
                        sb.Append(k);
                        sb.Append(", ");
                        sb.Append(sj);
                        if (k < (l - 1))
                        {
                            sb.Append(" + ");
                        }
                        else
                        {
                            sb.Append(";");
                            mult.Add(sb.ToString());
                            sb = new StringBuilder();
                        }
                    }
                }
            }
            return mult;
        }

        List<string> CreateScalarProduct(ObjectFormulaTree tree, string ret, string[] parameters, out IList<string> variables, out IList<string> initializers)
        {
            List<string> res = new List<string>();
            variables = new List<string>();
            initializers = new List<string>();
            variables = new List<string>();
            initializers = new List<string>();
            ArrayReturnType[] t = new ArrayReturnType[tree.Count];
            for (int i = 0; i < t.Length; i++)
            {
                t[i] = tree[i].ReturnType as ArrayReturnType;
            }
            int n = t[0].Dimension[0];
            int m = t[1].Dimension[0];
            string p1 = parameters[0];
            string p2 = parameters[1];
            StringBuilder sb = new StringBuilder();
            string s = ret + " = ";
            sb.Append(s);
            for (int i = 0; i < n; i++)
            {
                string ind = "[" + i + "]";
                string sp = p1 + ind + " * " + p2 + ind;
                sb.Append(sp);
                if (i < n - 1)
                {
                    sb.Append(" + ");
                }
            }
            sb.Append(";");
            res.Add(sb.ToString());
            return res;
        }


        #endregion
    }
}

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