# How to define a symbolic vector using for loop with Symbolics.jl?

**URL:** https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262
**Category:** General Usage
**Tags:** question, package
**Created:** [October 25, 2022, 10:09pm UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262 "2022-10-25T22:09:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)
#### Post date: [October 25, 2022, 10:09pm UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/1 "2022-10-25T22:09:25Z")

</div>

Dear all,

How to define a symbolic vector using for loop with Symbolics.jl?  
For example, x=[x1, x2, x3, x4, …, x100], where xi are all variables defined using @variables.  
It can be easily done using sympy.jl as  
julia\> N=100, x = [symbols(“x$i”) for i in 1:N].

Please note that I must use Symbolics.jl for the sake of speed and complicated matrix manipulation.  
I am aware of the symbolic arrays introduced at [Symbolic arrays · Symbolics.jl](https://symbolics.juliasymbolics.org/dev/manual/arrays/),  
but it does not work.

Cheers,

Sean

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 25, 2022, 10:20pm UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/2 "2022-10-25T22:20:19Z")

</div>

This might do it:

```julia
julia> [getfield(Main, Symbol("x$i")) for i in 1:3]
3-element Vector{Num}:
 x1
 x2
 x3

```

But, perhaps it is better to go this route:

```julia
julia> @variables y[1:3]
1-element Vector{Symbolics.Arr{Num, 1}}:
 y[1:3]

```

---

<div class="post-metadata">

### Author: ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)
#### Post date: [October 25, 2022, 10:32pm UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/3 "2022-10-25T22:32:56Z")

</div>

Thanks. Neither of them work.

julia\> [getfield(Main, Symbol(“x$i”)) for i in 1:3]

ERROR: UndefVarError: x1 not defined

@variables y[1:3] does not contain variables xi.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 25, 2022, 10:42pm UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/4 "2022-10-25T22:42:25Z")

</div>

The `getfield` works if variables are defined in a global scope. If variables are defined locally, a better solution would be to do:

```julia
xvars = @variables x1 x2 x3

```

and then `xvars` already contains a vector of all the `x` vars.

The `Main` in first option is the Module name where the `xi`s are defined, if it is in another module, then replace this `Main` with the name of module or more generally:

```julia
[getfield(@ __MODULE__ , Symbol(“x$i”)) for i in 1:3]

```

As for other option, it is a suggestion to go in a different way than defining many `xi`s in a long `@variables` line.

Hopefully this clarification will help.

---

<div class="post-metadata">

### Author: ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)
#### Post date: [October 26, 2022, 1:30am UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/5 "2022-10-26T01:30:57Z")

</div>

Thanks. Defining xi using “@variables x1 x2 x3” is exactly my problem. How do use for loop with @variables to define xi?

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 26, 2022, 7:31am UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/6 "2022-10-26T07:31:52Z")

</div>

Hopefully I understood this time and:

```julia
@eval @variables $([Symbol("x$i") for i=1:3]...)

```

is what you were looking for.

---

<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: [October 26, 2022, 10:50am UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/7 "2022-10-26T10:50:33Z")

</div>

A symbolic vector and a vector of symbols are different things. For a vector of symbols, use the interpolation syntax, i.e.

```julia
sym = :x1
@variables $sym # (defines a variable x1)

```

---

<div class="post-metadata">

### Author: ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)
#### Post date: [October 26, 2022, 7:32pm UTC](https://discourse.julialang.org/t/how-to-define-a-symbolic-vector-using-for-loop-with-symbolics-jl/89262/8 "2022-10-26T19:32:28Z")

</div>

Thank you @Dan@ChrisRackauckas for your reply.  
The following post and the referred one shows how to do

> <https://github.com/JuliaSymbolics/Symbolics.jl/issues/767#issuecomment-1291476389>
>
> How to define a symbolic vector using for loop with Symbolics.jl?
> For example, …x=\[x1, x2, x3, x4, …, x100\], where xi are all variables defined using @variables.
> It can be easily done using sympy.jl as
> julia\> N=100, x = \[symbols(“x$i”) for i in 1:N\].
> 
> For short vector, x = @variables x1, x2, x3 work. However for, 100 or even 1000 long vectors, a for loop would be useful.
> 
> Please note that I must use Symbolics.jl for the sake of speed and complicated matrix manipulation.
> I am aware of the symbolic arrays introduced at \[Symbolic arrays · Symbolics.jl 1\](https://symbolics.juliasymbolics.org/dev/manual/arrays/),
> but it does not work for my purpose.
