Click here to Skip to main content
15,879,184 members
Articles / Programming Languages / C#

Universal Framework for Science and Engineering - Part 6: Determination of Orbits of Artificial Satellites

Rate me:
Please Sign up or sign in to vote.
4.88/5 (28 votes)
8 Jul 2011CPOL19 min read 82.4K   6.6K   82  
An article on framework applications to determine the orbits of artificial satellites
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace ClassicalAlgebra
{
    /// <summary>
    /// Complex number
    /// </summary>
    public class Complex : ISerializable, ICloneable
    {

        /// <summary>
        /// Real part
        /// </summary>
        private double re;

        /// <summary>
        /// Image part
        /// </summary>
        private double im;

        /// <summary>
        /// Constructor from double
        /// </summary>
        /// <param name="x">The double</param>
        public Complex(double x)
            : this(x, 0)
        {
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public Complex()
            : this(0)
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="re">real</param>
        /// <param name="im">imag</param>
        public Complex(double re, double im)
        {
            this.re = re;
            this.im = im;
        }

        /// <summary>
        /// ISerializable interface implementation
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Serialization context</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Re", re);
            info.AddValue("Im", im);
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        public Complex(SerializationInfo info, StreamingContext context)
        {
            re = (double)info.GetValue("Re", typeof(double));
            im = (double)info.GetValue("Im", typeof(double));
        }



        /// <summary>
        /// Overriden equals function
        /// </summary>
        /// <param name="o">Element for comparation</param>
        /// <returns>True if o equals this object and false otherwise</returns>
        public override bool Equals(object o)
        {
            if (!(o is Complex))
            {
                throw new Exception("Comparation of QElement with another type element");
            }
            Complex c = o as Complex;
            return (re == c.re) & (im == c.im);
        }

        /// <summary>
        /// Overriden GetHashCode
        /// </summary>
        /// <returns>Hash Code</returns>
        public override int GetHashCode()
        {
            return (int)re * (int)im;
        }

        /// <summary>
        /// Inversion
        /// </summary>
        public Complex Invert
        {
            get
            {
                double n = Norm;
                n *= n;
                return new Complex(re / n, -im / n);
            }
        }


        /// <summary>
        /// Is invertable flag
        /// </summary>
        public bool IsInvertable
        {
            get
            {
                return (re != 0) & (im != 0);
            }
        }

        /// <summary>
        /// Clones itself
        /// </summary>
        /// <returns>A clone</returns>
        public object Clone()
        {
            return new Complex(re, im);
        }

        /// <summary>
        /// Norm of the element
        /// </summary>
        public double Norm
        {
            get
            {
                return Math.Abs(Math.Sqrt((re * re) + (im * im)));
            }
        }

        /// <summary>
        /// Square
        /// </summary>
        public double Square
        {
            get
            {
                return (re * re) + (im * im);
            }
        }

        /// <summary>
        /// Square root
        /// </summary>
        public Complex SquareRoot
        {
            get
            {
                if (im < 1e-15)
                {
                    if (re >= 0)
                    {
                        return Math.Sqrt(re);
                    }
                    return new Complex(0, Math.Sqrt(-re));
                }
                double n = Math.Sqrt(Norm);
                double ar = Phi / 2;
                double rr = n * Math.Cos(ar);
                double ri = n * Math.Sin(ar);
                return new Complex(rr, ri);
            }
        }


        /// <summary>
        /// Real part
        /// </summary>
        public double Real
        {
            get
            {
                return re;
            }
        }

        /// <summary>
        /// Image part
        /// </summary>
        public double Imag
        {
            get
            {
                return im;
            }
        }

        /// <summary>
        /// Phase of complex number
        /// </summary>
        public double Phi
        {
            get
            {
                return Math.Atan2(im, re);
            }
        }

        /// <summary>
        /// Addition
        /// </summary>
        /// <param name="e">Element to add</param>
        /// <returns>Sum</returns>
        public Complex Add(Complex e)
        {
            return new Complex(re + e.re, im + e.im);
        }

        /// <summary>
        /// Subtraction
        /// </summary>
        /// <param name="e">Subtrahend</param>
        /// <returns>Sum</returns>
        public Complex Subtract(Complex e)
        {
            return new Complex(re - e.re, im - e.im);
        }

        /// <summary>
        /// Sqrt
        /// </summary>
        public Complex Sqrt
        {
            get
            {
                double rho = Norm;
                rho = Math.Sqrt(rho);
                double phi = Phi;
                phi *= 0.5;
                return new Complex(rho * Math.Cos(phi), rho * Math.Sin(phi));
            }
        }

        /// <summary>
        /// Multiplication
        /// </summary>
        /// <param name="e">Multiplier</param>
        /// <returns>Product</returns>
        public Complex Multiply(Complex e)
        {
            return new Complex(re * e.re - im * e.im, re * e.im + im * e.re);
        }


        /// <summary>
        /// Division
        /// </summary>
        /// <param name="e">Dividend</param>
        /// <returns>Relation</returns>
        public Complex Divide(Complex e)
        {
            double n = e.Norm;
            n *= n;
            return new Complex((re * e.re + im * e.im) / n, (-re * e.im + im * e.re) / n);
        }



        /// <summary>
        /// Conversion from double
        /// </summary>
        /// <param name="x">The double</param>
        /// <returns>Conversion result</returns>
        static public implicit operator Complex(double x)
        {
            return new Complex(x);
        }

  
        /// <summary>
        /// Implicit transformation
        /// </summary>
        /// <param name="i">input</param>
        /// <returns>Transformation result</returns>
        static public implicit operator Complex(int i)
        {
            return new Complex((double)i);
        }


        /// <summary>
        /// Copy
        /// </summary>
        public Complex Copy
        {
            get
            {
                return new Complex(re, im);
            }
        }

        /// <summary>
        /// Addition
        /// </summary>
        /// <param name="a">First element</param>
        /// <param name="b">Second element</param>
        /// <returns>Operation result</returns>
        public static Complex operator +(Complex a, Complex b)
        {
            return new Complex(a.Real + b.Real, a.Imag + b.Imag);
        }

        /// <summary>
        /// Subtraction
        /// </summary>
        /// <param name="a">First element</param>
        /// <param name="b">Second element</param>
        /// <returns>Operation result</returns>
        public static Complex operator -(Complex a, Complex b)
        {
            return new Complex(a.Real - b.Real, a.Imag - b.Imag);
        }

        /// <summary>
        /// Multiplication
        /// </summary>
        /// <param name="a">First element</param>
        /// <param name="b">Second element</param>
        /// <returns>Operation result</returns>
        public static Complex operator *(Complex a, Complex b)
        {
            return a.Multiply(b);
        }

        /// <summary>
        /// Division
        /// </summary>
        /// <param name="a">First element</param>
        /// <param name="b">Second element</param>
        /// <returns>Operation result</returns>
        public static Complex operator /(Complex a, Complex b)
        {
            return a.Divide(b);;
        }


        /// <summary>
        /// Unary minus
        /// </summary>
        /// <param name="a">Argument</param>
        /// <returns>Operation result</returns>
        public static Complex operator -(Complex a)
        {
            return new Complex(-a.Real, -a.Imag);
        }


        /// <summary>
        /// Iteration for finding roots
        /// </summary>
        /// <param name="p">Polynom</param>
        /// <param name="der">Derivation</param>
        /// <param name="h">Auxiliary polynom</param>
        /// <returns>Iteration result</returns>
        internal Complex Iterate(ComplexPolynom p, ComplexPolynom der, ComplexPolynom h)
        {
            Complex he = h[this] as Complex;
            Complex sq = he.Sqrt;
            Complex d = der[this];
            Complex den1 = d + sq as Complex;
            Complex den2 = d - sq as Complex;
            Complex den = (den1.Norm > den2.Norm) ? den1 : den2;
            return p[this] / den as Complex;
        }
    }
}

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