From 66cde460d9844487516807f3248744ab8fe967da Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 7 Sep 2017 18:40:53 -0400 Subject: [PATCH] deprecate convert-to-construct fallback --- NEWS.md | 4 ++ base/abstractarray.jl | 19 ++++---- base/array.jl | 17 ++------ base/bitarray.jl | 21 +++------ base/boot.jl | 8 ++++ base/dates/periods.jl | 7 ++- base/deprecated.jl | 15 +++++++ base/libgit2/types.jl | 1 + base/linalg/bidiag.jl | 20 +++++---- base/linalg/cholesky.jl | 34 +++++++-------- base/linalg/diagonal.jl | 12 +++--- base/linalg/eigen.jl | 10 ++--- base/linalg/factorization.jl | 5 ++- base/linalg/givens.jl | 15 ++++--- base/linalg/hessenberg.jl | 16 +++---- base/linalg/ldlt.jl | 22 +++++----- base/linalg/lq.jl | 46 ++++++++++---------- base/linalg/lu.jl | 34 +++++++-------- base/linalg/qr.jl | 58 ++++++++++++------------- base/linalg/rowvector.jl | 2 +- base/linalg/schur.jl | 10 ++--- base/linalg/special.jl | 40 +++++++---------- base/linalg/svd.jl | 10 ++--- base/linalg/symmetric.jl | 18 ++++---- base/linalg/triangular.jl | 26 ++++++----- base/linalg/tridiag.jl | 28 ++++++------ base/nullable.jl | 12 ++---- base/nullabletype.jl | 4 +- base/precompile.jl | 1 - base/range.jl | 49 ++++++++++----------- base/replutil.jl | 2 +- base/sharedarray.jl | 8 ++-- base/socket.jl | 2 +- base/sparse/cholmod.jl | 84 ++++++++++++++++++------------------ base/sparse/sparse.jl | 2 +- base/sparse/sparsematrix.jl | 31 +++++++------ base/sparse/sparsevector.jl | 40 ++++++++--------- base/statistics.jl | 6 +-- base/sysimg.jl | 4 -- base/twiceprecision.jl | 19 ++++---- test/abstractarray.jl | 14 +++--- test/arrayops.jl | 2 +- test/dimensionful.jl | 7 +-- 43 files changed, 388 insertions(+), 397 deletions(-) diff --git a/NEWS.md b/NEWS.md index 751386438661e3..6484933ac26824 100644 --- a/NEWS.md +++ b/NEWS.md @@ -17,6 +17,10 @@ Language changes * The syntax for parametric methods, `function f{T}(x::T)`, has been changed to `function f(x::T) where {T}` ([#11310]). + * The fallback constructor that calls `convert` is deprecated. Instead, new types should + prefer to define constructors, and add `convert` methods that call those constructors + only as necessary ([#15120]). + * The syntax `1.+2` is deprecated, since it is ambiguous: it could mean either `1 .+ 2` (the current meaning) or `1. + 2` ([#19089]). diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 08f0beba1158a0..551ece4f7969df 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -11,6 +11,17 @@ Supertype for `N`-dimensional arrays (or array-like types) with elements of type """ AbstractArray +convert(::Type{T}, a::T) where {T<:AbstractArray} = a +convert(::Type{T}, a::AbstractArray) where {T<:AbstractArray} = T(a) + +if module_name(@__MODULE__) === :Base # avoid method overwrite +# catch undefined constructors before the deprecation kicks in +# TODO: remove when deprecation is removed +function (::Type{T})(arg) where {T<:AbstractArray} + throw(MethodError(T, (arg,))) +end +end + """ size(A::AbstractArray, [dim...]) @@ -839,14 +850,6 @@ isempty(a::AbstractArray) = (_length(a) == 0) # keys with an IndexStyle keys(s::IndexStyle, A::AbstractArray, B::AbstractArray...) = eachindex(s, A, B...) -## Conversions ## - -convert(::Type{AbstractArray{T,N}}, A::AbstractArray{T,N}) where {T,N } = A -convert(::Type{AbstractArray{T,N}}, A::AbstractArray{S,N}) where {T,S,N} = copy!(similar(A,T), A) -convert(::Type{AbstractArray{T}}, A::AbstractArray{S,N}) where {T,S,N} = convert(AbstractArray{T,N}, A) - -convert(::Type{Array}, A::AbstractArray{T,N}) where {T,N} = convert(Array{T,N}, A) - """ of_indices(x, y) diff --git a/base/array.jl b/base/array.jl index 19afbffbbe4ea5..5a1648ba2a75f3 100644 --- a/base/array.jl +++ b/base/array.jl @@ -549,23 +549,14 @@ end one(x::AbstractMatrix{T}) where {T} = _one(one(T), x) oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x) -## Conversions ## - -convert(::Type{Vector}, x::AbstractVector{T}) where {T} = convert(Vector{T}, x) -convert(::Type{Matrix}, x::AbstractMatrix{T}) where {T} = convert(Matrix{T}, x) - -convert(::Type{Array{T}}, x::Array{T,n}) where {T,n} = x -convert(::Type{Array{T,n}}, x::Array{T,n}) where {T,n} = x - -convert(::Type{Array{T}}, x::AbstractArray{S,n}) where {T,n,S} = convert(Array{T,n}, x) -convert(::Type{Array{T,n}}, x::AbstractArray{S,n}) where {T,n,S} = copy!(Array{T,n}(size(x)), x) - promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(promote_type(T,S), a, b) -# constructors should make copies +## Constructors ## if module_name(@__MODULE__) === :Base # avoid method overwrite -(::Type{T})(x::T) where {T<:Array} = copy(x) +# constructors should make copies +Array{T,N}(x::AbstractArray{S,N}) where {T,N,S} = copy!(Array{T,N}(size(x)), x) +AbstractArray{T,N}(A::AbstractArray{S,N}) where {T,N,S} = copy!(similar(A,T), A) end ## copying iterators to containers diff --git a/base/bitarray.jl b/base/bitarray.jl index 71c2702a34d59d..e39f6c7d375c4f 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -497,12 +497,10 @@ function _bitreshape(B::BitArray, dims::NTuple{N,Int}) where N return Br end -## Conversions ## +## Constructors ## -convert(::Type{Array{T}}, B::BitArray{N}) where {T,N} = convert(Array{T,N}, B) -convert(::Type{Array{T,N}}, B::BitArray{N}) where {T,N} = _convert(Array{T,N}, B) # see #15801 -function _convert(::Type{Array{T,N}}, B::BitArray{N}) where {T,N} - A = Array{T}(size(B)) +function Array{T,N}(B::BitArray{N}) where {T,N} + A = Array{T,N}(size(B)) Bc = B.chunks @inbounds for i = 1:length(A) A[i] = unsafe_bitgetindex(Bc, i) @@ -510,8 +508,8 @@ function _convert(::Type{Array{T,N}}, B::BitArray{N}) where {T,N} return A end -convert(::Type{BitArray}, A::AbstractArray{T,N}) where {T,N} = convert(BitArray{N}, A) -function convert(::Type{BitArray{N}}, A::AbstractArray{T,N}) where N where T +BitArray(A::AbstractArray{<:Any,N}) where {N} = BitArray{N}(A) +function BitArray{N}(A::AbstractArray{T,N}) where N where T B = BitArray(size(A)) Bc = B.chunks l = length(B) @@ -536,7 +534,7 @@ function convert(::Type{BitArray{N}}, A::AbstractArray{T,N}) where N where T return B end -function convert(::Type{BitArray{N}}, A::Array{Bool,N}) where N +function BitArray{N}(A::Array{Bool,N}) where N B = BitArray(size(A)) Bc = B.chunks l = length(B) @@ -545,16 +543,9 @@ function convert(::Type{BitArray{N}}, A::Array{Bool,N}) where N return B end -convert(::Type{BitArray{N}}, B::BitArray{N}) where {N} = B -convert(::Type{AbstractArray{T,N}}, B::BitArray{N}) where {T,N} = convert(Array{T,N}, B) - reinterpret(::Type{Bool}, B::BitArray, dims::NTuple{N,Int}) where {N} = reinterpret(B, dims) reinterpret(B::BitArray, dims::NTuple{N,Int}) where {N} = reshape(B, dims) -## Constructors from generic iterables ## - -BitArray(A::AbstractArray{<:Any,N}) where {N} = convert(BitArray{N}, A) - if module_name(@__MODULE__) === :Base # avoid method overwrite (::Type{T})(x::T) where {T<:BitArray} = copy(x) end diff --git a/base/boot.jl b/base/boot.jl index 9c67d81ae21f34..9882134689bc01 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -382,6 +382,13 @@ Array{T}(m::Int, n::Int, o::Int) where {T} = Array{T,3}(m, n, o) Array{T,1}() where {T} = Array{T,1}(0) +(::Type{Array{T,N} where T})(x::AbstractArray{S,N}) where {S,N} = Array{S,N}(x) + +Array(A::AbstractArray{T,N}) where {T,N} = Array{T,N}(A) +Array{T}(A::AbstractArray{S,N}) where {T,N,S} = Array{T,N}(A) + +AbstractArray{T}(A::AbstractArray{S,N}) where {T,S,N} = AbstractArray{T,N}(A) + # primitive Symbol constructors function Symbol(s::String) return ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int), @@ -602,6 +609,7 @@ UInt128(x::BuiltinInts) = toUInt128(x)::UInt128 UInt32(x::Char) = bitcast(UInt32, x) Char(x::UInt32) = bitcast(Char, x) Char(x::Number) = Char(UInt32(x)) +Char(x::Char) = x (::Type{T})(x::Char) where {T<:Number} = T(UInt32(x)) (::Type{T})(x::T) where {T<:Number} = x diff --git a/base/dates/periods.jl b/base/dates/periods.jl index 6f30613c31595b..aa4e984072c6b9 100644 --- a/base/dates/periods.jl +++ b/base/dates/periods.jl @@ -401,8 +401,13 @@ function divexact(x, y) return q end +# TODO: this is needed to prevent undefined Period constructors from +# hitting the deprecated construct-to-convert fallback. +(::Type{T})(p::Period) where {T<:Period} = convert(T, p)::T + # FixedPeriod conversions and promotion rules -const fixedperiod_conversions = [(Week, 7), (Day, 24), (Hour, 60), (Minute, 60), (Second, 1000), (Millisecond, 1000), (Microsecond, 1000), (Nanosecond, 1)] +const fixedperiod_conversions = [(:Week, 7), (:Day, 24), (:Hour, 60), (:Minute, 60), (:Second, 1000), + (:Millisecond, 1000), (:Microsecond, 1000), (:Nanosecond, 1)] for i = 1:length(fixedperiod_conversions) T, n = fixedperiod_conversions[i] N = Int64(1) diff --git a/base/deprecated.jl b/base/deprecated.jl index bc77a0723dd4d4..cf379bfed21297 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1697,6 +1697,21 @@ export hex2num @deprecate convert(dt::Type{<:Integer}, ip::IPAddr) dt(ip) +function (::Type{T})(arg) where {T} + if applicable(convert, T, arg) + sig = which(convert, (Type{T}, typeof(arg))).sig + if sig == (Tuple{typeof(convert),Type{S},Number} where S<:Number) || + sig == (Tuple{typeof(convert),Type{S},AbstractArray} where S<:AbstractArray) + # matches a catch-all converter; will stack overflow + throw(MethodError(T, (arg,))) + end + # if `convert` call would not work, just let the method error happen + depwarn("Constructors no longer fall back to `convert`. A constructor `$T(::$(typeof(arg)))` should be defined instead.", :Type) + end + convert(T, arg)::T +end +# related items to remove in: abstractarray.jl, dates/periods.jl + # Issue #19923 @deprecate ror circshift @deprecate ror! circshift! diff --git a/base/libgit2/types.jl b/base/libgit2/types.jl index 092bea2925b701..dcd22378442bfe 100644 --- a/base/libgit2/types.jl +++ b/base/libgit2/types.jl @@ -20,6 +20,7 @@ struct GitHash <: AbstractGitHash GitHash(val::NTuple{OID_RAWSZ, UInt8}) = new(val) end GitHash() = GitHash(ntuple(i->zero(UInt8), OID_RAWSZ)) +GitHash(h::GitHash) = h """ GitShortHash(hash::GitHash, len::Integer) diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index ceb76007416840..4a5d6038647545 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -93,6 +93,8 @@ function Bidiagonal(A::AbstractMatrix, uplo::Symbol) Bidiagonal(diag(A, 0), diag(A, uplo == :U ? 1 : -1), uplo) end +Bidiagonal(A::Bidiagonal) = A + function getindex(A::Bidiagonal{T}, i::Integer, j::Integer) where T if !((1 <= i <= size(A,2)) && (1 <= j <= size(A,2))) throw(BoundsError(A,(i,j))) @@ -131,7 +133,7 @@ function Base.replace_in_print_matrix(A::Bidiagonal,i::Integer,j::Integer,s::Abs end #Converting from Bidiagonal to dense Matrix -function convert(::Type{Matrix{T}}, A::Bidiagonal) where T +function Matrix{T}(A::Bidiagonal) where T n = size(A, 1) B = zeros(T, n, n) for i = 1:n - 1 @@ -145,14 +147,14 @@ function convert(::Type{Matrix{T}}, A::Bidiagonal) where T B[n,n] = A.dv[n] return B end -convert(::Type{Matrix}, A::Bidiagonal{T}) where {T} = convert(Matrix{T}, A) -convert(::Type{Array}, A::Bidiagonal) = convert(Matrix, A) -full(A::Bidiagonal) = convert(Array, A) +Matrix(A::Bidiagonal{T}) where {T} = Matrix{T}(A) +Array(A::Bidiagonal) = Matrix(A) +full(A::Bidiagonal) = Array(A) promote_rule(::Type{Matrix{T}}, ::Type{<:Bidiagonal{S}}) where {T,S} = Matrix{promote_type(T,S)} #Converting from Bidiagonal to Tridiagonal -Tridiagonal(M::Bidiagonal{T}) where {T} = convert(Tridiagonal{T}, M) -function convert(::Type{Tridiagonal{T}}, A::Bidiagonal) where T +Tridiagonal(M::Bidiagonal{T}) where {T} = Tridiagonal{T}(M) +function Tridiagonal{T}(A::Bidiagonal) where T dv = convert(AbstractVector{T}, A.dv) ev = convert(AbstractVector{T}, A.ev) z = fill!(similar(ev), zero(T)) @@ -161,12 +163,12 @@ end promote_rule(::Type{<:Tridiagonal{T}}, ::Type{<:Bidiagonal{S}}) where {T,S} = Tridiagonal{promote_type(T,S)} # No-op for trivial conversion Bidiagonal{T} -> Bidiagonal{T} -convert(::Type{Bidiagonal{T}}, A::Bidiagonal{T}) where {T} = A +Bidiagonal{T}(A::Bidiagonal{T}) where {T} = A # Convert Bidiagonal to Bidiagonal{T} by constructing a new instance with converted elements -convert(::Type{Bidiagonal{T}}, A::Bidiagonal) where {T} = +Bidiagonal{T}(A::Bidiagonal) where {T} = Bidiagonal(convert(AbstractVector{T}, A.dv), convert(AbstractVector{T}, A.ev), A.uplo) # When asked to convert Bidiagonal to AbstractMatrix{T}, preserve structure by converting to Bidiagonal{T} <: AbstractMatrix{T} -convert(::Type{AbstractMatrix{T}}, A::Bidiagonal) where {T} = convert(Bidiagonal{T}, A) +AbstractMatrix{T}(A::Bidiagonal) where {T} = convert(Bidiagonal{T}, A) broadcast(::typeof(big), B::Bidiagonal) = Bidiagonal(big.(B.dv), big.(B.ev), B.uplo) diff --git a/base/linalg/cholesky.jl b/base/linalg/cholesky.jl index f52d3a281cf1df..958a647e46d349 100644 --- a/base/linalg/cholesky.jl +++ b/base/linalg/cholesky.jl @@ -347,32 +347,32 @@ function cholfact(x::Number, uplo::Symbol=:U) end -function convert(::Type{Cholesky{T}}, C::Cholesky) where T +function Cholesky{T}(C::Cholesky) where T Cnew = convert(AbstractMatrix{T}, C.factors) Cholesky{T, typeof(Cnew)}(Cnew, C.uplo, C.info) end -convert(::Type{Factorization{T}}, C::Cholesky{T}) where {T} = C -convert(::Type{Factorization{T}}, C::Cholesky) where {T} = convert(Cholesky{T}, C) -convert(::Type{CholeskyPivoted{T}},C::CholeskyPivoted{T}) where {T} = C -convert(::Type{CholeskyPivoted{T}},C::CholeskyPivoted) where {T} = +Factorization{T}(C::Cholesky{T}) where {T} = C +Factorization{T}(C::Cholesky) where {T} = Cholesky{T}(C) +CholeskyPivoted{T}(C::CholeskyPivoted{T}) where {T} = C +CholeskyPivoted{T}(C::CholeskyPivoted) where {T} = CholeskyPivoted(AbstractMatrix{T}(C.factors),C.uplo,C.piv,C.rank,C.tol,C.info) -convert(::Type{Factorization{T}}, C::CholeskyPivoted{T}) where {T} = C -convert(::Type{Factorization{T}}, C::CholeskyPivoted) where {T} = convert(CholeskyPivoted{T}, C) +Factorization{T}(C::CholeskyPivoted{T}) where {T} = C +Factorization{T}(C::CholeskyPivoted) where {T} = CholeskyPivoted{T}(C) -convert(::Type{AbstractMatrix}, C::Cholesky) = C.uplo == 'U' ? C[:U]'C[:U] : C[:L]*C[:L]' -convert(::Type{AbstractArray}, C::Cholesky) = convert(AbstractMatrix, C) -convert(::Type{Matrix}, C::Cholesky) = convert(Array, convert(AbstractArray, C)) -convert(::Type{Array}, C::Cholesky) = convert(Matrix, C) -full(C::Cholesky) = convert(AbstractArray, C) +AbstractMatrix(C::Cholesky) = C.uplo == 'U' ? C[:U]'C[:U] : C[:L]*C[:L]' +AbstractArray(C::Cholesky) = AbstractMatrix(C) +Matrix(C::Cholesky) = Array(AbstractArray(C)) +Array(C::Cholesky) = Matrix(C) +full(C::Cholesky) = AbstractArray(C) -function convert(::Type{AbstractMatrix}, F::CholeskyPivoted) +function AbstractMatrix(F::CholeskyPivoted) ip = invperm(F[:p]) (F[:L] * F[:U])[ip,ip] end -convert(::Type{AbstractArray}, F::CholeskyPivoted) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::CholeskyPivoted) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::CholeskyPivoted) = convert(Matrix, F) -full(F::CholeskyPivoted) = convert(AbstractArray, F) +AbstractArray(F::CholeskyPivoted) = AbstractMatrix(F) +Matrix(F::CholeskyPivoted) = Array(AbstractArray(F)) +Array(F::CholeskyPivoted) = Matrix(F) +full(F::CholeskyPivoted) = AbstractArray(F) copy(C::Cholesky) = Cholesky(copy(C.factors), C.uplo, C.info) copy(C::CholeskyPivoted) = CholeskyPivoted(copy(C.factors), C.uplo, C.piv, C.rank, C.tol, C.info) diff --git a/base/linalg/diagonal.jl b/base/linalg/diagonal.jl index dfa8408594780c..b20a1583001d1d 100644 --- a/base/linalg/diagonal.jl +++ b/base/linalg/diagonal.jl @@ -49,12 +49,12 @@ Diagonal(V::AbstractVector{T}) where {T} = Diagonal{T,typeof(V)}(V) Diagonal{T}(V::AbstractVector{T}) where {T} = Diagonal{T,typeof(V)}(V) Diagonal{T}(V::AbstractVector) where {T} = Diagonal{T}(convert(AbstractVector{T}, V)) -convert(::Type{Diagonal{T}}, D::Diagonal{T}) where {T} = D -convert(::Type{Diagonal{T}}, D::Diagonal) where {T} = Diagonal{T}(convert(AbstractVector{T}, D.diag)) -convert(::Type{AbstractMatrix{T}}, D::Diagonal) where {T} = convert(Diagonal{T}, D) -convert(::Type{Matrix}, D::Diagonal) = diagm(D.diag) -convert(::Type{Array}, D::Diagonal) = convert(Matrix, D) -full(D::Diagonal) = convert(Array, D) +Diagonal{T}(D::Diagonal{T}) where {T} = D +Diagonal{T}(D::Diagonal) where {T} = Diagonal{T}(convert(AbstractVector{T}, D.diag)) +AbstractMatrix{T}(D::Diagonal) where {T} = Diagonal{T}(D) +Matrix(D::Diagonal) = diagm(D.diag) +Array(D::Diagonal) = Matrix(D) +full(D::Diagonal) = Array(D) function similar(D::Diagonal, ::Type{T}) where T return Diagonal{T}(similar(D.diag, T)) diff --git a/base/linalg/eigen.jl b/base/linalg/eigen.jl index 0e8e8d549bddf7..4f03d61d94db32 100644 --- a/base/linalg/eigen.jl +++ b/base/linalg/eigen.jl @@ -433,8 +433,8 @@ eigvecs(A::AbstractMatrix, B::AbstractMatrix) = eigvecs(eigfact(A, B)) # Conversion methods ## Can we determine the source/result is Real? This is not stored in the type Eigen -convert(::Type{AbstractMatrix}, F::Eigen) = F.vectors * Diagonal(F.values) / F.vectors -convert(::Type{AbstractArray}, F::Eigen) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::Eigen) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::Eigen) = convert(Matrix, F) -full(F::Eigen) = convert(AbstractArray, F) +AbstractMatrix(F::Eigen) = F.vectors * Diagonal(F.values) / F.vectors +AbstractArray(F::Eigen) = AbstractMatrix(F) +Matrix(F::Eigen) = Array(AbstractArray(F)) +Array(F::Eigen) = Matrix(F) +full(F::Eigen) = AbstractArray(F) diff --git a/base/linalg/factorization.jl b/base/linalg/factorization.jl index 9acaef101ccaac..ccb5cf60c7a734 100644 --- a/base/linalg/factorization.jl +++ b/base/linalg/factorization.jl @@ -45,8 +45,11 @@ function det(F::Factorization) return exp(d)*s end +convert(::Type{T}, f::T) where {T<:Factorization} = f +convert(::Type{T}, f::Factorization) where {T<:Factorization} = T(f) + ### General promotion rules -convert(::Type{Factorization{T}}, F::Factorization{T}) where {T} = F +Factorization{T}(F::Factorization{T}) where {T} = F inv(F::Factorization{T}) where {T} = A_ldiv_B!(F, eye(T, size(F,1))) Base.hash(F::Factorization, h::UInt) = mapreduce(f -> hash(getfield(F, f)), hash, h, 1:nfields(F)) diff --git a/base/linalg/givens.jl b/base/linalg/givens.jl index de621983494d42..7e38e88979531f 100644 --- a/base/linalg/givens.jl +++ b/base/linalg/givens.jl @@ -34,12 +34,15 @@ mutable struct Rotation{T} <: AbstractRotation{T} rotations::Vector{Givens{T}} end -convert(::Type{Givens{T}}, G::Givens{T}) where {T} = G -convert(::Type{Givens{T}}, G::Givens) where {T} = Givens(G.i1, G.i2, convert(T, G.c), convert(T, G.s)) -convert(::Type{Rotation{T}}, R::Rotation{T}) where {T} = R -convert(::Type{Rotation{T}}, R::Rotation) where {T} = Rotation{T}([convert(Givens{T}, g) for g in R.rotations]) -convert(::Type{AbstractRotation{T}}, G::Givens) where {T} = convert(Givens{T}, G) -convert(::Type{AbstractRotation{T}}, R::Rotation) where {T} = convert(Rotation{T}, R) +convert(::Type{T}, r::T) where {T<:AbstractRotation} = r +convert(::Type{T}, r::AbstractRotation) where {T<:AbstractRotation} = T(r) + +Givens{T}(G::Givens{T}) where {T} = G +Givens{T}(G::Givens) where {T} = Givens(G.i1, G.i2, convert(T, G.c), convert(T, G.s)) +Rotation{T}(R::Rotation{T}) where {T} = R +Rotation{T}(R::Rotation) where {T} = Rotation{T}([Givens{T}(g) for g in R.rotations]) +AbstractRotation{T}(G::Givens) where {T} = Givens{T}(G) +AbstractRotation{T}(R::Rotation) where {T} = Rotation{T}(R) adjoint(G::Givens) = Givens(G.i1, G.i2, conj(G.c), -G.s) adjoint(R::Rotation{T}) where {T} = Rotation{T}(reverse!([adjoint(r) for r in R.rotations])) diff --git a/base/linalg/hessenberg.jl b/base/linalg/hessenberg.jl index ebb7e8ea0f665a..73219748b83b41 100644 --- a/base/linalg/hessenberg.jl +++ b/base/linalg/hessenberg.jl @@ -77,14 +77,14 @@ function getindex(A::HessenbergQ, i::Integer, j::Integer) end ## reconstruct the original matrix -convert(::Type{Matrix}, A::HessenbergQ{<:BlasFloat}) = LAPACK.orghr!(1, size(A.factors, 1), copy(A.factors), A.τ) -convert(::Type{Array}, A::HessenbergQ) = convert(Matrix, A) -full(A::HessenbergQ) = convert(Array, A) -convert(::Type{AbstractMatrix}, F::Hessenberg) = (fq = Array(F[:Q]); (fq * F[:H]) * fq') -convert(::Type{AbstractArray}, F::Hessenberg) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::Hessenberg) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::Hessenberg) = convert(Matrix, F) -full(F::Hessenberg) = convert(AbstractArray, F) +Matrix(A::HessenbergQ{<:BlasFloat}) = LAPACK.orghr!(1, size(A.factors, 1), copy(A.factors), A.τ) +Array(A::HessenbergQ) = Matrix(A) +full(A::HessenbergQ) = Array(A) +AbstractMatrix(F::Hessenberg) = (fq = Array(F[:Q]); (fq * F[:H]) * fq') +AbstractArray(F::Hessenberg) = AbstractMatrix(F) +Matrix(F::Hessenberg) = Array(AbstractArray(F)) +Array(F::Hessenberg) = Matrix(F) +full(F::Hessenberg) = AbstractArray(F) A_mul_B!(Q::HessenbergQ{T}, X::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.ormhr!('L', 'N', 1, size(Q.factors, 1), Q.factors, Q.τ, X) diff --git a/base/linalg/ldlt.jl b/base/linalg/ldlt.jl index 00bd27afc699b9..6d5751f7315baa 100644 --- a/base/linalg/ldlt.jl +++ b/base/linalg/ldlt.jl @@ -7,13 +7,13 @@ end size(S::LDLt) = size(S.data) size(S::LDLt, i::Integer) = size(S.data, i) -convert(::Type{LDLt{T,S}}, F::LDLt) where {T,S} = LDLt{T,S}(convert(S, F.data)) +LDLt{T,S}(F::LDLt) where {T,S<:AbstractMatrix} = LDLt{T,S}(convert(S, F.data)) # NOTE: the annotaion <:AbstractMatrix shouldn't be necessary, it is introduced # to avoid an ambiguity warning (see issue #6383) -convert(::Type{LDLt{T}}, F::LDLt{S,U}) where {T,S,U<:AbstractMatrix} = convert(LDLt{T,U}, F) +LDLt{T}(F::LDLt{S,U}) where {T,S,U<:AbstractMatrix} = LDLt{T,U}(F) -convert(::Type{Factorization{T}}, F::LDLt{T}) where {T} = F -convert(::Type{Factorization{T}}, F::LDLt{S,U}) where {T,S,U} = convert(LDLt{T,U}, F) +Factorization{T}(F::LDLt{T}) where {T} = F +Factorization{T}(F::LDLt{S,U}) where {T,S,U} = LDLt{T,U}(F) # SymTridiagonal """ @@ -41,7 +41,7 @@ factorization `F = ldltfact(A)` is to solve the linear system of equations `Ax = """ function ldltfact(M::SymTridiagonal{T}) where T S = typeof(zero(T)/one(T)) - return S == T ? ldltfact!(copy(M)) : ldltfact!(convert(SymTridiagonal{S}, M)) + return S == T ? ldltfact!(copy(M)) : ldltfact!(SymTridiagonal{S}(M)) end factorize(S::SymTridiagonal) = ldltfact(S) @@ -77,15 +77,15 @@ function A_ldiv_B!(S::LDLt{T,M}, B::AbstractVecOrMat{T}) where {T,M<:SymTridiago end # Conversion methods -function convert(::Type{SymTridiagonal}, F::LDLt) +function SymTridiagonal(F::LDLt) e = copy(F.data.ev) d = copy(F.data.dv) e .*= d[1:end-1] d[2:end] += e .* F.data.ev SymTridiagonal(d, e) end -convert(::Type{AbstractMatrix}, F::LDLt) = convert(SymTridiagonal, F) -convert(::Type{AbstractArray}, F::LDLt) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::LDLt) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::LDLt) = convert(Matrix, F) -full(F::LDLt) = convert(AbstractArray, F) +AbstractMatrix(F::LDLt) = SymTridiagonal(F) +AbstractArray(F::LDLt) = AbstractMatrix(F) +Matrix(F::LDLt) = Array(AbstractArray(F)) +Array(F::LDLt) = Matrix(F) +full(F::LDLt) = AbstractArray(F) diff --git a/base/linalg/lq.jl b/base/linalg/lq.jl index 3c535433e6cf55..d40cd156e0031a 100644 --- a/base/linalg/lq.jl +++ b/base/linalg/lq.jl @@ -47,14 +47,14 @@ end copy(A::LQ) = LQ(copy(A.factors), copy(A.τ)) -convert(::Type{LQ{T}},A::LQ) where {T} = LQ(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) -convert(::Type{Factorization{T}}, A::LQ{T}) where {T} = A -convert(::Type{Factorization{T}}, A::LQ) where {T} = convert(LQ{T}, A) -convert(::Type{AbstractMatrix}, A::LQ) = A[:L]*A[:Q] -convert(::Type{AbstractArray}, A::LQ) = convert(AbstractMatrix, A) -convert(::Type{Matrix}, A::LQ) = convert(Array, convert(AbstractArray, A)) -convert(::Type{Array}, A::LQ) = convert(Matrix, A) -full(A::LQ) = convert(AbstractArray, A) +LQ{T}(A::LQ) where {T} = LQ(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) +Factorization{T}(A::LQ{T}) where {T} = A +Factorization{T}(A::LQ) where {T} = LQ{T}(A) +AbstractMatrix(A::LQ) = A[:L]*A[:Q] +AbstractArray(A::LQ) = AbstractMatrix(A) +Matrix(A::LQ) = Array(AbstractArray(A)) +Array(A::LQ) = Matrix(A) +full(A::LQ) = AbstractArray(A) adjoint(A::LQ{T}) where {T} = QR{T,typeof(A.factors)}(A.factors', A.τ) @@ -86,10 +86,10 @@ function show(io::IO, C::LQ) show(io, C[:Q]) end -convert(::Type{LQPackedQ{T}}, Q::LQPackedQ) where {T} = LQPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) -convert(::Type{AbstractMatrix{T}}, Q::LQPackedQ) where {T} = convert(LQPackedQ{T}, Q) -convert(::Type{Matrix}, A::LQPackedQ) = LAPACK.orglq!(copy(A.factors),A.τ) -convert(::Type{Array}, A::LQPackedQ) = convert(Matrix, A) +LQPackedQ{T}( Q::LQPackedQ) where {T} = LQPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) +AbstractMatrix{T}(Q::LQPackedQ) where {T} = LQPackedQ{T}(Q) +Matrix(A::LQPackedQ) = LAPACK.orglq!(copy(A.factors),A.τ) +Array(A::LQPackedQ) = Matrix(A) function full(A::LQPackedQ{T}; thin::Bool = true) where T #= We construct the full eye here, even though it seems inefficient, because every element in the output matrix is a function of all the elements of @@ -98,7 +98,7 @@ function full(A::LQPackedQ{T}; thin::Bool = true) where T explicitly constructing Q, rather than using the ldiv or mult methods, may be a wasteful allocation. =# if thin - convert(Array, A) + Array(A) else A_mul_B!(A, eye(T, size(A.factors,2), size(A.factors,1))) end @@ -124,15 +124,15 @@ A_mul_B!(A::LQ{T}, B::QR{T}) where {T<:BlasFloat} = A[:L]*LAPACK.ormlq!('L','N', A_mul_B!(A::QR{T}, B::LQ{T}) where {T<:BlasFloat} = A_mul_B!(zeros(full(A)), full(A), full(B)) function *(A::LQ{TA}, B::StridedVecOrMat{TB}) where {TA,TB} TAB = promote_type(TA, TB) - A_mul_B!(convert(Factorization{TAB},A), copy_oftype(B, TAB)) + A_mul_B!(Factorization{TAB}(A), copy_oftype(B, TAB)) end function *(A::LQ{TA},B::QR{TB}) where {TA,TB} TAB = promote_type(TA, TB) - A_mul_B!(convert(Factorization{TAB},A), convert(Factorization{TAB},B)) + A_mul_B!(Factorization{TAB}(A), Factorization{TAB}(B)) end function *(A::QR{TA},B::LQ{TB}) where {TA,TB} TAB = promote_type(TA, TB) - A_mul_B!(convert(Factorization{TAB},A), convert(Factorization{TAB},B)) + A_mul_B!(Factorization{TAB}(A), Factorization{TAB}(B)) end ## Multiplication by Q @@ -140,7 +140,7 @@ end A_mul_B!(A::LQPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.ormlq!('L','N',A.factors,A.τ,B) function (*)(A::LQPackedQ, B::StridedVecOrMat) TAB = promote_type(eltype(A), eltype(B)) - A_mul_B!(convert(AbstractMatrix{TAB}, A), copy_oftype(B, TAB)) + A_mul_B!(AbstractMatrix{TAB}(A), copy_oftype(B, TAB)) end ### QcB @@ -149,9 +149,9 @@ Ac_mul_B!(A::LQPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasComplex} = LAPAC function Ac_mul_B(A::LQPackedQ, B::StridedVecOrMat) TAB = promote_type(eltype(A), eltype(B)) if size(B,1) == size(A.factors,2) - Ac_mul_B!(convert(AbstractMatrix{TAB}, A), copy_oftype(B, TAB)) + Ac_mul_B!(AbstractMatrix{TAB}(A), copy_oftype(B, TAB)) elseif size(B,1) == size(A.factors,1) - Ac_mul_B!(convert(AbstractMatrix{TAB}, A), [B; zeros(TAB, size(A.factors, 2) - size(A.factors, 1), size(B, 2))]) + Ac_mul_B!(AbstractMatrix{TAB}(A), [B; zeros(TAB, size(A.factors, 2) - size(A.factors, 1), size(B, 2))]) else throw(DimensionMismatch("first dimension of B, $(size(B,1)), must equal one of the dimensions of A, $(size(A))")) end @@ -175,9 +175,9 @@ A_mul_B!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasFloat} = LAPACK.orm function *(A::StridedMatrix{TA}, B::LQPackedQ{TB}) where {TA,TB} TAB = promote_type(TA,TB) if size(B.factors,2) == size(A,2) - A_mul_B!(copy_oftype(A, TAB),convert(AbstractMatrix{TAB},B)) + A_mul_B!(copy_oftype(A, TAB),AbstractMatrix{TAB}(B)) elseif size(B.factors,1) == size(A,2) - A_mul_B!( [A zeros(TAB, size(A,1), size(B.factors,2)-size(B.factors,1))], convert(AbstractMatrix{TAB},B)) + A_mul_B!( [A zeros(TAB, size(A,1), size(B.factors,2)-size(B.factors,1))], AbstractMatrix{TAB}(B)) else throw(DimensionMismatch("second dimension of A, $(size(A,2)), must equal one of the dimensions of B, $(size(B))")) end @@ -208,7 +208,7 @@ function (\)(A::LQ{TA}, b::StridedVector{Tb}) where {TA,Tb} S = promote_type(TA,Tb) m = checksquare(A) m == length(b) || throw(DimensionMismatch("left hand side has $m rows, but right hand side has length $(length(b))")) - AA = convert(Factorization{S}, A) + AA = Factorization{S}(A) x = A_ldiv_B!(AA, copy_oftype(b, S)) return x end @@ -216,7 +216,7 @@ function (\)(A::LQ{TA},B::StridedMatrix{TB}) where {TA,TB} S = promote_type(TA,TB) m = checksquare(A) m == size(B,1) || throw(DimensionMismatch("left hand side has $m rows, but right hand side has $(size(B,1)) rows")) - AA = convert(Factorization{S}, A) + AA = Factorization{S}(A) X = A_ldiv_B!(AA, copy_oftype(B, S)) return X end diff --git a/base/linalg/lu.jl b/base/linalg/lu.jl index beaaf3f09c9ca6..ef1a52b04d37c0 100644 --- a/base/linalg/lu.jl +++ b/base/linalg/lu.jl @@ -192,13 +192,13 @@ function lu(A::AbstractMatrix, pivot::Union{Val{false}, Val{true}} = Val(true)) F[:L], F[:U], F[:p] end -function convert(::Type{LU{T}}, F::LU) where T +function LU{T}(F::LU) where T M = convert(AbstractMatrix{T}, F.factors) LU{T,typeof(M)}(M, F.ipiv, F.info) end -convert(::Type{LU{T,S}}, F::LU) where {T,S} = LU{T,S}(convert(S, F.factors), F.ipiv, F.info) -convert(::Type{Factorization{T}}, F::LU{T}) where {T} = F -convert(::Type{Factorization{T}}, F::LU) where {T} = convert(LU{T}, F) +LU{T,S}(F::LU) where {T,S} = LU{T,S}(convert(S, F.factors), F.ipiv, F.info) +Factorization{T}(F::LU{T}) where {T} = F +Factorization{T}(F::LU) where {T} = LU{T}(F) copy(A::LU{T,S}) where {T,S} = LU{T,S}(copy(A.factors), copy(A.ipiv), A.info) @@ -546,13 +546,13 @@ end /(B::AbstractMatrix,A::LU) = At_ldiv_Bt(A,B).' # Conversions -convert(::Type{AbstractMatrix}, F::LU) = (F[:L] * F[:U])[invperm(F[:p]),:] -convert(::Type{AbstractArray}, F::LU) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::LU) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::LU) = convert(Matrix, F) -full(F::LU) = convert(AbstractArray, F) +AbstractMatrix(F::LU) = (F[:L] * F[:U])[invperm(F[:p]),:] +AbstractArray(F::LU) = AbstractMatrix(F) +Matrix(F::LU) = Array(AbstractArray(F)) +Array(F::LU) = Matrix(F) +full(F::LU) = AbstractArray(F) -function convert(::Type{Tridiagonal}, F::Base.LinAlg.LU{T,Tridiagonal{T,V}}) where {T,V} +function Tridiagonal(F::Base.LinAlg.LU{T,Tridiagonal{T,V}}) where {T,V} n = size(F, 1) dl = copy(F.factors.dl) @@ -586,12 +586,8 @@ function convert(::Type{Tridiagonal}, F::Base.LinAlg.LU{T,Tridiagonal{T,V}}) whe end return Tridiagonal(dl, d, du) end -convert(::Type{AbstractMatrix}, F::LU{T,Tridiagonal{T,V}}) where {T,V} = - convert(Tridiagonal, F) -convert(::Type{AbstractArray}, F::LU{T,Tridiagonal{T,V}}) where {T,V} = - convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::LU{T,Tridiagonal{T,V}}) where {T,V} = - convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::LU{T,Tridiagonal{T,V}}) where {T,V} = - convert(Matrix, F) -full(F::LU{T,Tridiagonal{T,V}}) where {T,V} = convert(AbstractArray, F) +AbstractMatrix(F::LU{T,Tridiagonal{T,V}}) where {T,V} = Tridiagonal(F) +AbstractArray(F::LU{T,Tridiagonal{T,V}}) where {T,V} = AbstractMatrix(F) +Matrix(F::LU{T,Tridiagonal{T,V}}) where {T,V} = Array(AbstractArray(F)) +Array(F::LU{T,Tridiagonal{T,V}}) where {T,V} = Matrix(F) +full(F::LU{T,Tridiagonal{T,V}}) where {T,V} = AbstractArray(F) diff --git a/base/linalg/qr.jl b/base/linalg/qr.jl index c7e266f98b2d93..a08fef4520d239 100644 --- a/base/linalg/qr.jl +++ b/base/linalg/qr.jl @@ -352,25 +352,25 @@ function qr!(v::AbstractVector) end # Conversions -convert(::Type{QR{T}}, A::QR) where {T} = QR(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) -convert(::Type{Factorization{T}}, A::QR{T}) where {T} = A -convert(::Type{Factorization{T}}, A::QR) where {T} = convert(QR{T}, A) -convert(::Type{QRCompactWY{T}}, A::QRCompactWY) where {T} = QRCompactWY(convert(AbstractMatrix{T}, A.factors), convert(AbstractMatrix{T}, A.T)) -convert(::Type{Factorization{T}}, A::QRCompactWY{T}) where {T} = A -convert(::Type{Factorization{T}}, A::QRCompactWY) where {T} = convert(QRCompactWY{T}, A) -convert(::Type{AbstractMatrix}, F::Union{QR,QRCompactWY}) = F[:Q] * F[:R] -convert(::Type{AbstractArray}, F::Union{QR,QRCompactWY}) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::Union{QR,QRCompactWY}) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::Union{QR,QRCompactWY}) = convert(Matrix, F) -full(F::Union{QR,QRCompactWY}) = convert(AbstractArray, F) -convert(::Type{QRPivoted{T}}, A::QRPivoted) where {T} = QRPivoted(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ), A.jpvt) -convert(::Type{Factorization{T}}, A::QRPivoted{T}) where {T} = A -convert(::Type{Factorization{T}}, A::QRPivoted) where {T} = convert(QRPivoted{T}, A) -convert(::Type{AbstractMatrix}, F::QRPivoted) = (F[:Q] * F[:R])[:,invperm(F[:p])] -convert(::Type{AbstractArray}, F::QRPivoted) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::QRPivoted) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::QRPivoted) = convert(Matrix, F) -full(F::QRPivoted) = convert(AbstractArray, F) +QR{T}(A::QR) where {T} = QR(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) +Factorization{T}(A::QR{T}) where {T} = A +Factorization{T}(A::QR) where {T} = QR{T}(A) +QRCompactWY{T}(A::QRCompactWY) where {T} = QRCompactWY(convert(AbstractMatrix{T}, A.factors), convert(AbstractMatrix{T}, A.T)) +Factorization{T}(A::QRCompactWY{T}) where {T} = A +Factorization{T}(A::QRCompactWY) where {T} = QRCompactWY{T}(A) +AbstractMatrix(F::Union{QR,QRCompactWY}) = F[:Q] * F[:R] +AbstractArray(F::Union{QR,QRCompactWY}) = AbstractMatrix(F) +Matrix(F::Union{QR,QRCompactWY}) = Array(AbstractArray(F)) +Array(F::Union{QR,QRCompactWY}) = Matrix(F) +full(F::Union{QR,QRCompactWY}) = AbstractArray(F) +QRPivoted{T}(A::QRPivoted) where {T} = QRPivoted(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ), A.jpvt) +Factorization{T}(A::QRPivoted{T}) where {T} = A +Factorization{T}(A::QRPivoted) where {T} = QRPivoted{T}(A) +AbstractMatrix(F::QRPivoted) = (F[:Q] * F[:R])[:,invperm(F[:p])] +AbstractArray(F::QRPivoted) = AbstractMatrix(F) +Matrix(F::QRPivoted) = Array(AbstractArray(F)) +Array(F::QRPivoted) = Matrix(F) +full(F::QRPivoted) = AbstractArray(F) function show(io::IO, F::Union{QR, QRCompactWY, QRPivoted}) println(io, "$(typeof(F)) with factors Q and R:") @@ -452,14 +452,14 @@ struct QRCompactWYQ{S, M<:AbstractMatrix} <: AbstractQ{S} end QRCompactWYQ(factors::AbstractMatrix{S}, T::Matrix{S}) where {S} = QRCompactWYQ{S,typeof(factors)}(factors, T) -convert(::Type{QRPackedQ{T}}, Q::QRPackedQ) where {T} = QRPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) -convert(::Type{AbstractMatrix{T}}, Q::QRPackedQ{T}) where {T} = Q -convert(::Type{AbstractMatrix{T}}, Q::QRPackedQ) where {T} = convert(QRPackedQ{T}, Q) -convert(::Type{QRCompactWYQ{S}}, Q::QRCompactWYQ) where {S} = QRCompactWYQ(convert(AbstractMatrix{S}, Q.factors), convert(AbstractMatrix{S}, Q.T)) -convert(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ{S}) where {S} = Q -convert(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ) where {S} = convert(QRCompactWYQ{S}, Q) -convert(::Type{Matrix}, A::AbstractQ{T}) where {T} = A_mul_B!(A, eye(T, size(A.factors, 1), min(size(A.factors)...))) -convert(::Type{Array}, A::AbstractQ) = convert(Matrix, A) +QRPackedQ{T}(Q::QRPackedQ) where {T} = QRPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) +AbstractMatrix{T}(Q::QRPackedQ{T}) where {T} = Q +AbstractMatrix{T}(Q::QRPackedQ) where {T} = QRPackedQ{T}(Q) +QRCompactWYQ{S}(Q::QRCompactWYQ) where {S} = QRCompactWYQ(convert(AbstractMatrix{S}, Q.factors), convert(AbstractMatrix{S}, Q.T)) +AbstractMatrix{S}(Q::QRCompactWYQ{S}) where {S} = Q +AbstractMatrix{S}(Q::QRCompactWYQ) where {S} = QRCompactWYQ{S}(Q) +Matrix(A::AbstractQ{T}) where {T} = A_mul_B!(A, eye(T, size(A.factors, 1), min(size(A.factors)...))) +Array(A::AbstractQ) = Matrix(A) """ full(A::AbstractQ; thin::Bool=true) -> Matrix @@ -474,7 +474,7 @@ that spans all rows of `R` in its corresponding QR factorization. """ function full(A::AbstractQ{T}; thin::Bool = true) where T if thin - convert(Array, A) + Array(A) else A_mul_B!(A, eye(T, size(A.factors, 1))) end @@ -824,7 +824,7 @@ function (\)(A::Union{QR{TA},QRCompactWY{TA},QRPivoted{TA}}, B::AbstractVecOrMat m, n = size(A) m == size(B,1) || throw(DimensionMismatch("left hand side has $m rows, but right hand side has $(size(B,1)) rows")) - AA = convert(Factorization{S}, A) + AA = Factorization{S}(A) X = A_ldiv_B!(AA, _append_zeros(B, S, n)) diff --git a/base/linalg/rowvector.jl b/base/linalg/rowvector.jl index cc59066a0670b3..6ef2d92dc27e0d 100644 --- a/base/linalg/rowvector.jl +++ b/base/linalg/rowvector.jl @@ -45,7 +45,7 @@ const ConjRowVector{T,CV<:ConjVector} = RowVector{T,CV} error("RowVector expects 1×N size, got $n") # Conversion of underlying storage -convert(::Type{RowVector{T,V}}, rowvec::RowVector) where {T,V<:AbstractVector} = +RowVector{T,V}(rowvec::RowVector) where {T,V<:AbstractVector} = RowVector{T,V}(convert(V,rowvec.vec)) # similar tries to maintain the RowVector wrapper and the parent type diff --git a/base/linalg/schur.jl b/base/linalg/schur.jl index e53741108596b3..505c0f77f875b2 100644 --- a/base/linalg/schur.jl +++ b/base/linalg/schur.jl @@ -277,11 +277,11 @@ function schur(A::StridedMatrix, B::StridedMatrix) end # Conversion -convert(::Type{AbstractMatrix}, F::Schur) = (F.Z * F.T) * F.Z' -convert(::Type{AbstractArray}, F::Schur) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::Schur) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::Schur) = convert(Matrix, F) -full(F::Schur) = convert(AbstractArray, F) +AbstractMatrix(F::Schur) = (F.Z * F.T) * F.Z' +AbstractArray(F::Schur) = AbstractMatrix(F) +Matrix(F::Schur) = Array(AbstractArray(F)) +Array(F::Schur) = Matrix(F) +full(F::Schur) = AbstractArray(F) copy(F::Schur) = Schur(copy(F.T), copy(F.Z), copy(F.values)) copy(F::GeneralizedSchur) = GeneralizedSchur(copy(F.S), copy(F.T), copy(F.alpha), copy(F.beta), copy(F.Q), copy(F.Z)) diff --git a/base/linalg/special.jl b/base/linalg/special.jl index d41078fe2a238c..8ff51e81c89b7f 100644 --- a/base/linalg/special.jl +++ b/base/linalg/special.jl @@ -3,47 +3,40 @@ # Methods operating on different special matrix types # Interconversion between special matrix types -convert(::Type{Bidiagonal}, A::Diagonal) = - Bidiagonal(A.diag, fill!(similar(A.diag, length(A.diag)-1), 0), :U) -convert(::Type{SymTridiagonal}, A::Diagonal) = - SymTridiagonal(A.diag, fill!(similar(A.diag, length(A.diag)-1), 0)) -convert(::Type{Tridiagonal}, A::Diagonal) = - Tridiagonal(fill!(similar(A.diag, length(A.diag)-1), 0), A.diag, - fill!(similar(A.diag, length(A.diag)-1), 0)) - -function convert(::Type{Diagonal}, A::Union{Bidiagonal, SymTridiagonal}) +Bidiagonal(A::Diagonal) = Bidiagonal(A.diag, fill!(similar(A.diag, length(A.diag)-1), 0), :U) +SymTridiagonal(A::Diagonal) = SymTridiagonal(A.diag, fill!(similar(A.diag, length(A.diag)-1), 0)) +Tridiagonal(A::Diagonal) = Tridiagonal(fill!(similar(A.diag, length(A.diag)-1), 0), A.diag, + fill!(similar(A.diag, length(A.diag)-1), 0)) + +function Diagonal(A::Union{Bidiagonal, SymTridiagonal}) if !iszero(A.ev) throw(ArgumentError("matrix cannot be represented as Diagonal")) end Diagonal(A.dv) end -function convert(::Type{SymTridiagonal}, A::Bidiagonal) +function SymTridiagonal(A::Bidiagonal) if !iszero(A.ev) throw(ArgumentError("matrix cannot be represented as SymTridiagonal")) end SymTridiagonal(A.dv, A.ev) end -convert(::Type{Tridiagonal}, A::Bidiagonal{T}) where {T} = - Tridiagonal(A.uplo == 'U' ? fill!(similar(A.ev), 0) : A.ev, A.dv, - A.uplo == 'U' ? A.ev : fill!(similar(A.ev), 0)) - -function convert(::Type{Bidiagonal}, A::SymTridiagonal) +function Bidiagonal(A::SymTridiagonal) if !iszero(A.ev) throw(ArgumentError("matrix cannot be represented as Bidiagonal")) end Bidiagonal(A.dv, A.ev, :U) end -function convert(::Type{Diagonal}, A::Tridiagonal) +function Diagonal(A::Tridiagonal) if !(iszero(A.dl) && iszero(A.du)) throw(ArgumentError("matrix cannot be represented as Diagonal")) end Diagonal(A.d) end -function convert(::Type{Bidiagonal}, A::Tridiagonal) +function Bidiagonal(A::Tridiagonal) if iszero(A.dl) return Bidiagonal(A.d, A.du, :U) elseif iszero(A.du) @@ -53,25 +46,25 @@ function convert(::Type{Bidiagonal}, A::Tridiagonal) end end -function convert(::Type{SymTridiagonal}, A::Tridiagonal) +function SymTridiagonal(A::Tridiagonal) if A.dl != A.du throw(ArgumentError("matrix cannot be represented as SymTridiagonal")) end SymTridiagonal(A.d, A.dl) end -function convert(::Type{Tridiagonal}, A::SymTridiagonal) +function Tridiagonal(A::SymTridiagonal) Tridiagonal(copy(A.ev), A.dv, A.ev) end -function convert(::Type{Diagonal}, A::AbstractTriangular) +function Diagonal(A::AbstractTriangular) if full(A) != diagm(diag(A)) throw(ArgumentError("matrix cannot be represented as Diagonal")) end Diagonal(diag(A)) end -function convert(::Type{Bidiagonal}, A::AbstractTriangular) +function Bidiagonal(A::AbstractTriangular) fA = full(A) if fA == diagm(diag(A)) + diagm(diag(fA, 1), 1) return Bidiagonal(diag(A), diag(fA,1), :U) @@ -82,10 +75,9 @@ function convert(::Type{Bidiagonal}, A::AbstractTriangular) end end -convert(::Type{SymTridiagonal}, A::AbstractTriangular) = - convert(SymTridiagonal, convert(Tridiagonal, A)) +SymTridiagonal(A::AbstractTriangular) = SymTridiagonal(Tridiagonal(A)) -function convert(::Type{Tridiagonal}, A::AbstractTriangular) +function Tridiagonal(A::AbstractTriangular) fA = full(A) if fA == diagm(diag(A)) + diagm(diag(fA, 1), 1) + diagm(diag(fA, -1), -1) return Tridiagonal(diag(fA, -1), diag(A), diag(fA,1)) diff --git a/base/linalg/svd.jl b/base/linalg/svd.jl index f18faf9ded8b38..06c9bbd2086996 100644 --- a/base/linalg/svd.jl +++ b/base/linalg/svd.jl @@ -312,8 +312,8 @@ end svdvals(x::Number, y::Number) = abs(x/y) # Conversion -convert(::Type{AbstractMatrix}, F::SVD) = (F.U * Diagonal(F.S)) * F.Vt -convert(::Type{AbstractArray}, F::SVD) = convert(AbstractMatrix, F) -convert(::Type{Matrix}, F::SVD) = convert(Array, convert(AbstractArray, F)) -convert(::Type{Array}, F::SVD) = convert(Matrix, F) -full(F::SVD) = convert(AbstractArray, F) +AbstractMatrix(F::SVD) = (F.U * Diagonal(F.S)) * F.Vt +AbstractArray(F::SVD) = AbstractMatrix(F) +Matrix(F::SVD) = Array(AbstractArray(F)) +Array(F::SVD) = Matrix(F) +full(F::SVD) = AbstractArray(F) diff --git a/base/linalg/symmetric.jl b/base/linalg/symmetric.jl index 3335ffda0050d9..4bd5a0f3c93aa4 100644 --- a/base/linalg/symmetric.jl +++ b/base/linalg/symmetric.jl @@ -149,17 +149,17 @@ function similar(A::Hermitian, ::Type{T}) where T end # Conversion -convert(::Type{Matrix}, A::Symmetric) = copytri!(convert(Matrix, copy(A.data)), A.uplo) -convert(::Type{Matrix}, A::Hermitian) = copytri!(convert(Matrix, copy(A.data)), A.uplo, true) -convert(::Type{Array}, A::Union{Symmetric,Hermitian}) = convert(Matrix, A) +Matrix(A::Symmetric) = copytri!(convert(Matrix, copy(A.data)), A.uplo) +Matrix(A::Hermitian) = copytri!(convert(Matrix, copy(A.data)), A.uplo, true) +Array(A::Union{Symmetric,Hermitian}) = convert(Matrix, A) full(A::Union{Symmetric,Hermitian}) = convert(Array, A) parent(A::HermOrSym) = A.data -convert(::Type{Symmetric{T,S}},A::Symmetric{T,S}) where {T,S<:AbstractMatrix} = A -convert(::Type{Symmetric{T,S}},A::Symmetric) where {T,S<:AbstractMatrix} = Symmetric{T,S}(convert(S,A.data),A.uplo) -convert(::Type{AbstractMatrix{T}}, A::Symmetric) where {T} = Symmetric(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) -convert(::Type{Hermitian{T,S}},A::Hermitian{T,S}) where {T,S<:AbstractMatrix} = A -convert(::Type{Hermitian{T,S}},A::Hermitian) where {T,S<:AbstractMatrix} = Hermitian{T,S}(convert(S,A.data),A.uplo) -convert(::Type{AbstractMatrix{T}}, A::Hermitian) where {T} = Hermitian(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) +Symmetric{T,S}(A::Symmetric{T,S}) where {T,S<:AbstractMatrix} = A +Symmetric{T,S}(A::Symmetric) where {T,S<:AbstractMatrix} = Symmetric{T,S}(convert(S,A.data),A.uplo) +AbstractMatrix{T}(A::Symmetric) where {T} = Symmetric(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) +Hermitian{T,S}(A::Hermitian{T,S}) where {T,S<:AbstractMatrix} = A +Hermitian{T,S}(A::Hermitian) where {T,S<:AbstractMatrix} = Hermitian{T,S}(convert(S,A.data),A.uplo) +AbstractMatrix{T}(A::Hermitian) where {T} = Hermitian(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) copy(A::Symmetric{T,S}) where {T,S} = (B = copy(A.data); Symmetric{T,typeof(B)}(B,A.uplo)) copy(A::Hermitian{T,S}) where {T,S} = (B = copy(A.data); Hermitian{T,typeof(B)}(B,A.uplo)) diff --git a/base/linalg/triangular.jl b/base/linalg/triangular.jl index df46c9239931de..248b8594ea6a2a 100644 --- a/base/linalg/triangular.jl +++ b/base/linalg/triangular.jl @@ -13,22 +13,20 @@ for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular, data::S end $t(A::$t) = A + $t{T}(A::$t{T}) where {T} = A function $t(A::AbstractMatrix) Base.LinAlg.checksquare(A) return $t{eltype(A), typeof(A)}(A) end - size(A::$t, d) = size(A.data, d) - size(A::$t) = size(A.data) - - convert(::Type{$t{T}}, A::$t{T}) where {T} = A - function convert(::Type{$t{T}}, A::$t) where T + function $t{T}(A::$t) where T Anew = convert(AbstractMatrix{T}, A.data) $t(Anew) end - convert(::Type{AbstractMatrix{T}}, A::$t{T}) where {T} = A - convert(::Type{AbstractMatrix{T}}, A::$t) where {T} = convert($t{T}, A) - convert(::Type{Matrix}, A::$t{T}) where {T} = convert(Matrix{T}, A) + Matrix(A::$t{T}) where {T} = Matrix{T}(A) + + size(A::$t, d) = size(A.data, d) + size(A::$t) = size(A.data) function similar(A::$t, ::Type{T}) where T B = similar(A.data, T) @@ -98,19 +96,19 @@ imag(A::LowerTriangular) = LowerTriangular(imag(A.data)) imag(A::UnitLowerTriangular) = LowerTriangular(tril!(imag(A.data),-1)) imag(A::UnitUpperTriangular) = UpperTriangular(triu!(imag(A.data),1)) -convert(::Type{Array}, A::AbstractTriangular) = convert(Matrix, A) -full(A::AbstractTriangular) = convert(Array, A) +Array(A::AbstractTriangular) = Matrix(A) +full(A::AbstractTriangular) = Array(A) parent(A::AbstractTriangular) = A.data # then handle all methods that requires specific handling of upper/lower and unit diagonal -function convert(::Type{Matrix{T}}, A::LowerTriangular) where T +function Matrix{T}(A::LowerTriangular) where T B = Matrix{T}(size(A, 1), size(A, 1)) copy!(B, A.data) tril!(B) B end -function convert(::Type{Matrix{T}}, A::UnitLowerTriangular) where T +function Matrix{T}(A::UnitLowerTriangular) where T B = Matrix{T}(size(A, 1), size(A, 1)) copy!(B, A.data) tril!(B) @@ -119,13 +117,13 @@ function convert(::Type{Matrix{T}}, A::UnitLowerTriangular) where T end B end -function convert(::Type{Matrix{T}}, A::UpperTriangular) where T +function Matrix{T}(A::UpperTriangular) where T B = Matrix{T}(size(A, 1), size(A, 1)) copy!(B, A.data) triu!(B) B end -function convert(::Type{Matrix{T}}, A::UnitUpperTriangular) where T +function Matrix{T}(A::UnitUpperTriangular) where T B = Matrix{T}(size(A, 1), size(A, 1)) copy!(B, A.data) triu!(B) diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index 299bd571b6dd71..24f6d0dbed9933 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -76,11 +76,11 @@ function SymTridiagonal(A::AbstractMatrix) end end -convert(::Type{SymTridiagonal{T}}, S::SymTridiagonal) where {T} = +SymTridiagonal{T}(S::SymTridiagonal) where {T} = SymTridiagonal(convert(AbstractVector{T}, S.dv), convert(AbstractVector{T}, S.ev)) -convert(::Type{AbstractMatrix{T}}, S::SymTridiagonal) where {T} = +AbstractMatrix{T}(S::SymTridiagonal) where {T} = SymTridiagonal(convert(AbstractVector{T}, S.dv), convert(AbstractVector{T}, S.ev)) -function convert(::Type{Matrix{T}}, M::SymTridiagonal) where T +function Matrix{T}(M::SymTridiagonal) where T n = size(M, 1) Mf = zeros(T, n, n) @inbounds begin @@ -93,9 +93,9 @@ function convert(::Type{Matrix{T}}, M::SymTridiagonal) where T end return Mf end -convert(::Type{Matrix}, M::SymTridiagonal{T}) where {T} = convert(Matrix{T}, M) -convert(::Type{Array}, M::SymTridiagonal) = convert(Matrix, M) -full(M::SymTridiagonal) = convert(Array, M) +Matrix(M::SymTridiagonal{T}) where {T} = Matrix{T}(M) +Array(M::SymTridiagonal) = Matrix(M) +full(M::SymTridiagonal) = Array(M) size(A::SymTridiagonal) = (length(A.dv), length(A.dv)) function size(A::SymTridiagonal, d::Integer) @@ -482,7 +482,7 @@ function size(M::Tridiagonal, d::Integer) end end -function convert(::Type{Matrix{T}}, M::Tridiagonal{T}) where T +function Matrix{T}(M::Tridiagonal{T}) where T A = zeros(T, size(M)) for i = 1:length(M.d) A[i,i] = M.d[i] @@ -493,9 +493,9 @@ function convert(::Type{Matrix{T}}, M::Tridiagonal{T}) where T end A end -convert(::Type{Matrix}, M::Tridiagonal{T}) where {T} = convert(Matrix{T}, M) -convert(::Type{Array}, M::Tridiagonal) = convert(Matrix, M) -full(M::Tridiagonal) = convert(Array, M) +Matrix(M::Tridiagonal{T}) where {T} = Matrix{T}(M) +Array(M::Tridiagonal) = Matrix(M) +full(M::Tridiagonal) = Array(M) function similar(M::Tridiagonal, ::Type{T}) where T Tridiagonal{T}(similar(M.dl, T), similar(M.d, T), similar(M.du, T)) end @@ -631,11 +631,11 @@ end inv(A::Tridiagonal) = inv_usmani(A.dl, A.d, A.du) det(A::Tridiagonal) = det_usmani(A.dl, A.d, A.du) -convert(::Type{Tridiagonal{T}},M::Tridiagonal) where {T} = +Tridiagonal{T}(M::Tridiagonal) where {T} = Tridiagonal(convert(AbstractVector{T}, M.dl), convert(AbstractVector{T}, M.d), convert(AbstractVector{T}, M.du)) -convert(::Type{AbstractMatrix{T}},M::Tridiagonal) where {T} = convert(Tridiagonal{T}, M) -convert(::Type{Tridiagonal{T}}, M::SymTridiagonal{T}) where {T} = Tridiagonal(M) -function convert(::Type{SymTridiagonal{T}}, M::Tridiagonal) where T +AbstractMatrix{T}(M::Tridiagonal) where {T} = Tridiagonal{T}(M) +Tridiagonal{T}(M::SymTridiagonal{T}) where {T} = Tridiagonal(M) +function SymTridiagonal{T}(M::Tridiagonal) where T if M.dl == M.du return SymTridiagonal{T}(convert(AbstractVector{T},M.d), convert(AbstractVector{T},M.dl)) else diff --git a/base/nullable.jl b/base/nullable.jl index 514b5b155c753d..2198d0ac0763eb 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -51,21 +51,15 @@ Nullable() = Nullable{Union{}}() eltype(::Type{Nullable{T}}) where {T} = T -convert(::Type{Nullable{T}}, x::Nullable{T}) where {T} = x +convert(::Type{T}, x::T) where {T<:Nullable} = x convert(::Type{Nullable }, x::Nullable ) = x -convert(t::Type{Nullable{T}}, x::Any) where {T} = convert(t, convert(T, x)) - function convert(::Type{Nullable{T}}, x::Nullable) where T return isnull(x) ? Nullable{T}() : Nullable{T}(convert(T, get(x))) end -convert(::Type{Nullable{T}}, x::T) where {T<:Nullable} = Nullable{T}(x) -convert(::Type{Nullable{T}}, x::T) where {T} = Nullable{T}(x) -convert(::Type{Nullable }, x::T) where {T} = Nullable{T}(x) - -convert(::Type{Nullable{T}}, ::Void) where {T} = Nullable{T}() -convert(::Type{Nullable }, ::Void) = Nullable{Union{}}() +convert(::Type{T}, x::Any ) where {T<:Nullable} = T(x) +convert(::Type{T}, x::Void) where {T<:Nullable} = T() promote_rule(::Type{Nullable{S}}, ::Type{T}) where {S,T} = Nullable{promote_type(S, T)} promote_rule(::Type{Nullable{S}}, ::Type{Nullable{T}}) where {S,T} = Nullable{promote_type(S, T)} diff --git a/base/nullabletype.jl b/base/nullabletype.jl index 8c1c9c3d1d91c2..5858bead1ca482 100644 --- a/base/nullabletype.jl +++ b/base/nullabletype.jl @@ -5,5 +5,7 @@ struct Nullable{T} value::T Nullable{T}() where {T} = new(false) - Nullable{T}(value::T, hasvalue::Bool=true) where {T} = new(hasvalue, value) + Nullable{T}(value, hasvalue::Bool=true) where {T} = new(hasvalue, value) + Nullable{T}(::Void) where {T} = Nullable{T}() + (::Type{Nullable{Void}})(::Void) = new{Void}(true, nothing) end diff --git a/base/precompile.jl b/base/precompile.jl index 4981c89231dd3a..ec0f597f20090e 100644 --- a/base/precompile.jl +++ b/base/precompile.jl @@ -599,7 +599,6 @@ precompile(Tuple{typeof(Base.matchall), Base.Regex, String, Bool}) precompile(Tuple{typeof(Base.skip_deleted), Base.Dict{String, String}, Int64}) precompile(Tuple{typeof(Base.reverse), String}) precompile(Tuple{typeof(Core.Inference.isbits), Tuple{Void, Bool}}) -precompile(Tuple{typeof(Base._convert), Type{Array{Method, 1}}, Base.BitArray{1}}) precompile(Tuple{typeof(Base.unsafe_copy!), Array{Method, 1}, Int64, Array{Method, 1}, Int64, Int64}) precompile(Tuple{typeof(Base.copy!), Array{Method, 1}, Int64, Array{Method, 1}, Int64, Int64}) precompile(Tuple{typeof(Core.Inference.length), Tuple{Core.Inference.PartialTypeVar}}) diff --git a/base/range.jl b/base/range.jl index 6452dc1847b4de..5fb4d6c6321853 100644 --- a/base/range.jl +++ b/base/range.jl @@ -811,64 +811,61 @@ el_same(::Type, a, b) = typejoin(a, b) promote_rule(a::Type{UnitRange{T1}}, b::Type{UnitRange{T2}}) where {T1,T2} = el_same(promote_type(T1,T2), a, b) -convert(::Type{UnitRange{T}}, r::UnitRange{T}) where {T<:Real} = r -convert(::Type{UnitRange{T}}, r::UnitRange) where {T<:Real} = UnitRange{T}(r.start, r.stop) +UnitRange{T}(r::UnitRange{T}) where {T<:Real} = r +UnitRange{T}(r::UnitRange) where {T<:Real} = UnitRange{T}(r.start, r.stop) promote_rule(a::Type{OneTo{T1}}, b::Type{OneTo{T2}}) where {T1,T2} = el_same(promote_type(T1,T2), a, b) -convert(::Type{OneTo{T}}, r::OneTo{T}) where {T<:Real} = r -convert(::Type{OneTo{T}}, r::OneTo) where {T<:Real} = OneTo{T}(r.stop) +OneTo{T}(r::OneTo{T}) where {T<:Integer} = r +OneTo{T}(r::OneTo) where {T<:Integer} = OneTo{T}(r.stop) promote_rule(a::Type{UnitRange{T1}}, ::Type{UR}) where {T1,UR<:AbstractUnitRange} = promote_rule(a, UnitRange{eltype(UR)}) -convert(::Type{UnitRange{T}}, r::AbstractUnitRange) where {T<:Real} = UnitRange{T}(first(r), last(r)) -convert(::Type{UnitRange}, r::AbstractUnitRange) = UnitRange(first(r), last(r)) +UnitRange{T}(r::AbstractUnitRange) where {T<:Real} = UnitRange{T}(first(r), last(r)) +UnitRange(r::AbstractUnitRange) = UnitRange(first(r), last(r)) -convert(::Type{AbstractUnitRange{T}}, r::AbstractUnitRange{T}) where {T} = r -convert(::Type{AbstractUnitRange{T}}, r::UnitRange) where {T} = convert(UnitRange{T}, r) -convert(::Type{AbstractUnitRange{T}}, r::OneTo) where {T} = convert(OneTo{T}, r) +AbstractUnitRange{T}(r::AbstractUnitRange{T}) where {T} = r +AbstractUnitRange{T}(r::UnitRange) where {T} = UnitRange{T}(r) +AbstractUnitRange{T}(r::OneTo) where {T} = OneTo{T}(r) promote_rule(::Type{StepRange{T1a,T1b}}, ::Type{StepRange{T2a,T2b}}) where {T1a,T1b,T2a,T2b} = el_same(promote_type(T1a,T2a), # el_same only operates on array element type, so just promote second type parameter StepRange{T1a, promote_type(T1b,T2b)}, StepRange{T2a, promote_type(T1b,T2b)}) -convert(::Type{StepRange{T1,T2}}, r::StepRange{T1,T2}) where {T1,T2} = r +StepRange{T1,T2}(r::StepRange{T1,T2}) where {T1,T2} = r promote_rule(a::Type{StepRange{T1a,T1b}}, ::Type{UR}) where {T1a,T1b,UR<:AbstractUnitRange} = promote_rule(a, StepRange{eltype(UR), eltype(UR)}) -convert(::Type{StepRange{T1,T2}}, r::AbstractRange) where {T1,T2} = +StepRange{T1,T2}(r::AbstractRange) where {T1,T2} = StepRange{T1,T2}(convert(T1, first(r)), convert(T2, step(r)), convert(T1, last(r))) -convert(::Type{StepRange}, r::AbstractUnitRange{T}) where {T} = +StepRange(r::AbstractUnitRange{T}) where {T} = StepRange{T,T}(first(r), step(r), last(r)) -convert(::Type{StepRange{T1,T2} where T1}, r::AbstractRange) where {T2} = - convert(StepRange{eltype(r),T2}, r) +(::Type{StepRange{T1,T2} where T1})(r::AbstractRange) where {T2} = StepRange{eltype(r),T2}(r) promote_rule(::Type{StepRangeLen{T1,R1,S1}},::Type{StepRangeLen{T2,R2,S2}}) where {T1,T2,R1,R2,S1,S2} = el_same(promote_type(T1,T2), StepRangeLen{T1,promote_type(R1,R2),promote_type(S1,S2)}, StepRangeLen{T2,promote_type(R1,R2),promote_type(S1,S2)}) -convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{T,R,S}) where {T,R,S} = r -convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen) where {T,R,S} = +StepRangeLen{T,R,S}(r::StepRangeLen{T,R,S}) where {T,R,S} = r +StepRangeLen{T,R,S}(r::StepRangeLen) where {T,R,S} = StepRangeLen{T,R,S}(convert(R, r.ref), convert(S, r.step), length(r), r.offset) -convert(::Type{StepRangeLen{T}}, r::StepRangeLen) where {T} = +StepRangeLen{T}(r::StepRangeLen) where {T} = StepRangeLen(convert(T, r.ref), convert(T, r.step), length(r), r.offset) promote_rule(a::Type{StepRangeLen{T,R,S}}, ::Type{OR}) where {T,R,S,OR<:AbstractRange} = promote_rule(a, StepRangeLen{eltype(OR), eltype(OR), eltype(OR)}) -convert(::Type{StepRangeLen{T,R,S}}, r::AbstractRange) where {T,R,S} = +StepRangeLen{T,R,S}(r::AbstractRange) where {T,R,S} = StepRangeLen{T,R,S}(R(first(r)), S(step(r)), length(r)) -convert(::Type{StepRangeLen{T}}, r::AbstractRange) where {T} = +StepRangeLen{T}(r::AbstractRange) where {T} = StepRangeLen(T(first(r)), T(step(r)), length(r)) -convert(::Type{StepRangeLen}, r::AbstractRange) = convert(StepRangeLen{eltype(r)}, r) +StepRangeLen(r::AbstractRange) = StepRangeLen{eltype(r)}(r) promote_rule(a::Type{LinSpace{T1}}, b::Type{LinSpace{T2}}) where {T1,T2} = el_same(promote_type(T1,T2), a, b) -convert(::Type{LinSpace{T}}, r::LinSpace{T}) where {T} = r -convert(::Type{LinSpace{T}}, r::AbstractRange) where {T} = - LinSpace{T}(first(r), last(r), length(r)) -convert(::Type{LinSpace}, r::AbstractRange{T}) where {T} = - convert(LinSpace{T}, r) +LinSpace{T}(r::LinSpace{T}) where {T} = r +LinSpace{T}(r::AbstractRange) where {T} = LinSpace{T}(first(r), last(r), length(r)) +LinSpace(r::AbstractRange{T}) where {T} = LinSpace{T}(r) promote_rule(a::Type{LinSpace{T}}, ::Type{OR}) where {T,OR<:OrdinalRange} = promote_rule(a, LinSpace{eltype(OR)}) @@ -894,7 +891,7 @@ function vcat(rs::AbstractRange{T}...) where T return a end -convert(::Type{Array{T,1}}, r::AbstractRange{T}) where {T} = vcat(r) +Array{T,1}(r::AbstractRange{T}) where {T} = vcat(r) collect(r::AbstractRange) = vcat(r) reverse(r::OrdinalRange) = colon(last(r), -step(r), first(r)) diff --git a/base/replutil.jl b/base/replutil.jl index 5fef42c044118e..eeddbadfa8b2de 100644 --- a/base/replutil.jl +++ b/base/replutil.jl @@ -690,5 +690,5 @@ Determines whether a method is the default method which is provided to all types Such a method is usually undesirable to be displayed to the user in the REPL. """ function is_default_method(m::Method) - return m.module == Base && m.file == Symbol("sysimg.jl") && m.sig == Tuple{Type{T},Any} where T + return m.module == Base && m.sig == Tuple{Type{T},Any} where T end diff --git a/base/sharedarray.jl b/base/sharedarray.jl index 3cdeb3bcd88a1e..9f26725c21c0c8 100644 --- a/base/sharedarray.jl +++ b/base/sharedarray.jl @@ -328,15 +328,15 @@ localindexes(S::SharedArray) = S.pidx > 0 ? range_1dim(S, S.pidx) : 1:0 unsafe_convert(::Type{Ptr{T}}, S::SharedArray{T}) where {T} = unsafe_convert(Ptr{T}, sdata(S)) unsafe_convert(::Type{Ptr{T}}, S::SharedArray ) where {T} = unsafe_convert(Ptr{T}, sdata(S)) -function convert(::Type{SharedArray}, A::Array) +function SharedArray(A::Array) S = SharedArray{eltype(A),ndims(A)}(size(A)) copy!(S, A) end -function convert(::Type{SharedArray{T}}, A::Array) where T +function SharedArray{T}(A::Array) where T S = SharedArray{T,ndims(A)}(size(A)) copy!(S, A) end -function convert(::Type{SharedArray{TS,N}}, A::Array{TA,N}) where {TS,TA,N} +function SharedArray{TS,N}(A::Array{TA,N}) where {TS,TA,N} S = SharedArray{TS,ndims(A)}(size(A)) copy!(S, A) end @@ -475,7 +475,7 @@ function show(io::IO, mime::MIME"text/plain", S::SharedArray) end end -convert(::Type{Array}, S::SharedArray) = S.s +Array(S::SharedArray) = S.s # pass through getindex and setindex! - unlike DArrays, these always work on the complete array getindex(S::SharedArray, i::Real) = getindex(S.s, i) diff --git a/base/socket.jl b/base/socket.jl index b16df2869ea772..c9e368d76a3ba1 100644 --- a/base/socket.jl +++ b/base/socket.jl @@ -4,7 +4,7 @@ abstract type IPAddr end Base.isless(a::T, b::T) where {T<:IPAddr} = isless(a.host, b.host) -(dt::Type{<:Integer})(ip::IPAddr) = dt(ip.host) +(dt::Type{<:Integer})(ip::IPAddr) = dt(ip.host)::dt struct IPv4 <: IPAddr host::UInt32 diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index 4939f1af57890b..712b15de0fbe6e 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -346,6 +346,7 @@ mutable struct Factor{Tv} <: Factorization{Tv} end end Factor(p::Ptr{C_Factor{Tv}}) where {Tv<:VTypes} = Factor{Tv}(p) +Factor(x::Factor) = x # All pointer loads should be checked to make sure that SuiteSparse is not called with # a C_NULL pointer which could cause a segfault. Pointers are set to null @@ -820,7 +821,7 @@ get_perm(FC::FactorComponent) = get_perm(Factor(FC)) ######################### # Convertion/construction -function convert(::Type{Dense{T}}, A::StridedVecOrMat) where T<:VTypes +function Dense{T}(A::StridedVecOrMat) where T<:VTypes d = allocate_dense(size(A, 1), size(A, 2), stride(A, 2), T) s = unsafe_load(d.p) for i in eachindex(A) @@ -828,11 +829,11 @@ function convert(::Type{Dense{T}}, A::StridedVecOrMat) where T<:VTypes end d end -function convert(::Type{Dense}, A::StridedVecOrMat) +function Dense(A::StridedVecOrMat) T = promote_type(eltype(A), Float64) - return convert(Dense{T}, A) + return Dense{T}(A) end -convert(::Type{Dense}, A::Sparse) = sparse_to_dense(A) +Dense(A::Sparse) = sparse_to_dense(A) # This constructior assumes zero based colptr and rowval function Sparse(m::Integer, n::Integer, @@ -914,9 +915,8 @@ function Sparse(A::SparseMatrixCSC{Tv,SuiteSparse_long}, stype::Integer) where T end # convert SparseVectors into CHOLMOD Sparse types through a mx1 CSC matrix -convert(::Type{Sparse}, A::SparseVector{<:VTypes,SuiteSparse_long}) = - convert(Sparse, convert(SparseMatrixCSC, A)) -function convert(::Type{Sparse}, A::SparseMatrixCSC{<:VTypes,<:ITypes}) +Sparse(A::SparseVector{<:VTypes,SuiteSparse_long}) = Sparse(SparseMatrixCSC(A)) +function Sparse(A::SparseMatrixCSC{<:VTypes,<:ITypes}) o = Sparse(A, 0) # check if array is symmetric and change stype if it is if ishermitian(o) @@ -924,29 +924,27 @@ function convert(::Type{Sparse}, A::SparseMatrixCSC{<:VTypes,<:ITypes}) end o end -convert(::Type{Sparse}, A::SparseMatrixCSC{Complex{Float32},<:ITypes}) = - convert(Sparse, convert(SparseMatrixCSC{Complex{Float64},SuiteSparse_long}, A)) -convert(::Type{Sparse}, A::Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}) = +Sparse(A::SparseMatrixCSC{Complex{Float32},<:ITypes}) = + Sparse(SparseMatrixCSC{Complex{Float64},SuiteSparse_long}(A)) +Sparse(A::Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}) = Sparse(A.data, A.uplo == 'L' ? -1 : 1) -convert(::Type{Sparse}, A::Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}) where {Tv<:VTypes} = +Sparse(A::Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}) where {Tv<:VTypes} = Sparse(A.data, A.uplo == 'L' ? -1 : 1) -function convert(::Type{Sparse}, - A::Union{SparseMatrixCSC{BigFloat,Ti}, - Symmetric{BigFloat,SparseMatrixCSC{BigFloat,Ti}}, - Hermitian{Complex{BigFloat},SparseMatrixCSC{Complex{BigFloat},Ti}}}, - args...) where Ti<:ITypes - throw(MethodError(convert, (Sparse, A))) +function Sparse(A::Union{SparseMatrixCSC{BigFloat,Ti}, + Symmetric{BigFloat,SparseMatrixCSC{BigFloat,Ti}}, + Hermitian{Complex{BigFloat},SparseMatrixCSC{Complex{BigFloat},Ti}}}, + args...) where Ti<:ITypes + throw(MethodError(Sparse, (A,))) end -function convert(::Type{Sparse}, - A::Union{SparseMatrixCSC{T,Ti}, - Symmetric{T,SparseMatrixCSC{T,Ti}}, - Hermitian{T,SparseMatrixCSC{T,Ti}}}, - args...) where T where Ti<:ITypes - return Sparse(convert(AbstractMatrix{promote_type(Float64, T)}, A), args...) +function Sparse(A::Union{SparseMatrixCSC{T,Ti}, + Symmetric{T,SparseMatrixCSC{T,Ti}}, + Hermitian{T,SparseMatrixCSC{T,Ti}}}, + args...) where T where Ti<:ITypes + return Sparse(AbstractMatrix{promote_type(Float64, T)}(A), args...) end # Useful when reading in files, but not type stable -function convert(::Type{Sparse}, p::Ptr{C_SparseVoid}) +function Sparse(p::Ptr{C_SparseVoid}) if p == C_NULL throw(ArgumentError("sparse matrix construction failed for " * "unknown reasons. Please submit a bug report.")) @@ -991,8 +989,8 @@ function convert(::Type{Sparse}, p::Ptr{C_SparseVoid}) return Sparse(convert(Ptr{C_Sparse{Tv}}, p)) end -convert(::Type{Sparse}, A::Dense) = dense_to_sparse(A, SuiteSparse_long) -convert(::Type{Sparse}, L::Factor) = factor_to_sparse!(copy(L)) +Sparse(A::Dense) = dense_to_sparse(A, SuiteSparse_long) +Sparse(L::Factor) = factor_to_sparse!(copy(L)) function Sparse(filename::String) open(filename) do f return read_sparse(f, SuiteSparse_long) @@ -1000,7 +998,7 @@ function Sparse(filename::String) end ## convertion back to base Julia types -function convert(::Type{Matrix{T}}, D::Dense{T}) where T +function Matrix{T}(D::Dense{T}) where T s = unsafe_load(D.p) a = Matrix{T}(s.nrow, s.ncol) copy!(a, D) @@ -1028,16 +1026,16 @@ function _copy!(dest::AbstractArray, D::Dense) end dest end -convert(::Type{Matrix}, D::Dense{T}) where {T} = convert(Matrix{T}, D) -function convert(::Type{Vector{T}}, D::Dense{T}) where T +Matrix(D::Dense{T}) where {T} = Matrix{T}(D) +function Vector{T}(D::Dense{T}) where T if size(D, 2) > 1 throw(DimensionMismatch("input must be a vector but had $(size(D, 2)) columns")) end copy!(Vector{T}(size(D, 1)), D) end -convert(::Type{Vector}, D::Dense{T}) where {T} = convert(Vector{T}, D) +Vector(D::Dense{T}) where {T} = Vector{T}(D) -function convert(::Type{SparseMatrixCSC{Tv,SuiteSparse_long}}, A::Sparse{Tv}) where Tv +function SparseMatrixCSC{Tv,SuiteSparse_long}(A::Sparse{Tv}) where Tv s = unsafe_load(A.p) if s.stype != 0 throw(ArgumentError("matrix has stype != 0. Convert to matrix " * @@ -1055,7 +1053,7 @@ function convert(::Type{SparseMatrixCSC{Tv,SuiteSparse_long}}, A::Sparse{Tv}) wh return B end end -function convert(::Type{Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}}, A::Sparse{Float64}) +function (::Type{Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}})(A::Sparse{Float64}) s = unsafe_load(A.p) if !issymmetric(A) throw(ArgumentError("matrix is not symmetric")) @@ -1072,7 +1070,7 @@ function convert(::Type{Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_lo return B end end -function convert(::Type{Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}}, A::Sparse{Tv}) where Tv<:VTypes +function Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}(A::Sparse{Tv}) where Tv<:VTypes s = unsafe_load(A.p) if !ishermitian(A) throw(ArgumentError("matrix is not Hermitian")) @@ -1092,16 +1090,16 @@ end function sparse(A::Sparse{Float64}) # Notice! Cannot be type stable because of stype s = unsafe_load(A.p) if s.stype == 0 - return convert(SparseMatrixCSC{Float64,SuiteSparse_long}, A) + return SparseMatrixCSC{Float64,SuiteSparse_long}(A) end - return convert(Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}, A) + return Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}(A) end function sparse(A::Sparse{Complex{Float64}}) # Notice! Cannot be type stable because of stype s = unsafe_load(A.p) if s.stype == 0 - return convert(SparseMatrixCSC{Complex{Float64},SuiteSparse_long}, A) + return SparseMatrixCSC{Complex{Float64},SuiteSparse_long}(A) end - return convert(Hermitian{Complex{Float64},SparseMatrixCSC{Complex{Float64},SuiteSparse_long}}, A) + return Hermitian{Complex{Float64},SparseMatrixCSC{Complex{Float64},SuiteSparse_long}}(A) end function sparse(F::Factor) s = unsafe_load(pointer(F)) @@ -1638,10 +1636,10 @@ end SparseVecOrMat{Tv,Ti} = Union{SparseVector{Tv,Ti}, SparseMatrixCSC{Tv,Ti}} function (\)(L::FactorComponent, b::Vector) - reshape(convert(Matrix, L\Dense(b)), length(b)) + reshape(Matrix(L\Dense(b)), length(b)) end function (\)(L::FactorComponent, B::Matrix) - convert(Matrix, L\Dense(B)) + Matrix(L\Dense(B)) end function (\)(L::FactorComponent, B::SparseVecOrMat) sparse(L\Sparse(B,0)) @@ -1655,14 +1653,14 @@ Ac_ldiv_B(L::FactorComponent, B::RowVector) = adjoint(L)\B # ambiguity # Likewise the two following explicit Vector and Matrix defs (rather than a single VecOrMat) (\)(L::Factor{T}, B::Vector{Complex{T}}) where {T<:Float64} = complex.(L\real(B), L\imag(B)) (\)(L::Factor{T}, B::Matrix{Complex{T}}) where {T<:Float64} = complex.(L\real(B), L\imag(B)) -(\)(L::Factor{T}, b::StridedVector) where {T<:VTypes} = Vector(L\convert(Dense{T}, b)) -(\)(L::Factor{T}, B::StridedMatrix) where {T<:VTypes} = Matrix(L\convert(Dense{T}, B)) +(\)(L::Factor{T}, b::StridedVector) where {T<:VTypes} = Vector(L\Dense{T}(b)) +(\)(L::Factor{T}, B::StridedMatrix) where {T<:VTypes} = Matrix(L\Dense{T}(B)) (\)(L::Factor, B::Sparse) = spsolve(CHOLMOD_A, L, B) # When right hand side is sparse, we have to ensure that the rhs is not marked as symmetric. (\)(L::Factor, B::SparseVecOrMat) = sparse(spsolve(CHOLMOD_A, L, Sparse(B, 0))) Ac_ldiv_B(L::Factor, B::Dense) = solve(CHOLMOD_A, L, B) -Ac_ldiv_B(L::Factor, B::VecOrMat) = convert(Matrix, solve(CHOLMOD_A, L, Dense(B))) +Ac_ldiv_B(L::Factor, B::VecOrMat) = Matrix(solve(CHOLMOD_A, L, Dense(B))) Ac_ldiv_B(L::Factor, B::Sparse) = spsolve(CHOLMOD_A, L, B) Ac_ldiv_B(L::Factor, B::SparseVecOrMat) = Ac_ldiv_B(L, Sparse(B)) @@ -1678,7 +1676,7 @@ for f in (:\, :Ac_ldiv_B) if issuccess(F) return ($f)(F, B) else - return ($f)(lufact(convert(SparseMatrixCSC{eltype(A), SuiteSparse_long}, A)), B) + return ($f)(lufact(SparseMatrixCSC{eltype(A), SuiteSparse_long}(A)), B) end end end diff --git a/base/sparse/sparse.jl b/base/sparse/sparse.jl index abe6289b180702..1762a01e8bab39 100644 --- a/base/sparse/sparse.jl +++ b/base/sparse/sparse.jl @@ -24,7 +24,7 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh, vcat, hcat, hvcat, cat, imag, indmax, ishermitian, kron, length, log, log1p, max, min, maximum, minimum, norm, one, promote_eltype, real, reinterpret, reshape, rot180, rotl90, rotr90, round, scale!, setindex!, similar, size, transpose, tril, - triu, vec, permute!, map, map! + triu, vec, permute!, map, map!, Array export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros, diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index e6bc05766c1101..5c9444b268dde7 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -355,31 +355,30 @@ function similar(S::SparseMatrixCSC, ::Type{Tv}, ::Type{Ti}) where {Tv,Ti} end @inline similar(S::SparseMatrixCSC, ::Type{Tv}, d::Dims) where {Tv} = spzeros(Tv, d...) -# convert'ing between SparseMatrixCSC types -convert(::Type{AbstractMatrix{Tv}}, A::SparseMatrixCSC{Tv}) where {Tv} = A -convert(::Type{AbstractMatrix{Tv}}, A::SparseMatrixCSC) where {Tv} = convert(SparseMatrixCSC{Tv}, A) -convert(::Type{SparseMatrixCSC{Tv}}, S::SparseMatrixCSC{Tv}) where {Tv} = S -convert(::Type{SparseMatrixCSC{Tv}}, S::SparseMatrixCSC) where {Tv} = convert(SparseMatrixCSC{Tv,eltype(S.colptr)}, S) -convert(::Type{SparseMatrixCSC{Tv,Ti}}, S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = S -function convert(::Type{SparseMatrixCSC{Tv,Ti}}, S::SparseMatrixCSC) where {Tv,Ti} +# converting between SparseMatrixCSC types +AbstractMatrix{Tv}(A::SparseMatrixCSC) where {Tv} = SparseMatrixCSC{Tv}(A) +SparseMatrixCSC{Tv}(S::SparseMatrixCSC{Tv}) where {Tv} = copy(S) +SparseMatrixCSC{Tv}(S::SparseMatrixCSC) where {Tv} = SparseMatrixCSC{Tv,eltype(S.colptr)}(S) +SparseMatrixCSC{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = copy(S) +function SparseMatrixCSC{Tv,Ti}(S::SparseMatrixCSC) where {Tv,Ti} eltypeTicolptr = convert(Vector{Ti}, S.colptr) eltypeTirowval = convert(Vector{Ti}, S.rowval) eltypeTvnzval = convert(Vector{Tv}, S.nzval) return SparseMatrixCSC(S.m, S.n, eltypeTicolptr, eltypeTirowval, eltypeTvnzval) end -# convert'ing from other matrix types to SparseMatrixCSC (also see sparse()) -convert(::Type{SparseMatrixCSC}, M::Matrix) = sparse(M) -convert(::Type{SparseMatrixCSC}, M::AbstractMatrix{Tv}) where {Tv} = convert(SparseMatrixCSC{Tv,Int}, M) -convert(::Type{SparseMatrixCSC{Tv}}, M::AbstractMatrix{Tv}) where {Tv} = convert(SparseMatrixCSC{Tv,Int}, M) -function convert(::Type{SparseMatrixCSC{Tv,Ti}}, M::AbstractMatrix) where {Tv,Ti} +# converting from other matrix types to SparseMatrixCSC (also see sparse()) +SparseMatrixCSC(M::Matrix) = sparse(M) +SparseMatrixCSC(M::AbstractMatrix{Tv}) where {Tv} = SparseMatrixCSC{Tv,Int}(M) +SparseMatrixCSC{Tv}(M::AbstractMatrix{Tv}) where {Tv} = SparseMatrixCSC{Tv,Int}(M) +function SparseMatrixCSC{Tv,Ti}(M::AbstractMatrix) where {Tv,Ti} (I, J, V) = findnz(M) eltypeTiI = convert(Vector{Ti}, I) eltypeTiJ = convert(Vector{Ti}, J) eltypeTvV = convert(Vector{Tv}, V) return sparse_IJ_sorted!(eltypeTiI, eltypeTiJ, eltypeTvV, size(M)...) end -# convert'ing from SparseMatrixCSC to other matrix types -function convert(::Type{Matrix}, S::SparseMatrixCSC{Tv}) where Tv +# converting from SparseMatrixCSC to other matrix types +function Matrix(S::SparseMatrixCSC{Tv}) where Tv # Handle cases where zero(Tv) is not defined but the array is dense. A = length(S) == nnz(S) ? Matrix{Tv}(S.m, S.n) : zeros(Tv, S.m, S.n) for Sj in 1:S.n @@ -391,8 +390,8 @@ function convert(::Type{Matrix}, S::SparseMatrixCSC{Tv}) where Tv end return A end -convert(::Type{Array}, S::SparseMatrixCSC) = convert(Matrix, S) -full(S::SparseMatrixCSC) = convert(Array, S) +Array(S::SparseMatrixCSC) = Matrix(S) +full(S::SparseMatrixCSC) = Array(S) """ full(S) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 95c50798f3cdbd..eed31b5273a602 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -314,16 +314,14 @@ end ### Conversion # convert SparseMatrixCSC to SparseVector -function convert(::Type{SparseVector{Tv,Ti}}, s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti<:Integer} +function SparseVector{Tv,Ti}(s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti<:Integer} size(s, 2) == 1 || throw(ArgumentError("The input argument must have a single-column.")) SparseVector(s.m, s.rowval, s.nzval) end -convert(::Type{SparseVector{Tv}}, s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = - convert(SparseVector{Tv,Ti}, s) +SparseVector{Tv}(s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = SparseVector{Tv,Ti}(s) -convert(::Type{SparseVector}, s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = - convert(SparseVector{Tv,Ti}, s) +SparseVector(s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = SparseVector{Tv,Ti}(s) # convert Vector to SparseVector @@ -341,7 +339,7 @@ julia> sparsevec([1.0, 2.0, 0.0, 0.0, 3.0, 0.0]) [5] = 3.0 ``` """ -sparsevec(a::AbstractVector{T}) where {T} = convert(SparseVector{T, Int}, a) +sparsevec(a::AbstractVector{T}) where {T} = SparseVector{T, Int}(a) sparsevec(a::AbstractArray) = sparsevec(vec(a)) sparsevec(a::AbstractSparseArray) = vec(a) sparsevec(a::AbstractSparseVector) = vec(a) @@ -374,23 +372,21 @@ function _dense2sparsevec(s::AbstractArray{Tv}, initcap::Ti) where {Tv,Ti} SparseVector(n, nzind, nzval) end -convert(::Type{SparseVector{Tv,Ti}}, s::AbstractVector{Tv}) where {Tv,Ti} = +SparseVector{Tv,Ti}(s::AbstractVector{Tv}) where {Tv,Ti} = _dense2sparsevec(s, convert(Ti, max(8, div(length(s), 8)))) -convert(::Type{SparseVector{Tv}}, s::AbstractVector{Tv}) where {Tv} = - convert(SparseVector{Tv,Int}, s) +SparseVector{Tv}(s::AbstractVector{Tv}) where {Tv} = SparseVector{Tv,Int}(s) -convert(::Type{SparseVector}, s::AbstractVector{Tv}) where {Tv} = - convert(SparseVector{Tv,Int}, s) +SparseVector(s::AbstractVector{Tv}) where {Tv} = SparseVector{Tv,Int}(s) # convert between different types of SparseVector -convert(::Type{SparseVector{Tv}}, s::SparseVector{Tv}) where {Tv} = s -convert(::Type{SparseVector{Tv,Ti}}, s::SparseVector{Tv,Ti}) where {Tv,Ti} = s -convert(::Type{SparseVector{Tv,Ti}}, s::SparseVector) where {Tv,Ti} = +SparseVector{Tv}(s::SparseVector{Tv}) where {Tv} = s +SparseVector{Tv,Ti}(s::SparseVector{Tv,Ti}) where {Tv,Ti} = s +SparseVector{Tv,Ti}(s::SparseVector) where {Tv,Ti} = SparseVector{Tv,Ti}(s.n, convert(Vector{Ti}, s.nzind), convert(Vector{Tv}, s.nzval)) -convert(::Type{SparseVector{Tv}}, s::SparseVector{<:Any,Ti}) where {Tv,Ti} = +SparseVector{Tv}(s::SparseVector{<:Any,Ti}) where {Tv,Ti} = SparseVector{Tv,Ti}(s.n, s.nzind, convert(Vector{Tv}, s.nzval)) @@ -805,7 +801,7 @@ end ### Conversion to matrix -function convert(::Type{SparseMatrixCSC{Tv,Ti}}, x::AbstractSparseVector) where {Tv,Ti} +function SparseMatrixCSC{Tv,Ti}(x::AbstractSparseVector) where {Tv,Ti} n = length(x) xnzind = nonzeroinds(x) xnzval = nonzeros(x) @@ -818,13 +814,11 @@ function convert(::Type{SparseMatrixCSC{Tv,Ti}}, x::AbstractSparseVector) where SparseMatrixCSC(n, 1, colptr, rowval, nzval) end -convert(::Type{SparseMatrixCSC{Tv}}, x::AbstractSparseVector{<:Any,Ti}) where {Tv,Ti} = - convert(SparseMatrixCSC{Tv,Ti}, x) +SparseMatrixCSC{Tv}(x::AbstractSparseVector{<:Any,Ti}) where {Tv,Ti} = SparseMatrixCSC{Tv,Ti}(x) -convert(::Type{SparseMatrixCSC}, x::AbstractSparseVector{Tv,Ti}) where {Tv,Ti} = - convert(SparseMatrixCSC{Tv,Ti}, x) +SparseMatrixCSC(x::AbstractSparseVector{Tv,Ti}) where {Tv,Ti} = SparseMatrixCSC{Tv,Ti}(x) -function convert(::Type{Vector}, x::AbstractSparseVector{Tv}) where Tv +function Vector(x::AbstractSparseVector{Tv}) where Tv n = length(x) n == 0 && return Vector{Tv}(0) nzind = nonzeroinds(x) @@ -837,8 +831,8 @@ function convert(::Type{Vector}, x::AbstractSparseVector{Tv}) where Tv end return r end -convert(::Type{Array}, x::AbstractSparseVector) = convert(Vector, x) -full(x::AbstractSparseVector) = convert(Array, x) +Array(x::AbstractSparseVector) = Vector(x) +full(x::AbstractSparseVector) = Array(x) ### Array manipulation diff --git a/base/statistics.jl b/base/statistics.jl index 5094e43762ad0e..f37cad0cec742d 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -706,14 +706,14 @@ end T = promote_type(eltype(v), typeof(v[1]*h)) if h == 0 - return T(v[i]) + return convert(T, v[i]) else a = v[i] b = v[i+1] if isfinite(a) && isfinite(b) - return T(a + h*(b-a)) + return convert(T, a + h*(b-a)) else - return T((1-h)*a + h*b) + return convert(T, (1-h)*a + h*b) end end end diff --git a/base/sysimg.jl b/base/sysimg.jl index 55e64328aac3a5..883c174b470d48 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -102,10 +102,6 @@ include("refpointer.jl") include("checked.jl") using .Checked -# buggy handling of ispure in type-inference means this should be -# after re-defining the basic operations that they might try to call -(::Type{T})(arg) where {T} = convert(T, arg)::T # Hidden from the REPL. - # vararg Symbol constructor Symbol(x...) = Symbol(string(x...)) diff --git a/base/twiceprecision.jl b/base/twiceprecision.jl index 0e9a32f12a9ff5..ab3c5474720ab8 100644 --- a/base/twiceprecision.jl +++ b/base/twiceprecision.jl @@ -240,12 +240,15 @@ promote_rule(::Type{TwicePrecision{R}}, ::Type{TwicePrecision{S}}) where {R,S} = promote_rule(::Type{TwicePrecision{R}}, ::Type{S}) where {R,S} = TwicePrecision{promote_type(R,S)} +(::Type{T})(x::TwicePrecision) where {T<:Number} = T(x.hi + x.lo)::T +TwicePrecision{T}(x::Number) where {T} = TwicePrecision{T}(T(x), zero(T)) + convert(::Type{TwicePrecision{T}}, x::TwicePrecision{T}) where {T} = x convert(::Type{TwicePrecision{T}}, x::TwicePrecision) where {T} = TwicePrecision{T}(convert(T, x.hi), convert(T, x.lo)) -convert(::Type{T}, x::TwicePrecision) where {T<:Number} = convert(T, x.hi + x.lo) -convert(::Type{TwicePrecision{T}}, x::Number) where {T} = TwicePrecision{T}(convert(T, x), zero(T)) +convert(::Type{T}, x::TwicePrecision) where {T<:Number} = T(x) +convert(::Type{TwicePrecision{T}}, x::Number) where {T} = TwicePrecision{T}(x) float(x::TwicePrecision{<:AbstractFloat}) = x float(x::TwicePrecision) = TwicePrecision(float(x.hi), float(x.lo)) @@ -475,19 +478,19 @@ end /(r::StepRangeLen{<:Real,<:TwicePrecision}, x::Real) = StepRangeLen(r.ref/x, twiceprecision(r.step/x, nbitslen(r)), length(r), r.offset) -convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{T,R,S}) where {T<:AbstractFloat,R<:TwicePrecision,S<:TwicePrecision} = r +StepRangeLen{T,R,S}(r::StepRangeLen{T,R,S}) where {T<:AbstractFloat,R<:TwicePrecision,S<:TwicePrecision} = r -convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen) where {T<:AbstractFloat,R<:TwicePrecision,S<:TwicePrecision} = +StepRangeLen{T,R,S}(r::StepRangeLen) where {T<:AbstractFloat,R<:TwicePrecision,S<:TwicePrecision} = _convertSRL(StepRangeLen{T,R,S}, r) -convert(::Type{StepRangeLen{Float64}}, r::StepRangeLen) = +(::Type{StepRangeLen{Float64}})(r::StepRangeLen) = _convertSRL(StepRangeLen{Float64,TwicePrecision{Float64},TwicePrecision{Float64}}, r) -convert(::Type{StepRangeLen{T}}, r::StepRangeLen) where {T<:IEEEFloat} = +StepRangeLen{T}(r::StepRangeLen) where {T<:IEEEFloat} = _convertSRL(StepRangeLen{T,Float64,Float64}, r) -convert(::Type{StepRangeLen{Float64}}, r::AbstractRange) = +(::Type{StepRangeLen{Float64}})(r::AbstractRange) = _convertSRL(StepRangeLen{Float64,TwicePrecision{Float64},TwicePrecision{Float64}}, r) -convert(::Type{StepRangeLen{T}}, r::AbstractRange) where {T<:IEEEFloat} = +StepRangeLen{T}(r::AbstractRange) where {T<:IEEEFloat} = _convertSRL(StepRangeLen{T,Float64,Float64}, r) function _convertSRL(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{<:Integer}) where {T,R,S} diff --git a/test/abstractarray.jl b/test/abstractarray.jl index a736681aac040d..4491df1ef17241 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -197,9 +197,9 @@ end T24Linear(::Type{T}, dims::Int...) where T = T24Linear(T, dims) T24Linear(::Type{T}, dims::NTuple{N,Int}) where {T,N} = T24Linear{T,N,dims}() -Base.convert(::Type{T24Linear }, X::AbstractArray{T,N}) where {T,N } = convert(T24Linear{T,N}, X) -Base.convert(::Type{T24Linear{T }}, X::AbstractArray{_,N}) where {T,N,_} = convert(T24Linear{T,N}, X) -Base.convert(::Type{T24Linear{T,N}}, X::AbstractArray ) where {T,N } = T24Linear{T,N,size(X)}(X...) +T24Linear( X::AbstractArray{T,N}) where {T,N } = T24Linear{T,N}(X) +T24Linear{T }(X::AbstractArray{_,N}) where {T,N,_} = T24Linear{T,N}(X) +T24Linear{T,N}(X::AbstractArray ) where {T,N } = T24Linear{T,N,size(X)}(X...) Base.size(::T24Linear{T,N,dims}) where {T,N,dims} = dims import Base: IndexLinear @@ -215,10 +215,10 @@ end TSlow(::Type{T}, dims::Int...) where {T} = TSlow(T, dims) TSlow(::Type{T}, dims::NTuple{N,Int}) where {T,N} = TSlow{T,N}(Dict{NTuple{N,Int}, T}(), dims) -Base.convert(::Type{TSlow{T,N}}, X::TSlow{T,N}) where {T,N } = X -Base.convert(::Type{TSlow }, X::AbstractArray{T,N}) where {T,N } = convert(TSlow{T,N}, X) -Base.convert(::Type{TSlow{T }}, X::AbstractArray{_,N}) where {T,N,_} = convert(TSlow{T,N}, X) -Base.convert(::Type{TSlow{T,N}}, X::AbstractArray ) where {T,N } = begin +TSlow{T,N}(X::TSlow{T,N}) where {T,N } = X +TSlow( X::AbstractArray{T,N}) where {T,N } = TSlow{T,N}(X) +TSlow{T }(X::AbstractArray{_,N}) where {T,N,_} = TSlow{T,N}(X) +TSlow{T,N}(X::AbstractArray ) where {T,N } = begin A = TSlow(T, size(X)) for I in CartesianRange(size(X)) A[I.I...] = X[I.I...] diff --git a/test/arrayops.jl b/test/arrayops.jl index 89c1c27556d938..d97d863b134cea 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2141,7 +2141,7 @@ end Base.TypeArithmetic(::Type{F21666{T}}) where {T} = T() Base.:+(x::F, y::F) where {F <: F21666} = F(x.x + y.x) -Base.convert(::Type{Float64}, x::F21666) = Float64(x.x) +Float64(x::F21666) = Float64(x.x) @testset "Exactness of cumsum # 21666" begin # test that cumsum uses more stable algorithm # for types with unknown/rounding arithmetic diff --git a/test/dimensionful.jl b/test/dimensionful.jl index bb3b5ac2043af5..5c526c17e93ff7 100644 --- a/test/dimensionful.jl +++ b/test/dimensionful.jl @@ -11,12 +11,9 @@ struct Furlong{p,T<:Number} <: Number Furlong{p,T}(x::Furlong{p}) where {p,T} = new(x.val) end Furlong(x::T) where {T<:Number} = Furlong{1,T}(x) -(::Type{T})(x::Furlong{p,T}) where {p,T} = x.val +(::Type{T})(x::Furlong) where {T<:Number} = T(x.val) Furlong{p}(v::Number) where {p} = Furlong{p,typeof(v)}(v) -Base.convert(::Type{Furlong{p,T}}, x::Furlong{p,S}) where {T,p,S} = Furlong{p,T}(convert(T,x.val)) -Base.convert(::Type{Furlong{0,T}}, x::Furlong{0}) where {T} = Furlong{0,T}(convert(T, x.val)) -Base.convert(::Type{T}, x::Furlong{0}) where {T<:Number} = convert(T, x.val) -Base.convert(::Type{Furlong{0,T}}, x::Number) where {T} = Furlong{0,T}(convert(T, x)) +Furlong{p,T}(x::Furlong{p,S}) where {T,p,S} = Furlong{p,T}(T(x.val)) Base.promote_type(::Type{Furlong{p,T}}, ::Type{Furlong{p,S}}) where {p,T,S} = (Base.@_pure_meta; Furlong{p,promote_type(T,S)})