Skip to content

tkf/BroadcastableStructs.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BroadcastableStructs

Stable Dev Build Status Codecov Coveralls

BroadcastableStructs.jl provides an easy way to create a struct which can be broadcasted as an argument (BroadcastableStruct) and also as a callable (BroadcastableCallable). BroadcastableCallable supports efficient differentiation with Zygote.jl when combined with ChainCutters.jl.

BroadcastableStruct is an efficient way to treat struct-of-arrays as array-of-structs within broadcasting expressions without actually constructing the array.

julia> using BroadcastableStructs: BroadcastableStruct

julia> struct Point2D{TX, TY} <: BroadcastableStruct
           x::TX
           y::TY
       end

julia> dist(p::Point2D, q::Point2D) = sum(abs(p.x - q.x) + abs(p.y - q.y));

julia> p = Point2D(1:2, 3:4)
Point2D{UnitRange{Int64},UnitRange{Int64}}(1:2, 3:4)

julia> q = Point2D(5, 6)
Point2D{Int64,Int64}(5, 6)

julia> dist.(p, q)
2-element Array{Int64,1}:
 7
 5

BroadcastableCallable can be used to create a callable object that is broadcasted over its parameters as well as arguments.

julia> using BroadcastableStructs: BroadcastableCallable

julia> struct WeightedAdd{A, B} <: BroadcastableCallable
           a::A
           b::B
       end

julia> (f::WeightedAdd)(x, y) = f.a * x + f.b * y;

julia> WeightedAdd(1, 2)(3, 4)
11

julia> f = WeightedAdd(1:2, 3)
WeightedAdd{UnitRange{Int64},Int64}(1:2, 3)

julia> f.(4:5, 6)
2-element Array{Int64,1}:
 22
 28

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages