# Symbolics and vector

**URL:** https://discourse.julialang.org/t/symbolics-and-vector/110457
**Category:** General Usage
**Tags:** symbolics
**Created:** [February 20, 2024, 2:40pm UTC](https://discourse.julialang.org/t/symbolics-and-vector/110457 "2024-02-20T14:40:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)
#### Post date: [February 20, 2024, 2:40pm UTC](https://discourse.julialang.org/t/symbolics-and-vector/110457/1 "2024-02-20T14:40:37Z")

</div>

So in a similar way to [this post](https://discourse.julialang.org/t/symbolics-jl-jacobian/64654) I wonder why I am getting the following:

```julia
using Symbolics
@variables x[1:5] y

ex1 = x[1]^2+3*x[5]+x[3]*y
ex2 = x[1]*x[2]*x[4]^2+10

Symbolics.jacobian([ex2], x[1:5]) ###Works, gives : x[2]*(x[4]^2) x[1]*(x[4]^2) 0 2x[1]*x[2]*x[4] 0
Symbolics.jacobian([ex2], x) ###Works, gives : x[2]*(x[4]^2) x[1]*(x[4]^2) 0 2x[1]*x[2]*x[4] 0
Symbolics.jacobian([ex1], x) ###Works, gives 2x[1] 0 y 0 3
Symbolics.jacobian([ex1], [x, y]) ###Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64)
Symbolics.jacobian([ex1], [x[1:5], y]) ####Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64)
Symbolics.jacobian([ex1], [x[1],x[2],x[3],x[4],x[5], y]) ###Works, gives: 2x[1] 0 y 0 3 x[3]
Symbolics.jacobian([ex1], [x[1], y]) ###Works, gives: 2x[1] x[3]
Symbolics.jacobian([ex1], [x[1:2], y]) ###Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64)

###############Different definition of the vector x, found [here](https://github.com/JuliaSymbolics/Symbolics.jl/issues/767#issuecomment-1291476389)
@variables y
x= Symbolics.variables(:x, 1:5) 

ex1 = x[1]^2+3*x[5]+x[3]*y
ex2 = x[1]*x[2]*x[4]^2+10

Symbolics.jacobian([ex2], x[1:5]) ###Works, gives x₂*(x₄^2) x₁*(x₄^2) 0 2x₁*x₂*x₄ 0
# Symbolics.jacobian([ex2], x) ###Works, gives x₂*(x₄^2) x₁*(x₄^2) 0 2x₁*x₂*x₄ 0
# Symbolics.jacobian([ex1], x) ###Works, gives 2x₁ 0 y 0 3
# Symbolics.jacobian([ex1], [x, y]) ###Gives 0 x₃
# Symbolics.jacobian([ex1], [x[1:5], y]) ####Gives 0 x₃
# Symbolics.jacobian([ex1], [x[1],x[2],x[3],x[4],x[5], y]) ####Works, gives 2x₁ 0 y 0 3 x₃
# Symbolics.jacobian([ex1], [x[1], y]) ###Gives 2x₁ x₃
# Symbolics.jacobian([ex1], [x[1:2], y]) ###Gives 0 x₃

```

Two questions:

- Is there a way to avoid writing the vector explicitly to get the jacobian? With five elements it’s OK, but more will be rather tedious.
- Which way of defining vector variables is better, `x= Symbolics.variables(:x, 1:5) ` or `@variables x[1:5]`?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 23, 2024, 3:00pm UTC](https://discourse.julialang.org/t/symbolics-and-vector/110457/2 "2024-02-23T15:00:27Z")

</div>

> [@blob](#):
>
> Which way of defining vector variables is better, `x= Symbolics.variables(:x, 1:5) ` or `@variables x[1:5]`?

Neither is necessarily “better”, they are just different representations.

Instead of just answering here, I improved the docs. See:

> **[Symbolic Arrays · Symbolics.jl](https://symbolics.juliasymbolics.org/stable/manual/arrays/)**
>
> Documentation for Symbolics.jl.

Let me know if you have more questions.

---

<div class="post-metadata">

### Author: ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)
#### Post date: [February 24, 2024, 12:54pm UTC](https://discourse.julialang.org/t/symbolics-and-vector/110457/3 "2024-02-24T12:54:09Z")

</div>

Thanks! The documentation was quite helpful and now the example makes more sense:

```julia
#######Differences
@variables x[1:5] y
ex1 = x[1]^2+3*x[5]+x[3]*y
# Symbolics.jacobian([ex1], [x, y]) ###Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64) - no jacobian for an entire array x
Symbolics.jacobian([ex1], vcat(x, y)) ###Works, gives 2x₁ 0 y 0 3 x₃

@variables y
x= Symbolics.variables(:x, 1:5) 
ex1 = x[1]^2+3*x[5]+x[3]*y
Symbolics.jacobian([ex1], [x, y]) ###Gives 0 x₃ - there is no element x in [x,y] here, so we get zero
Symbolics.jacobian([ex1], vcat(x, y)) ###Works, gives 2x₁ 0 y 0 3 x₃

```

For future reference, the problem from the initial post (how to do Jacobians without writing out all the elements) turned out to be my expectation of Matlab-like behaviour of `[]`:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/b/bbc65f7873de4137e38f871802581852a10afab7.png)  
and  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/6/36569f019cb62869593d9f385e8bd3e19070aba2.png)
