# JuMP--how to take the gradient of quadratic constraints?

**URL:** <https://discourse.julialang.org/t/jump-how-to-take-the-gradient-of-quadratic-constraints/78893>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [April 2, 2022, 12:54am UTC](https://discourse.julialang.org/t/jump-how-to-take-the-gradient-of-quadratic-constraints/78893 "2022-04-02T00:54:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjuly](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@rjuly](https://discourse.julialang.org/u/rjuly)\
**Post date:** [April 2, 2022, 12:54am UTC](https://discourse.julialang.org/t/jump-how-to-take-the-gradient-of-quadratic-constraints/78893/1 "2022-04-02T00:54:58Z")

</div>

I need to get the constraint Jacobian for a model which contains linear, quadratic, and nonlinear constraints. For the nonlinear constraints, I can use the NLPEvaluator with function eval\_constraint\_jacobian. For the linear constraints, my current strategy is to get the constraint coefficients as follows:

```julia
linear_constraint_references = all_constraints(model, AffExpr, MOI.EqualTo{Float64})
x = all_variables(model)
linear_jacobian = zeros(length(linear_constraint_references), length(x))
for r = 1:length(linear_constraint_references)
    for v = 1:length(x)
        linear_jacobian[r,v] = normalized_coefficient(linear_constraint_references[r], x[v])
    end
end

```

However, I’m getting stuck on the quadratic constraints. I can get the coefficient for any linear terms in the quadratic expression using the normalized\_coefficient method, but I don’t know how to query the quadratic constraint to find the coefficient of any quadratic terms. Is there a way to do this? Or is there another method to take the gradient of the quadratic constraint?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 2, 2022, 2:51am UTC](https://discourse.julialang.org/t/jump-how-to-take-the-gradient-of-quadratic-constraints/78893/2 "2022-04-02T02:51:09Z")

</div>

You can do something similar, and get the coefficient of a quadratic expression: [Expressions · JuMP](https://jump.dev/JuMP.jl/stable/manual/expressions/#Coefficients-2)

```julia
func_set = constraint_object(r)
c = coefficient(func_set.func, x, y) # quadratic coefficient of x * y
c = coefficient(func_set.func, x) # affine coefficient of x

```

See also [Computing Hessians · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/querying_hessians/#Hessians-from-QuadExpr-functions)

---

<div class="post-metadata">

**Author:** ![rjuly](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@rjuly](https://discourse.julialang.org/u/rjuly)\
**Post date:** [April 2, 2022, 2:45pm UTC](https://discourse.julialang.org/t/jump-how-to-take-the-gradient-of-quadratic-constraints/78893/3 "2022-04-02T14:45:37Z")

</div>

Got it, that’s exactly what I needed! Thank you.
