Skip to content

Commit b09390e

Browse files
author
Benjamin Chrétien
committed
Fix error with column-major storage order.
1 parent f4d0247 commit b09390e

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/wrap.cc

+16-6
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,28 @@ namespace roboptim
300300
return;
301301
}
302302

303-
// TODO: check storage order (NumPy expects C-style here, but Eigen uses ColMajor as default)
304-
// Moreover, RobOptim users can change the storage order.
305303
npy_intp inputSize =
306304
static_cast<npy_intp> (::roboptim::core::python::Function::inputSize ());
307305
npy_intp outputSize =
308306
static_cast<npy_intp> (::roboptim::core::python::Function::outputSize ());
309307

308+
// Check storage order and map memory accordingly (PyArray_SimpleNewFromData
309+
// expects a row-major matrix).
310+
PyObject* jacobianNumpy = NULL;
310311
npy_intp sizes[2] = {outputSize, inputSize};
311312

312-
PyObject* jacobianNumpy =
313-
PyArray_SimpleNewFromData (2, &sizes[0], NPY_DOUBLE,
314-
jacobian.data ());
313+
if (::roboptim::StorageOrder == Eigen::RowMajor)
314+
{
315+
jacobianNumpy = PyArray_SimpleNewFromData (2, &sizes[0], NPY_DOUBLE,
316+
jacobian.data ());
317+
}
318+
else
319+
{
320+
jacobianNumpy = PyArray_NewFromDescr (&PyArray_Type, PyArray_DescrFromType (PyArray_DOUBLE),
321+
2, sizes, NULL, jacobian.data (),
322+
NPY_WRITEABLE | NPY_F_CONTIGUOUS, NULL);
323+
}
324+
315325
if (!jacobianNumpy)
316326
{
317327
PyErr_SetString (PyExc_TypeError, "cannot convert result");
@@ -1176,7 +1186,7 @@ getName (PyObject*, PyObject* args)
11761186
}
11771187

11781188
static PyObject*
1179-
getStorageOrder (PyObject*, PyObject* args)
1189+
getStorageOrder (PyObject*, PyObject*)
11801190
{
11811191
char storage_order = (::roboptim::core::python::NPY_STORAGE_ORDER == NPY_F_CONTIGUOUS)? 'F':'C';
11821192
return Py_BuildValue("c", storage_order);

tests/schittkowski.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ def test_problem_48(self):
248248
solver.solve ()
249249
r = solver.minimum ()
250250
print (r)
251-
if not type(r) == roboptim.core.PySolverError:
252-
numpy.testing.assert_almost_equal (r.value, [0.])
253-
numpy.testing.assert_almost_equal (r.x, [1., 1., 1., 1., 1.])
251+
numpy.testing.assert_almost_equal (r.value, [0.])
252+
numpy.testing.assert_almost_equal (r.x, [1., 1., 1., 1., 1.])
254253

255254
if __name__ == '__main__':
256255
unittest.main ()

tests/wrap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def jacobian(result, x):
147147
#self.assertRaises(NotImplementedError,
148148
#lambda: roboptim.core.gradient (f, gradient, x, 0))
149149

150-
jacobian = numpy.zeros((outputSize, inputSize))
150+
jacobian = numpy.zeros((outputSize, inputSize), order=roboptim.core.getStorageOrder())
151151
roboptim.core.jacobian (f, jacobian, x)
152152
expected_jacobian = numpy.array([[1., 1.], [1., -1.], [10., 15.]])
153153
numpy.testing.assert_almost_equal (jacobian, expected_jacobian)

0 commit comments

Comments
 (0)