# NCP-function implementation (ifelse)

**URL:** <https://discourse.julialang.org/t/ncp-function-implementation-ifelse/30411>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [October 28, 2019, 5:48pm UTC](https://discourse.julialang.org/t/ncp-function-implementation-ifelse/30411 "2019-10-28T17:48:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SingingKim](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@SingingKim](https://discourse.julialang.org/u/SingingKim)\
**Post date:** [October 28, 2019, 5:48pm UTC](https://discourse.julialang.org/t/ncp-function-implementation-ifelse/30411/1 "2019-10-28T17:48:06Z")

</div>

Hi, I’m trying to implement a model with constraints based on an NCP-function (continuous & differentiable) and put it into an NLP solver such as IPOPT:

```julia
@constraint(m, f(x,y) <= 0)

```

where

```julia
f(x,y) := xy if x+y >= 0
          -0.5(x^2 + y^2 ) if x+y < 0 

```

Can I get some hints on how to implement such constraints on JuMP if it’s possible?

---

<div class="post-metadata">

**Author:** ![Paul\_Schrimpf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_schrimpf/32/10134_2.png) [@Paul\_Schrimpf](https://discourse.julialang.org/u/Paul_Schrimpf)\
**Post date:** [October 28, 2019, 6:21pm UTC](https://discourse.julialang.org/t/ncp-function-implementation-ifelse/30411/2 "2019-10-28T18:21:08Z")

</div>

You could write a normal Julia function for `f`, and then call `JuMP.register` and `@NLconstraint`, see [Nonlinear Modeling — JuMP -- Julia for Mathematical Programming 0.13 documentation](http://www.juliaopt.org/JuMP.jl/v0.13/nlp.html#user-defined-functions)

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [October 28, 2019, 6:24pm UTC](https://discourse.julialang.org/t/ncp-function-implementation-ifelse/30411/3 "2019-10-28T18:24:07Z")

</div>

`@constraint(model, ifelse(x + y >= 0, x*y, -0.5*(x^2 + y^2)) <= 0)` is another way. If you can avoid user-defined functions, it’s better to do so because registering multivariate user-defined functions disables hessian computations.

---

<div class="post-metadata">

**Author:** ![SingingKim](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@SingingKim](https://discourse.julialang.org/u/SingingKim)\
**Post date:** [October 28, 2019, 7:28pm UTC](https://discourse.julialang.org/t/ncp-function-implementation-ifelse/30411/4 "2019-10-28T19:28:46Z")

</div>

This works great. Thank you very much!
