Declare arbitrary number of variables to create the ring of polynomials julia

Viewed 30

I would like to have an algorithm for a n=a (where a is any number that I choose), such that Julia understands that I declare the monomial variables x_{1}...x_{a} with @polyvar

Which package do you recommend me to do this?TypedPolynomials?

Thank you for your help

1 Answers

Normally you almost always use Vectors for things like this:

julia> a=3
3

julia> x = [n for n in 1:a]
3-element Vector{Int64}:
 1
 2
 3

julia> x[2]
2

It is very hard to think about a scenario when it is not the best idea.

However, if you really need you can do a macro. The rule of thumb with Julia macros is "if you do not know how to write it perhaps you do not need it". Anyway here it is:

julia> macro var(n, initval)
           exs = [ :( $(Symbol("x_",i)) = $initval ) for i in 1:n ]
           esc(Expr(:block, exs...))
       end
@var (macro with 1 method)

julia> @var(5, 77)
77

julia> x_4
77
Related