using System; using CenterSpace.NMath.Core; namespace CenterSpace.NMath.Examples.CSharp { /// <summary> /// A .NET example in C# showing how to create and manipulate polynomial objects. /// </summary> class PolynomialExample { static void Main( string[] args ) { Console.WriteLine(); // Class Polynomial represents a polynomial by its coefficients, arranged in // ascending order-that is, a vector of coefficients a0, a1, ... an such that // f(x) = a0*x^0 + a1*X^1 + ... + an*x^n. // A Polynomial instance can be constructed in two ways. If you know the // exact form of the polynomial, simply pass in a vector of coefficients. var coef = new DoubleVector( "3 1 -2 0 5" ); var f = new Polynomial( coef ); // f(x) = 5x^4 - 2x^2 + x + 3 Console.WriteLine( "f(x) = {0}", f.ToString() ); Console.WriteLine( "Degree = {0}", f.Degree ); Console.WriteLine( "Coefficients = {0}\n", f.Coeff ); // You can also interpolate a polynomial through a set of points. If the // number of points is n, then the constructed polynomial will have degree // n - 1 and pass through the interpolation points. For example, this code // interpolates a polynomial through the points (1,6), (2,11), and (3,20): var x = new DoubleVector( "1 2 3" ); var y = new DoubleVector( "6 11 20" ); var g = new Polynomial( x, y ); // g(x) = 2x^2 - x + 5 Console.WriteLine( "g(x) = {0}", g.ToString( "G3" ) ); Console.WriteLine( "Degree = {0}", g.Degree ); Console.WriteLine( "Coefficients = {0}\n", g.Coeff ); // The Evaluate() method evaluates a polynomial at a given x-value, or // vector of x-values. This code evaluates f at ten points between 0 and 1: x = new DoubleVector( 10, 0.1, 1.0 / 10 ); y = f.Evaluate( x ); Console.WriteLine( "x = {0}", x ); Console.WriteLine( "y = {0}\n", y ); // Class Polynomial provides overloads of the arithmetic operators (and // equivalent named methods) that work with either with two polynomials, or // with a polynomial and a scalar. For example: Polynomial h = ( f + g ) * g / 2; Console.WriteLine( "h(x) = {0}", h.ToString() ); Console.WriteLine( "h(2) = {0}", h.Evaluate( 2 ) ); Console.WriteLine(); // The Integrate() method computes the integral of a polynomial over a given // interval. Console.WriteLine( "Integral of h(x) over 0 to 1 = {0}", h.Integrate( 0, 1 ).ToString( "G3" ) ); Console.WriteLine(); // The AntiDerivative() method returns a new polynomial encapsulating // the anti-derivative (indefinite integral) of the current polynomial. // The constant of integration is assumed to be zero. Polynomial hAntiDeriv = h.AntiDerivative(); Console.WriteLine( "Antiderivative of h(x)..." ); Console.WriteLine( hAntiDeriv.ToString( "G3" ) ); Console.WriteLine(); // The Differentiate() method computes the derivative of a polynomial at a // given x-value. Console.WriteLine( "Derivative of h(x) at 1 = {0}", h.Differentiate( 1 ).ToString( "G3" ) ); Console.WriteLine(); // Derivative() returns a new polynomial that is the first derivative of // the current polynomial. Console.WriteLine( "First derivative of h(x)..." ); Console.WriteLine( h.Derivative().ToString( "G3" ) ); Console.WriteLine(); Console.WriteLine( "Second derivative of h(x)..." ); Console.WriteLine( h.Derivative().Derivative().ToString( "G3" ) ); Console.WriteLine(); // Check that the derivative of the anti-derivative of h(x) == h(x) Console.WriteLine( "Derivative of the antiderivative of h(x)..." ); Console.WriteLine( h.AntiDerivative().Derivative().ToString( "G3" ) ); Console.WriteLine(); Console.WriteLine( "Press Enter Key" ); Console.Read(); } }// class }// namespace← All NMath Code Examples