Extreme Optimization > QuickStart Samples > Current Page

Extreme Optimization QuickStart Samples

Quadratic Curve QuickStart Sample (C#)

Quadratic Curve QuickStart Sample (C#)

Illustrates the use of the Quadratic class (Extreme.Mathematics.Curves namespace) in C#.

VB.NET code Back to QuickStart Samples

using System;

namespace Extreme.Mathematics.QuickStart.CSharp
{
    // The Point structure and Quadratic class reside in the 
    // Extreme.Mathematics.Curves namespace.
    using Extreme.Mathematics.Curves;

    /// <summary>
    /// Illustrates the use of the Point structure and the Quadratic
    /// class in the Extreme.Mathematics.Curve namespace of the Extreme
    /// Optimization Mathematics Library for .NET.
    /// </summary>
    class QuadraticCurve
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // All curves inherit from the Curve abstract base
            // class. The Quadratic class overrides implements all
            // the methods and properties of the Curve class,
            // and adds a few more.
            //
            // In a few places, we will need the Point structure.
            // It simply containts an X and a Y component, much
            // like the PointF structure in the System.Drawing
            // namespace.
            //
            // Let's create some points:
            Point point1 = new Point(-1, -4);
            Point point2 = new Point(3, 12);
            Point point3 = new Point(0, -3);

            //
            // Quadratic constructors
            //

            // The Quadratic class has multiple constructors. Each
            // constructor derives from a different way to define
            // a quadratic curve or parabola.

            // 1st option: a quadratic curve through 3 points.
            Quadratic quadratic1 = new Quadratic(point1, point2, point3);
            // 2nd option: same as above, but we use the x and 
            // y coordinates of the points directly.
            Quadratic quadratic2 = new Quadratic(point1.X, point1.Y, 
                point2.X, point2.Y, point3.X, point3.Y);
            // 3rd option: give the three coefficients, a, b, and c
            // of thequadratic form ax^2+bx+c.
            Quadratic quadratic3 = new Quadratic(1, 2, 1);

            //
            // Curve Parameters
            //

            // The shape of any curve is determined by a set of parameters.
            // These parameters can be retrieved and set through the
            // Parameters collection. The number of parameters for a curve
            // is given by this collection's Count property.
            //
            // Quadratic curves have three parameters.
            Console.WriteLine("quadratic1.Parameters.Count = {0}", 
                quadratic1.Parameters.Count);
            // Parameters can easily be retrieved:
            Console.WriteLine("quadratic1 parameters; {0}, {1}, {2}", 
                quadratic1.Parameters[0], quadratic1.Parameters[1],
                quadratic1.Parameters[2]);
            // We can see that quadratic2 defines the same quadratic 
            // curve as quadratic1, but quadratic3 is different:
            Console.WriteLine("quadratic2 parameters; {0}, {1}, {2}", 
                quadratic2.Parameters[0], quadratic2.Parameters[1],
                quadratic2.Parameters[2]);
            Console.WriteLine("quadratic3 parameters; {0}, {1}, {2}", 
                quadratic3.Parameters[0], quadratic3.Parameters[1],
                quadratic3.Parameters[2]);
            // Parameters can also be set:
            quadratic3.Parameters[0] = 1;

            //
            // Curve Methods
            //

            // The ValueAt method returns the y value of the
            // curve at the specified x value:
            Console.WriteLine("quadratic1.ValueAt(2) = {0}", quadratic1.ValueAt(2));

            // The SlopeAt method returns the slope of the curve
            // a the specified x value:
            Console.WriteLine("quadratic1.SlopeAt(2) = {0}", quadratic1.SlopeAt(2));

            // You can also create a new curve that is the 
            // derivative of the original:
            Curve derivative = quadratic1.GetDerivative();
            Console.WriteLine("Slope at 2 (derivative) = {0}", derivative.ValueAt(2));
            // For a quadratic curve, the derivative is a Line:
            Console.WriteLine("Type of derivative: {0}",
                derivative.GetType().ToString());
            Console.WriteLine("Derivative parameters:");
            Console.WriteLine("  Y-intercept = {0}", derivative.Parameters[0]);
            Console.WriteLine("  Slope = {0}", derivative.Parameters[1]);

            // You can get a Line that is the tangent to a curve
            // at a specified x value using the TangentAt method:
            Line tangent = quadratic1.TangentAt(2);
            Console.WriteLine("Tangent line at 2:");
            Console.WriteLine("  Y-intercept = {0}", tangent.Parameters[0]);
            Console.WriteLine("  Slope = {0}", tangent.Parameters[1]);

            // For many curves, you can evaluate a definite
            // integral exactly:
            Console.WriteLine("Integral of quadratic1 between 0 and 1 = {0}",
                quadratic1.Integral(0, 1));

            // You can find the zeroes or roots of the curve
            // by calling the FindRoots method:
            double[] roots = quadratic1.FindRoots();
            Console.WriteLine("Number of roots of quadratic1: {0}",
                roots.Length);
            Console.WriteLine("Value of root 1 = {0}", roots[0]);
            Console.WriteLine("Value of root 2 = {0}", roots[1]);
            // quadratic3 has one double root at x = -1:
            roots = quadratic3.FindRoots();
            Console.WriteLine("Number of roots of quadratic3: {0}",
                roots.Length);
            Console.WriteLine("Value of root = {0}", roots[0]);

            // Let's change quadratic3 so it has no real roots:
            quadratic3.Parameters[0] = 2;
            roots = quadratic3.FindRoots();
            Console.WriteLine("Number of roots of new quadratic3: {0}",
                roots.Length);

            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