SymEngine.jl icon indicating copy to clipboard operation
SymEngine.jl copied to clipboard

A new macro for @vars

Open AmplitudeGravity opened this issue 5 years ago • 2 comments

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)

AmplitudeGravity avatar Apr 13 '20 14:04 AmplitudeGravity

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.

jverzani avatar Apr 13 '20 15:04 jverzani

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

That is only realized part feature of macro @vars

AmplitudeGravity avatar Apr 14 '20 04:04 AmplitudeGravity