📄 advancedpolynomials.vb
字号:
' The DoubleComplex structure resides in the Extreme namespace.
Imports Extreme
' The Polynomial class resides in the Extreme.Mathematics.Curves namespace.
Imports Extreme.Mathematics.Curves
Namespace Extreme.Mathematics.QuickStart.VB
Module AdvancedPolynomials
' Illustrates the more advanced uses of the Polynomial class
' in the Extreme.Mathematics.Curve namespace of the Extreme Optimization
' Mathematics Library for .NET.
Sub Main()
' 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.
Dim index As Int32
'
' DoubleComplex numbers and polynomials
'
Dim polynomial As 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.
Dim z1 As DoubleComplex = 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:
Dim roots As Double() = 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:
Dim complexRoots As DoubleComplex() = 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:
Dim xValues As Double() = New Double(6) {}
Dim yValues As Double() = New Double(6) {}
Dim angle As Double = 0
For index = 0 To 6
xValues(index) = Math.Cos(angle)
yValues(index) = -Math.Sin(angle)
angle = angle + Extreme.Mathematics.Constants.Pi / 6
Next
' Now we can find the least squares polynomial
' by calling the ststic LeastSquaresFit method.
' The last parameter is the degree of the desired
' polynomial.
Dim lsqPolynomial As Polynomial = _
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:
Dim a As polynomial = New polynomial(New Double() {4, -2, 2})
Dim b As polynomial = New polynomial(New Double() {-3, 1})
Dim c As Polynomial
Console.WriteLine("a = {0}", a.ToString())
Console.WriteLine("b = {0}", b.ToString())
c = polynomial.Add(a, b)
Console.WriteLine("Add(a, b) = {0}", c.ToString())
c = polynomial.Subtract(a, b)
Console.WriteLine("Subtract(a, b) = {0}", c.ToString())
c = polynomial.Multiply(a, b)
Console.WriteLine("Multiply(a, b) = {0}", c.ToString())
c = polynomial.Divide(a, b)
Console.WriteLine("Divide(a, b) = {0}", c.ToString())
c = polynomial.Modulus(a, b)
Console.WriteLine("Remainder(a, b) = {0}", c.ToString())
' You can also calculate quotient and remainder
' at the same time by calling the overloaded Divide
' method:
Dim d As Polynomial
c = polynomial.Divide(a, b, 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()
End Sub
End Module
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -