# Function Set in OPEN CASCADE

### Function Set in OPEN CASCADE

eryar@163.com

Abstract. The common math algorithms library provides a C++ implementation of the most frequently used mathematical algorithms. These include: algorithms to solve a set of linear algebraic equations, algorithms to find the minimum of a function of one or more independent variables, algorithms to find roots of one, or of a set of non-linear equations, algorithm to find the eigenvalues and eigenvectors of a square matrix. The solver for function set is used widely in extrema value evaluation, point project on to curve and surface, also used to solve the point inverse for geometry curve and surface. The paper focus on the function set concept and its solver in OPEN CASCADE.

Key Words. Function Set, Non-Linear Equation Solver, Equations Root,

1. Introduction

Figure 1.1 math_FunctionSet class diagram in OPENCASCADE

2. Function Set

v NbVariables()：方程组中变量的个数；

v NbEquations()：方程组中方程的个数；

v Value()：计算指定变量的方程组的值；

v Derivatives()：计算指定变量的方程组的微分值；

v Values()：计算指定变量的方程组的值和微分值；

```/*
*
*           File : main.cpp
*         Author : Shing Liu(eryar@163.com)
*           Date : 2016-01-12 21:00
*
*    Description : test function set.
*/
#define WNT
#include <math_FunctionSetRoot.hxx>
#include <math_FunctionSetWithDerivatives.hxx>

#pragma comment(lib, \"TKernel.lib\")
#pragma comment(lib, \"TKMath.lib\")

/**
* @brief test function for a circle and a curve:
*
*  F1(x1,x2) = (x1)^2 + (x2)^2 -4
*  F2(x1,x2) = e^(x1) + x2 - 1
*
* The derivatives of the function set are:
*  Dx1f1(x1,x2) = 2.0 * x1
*  Dx2f1(x1,x2) = 2.0 * x2
*  Dx1f2(x1,x2) = e^(x1)
*  Dx2f2(x1,x2) = 1.0
*/
class test_FunctionSet: public math_FunctionSetWithDerivatives
{
public:
virtual Standard_Integer NbVariables() const
{
return 2;
}

virtual Standard_Integer NbEquations() const
{
return 2;
}

virtual Standard_Boolean Value(const math_Vector& X, math_Vector& F)
{
F(1) = X(1) * X(1) + X(2) * X(2) - 4.0;
F(2) = exp(X(1)) + X(2) - 1.0;

return Standard_True;
}

virtual Standard_Boolean Derivatives(const math_Vector& X, math_Matrix& D)
{
D(1,1) = 2.0 * X(1);
D(1,2) = 2.0 * X(2);
D(2,1) = exp(X(1));
D(2,2) = 1.0;

return Standard_True;
}

virtual Standard_Boolean Values(const math_Vector& X, math_Vector& F, math_Matrix& D)
{
Value(X, F);
Derivatives(X, D);

return Standard_True;
}
};

void testFunctionSet(void)
{
test_FunctionSet aTestFunctionSet;

math_FunctionSetRoot aSolver(aTestFunctionSet);
math_Vector aStartPoint(1, 2, 0.0);

// initial guess point(-2.0, 0.0)
aStartPoint(1) = -2.0;
aStartPoint(2) = 0.0;
aSolver.Perform(aTestFunctionSet, aStartPoint);
std::cout << aSolver << std::endl;

// initial guess point(0.0, -2.0)
aStartPoint(1) = 0.0;
aStartPoint(2) = -2.0;
aSolver.Perform(aTestFunctionSet, aStartPoint);
std::cout << aSolver << std::endl;
}

int main(int argc, char* argv[])
{
testFunctionSet();
return 0;
}```

Figure 2.1 Evaluate result

3. Application

Figure 3.1 Extrema algorithms implemented by Function Set

Figure 3.2 Point project on curve

```//! Functional for search of extremum of the distance between point P and
//! surface S, starting from approximate solution (u0, v0).
//!
//! The class inherits math_FunctionSetWithDerivatives and thus is intended
//! for use in math_FunctionSetRoot algorithm .
//!
//! Denoting derivatives of the surface S(u,v) by u and v, respectively, as
//! Su and Sv, the two functions to be nullified are:
//!
//! F1(u,v) = (S - P) * Su
//! F2(u,v) = (S - P) * Sv
//!
//! The derivatives of the functional are:
//!
//! Duf1(u,v) = Su^2    + (S-P) * Suu;
//! Dvf1(u,v) = Su * Sv + (S-P) * Suv
//! Duf2(u,v) = Sv * Su + (S-P) * Suv = Dvf1
//! Dvf2(u,v) = Sv^2    + (S-P) * Svv
//!
//! Here * denotes scalar product, and ^2 is square power.
class Extrema_FuncExtPS  : public math_FunctionSetWithDerivatives
{
public:

DEFINE_STANDARD_ALLOC

Standard_EXPORT Extrema_FuncExtPS();

Standard_EXPORT Extrema_FuncExtPS(const gp_Pnt& P, const Adaptor3d_Surface& S);

//! sets the field mysurf of the function.
Standard_EXPORT void Initialize (const Adaptor3d_Surface& S);

//! sets the field mysurf of the function.
Standard_EXPORT void SetPoint (const gp_Pnt& P);

Standard_EXPORT Standard_Integer NbVariables() const Standard_OVERRIDE;

Standard_EXPORT Standard_Integer NbEquations() const Standard_OVERRIDE;

//! Calculate Fi(U,V).
Standard_EXPORT Standard_Boolean Value (const math_Vector& UV, math_Vector& F) Standard_OVERRIDE;

//! Calculate Fi\'(U,V).
Standard_EXPORT Standard_Boolean Derivatives (const math_Vector& UV, math_Matrix& DF) Standard_OVERRIDE;

//! Calculate Fi(U,V) and Fi\'(U,V).
Standard_EXPORT Standard_Boolean Values (const math_Vector& UV, math_Vector& F, math_Matrix& DF) Standard_OVERRIDE;

//! Save the found extremum.
Standard_EXPORT virtual Standard_Integer GetStateNumber() Standard_OVERRIDE;

//! Return the number of found extrema.
Standard_EXPORT Standard_Integer NbExt() const;

//! Return the value of the Nth distance.
Standard_EXPORT Standard_Real SquareDistance (const Standard_Integer N) const;

//! Returns the Nth extremum.
Standard_EXPORT const Extrema_POnSurf& Point (const Standard_Integer N) const;

protected:

private:
gp_Pnt myP;
Standard_Real myU;
Standard_Real myV;
gp_Pnt myPs;
TColStd_SequenceOfReal mySqDist;
Extrema_SequenceOfPOnSurf myPoint;
Standard_Boolean myPinit;
Standard_Boolean mySinit;
};```

4. Conclusion

5. References

1. 赵罡, 穆国旺, 王拉柱译. 非均匀有理B样条. 清华大学出版社. 1995

2. 易大义, 陈道琦. 数值分析引论. 浙江大学出版社. 1998

3. 易大义,沈云宝,李有法. 计算方法. 浙江大学出版社. 2002

4. 蒋尔雄,赵风光,苏仰锋. 数值逼近. 复旦大学出版社. 2012

5. 王仁宏,李崇君,朱春钢. 计算几何教程. 科学出版社. 2008

6. 同济大学数学教研室. 高等数学. 高等教育出版社. 1996

7. 同济大学应用数学系. 线性代数. 高等教育出版社. 2003