# Defining constraints using JuMP

**URL:** <https://discourse.julialang.org/t/defining-constraints-using-jump/69659>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 13, 2021, 6:35am UTC](https://discourse.julialang.org/t/defining-constraints-using-jump/69659 "2021-10-13T06:35:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FH96](https://avatars.discourse-cdn.com/v4/letter/f/4da419/32.png) [@FH96](https://discourse.julialang.org/u/FH96)\
**Post date:** [October 13, 2021, 6:35am UTC](https://discourse.julialang.org/t/defining-constraints-using-jump/69659/1 "2021-10-13T06:35:39Z")

</div>

while defining constraints in a nonlinear optimization problem ,I face this error:  
“MethodError: no method matching \*(::DataFrame, ::Vector{VariableRef}))”

can anyone help?

here’s part of my code:

````julia
s_matrix_df=DataFrames.DataFrame(s_matrix)
@variable(model,v[1:(size(s_matrix_df,2)-1)]>=0)
@constraint(model,s_matrix_df[:,2:end]*v ==0)```
````

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [October 13, 2021, 7:05am UTC](https://discourse.julialang.org/t/defining-constraints-using-jump/69659/2 "2021-10-13T07:05:59Z")

</div>

A couple of questions:

- Why do you wrap your matrix in a `DataFrame` if you then use it as a matrix afterwards? I don’t think that linear algebra on dataframes is supported in this way.
- Your constraint asks that a matrix-vector product (result being a vector) should equal 0 (a scalar). This is a dimension mismatch, so you could use a zero vector on the right-hand side, or use `.==` for _broadcasting_.
- You can also make the matrix-vector product explicit with the `dot` function from `LinearAlgebra`.

---

<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:** [October 13, 2021, 7:38am UTC](https://discourse.julialang.org/t/defining-constraints-using-jump/69659/3 "2021-10-13T07:38:15Z")

</div>

Documentation: [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Vectorized-constraints)

---

<div class="post-metadata">

**Author:** ![FH96](https://avatars.discourse-cdn.com/v4/letter/f/4da419/32.png) [@FH96](https://discourse.julialang.org/u/FH96)\
**Post date:** [October 13, 2021, 12:39pm UTC](https://discourse.julialang.org/t/defining-constraints-using-jump/69659/4 "2021-10-13T12:39:55Z")

</div>

First of all thanks a lot for noticing my question 🙏  
I’m new to julia and JuMP and all my experience using them has been just a couple of days

Here are my answers to your questions:

1.This matrix was a csv file which I read using `CSV` and converted it to a `DataFrame` because in other constraints I need to select some special columns (based on column name).Having worked with pandas in python , I did so. If there is a better way of doing this please inform me.

2.The error I’ve mentioned is not related to the `.` as I played with the code and deleted and wrote the dot ,but I got same error!(I’ve just accidentally copied the “without dot” version here)

3.Thank you so much ,`dot` worked for me!!

there’s a problem with another constraint which is nonlinear:  
“Unexpected array VariableRef in nonlinear expression. nonlinear expressions may contain only scalar expressions”

here is the constraint:  
`@NLconstraint(model,[sum(c[j]*v[indx_of_P[j]]) for j in 1:length(c)] ==uptake*g )`

I tried to define a function, but it didn’t work  
`f(x,y)=sum(x[j]*y[indx_of_P[j]] for j in 1:length(x))`  
` @NLconstraint(model,f(c,v)==uptake*g )`

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [October 13, 2021, 1:43pm UTC](https://discourse.julialang.org/t/defining-constraints-using-jump/69659/5 "2021-10-13T13:43:42Z")

</div>

Re 2: I expected this error to show up “next”.

For your new question: I think you’ll need to create multiple constraints, one for each component. This is also mentioned in a [later part of the documentation](https://jump.dev/JuMP.jl/stable/manual/nlp/#Scalar-operations-only).
