-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
getting closer to JuliaLang/julia#7568
- Loading branch information
1 parent
9216430
commit 2e8fec6
Showing
7 changed files
with
248 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module FixedSizeArrays | ||
|
||
include("core.jl") | ||
include("staged.jl") | ||
include("ops.jl") | ||
include("array_of_fixedsize.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
immutable FixedUnitRange{From, To} end | ||
immutable FixedStepRange{From, Step, To} end | ||
immutable LOL | ||
x::Float32 | ||
abstract FixedSizeVector{T, N} | ||
|
||
gen_fixedsizevector_type(name::DataType, T::Symbol, N::Int) = gen_fixedsizevector_type(symbol(string(name.name.name)), T, N) | ||
function gen_fixedsizevector_type(name::Symbol, T::Symbol, N::Int) | ||
fields = [Expr(:(::), symbol("I_$i"), T) for i = 1:N] | ||
typename = symbol("FS$name") | ||
eval(quote | ||
immutable $(typename){$T} <: $(name){$T} | ||
$(fields...) | ||
end | ||
end) | ||
typename | ||
end | ||
Base.endof(a::LOL) = 1 | ||
Base.getindex{From, To}(a::LOL, b::FixedUnitRange{From, To}) = println(From, " ", To) | ||
Base.getindex(a::LOL, b::UnitRange) = a[FixedUnitRange{first(b), last(b)}()] | ||
a(x::Colon) = println(x) | ||
a(:) | ||
LOL(1f0)[:] | ||
|
||
stagedfunction Base.call{T <: FixedSizeVector, ET}(t::Type{T}, data::ET...) | ||
Tsuper, Nsuper = super(T).parameters | ||
N = length(data) | ||
@assert Nsuper == N "not the right dimension" | ||
typename = gen_fixedsizevector_type(T, symbol(string(Tsuper.name)), N) | ||
original_typename = t.name.name | ||
:($(original_typename)($(typename)(data...))) | ||
end | ||
|
||
immutable LOL{T <: FixedSizeVector{Real, 3}} <: FixedSizeVector{Real, 3} | ||
data::T | ||
end | ||
|
||
@show LOL(1,2,3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +0,0 @@ | ||
immutable IMax <: Base.Func{2} end | ||
call(::IMax, x, y) = max(x,y) | ||
immutable IMin <: Base.Func{2} end | ||
call(::IMin, x, y) = min(x,y) | ||
Base.maximum{T}(v::Vector{Vector3{T}}) = reduce(IMax(), Vector3(typemin(T)), v) | ||
Base.minimum{T}(v::Vector{Vector3{T}}) = reduce(IMin(), Vector3(typemax(T)), v) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
mportall Base | ||
|
||
abstract FixedSizeArray{T, SZ, N} | ||
abstract FixedSizeVector{T, N} <: FixedSizeArray{T, (N,), 1} | ||
|
||
#Constructor | ||
call{T <: FixedSizeVector, ET}(t::Type{T}, data::ET...) = t(ntuple(x->data[x], length(data))) | ||
stagedfunction call{T <: FixedSizeVector, N, ET}(t::Type{T}, data::NTuple{N, ET}) | ||
Tsuper, Nsuper = super(T).parameters | ||
@assert Nsuper == N "not the right dimension" | ||
typename = gen_fixedsizevector_type(T, Tsuper.name, N) | ||
:($typename(data...)) | ||
end | ||
gen_fixedsizevector_type(name::DataType, T::Symbol, N::Int) = gen_fixedsizevector_type(symbol(string(name.name.name)), T, N) | ||
function gen_fixedsizevector_type(name::Symbol, T::Symbol, N::Int) | ||
fields = [Expr(:(::), symbol("I_$i"), T) for i = 1:N] | ||
typename = symbol("FS$name") | ||
eval(quote | ||
immutable $(typename){$T} <: $(name){$T} | ||
$(fields...) | ||
end | ||
end) | ||
typename | ||
end | ||
|
||
# Usual functions | ||
getindex{T <: FixedSizeVector}(c::T, i::Int) = c.(i) | ||
# Ugly workaround for not having triangular dispatch: | ||
eltype{T <: FixedSizeVector}(A::Type{T}) = A.types[1] | ||
length{T <: FixedSizeVector}(A::Type{T}) = length(A.types) | ||
ndims{T <: FixedSizeVector}(A::Type{T}) = 1 | ||
size{T <: FixedSizeVector}(A::Type{T}) = (length(A),) | ||
size{T <: FixedSizeVector}(A::Type{T}, d::Integer) = (length(A),) # should throw an error!? | ||
|
||
# The usual vector functions | ||
abstract Func{Name, N} | ||
stagedfunction map{T <: FixedSizeVector}(f::Func{1}, a::T) | ||
typename = symbol("$(a.name.name)") | ||
arguments = [:(f(a.($i))) for i=1:length(T)] | ||
:($typename($(arguments...))) | ||
end | ||
stagedfunction map{T <: FixedSizeVector, FName}(f::Func{FName, 2}, a::T, b::T) | ||
typename = symbol("$(a.name.name)") | ||
arguments = [:($FName(a.($i), b.($i))) for i=1:length(T)] | ||
:($typename($(arguments...))) | ||
end | ||
|
||
const binaryOps = (:.+, :.-,:.*, :./, :.\, :.^,:*,:/, | ||
:.==, :.!=, :.<, :.<=, :.>, :.>=, :+, :-, | ||
:min, :max, | ||
:div, :fld, :rem, :mod, :mod1, :cmp, | ||
:atan2, :besselj, :bessely, :hankelh1, :hankelh2, | ||
:besseli, :besselk, :beta, :lbeta) | ||
for op in binaryOps | ||
const unicsymb = gensym() | ||
eval(quote | ||
immutable $unicsymb <: Func{symbol(string($op)), 2} end | ||
$op{T <: FixedSizeVector}(x::T, y::T) = map($unicsymb(), x, y) | ||
end) | ||
end | ||
|
||
# Some syntactic sugar for accessing named dimensions | ||
abstract Dimension{T} <: Number | ||
stagedfunction getindex{FT <: FixedSizeVector, T <: Dimension}(a::FT, key::Type{T}) | ||
index = fieldindex(a, T) | ||
:(T(a[$index])) | ||
end | ||
macro accessors(typ, fields) | ||
result = Any[] | ||
for elem in fields.args | ||
push!(result, quote | ||
fieldindex{T <: $typ}(::Type{T}, ::Type{$(elem.args[1])}) = $(elem.args[2]) | ||
end) | ||
end | ||
esc(Expr(:block, result...)) | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using FixedSizeArrays | ||
|
||
|
||
square(x) = x*x | ||
|
||
const unaryOps = (-, ~, conj, abs, | ||
sin, cos, tan, sinh, cosh, tanh, | ||
asin, acos, atan, asinh, acosh, atanh, | ||
sec, csc, cot, asec, acsc, acot, | ||
sech, csch, coth, asech, acsch, acoth, | ||
sinc, cosc, cosd, cotd, cscd, secd, | ||
sind, tand, acosd, acotd, acscd, asecd, | ||
asind, atand, radians2degrees, degrees2radians, | ||
log, log2, log10, log1p, exponent, exp, | ||
exp2, expm1, cbrt, sqrt, square, erf, | ||
erfc, erfcx, erfi, dawson, ceil, floor, | ||
trunc, round, significand, lgamma, hypot, | ||
gamma, lfact, frexp, modf, airy, airyai, | ||
airyprime, airyaiprime, airybi, airybiprime, | ||
besselj0, besselj1, bessely0, bessely1, | ||
eta, zeta, digamma) | ||
|
||
# vec-vec and vec-scalar | ||
const binaryOps = (.+, .-,.*, ./, .\, .^,*,/, | ||
.==, .!=, .<, .<=, .>, .>=, +, -, | ||
min, max, | ||
div, fld, rem, mod, mod1, cmp, | ||
atan2, besselj, bessely, hankelh1, hankelh2, | ||
besseli, besselk, beta, lbeta) | ||
|
||
const reductions = (sum, prod, minimum,(maximum) | ||
testresult = Dict{Function, Any}() | ||
|
||
function Base.filter(fun, x...) | ||
result = [] | ||
@assert length(unique(map(length, x))) == 1 "all iterables need to have the same length. Lengths given: $(map(length, x))" | ||
for i=1:length(x[1]) | ||
args = ntuple(j-> x[j][i], length(x)) | ||
fun(args...) && push!(result, args) | ||
end | ||
result | ||
end | ||
|
||
@show filter(!=, [2,3,4,5], [2,1,4,7]) | ||
|
||
Base.call{FS <: AbstractFixedSizeArray, T, N}(::Type{FS}, a::Array{T, N}) = AbstractFixedSizeArray{T, N, size(a)}(a...) | ||
Base.call{FS <: AbstractFixedSizeArray, T, N}(::Type{FS}, a::Array{T, N}) = AbstractFixedSizeArray{T, N, size(a)}(a...) | ||
|
||
|
||
|
||
nvec{T, N}(x::Array{T,N}) = AbstractFixedSizeArray(x) | ||
|
||
|
||
function testunaray() | ||
baseline = Any[rand(2), rand(3), rand(3,7), rand(7,2), rand(Float32, 4, 1), rand(Float32, 4), rand(Float64, 4,4)] | ||
test = map(nvec, baseline) | ||
for op in unaryOps | ||
baeline_result = map(op, baseline) | ||
test_result = map(op, test) | ||
|
||
if all(map(==, baeline_result, test_result)) | ||
testresult[op] = "passed" | ||
else | ||
testresult[op] = ["didn't pass: ", filter(!=, baseline_result, test_result)] | ||
end | ||
end | ||
|
||
end |