symengine / SymEngine.jl

Julia wrappers of SymEngine
MIT License
192 stars 43 forks source link

A new macro for @vars #206

Open AmplitudeGravity opened 4 years ago

AmplitudeGravity commented 4 years ago

I write a macro for @vars when I use symengine. It can be applied to construct multi symbol variables. It seems convenience. I donot know if there are better way to realize this feature. I just leave the code here. Feel free to use or improve it.

macro vars(x,n::Int64)
    q=Expr(:block)
    for i = 1:n
        push!(q.args, Expr(:(=), esc(Symbol("$x$i")), Expr(:call, :(SymEngine._symbol), Expr(:quote, Symbol("$x$i")))))
    end
    push!(q.args, Expr(:tuple, map(esc, "$x".*map(string,1:n).|>Symbol)...))
    q
end

To use it, you can call @vars x 16 @vars α 16 The outputs are

(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16)

(α1, α2, α3, α4, α5, α6, α7, α8, α9, α10, α11, α12, α13, α14, α15, α16)

jverzani commented 4 years ago

This is also available through something like: [symbols("α$i") for i in 1:30]. I would think that is sufficient. Users might get confused as to what this does: n=16; @vars x n.

AmplitudeGravity commented 4 years ago

This is also available through something like: [symbols("α$i") for i in 1:30].

That is only realized part feature of macro @vars