I am a bit new to programming and am currently working with solving optimization problems in JuMP in Julia.

I got a tip from the JuMP page that I should also use @constraint if a term is quadratic. However, how do I know if a constraint is quadratic?

For instance, should I use @constraint or @NLconstraint for $z - 2 + 4 z y = 0$ and $x - z^2 \geq 0$?

All I know is that it should have the form $\frac{1}2 x H x + q x$ where $x$ is the vector with all the variables.

  • $\begingroup$ 1/2 x * H * x is just a fancy way to write $\sum_{j_1} \sum_{j_2} h_{j_1,j_2} x_{j_1} x_{j_2}$ $\endgroup$
    – fontanf
    Sep 23 at 13:48
  • $\begingroup$ If you're looking for solvers, note that quadratic constraints mean that your model is not a quadratic programming problem. You need a QPQC solver, or nonlinear programming solver $\endgroup$ Sep 23 at 15:41

1 Answer 1


A "quadratic constraint" is a constraint of the form $f(x) \leq 0$, where $f(x)$ is a quadratic function, i.e., as you wrote, $$ f(x) = \frac{1}{2}x^{T}Hx + q^{T}x + b $$ for some square matrix $H \in \mathbb{R}^{n \times n}$, vector $q \in \mathbb{R}^{n}$, and some scalar $b \in \mathbb{R}$.

In JuMP, you can declare such quadratic constraints with the @constraint syntax directly. The @NLconstraint macro is designed for more general nonlinear that require special handling of function evaluation and derivatives. For instance, if your constraints have high-order polynomial functions ($x^{3}$ and above), or terms like sin, cos, log or exp, you should use @NLconstraint.

You can have a look at the quadratic portfolio optimization and quadratically constrained programming examples from the JuMP documentation.

PS: there is more JuMP-centric traffic on the Julia Discourse forum. You are more likely to get expert feedback there, especially on code-related questions.

  • $\begingroup$ You can build higher order polynomials via quadratic constraints though or use PolyJuMP compared to the general purpose @NLConstraints. This can be useful to make some solvers like Gurobi solve problems they won't otherwise as @NLConstraints is not supported by some, however quadratic terms are. $\endgroup$ Sep 22 at 15:57
  • $\begingroup$ To suggested editor: No one letter edits, leave the blame with the prior editor and reviewer (who missed too); don't compound their mistake with another. --- Edit reason (excuse) you offered: " summary: Fixed typo. Embedding URLs is better than inlining them", your personal preference to undo the embedding and place the URLs at the end doesn't override SE's preference that they be inlined nor allow such an excuse to be used to go around the character minimum; all these things are for reasons I don't need to agree with, but do need to follow; or write a meta asking for it be changed. $\endgroup$
    – Rob
    Sep 24 at 23:50
  • $\begingroup$ My reason to reject, yesterday, was: or.stackexchange.com/review/suggested-edits/4014 - due to en.wikipedia.org/wiki/… the author should have been consulted with a comment, rather than editing and approving; it could have been left with the one reviewer and awaiting author approval. $\endgroup$
    – Rob
    Sep 24 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.