Skip to content

Commit fc555ab

Browse files
committed
Scipy: fixed style with clang-format
1 parent 816fdac commit fc555ab

File tree

3 files changed

+52
-55
lines changed

3 files changed

+52
-55
lines changed

math/scipy/src/ScipyMinimizer.cxx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ScipyMinimizer::ScipyMinimizer() : BasicMinimizer()
7474
fOptions.SetMinimizerType("Scipy");
7575
fOptions.SetMinimizerAlgorithm("L-BFGS-B");
7676
PyInitialize();
77-
fHessianFunc = nullptr;
77+
fHessianFunc = nullptr;
7878
// set extra options
7979
SetAlgoExtraOptions();
8080
fConstraintsList = PyList_New(0);
@@ -239,13 +239,13 @@ bool ScipyMinimizer::Minimize()
239239

240240
if (GetVariableSettings(i, varsettings)) {
241241
if (varsettings.HasLowerLimit()) {
242-
foundBounds=true;
242+
foundBounds = true;
243243
PyList_SetItem(pylimits_lower, i, PyFloat_FromDouble(varsettings.LowerLimit()));
244244
} else {
245245
PyList_SetItem(pylimits_lower, i, PyFloat_FromDouble(-NPY_INFINITY));
246246
}
247247
if (varsettings.HasUpperLimit()) {
248-
foundBounds=true;
248+
foundBounds = true;
249249
PyList_SetItem(pylimits_upper, i, PyFloat_FromDouble(varsettings.UpperLimit()));
250250
} else {
251251
PyList_SetItem(pylimits_upper, i, PyFloat_FromDouble(NPY_INFINITY));
@@ -255,35 +255,32 @@ bool ScipyMinimizer::Minimize()
255255
}
256256
}
257257
PyObject *pybounds = Py_None;
258-
if(foundBounds)
259-
{
258+
if (foundBounds) {
260259
PyTuple_SetItem(pybounds_args, 0, pylimits_lower);
261260
PyTuple_SetItem(pybounds_args, 1, pylimits_upper);
262261
pybounds = PyObject_CallObject(fBoundsMod, pybounds_args);
263262
}
264-
263+
265264
// minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None,
266265
// callback=None, options=None)
267266
PyObject *args = Py_BuildValue("(OO)", fTarget, x0);
268-
PyObject *kw = Py_BuildValue("{s:s,s:O,,s:O,s:O,s:O,s:d,s:O}", "method", method.c_str(), "jac", fJacobian, "hess",
269-
fHessian, "bounds", pybounds,"constraints",fConstraintsList, "tol", Tolerance(),
270-
"options", pyoptions);
271-
if(PrintLevel()>0)
272-
{
273-
std::cout<<"========Minimizer Parameters========\n";
267+
PyObject *kw =
268+
Py_BuildValue("{s:s,s:O,,s:O,s:O,s:O,s:d,s:O}", "method", method.c_str(), "jac", fJacobian, "hess", fHessian,
269+
"bounds", pybounds, "constraints", fConstraintsList, "tol", Tolerance(), "options", pyoptions);
270+
if (PrintLevel() > 0) {
271+
std::cout << "========Minimizer Parameters========\n";
274272
PyPrint(kw);
275-
std::cout<<"====================================\n";
273+
std::cout << "====================================\n";
276274
}
277275
PyObject *result = PyObject_Call(fMinimize, args, kw);
278276
if (result == NULL) {
279277
PyErr_Print();
280278
return false;
281279
}
282-
if(PrintLevel()>0)
283-
{
284-
std::cout<<"========Minimizer Results========\n";
280+
if (PrintLevel() > 0) {
281+
std::cout << "========Minimizer Results========\n";
285282
PyPrint(result);
286-
std::cout<<"=================================\n";
283+
std::cout << "=================================\n";
287284
}
288285
Py_DECREF(pybounds);
289286
Py_DECREF(args);
@@ -323,8 +320,10 @@ bool ScipyMinimizer::Minimize()
323320
std::cout << "=== Status: " << status << std::endl;
324321
std::cout << "=== Message: " << message << std::endl;
325322
std::cout << "=== Function calls: " << nfev << std::endl;
326-
if(success) fStatus=0;
327-
else fStatus = status; //suggested by Lorenzo.
323+
if (success)
324+
fStatus = 0;
325+
else
326+
fStatus = status; // suggested by Lorenzo.
328327

329328
Py_DECREF(result);
330329
return success;

math/scipy/test/testScipyMinimizer.cxx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ double ConstRosenBrock(const std::vector<double> &x)
4848
return x[0] * x[0] - x[1] * x[1] + 1;
4949
}
5050
// NOTE: "dogleg" is problematic, requires better tuning of the parameters
51-
std::string methods[] = {"Nelder-Mead", "L-BFGS-B", "Powell", "CG", "BFGS",
52-
"TNC", "COBYLA", "SLSQP", "trust-constr", "Newton-CG",
53-
"trust-ncg", "trust-exact", "trust-krylov"};
51+
std::string methods[] = {"Nelder-Mead", "L-BFGS-B", "Powell", "CG", "BFGS",
52+
"TNC", "COBYLA", "SLSQP", "trust-constr", "Newton-CG",
53+
"trust-ncg", "trust-exact", "trust-krylov"};
5454

5555
// Testing fit using class with gradient
5656
class ScipyFitClass : public ::testing::Test {
@@ -78,15 +78,15 @@ class ScipyFitClass : public ::testing::Test {
7878
minimizer->SetVariable(1, "y", variable[1], step[1]);
7979
if (useConstraint)
8080
minimizer->AddConstraintFunction(ConstRosenBrock, "ineq");
81-
81+
8282
minimizer->Minimize();
8383
}
8484
};
8585

86-
TEST_F(ScipyFitClass, Fit)
86+
TEST_F(ScipyFitClass, Fit)
8787
{
8888
for (const std::string &text : methods) {
89-
Fit(text.c_str(),false);
89+
Fit(text.c_str(), false);
9090
EXPECT_EQ(1000000, minimizer->MaxFunctionCalls());
9191

9292
EXPECT_EQ(100000, minimizer->MaxIterations());
@@ -117,7 +117,6 @@ TEST_F(ScipyFitClass, Fit)
117117
}
118118
}
119119

120-
121120
TEST_F(ScipyFitClass, FitContraint) // using constraint function
122121
{
123122
for (const std::string &text : methods) {

tutorials/fit/scipy.C

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
#include "Math/MinimizerOptions.h"
77
#include "TStopwatch.h"
88

9-
10-
double RosenBrock(const double *xx )
9+
double RosenBrock(const double *xx)
1110
{
12-
const Double_t x = xx[0];
13-
const Double_t y = xx[1];
14-
const Double_t tmp1 = y-x*x;
15-
const Double_t tmp2 = 1-x;
16-
return 100*tmp1*tmp1+tmp2*tmp2;
11+
const Double_t x = xx[0];
12+
const Double_t y = xx[1];
13+
const Double_t tmp1 = y - x * x;
14+
const Double_t tmp2 = 1 - x;
15+
return 100 * tmp1 * tmp1 + tmp2 * tmp2;
1716
}
1817

1918
double RosenBrockGrad(const double *x, unsigned int ipar)
@@ -28,55 +27,55 @@ bool RosenBrockHessian(const std::vector<double> &xx, double *hess)
2827
{
2928
const double x = xx[0];
3029
const double y = xx[1];
31-
32-
hess[0] = 1200*x*x - 400*y + 2;
33-
hess[1] = -400*x;
34-
hess[2] = -400*x;
30+
31+
hess[0] = 1200 * x * x - 400 * y + 2;
32+
hess[1] = -400 * x;
33+
hess[2] = -400 * x;
3534
hess[3] = 200;
36-
35+
3736
return true;
3837
}
3938

4039
// methods that requires hessian to work "dogleg", "trust-ncg","trust-exact","trust-krylov"
4140
using namespace std;
4241
int scipy()
43-
{
44-
45-
std::string methods[]={"Nelder-Mead","L-BFGS-B","Powell","CG","BFGS","TNC","COBYLA","SLSQP","trust-constr","Newton-CG", "dogleg", "trust-ncg","trust-exact","trust-krylov"};
42+
{
43+
44+
std::string methods[] = {"Nelder-Mead", "L-BFGS-B", "Powell", "CG", "BFGS",
45+
"TNC", "COBYLA", "SLSQP", "trust-constr", "Newton-CG",
46+
"dogleg", "trust-ncg", "trust-exact", "trust-krylov"};
4647
TStopwatch t;
47-
for(const std::string &text : methods)
48-
{
48+
for (const std::string &text : methods) {
4949
ROOT::Math::Experimental::ScipyMinimizer minimizer(text.c_str());
5050
minimizer.SetMaxFunctionCalls(1000000);
5151
minimizer.SetMaxIterations(100000);
5252
minimizer.SetTolerance(1e-3);
53-
ROOT::Math::GradFunctor f(&RosenBrock,&RosenBrockGrad,2);
54-
double step[2] = {0.01,0.01};
55-
double variable[2] = { -1.2,1.0};
56-
53+
ROOT::Math::GradFunctor f(&RosenBrock, &RosenBrockGrad, 2);
54+
double step[2] = {0.01, 0.01};
55+
double variable[2] = {-1.2, 1.0};
56+
5757
minimizer.SetFunction(f);
5858
minimizer.SetHessianFunction(RosenBrockHessian);
59-
59+
6060
// variables to be minimized!
61-
minimizer.SetVariable(0,"x",variable[0], step[0]);
62-
minimizer.SetVariable(1,"y",variable[1], step[1]);
61+
minimizer.SetVariable(0, "x", variable[0], step[0]);
62+
minimizer.SetVariable(1, "y", variable[1], step[1]);
6363
minimizer.SetVariableLimits(0, -2.0, 2.0);
6464
minimizer.SetVariableLimits(1, -2.0, 2.0);
6565

6666
t.Reset();
6767
t.Start();
68-
minimizer.Minimize();
68+
minimizer.Minimize();
6969
t.Stop();
7070
const double *xs = minimizer.X();
71-
cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
72-
<< RosenBrock(xs) << endl;
73-
cout << "Cpu Time (sec) = " << t.CpuTime() <<endl<< "Real Time (sec) = " << t.RealTime() << endl;
71+
cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): " << RosenBrock(xs) << endl;
72+
cout << "Cpu Time (sec) = " << t.CpuTime() << endl << "Real Time (sec) = " << t.RealTime() << endl;
7473
cout << endl << "===============" << endl;
7574
}
7675
return 0;
7776
}
7877

7978
int main()
8079
{
81-
return scipy();
80+
return scipy();
8281
}

0 commit comments

Comments
 (0)