How amazing! I have been about to post the very same question to this list! 
So, I wouldn't say it's a stupid question at all :)

I believe this kind of packing/unpacking of several heterogeneous (in 
structure, not in type) parameters appear a lot in modeling and 
optimization.
This probably is a question transversal to many (all) programming 
languages, but after porting some code of mine from Python to Julia this 
issue is now hitting me as the major bottleneck.

I need to pack several parameters which are scalars, vectors and symmetric 
matrices of fixed size.
My first attempt was to define a generic packing function to map from a 
Dict of symbol=>parameter to the "vector form" which used a pack function 
dispatched on the parameters type. The vector form was exposed externally 
to the optimization routines and the Dict form was used internally in the 
model. The problem of that format approach was that Julia base types were 
not expressive enough, for example, to know the size of a Vector parameter 
or to know that a Matrix was symmetric and thus only a set of its unique 
elements must be packed to the vector form.

I then thought about creating my own Types, but that would represent a huge 
task not worth it. If it were possible to delegate methods from a type to 
one of its fields (https://github.com/JuliaLang/julia/pull/3292) that would 
be a much  easier task:
    immutable SymmetricMatrix{T}
        a::Matrix{T}
    end
    @somehow_delegate_methods SymmetricMatrix --> SymmetricMatrix.a

I ended up defining a 'format' array made of (Symbol, Symbol) pairs were 
the first symbol is the parameter name and the second symbol is just a 
label indicating the type of the parameter over which some unpack/pack are 
dispatched (if interested see 
https://github.com/cdsousa/Robotics.jl/blob/master/src/dynparams.jl).

Anyway I still have a feeling that this is suboptimal, and I wonder what 
are the common practices in other languages.
I even did a rough search on StackOverflow but found nothing so far...



On Friday, March 7, 2014 2:15:58 PM UTC, Yuuki Soho wrote:
>
> It's a bit of a stupid question, but I don't really know how to deal with 
> this efficiently.
>
> So, in many application I have some model with parameters, and I want to 
> the able to change the number of parameters, or they order easily.
>
> For passing parameters to functions I want to pack them into a vector p, 
> such that I don't have huge function definition, but inside
>
> the function's body I'd prefer to have all the parameters given by their 
> name, so I can use them in equations (instead of using p[1], p[2], ...).
>
>
> I can write two functions p = pack(a,b,c) and (a,b,c) = unpack(p) but 
> that's pretty restrictive because if you add or remove a parameters, I have 
> to change all 
>
> my function calls and definition. If I add another model I also need to 
> write another pack and unpack pairs.
>
>
> Is there an better approach to do this in Julia ? I was thinking maybe 
> doing a macro @unpack p that would spawn all the variables needed, but I'm 
> not
>
> sure that's the right way to do it.
>

Reply via email to