With this section we provide source code examples which illustrate explicitly how a uni-dimensional function is implemented. In particular, we provide the source code for the implementation of a polynomial.
Here we provide the definition of the object function:f(x) = 3x^2 + 4x + 3which is a linear function.
In order to provide an implementation for this interface you are required to provide an implementation for the following methods:
GetValueAtVector(double[])- Returns the value of the function f, at a given point.
GetDerivativeAt(double[])- Returns the value of the derivative of the function f, at a given point.
using System;
using WebCab.Libraries.Math.Optimization.UniDimensional;
public class PolynomialFunction : Derivative {
// Returns the value of the objective function at a given point.
//
public double GetValueAt(double evaluationPoint) {
return (3*evaluationPoint*evaluationPoint + 2*evaluationPoint +3);
}
// Returns the value of the derivative of the objective function
// evaluated at a given point.
//
public double GetDerivativeAt(double evaluationPoint) {
return (6*evaluationPoint + 4);
}
}
Imports System
Imports WebCab.Libraries.Math.Optimization.UniDimensional
Public Class Polynomial Implements Derivative {
' Returns the value of the objective function at a given point.
'
Public Function GetValueAt(evaluationPoint As Double) As Double Implements Derivative.GetValueAt
Return (3*evaluationPoint*evaluationPoint + 2*evaluationPoint +3)
End Function 'GetValueAt
' Returns the value of the derivative of the objective function
' evaluated at a given point.
'
Public Function GetDerivativeAt(evaluationPoint As Double) As Double Implements Derivative.GetDrivativeAt
Return (6*evaluationPoint + 4)
End Function 'GetDerivativeAt
End Class 'Polynomial
unit PolynomialFunction_Unit
interface
uses WebCab.Libraries.Math.Optimization.UniDimensional;
type
PolynomialFunction = class(Derivative)
public
function GetValueAt(x: System.Double): System.Double;
function GetDerivativeAt(x: System.Double): System.Double;
end;
implementation
(*
* Returns the value of the objective function at a given point.
*)
function PolynomialFunction.GetValueAt(evaluationPoint: System.Double): System.Double;
begin
Result := 3*evaluationPoint*evaluationPoint + 2*evaluationPoint +3;
end;
(*
* Returns the value of the gradient of the objective function
* evaluated at a given point.
*)
function PolynomialFunction.GetDerivativeAt(evaluationPoint: System.Double): System.Double;
begin
Result := 6*evaluationPoint + 4;
end;
end.
For a list of all members of this type, see Derivative Members.
Namespace: WebCab.COM.Math.Optimization.UniDimensional
Assembly: WebCab.COM.Optimization (in WebCab.COM.Optimization.dll)
Derivative Members | WebCab.COM.Math.Optimization.UniDimensional Namespace | UniDimensionalFunction