Friday, February 26, 2010

.NET 4.0 : Complex Numbers

 

    Hello today I’m going to start to describe and apply the new classes which were added into .NET 4.0 Framework.

 

When I was 16, it always was a problem there. Sex was in the air, a lot of different opportunities had the habit to rise without any prevention at all, and I was loving challenges. BUT, it also was the period when the most useless and useful information was taken in and out from the classes. Physics, mathematics, chemistry and others were at the same time boring and challenging.

One of the basic subjects in math was : Quadratic equations, and also introduction of complex numbers, which was pretty strange… imaginary and real parts formed the complex number. But these numbers has a lot applications in physics, astronomy and geometry.

Question how did we solved these kind of equations in the past?
We did a structure or a class and tried different things with it, like multiplication and division, and others as well. But sometimes we didn’t really figured out very well the logic.

Now, guess what, in .NET 4.0 we already got a Complex structure, which in fact represents the complex number.

So here is a very simple application of it:

using System;
using System.Numerics;

namespace Intro.ComplexNumbers
{
    class Program
    {

        static void Main(string[] args)
        {
            // do it always, we'll stop the loop by pressing Ctrl + C.
            while (true)
            {
                string errorMsg = string.Empty;

                Console.WriteLine("To solve the quadratic equations using the Quadratic Formula,");

                int a = ReadCoeficientValueFromConsole("a", "Please enter ax^2: ", ref errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    ShowErrorMessage(errorMsg);
                    break;    
                }

                int b = ReadCoeficientValueFromConsole("b", "Please enter bx: ", ref errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    ShowErrorMessage(errorMsg);
                    break;
                }

                int c = ReadCoeficientValueFromConsole("c", "Please enter c: ", ref errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    ShowErrorMessage(errorMsg);
                    break;
                }

                // we're assuming that the discriminant is negative
                // for more information visit http://en.wikipedia.org/wiki/Quadratic_Formula#Quadratic_formula
                Complex root1 = new Complex(-b / 2 * a, Math.Sqrt(4 * a * c - b * b) / 2 * a);
                Complex root2 = new Complex(-b / 2 * a, (-1) * Math.Sqrt(4 * a * c - b * b) / 2 * a);

                Console.WriteLine("Roots are: ");
                Console.WriteLine("Root1 (positive): {0}", root1);
                Console.WriteLine("Root2 (negative): {0}", root2);
            }
        }

        /// 
        /// Shows error message in console.
        /// 
        /// Error message.
        private static void ShowErrorMessage(string errorMsg)
        {
            Console.WriteLine(errorMsg);
            Console.ReadKey();
        }

        /// 
        /// Reads a string from console and tries to convert it into an Int32, and assign it 
        /// to a coefficient.
        /// 
        /// Coefficient name.
        /// String which will be shown to ask the coefficient introduction.
        /// The coefficient Int32 value.
        private static int ReadCoeficientValueFromConsole(string coefficientName, string requieringString, ref string errorMsg)
        {
            int coeficient = 0;

            Console.WriteLine(requieringString);

            string aValue = Console.ReadLine();
            if (!string.IsNullOrEmpty(aValue))
            {
                try
                {
                    coeficient = Convert.ToInt32(aValue);
                }
                catch (FormatException)
                {
                    errorMsg = "Your '" + coefficientName + "' coeficient is not a numeric value.";
                }
                catch (Exception)
                {
                    errorMsg = "There was an exception raised because of coeficient '" + coefficientName + "'";
                }
            }

            return coeficient;
        }
    }
}

Here are some screenshots:

Figure 1. Coefficient error

Figure 2. Good coefficients solving 3x2 + 9x +7 = 0

Download source from here

NOTE: I’ve done this project using Visual Studio 2010 RC.

No comments:

Post a Comment