Skip to content

Commit

Permalink
Several changes suggested in the review by Tony Kelman @tkelman
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Oct 28, 2016
1 parent f9dbffc commit c274f18
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 77 deletions.
10 changes: 5 additions & 5 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ end

import Base: A_mul_B!, A_mul_Bc!, A_mul_Bc, A_mul_Bt!, A_mul_Bt, Ac_mul_B, Ac_mul_B!, Ac_mul_Bc, Ac_mul_Bc!, At_mul_B, At_mul_B!, At_mul_Bt, At_mul_Bt!

if VERSION >= v"0.5.0-dev" ## v0.4 ambiguity-hell with AbstractTriangular c.s.
if VERSION v"0.5.0" ## v0.4 ambiguity-hell with AbstractTriangular c.s.
## Assume dimensions/names are correct
for op in (:A_mul_B!, :A_mul_Bc!, :A_mul_Bt!, :Ac_mul_B!, :Ac_mul_Bc!, :At_mul_B!, :At_mul_Bt!)
@eval ($op)(C::NamedMatrix, A::AbstractMatrix, B::AbstractMatrix) = ($op)(C.array, A, B)
Expand Down Expand Up @@ -125,10 +125,10 @@ end
\{Tx<:Number,Ty<:Number}(x::Diagonal{Tx}, y::NamedVector{Ty}) = x \ y.array
\{Tx<:Number,Ty<:Number}(x::Union{Bidiagonal{Tx},LinAlg.AbstractTriangular{Tx}}, y::NamedVector{Ty}) = x \ y.array
\{Tx<:Number,Ty<:Number}(x::Union{Bidiagonal{Tx},LinAlg.AbstractTriangular{Tx}}, y::NamedMatrix{Ty}) = NamedArray(x \ y.array, (defaultnamesdict(size(x,1)), y.dicts[2]), (:A, y.dimnames[2]))
if VERSION >= v"0.4.0-dev"
\(x::Bidiagonal,y::NamedVector) = NamedArray(x \ y.array, ([string(i) for i in 1:size(x,2)], names(y,2)), (:A, y.dimnames[2]))
\(x::Bidiagonal,y::NamedMatrix) = NamedArray(x \ y.array, ([string(i) for i in 1:size(x,2)], names(y,2)), (:A, y.dimnames[2]))
end

\(x::Bidiagonal,y::NamedVector) = NamedArray(x \ y.array, ([string(i) for i in 1:size(x,2)], names(y,2)), (:A, y.dimnames[2]))
\(x::Bidiagonal,y::NamedMatrix) = NamedArray(x \ y.array, ([string(i) for i in 1:size(x,2)], names(y,2)), (:A, y.dimnames[2]))

## AbstractVectorOrMat gives us more ambiguities than separate entries...
\(x::AbstractVector, y::NamedVector) = x \ y.array
\(x::AbstractMatrix, y::NamedVector) = x \ y.array
Expand Down
2 changes: 1 addition & 1 deletion src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ if VERSION < v"0.5.0-dev+2023"
displaysize(io::IO) = Base.tty_size()
end

if VERSION < v"0.5.0-dev"
if !isdefined(Core, :String)
String = ASCIIString
end
8 changes: 3 additions & 5 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ defaultdimnames(ndim::Integer) = map(defaultdimname, tuple(1:ndim...))
defaultdimnames(a::AbstractArray) = defaultdimnames(ndims(a))

## disambiguation (Argh...)
if VERSION v"0.4-dev"
NamedArray{T,N}(a::AbstractArray{T,N}, names::Tuple{}, dimnames::NTuple{N}) = NamedArray{T,N,typeof(a),Tuple{}}(a, (), ())
NamedArray{T,N}(a::AbstractArray{T,N}, names::Tuple{}) = NamedArray{T,N,typeof(a),Tuple{}}(a, (), ())
end
NamedArray{T,N}(a::AbstractArray{T,N}, names::Tuple{}, dimnames::NTuple{N}) = NamedArray{T,N,typeof(a),Tuple{}}(a, (), ())
NamedArray{T,N}(a::AbstractArray{T,N}, names::Tuple{}) = NamedArray{T,N,typeof(a),Tuple{}}(a, (), ())

## Basic constructor: array, tuple of dicts, tuple
## This calls the inner constructor with the appropriate types
Expand Down Expand Up @@ -68,6 +66,6 @@ function NamedArray(T::DataType, dims::Int...)
NamedArray(a, tuple(names...), tuple(dimnames...))
end

if VERSION >= v"0.5.0-dev"
if VERSION >= v"0.5.0-dev+2396"
include("typedconstructor.jl")
end
14 changes: 0 additions & 14 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,3 @@ array(n::NamedArray) = n.array

## to other type
convert{T}(::Type{NamedArray{T}}, n::NamedArray) = NamedArray(convert(Array{T}, n.array), n.dicts, n.dimnames)

#function promote_rule{T1<:Real,T2<:Real,N}(::Type{Array{T1,N}}, ::Type{NamedArray{T2,N}})
# println("my rule")
# t = promote_type(T1,T2)
# Array{t,N}
#end

## convenience functions
##if VERSION < v"0.4-dev"
## for tf in [:float16, :float32, :float64, :complex32, :complex64, :complex128]
## eval(Expr(:import, :Base, tf))
## @eval ($tf)(a::NamedArray) = NamedArray(($tf)(a.array), a.dicts, a.dimnames)
## end
##end
14 changes: 7 additions & 7 deletions src/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import Base: getindex

## ambiguity from abstractarray.jl
if VERSION < v"0.5-dev"
if VERSION < v"0.5"
## ambiguity from abstractarray.jl
getindex(n::NamedArray, i::Real) = namedgetindex(n, indices(n.dicts[1], i))
getindex(n::NamedArray, i::AbstractArray) = namedgetindex(n, indices(n.dicts[1], i))
end
Expand All @@ -17,7 +17,7 @@ getindex(n::NamedArray, ::Colon) = n.array[:]

## special 0-dimensional case
getindex{T}(n::NamedArray{T,0}, i::Real) = getindex(n.array, i)
if VERSION < v"0.5-dev"
if VERSION < v"0.5"
getindex(n::NamedArray, i) = namedgetindex(n, indices(n.dicts[1], i))
getindex(n::NamedArray, i1, i2) = namedgetindex(n, indices(n.dicts[1], i1), indices(n.dicts[2], i2))
getindex(n::NamedArray, i1, i2, i3) = namedgetindex(n, indices(n.dicts[1], i1), indices(n.dicts[2], i2), indices(n.dicts[3], i3))
Expand Down Expand Up @@ -63,7 +63,7 @@ indices(dict::Associative, i::Not) = setdiff(1:length(dict), indices(dict, i.ind
## and has been computed by `indices()`

## Simple scalar indexing
if VERSION < v"0.5.0-dev"
if VERSION < v"0.5"
namedgetindex(n::NamedArray, i::Integer) = getindex(n.array, i)
namedgetindex(n::NamedArray, i1::Integer, i2::Integer) = getindex(n.array, i1, i2)
namedgetindex(n::NamedArray, i1::Integer, i2::Integer, i3::Integer) = getindex(n.array, i1, i2, i3)
Expand All @@ -80,7 +80,7 @@ dimkeepingtype(x::Range) = true
dimkeepingtype(x::BitArray) = true

## Slices etc.
if VERSION < v"0.5.0-dev"
if VERSION < v"0.5"
## in julia pre 0.5, only trailing singleton dimensions are removed
function namedgetindex(n::NamedArray, index...)
a = getindex(n.array, index...)
Expand Down Expand Up @@ -155,7 +155,7 @@ import Base.setindex!

setindex!{T}(A::NamedArray{T}, x) = setindex!(A, convert(T,x), 1)

if VERSION < v"0.5-dev"
if VERSION < v"0.5"
setindex!{T}(n::NamedArray{T}, x, i1::Real) = setindex!(n.array, convert(T,x), indices(n.dicts[1],i1))
setindex!{T}(n::NamedArray{T}, x, i1::Real, i2::Real) = setindex!(n.array, convert(T,x), indices(n.dicts[1], i1), indices(n.dicts[2], i2))
setindex!{T}(n::NamedArray{T}, x, i1::Real, i2::Real, i3::Real) = setindex!(n.array, convert(T,x), indices(n.dicts[1],i1), indices(n.dicts[2], i2), indices(n.dicts[3], i3))
Expand All @@ -178,7 +178,7 @@ function setindex!{T}(A::NamedArray{T}, X::ArrayOrNamed{T}, I::Range{Int})
return A
end

if VERSION < v"0.5-dev"
if VERSION < v"0.5"
# n[[1,3,4,6]] = 1:4
setindex!{T<:Real}(A::NamedArray, X::AbstractArray, I::AbstractVector{T}) = setindex!(A.array, X, I)

Expand Down
4 changes: 2 additions & 2 deletions src/keepnames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ function Base.vcat(N::NamedVector...)
end

## broadcast
if VERSION < v"0.5.0-dev"
if VERSION < v"0.5"
Base.Broadcast.broadcast(f, n::NamedArray, As...) = broadcast!(f, similar(n, Base.Broadcast.broadcast_shape(n, As...)), n, As...)
else
Base.Broadcast.broadcast_t(f, T, n::NamedArray, As...) = broadcast!(f, similar(n, T, Base.Broadcast.broadcast_shape(n, As...)), n, As...)
end

## keep names intact
if VERSION < v"0.5-dev"
if VERSION < v"0.5"
for f in (:sin, :cos, :tan, :sind, :cosd, :tand, :sinpi, :cospi, :sinh, :cosh, :tanh, :asin, :acos, :atan, :asind, :acosd, :sec, :csc, :cot, :secd, :cscd, :cotd, :asec, :acsc, :asecd, :acscd, :acotd, :sech, :csch, :coth, :asinh, :acosh, :atanh, :asech, :acsch, :acoth, :sinc, :cosc, :deg2rad, :log, :log2, :log10, :log1p, :exp, :exp2, :exp10, :expm1, :ceil, :floor, :trunc, :round, :abs, :abs2, :sign, :signbit, :sqrt, :isqrt, :cbrt, :erf, :erfc, :erfcx, :erfi, :dawson, :erfinv, :erfcinv, :real, :imag, :conj, :angle, :cis, :gamma, :lgamma, :digamma, :invdigamma, :trigamma, :airyai, :airyprime, :airyaiprime, :airybi, :airybiprime, :besselj0, :besselj1, :bessely0, :bessely1, :eta, :zeta)
eval(Expr(:import, :Base, f))
@eval ($f)(a::NamedArray) = NamedArray(($f)(a.array), a.dicts, a.dimnames)
Expand Down
2 changes: 1 addition & 1 deletion src/rearrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ rotr90(n::NamedArray) = transpose(flipdim(n, 1))
rotl90(n::NamedArray) = transpose(flipdim(n, 2))
rot180(n::NamedArray) = NamedArray(rot180(n.array), tuple([reverse(name) for name in names(n)]...), n.dimnames)

if VERSION < v"0.5.0-dev"
if VERSION < v"0.5"
import Base.nthperm, Base.nthperm!
else
import Combinatorics.nthperm, Combinatorics.nthperm!
Expand Down
22 changes: 12 additions & 10 deletions test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ end
@test ((1:6) - v).array == (1:6) - v.array

## matmul
if VERSION >= v"0.5.0-dev" ## v0.4 ambiguity-hell with AbstractTriangular c.s.
if VERSION v"0.5" ## v0.4 ambiguity-hell with AbstractTriangular c.s.
## Assume dimensions/names are correct
c = NamedArray(Float64, 5, 5)
A_mul_B!(c, randn(5, 10), randn(10, 5))
A_mul_Bc!(c, randn(5, 10), randn(5, 10))
A_mul_Bt!(c, randn(5, 10), randn(5, 10))
Ac_mul_B!(c, randn(10, 5), randn(10, 5))
At_mul_B!(c, randn(10, 5), randn(10, 5))
At_mul_Bt!(c, randn(10,5), randn(5, 10))
r5x10 = randn(5, 10)
r10x5 = randn(10, 5)
@test A_mul_B!(c, r5x10, r10x5) == r5x10 * r10x5
@test A_mul_Bc!(c, r5x10, r5x10) == r5x10 * r5x10'
@test A_mul_Bt!(c, r5x10, r5x10) == r5x10 * r5x10'
@test Ac_mul_B!(c, r10x5, r10x5) == r10x5' * r10x5
@test At_mul_B!(c, r10x5, r10x5) == r10x5' * r10x5
@test At_mul_Bt!(c, r10x5, r5x10) == r10x5' * r5x10'
end

for m in (NamedArray(rand(4)), NamedArray(rand(4,3)))
m * m'
m' * m
@test m * m' == m.array * m.array'
@test m' * m == m.array' * m.array
@test n * m == n * m.array == n.array * m == n.array * m.array
## the first expression dispatches Ac_mul_Bc!:
@test isapprox(m' * n.array', m' * n')
Expand Down Expand Up @@ -151,7 +153,7 @@ lufa = lufact(n.array)
a = randn(1000,10); s = NamedArray(a'a)

## The necessity for isapprox sugests we don't get BLAS implementations but a fallback...
if VERSION >= v"0.5.0-dev" ## v0.4 cholfact is different?
if VERSION v"0.5" ## v0.4 cholfact is different?
@test isapprox(cholfact(s).factors.array, cholfact(s.array).factors)
end

Expand Down
8 changes: 3 additions & 5 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ n4 = NamedArray(a, (OrderedDict("a"=>1,"b"=>2), OrderedDict("C"=>1,"D"=>2,"E"=>3
@test names(n3,2) == names(n4,2) == ["C","D","E"]

## 0-dim case #21
if VERSION v"0.4-dev"
n0 = NamedArray(Array{Int}())
@test size(n0) == ()
@test n0[1] == n0.array[1]
end
n0 = NamedArray(Array{Int}())
@test size(n0) == ()
@test n0[1] == n0.array[1]

## Calling constructors through convert, #38
@test convert(NamedArray, n.array) == NamedArray(n.array)
Expand Down
2 changes: 1 addition & 1 deletion test/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ first = n.array[1,:]
@test names(n[Not("two"), :]) == names(n[1:1, :])
@test n[:, ["b", "d"]] == view(n, :, ["b", "d"]) == n[:, [2, 4]]

if VERSION < v"0.5.0-dev"
if VERSION < v"0.5"
@test names(n["one", :],1) == ["one"]
end
@test names(n[Not("one"), :],1) == ["two"]
Expand Down
2 changes: 1 addition & 1 deletion test/init-namedarrays.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
n = NamedArray(rand(2,4))
Letters = [string(Char(64+i)) for i in 1:26]
letters = [string(Char(64+32+i)) for i in 1:26]
if VERSION < v"0.5.0-dev"
if VERSION < v"0.5"
letters = ASCIIString[ascii(s) for s in letters]
end

Expand Down
6 changes: 1 addition & 5 deletions test/matrixops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ for t in (Float32, Float64)
d = diag(a)
diagm(d)
cond(a)
if VERSION v"0.4-dev"
nullspace(a)
else
null(a)
end
nullspace(a)
kron(a, a)
linreg(a[:,1], b[:,1])
lyap(a, c)
Expand Down
2 changes: 1 addition & 1 deletion test/names.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include("init-namedarrays.jl")
@test dimnames(n.array, 1) == :A
@test dimnames(n.array, 2) == :B

if VERSION > v"0.5.0-dev" ## ascii and utf-8 are both String
if VERSION v"0.5" ## ascii and utf-8 are both String
dn1 = ["", ""]
dn2 = ["", "", "", ""]
else
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ using NamedArrays
using Base.Test
using DataStructures

if VERSION < v"0.5.0-dev"
view(n, args...) = getindex(n, args...)
if VERSION < v"0.5"
view(n::NamedArray, args...) = getindex(n, args...)
end

include("test.jl")
95 changes: 82 additions & 13 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,96 @@ println("show,")

include("init-namedarrays.jl")

if VERSION >= v"0.4.0-dev"
println(NamedArray(Array{Int}()))
if VERSION v"0.5"
tostring(b::IOBuffer) = String(b)
times = "×" ## \times
else
tostring(b::IOBuffer) = UTF8String(b.data)
times = "x" # ex
end
println(NamedArray([]))
print(n)
println()
show(n)
show(STDOUT, MIME"text/plain"(), n)
show(NamedArray(randn(2,1000)))
show(NamedArray(randn(1000,2)))
show(NamedArray(randn(1000)))

function showlines(x...)
buf = IOBuffer()
show(buf, x...)
return split(tostring(buf), "\n")
end

lines = showlines(NamedArray(Array{Int}()))
@test length(lines) == 2
@test lines[1] == "0-dimensional Named Array{Int64,0}"

lines = showlines(NamedArray([]))
@test length(lines) == 2
@test lines[1] == "0-element Named Array{Any,1}"

for lines in Any[showlines(n), showlines(MIME"text/plain"(), n)]
@test length(lines) == 5
@test lines[1] == "2$(times)4 Named Array{Float64,2}"
@test split(lines[2]) == vcat(["A", "", "B", ""], letters[1:4])
end

## wide array abbreviated
lines = showlines(NamedArray(randn(2,1000)))
@test length(lines) == 5
@test lines[1] == "2$(times)1000 Named Array{Float64,2}"
header = split(lines[2])
@test header[vcat(1:5, end)] == ["A", "", "B", "", "1", "1000"]
@test "" in header
for (i, line) in enumerate(lines[end-1:end])
@test split(line)[1] == string(i)
end

## tall array abbreviated
lines = showlines(NamedArray(randn(1000,2)))
@test length(lines) > 7
@test lines[1] == "1000$(times)2 Named Array{Float64,2}"
@test split(lines[2]) == ["A", "", "B", "", "1", "2"]
@test split(lines[4])[1] == "1"
@test split(lines[end])[1] == "1000"

## tall vector abbreviated
lines = showlines(NamedArray(randn(1000)))
@test length(lines) > 7
@test lines[1] == "1000-element Named Array{Float64,1}"
@test split(lines[2]) == ["A", ""]
@test split(lines[4])[1] == "1"
@test split(lines[end])[1] == "1000"

## non-standard integer indexing
zo = [0,1]
println(NamedArray(rand(2,2,2), (zo, zo, zo), ("base", "zero", "indexing")))
lines = showlines(NamedArray(rand(2,2,2), (zo, zo, zo), ("base", "zero", "indexing")))
@test length(lines) == 13
@test lines[1] == "2$(times)2$(times)2 Named Array{Float64,3}"

for (index, offset) in ([0, 3], [1, 9])
@test lines[offset] == "[:, :, indexing=$index] ="
@test split(lines[offset+1]) == ["base", "", "zero", "", "0", "1"]
@test split(lines[offset+3])[1] == "0"
@test split(lines[offset+4])[1] == "1"
end

for ndim in 1:5
println(NamedArray(rand(fill(2,ndim)...)))
lines = showlines(NamedArray(rand(fill(2,ndim)...)))
if (ndim == 1)
line1 = "2-element Named Array{Float64,1}"
else
line1 = join(repmat(["2"], ndim), times) * " Named Array{Float64,$ndim}"
end
@test lines[1] == line1
if ndim 3
@test startswith(lines[3], "[:, :, C=1")
@test split(lines[4]) == ["A", "", "B", "", "1", "2"]
end
end
## various singletons
println(NamedArray(rand(1,2,2)))
println(NamedArray(rand(2,1,2)))
println(NamedArray(rand(2,2,1)))

## sparse array
nms = [string(hash(i)) for i in 1:1000]
show(NamedArray(sprand(1000,1000, 1e-4), (nms, nms)))
lines = showlines(NamedArray(sprand(1000,1000, 1e-4), (nms, nms)))
@test length(lines) > 7
@test startswith(lines[1], "1000×1000 Named sparse matrix with")
@test endswith(lines[1], "Float64 nonzero entries:")
@test sum([contains(line, "") for line in lines]) == 1
Loading

0 comments on commit c274f18

Please sign in to comment.