Skip to content

Commit d4adb89

Browse files
committed
added some documentations, removed some comments, added some deletes in the destructor
1 parent d65ea94 commit d4adb89

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

math/scipy/inc/Math/ScipyMinimizer.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,19 @@ namespace Experimental {
5050

5151
//_____________________________________________________________________________________
5252
/**
53-
* \class ScipyMinimizer
54-
* ScipyMinimizer class.
55-
* Implementation for Scipy ... TODO
53+
\class ScipyMinimizer
54+
ScipyMinimizer class.
55+
Scipy minimizer implementation using Python C API that supports several methods such as
56+
Nelder-Mead, L-BFGS-B, Powell, CG, BFGS, TNC, COBYLA, SLSQP, trust-constr,
57+
Newton-CG, dogleg, trust-ncg, trust-exact and trust-krylov.
5658
57-
See <A HREF="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html">Scipy doc</A>
58-
from more info on the Scipy minimization algorithms.
59+
It supports the Jacobian (Gradients), Hessian and bounds for the variables.
60+
61+
Support for constraint functions will be implemented in the next releases.
62+
You can find a macro example in the folder $ROOTSYS/tutorial/fit/scipy.C
63+
64+
See <A HREF="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html">Scipy doc</A>
65+
from more info on the Scipy minimization algorithms.
5966
6067
@ingroup MultiMin
6168
*/

math/scipy/src/ScipyMinimizer.cxx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ using namespace ROOT::Fit;
2121
const ROOT::Math::IMultiGenFunction *gFunction = nullptr;
2222
/// function wrapper for the gradient of the function to be minimized
2323
const ROOT::Math::IMultiGradFunction *gGradFunction = nullptr;
24-
24+
/// function wrapper for the hessian of the function to be minimized
2525
std::function<bool(const std::vector<double> &, double *)> gfHessianFunction;
2626

27+
/// simple function for debugging
2728
#define PyPrint(pyo) PyObject_Print(pyo, stdout, Py_PRINT_RAW)
2829

30+
31+
/// function to wrap into Python the C/C++ target function to be minimized
2932
PyObject *target_function(PyObject * /*self*/, PyObject *args)
3033
{
3134
PyArrayObject *arr = (PyArrayObject *)PyTuple_GetItem(args, 0);
@@ -36,6 +39,7 @@ PyObject *target_function(PyObject * /*self*/, PyObject *args)
3639
return PyFloat_FromDouble(r);
3740
};
3841

42+
/// function to wrap into Python the C/C++ jacobian function
3943
PyObject *jac_function(PyObject * /*self*/, PyObject *args)
4044
{
4145
PyArrayObject *arr = (PyArrayObject *)PyTuple_GetItem(args, 0);
@@ -49,6 +53,7 @@ PyObject *jac_function(PyObject * /*self*/, PyObject *args)
4953
return py_array;
5054
};
5155

56+
/// function to wrap into Python the C/C++ hessian function
5257
PyObject *hessian_function(PyObject * /*self*/, PyObject *args)
5358
{
5459
PyArrayObject *arr = (PyArrayObject *)PyTuple_GetItem(args, 0);
@@ -60,9 +65,6 @@ PyObject *hessian_function(PyObject * /*self*/, PyObject *args)
6065
gfHessianFunction(x, values);
6166
npy_intp dims[2] = {size, size};
6267
PyObject *py_array = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, values);
63-
// std::cout<<"---------------"<<std::endl;
64-
// PyPrint(py_array);
65-
// std::cout<<"---------------"<<std::endl;
6668
return py_array;
6769
};
6870

@@ -71,9 +73,7 @@ ScipyMinimizer::ScipyMinimizer() : BasicMinimizer()
7173
{
7274
fOptions.SetMinimizerType("Scipy");
7375
fOptions.SetMinimizerAlgorithm("L-BFGS-B");
74-
if (!PyIsInitialized()) {
75-
PyInitialize();
76-
}
76+
PyInitialize();
7777
fHessianFunc = [](const std::vector<double> &, double *) -> bool { return false; };
7878
// set extra options
7979
SetAlgoExtraOptions();
@@ -84,9 +84,7 @@ ScipyMinimizer::ScipyMinimizer(const char *type)
8484
{
8585
fOptions.SetMinimizerType("Scipy");
8686
fOptions.SetMinimizerAlgorithm(type);
87-
if (!PyIsInitialized()) {
88-
PyInitialize();
89-
}
87+
PyInitialize();
9088
fHessianFunc = [](const std::vector<double> &, double *) -> bool { return false; };
9189
// set extra options
9290
SetAlgoExtraOptions();
@@ -169,23 +167,23 @@ void ScipyMinimizer::PyInitialize()
169167
// Finalize Python interpreter
170168
void ScipyMinimizer::PyFinalize()
171169
{
172-
if (fMinimize)
173-
Py_DECREF(fMinimize);
174-
if (fBoundsMod)
175-
Py_DECREF(fBoundsMod);
176170
Py_Finalize();
177171
}
178172

179173
//_______________________________________________________________________
180174
int ScipyMinimizer::PyIsInitialized()
181175
{
182-
if (!Py_IsInitialized())
183-
return kFALSE;
184-
return kTRUE;
176+
return Py_IsInitialized();
185177
}
186178

187179
//_______________________________________________________________________
188-
ScipyMinimizer::~ScipyMinimizer() {}
180+
ScipyMinimizer::~ScipyMinimizer()
181+
{
182+
if (fMinimize)
183+
Py_DECREF(fMinimize);
184+
if (fBoundsMod)
185+
Py_DECREF(fBoundsMod);
186+
}
189187

190188
//_______________________________________________________________________
191189
bool ScipyMinimizer::Minimize()

0 commit comments

Comments
 (0)