Re: [julia-users] Create local function variables programmatically from a dictionnary

2016-01-28 Thread Fady Shoukry


On Thursday, 28 January 2016 08:12:06 UTC-5, Stefan Karpinski wrote:
>
> Please try to avoid this business of creating the same conversation in 
> three places.
>
> On Thu, Jan 28, 2016 at 8:10 AM, Stefan Karpinski <ste...@karpinski.org 
> > wrote:
>
>>
>> http://stackoverflow.com/questions/35051773/create-local-variables-programmatically-from-a-dictionary-in-julia
>>
>> On Thu, Jan 28, 2016 at 8:10 AM, Stefan Karpinski <ste...@karpinski.org 
>> > wrote:
>>
>>> https://groups.google.com/forum/#!topic/julia-users/axwQqFeIlCQ
>>>
>>> On Wed, Jan 27, 2016 at 11:05 PM, Yichao Yu <yyc...@gmail.com 
>>> > wrote:
>>>
>>>> On Wed, Jan 27, 2016 at 9:47 PM, Fady Shoukry <fadyms...@gmail.com 
>>>> > wrote:
>>>> >
>>>> >>
>>>> >> Depending on what API JuMP provide and what API you want to provide, 
>>>> I
>>>> >> think you should either keep the dict (if JuMP can handle it) or use
>>>> >> meta programing to construct an AST/function based on the user input
>>>> >> and evaluate that.
>>>> >
>>>> >
>>>> > Thanks for the quick reply. Could you elaborate on that last part 
>>>> regarding
>>>> > constructing an AST/function (with an example if possible)?
>>>> >
>>>>
>>>> A simple example that hopefully shows what I meant,
>>>>
>>>>
>>>> ```
>>>> julia> vars = [:a, :b]
>>>> 2-element Array{Symbol,1}:
>>>>  :a
>>>>  :b
>>>>
>>>> julia> expr = :(a + b)
>>>> :(a + b)
>>>>
>>>> julia> function make_function(name, vars, expr)
>>>>:(function $name()
>>>>$([:($v = 1) for v in vars]...)
>>>>$expr
>>>>  end)
>>>>end
>>>> make_function (generic function with 1 method)
>>>>
>>>> julia> func_ast = make_function(:new_function, vars, expr)
>>>> :(function new_function() # none, line 3:
>>>> a = 1
>>>> b = 1 # none, line 4:
>>>> a + b
>>>> end)
>>>>
>>>> julia> eval(func_ast)
>>>> new_function (generic function with 1 method)
>>>>
>>>> julia> new_function()
>>>> 2
>>>>
>>>> julia> @code_warntype new_function()
>>>> Variables:
>>>>   a::Int64
>>>>   b::Int64
>>>>
>>>> Body:
>>>>   begin  # none, line 3:
>>>>   a = 1
>>>>   b = 1 # none, line 4:
>>>>   return (Base.box)(Int64,(Base.add_int)(a::Int64,b::Int64))
>>>>   end::Int64
>>>> ```
>>>>
>>>
>>>
>>
>
Apologies, the second Google groups post was posted by mistake and is 
incomplete and the stackoverflow question (by a colleague of mine) was in 
hopes to get as many opinions as possible, everyone is very responsive and 
see I see that was unnecessary, my bad.

Thanks for the valuable input, I will look further into the JuMP API as per 
your comment. Otherwise, it saves time to know that this is not doable by 
design. Just out of curiosity, is there a specific reason why this is not a 
feature you would want to allow?


[julia-users] Programmatically creating variables from a dictionary

2016-01-27 Thread Fady Shoukry
Hey everyone,

As the subject line suggests, I am trying to programmatically create 
variables from a supplied dictionary in a function. I am aware that I can 
use eval() but eval will generate global variables which is something I am 
trying to avoid for its performance disadvantages. Here is an example of 
what I am trying to accomplish.

function defVariables(dict::Dict{Symbol}{Any})
# This function should define the variables as follows:



[julia-users] Create local function variables programmatically from a dictionnary

2016-01-27 Thread Fady Shoukry
Hi everyone, 

As the title suggests, I am trying to create local variables to a function 
for a supplied dictionary argument. The Dict would be of type {Symbol, Any} 
and the goal is to create a variable for each symbol that has the 
corresponding value. I am aware that I could use eval in something like:

for (k, v) in dict
eval(:($k = $v))
end

However, eval defined its variables in the global scope which has 
performance repercussions if I understand correctly, so I am trying to 
avoid that.

I will add a bit of context in case you have suggestions that are 
drastically different than the path I am going for. I am doing this as I am 
implementing a wrapper around the JuMP JuliaOpt module allowing the users 
to dynamically create optimization models with arbitrary constraint 
expressions. Since the constraint expressions are defined using symbols, 
and JuMP requires those symbols to be defined in the current scope, I want 
the user to provide those symbols and their values in a dictionary to the 
function. Defining the symbols globally would be cumbersome as the user 
should ideally be able to run the function multiple times with the same 
constraint expressions (i.e. same symbols) but different values. 

I appreciate your help!


Re: [julia-users] Create local function variables programmatically from a dictionnary

2016-01-27 Thread Fady Shoukry


>
> Depending on what API JuMP provide and what API you want to provide, I 
> think you should either keep the dict (if JuMP can handle it) or use 
> meta programing to construct an AST/function based on the user input 
> and evaluate that. 
>

Thanks for the quick reply. Could you elaborate on that last part regarding 
constructing an AST/function (with an example if possible)?