Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type stability in all norm variants #6101

Merged
merged 1 commit into from
Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/linalg/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ for Ti in (:Int32,:Int64)
ccall((@chm_nm "nnz" $Ti
,:libcholmod), Int, (Ptr{c_CholmodSparse{Tv,$Ti}},Ptr{Uint8}),&A.c,cmn($Ti))
end
function norm{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti},p::Number)
function norm{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti},p::Real)
ccall((@chm_nm "norm_sparse" $Ti
, :libcholmod), Float64,
(Ptr{c_CholmodSparse{Tv,$Ti}}, Cint, Ptr{Uint8}),
Expand Down
8 changes: 2 additions & 6 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ function norm{T<:BlasFloat, TI<:Integer}(x::StridedVector{T}, rx::Union(Range1{T
BLAS.nrm2(length(rx), pointer(x)+(first(rx)-1)*sizeof(T), step(rx))
end

function vecnorm{T<:BlasFloat}(x::Union(Array{T},StridedVector{T}), p::Real=2)
length(x) == 0 && return zero(T)
p == 1 && T <: Real && return BLAS.asum(x)
p == 2 && return BLAS.nrm2(x)
invoke(vecnorm, (Any, Real), x, p)
end
vecnorm1{T<:BlasReal}(x::Union(Array{T},StridedVector{T})) = BLAS.asum(x)
vecnorm2{T<:BlasFloat}(x::Union(Array{T},StridedVector{T})) = BLAS.nrm2(x)

function triu!{T}(M::Matrix{T}, k::Integer)
m, n = size(M)
Expand Down
29 changes: 15 additions & 14 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,36 +145,40 @@ norm(x::AbstractVector, p::Real=2) = vecnorm(x, p)

function norm1{T}(A::AbstractMatrix{T})
m,n = size(A)
nrm = zero(real(zero(T)))
Tnorm = typeof(float(real(zero(T))))
Tsum = promote_type(Float64,Tnorm)
nrm::Tsum = 0
@inbounds begin
for j = 1:n
nrmj = zero(real(zero(T)))
nrmj::Tsum = 0
for i = 1:m
nrmj += abs(A[i,j])
end
nrm = max(nrm,nrmj)
end
end
return nrm
return convert(Tnorm, nrm)
end
function norm2(A::AbstractMatrix)
function norm2{T}(A::AbstractMatrix{T})
m,n = size(A)
if m == 0 || n == 0 return real(zero(eltype(A))) end
svdvals(A)[1]
Tnorm = typeof(float(real(zero(T))))
(m == 0 || n == 0) ? zero(Tnorm) : convert(Tnorm, svdvals(A)[1])
end
function normInf{T}(A::AbstractMatrix{T})
m,n = size(A)
nrm = zero(real(zero(T)))
Tnorm = typeof(float(real(zero(T))))
Tsum = promote_type(Float64,Tnorm)
nrm::Tsum = 0
@inbounds begin
for i = 1:m
nrmi = zero(real(zero(T)))
nrmi::Tsum = 0
for j = 1:n
nrmi += abs(A[i,j])
end
nrm = max(nrm,nrmi)
end
end
return nrm
return convert(Tnorm, nrm)
end
function norm{T}(A::AbstractMatrix{T}, p::Real=2)
p == 2 && return norm2(A)
Expand All @@ -183,11 +187,8 @@ function norm{T}(A::AbstractMatrix{T}, p::Real=2)
throw(ArgumentError("invalid p-norm p=$p. Valid: 1, 2, Inf"))
end

function norm(x::Number, p=2)
if p == 1 || p == Inf || p == -Inf return abs(x) end
p == 0 && return ifelse(x != 0, 1, 0)
float(abs(x))
end
norm(x::Number, p::Real=2) =
p == 0 ? convert(typeof(real(x)), ifelse(x != 0, 1, 0)) : abs(x)

rank(A::AbstractMatrix, tol::Real) = sum(svdvals(A) .> tol)
function rank(A::AbstractMatrix)
Expand Down
37 changes: 20 additions & 17 deletions base/linalg/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,28 +462,31 @@ vecnorm(A::SparseMatrixCSC, p::Real=2) = vecnorm(A.nzval, p)
function norm(A::SparseMatrixCSC,p::Real=1)
m, n = size(A)
if m == 0 || n == 0 || isempty(A)
return real(zero(eltype(A)))
return float(real(zero(eltype(A))))
elseif m == 1 || n == 1
return norm(reshape(full(A), length(A)), p)
elseif p==1
nA = real(zero(eltype(A)))
for j=1:n
colSum = real(zero(eltype(A)))
for i = A.colptr[j]:A.colptr[j+1]-1
colSum += abs(A.nzval[i])
else
Tnorm = typeof(float(real(zero(eltype(A)))))
Tsum = promote_type(Float64,Tnorm)
if p==1
nA::Tsum = 0
for j=1:n
colSum::Tsum = 0
for i = A.colptr[j]:A.colptr[j+1]-1
colSum += abs(A.nzval[i])
end
nA = max(nA, colSum)
end
nA = max(nA, colSum)
end
elseif p==Inf
rowSum = zeros(typeof(real(A[1])),m)
for i=1:length(A.nzval)
rowSum[A.rowval[i]] += abs(A.nzval[i])
return convert(Tnorm, nA)
elseif p==Inf
rowSum = zeros(Tsum,m)
for i=1:length(A.nzval)
rowSum[A.rowval[i]] += abs(A.nzval[i])
end
return convert(Tnorm, maximum(rowSum))
end
nA = maximum(rowSum)
else
throw(ArgumentError("invalid p-norm p=$p. Valid: 1, Inf"))
end
return nA
throw(ArgumentError("invalid p-norm p=$p. Valid: 1, Inf"))
end

# TODO
Expand Down