@@ -120,4 +120,99 @@ static PyObject *py_get_hessian(PyObject *self, PyObject *args)
120120 return Py_BuildValue ("(OOO(ii))" , data , indices , indptr , H -> m , H -> n );
121121}
122122
123+ static PyObject * py_get_problem_hessian_sparsity_coo (PyObject * self , PyObject * args )
124+ {
125+ PyObject * prob_capsule ;
126+ if (!PyArg_ParseTuple (args , "O" , & prob_capsule ))
127+ {
128+ return NULL ;
129+ }
130+
131+ problem * prob =
132+ (problem * ) PyCapsule_GetPointer (prob_capsule , PROBLEM_CAPSULE_NAME );
133+ if (!prob )
134+ {
135+ PyErr_SetString (PyExc_ValueError , "invalid problem capsule" );
136+ return NULL ;
137+ }
138+
139+ if (!prob -> lagrange_hessian_coo )
140+ {
141+ PyErr_SetString (PyExc_RuntimeError ,
142+ "hessian COO not initialized - call "
143+ "problem_init_hessian_coo_lower_triangular first" );
144+ return NULL ;
145+ }
146+
147+ COO_Matrix * coo = prob -> lagrange_hessian_coo ;
148+ npy_intp nnz = coo -> nnz ;
149+
150+ PyObject * rows = PyArray_SimpleNew (1 , & nnz , NPY_INT32 );
151+ PyObject * cols = PyArray_SimpleNew (1 , & nnz , NPY_INT32 );
152+
153+ if (!rows || !cols )
154+ {
155+ Py_XDECREF (rows );
156+ Py_XDECREF (cols );
157+ return NULL ;
158+ }
159+
160+ memcpy (PyArray_DATA ((PyArrayObject * ) rows ), coo -> rows , nnz * sizeof (int ));
161+ memcpy (PyArray_DATA ((PyArrayObject * ) cols ), coo -> cols , nnz * sizeof (int ));
162+
163+ return Py_BuildValue ("(OO(ii))" , rows , cols , coo -> m , coo -> n );
164+ }
165+
166+ static PyObject * py_problem_eval_hessian_vals_coo (PyObject * self , PyObject * args )
167+ {
168+ PyObject * prob_capsule ;
169+ double obj_factor ;
170+ PyObject * lagrange_obj ;
171+
172+ if (!PyArg_ParseTuple (args , "OdO" , & prob_capsule , & obj_factor , & lagrange_obj ))
173+ {
174+ return NULL ;
175+ }
176+
177+ problem * prob =
178+ (problem * ) PyCapsule_GetPointer (prob_capsule , PROBLEM_CAPSULE_NAME );
179+ if (!prob )
180+ {
181+ PyErr_SetString (PyExc_ValueError , "invalid problem capsule" );
182+ return NULL ;
183+ }
184+
185+ /* Convert lagrange to contiguous C array */
186+ PyArrayObject * lagrange_arr = (PyArrayObject * ) PyArray_FROM_OTF (
187+ lagrange_obj , NPY_DOUBLE , NPY_ARRAY_IN_ARRAY );
188+ if (!lagrange_arr )
189+ {
190+ return NULL ;
191+ }
192+
193+ double * lagrange = (double * ) PyArray_DATA (lagrange_arr );
194+
195+ /* Compute Hessian */
196+ problem_hessian (prob , obj_factor , lagrange );
197+
198+ Py_DECREF (lagrange_arr );
199+
200+ /* Refresh COO values from the CSR Hessian */
201+ refresh_lower_triangular_coo (prob -> lagrange_hessian_coo ,
202+ prob -> lagrange_hessian -> x );
203+
204+ COO_Matrix * coo = prob -> lagrange_hessian_coo ;
205+ npy_intp nnz = coo -> nnz ;
206+
207+ PyObject * data = PyArray_SimpleNew (1 , & nnz , NPY_DOUBLE );
208+ if (!data )
209+ {
210+ return NULL ;
211+ }
212+
213+ memcpy (PyArray_DATA ((PyArrayObject * ) data ), coo -> x , nnz * sizeof (double ));
214+
215+ return data ;
216+ }
217+
123218#endif /* PROBLEM_HESSIAN_H */
0 commit comments