# Nonlinear expressions may contain only scalar

**URL:** <https://discourse.julialang.org/t/nonlinear-expressions-may-contain-only-scalar/80721>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [May 8, 2022, 8:35pm UTC](https://discourse.julialang.org/t/nonlinear-expressions-may-contain-only-scalar/80721 "2022-05-08T20:35:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [May 8, 2022, 9:05pm UTC](https://discourse.julialang.org/t/nonlinear-expressions-may-contain-only-scalar/80721/2 "2022-05-08T21:05:16Z")

</div>

Welcome to JuMP!

Quite a few things:

- You can use linear algebra (e.g., `b' * e`) in `@constraint`, but not `@NLconstraint`. It’s annoying, but we’re [working on fixing it](https://jump.dev/announcements/2022/02/21/lanl/).
- If the terms are linear or quadratic, you can use `@constraint`. You only need `@NLconstraint` for things other than linear and quadratic.
- Your syntax for PSD constraints is slightly wrong
- Ipopt doesn’t support PSD constraints

Here’s how I would re-write your constraints

```julia
using JuMP
s, e = 2, [1, 1]
model = Model()
@variable(model, a[1:s, 1:s] >= 0)
@variable(model, r[1:s, 1:s], Symmetric)
@variable(model, b[1:s] >= 0)

# You can use linear algebra in `@constraint`
@constraint(model, c1, b' * e == 1)
# but not in `@NLconstraint`
# @NLconstraint(model, c2, b' * c == 1/2)
# What is c???
# @NLconstraint(model, c2, sum(b[i] * c[i] for i in 1:s) == 1/2)

# @NLconstraint(model, c3, b' * (a*e).^2 == 1/3)
a_e = @expression(model, a * e)
@NLconstraint(model, c3, sum(b[i] * a_e[i]^2 for i in 1:s) == 1 / 3)

# @NLconstraint(model, c4, b' * a * (a*e) ==1/6)
a_a_e = @expression(model, a * a * e)
@NLconstraint(model, c4, sum(b[i] * a_a_e[i] for i in 1:s) == 1 / 6)

# @NLconstraint(model, c5, r*a + a'*r -b*b',PSD)
X = r * a + a' * r - b * b'
@constraint(model, c5, Symmetric(X) >= 0, PSDCone())

```

The `c5` constraint is going to become a problem. I don’t know a solver which would support quadratic in PSD cone. and especially when it isn’t positive semidefinite.

---

_[View the full topic](https://discourse.julialang.org/t/nonlinear-expressions-may-contain-only-scalar/80721)._
