Mathematical expression library was developed in C++ programming language. Though C++ is not that common in scientific computing as, for instance, Python and Fortran we believe it is still important to support it because a significant fraction of researchers and practitioners use this language in their work. C++ is significantly faster than Python and it supports templates and other advanced object-oriented capabilities. Such capabilities are crucial for processing polymorphic expressions and building extensible tools for computing function values and bounds. Another reason why we use C++ is a possibility of integration with existing code and libraries in our department.

The library of mathematical expressions is represented by the template class Expr <T>, where C++ standard real types double and float or the type Interval<> can be used as the template parameter *T*. This enables describing mathematical expressions based on real numbers or intervals.

The Expr<T> class has two constructors. The first constructor has one input parameter of type double. It allows to create a constant expression based on the value of this parameter. Also, this constructor is used to automatically convert real or integer numbers to Expr<T> type. It is worth noting that the automatic conversion of a number into an expression occurs if the number is to the right of an expression of type Expr<T>. If the number is on the left, then the C++ overload operator is used. An example of such an operator (binary “-“) is given in the Figure 5.

Figure 5 The operator of overloading the operation of subtraction in the class Expr<T>.

The second constructor of Expr<T> with no parameters can describe a vector of variables that is used to implement a function. A vector of variables with a single name (for example, *x*) whose size corresponds to the dimension of the space. In this case, an index is used to refer to a specific element of this vector. The index can be represented as an integer starting with 0, type Iterator, or an arbitrary expression of type Expr<T>. An access to individual parameters is provided by an overloaded square brackets operator Figure 6. For example, the vector variable x was created to implement the Rosenbrock function Figure 1. Next, we refer to the elements of this vector using the iterator *i* or the calculated expression *t + 1*. The size of the vector x is determined by the input parameter *n* of Rosenbrock function. The vector variable x is also created in DropWave function Figure 3, but the elements of this vector are accessed using integers 0 or 1 since the dimension of the space equals to 2.

Figure 6 The overloaded square brackets operators in the class Expr<T>.

Standard mathematical operations such as addition, subtraction, multiplication etc. are implemented using C++ standard operators overloading techniques. To describe various mathematical expressions, the library includes elementary mathematical functions. The library implements trigonometric functions sin, cos, tg, ctg, as well as inverse trigonometric functions acos, asin, atg, actg. In addition, the exponential function exp, the logarithmic functions ln and log, the function for computing the power pow, the absolute value abs, the minimum function min and the maximum function max are supported. The IfThen ternary operation is implemented for the organization of conditional logic in mathematical expressions. The functions LoopSum and LoopMul implement summation and multiplication respectively. To print a mathematical expression of type Expr<T>, an overloaded operator << is used.

The calc method Figure 7 was implemented in the Expr<T> class for calculating the value of a function at a given point. The parameter of the method is an algorithm – an object of the type Algorithm<T> that determines how to calculate the value of the function. The constructor of the algorithm is given a point of a box where the value of the function is calculated. This parameter has the type of vector std::vector<T>.

Figure 7 The calc method is required to calculate the value of a mathematical expression.

The idea of an algorithm in our library follows the concept of the design pattern strategy [9], where the same data (in our case, a mathematical expression) can be processed by different algorithms. Currently, four types of algorithms are implemented: FuncAlg<T> for calculating the value of the function, InterEvalAlg<T> for calculating the interval estimates of the function, ValDerAlg<T> for calculating the gradient of a function and IntervalDerAlg<T> for calculation of an interval estimation of the gradient of a function. All these algorithms are inherited from the base class Algorithm<T>. Figure 8 shows an example of calculating the value of a function at a given point. The calc method returns a scalar value of a function of type *T* for a given point.

Figure 8 Calling the calc method to get the value of the DropWave function at the given point (0.5, 1.5).

The library of mathematical expressions allows to calculate the interval estimation of a function on a given box. The calc method described above should be used for this purpose with InterEvalAlg<T> algorithm as a parameter Figure 9. Note that as the template parameter *T* in std::vector<T>, the type Interval<double> is passed because the vector stores the bounds of the box, and not the specified point as described in the first case. Type InterEvalAlg<T> takes double as a parameter of the template *T*, and then, by inheritance, the type InterEvalAlg<T> is converted to Algorithm<Interval<T>>. The calc method returns an interval estimate of a function of type Interval<T> (Section IV) over a given box.

Figure 9 Calculation of the interval estimate of DropWave function over the box [0.5, 1.5]x [-0.5, 0.5].

To calculate the gradient of a function, first it’s necessary to create a mathematical expression by passing the type ValDer<T> as a template parameter Figure 10. The ValDer<T> type describes the function gradient and its value. Next, the ValDerAlg<T> algorithm is passed to the calc method. The point std::vector<T> in which the function gradient is calculated is passed to the constructor of this algorithm. As a result, method calc returns an object of type ValDer<T>.

Figure 10 Calculate the gradient of the DropWave function at the point (0.5, 1.5).

The calculation of the interval estimate of the function gradient is shown in Figure 11. First, it’s necessary to create the mathematical expression by passing the type IntervalDer<T> as a template parameter. This type describes the interval estimate of the function gradient and the interval estimation of the function itself. Then the calc method must be passed the algorithm IntervalDerAlg<T>. The constructor of this algorithm takes a box on which the interval estimation of the function gradient is calculated. A box is described using the type std::vector<Interval<T>>. As a result, the calc method returns an object of type IntervalDer<T>, characterizing the interval enclosure of the gradient.

Figure 11 Calculations of the interval estimation of the gradient of the DropWave function on a box with boundaries [0.5, 1.5] and [-0.5, 0.5].

## Comments (0)

General note:By using the comment function on degruyter.com you agree to our Privacy Statement. A respectful treatment of one another is important to us. Therefore we would like to draw your attention to our House Rules.