Summary: A user-defined function can be formulated in SymPy, whose derivatives can be obtained almost automatically, and numerical functions for the objective function and its derivatives can be retrieved by lambdify
. By using lambdify
ed Python function, you do not trade efficience due to symbolic evaluation. Also, we can make use of SciPy optimization package which contains Newton method.
scipy.optimize.minimize
offers Newton method. You can feed it with your objective function and its derivative and get the result.
The use of symbolic math in your original code does not makes sense to me as it deals with a specific math formula. Use NumPy and you get better execution performance.
If you are thinking of offering a general math interface to various math formulae, SymPy is great because we can enjoy its derivative functionality.
Several months ago, I have played with the combination of SymPy and SciPy optimize to do a physical model of spring-connected bodies.
https://github.com/wakita/symdoc/blob/master/kk.ipynb
- The formula of the objective function is given in In[9].
- A Python function that corresponds to the objective function is obtained in In[10].
- Derivatives of the objective function is obtained in In[14].
- Newton-based Minimization is performed in In[19]. It makes use of the first- and second-order derivatives of the objective function.
The beauty of using SymPy is that as you see in my example, I do not mess with complex calculation of derivatives at all and for another, the solution deals with any formula that is expressed in SymPy.