Extreme Optimization > QuickStart Samples > Advanced Polynomials QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Advanced Polynomials QuickStart Sample (C#)

Illustrates more advanced uses of the Polynomial class, including real and complex root finding, calculating least squares polynomials and polynomial arithmetic in C#.

VB.NET code Back to QuickStart Samples

using System;

namespace Extreme.Mathematics.QuickStart.CSharp
{
    // The DoubleComplex structure resides in the Extreme namespace.
    using Extreme;
    // The Polynomial class resides in the Extreme.Mathematics.Curves namespace.
    using Extreme.Mathematics.Curves;

    /// <summary>
    /// Illustrates the more advanced uses of the Polynomial class 
    /// in the Extreme.Mathematics.Curve namespace.
    /// </summary>
    class AdvancedPolynomials
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // Basic operations on polynomials are covered in the
            // BasicPolynomials QuickStart Sample. This QuickStart
            // Sample focuses on more advanced topics, including
            // finding complex roots, calculating least-squares
            // polynomials, and polynomial arithmetic.

            // Index variable.
            int index;

            //
            // DoubleComplex numbers and polynomials
            //

            Polynomial polynomial = new Polynomial(new double[] {-2, 0, 1, 1});

            // The Polynomial class supports complex numbers
            // as arguments for polynomials. It does not support
            // polynomials with complex coefficients.
            //
            // For more about complex numbers, see the
            // ComplexNumbers QuickStart Sample.
            DoubleComplex z1 = new DoubleComplex(1, 2);

            // Polynomial provides overloads of ValueAt and
            // SlopeAt for complex arguments:
            Console.WriteLine("polynomial.ValueAt({0}) = {1}",
                z1, polynomial.ValueAt(z1));
            Console.WriteLine("polynomial.SlopeAt({0}) = {1}",
                z1, polynomial.SlopeAt(z1));

            //
            // Real and complex roots
            //
            // Our polynomial has only one real root:
            double[] roots = polynomial.FindRoots();
            Console.WriteLine("Number of roots of polynomial1: {0}",
                roots.Length);
            Console.WriteLine("Value of root 1 = {0}", roots[0]);
            // The FindComplexRoots method returns all three
            // roots, two of which are complex:
            DoubleComplex[] complexRoots = polynomial.FindComplexRoots();
            Console.WriteLine("Number of complex roots: {0}",
                complexRoots.Length);
            Console.WriteLine("Value of root 1 = {0}", 
                complexRoots[0]);
            Console.WriteLine("Value of root 2 = {0}", 
                complexRoots[1]);
            Console.WriteLine("Value of root 3 = {0}", 
                complexRoots[2]);

            //
            // Least squares polynomials
            //
            
            // Let's approximate 7 points on the unit circle
            // by a fourth degree polynomial in the least squares
            // sense.
            // First, we create two arrays containing the x and
            // y values of our data points:
            double[] xValues = new double[7];
            double[] yValues = new double[7];
            double angle = 0;
            for(index = 0; index < 7; index++)
            {
                xValues[index] = Math.Cos(angle);
                yValues[index] = -Math.Sin(angle);
                angle = angle + Constants.Pi / 6;
            }
            // Now we can find the least squares polynomial
            // by calling the ststic LeastSquaresFit method.
            // The last parameter is the degree of the desired
            // polynomial.
            Polynomial lsqPolynomial = 
                Polynomial.LeastSquaresFit(xValues, yValues, 4);
            // Note that, as expected, the odd coefficients
            // are close to zero.
            Console.WriteLine("Least squares fit: {0}", 
                lsqPolynomial.ToString());
            
            //
            // Polynomial arithmetic
            //

            // We can add, subtract, multiply and divide
            // polynomials using overloaded operators:
            Polynomial a = new Polynomial(new double[] {4, -2, 2});
            Polynomial b = new Polynomial(new double[] {-3, 1});

            Console.WriteLine("a = {0}", a.ToString());
            Console.WriteLine("b = {0}", b.ToString());
            Polynomial c = a + b;
            Console.WriteLine("a + b = {0}", c.ToString());
            c = a - b;
            Console.WriteLine("a - b = {0}", c.ToString());
            c = a * b;
            Console.WriteLine("a * b = {0}", c.ToString());
            c = a / b;
            Console.WriteLine("a / b = {0}", c.ToString());
            c = a % b;
            Console.WriteLine("a % b = {0}", c.ToString());
            // You can also calculate quotient and remainder
            // at the same time by calling the overloaded Divide
            // method:
            Polynomial d;
            c = Polynomial.Divide(a, b, out d);
            Console.WriteLine("Using Divide method:");
            Console.WriteLine("  a / b = {0}", c.ToString());
            Console.WriteLine("  a % b = {0}", d.ToString());

            Console.Write("Press Enter key to exit...");
            Console.ReadLine();
        }
    }
}
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army