# Extracting the coefficient of a nonlinear term in a JuMP model

**URL:** <https://discourse.julialang.org/t/extracting-the-coefficient-of-a-nonlinear-term-in-a-jump-model/126438>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [February 28, 2025, 10:33pm UTC](https://discourse.julialang.org/t/extracting-the-coefficient-of-a-nonlinear-term-in-a-jump-model/126438 "2025-02-28T22:33:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![handahye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/handahye/32/215530_2.png) [@handahye](https://discourse.julialang.org/u/handahye)\
**Post date:** [February 28, 2025, 10:33pm UTC](https://discourse.julialang.org/t/extracting-the-coefficient-of-a-nonlinear-term-in-a-jump-model/126438/1 "2025-02-28T22:33:37Z")

</div>

For a generic nonlinear constraint, is there a way for me to extract the coefficient of a nonlinear term (e.g., multilinear terms)?

e.g.,

```julia
model = Model()
@variable(model, x[1:3])
@constraint(model, (x[1] - (((3* ((x[2] ^ 2.0) + (x[3] ^ 2.0))))))<=0)

```

In above model, I wish to extract the coefficient of x[3]^2, which will be -3. I am not building the model myself, but rather reading it from an .nlp file, so even though the constraint can written with less parentheses, when I read in the model, the constraint will have a form like above.

The type of constraint is “ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarNonlinearFunction, MathOptInterface.LessThan{Float64}}, ScalarShape}”.

---

<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:** [February 28, 2025, 10:56pm UTC](https://discourse.julialang.org/t/extracting-the-coefficient-of-a-nonlinear-term-in-a-jump-model/126438/2 "2025-02-28T22:56:22Z")

</div>

Hi @handahye, welcome to the forum 😄

The simple answer is “no”.

If you grab the constraint function:

```julia
model = Model()
@variable(model, x[1:3])
c = @constraint(model, (x[1] - (((3* ((x[2] ^ 2.0) + (x[3] ^ 2.0)))))) <= 0)
o = constraint_object(c)
o.func

```

You can play around to see what the fields are: [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nonlinear/#Fields)

```Julia
julia> o.func.args[1].args[2].args
2-element Vector{Any}:
 3.0
  (x[2] ^ 2.0) + (x[3] ^ 2.0)

```

I don’t know what an `.nlp` file is, but if you had written the constraint as the quadratic:

```Julia
c_quad = @constraint(model, x[1] - 3 * (x[2]^2 + x[3]^2) <= 0)
c_quad_object = constraint_object(c_quad)
coefficient(c_quad_object.func, x[2], x[2])

```

this would work.

We’re working on some features to automatically convert nonlinear constraints to quadratic, etc, that might help in a future release of JuMP: [[Nonlinear] add SymbolicAD submodule by odow · Pull Request #2624 · jump-dev/MathOptInterface.jl · GitHub](https://github.com/jump-dev/MathOptInterface.jl/pull/2624)

---

<div class="post-metadata">

**Author:** ![handahye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/handahye/32/215530_2.png) [@handahye](https://discourse.julialang.org/u/handahye)\
**Post date:** [March 3, 2025, 6:20pm UTC](https://discourse.julialang.org/t/extracting-the-coefficient-of-a-nonlinear-term-in-a-jump-model/126438/3 "2025-03-03T18:20:49Z")

</div>

Many thanks for your response, @odow !
