From ba4df47fe17e9c2f12454d3b91bfe4d3fdf83de0 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 10 May 2013 19:15:46 +0100 Subject: [PATCH 1/4] added isela function, removed redundant functions: isinteger, isreal, iscomplex, isbool. renamed integer_valued, real_valued, float64_valued --- base/abstractarray.jl | 10 +- base/complex.jl | 19 ++-- base/deprecated.jl | 8 ++ base/exports.jl | 11 +- base/floatfuncs.jl | 2 +- base/linalg.jl | 2 + base/linalg/arnoldi.jl | 2 +- base/linalg/dense.jl | 9 +- base/linalg/factorization.jl | 32 ++++-- base/linalg/lapack.jl | 12 +- base/linalg/tridiag.jl | 6 +- base/mpfr.jl | 12 +- base/number.jl | 14 +-- base/rational.jl | 12 +- base/sparse.jl | 2 +- doc/helpdb.jl | 210 ++++++++++++++++++++--------------- doc/stdlib/base.rst | 26 ++--- test/blas.jl | 2 +- test/linalg.jl | 6 +- test/mpfr.jl | 6 +- 20 files changed, 212 insertions(+), 191 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index c0f69af9e0012..c8a34ac2ef099 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -10,6 +10,7 @@ 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)) +isela{Ta,n,Tb}(::AbstractArray{Ta,n},::Type{Tb}) = Ta <: Tb ndims{T,n}(::AbstractArray{T,n}) = n ndims{T,n}(::Type{AbstractArray{T,n}}) = n ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T)) @@ -20,15 +21,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 diff --git a/base/complex.jl b/base/complex.jl index 48592aa2d4d6e..587940ad49fc6 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -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)) +isrealvalued{T<:Real}(z::Complex{T}) = imag(z) == 0 +isintegervalued(z::Complex) = isrealvalued(z) && isintegervalued(real(z)) isfinite(z::Complex) = isfinite(real(z)) && isfinite(imag(z)) reim(z) = (real(z), imag(z)) @@ -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)) @@ -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) = isrealvalued(z) && real(z) == x +==(x::Real, z::Complex) = isrealvalued(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) = isrealvalued(z) && isequal(real(z),x) +isequal(x::Real, z::Complex) = isrealvalued(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)); isrealvalued(z) ? r : bitmix(r,hash(imag(z)))) conj(z::Complex) = complex(real(z),-imag(z)) abs(z::Complex) = hypot(real(z), imag(z)) diff --git a/base/deprecated.jl b/base/deprecated.jl index ed5bdee70bfd8..609d0213dfd12 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -193,10 +193,18 @@ export PipeString @deprecate unsafe_assign unsafe_store! @deprecate add_each! union! @deprecate del_each! setdiff! +@deprecate real_valued isrealvalued +@deprecate integer_valued isintegervalued +@deprecate float64_valued isfloat64valued @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) isela(x,Bool) +@deprecate isinteger(x) isela(x,Integer) +@deprecate isreal(x) isela(x,Real) +@deprecate iscomplex(x) isela(x,Complex) + # note removed macros: str, B_str, I_str, E_str, L_str, L_mstr, I_mstr, E_mstr diff --git a/base/exports.jl b/base/exports.jl index f7b59d768ccdc..e64c068b402f9 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -336,7 +336,6 @@ export float, float32, float64, - float64_valued, floor, frexp, gamma, @@ -356,23 +355,22 @@ export int8, integer, integer_partitions, - integer_valued, inv, invmod, iround, - isbool, - iscomplex, isdenormal, + isela, iseven, isfinite, + isfloat64valued, isinf, - isinteger, + isintegervalued, isnan, isodd, ispow2, isprime, isqrt, - isreal, + isrealvalued, itrunc, lcm, ldexp, @@ -402,7 +400,6 @@ export primes, radians2degrees, real, - real_valued, realmax, realmin, reim, diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 4bcc6a6aa9591..147ce514baedf 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -29,7 +29,7 @@ maxintfloat(::Type{Float32}) = float32(16777216.) maxintfloat{T<:FloatingPoint}(x::T) = maxintfloat(T) maxintfloat() = maxintfloat(Float64) -integer_valued(x::FloatingPoint) = (trunc(x)==x)&isfinite(x) +isintegervalued(x::FloatingPoint) = (trunc(x)==x)&isfinite(x) ## precision, as defined by the effective number of bits in the mantissa ## get_precision(::Float32) = 24 diff --git a/base/linalg.jl b/base/linalg.jl index c59d9c4c0d8da..5d00ef5a1405d 100644 --- a/base/linalg.jl +++ b/base/linalg.jl @@ -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 diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index 37801d7411763..2e3c8614134ce 100644 --- a/base/linalg/arnoldi.jl +++ b/base/linalg/arnoldi.jl @@ -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 = isela(A,Complex) bmat = "I" # Compute the Ritz values and Ritz vectors diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index 6f941ce0ff97b..1743a464faf6f 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -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 isintegervalued(p) ip = integer(real(p)) if ip < 0 return inv(Base.power_by_squaring(A, -ip)) @@ -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) @@ -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!(isela(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]) @@ -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) diff --git a/base/linalg/factorization.jl b/base/linalg/factorization.jl index 790eca8caf78d..4b847f5328de5 100644 --- a/base/linalg/factorization.jl +++ b/base/linalg/factorization.jl @@ -251,7 +251,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) @@ -263,7 +265,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', isela(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)] @@ -345,7 +347,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) @@ -357,7 +360,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', isela(B.hh,Complex) ? 'C' : 'T', B.hh, B.tau, Ac) end ##TODO: Add methods for rank(A::QRP{T}) and adjust the (\) method accordingly @@ -407,11 +410,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 @@ -430,6 +432,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)) @@ -442,24 +451,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) + isela(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) + isela(v,Complex) ? error("Complex eigenvalues cannot be ordered") : min(v) end inv(A::Eigen) = scale(A.vectors, 1.0/A.values)*A.vectors' diff --git a/base/linalg/lapack.jl b/base/linalg/lapack.jl index 5f0f869689eca..ccc30e9df6a00 100644 --- a/base/linalg/lapack.jl +++ b/base/linalg/lapack.jl @@ -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 = isela(A,Complex) if cmplx; rwork = Array($relty, 2n); end for i in 1:2 if cmplx @@ -575,7 +575,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 = isela(A,Complex) if cmplx W = Array($elty, n) rwork = Array($relty, 2n) @@ -642,7 +642,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 = isela(A,Complex) if cmplx rwork = Array($relty, job == 'N' ? 7*minmn : 5*minmn*minmn + 5*minmn) end @@ -694,7 +694,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 = isela(A,Complex) if cmplx; rwork = Array($relty, 5minmn); end lwork = blas_int(-1) info = Array(BlasInt, 1) @@ -755,7 +755,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 = isela(A,Complex) if cmplx; rwork = Array($relty, 2n); end iwork = Array(BlasInt, n) info = Array(BlasInt, 1) @@ -1476,7 +1476,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 = isela(A,Complex) n = size(A, 1) W = Array($relty, n) work = Array($elty, 1) diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index ea4bc2ef9816f..dabb9c7f47e32 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -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} diff --git a/base/mpfr.jl b/base/mpfr.jl index a675a645aa782..59aa7e88cc536 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -17,7 +17,7 @@ export import Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely, bessely0, bessely1, ceil, cmp, convert, copysign, exp, exp2, - exponent, factorial, floor, hypot, integer_valued, iround, isfinite, + exponent, factorial, floor, hypot, isintegervalued, iround, isfinite, isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf, nextfloat, prevfloat, promote_rule, rem, round, show, showcompact, sum, sqrt, string, trunc, get_precision, exp10, expm1, gamma, lgamma, digamma, @@ -102,7 +102,7 @@ convert(::Type{FloatingPoint}, x::BigInt) = BigFloat(x) for to in (Int8, Int16, Int32, Int64) @eval begin function convert(::Type{$to}, x::BigFloat) - (integer_valued(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) + (isintegervalued(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) convert($to, ccall((:mpfr_get_si,:libmpfr), Clong, (Ptr{BigFloat}, Int32), &x, RoundToZero)) end @@ -112,7 +112,7 @@ end for to in (Uint8, Uint16, Uint32, Uint64) @eval begin function convert(::Type{$to}, x::BigFloat) - (integer_valued(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) + (isintegervalued(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) convert($to, ccall((:mpfr_get_ui,:libmpfr), Culong, (Ptr{BigFloat}, Int32), &x, RoundToZero)) end @@ -120,7 +120,7 @@ for to in (Uint8, Uint16, Uint32, Uint64) end function convert(::Type{BigInt}, x::BigFloat) - if integer_valued(x) + if isintegervalued(x) return itrunc(x) else throw(InexactError()) @@ -460,7 +460,7 @@ function bessely(n::Integer, x::BigFloat) end function factorial(x::BigFloat) - if x < 0 || !integer_valued(x) + if x < 0 || !isintegervalued(x) throw(DomainError()) end ui = convert(Culong, x) @@ -617,7 +617,7 @@ function exponent(x::BigFloat) return ccall((:mpfr_get_exp, :libmpfr), Clong, (Ptr{BigFloat},), &x) - 1 end -function integer_valued(x::BigFloat) +function isintegervalued(x::BigFloat) return ccall((:mpfr_integer_p, :libmpfr), Int32, (Ptr{BigFloat},), &x) != 0 end diff --git a/base/number.jl b/base/number.jl index 51becf7cd7a6b..80846e48373f6 100644 --- a/base/number.jl +++ b/base/number.jl @@ -1,21 +1,13 @@ ## generic operations on numbers ## -isreal(x::Number) = false -isreal(x::Real) = true - -isinteger(x::Number) = false -isinteger(x::Integer) = true - -real_valued(x::Real) = true -integer_valued(x::Integer) = true - -isbool(x::Number) = false -isbool(x::Bool) = true +isrealvalued(x::Real) = true +isintegervalued(x::Integer) = true size(x::Number) = () size(x::Number,d) = convert(Int,d)<1 ? throw(BoundsError()) : 1 eltype(x::Number) = typeof(x) eltype{T<:Number}(::Type{T}) = T +isela{Ta,Tb}(::Ta,::Type{Tb}) = Ta <: Tb ndims(x::Number) = 0 ndims{T<:Number}(::Type{T}) = 0 length(x::Number) = 1 diff --git a/base/rational.jl b/base/rational.jl index aa451aa253c90..7ac2b1ace58f3 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -92,11 +92,11 @@ isfinite(x::Rational) = x.den != 0 typemin{T<:Integer}(::Type{Rational{T}}) = -one(T)//zero(T) typemax{T<:Integer}(::Type{Rational{T}}) = one(T)//zero(T) -integer_valued(x::Rational) = x.den == 1 -float64_valued(x::Rational) = abs(x.num) <= x.den*maxintfloat(Float64) +isintegervalued(x::Rational) = x.den == 1 +isfloat64valued(x::Rational) = abs(x.num) <= x.den*maxintfloat(Float64) -hash(x::Rational) = integer_valued(x) ? hash(x.num) : - float64_valued(x) ? hash(float64(x)) : +hash(x::Rational) = isintegervalued(x) ? hash(x.num) : + isfloat64valued(x) ? hash(float64(x)) : bitmix(hash(x.num),hash(x.den)) -(x::Rational) = (-x.num) // x.den @@ -111,8 +111,8 @@ hash(x::Rational) = integer_valued(x) ? hash(x.num) : ==(x::Integer , y::Rational) = y == x # needed to avoid ambiguity between ==(x::Real, z::Complex) and ==(x::Rational, y::Number) -==(z::Complex , x::Rational) = real_valued(z) && real(z) == x -==(x::Rational, z::Complex ) = real_valued(z) && real(z) == x +==(z::Complex , x::Rational) = isrealvalued(z) && real(z) == x +==(x::Rational, z::Complex ) = isrealvalued(z) && real(z) == x ==(x::Rational, y::Number ) = x.num == x.den*y ==(x::Number , y::Rational) = y == x diff --git a/base/sparse.jl b/base/sparse.jl index 95543106dd504..80c2dfab24cda 100644 --- a/base/sparse.jl +++ b/base/sparse.jl @@ -442,7 +442,7 @@ function sprand(m::Integer, n::Integer, density::FloatingPoint, rng::Function, v I = rand!(1:m, Array(Int, numnz)) J = rand!(1:n, Array(Int, numnz)) S = sparse(I, J, v, m, n) - if !isbool(v) + if !isela(v,Bool) S.nzval = rng(nnz(S)) end diff --git a/doc/helpdb.jl b/doc/helpdb.jl index bd1d4fc9bef67..6e67cbd689997 100644 --- a/doc/helpdb.jl +++ b/doc/helpdb.jl @@ -808,26 +808,19 @@ collection) "), -("Set-Like Collections","Base","add_each!","add_each!(collection, -iterable) - - - Adds each element in iterable to the collection. - -"), - ("Set-Like Collections","Base","Set","Set(x...) Construct a \"Set\" with the given elements. Should be used instead - of \"IntSet\" for sparse integer sets. + of \"IntSet\" for sparse integer sets, or for sets of arbitrary + objects. "), ("Set-Like Collections","Base","IntSet","IntSet(i...) - Construct an \"IntSet\" of the given integers. Implemented as a bit + Construct a sorted set of the given integers. Implemented as a bit string, and therefore designed for dense integer sets. If the set will be sparse (for example holding a single very large integer), use \"Set\" instead. @@ -842,11 +835,10 @@ iterable) "), -("Set-Like Collections","Base","union!","union!(s1, s2) +("Set-Like Collections","Base","union!","union!(s, iterable) - Constructs the union of IntSets s1 and s2, stores the result in - \"s1\". + Union each element of \"iterable\" into set \"s\" in-place. "), @@ -866,6 +858,13 @@ iterable) "), +("Set-Like Collections","Base","setdiff!","setdiff!(s, iterable) + + + Remove each element of \"iterable\" from set \"s\" in-place. + +"), + ("Set-Like Collections","Base","symdiff","symdiff(s1, s2...) @@ -912,13 +911,6 @@ iterable) "), -("Set-Like Collections","Base","del_each!","del_each!(s, itr) - - - Deletes each element of itr in set s in-place. - -"), - ("Set-Like Collections","Base","intersect!","intersect!(s1, s2) @@ -2510,41 +2502,39 @@ stream[, offset]) "), ("Mathematical Functions","Base","round","round(x[, digits[, base]]) --> FloatingPoint - \"round(x)\" returns the nearest integer to \"x\". \"round(x, - digits)\" rounds to the specified number of digits after the - decimal place, or before if negative, e.g., \"round(pi,2)\" is - \"3.14\". \"round(x, digits, base)\" rounds using a different base, - defaulting to 10, e.g., \"round(pi, 3, 2)\" is \"3.125\". + \"round(x)\" returns the nearest integral value of the same type as + \"x\" to \"x\". \"round(x, digits)\" rounds to the specified number + of digits after the decimal place, or before if negative, e.g., + \"round(pi,2)\" is \"3.14\". \"round(x, digits, base)\" rounds + using a different base, defaulting to 10, e.g., \"round(pi, 3, 2)\" + is \"3.125\". "), -("Mathematical Functions","Base","ceil","ceil(x[, digits[, base]]) -> -FloatingPoint +("Mathematical Functions","Base","ceil","ceil(x[, digits[, base]]) - Returns the nearest integer not less than \"x\". \"digits\" and - \"base\" work as above. + Returns the nearest integral value of the same type as \"x\" not + less than \"x\". \"digits\" and \"base\" work as above. "), ("Mathematical Functions","Base","floor","floor(x[, digits[, base]]) --> FloatingPoint - Returns the nearest integer not greater than \"x\". \"digits\" and - \"base\" work as above. + Returns the nearest integral value of the same type as \"x\" not + greater than \"x\". \"digits\" and \"base\" work as above. "), ("Mathematical Functions","Base","trunc","trunc(x[, digits[, base]]) --> FloatingPoint - Returns the nearest integer not greater in magnitude than \"x\". - \"digits\" and \"base\" work as above. + Returns the nearest integral value of the same type as \"x\" not + greater in magnitude than \"x\". \"digits\" and \"base\" work as + above. "), @@ -2577,7 +2567,6 @@ FloatingPoint "), ("Mathematical Functions","Base","signif","signif(x, digits[, base]) --> FloatingPoint Rounds (in the sense of \"round\") \"x\" so that there are @@ -2702,28 +2691,28 @@ FloatingPoint "), -("Mathematical Functions","Base","erfinv","erfinv(x) +("Mathematical Functions","Base","dawson","dawson(x) - Compute the inverse error function of a real \"x\", - so that \\operatorname{erf}(\\operatorname{erfinv}(x)) = x. + Compute the Dawson function (scaled imaginary error function) of + \"x\", defined by \\frac{\\sqrt{\\pi}}{2} e^{-x^2} + \\operatorname{erfi}(x). "), -("Mathematical Functions","Base","erfcinv","erfcinv(x) +("Mathematical Functions","Base","erfinv","erfinv(x) - Compute the inverse complementary error function of a real \"x\", - so that \\operatorname{erfc}(\\operatorname{erfcinv}(x)) = x. + Compute the inverse error function of a real \"x\", defined by + \\operatorname{erf}(\\operatorname{erfinv}(x)) = x. "), -("Mathematical Functions","Base","dawson","dawson(x) +("Mathematical Functions","Base","erfcinv","erfcinv(x) - Compute the Dawson function (scaled imaginary error function) of - \"x\", defined by \\frac{\\sqrt{\\pi}}{2} e^{-x^2} - \\operatorname{erfi}(x). + Compute the inverse error complementary function of a real \"x\", + defined by \\operatorname{erfc}(\\operatorname{erfcinv}(x)) = x. "), @@ -3153,13 +3142,6 @@ FloatingPoint "), -("Data Formats","Base","isbool","isbool(x) - - - Test whether number or array is boolean - -"), - ("Data Formats","Base","int","int(x) @@ -3187,13 +3169,6 @@ FloatingPoint "), -("Data Formats","Base","isinteger","isinteger(x) - - - Test whether a number or array is of integer type - -"), - ("Data Formats","Base","signed","signed(x) @@ -3319,7 +3294,7 @@ FloatingPoint "), -("Data Formats","Base","float64_valued","float64_valued(x::Rational) +("Data Formats","Base","isfloat64valued","isfloat64valued(x::Rational) True if \"x\" can be losslessly represented as a \"Float64\" data @@ -3355,20 +3330,6 @@ FloatingPoint "), -("Data Formats","Base","iscomplex","iscomplex(x) -> Bool - - - Test whether a number or array is of a complex type - -"), - -("Data Formats","Base","isreal","isreal(x) -> Bool - - - Test whether a number or array is of a real type - -"), - ("Data Formats","Base","bswap","bswap(n) @@ -3533,14 +3494,14 @@ FloatingPoint "), -("Numbers","Base","integer_valued","integer_valued(x) +("Numbers","Base","isintegervalued","isintegervalued(x) Test whether \"x\" is numerically equal to some integer "), -("Numbers","Base","real_valued","real_valued(x) +("Numbers","Base","isrealvalued","isrealvalued(x) Test whether \"x\" is numerically equal to some real number @@ -3768,6 +3729,13 @@ Integer "), +("Arrays","Base","isela","isela(A, T) + + + Tests whether A or its elements are of type T + +"), + ("Arrays","Base","length","length(A) -> Integer @@ -4860,13 +4828,14 @@ flags[, timelimit]]]) "), ("Parallel Computing","Base","addprocs","addprocs({\"host1\", -\"host2\", ...}; tunnel=false) +\"host2\", ...}; tunnel=false, dir=JULIA_HOME) Add processes on remote machines via SSH. Requires julia to be installed in the same location on each node, or to be available via a shared file system. If \"tunnel\" is \"true\" then SSH tunneling - will be used. + will be used. Named argument \"dir\" optionally specifies the + location of the julia binaries on the worker nodes. "), @@ -5492,7 +5461,7 @@ own]) "), -("Tasks","Base","tls","tls(symbol) +("Tasks","Base","task_local_storage","task_local_storage(symbol) Look up the value of a symbol in the current task's task-local @@ -5500,7 +5469,8 @@ own]) "), -("Tasks","Base","tls","tls(symbol, value) +("Tasks","Base","task_local_storage","task_local_storage(symbol, +value) Assign a value to a symbol in the current task's task-local @@ -5508,6 +5478,73 @@ own]) "), +("Base.Collections","Base.Collections","PriorityQueue{K,V}","Priority +Queue{K,V}([ord]) + + + Construct a new PriorityQueue, with keys of type K and + values/priorites of type V. If an order is not given, the priority + queue is min-ordered using the default comparison for V. + +"), + +("Base.Collections","Base.Collections","enqueue!","enqueue!(pq, k, v) + + + Insert the a key \"k\" into a priority queue \"pq\" with priority + \"v\". + +"), + +("Base.Collections","Base.Collections","dequeue!","dequeue!(pq) + + + Remove and return the lowest priority key from a priority queue. + +"), + +("Base.Collections","Base.Collections","heapify","heapify(v[, ord]) + + + Return a new vector in binary heap order, optionally using the + given ordering. + +"), + +("Base.Collections","Base.Collections","heapify!","heapify!(v[, ord]) + + + In-place heapify. + +"), + +("Base.Collections","Base.Collections","isheap","isheap(v[, ord]) + + + Return true iff an array is heap-ordered according to the given + order. + +"), + +("Base.Collections","Base.Collections","heappush!","heappush!(v[, +ord]) + + + Given a binary heap-ordered array, push a new element, preserving + the heap property. For efficiency, this function does not check + that the array is indeed heap-ordered. + +"), + +("Base.Collections","Base.Collections","heappop!","heappop!(v[, ord]) + + + Given a binary heap-ordered array, remove and return the lowest + ordered element. For efficiency, this function does not check that + the array is indeed heap-ordered. + +"), + ("Constants","Base","OS_NAME","OS_NAME @@ -6702,11 +6739,10 @@ y) ("Base.Sort","Base.Sort","searchsorted","searchsorted(a, x[, ord]) - Returns the index of the first value of \"a\" equal to or - succeeding \"x\", according to ordering \"ord\" (default: - \"Sort.Forward\"). - - Alias for \"searchsortedfirst()\" + Returns the range of indices of \"a\" equal to \"x\", assuming + \"a\" is sorted according to ordering \"ord\" (default: + \"Sort.Forward\"). Returns an empty range located at the insertion + point if \"a\" does not contain \"x\". "), diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index cfefc2683b70f..bf71f0b2ebf57 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -1771,10 +1771,6 @@ Data Formats Convert a number or numeric array to boolean -.. function:: isbool(x) - - Test whether number or array is boolean - .. function:: int(x) Convert a number or array to the default integer type on your platform. Alternatively, ``x`` can be a string, which is parsed as an integer. @@ -1787,10 +1783,6 @@ Data Formats Convert a number or array to integer type. If ``x`` is already of integer type it is unchanged, otherwise it converts it to the default integer type on your platform. -.. function:: isinteger(x) - - Test whether a number or array is of integer type - .. function:: signed(x) Convert a number to a signed integer @@ -1861,7 +1853,7 @@ Data Formats Get the exponent of a normalized floating-point number. -.. function:: float64_valued(x::Rational) +.. function:: isfloat64valued(x::Rational) True if ``x`` can be losslessly represented as a ``Float64`` data type @@ -1881,14 +1873,6 @@ Data Formats Convert real numbers or arrays to complex -.. function:: iscomplex(x) -> Bool - - Test whether a number or array is of a complex type - -.. function:: isreal(x) -> Bool - - Test whether a number or array is of a real type - .. function:: bswap(n) Byte-swap an integer @@ -1981,11 +1965,11 @@ Numbers Get the previous floating point number in lexicographic order -.. function:: integer_valued(x) +.. function:: isintegervalued(x) Test whether ``x`` is numerically equal to some integer -.. function:: real_valued(x) +.. function:: isrealvalued(x) Test whether ``x`` is numerically equal to some real number @@ -2124,6 +2108,10 @@ Basic functions Returns the type of the elements contained in A +.. function:: isela(A,T) + + Tests whether A or its elements are of type T + .. function:: length(A) -> Integer Returns the number of elements in A (note that this differs from MATLAB where ``length(A)`` is the largest dimension of ``A``) diff --git a/test/blas.jl b/test/blas.jl index c4aa8e97ececa..fa6d7860b5252 100644 --- a/test/blas.jl +++ b/test/blas.jl @@ -50,7 +50,7 @@ for elty in (Float32, Float64, Complex64, Complex128) @assert all(LinAlg.BLAS.gemm('N', 'N', I4, U4) .== U4) @assert all(LinAlg.BLAS.gemm('N', 'T', I4, U4) .== L4) # gemm compared to (sy)(he)rk - if iscomplex(elm1) + if isela(elm1,Complex) @assert all(triu(LinAlg.BLAS.herk('U', 'N', U4)) .== triu(LinAlg.BLAS.gemm('N', 'T', U4, U4))) @assert all(tril(LinAlg.BLAS.herk('L', 'N', U4)) .== tril(LinAlg.BLAS.gemm('N', 'T', U4, U4))) @assert all(triu(LinAlg.BLAS.herk('U', 'N', L4)) .== triu(LinAlg.BLAS.gemm('N', 'T', L4, L4))) diff --git a/test/linalg.jl b/test/linalg.jl index 7fb2ec6fee096..412ebdcbb0bbf 100644 --- a/test/linalg.jl +++ b/test/linalg.jl @@ -66,13 +66,13 @@ for elty in (Float32, Float64, Complex64, Complex128) @test_approx_eq f[:vectors]*f[:Schur]*f[:vectors]' a @test_approx_eq sort(real(f[:values])) sort(real(d)) @test_approx_eq sort(imag(f[:values])) sort(imag(d)) - @test istriu(f[:Schur]) || isreal(a) + @test istriu(f[:Schur]) || isela(a,Real) f = schurfact(a[1:5,1:5], a[6:10,6:10]) # Generalized Schur @test_approx_eq f[:Q]*f[:S]*f[:Z]' a[1:5,1:5] @test_approx_eq f[:Q]*f[:T]*f[:Z]' a[6:10,6:10] - @test istriu(f[:S]) || isreal(a) - @test istriu(f[:T]) || isreal(a) + @test istriu(f[:S]) || isela(a,Real) + @test istriu(f[:T]) || isela(a,Real) usv = svdfact(a) # singular value decomposition @test_approx_eq usv[:U]*scale(usv[:S],usv[:Vt]) a diff --git a/test/mpfr.jl b/test/mpfr.jl index 88497703c61c1..811f08711d2e4 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -179,9 +179,9 @@ x = BigFloat(12) @test get_precision(x) == old_precision @test_fails set_bigfloat_precision(1) -# integer_valued -@test integer_valued(BigFloat(12)) -@test !integer_valued(BigFloat(12.12)) +# isintegervalued +@test isintegervalued(BigFloat(12)) +@test !isintegervalued(BigFloat(12.12)) # nextfloat / prevfloat with_bigfloat_precision(53) do From 9db107148bd85502253ca2868a6fadf9ac716e39 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Sat, 11 May 2013 10:11:50 +0100 Subject: [PATCH 2/4] rename isinteger/realvalued -> isinteger/real, isela -> iseltype --- base/abstractarray.jl | 2 +- base/complex.jl | 14 +++++++------- base/deprecated.jl | 12 +++++------- base/exports.jl | 8 ++++---- base/floatfuncs.jl | 2 +- base/linalg/arnoldi.jl | 2 +- base/linalg/dense.jl | 4 ++-- base/linalg/factorization.jl | 8 ++++---- base/linalg/lapack.jl | 12 ++++++------ base/mpfr.jl | 12 ++++++------ base/number.jl | 5 ++--- base/rational.jl | 12 ++++++------ base/sparse.jl | 2 +- doc/helpdb.jl | 8 ++++---- doc/stdlib/base.rst | 8 ++++---- test/blas.jl | 2 +- test/linalg.jl | 6 +++--- test/mpfr.jl | 6 +++--- 18 files changed, 61 insertions(+), 64 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index c8a34ac2ef099..ab20ac3dc0376 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -10,7 +10,7 @@ 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)) -isela{Ta,n,Tb}(::AbstractArray{Ta,n},::Type{Tb}) = Ta <: Tb +iseltype(x,T) = eltype(x) <: T ndims{T,n}(::AbstractArray{T,n}) = n ndims{T,n}(::Type{AbstractArray{T,n}}) = n ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T)) diff --git a/base/complex.jl b/base/complex.jl index 587940ad49fc6..e001fce096f0b 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -43,8 +43,8 @@ for fn in _numeric_conversion_func_names @eval $fn(z::Complex) = complex($fn(real(z)),$fn(imag(z))) end -isrealvalued{T<:Real}(z::Complex{T}) = imag(z) == 0 -isintegervalued(z::Complex) = isrealvalued(z) && isintegervalued(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)) @@ -104,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) = isrealvalued(z) && real(z) == x -==(x::Real, z::Complex) = isrealvalued(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) = isrealvalued(z) && isequal(real(z),x) -isequal(x::Real, z::Complex) = isrealvalued(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)); isrealvalued(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)) diff --git a/base/deprecated.jl b/base/deprecated.jl index 609d0213dfd12..44abb66a1c156 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -193,17 +193,15 @@ export PipeString @deprecate unsafe_assign unsafe_store! @deprecate add_each! union! @deprecate del_each! setdiff! -@deprecate real_valued isrealvalued -@deprecate integer_valued isintegervalued -@deprecate float64_valued isfloat64valued +@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) isela(x,Bool) -@deprecate isinteger(x) isela(x,Integer) -@deprecate isreal(x) isela(x,Real) -@deprecate iscomplex(x) isela(x,Complex) +@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 diff --git a/base/exports.jl b/base/exports.jl index e64c068b402f9..1b73eb151b7ad 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -359,18 +359,18 @@ export invmod, iround, isdenormal, - isela, + iseltype, iseven, isfinite, - isfloat64valued, + isfloat64, isinf, - isintegervalued, + isinteger, isnan, isodd, ispow2, isprime, isqrt, - isrealvalued, + isreal, itrunc, lcm, ldexp, diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 147ce514baedf..23e7b05633ae8 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -29,7 +29,7 @@ maxintfloat(::Type{Float32}) = float32(16777216.) maxintfloat{T<:FloatingPoint}(x::T) = maxintfloat(T) maxintfloat() = maxintfloat(Float64) -isintegervalued(x::FloatingPoint) = (trunc(x)==x)&isfinite(x) +isinteger(x::FloatingPoint) = (trunc(x)==x)&isfinite(x) ## precision, as defined by the effective number of bits in the mantissa ## get_precision(::Float32) = 24 diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index 2e3c8614134ce..e3fc1f49b59cf 100644 --- a/base/linalg/arnoldi.jl +++ b/base/linalg/arnoldi.jl @@ -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 = isela(A,Complex) + cmplx = iseltype(A,Complex) bmat = "I" # Compute the Ritz values and Ritz vectors diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index 1743a464faf6f..d6f2d4bb42029 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -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 isintegervalued(p) + if isinteger(p) ip = integer(real(p)) if ip < 0 return inv(Base.power_by_squaring(A, -ip)) @@ -382,7 +382,7 @@ function sqrtm(A::StridedMatrix, cond::Bool) if ishermitian(A) return sqrtm(Hermitian(A), cond) else - SchurF = schurfact!(isela(A,Complex) ? 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]) diff --git a/base/linalg/factorization.jl b/base/linalg/factorization.jl index 4b847f5328de5..3561fa1c81fff 100644 --- a/base/linalg/factorization.jl +++ b/base/linalg/factorization.jl @@ -265,7 +265,7 @@ function A_mul_Bc{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRPackedQ{T}) else throw(DimensionMismatch("")) end - LAPACK.gemqrt!('R', isela(B.vs,Complex) ? '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)] @@ -360,7 +360,7 @@ function A_mul_Bc{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRPivotedQ{T}) else throw(DimensionMismatch("")) end - LAPACK.ormqr!('R', isela(B.hh,Complex) ? '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 @@ -467,11 +467,11 @@ eigvals(x::Number) = [one(x)] #Computes maximum and minimum eigenvalue function eigmax(A::Union(Number, StridedMatrix)) v = eigvals(A) - isela(v,Complex) ? 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) - isela(v,Complex) ? 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' diff --git a/base/linalg/lapack.jl b/base/linalg/lapack.jl index ccc30e9df6a00..8625caa727c22 100644 --- a/base/linalg/lapack.jl +++ b/base/linalg/lapack.jl @@ -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 = isela(A,Complex) + cmplx = iseltype(A,Complex) if cmplx; rwork = Array($relty, 2n); end for i in 1:2 if cmplx @@ -575,7 +575,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 = isela(A,Complex) + cmplx = iseltype(A,Complex) if cmplx W = Array($elty, n) rwork = Array($relty, 2n) @@ -642,7 +642,7 @@ for (geev, gesvd, gesdd, ggsvd, elty, relty) in work = Array($elty, 1) lwork = blas_int(-1) S = Array($relty, minmn) - cmplx = isela(A,Complex) + cmplx = iseltype(A,Complex) if cmplx rwork = Array($relty, job == 'N' ? 7*minmn : 5*minmn*minmn + 5*minmn) end @@ -694,7 +694,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 = isela(A,Complex) + cmplx = iseltype(A,Complex) if cmplx; rwork = Array($relty, 5minmn); end lwork = blas_int(-1) info = Array(BlasInt, 1) @@ -755,7 +755,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 = isela(A,Complex) + cmplx = iseltype(A,Complex) if cmplx; rwork = Array($relty, 2n); end iwork = Array(BlasInt, n) info = Array(BlasInt, 1) @@ -1476,7 +1476,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 = isela(A,Complex) + cmplx = iseltype(A,Complex) n = size(A, 1) W = Array($relty, n) work = Array($elty, 1) diff --git a/base/mpfr.jl b/base/mpfr.jl index 59aa7e88cc536..4caa5e5520424 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -17,7 +17,7 @@ export import Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely, bessely0, bessely1, ceil, cmp, convert, copysign, exp, exp2, - exponent, factorial, floor, hypot, isintegervalued, iround, isfinite, + exponent, factorial, floor, hypot, isinteger, iround, isfinite, isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf, nextfloat, prevfloat, promote_rule, rem, round, show, showcompact, sum, sqrt, string, trunc, get_precision, exp10, expm1, gamma, lgamma, digamma, @@ -102,7 +102,7 @@ convert(::Type{FloatingPoint}, x::BigInt) = BigFloat(x) for to in (Int8, Int16, Int32, Int64) @eval begin function convert(::Type{$to}, x::BigFloat) - (isintegervalued(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) + (isinteger(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) convert($to, ccall((:mpfr_get_si,:libmpfr), Clong, (Ptr{BigFloat}, Int32), &x, RoundToZero)) end @@ -112,7 +112,7 @@ end for to in (Uint8, Uint16, Uint32, Uint64) @eval begin function convert(::Type{$to}, x::BigFloat) - (isintegervalued(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) + (isinteger(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) convert($to, ccall((:mpfr_get_ui,:libmpfr), Culong, (Ptr{BigFloat}, Int32), &x, RoundToZero)) end @@ -120,7 +120,7 @@ for to in (Uint8, Uint16, Uint32, Uint64) end function convert(::Type{BigInt}, x::BigFloat) - if isintegervalued(x) + if isinteger(x) return itrunc(x) else throw(InexactError()) @@ -460,7 +460,7 @@ function bessely(n::Integer, x::BigFloat) end function factorial(x::BigFloat) - if x < 0 || !isintegervalued(x) + if x < 0 || !isinteger(x) throw(DomainError()) end ui = convert(Culong, x) @@ -617,7 +617,7 @@ function exponent(x::BigFloat) return ccall((:mpfr_get_exp, :libmpfr), Clong, (Ptr{BigFloat},), &x) - 1 end -function isintegervalued(x::BigFloat) +function isinteger(x::BigFloat) return ccall((:mpfr_integer_p, :libmpfr), Int32, (Ptr{BigFloat},), &x) != 0 end diff --git a/base/number.jl b/base/number.jl index 80846e48373f6..41479cc56974e 100644 --- a/base/number.jl +++ b/base/number.jl @@ -1,13 +1,12 @@ ## generic operations on numbers ## -isrealvalued(x::Real) = true -isintegervalued(x::Integer) = true +isreal(x::Real) = true +isinteger(x::Integer) = true size(x::Number) = () size(x::Number,d) = convert(Int,d)<1 ? throw(BoundsError()) : 1 eltype(x::Number) = typeof(x) eltype{T<:Number}(::Type{T}) = T -isela{Ta,Tb}(::Ta,::Type{Tb}) = Ta <: Tb ndims(x::Number) = 0 ndims{T<:Number}(::Type{T}) = 0 length(x::Number) = 1 diff --git a/base/rational.jl b/base/rational.jl index 7ac2b1ace58f3..4011e66a38165 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -92,11 +92,11 @@ isfinite(x::Rational) = x.den != 0 typemin{T<:Integer}(::Type{Rational{T}}) = -one(T)//zero(T) typemax{T<:Integer}(::Type{Rational{T}}) = one(T)//zero(T) -isintegervalued(x::Rational) = x.den == 1 -isfloat64valued(x::Rational) = abs(x.num) <= x.den*maxintfloat(Float64) +isinteger(x::Rational) = x.den == 1 +isfloat64(x::Rational) = abs(x.num) <= x.den*maxintfloat(Float64) -hash(x::Rational) = isintegervalued(x) ? hash(x.num) : - isfloat64valued(x) ? hash(float64(x)) : +hash(x::Rational) = isinteger(x) ? hash(x.num) : + isfloat64(x) ? hash(float64(x)) : bitmix(hash(x.num),hash(x.den)) -(x::Rational) = (-x.num) // x.den @@ -111,8 +111,8 @@ hash(x::Rational) = isintegervalued(x) ? hash(x.num) : ==(x::Integer , y::Rational) = y == x # needed to avoid ambiguity between ==(x::Real, z::Complex) and ==(x::Rational, y::Number) -==(z::Complex , x::Rational) = isrealvalued(z) && real(z) == x -==(x::Rational, z::Complex ) = isrealvalued(z) && real(z) == x +==(z::Complex , x::Rational) = isreal(z) && real(z) == x +==(x::Rational, z::Complex ) = isreal(z) && real(z) == x ==(x::Rational, y::Number ) = x.num == x.den*y ==(x::Number , y::Rational) = y == x diff --git a/base/sparse.jl b/base/sparse.jl index 80c2dfab24cda..24f7f0fa58f71 100644 --- a/base/sparse.jl +++ b/base/sparse.jl @@ -442,7 +442,7 @@ function sprand(m::Integer, n::Integer, density::FloatingPoint, rng::Function, v I = rand!(1:m, Array(Int, numnz)) J = rand!(1:n, Array(Int, numnz)) S = sparse(I, J, v, m, n) - if !isela(v,Bool) + if !iseltype(v,Bool) S.nzval = rng(nnz(S)) end diff --git a/doc/helpdb.jl b/doc/helpdb.jl index 6e67cbd689997..12e3b17126c8e 100644 --- a/doc/helpdb.jl +++ b/doc/helpdb.jl @@ -3294,7 +3294,7 @@ stream[, offset]) "), -("Data Formats","Base","isfloat64valued","isfloat64valued(x::Rational) +("Data Formats","Base","isfloat64","isfloat64(x::Rational) True if \"x\" can be losslessly represented as a \"Float64\" data @@ -3494,14 +3494,14 @@ stream[, offset]) "), -("Numbers","Base","isintegervalued","isintegervalued(x) +("Numbers","Base","isinteger","isinteger(x) Test whether \"x\" is numerically equal to some integer "), -("Numbers","Base","isrealvalued","isrealvalued(x) +("Numbers","Base","isreal","isreal(x) Test whether \"x\" is numerically equal to some real number @@ -3729,7 +3729,7 @@ Integer "), -("Arrays","Base","isela","isela(A, T) +("Arrays","Base","iseltype","iseltype(A, T) Tests whether A or its elements are of type T diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index bf71f0b2ebf57..5e75efdd3ab84 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -1853,7 +1853,7 @@ Data Formats Get the exponent of a normalized floating-point number. -.. function:: isfloat64valued(x::Rational) +.. function:: isfloat64(x::Rational) True if ``x`` can be losslessly represented as a ``Float64`` data type @@ -1965,11 +1965,11 @@ Numbers Get the previous floating point number in lexicographic order -.. function:: isintegervalued(x) +.. function:: isinteger(x) Test whether ``x`` is numerically equal to some integer -.. function:: isrealvalued(x) +.. function:: isreal(x) Test whether ``x`` is numerically equal to some real number @@ -2108,7 +2108,7 @@ Basic functions Returns the type of the elements contained in A -.. function:: isela(A,T) +.. function:: iseltype(A,T) Tests whether A or its elements are of type T diff --git a/test/blas.jl b/test/blas.jl index fa6d7860b5252..cbac8861b4dc1 100644 --- a/test/blas.jl +++ b/test/blas.jl @@ -50,7 +50,7 @@ for elty in (Float32, Float64, Complex64, Complex128) @assert all(LinAlg.BLAS.gemm('N', 'N', I4, U4) .== U4) @assert all(LinAlg.BLAS.gemm('N', 'T', I4, U4) .== L4) # gemm compared to (sy)(he)rk - if isela(elm1,Complex) + if iseltype(elm1,Complex) @assert all(triu(LinAlg.BLAS.herk('U', 'N', U4)) .== triu(LinAlg.BLAS.gemm('N', 'T', U4, U4))) @assert all(tril(LinAlg.BLAS.herk('L', 'N', U4)) .== tril(LinAlg.BLAS.gemm('N', 'T', U4, U4))) @assert all(triu(LinAlg.BLAS.herk('U', 'N', L4)) .== triu(LinAlg.BLAS.gemm('N', 'T', L4, L4))) diff --git a/test/linalg.jl b/test/linalg.jl index 412ebdcbb0bbf..6c30e16333b55 100644 --- a/test/linalg.jl +++ b/test/linalg.jl @@ -66,13 +66,13 @@ for elty in (Float32, Float64, Complex64, Complex128) @test_approx_eq f[:vectors]*f[:Schur]*f[:vectors]' a @test_approx_eq sort(real(f[:values])) sort(real(d)) @test_approx_eq sort(imag(f[:values])) sort(imag(d)) - @test istriu(f[:Schur]) || isela(a,Real) + @test istriu(f[:Schur]) || iseltype(a,Real) f = schurfact(a[1:5,1:5], a[6:10,6:10]) # Generalized Schur @test_approx_eq f[:Q]*f[:S]*f[:Z]' a[1:5,1:5] @test_approx_eq f[:Q]*f[:T]*f[:Z]' a[6:10,6:10] - @test istriu(f[:S]) || isela(a,Real) - @test istriu(f[:T]) || isela(a,Real) + @test istriu(f[:S]) || iseltype(a,Real) + @test istriu(f[:T]) || iseltype(a,Real) usv = svdfact(a) # singular value decomposition @test_approx_eq usv[:U]*scale(usv[:S],usv[:Vt]) a diff --git a/test/mpfr.jl b/test/mpfr.jl index 811f08711d2e4..6aba4961ae3a4 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -179,9 +179,9 @@ x = BigFloat(12) @test get_precision(x) == old_precision @test_fails set_bigfloat_precision(1) -# isintegervalued -@test isintegervalued(BigFloat(12)) -@test !isintegervalued(BigFloat(12.12)) +# isinteger +@test isinteger(BigFloat(12)) +@test !isinteger(BigFloat(12.12)) # nextfloat / prevfloat with_bigfloat_precision(53) do From 34087767a5cef0bed3477384193edf2b6859f6fe Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Sun, 12 May 2013 09:36:05 +0100 Subject: [PATCH 3/4] add array methods for isinteger, isreal, isfloat64 --- base/abstractarray.jl | 7 +++++++ doc/helpdb.jl | 10 ++++++---- doc/stdlib/base.rst | 6 +++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index ab20ac3dc0376..eddd312ebced2 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -11,6 +11,13 @@ 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)) diff --git a/doc/helpdb.jl b/doc/helpdb.jl index 12e3b17126c8e..f9b9b79884855 100644 --- a/doc/helpdb.jl +++ b/doc/helpdb.jl @@ -3297,8 +3297,8 @@ stream[, offset]) ("Data Formats","Base","isfloat64","isfloat64(x::Rational) - True if \"x\" can be losslessly represented as a \"Float64\" data - type + Tests whether \"x\" or all its elements can be losslessly + represented as a \"Float64\" data type "), @@ -3497,14 +3497,16 @@ stream[, offset]) ("Numbers","Base","isinteger","isinteger(x) - Test whether \"x\" is numerically equal to some integer + Test whether \"x\" or all its elements are numerically equal to + some integer "), ("Numbers","Base","isreal","isreal(x) - Test whether \"x\" is numerically equal to some real number + Test whether \"x\" or all its elements are numerically equal to + some real number "), diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 5e75efdd3ab84..909434ed71cb2 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -1855,7 +1855,7 @@ Data Formats .. function:: isfloat64(x::Rational) - True if ``x`` can be losslessly represented as a ``Float64`` data type + Tests whether ``x`` or all its elements can be losslessly represented as a ``Float64`` data type .. function:: complex64(r,i) @@ -1967,11 +1967,11 @@ Numbers .. function:: isinteger(x) - Test whether ``x`` is numerically equal to some integer + Test whether ``x`` or all its elements are numerically equal to some integer .. function:: isreal(x) - Test whether ``x`` is numerically equal to some real number + Test whether ``x`` or all its elements are numerically equal to some real number .. function:: BigInt(x) From d8c118213e9ef457e51332a0a4abf299b752ad36 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Sun, 12 May 2013 14:02:45 +0100 Subject: [PATCH 4/4] missing methods for isfloat64 --- base/floatfuncs.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 23e7b05633ae8..dd4031bf01dbd 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -30,6 +30,9 @@ maxintfloat{T<:FloatingPoint}(x::T) = maxintfloat(T) maxintfloat() = maxintfloat(Float64) 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