Extreme Optimization >
QuickStart Samples >
Basic Polynomials QuickStart Sample (F#)
Extreme Optimization QuickStart Samples
Basic Polynomials QuickStart Sample (F#)
Illustrates the basic use of the Polynomial class (Extreme.Mathematics.Curves
namespace) in F#.
C# code VB.NET code Back to
QuickStart Samples
#light
#r "Extreme.Numerics.dll"
open System
// The Polynomial class resides in the Extreme.Mathematics.Curves namespace.
open Extreme.Mathematics.Curves
// 1st option: a polynomial of a specified degree.
let polynomial1 = new Polynomial(3)
// Now set the coefficients individually. The coefficients
// can be set using the indexer property. The constant term
// has index 0:
polynomial1.Coefficient(3) <- 1.0
polynomial1.Coefficient(2) <- 1.0
polynomial1.Coefficient(1) <- 0.0
polynomial1.Coefficient(0) <- -2.0
// 2nd option: specify the coefficients in the constructor
// as an array of doubles:
let coefficients = [| -2.0; 0.0; 1.0; 1.0 |]
let polynomial2 = new Polynomial(coefficients)
// In addition; you can create a polynomial that
// has certain roots using the static FromRoots
// method:
let roots = [| 1.0; 2.0; 3.0; 4.0 |]
let polynomial3 = Polynomial.FromRoots(roots)
// Or you can construct the interpolating polynomial
// by calling the static GetInterpolatingPolynomial
// method. The parameters are two double arrays
// containing the x values and y values respectively.
let xValues = [| 1.0; 2.0; 3.0; 4.0 |]
let yValues = [| 1.0; 4.0; 10.0; 8.0 |]
let polynomial4 = Polynomial.GetInterpolatingPolynomial(xValues, yValues)
// The ToString method gives a common string
// representation of the polynomial:
Console.WriteLine("polynomial3 = {0}", polynomial3.ToString())
//
// 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.
//
// For polynomials, the parameters are the coefficients
// of the polynomial. The constant term has index 0:
Console.WriteLine("polynomial1.Parameters.Count = {0}", polynomial1.Parameters.Count)
// Parameters can easily be retrieved:
Console.Write("polynomial1 parameters:")
for index = 0 to polynomial1.Parameters.Count -1 do
Console.Write("{0} ", polynomial1.Parameters.[index])
done
Console.WriteLine()
// We can see that polynomial2 defines the same polynomial
// curve as polynomial1:
Console.Write("polynomial2 parameters:")
for index = 0 to polynomial2.Parameters.Count -1 do
Console.Write("{0} ", polynomial2.Parameters.[index])
done
Console.WriteLine()
// Parameters can also be set:
polynomial2.Parameters.[0] <- 1.0
// For polynomials and other classes that inherit from
// the LinearCombination class, the parameters are also
// available through the indexer property of Polynomial.
// The following is equivalent to the line above:
polynomial2.Coefficient(0) <- 1.0
Console.WriteLine(polynomial2.Coefficient(2))
// The degree of the polynomial is returned by
// the Degree property:
Console.WriteLine("Degree of polynomial3 = {0}", polynomial3.Degree)
//
// Curve Methods
//
// The ValueAt method returns the y value of the
// curve at the specified x value:
Console.WriteLine("polynomial1.ValueAt(2) = {0}", polynomial1.ValueAt(2.0))
// The SlopeAt method returns the slope of the curve
// a the specified x value:
Console.WriteLine("polynomial1.SlopeAt(2) = {0}", polynomial1.SlopeAt(2.0))
// You can also create a new curve that is the
// derivative of the original:
let derivative = polynomial1.GetDerivative()
Console.WriteLine("Slope at 2 (derivative) = {0}", derivative.ValueAt(2.0))
// For a polynomial, the derivative is a Quadratic curve
// if the degree is equal to three:
Console.WriteLine("Type of derivative: {0}", derivative.GetType().FullName)
Console.Write("Derivative parameters: ")
for index = 0 to derivative.Parameters.Count -1 do
Console.Write("{0} ", derivative.Parameters.[index])
done
Console.WriteLine()
// If the degree is 4 or higher, the derivative is
// once again a polynomial:
Console.WriteLine("Type of derivative for polynomial3: {0}", polynomial3.GetDerivative().GetType().FullName)
// You can get a Line that is the tangent to a curve
// at a specified x value using the TangentAt method:
let tangent = polynomial1.TangentAt(2.0)
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 polynomial1 between 0 and 1 = {0}", polynomial1.Integral(0.0, 1.0))
// You can find the zeroes or roots of the curve
// by calling the FindRoots method. Note that this
// method only returns the real roots.
roots = polynomial1.FindRoots()
Console.WriteLine("Number of roots of polynomial1: {0}", roots.Length)
Console.WriteLine("Value of root 1 = {0}", roots.[0])
// Let's find polynomial3's roots again:
roots = polynomial3.FindRoots()
Console.WriteLine("Number of roots of polynomial3: {0}", roots.Length)
Console.WriteLine("Value of root = {0}", roots.[0])
Console.WriteLine("Value of root = {0}", roots.[1])
// Root finding isn't an exact science. Note the
// round-off error in these values:
Console.WriteLine("Value of root = {0}", roots.[2])
Console.WriteLine("Value of root = {0}", roots.[3])
// For more advanced uses of the Polynomial class,
// see the AdvancedPolynomials QuickStart sample.
Console.Write("Press Enter key to exit...")
Console.ReadLine()
Copyright 2004-2007,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, and Visual
Studio.NET are registered trademarks of Microsoft Corporation