Basic Polynomials in IronPython QuickStart Sample
Illustrates the basic use of the Polynomial class in IronPython.
View this sample in: C# Visual Basic F#
```Python import numerics from System import Array # The Polynomial class resides in the Extreme.Mathematics.Curves namespace. from Extreme.Mathematics.Curves import * # Illustrates the basic use of the Polynomial class in the # Extreme.Mathematics.Curve namespace of Numerics.NET. # All curves inherit from the Curve abstract base # class. The Polynomial class overrides implements all # the methods and properties of the Curve class, # and adds a few more. # # Polynomial constructors # # The Polynomial class has multiple constructors. Each # constructor derives from a different way to define # a polynomial or parabola. # 1st option: a polynomial of a specified degree. polynomial1 = Polynomial(3) # Now set the coefficients individually. The coefficients # can be set using the indexer property. The constant term # has index 0: polynomial1[3] = 1 polynomial1[2] = 1 polynomial1[1] = 0 polynomial1[0] = -2 # 2nd option: specify the coefficients in the constructor # as an array of doubles: coefficients = Array[float] ([ -2, 0, 1, 1 ]) polynomial2 = Polynomial(coefficients) # In addition, you can create a polynomial that # has certain roots using the static FromRoots # method: roots = Array[float] ([ 1, 2, 3, 4 ]) 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. xValues = Array[float] ([ 1, 2, 3, 4 ]) yValues = Array[float] ([ 1, 4, 10, 8 ]) polynomial4 = Polynomial.GetInterpolatingPolynomial(xValues, yValues) # The ToString method gives a common string # representation of the polynomial: print "polynomial3 =", 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: print "polynomial1.Parameters.Count =", polynomial1.Parameters.Count # Parameters can easily be retrieved: print "polynomial1 parameters:" for p in polynomial1.Parameters: print p, print # We can see that polynomial2 defines the same polynomial # curve as polynomial1: print "polynomial2 parameters:" for p in polynomial2.Parameters: print p, print # Parameters can also be set: polynomial2.Parameters[0] = 1 # 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[0] = 1 # The degree of the polynomial is returned by # the Degree property: print "Degree of polynomial3 =", polynomial3.Degree # # Curve Methods # # The ValueAt method returns the y value of the # curve at the specified x value: print "polynomial1.ValueAt(2) =", polynomial1.ValueAt(2) # The SlopeAt method returns the slope of the curve # a the specified x value: print "polynomial1.SlopeAt(2) =", polynomial1.SlopeAt(2) # You can also create a new curve that is the # derivative of the original: derivative = polynomial1.GetDerivative() print "Slope at 2 (derivative) =", derivative.ValueAt(2) # For a polynomial, the derivative is a Quadratic curve # if the degree is equal to three: print "Type of derivative:", derivative.GetType().FullName print "Derivative parameters: " for p in derivative.Parameters: print p, print # If the degree is 4 or higher, the derivative is # once again a polynomial: print "Type of derivative for polynomial3:", 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: tangent = polynomial1.TangentAt(2) print "Tangent line at 2:" print " Y-intercept =", tangent.Parameters[0] print " Slope =", tangent.Parameters[1] # For many curves, you can evaluate a definite # integral exactly: print "Integral of polynomial1 between 0 and 1 =", polynomial1.Integral(0, 1) # 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() print "Number of roots of polynomial1:", roots.Length print "Value of root 1 =", roots[0] # Let's find polynomial3's roots again: roots = polynomial3.FindRoots() print "Number of roots of polynomial3:", roots.Length print "Value of root =", roots[0] print "Value of root =", roots[1] # Root finding isn't an exact science. Note the # round-off error in these values: print "Value of root =", roots[2] print "Value of root =", roots[3] # For more advanced uses of the Polynomial class, # see the AdvancedPolynomials QuickStart sample. ```