Skip to content

Commit

Permalink
Merge pull request #3071 from simonbyrne/master
Browse files Browse the repository at this point in the history
changed isinteger, isreal, etc.
  • Loading branch information
JeffBezanson committed May 14, 2013
2 parents ced3e8f + d8c1182 commit aa33b77
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 194 deletions.
17 changes: 8 additions & 9 deletions base/abstractarray.jl
Expand Up @@ -10,6 +10,14 @@ eltype(x) = Any
eltype{T,n}(::AbstractArray{T,n}) = T
eltype{T,n}(::Type{AbstractArray{T,n}}) = T
eltype{T<:AbstractArray}(::Type{T}) = eltype(super(T))
iseltype(x,T) = eltype(x) <: T
isinteger(x::AbstractArray) = all(isinteger,x)
isinteger{T<:Integer,n}(x::AbstractArray{T,n}) = true
isreal(x::AbstractArray) = all(isreal,x)
isreal{T<:Real,n}(x::AbstractArray{T,n}) = true
isfloat64(x::AbstractArray) = all(isfloat64,x)
isfloat64{T<:Float64,n}(x::AbstractArray{T,n}) = true
isfloat64{T<:Float32,n}(x::AbstractArray{T,n}) = true
ndims{T,n}(::AbstractArray{T,n}) = n
ndims{T,n}(::Type{AbstractArray{T,n}}) = n
ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T))
Expand All @@ -20,15 +28,6 @@ last(a::AbstractArray) = a[end]

strides(a::AbstractArray) = ntuple(ndims(a), i->stride(a,i))::Dims

isinteger{T<:Integer}(::AbstractArray{T}) = true
isinteger(::AbstractArray) = false
isreal{T<:Real}(::AbstractArray{T}) = true
isreal(::AbstractArray) = false
iscomplex{T<:Complex}(::AbstractArray{T}) = true
iscomplex(::AbstractArray) = false
isbool(::AbstractArray{Bool}) = true
isbool(::AbstractArray) = false

function isassigned(a::AbstractArray, i::Int...)
# TODO
try
Expand Down
19 changes: 7 additions & 12 deletions base/complex.jl
Expand Up @@ -43,11 +43,8 @@ for fn in _numeric_conversion_func_names
@eval $fn(z::Complex) = complex($fn(real(z)),$fn(imag(z)))
end

iscomplex(x::Complex) = true
iscomplex(x::Number) = false

real_valued{T<:Real}(z::Complex{T}) = imag(z) == 0
integer_valued(z::Complex) = real_valued(z) && integer_valued(real(z))
isreal{T<:Real}(z::Complex{T}) = imag(z) == 0
isinteger(z::Complex) = isreal(z) && isinteger(real(z))

isfinite(z::Complex) = isfinite(real(z)) && isfinite(imag(z))
reim(z) = (real(z), imag(z))
Expand Down Expand Up @@ -91,8 +88,6 @@ end
type ImaginaryUnit <: Number end
const im = ImaginaryUnit()

iscomplex(::ImaginaryUnit) = true

convert{T<:Real}(::Type{Complex{T}}, ::ImaginaryUnit) = Complex{T}(zero(T),one(T))
convert(::Type{Complex}, ::ImaginaryUnit) = Complex(real(im),imag(im))

Expand All @@ -109,14 +104,14 @@ convert(::Type{Complex}, z::Complex) = z
convert(::Type{Complex}, x::Real) = complex(x)

==(z::Complex, w::Complex) = (real(z) == real(w)) & (imag(z) == imag(w))
==(z::Complex, x::Real) = real_valued(z) && real(z) == x
==(x::Real, z::Complex) = real_valued(z) && real(z) == x
==(z::Complex, x::Real) = isreal(z) && real(z) == x
==(x::Real, z::Complex) = isreal(z) && real(z) == x

isequal(z::Complex, w::Complex) = isequal(real(z),real(w)) & isequal(imag(z),imag(w))
isequal(z::Complex, x::Real) = real_valued(z) && isequal(real(z),x)
isequal(x::Real, z::Complex) = real_valued(z) && isequal(real(z),x)
isequal(z::Complex, x::Real) = isreal(z) && isequal(real(z),x)
isequal(x::Real, z::Complex) = isreal(z) && isequal(real(z),x)

hash(z::Complex) = (r = hash(real(z)); real_valued(z) ? r : bitmix(r,hash(imag(z))))
hash(z::Complex) = (r = hash(real(z)); isreal(z) ? r : bitmix(r,hash(imag(z))))

conj(z::Complex) = complex(real(z),-imag(z))
abs(z::Complex) = hypot(real(z), imag(z))
Expand Down
6 changes: 6 additions & 0 deletions base/deprecated.jl
Expand Up @@ -193,10 +193,16 @@ export PipeString
@deprecate unsafe_assign unsafe_store!
@deprecate add_each! union!
@deprecate del_each! setdiff!
@deprecate real_valued isreal
@deprecate integer_valued isinteger
@deprecate float64_valued isfloat64

@deprecate expr(hd, a...) Expr(hd, a...)
@deprecate expr(hd, a::Array{Any,1}) Expr(hd, a...)
@deprecate readdir(cmd::Cmd) readdir(string(cmd)[2:end-1])
@deprecate isbool(x) iseltype(x,Bool)
@deprecate iscomplex(x) iseltype(x,Complex)


# note removed macros: str, B_str, I_str, E_str, L_str, L_mstr, I_mstr, E_mstr

Expand Down
7 changes: 2 additions & 5 deletions base/exports.jl
Expand Up @@ -337,7 +337,6 @@ export
float,
float32,
float64,
float64_valued,
floor,
frexp,
gamma,
Expand All @@ -357,15 +356,14 @@ export
int8,
integer,
integer_partitions,
integer_valued,
inv,
invmod,
iround,
isbool,
iscomplex,
isdenormal,
iseltype,
iseven,
isfinite,
isfloat64,
isinf,
isinteger,
isnan,
Expand Down Expand Up @@ -403,7 +401,6 @@ export
primes,
radians2degrees,
real,
real_valued,
realmax,
realmin,
reim,
Expand Down
5 changes: 4 additions & 1 deletion base/floatfuncs.jl
Expand Up @@ -29,7 +29,10 @@ maxintfloat(::Type{Float32}) = float32(16777216.)
maxintfloat{T<:FloatingPoint}(x::T) = maxintfloat(T)
maxintfloat() = maxintfloat(Float64)

integer_valued(x::FloatingPoint) = (trunc(x)==x)&isfinite(x)
isinteger(x::FloatingPoint) = (trunc(x)==x)&isfinite(x)
isfloat64(x::Number) = float64(x) == x
isfloat64(::Float64) = true
isfloat64(::Float32) = true

## precision, as defined by the effective number of bits in the mantissa ##
get_precision(::Float32) = 24
Expand Down
2 changes: 2 additions & 0 deletions base/linalg.jl
Expand Up @@ -138,6 +138,8 @@ export


typealias BlasFloat Union(Float64,Float32,Complex128,Complex64)
typealias BlasReal Union(Float64,Float32)
typealias BlasComplex Union(Complex128,Complex64)
typealias BlasChar Char

if USE_LIB64
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/arnoldi.jl
Expand Up @@ -10,7 +10,7 @@ function eigs{T<:BlasFloat}(A::AbstractMatrix{T};
if m != n; error("Input must be square"); end
if n <= 6 && nev > n-1; nev = n-1; end
sym = issym(A)
cmplx = iscomplex(A)
cmplx = iseltype(A,Complex)
bmat = "I"

# Compute the Ritz values and Ritz vectors
Expand Down
9 changes: 5 additions & 4 deletions base/linalg/dense.jl
Expand Up @@ -206,7 +206,7 @@ randsym(n) = symmetrize!(randn(n,n))
^(A::Matrix, p::Integer) = p < 0 ? inv(A^-p) : Base.power_by_squaring(A,p)

function ^(A::Matrix, p::Number)
if integer_valued(p)
if isinteger(p)
ip = integer(real(p))
if ip < 0
return inv(Base.power_by_squaring(A, -ip))
Expand All @@ -218,7 +218,7 @@ function ^(A::Matrix, p::Number)
error("matrix must be square")
end
(v, X) = eig(A)
if isreal(v) && any(v.<0)
if any(v.<0)
v = complex(v)
end
if ishermitian(A)
Expand Down Expand Up @@ -382,7 +382,7 @@ function sqrtm(A::StridedMatrix, cond::Bool)
if ishermitian(A)
return sqrtm(Hermitian(A), cond)
else
SchurF = schurfact!(iscomplex(A) ? copy(A) : complex(A))
SchurF = schurfact!(iseltype(A,Complex) ? copy(A) : complex(A))
R = zeros(eltype(SchurF[:T]), n, n)
for j = 1:n
R[j,j] = sqrt(SchurF[:T][j,j])
Expand All @@ -409,7 +409,8 @@ end
sqrtm{T<:Integer}(A::StridedMatrix{T}, cond::Bool) = sqrtm(float(A), cond)
sqrtm{T<:Integer}(A::StridedMatrix{Complex{T}}, cond::Bool) = sqrtm(complex128(A), cond)
sqrtm(A::StridedMatrix) = sqrtm(A, false)
sqrtm(a::Number) = isreal(a) ? (b = sqrt(complex(a)); imag(b) == 0 ? real(b) : b) : sqrt(a)
sqrtm(a::Number) = (b = sqrt(complex(a)); imag(b) == 0 ? real(b) : b)
sqrtm(a::Complex) = sqrt(a)

function det(A::Matrix)
m, n = size(A)
Expand Down
32 changes: 22 additions & 10 deletions base/linalg/factorization.jl
Expand Up @@ -252,7 +252,9 @@ function *{T<:BlasFloat}(A::QRPackedQ{T}, B::StridedVecOrMat{T})
end
LAPACK.gemqrt!('L', 'N', A.vs, A.T, Bc)
end
Ac_mul_B(A::QRPackedQ, B::StridedVecOrMat) = LAPACK.gemqrt!('L', iscomplex(A.vs[1]) ? 'C' : 'T', A.vs, A.T, copy(B))
Ac_mul_B{T<:BlasReal}(A::QRPackedQ{T}, B::StridedVecOrMat) = LAPACK.gemqrt!('L','T',A.vs,A.T,copy(B))
Ac_mul_B{T<:BlasComplex}(A::QRPackedQ{T}, B::StridedVecOrMat) = LAPACK.gemqrt!('L','C',A.vs,A.T,copy(B))

*(A::StridedVecOrMat, B::QRPackedQ) = LAPACK.gemqrt!('R', 'N', B.vs, B.T, copy(A))
function A_mul_Bc{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRPackedQ{T})
m = size(A, 1)
Expand All @@ -264,7 +266,7 @@ function A_mul_Bc{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRPackedQ{T})
else
throw(DimensionMismatch(""))
end
LAPACK.gemqrt!('R', iscomplex(B.vs[1]) ? 'C' : 'T', B.vs, B.T, Ac)
LAPACK.gemqrt!('R', iseltype(B.vs,Complex) ? 'C' : 'T', B.vs, B.T, Ac)
end
## Least squares solution. Should be more careful about cases with m < n
(\)(A::QR, B::StridedVector) = Triangular(A[:R], 'U')\(A[:Q]'B)[1:size(A, 2)]
Expand Down Expand Up @@ -346,7 +348,8 @@ function *{T<:BlasFloat}(A::QRPivotedQ{T}, B::StridedVecOrMat{T})
end
LAPACK.ormqr!('L', 'N', A.hh, A.tau, Bc)
end
Ac_mul_B(A::QRPivotedQ, B::StridedVecOrMat) = LAPACK.ormqr!('L', iscomplex(A.hh[1]) ? 'C' : 'T', A.hh, A.tau, copy(B))
Ac_mul_B{T<:BlasReal}(A::QRPivotedQ{T}, B::StridedVecOrMat) = LAPACK.ormqr!('L','T',A.hh,A.tau,copy(B))
Ac_mul_B{T<:BlasComplex}(A::QRPivotedQ{T}, B::StridedVecOrMat) = LAPACK.ormqr!('L','C',A.hh,A.tau,copy(B))
*(A::StridedVecOrMat, B::QRPivotedQ) = LAPACK.ormqr!('R', 'N', B.hh, B.tau, copy(A))
function A_mul_Bc{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRPivotedQ{T})
m = size(A, 1)
Expand All @@ -358,7 +361,7 @@ function A_mul_Bc{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRPivotedQ{T})
else
throw(DimensionMismatch(""))
end
LAPACK.ormqr!('R', iscomplex(B.hh[1]) ? 'C' : 'T', B.hh, B.tau, Ac)
LAPACK.ormqr!('R', iseltype(B.hh,Complex) ? 'C' : 'T', B.hh, B.tau, Ac)
end

##TODO: Add methods for rank(A::QRP{T}) and adjust the (\) method accordingly
Expand Down Expand Up @@ -408,11 +411,10 @@ function getindex(A::Eigen, d::Symbol)
error("No such type field")
end

function eigfact!{T<:BlasFloat}(A::StridedMatrix{T})
function eigfact!{T<:BlasReal}(A::StridedMatrix{T})
n = size(A, 2)
if n == 0; return Eigen(zeros(T, 0), zeros(T, 0, 0)) end
if ishermitian(A) return eigfact!(Hermitian(A)) end
if iscomplex(A) return Eigen(LAPACK.geev!('N', 'V', A)[[1,3]]...) end

WR, WI, VL, VR = LAPACK.geev!('N', 'V', A)
if all(WI .== 0.) return Eigen(WR, VR) end
Expand All @@ -431,6 +433,13 @@ function eigfact!{T<:BlasFloat}(A::StridedMatrix{T})
return Eigen(complex(WR, WI), evec)
end

function eigfact!{T<:BlasComplex}(A::StridedMatrix{T})
n = size(A, 2)
if n == 0; return Eigen(zeros(T, 0), zeros(T, 0, 0)) end
if ishermitian(A) return eigfact!(Hermitian(A)) end
Eigen(LAPACK.geev!('N', 'V', A)[[1,3]]...)
end

eigfact(A::StridedMatrix) = eigfact!(copy(A))
eigfact{T<:Integer}(x::StridedMatrix{T}) = eigfact(float64(x))
eigfact(x::Number) = Eigen([x], fill(one(x), 1, 1))
Expand All @@ -443,24 +452,27 @@ end
#Calculates eigenvectors
eigvecs(A::Union(Number, StridedMatrix)) = eigfact(A)[:vectors]

function eigvals(A::StridedMatrix)
function eigvals{T<:BlasReal}(A::StridedMatrix{T})
if ishermitian(A) return eigvals(Hermitian(A)) end
if iscomplex(A) return LAPACK.geev!('N', 'N', copy(A))[1] end
valsre, valsim, _, _ = LAPACK.geev!('N', 'N', copy(A))
if all(valsim .== 0) return valsre end
return complex(valsre, valsim)
end
function eigvals{T<:BlasReal}(A::StridedMatrix{T})
if ishermitian(A) return eigvals(Hermitian(A)) end
LAPACK.geev!('N', 'N', copy(A))[1]
end

eigvals(x::Number) = [one(x)]

#Computes maximum and minimum eigenvalue
function eigmax(A::Union(Number, StridedMatrix))
v = eigvals(A)
iscomplex(v) ? error("Complex eigenvalues cannot be ordered") : max(v)
iseltype(v,Complex) ? error("Complex eigenvalues cannot be ordered") : max(v)
end
function eigmin(A::Union(Number, StridedMatrix))
v = eigvals(A)
iscomplex(v) ? error("Complex eigenvalues cannot be ordered") : min(v)
iseltype(v,Complex) ? error("Complex eigenvalues cannot be ordered") : min(v)
end

inv(A::Eigen) = scale(A.vectors, 1.0/A.values)*A.vectors'
Expand Down
12 changes: 6 additions & 6 deletions base/linalg/lapack.jl
Expand Up @@ -234,7 +234,7 @@ for (gebrd, gelqf, geqlf, geqrf, geqp3, geqrt3, gerqf, getrf, elty, relty) in
work = Array($elty, 1)
lwork = blas_int(-1)
info = Array(BlasInt, 1)
cmplx = iscomplex(A)
cmplx = iseltype(A,Complex)
if cmplx; rwork = Array($relty, 2n); end
for i in 1:2
if cmplx
Expand Down Expand Up @@ -618,7 +618,7 @@ for (geev, gesvd, gesdd, ggsvd, elty, relty) in
rvecs = jobvr == 'V'
VL = Array($elty, (n, lvecs ? n : 0))
VR = Array($elty, (n, rvecs ? n : 0))
cmplx = iscomplex(A)
cmplx = iseltype(A,Complex)
if cmplx
W = Array($elty, n)
rwork = Array($relty, 2n)
Expand Down Expand Up @@ -685,7 +685,7 @@ for (geev, gesvd, gesdd, ggsvd, elty, relty) in
work = Array($elty, 1)
lwork = blas_int(-1)
S = Array($relty, minmn)
cmplx = iscomplex(A)
cmplx = iseltype(A,Complex)
if cmplx
rwork = Array($relty, job == 'N' ? 7*minmn : 5*minmn*minmn + 5*minmn)
end
Expand Down Expand Up @@ -737,7 +737,7 @@ for (geev, gesvd, gesdd, ggsvd, elty, relty) in
U = Array($elty, jobu == 'A'? (m, m):(jobu == 'S'? (m, minmn) : (m, 0)))
VT = Array($elty, jobvt == 'A'? (n, n):(jobvt == 'S'? (minmn, n) : (n, 0)))
work = Array($elty, 1)
cmplx = iscomplex(A)
cmplx = iseltype(A,Complex)
if cmplx; rwork = Array($relty, 5minmn); end
lwork = blas_int(-1)
info = Array(BlasInt, 1)
Expand Down Expand Up @@ -798,7 +798,7 @@ for (geev, gesvd, gesdd, ggsvd, elty, relty) in
ldq = max(1, n)
Q = jobq == 'Q' ? Array($elty, ldq, n) : Array($elty, 0)
work = Array($elty, max(3n, m, p) + n)
cmplx = iscomplex(A)
cmplx = iseltype(A,Complex)
if cmplx; rwork = Array($relty, 2n); end
iwork = Array(BlasInt, n)
info = Array(BlasInt, 1)
Expand Down Expand Up @@ -1519,7 +1519,7 @@ for (syconv, syev, sysv, sytrf, sytri, sytrs, elty, relty) in
function syev!(jobz::BlasChar, uplo::BlasChar, A::StridedMatrix{$elty})
chkstride1(A)
chksquare(A)
cmplx = iscomplex(A)
cmplx = iseltype(A,Complex)
n = size(A, 1)
W = Array($relty, n)
work = Array($elty, 1)
Expand Down
6 changes: 2 additions & 4 deletions base/linalg/tridiag.jl
Expand Up @@ -325,10 +325,8 @@ LDLTTridiagonal{S<:BlasFloat,T<:BlasFloat}(D::Vector{S}, E::Vector{T}) = LDLTTri
ldltd!{T<:BlasFloat}(A::SymTridiagonal{T}) = LDLTTridiagonal(LAPACK.pttrf!(real(A.dv),A.ev)...)
ldltd{T<:BlasFloat}(A::SymTridiagonal{T}) = ldltd!(copy(A))

function (\){T<:BlasFloat}(C::LDLTTridiagonal{T}, B::StridedVecOrMat{T})
if iscomplex(B) return LAPACK.pttrs!('L', C.D, C.E, copy(B)) end
LAPACK.pttrs!(C.D, C.E, copy(B))
end
(\){T<:BlasReal}(C::LDLTTridiagonal{T}, B::StridedVecOrMat{T}) = LAPACK.pttrs!(C.D, C.E, copy(B))
(\){T<:BlasComplex}(C::LDLTTridiagonal{T}, B::StridedVecOrMat{T}) = LAPACK.pttrs!('L', C.D, C.E, copy(B))

type LUTridiagonal{T} <: Factorization{T}
dl::Vector{T}
Expand Down

0 comments on commit aa33b77

Please sign in to comment.