QuickStart Samples

# Piecewise Curves QuickStart Sample (IronPython)

Illustrates working with piecewise constant and piecewise linear curves using classes from the Extreme.Mathematics.Curves namespace in IronPython.

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

import numerics from System import Array # The piecewise curve classes reside in the # Extreme.Mathematics.Curves namespace. from Extreme.Mathematics.Curves import * from Extreme.Mathematics import * # Illustrates the use of the PiecewiseConstantCurve and # PiecewiseLinearCurve classes. # A piecewise curve is a curve that has a different definition # on subintervals of its domain. # # This QuickStart Sample illustrates constant and linear piecewise # curves, which - as the name suggest - are constant or linear # on each interval. # # For an example of cubic splines, see the CubicSplines QuickStart # Sample. # # # Piecewise constants # # All piecewise curves inherit from the PiecewiseCurve class. # Piecewise constant curves are implemented by the # PiecewiseConstantCurve class. It has three constructors. # The first constructor takes two double arrays as parameters. # These contain the x and y values of the data points: xValues = Array[float]([1, 2, 3, 4, 5, 6]) yValues = Array[float]([1, 3, 4, 3, 4, 2]) constant1 = PiecewiseConstantCurve(xValues, yValues) # The second constructor takes two Vector objects, containing the # x and y-values of the data points: xVector = Vector.Create(xValues) yVector = Vector.Create(yValues) constant2 = PiecewiseConstantCurve(xVector, yVector) # The third constructor only takes one parameter: an array of # Point structures that represent the data point. dataPoints = Array[Point]([Point(1, 1), Point(2, 3), Point(3, 4), Point(4, 3), Point(5, 4), Point(6, 2) ]) constant3 = PiecewiseConstantCurve(dataPoints) # # 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. # # Piecewise constant curves have 2n parameters, where n is the number of # data points. The first n parameters are the x-values. The next # n parameters are the y-values. print "constant1.Parameters.Count =", constant1.Parameters.Count # Parameters can easily be retrieved: print "constant1.Parameters[0] =", constant1.Parameters[0] # Parameters can also be set: constant1.Parameters[0] = 1 # # Curve Methods # # The ValueAt method returns the y value of the # curve at the specified x value: print "constant1.ValueAt(2.4) =", constant1.ValueAt(2.4) # The SlopeAt method returns the slope of the curve # a the specified x value: print "constant1.SlopeAt(2.4) =", constant1.SlopeAt(2.4) # The slope at the data points is Double.NaN if the value of the constant # is different on either side of the data point: print "constant1.SlopeAt(2) =", constant1.SlopeAt(2) # Piecewise constant curves do not have a defined derivative. # The GetDerivative method returns a GeneralCurve: derivative = constant1.GetDerivative() print "Type of derivative:", derivative.GetType().ToString() print "derivative(2.4) =", derivative.ValueAt(2.4) # You can get a Line that is the tangent to a curve # at a specified x value using the TangentAt method: tangent = constant1.TangentAt(2.4) print "Slope of tangent line at 2.4 =", tangent.Slope # The integral of a piecewise constant curve can be calculated exactly. print "Integral of constant1 between 1.4 and 4.6 =", constant1.Integral(1.4, 4.6) # # Piecewise linear curves # # Piecewise linear curves are used for linear interpolation # between data points. They are implemented by the # PiecewiseLinearCurve class. It has three constructors, # similar to the constructors for the PiecewiseLinearCurve # class..These constructors create the linear interpolating # curve between the data points. # The first constructor takes two double arrays as parameters. # These contain the x and y values of the data points: xValues2 = Array[float]([ 1, 2, 3, 4, 5, 6 ]) yValues2 = Array[float]([1, 3, 4, 3, 4, 2 ]) line1 = PiecewiseLinearCurve(xValues2, yValues2) # The second constructor takes two Vector objects, containing the # x and y-values of the data points: xVector2 = Vector.Create(xValues2) yVector2 = Vector.Create(yValues2) line2 = PiecewiseLinearCurve(xVector2, yVector2) # The third constructor only takes one parameter: an array of # Point structures that represent the data point. dataPoints2 = Array[Point]([ \ Point(1, 1), Point(2, 3), Point(3, 4), \ Point(4, 3), Point(5, 4), Point(6, 2) ]) line3 = PiecewiseLinearCurve(dataPoints) # # Curve Parameters # # Piecewise linear curves have 2n parameters, where n is the number of # data points. The first n parameters are the x-values. The next # n parameters are the y-values. print "line1.Parameters.Count =", line1.Parameters.Count # Parameters can easily be retrieved: print "line1.Parameters[0] =", line1.Parameters[0] # Parameters can also be set: line1.Parameters[0] = 1 # # Curve Methods # # The ValueAt method returns the y value of the # curve at the specified x value: print "line1.ValueAt(2.4) =", line1.ValueAt(2.4) # The SlopeAt method returns the slope of the curve # a the specified x value: print "line1.SlopeAt(2.4) =", line1.SlopeAt(2.4) # The slope at the data points is Double.NaN if the slope of the line # is different on either side of the data point: print "line1.SlopeAt(2) =", line1.SlopeAt(2) # Piecewise line curves do not have a defined derivative. # The GetDerivative method returns a GeneralCurve: derivative = line1.GetDerivative() print "Type of derivative:", derivative.GetType().ToString() print "derivative(2.4) =", derivative.ValueAt(2.4) # You can get a Line that is the tangent to a curve # at a specified x value using the TangentAt method: tangent = line1.TangentAt(2.4) print "Slope of tangent line at 2.4 =", tangent.Slope # The integral of a piecewise line curve can be calculated exactly. print "Integral of line1 between 1.4 and 4.6 =", line1.Integral(1.4, 4.6)

Copyright Â© 2003-2020, 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.