The simplest polynomial implementation : Polynomial « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » PolynomialScreenshots 
The simplest polynomial implementation


/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

//Compile:
//csc /debug- /o+ /out:polynomial.exe /r:system.dll  Counter.cs Driver.cs PolySimple.cs polynomial.cs ipoly.cs


//File:PolySimple.cs

namespace Polynomial
{
    using System;
    /// <summary>
    /// The simplest polynomial implementation
    /// </summary>
    /// <description>
    /// This implementation loops through the coefficients and evaluates each
    /// term of the polynomial.
    /// </description>
    class PolySimple: Polynomial
    {
        public PolySimple(params double[] coefficients): base(coefficients)
        {
        }

        public override double Evaluate(double value)
        {
            double retval = coefficients[0];
    
            double f = value;

            for (int i = 1; i < coefficients.Length; i++)
            {
                retval += coefficients[i* f;
                f *= value;
            }
            return(retval);
        }
    }
}

//File:Polynomial.cs
namespace Polynomial
{
    using System;
    using PolyInterface;

        /// <summary>
        /// The abstract class all implementations inherit from
        /// </summary>
    public abstract class Polynomial
    {
        public Polynomial(params double[] coefficients)
        {
            this.coefficients = new double[coefficients.Length];

            for (int i = 0; i < coefficients.Length; i++)
                this.coefficients[i= coefficients[i];
        }

        public abstract double Evaluate(double value);

        protected double[] coefficients = null;
    }
}
//File:IPoly.cs
namespace PolyInterface
{
        /// <summary>
        /// The interface that implementations will implement
        /// </summary>
    public interface IPolynomial
    {
        double Eval(double value);
    }
}

//File:Driver.cs
namespace Polynomial
{
    using System;
    using System.Diagnostics;

    /// <summary>
    /// Driver class for the project
    /// </summary>
    public class Driver
    {
        /// <summary>
        /// Times the evaluation of a polynomial
        /// </summary>
        /// <param name="p">The polynomial to evaluate</param>
        public static double TimeEvaluate(Polynomial p)
        {
            double value = 2.0;

            Console.WriteLine("{0}", p.GetType().Name);

                // Time the first iteration. This one is done
                // separately so that we can figure out the startup
                // overhead separately...
            long start = Counter.Value;

            p.Evaluate(0.0);    // do the first iteration.
            long delta = Counter.Value - start;
            Console.WriteLine("Overhead = {0:f2} seconds"(doubledelta/Counter.Frequency);
            Console.WriteLine("Eval({0}) = {1}", value, p.Evaluate(value));

            int limit = 100000;
            start = Counter.Value;
    
                // Evaluate the polynomial the required number of
                // times.
            double result = 0;
            for (int i = 0; i < limit; i++)
            {
                result += p.Evaluate(value);
            }
            delta = Counter.Value - start;
            
            double ips = (doublelimit * ((double)Counter.Frequency / (doubledelta);
            Console.WriteLine("Evalutions/Second = {0:f0}", ips);
            Console.WriteLine();

            return(ips);
        }
        /// <summary>
        /// Run all implementations for a given set of coefficients
        /// </summary>
        /// <param name="coeff"> </param>
        public static void Eval(double[] coeff)
        {
            Polynomial[] imps = new Polynomial []
                {
                    new PolySimple(coeff),
            };

            double[] results = new double[imps.Length];
            for (int index = 0; index < imps.Length; index++)
            {
                results[index= TimeEvaluate(imps[index]);
            }

            Console.WriteLine("Results for length = {0}", coeff.Length);
            for (int index = 0; index < imps.Length; index++)
            {
                Console.WriteLine("{0} = {1:f0}", imps[index], results[index]);
            }
            Console.WriteLine();
        }

        /// <summary>
        /// Maim function.
        /// </summary>
        public static void Main()
        {
            Eval(new Double[] {5.5});

                // Evaluate the first polynomial, with 7 elements
            double[] coeff = 
                new double[] {5.57.015305001001};
            
            Eval(coeff);
            
                // Evaluate the second polynomial, with 50 elements
            coeff = new double[50];
            for (int index = 0; index < 50; index++)
            {
                coeff[index= index;
            }
            Eval(coeff);
        }
    }
}

//File:Counter.cs
using System;

namespace Polynomial
{
    class Counter 
    {
        public static long Frequency 
        {
            get 
            {
                long freq = 0;
                QueryPerformanceFrequency(ref freq);
                return freq;
            }
        }
        public static long Value 
        {
            get 
            {
                long count = 0;
                QueryPerformanceCounter(ref count);
                return count;
            }
        }

        [System.Runtime.InteropServices.DllImport("KERNEL32")]
        private static extern bool QueryPerformanceCounter(  ref long lpPerformanceCount);

        [System.Runtime.InteropServices.DllImport("KERNEL32")]
        private static extern bool QueryPerformanceFrequencyref long lpFrequency);                     
    }
}




           
       
Polynomial.zip( 2 k)
Related examples in the same category
1.Polynomial all
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.