@@ -1862,12 +1862,13 @@ addConstraint (PyObject*, PyObject* args)
1862
1862
problem_t * problem = 0 ;
1863
1863
Function* function = 0 ;
1864
1864
PyObject* py_bounds = 0 ;
1865
+ PyObject* py_scaling = 0 ;
1865
1866
1866
1867
if (!PyArg_ParseTuple
1867
- (args, " O&O&O " ,
1868
+ (args, " O&O&OO " ,
1868
1869
&detail::problemConverter, &problem,
1869
1870
&detail::functionConverter, &function,
1870
- &py_bounds))
1871
+ &py_bounds, &py_scaling ))
1871
1872
return 0 ;
1872
1873
1873
1874
if (!problem)
@@ -1929,9 +1930,32 @@ addConstraint (PyObject*, PyObject* args)
1929
1930
return 0 ;
1930
1931
}
1931
1932
1933
+ double scaling = 1 .;
1934
+ if (py_scaling != Py_None)
1935
+ {
1936
+ if (PyFloat_Check (py_scaling))
1937
+ {
1938
+ scaling = PyFloat_AsDouble (py_scaling);
1939
+ }
1940
+ else if (PyList_Check (py_scaling)
1941
+ && (PyList_Size (py_scaling) == 1 )
1942
+ && (PyFloat_Check (PyList_GetItem (py_scaling, 0 ))))
1943
+ {
1944
+ PyObject* tmp = PyList_GetItem (py_scaling, 0 );
1945
+ scaling = PyFloat_AsDouble (tmp);
1946
+ }
1947
+ else
1948
+ {
1949
+ PyErr_SetString (PyExc_TypeError,
1950
+ " scaling should be a float or a list of floats." );
1951
+ return 0 ;
1952
+ }
1953
+ }
1954
+
1932
1955
problem->addConstraint (constraint,
1933
1956
Function::makeInterval (PyFloat_AsDouble (py_min),
1934
- PyFloat_AsDouble (py_max)));
1957
+ PyFloat_AsDouble (py_max)),
1958
+ scaling);
1935
1959
}
1936
1960
// Bounds = vector of pairs
1937
1961
else if (is_np_array
@@ -1944,6 +1968,28 @@ addConstraint (PyObject*, PyObject* args)
1944
1968
scaling_t scaling (constraint->outputSize (), 1 .);
1945
1969
intervals_t bounds (constraint->outputSize ());
1946
1970
1971
+ if (py_scaling != Py_None)
1972
+ {
1973
+ if (PyList_Check (py_scaling)
1974
+ && (PyList_Size (py_scaling) == constraint->outputSize ()))
1975
+ {
1976
+ for (int i = 0 ; i < constraint->outputSize (); ++i)
1977
+ {
1978
+ PyObject* tmp = PyList_GetItem (py_scaling, i);
1979
+ if (PyFloat_Check (tmp))
1980
+ {
1981
+ scaling[i] = PyFloat_AsDouble (tmp);
1982
+ }
1983
+ }
1984
+ }
1985
+ else
1986
+ {
1987
+ PyErr_SetString (PyExc_TypeError,
1988
+ " scaling should be a list of floats." );
1989
+ return 0 ;
1990
+ }
1991
+ }
1992
+
1947
1993
for (Function::size_type i = 0 ; i < constraint->outputSize (); ++i)
1948
1994
{
1949
1995
// TODO: check array type
0 commit comments