1616"""
1717
1818import numpy as np
19- from scipy import sparse
2019
2120import cvxpy as cp
2221
@@ -42,31 +41,21 @@ def __init__(self, cvxpy_problem: cp.Problem, verbose: bool = True):
4241 c_obj = convert_expr (cvxpy_problem .objective .expr , var_dict , n_vars )
4342 c_constraints = [convert_expr (c .expr , var_dict , n_vars ) for c in cvxpy_problem .constraints ]
4443 self ._capsule = _diffengine .make_problem (c_obj , c_constraints , verbose )
45- self ._jacobian_allocated = False
46- self ._hessian_allocated = False
4744
48- def init_jacobian (self ):
49- """Initialize Jacobian structures only. Must be called before jacobian()."""
50- _diffengine .problem_init_jacobian (self ._capsule )
51- self ._jacobian_allocated = True
52-
5345 def init_jacobian_coo (self ):
54- """Initialize Jacobian COO structures only .
46+ """Fill sparsity for the constraint Jacobian in COO format .
5547
56- Must be called before get_jacobian_sparsity_coo().
48+ Must be called once before get_jacobian_sparsity_coo() or eval_jacobian_vals ().
5749 """
5850 _diffengine .problem_init_jacobian_coo (self ._capsule )
59- self ._jacobian_allocated = True
60-
61- def init_hessian (self ):
62- """Initialize Hessian structures only. Must be called before hessian()."""
63- _diffengine .problem_init_hessian (self ._capsule )
64- self ._hessian_allocated = True
6551
6652 def init_hessian_coo_lower_tri (self ):
67- """Initialize Hessian COO structures only. Must be called before get_hessian()."""
53+ """Fill sparsity for the Lagrangian Hessian (lower triangle, COO).
54+
55+ Must be called once before get_problem_hessian_sparsity_coo() or
56+ eval_hessian_vals_coo_lower_tri().
57+ """
6858 _diffengine .problem_init_hessian_coo_lower_triangular (self ._capsule )
69- self ._hessian_allocated = True
7059
7160 def objective_forward (self , u : np .ndarray ) -> float :
7261 """Evaluate objective. Returns obj_value float."""
@@ -79,63 +68,43 @@ def constraint_forward(self, u: np.ndarray) -> np.ndarray:
7968 def gradient (self ) -> np .ndarray :
8069 """Compute gradient of objective. Call objective_forward first. Returns gradient array."""
8170 return _diffengine .problem_gradient (self ._capsule )
82-
83- def jacobian (self ) -> sparse .csr_matrix :
84- """Compute constraint Jacobian. Call constraint_forward first."""
85- data , indices , indptr , shape = _diffengine .problem_jacobian (self ._capsule )
86- return sparse .csr_matrix ((data , indices , indptr ), shape = shape )
8771
8872 def get_jacobian_sparsity_coo (self ) -> tuple [np .ndarray , np .ndarray ]:
89- """Get Jacobian sparsity pattern as COO. This function does not evaluate the jacobian."""
73+ """Return the sparsity pattern (row, col) of the constraint Jacobian.
74+
75+ Does not evaluate the Jacobian; only returns structural nonzero indices.
76+ Call init_jacobian_coo() first.
77+ """
9078 rows , cols , unused_shape = _diffengine .get_jacobian_sparsity_coo (self ._capsule )
9179 return rows , cols
9280
9381 def eval_jacobian_vals (self ) -> np .ndarray :
94- """Evaluate Jacobian values only .
82+ """Evaluate the constraint Jacobian and return its nonzero values .
9583
96- Call constraint_forward first. Returns jacobian values array.
84+ The values correspond to the sparsity pattern from get_jacobian_sparsity_coo().
85+ Call constraint_forward() first to set the evaluation point.
9786 """
9887 return _diffengine .problem_eval_jacobian_vals (self ._capsule )
9988
100- def get_jacobian (self ) -> sparse .csr_matrix :
101- """Get constraint Jacobian. This function does not evaluate the jacobian. """
102- data , indices , indptr , shape = _diffengine .get_jacobian (self ._capsule )
103- return sparse .csr_matrix ((data , indices , indptr ), shape = shape )
104-
10589 def get_problem_hessian_sparsity_coo (self ) -> tuple [np .ndarray , np .ndarray ]:
106- """Get Hessian sparsity pattern as COO. This function does not evaluate the hessian."""
90+ """Return the sparsity pattern (row, col) of the lower-triangular Lagrangian Hessian.
91+
92+ Does not evaluate the Hessian; only returns structural nonzero indices.
93+ Call init_hessian_coo_lower_tri() first.
94+ """
10795 rows , cols , unused_shape = _diffengine .get_problem_hessian_sparsity_coo (self ._capsule )
10896 return rows , cols
109-
97+
11098 def eval_hessian_vals_coo_lower_tri (
11199 self , obj_factor : float , lagrange : np .ndarray
112100 ) -> np .ndarray :
113- """Evaluate Hessian values only for lower triangular part.
114-
115- Call objective_forward and constraint_forward first.
116- """
117- return _diffengine .problem_eval_hessian_vals_coo (self ._capsule , obj_factor , lagrange )
101+ """Evaluate the lower-triangular Lagrangian Hessian and return its nonzero values.
118102
119- def hessian (self , obj_factor : float , lagrange : np .ndarray ) -> sparse .csr_matrix :
120- """Compute Lagrangian Hessian.
103+ Computes obj_factor * hess_f + sum(lagrange[i] * hess_gi), where f is the objective
104+ and gi are the constraints. The values correspond to the sparsity pattern from
105+ get_problem_hessian_sparsity_coo(). Only the lower triangle is returned.
121106
122- Computes: obj_factor * H_obj + sum(lagrange_i * H_constraint_i)
123-
124- Call objective_forward and constraint_forward before this.
125-
126- Args:
127- obj_factor: Weight for objective Hessian
128- lagrange: Array of Lagrange multipliers (length = total_constraint_size)
129-
130- Returns:
131- scipy CSR matrix of shape (n_vars, n_vars)
107+ Call objective_forward() and constraint_forward() first to set the evaluation point.
132108 """
133- data , indices , indptr , shape = _diffengine .problem_hessian (
134- self ._capsule , obj_factor , lagrange
135- )
136- return sparse .csr_matrix ((data , indices , indptr ), shape = shape )
109+ return _diffengine .problem_eval_hessian_vals_coo (self ._capsule , obj_factor , lagrange )
137110
138- def get_hessian (self ) -> sparse .csr_matrix :
139- """Get Lagrangian Hessian. This function does not evaluate the hessian."""
140- data , indices , indptr , shape = _diffengine .get_hessian (self ._capsule )
141- return sparse .csr_matrix ((data , indices , indptr ), shape = shape )
0 commit comments