Skip to content

simonbyrne/PackVec.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PackVec.jl

Build Status

Coverage Status

codecov.io

Many package interfaces expect vectors, e.g. Optim.jl and DifferentialEquations.jl.

But sometimes you want to work with more complicated objects. This provides simple functionality for packing and unpacking Julia objects into vectors.

Usage

The @pack! macro packs Julia objects into vectors:

using PackVec
vec = zeros(1 + 2 + 4 + 10) 

a = 1.0
b = [2.0, 3.0]
c = [4.0 5.0; 6.0 7.0]
d = UpperTriangular(ones(4,4)) # packs into 10 elements

@pack!(vec, a, b, c, d)

To unpack, use @unpack!: mutable objects (such as arrays) should be predefined, and immutable ones specified with a type specifier:

b = zeros(2)
c = zeros(2,2)
d = UpperTriangular(zeros(4,4))

@unpack!(vec, a::Real, b, c, d)

Mutable objects can be directly defined in the macro

@unpack!(vec, a::Real, b=zeros(2), c=zeros(2,2), d=UpperTriangular(4,4))

StaticArrays.jl is also supported:

@unpack!(vec, a::SVector{4}, b::SMatrix{3,3})

Extending

To extend this to more Julia objects, add methods to the pack! and unpack or unpack! functions. See the help for more details.

About

Pack/unpack Julia objects into vectors

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages