Monday, December 26, 2011

Imaginary Number Module

What follows is a simple bit of C# code for handling complex numbers. This was developed for a quantum mechanics engine I'm developing.
namespace ImaginaryNumber
{
    /// <summary>
    /// A class for handling complex numbers.
    /// Author: James Yakura/sidhe3141
    /// </summary>
    public class ComplexNumber
    {
        /// <summary>
        /// The real part of the number.
        /// </summary>
        int real_part;
        /// <summary>
        /// The imaginary part of the number.
        /// </summary>
        int imaginary_part;
        /// <summary>
        /// Creates a new complex number.
        /// </summary>
        /// <param name="real">The real part.</param>
        /// <param name="imaginary">The imaginary part.</param>
        public ComplexNumber(int real, int imaginary)
        {
            real_part = real;
            imaginary_part = imaginary;
        }
        /// <summary>
        /// Multiplies by a real number.
        /// </summary>
        /// <param name="mult">The number to multiply by.</param>
        /// <returns>The product of the two numbers.</returns>
        public ComplexNumber MultiplyBy(int mult)
        {
            int temp_real = real_part * mult;
            int temp_img = imaginary_part * mult;
            return new ComplexNumber(temp_real, temp_img);
        }
        /// <summary>
        /// Multiplies by a complex number.
        /// </summary>
        /// <param name="mult">The number to multiply by.</param>
        /// <returns>The product of the two numbers.</returns>
        public ComplexNumber MultiplyBy(ComplexNumber mult)
        {
            int temp_real = real_part * mult.real_part - imaginary_part * mult.imaginary_part;
            int temp_img = real_part * mult.imaginary_part + imaginary_part * mult.real_part;
            return new ComplexNumber(temp_real, temp_img);
        }
        /// <summary>
        /// Adds two complex numbers.
        /// </summary>
        /// <param name="add">The number to add.</param>
        /// <returns>The sum of the two numbers.</returns>
        public ComplexNumber AddTo(ComplexNumber add)
        {
            int temp_real = real_part + add.real_part;
            int temp_img = imaginary_part + add.imaginary_part;
            return new ComplexNumber(temp_real, temp_img);
        }
        /// <summary>
        /// Converts a real number to complex form.
        /// </summary>
        /// <param name="input">The number to be converted.</param>
        /// <returns>The complex form.</returns>
        public static ComplexNumber ComplexConversion(int input)
        {
            return new ComplexNumber(input, 0);
        }
        /// <summary>
        /// Gets and sets the real part of the number.
        /// </summary>
        public int RealPart
        {
            get
            {
                return real_part;
            }
            set
            {
                real_part = value;
            }
        }
        /// <summary>
        /// Gets and sets the coefficient of the imaginary part of the number.
        /// </summary>
        public int ImaginaryPart
        {
            get
            {
                return imaginary_part;
            }
            set
            {
                imaginary_part = value;
            }
        }
    }
}

No comments:

Post a Comment

pleasebecivil
civilitymakesthingseasier
pleesebecivil
civiltymakesthingseasier
pleasebecivilized
civilitymakesthingseesier