QuickStart Samples

# Advanced Polynomials QuickStart Sample (IronPython)

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

C# code Visual Basic code F# code Back to QuickStart Samples

import numerics from math import * from System import Array # The DoubleComplex structure resides in the Extreme.Mathematics namespace. from Extreme.Mathematics import * # The Polynomial class resides in the Extreme.Mathematics.Curves namespace. from Extreme.Mathematics.Curves import * #/ Illustrates the more advanced uses of the Polynomial class #/ in the Extreme.Mathematics.Curve namespace of the Extreme Optimization #/ Mathematics Library for .NET. # 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. # # Complex numbers and polynomials # polynomial = Polynomial(Array[float]([ -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. z1 = DoubleComplex(1, 2) # Polynomial provides variants of ValueAt and # SlopeAt for complex arguments: print "polynomial.ComplexValueAt({0}) = {1}", z1, polynomial.ComplexValueAt(z1) print "polynomial.ComplexSlopeAt({0}) = {1}", z1, polynomial.ComplexSlopeAt(z1) # # Real and complex roots # # Our polynomial has only one real root: roots = polynomial.FindRoots() print "Number of roots of polynomial1:", roots.Length print "Value of root 1 =", roots[0] # The FindComplexRoots method returns all three # roots, two of which are complex: complexRoots = polynomial.FindComplexRoots() print "Number of complex roots:", complexRoots.Length print "Value of root 1 =", complexRoots[0] print "Value of root 2 =", complexRoots[1] print "Value of root 3 =", 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: xValues = Array[float]([ cos(i * Constants.Pi / 6) for i in range(7) ]) yValues = Array[float]([ -sin(i * Constants.Pi / 6) for i in range(7) ]) # Now we can find the least squares polynomial # by calling the ststic LeastSquaresFit method. # The last parameter is the degree of the desired # polynomial. lsqPolynomial = Polynomial.LeastSquaresFit(xValues, yValues, 4) # Note that, as expected, the odd coefficients # are close to zero. print "Least squares fit:", lsqPolynomial.ToString() # # Polynomial arithmetic # # We can add, subtract, multiply and divide # polynomials using overloaded operators: a = Polynomial(Array[float]([ 4, -2, 4 ])) b = Polynomial(Array[float]([ -3, 1 ])) print "a =", a.ToString() print "b =", b.ToString() c = a + b print "a + b =", c.ToString() c = a - b print "a - b =", c.ToString() c = a * b print "a * b =", c.ToString() c = a / b print "a / b =", c.ToString() c = a % b print "a % b =", c.ToString()

Copyright Â© 2003-2021, 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*, *Visual Studio.NET*, and the *Optimized for Visual Studio* logo

are registered trademarks of Microsoft Corporation.