Skip to content

Commit 8e36860

Browse files
authored
Merge pull request #394 from JuliaArrays/ajf/fix-some-v0.7-issues
Fix some v0.7 issues
2 parents e019b69 + 6da25c4 commit 8e36860

13 files changed

+38
-29
lines changed

src/SDiagonal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ transpose(D::SDiagonal) = D
7373
adjoint(D::SDiagonal) = conj(D)
7474

7575
diag(D::SDiagonal) = D.diag
76-
trace(D::SDiagonal) = sum(D.diag)
76+
tr(D::SDiagonal) = sum(D.diag)
7777
det(D::SDiagonal) = prod(D.diag)
7878
logdet(D::SDiagonal{N,T}) where {N,T<:Real} = sum(log.(D.diag))
7979
function logdet(D::SDiagonal{N,T}) where {N,T<:Complex} #Make sure branch cut is correct

src/StaticArrays.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if VERSION < v"0.7-"
2525

2626
const adjoint = ctranspose
2727
const Adjoint = RowVector
28+
const tr = trace
2829
else
2930
using Compat
3031

@@ -35,7 +36,7 @@ else
3536
import Base: sqrt, exp, log
3637

3738
using LinearAlgebra
38-
import LinearAlgebra: transpose, adjoint, eye, vecdot, eig, eigvals, eigfact, lyap, trace,
39+
import LinearAlgebra: transpose, adjoint, eye, vecdot, eig, eigvals, eigfact, lyap, tr,
3940
kron, diag, vecnorm, norm, dot, diagm, lu, svd, svdvals, svdfact,
4041
factorize, ishermitian, issymmetric, isposdef, normalize,
4142
normalize!, Eigen, det, logdet, cross, diff, qr

src/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ similar(::Type{A},::Type{T},s::Size{S}) where {A<:Array,T,S} = sizedarray_simila
105105
if indexstyle == IndexLinear
106106
exprs = [:(a[$i]) for i = 1:prod(S)]
107107
else
108-
exprs = [:(a[$(inds)]) for inds CartesianRange(S)]
108+
exprs = [:(a[$(inds)]) for inds CartesianIndices(S)]
109109
end
110110

111111
return quote

src/broadcast.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ else
5959
Broadcast.DefaultArrayStyle(Broadcast._max(Val(M), Val(N)))
6060
BroadcastStyle(::StaticArrayStyle{M}, ::Broadcast.DefaultArrayStyle{0}) where {M} =
6161
StaticArrayStyle{M}()
62-
# FIXME: These two rules should be removed once VectorStyle and MatrixStyle are removed from base/broadcast.jl
63-
BroadcastStyle(::StaticArrayStyle{M}, ::Broadcast.VectorStyle) where M = Broadcast.Unknown()
64-
BroadcastStyle(::StaticArrayStyle{M}, ::Broadcast.MatrixStyle) where M = Broadcast.Unknown()
65-
# End FIXME
6662

6763
# Add a broadcast method that calls the @generated routine
6864
@inline function broadcast(f, ::StaticArrayStyle, ::Nothing, ::Nothing, As...)
@@ -134,7 +130,7 @@ end
134130
exprs_vals = [(!(a[i] <: AbstractArray) ? :(a[$i][]) : :(a[$i][$(broadcasted_index(sizes[i], current_ind))])) for i = 1:length(sizes)]
135131
exprs[current_ind...] = :(f($(exprs_vals...)))
136132

137-
# increment current_ind (maybe use CartesianRange?)
133+
# increment current_ind (maybe use CartesianIndices?)
138134
current_ind[1] += 1
139135
for i 1:length(newsize)
140136
if current_ind[i] > newsize[i]
@@ -151,7 +147,7 @@ end
151147
end
152148
end
153149

154-
eltype_exprs = [t <: AbstractArray ? :($(eltype(t))) : :($t) for t a]
150+
eltype_exprs = [t <: Union{AbstractArray, Ref} ? :(eltype($t)) : :($t) for t a]
155151
newtype_expr = :(return_type(f, Tuple{$(eltype_exprs...)}))
156152

157153
return quote
@@ -197,7 +193,7 @@ end
197193
exprs_vals = [(!(as[i] <: AbstractArray) ? :(as[$i][]) : :(as[$i][$(broadcasted_index(sizes[i], current_ind))])) for i = 1:length(sizes)]
198194
exprs[current_ind...] = :(dest[$j] = f($(exprs_vals...)))
199195

200-
# increment current_ind (maybe use CartesianRange?)
196+
# increment current_ind (maybe use CartesianIndices?)
201197
current_ind[1] += 1
202198
for i 1:length(newsize)
203199
if current_ind[i] > newsize[i]

src/eigen.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ end
100100

101101
eig3 = q + 2 * p * cos(phi)
102102
eig1 = q + 2 * p * cos(phi + (2*Sreal(pi)/3))
103-
eig2 = 3 * q - eig1 - eig3 # since trace(A) = eig1 + eig2 + eig3
103+
eig2 = 3 * q - eig1 - eig3 # since tr(A) = eig1 + eig2 + eig3
104104

105105
return SVector(eig1, eig2, eig3)
106106
end
@@ -268,7 +268,7 @@ end
268268

269269
eig3 = q + 2 * p * cos(phi)
270270
eig1 = q + 2 * p * cos(phi + (2*Sreal(pi)/3))
271-
eig2 = 3 * q - eig1 - eig3 # since trace(A) = eig1 + eig2 + eig3
271+
eig2 = 3 * q - eig1 - eig3 # since tr(A) = eig1 + eig2 + eig3
272272

273273
if r > 0 # Helps with conditioning the eigenvector calculation
274274
(eig1, eig3) = (eig3, eig1)

src/indexing.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ end
224224
@generated function _getindex(a::AbstractArray, ind_sizes::Tuple{Vararg{Size}}, inds)
225225
newsize = out_index_size(ind_sizes.parameters...)
226226
linearsizes = linear_index_size(ind_sizes.parameters...)
227-
exprs = Array{Expr}(linearsizes)
227+
exprs = Array{Expr}(undef, linearsizes)
228228

229229
# Iterate over input indices
230230
ind_types = inds.parameters
@@ -326,7 +326,7 @@ end
326326
# setindex! from a scalar
327327
@generated function _setindex!(a::AbstractArray, value, ind_sizes::Tuple{Vararg{Size}}, inds)
328328
linearsizes = linear_index_size(ind_sizes.parameters...)
329-
exprs = Array{Expr}(linearsizes)
329+
exprs = Array{Expr}(undef, linearsizes)
330330

331331
# Iterate over input indices
332332
ind_types = inds.parameters
@@ -363,7 +363,7 @@ end
363363
# setindex! from an array
364364
@generated function _setindex!(a::AbstractArray, v::AbstractArray, ind_sizes::Tuple{Vararg{Size}}, inds)
365365
linearsizes = linear_index_size(ind_sizes.parameters...)
366-
exprs = Array{Expr}(linearsizes)
366+
exprs = Array{Expr}(undef, linearsizes)
367367

368368
# Iterate over input indices
369369
ind_types = inds.parameters

src/linalg.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,11 @@ end
314314
@inline normalize(a::StaticVector) = inv(vecnorm(a))*a
315315
@inline normalize(a::StaticVector, p::Real) = inv(vecnorm(a, p))*a
316316

317-
@inline normalize!(a::StaticVector) = (a .*= inv(vecnorm(a)))
318-
@inline normalize!(a::StaticVector, p::Real) = (a .*= inv(vecnorm(a, p)))
317+
@inline normalize!(a::StaticVector) = (a .*= inv(vecnorm(a)); return a)
318+
@inline normalize!(a::StaticVector, p::Real) = (a .*= inv(vecnorm(a, p)); return a)
319319

320-
@inline trace(a::StaticMatrix) = _trace(Size(a), a)
321-
@generated function _trace(::Size{S}, a::StaticMatrix) where {S}
320+
@inline tr(a::StaticMatrix) = _tr(Size(a), a)
321+
@generated function _tr(::Size{S}, a::StaticMatrix) where {S}
322322
if S[1] != S[2]
323323
throw(DimensionMismatch("matrix is not square"))
324324
end

src/lyap.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ _lyap(::Size{(1,1)}, ::Size{(1,1)}, a::StaticMatrix, c::StaticMatrix) = -c/(2a[
44

55
@inline function _lyap(::Size{(2,2)}, ::Size{(2,2)}, a::StaticMatrix, c::StaticMatrix)
66
d = det(a)
7-
t = trace(a)
7+
t = tr(a)
88
-(d*c + (a - t*I)*c*(a-t*I)')/(2*d*t) # http://www.nber.org/papers/w8956.pdf
99
end
1010

src/mapreduce.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ end
9797
##################
9898

9999
# I'm not sure why the signature for this from Base precludes multiple arrays?
100-
# (also, why now mutating `mapreducedim!` and `reducedim!`?)
100+
# (also, why not mutating `mapreducedim!` and `reducedim!`?)
101101
# (similarly, `broadcastreduce` and `broadcastreducedim` sounds useful)
102102
@inline function mapreducedim(f, op, a::StaticArray, ::Type{Val{D}}) where {D}
103103
_mapreducedim(f, op, Size(a), a, Val{D})

test/SDiagonal.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@
3232
@test logdet(m) == logdet(m2)
3333
@test logdet(im*m) logdet(im*m2)
3434
@test det(m) == det(m2)
35-
@test trace(m) == trace(m2)
36-
@test logm(m) == logm(m2)
37-
@test expm(m) == expm(m2)
38-
@test sqrtm(m) == sqrtm(m2)
35+
@test tr(m) == tr(m2)
36+
if VERSION < v"0.7-"
37+
@test logm(m) == logm(m2)
38+
@test expm(m) == expm(m2)
39+
@test sqrtm(m) == sqrtm(m2)
40+
else
41+
@test log(m) == log(m2)
42+
@test exp(m) == exp(m2)
43+
@test sqrt(m) == sqrt(m2)
44+
end
3945
@test chol(m) == chol(m2)
4046

4147
# Aparently recursive chol never really worked

test/indexing.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@
169169
@testset "inferabilty of index_sizes helper" begin
170170
# see JuliaLang/julia#21244
171171
# it's not about inferring the correct type, but about inference throwing an error
172-
@test code_warntype(DevNull, StaticArrays.index_sizes, Tuple{Vararg{Any}}) == nothing
172+
if VERSION < v"0.7-"
173+
@test code_warntype(DevNull, StaticArrays.index_sizes, Tuple{Vararg{Any}}) == nothing
174+
else
175+
@test code_warntype(devnull, StaticArrays.index_sizes, Tuple{Vararg{Any}}) == nothing
176+
end
173177
end
174178
end

test/linalg.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,16 @@ using StaticArrays, Base.Test
161161
end
162162

163163
@testset "trace" begin
164-
@test trace(@SMatrix [1.0 2.0; 3.0 4.0]) === 5.0
165-
@test_throws DimensionMismatch trace(@SMatrix ones(5,4))
164+
@test tr(@SMatrix [1.0 2.0; 3.0 4.0]) === 5.0
165+
@test_throws DimensionMismatch tr(@SMatrix ones(5,4))
166166
end
167167

168168
@testset "size zero" begin
169169
@test vecdot(SVector{0, Float64}(()), SVector{0, Float64}(())) === 0.
170170
@test StaticArrays.bilinear_vecdot(SVector{0, Float64}(()), SVector{0, Float64}(())) === 0.
171171
@test vecnorm(SVector{0, Float64}(())) === 0.
172172
@test vecnorm(SVector{0, Float64}(()), 1) === 0.
173-
@test trace(SMatrix{0,0,Float64}(())) === 0.
173+
@test tr(SMatrix{0,0,Float64}(())) === 0.
174174
end
175175

176176
@testset "kron" begin

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ if VERSION > v"0.7-"
33
using Test
44
using Random
55
using LinearAlgebra
6+
using InteractiveUtils
67
else
78
using Base.Test
89
using Base.Random
910
const LinearAlgebra = Base.LinAlg
1011
const Adjoint = RowVector
12+
const tr = trace
1113
end
1214

1315
# We generate a lot of matrices using rand(), but unit tests should be

0 commit comments

Comments
 (0)