a macro should return an expression, it should not eval an expression have you taken a look at using @devec from the Devectorize package?
On Mon, Sep 1, 2014 at 2:06 AM, John Myles White <[email protected]> wrote: > Have you tried macroexpand? > > — John > > On Aug 31, 2014, at 10:28 PM, Mykel Kochenderfer <[email protected]> > wrote: > > > I want to do a calculation like this $\max_{a \in A} \sum_{s \in S} g(s, > a)$. > > > > Of course, I can do something like this: > > maximum([sum([g(s, a) for s in S]) for a in A]) > > > > But it seems like it would be nicer to have the s in S and a in A go in > front like in the written equation. I'd like something like this: > > @max (a in A) @sum (s in S) g(s, a) > > > > So, I tried writing these macros: > > macro max(range, ex) > > eval(:(maximum($(Expr(:typed_comprehension, :Float64, ex, range))))) > > end > > macro sum(range, ex) > > eval(:(sum($(Expr(:typed_comprehension, :Float64, ex, range))))) > > end > > > > To test this, I tried: > > A = 1:10 > > S = 1:10 > > g(s, a) = s*a > > @max (a in A) @sum (s in S) g(s, a) > > > > > > I get this error: > > `convert` has no method matching convert(::Type{Float64}, > ::StepRange{Int64,Int64}) > > > > However, @max (a in A) @sum (s in S) g(s, 1) > > works just fine. My macro doesn't seem to like having the a in the sum > expression. Any tips would be appreciated! > >
