Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Use new compact parametric syntax where possible #20446

Merged
merged 28 commits into from
Feb 9, 2017

Conversation

pabloferz
Copy link
Contributor

@pabloferz pabloferz changed the title Use new compact parametric syntax where possible WIP: Use new compact parametric syntax where possible Feb 3, 2017
@JeffBezanson
Copy link
Member

This is great. The payoff is especially large when multiple parameters are involved.

@@ -249,7 +249,7 @@ end

### Default to no pivoting (and storing of upper factor) when not explicit
cholfact!(A::Hermitian) = cholfact!(A, Val{false})
cholfact!{T<:Real,S}(A::Symmetric{T,S}) = cholfact!(A, Val{false})
cholfact!(A::Symmetric{<:Real}) = cholfact!(A, Val{false})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this really go from two parameters to one? I guess the 2nd isn't used at all?

Copy link
Contributor Author

@pabloferz pabloferz Feb 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

julia> Symmetric{<:Real}
Symmetric{#s1,S} where S<:(AbstractArray{T,2} where T) where #s1<:Real

And yes, the second isn't used.

@pabloferz pabloferz force-pushed the pz/newparamsyntax branch 4 times, most recently from 786df30 to ee778f5 Compare February 4, 2017 11:07
@@ -89,7 +89,7 @@ julia> extrema(b)
"""
linearindices(A) = (@_inline_meta; OneTo(_length(A)))
linearindices(A::AbstractVector) = (@_inline_meta; indices1(A))
eltype(::Type{A}) where A<:AbstractArray{E} where E = E
eltype(::Type{<:AbstractArray{E}}) where E = E
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parenthesize the where (E) ? would look a little more readable

@pabloferz pabloferz force-pushed the pz/newparamsyntax branch 2 times, most recently from fe86631 to 928c821 Compare February 4, 2017 11:34
@@ -46,43 +46,43 @@ end
read_tree!(idx::GitIndex, hash::AbstractGitHash) =
read_tree!(idx, GitTree(repository(idx), hash))

function add!{T<:AbstractString}(idx::GitIndex, files::T...;
flags::Cuint = Consts.INDEX_ADD_DEFAULT)
function add!(idx::GitIndex, files::AbstractString...;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should still work, but this change is slightly more permissive right? In the old version wouldn't each of files have to be the same type, but now they can individually be different AbstractString subtypes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like an improvement.

@@ -445,13 +445,13 @@ function getindex{T<:BlasFloat}(C::CholeskyPivoted{T}, d::Symbol)
throw(KeyError(d))
end

show{T,S<:AbstractMatrix}(io::IO, C::Cholesky{T,S}) =
show{T}(io::IO, C::Cholesky{T,<:AbstractMatrix}) =
Copy link
Contributor

@tkelman tkelman Feb 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the T be <:Any here? (also L454)

Copy link
Member

@stevengj stevengj Feb 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to support Foo{_,<:Bar} for this sort of thing eventually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<:Any is perfectly clear to me, I don't think it needs an abbreviation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that <:Any is clear, but I find @StevenG's

Foo{_,<:Bar}

cleaner than

Foo{<:Any,<:Bar}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giving a special meaning to underscore like that is not at all intuitive. Saving 4 characters isn't worth ending up with code that requires finding an obscure manual section to understand.

A_ldiv_B!{T,S<:StridedMatrix}(A::LU{T,S}, B::StridedMatrix) = A_ldiv_B!(UpperTriangular(A.factors), A_ldiv_B!(UnitLowerTriangular(A.factors), B[ipiv2perm(A.ipiv, size(B, 1)),:]))
A_ldiv_B!{T<:BlasFloat}(A::LU{T,<:StridedMatrix}, B::StridedVecOrMat{T}) = @assertnonsingular LAPACK.getrs!('N', A.factors, A.ipiv, B) A.info
A_ldiv_B!{T}(A::LU{T,<:StridedMatrix}, b::StridedVector) = A_ldiv_B!(UpperTriangular(A.factors), A_ldiv_B!(UnitLowerTriangular(A.factors), b[ipiv2perm(A.ipiv, length(b))]))
A_ldiv_B!{T}(A::LU{T,<:StridedMatrix}, B::StridedMatrix) = A_ldiv_B!(UpperTriangular(A.factors), A_ldiv_B!(UnitLowerTriangular(A.factors), B[ipiv2perm(A.ipiv, size(B, 1)),:]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like T could be <:Any for the latter two


cond{T<:BlasFloat,S<:StridedMatrix}(A::LU{T,S}, p::Number) = inv(LAPACK.gecon!(p == 1 ? '1' : 'I', A.factors, norm((A[:L]*A[:U])[A[:p],:], p)))
cond{T<:BlasFloat}(A::LU{T,<:StridedMatrix}, p::Number) = inv(LAPACK.gecon!(p == 1 ? '1' : 'I', A.factors, norm((A[:L]*A[:U])[A[:p],:], p)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LU{<:BlasFloat,<:StridedMatrix} should work for these 3?

det(A::Symmetric) = det(bkfact(A))

\{T,S<:StridedMatrix}(A::HermOrSym{T,S}, B::StridedVecOrMat) = \(bkfact(A.data, Symbol(A.uplo), issymmetric(A)), B)
\{T}(A::HermOrSym{T,<:StridedMatrix}, B::StridedVecOrMat) = \(bkfact(A.data, Symbol(A.uplo), issymmetric(A)), B)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could T be replaced by <:Any here?

LAPACK.trrfs!($uploc, 'N', $isunitc, A.data, B, X)

# Condition numbers
function cond{T<:BlasFloat,S}(A::$t{T,S}, p::Real=2)
function cond{T<:BlasFloat}(A::$t{T}, p::Real=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A::$t{<:BlasFloat} ?

LAPACK.trevc!('L', 'A', BlasInt[], tril!(A.data)')
end
function eigvecs{T<:BlasFloat,S<:StridedMatrix}(A::UnitLowerTriangular{T,S})
function eigvecs{T<:BlasFloat}(A::UnitLowerTriangular{T,<:StridedMatrix})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A::XTriangular{<:BlasFloat,<:StridedMatrix} for these 4?

issparse{T}(S::LowerTriangular{T, <:AbstractSparseMatrix}) = true
issparse{T}(S::LinAlg.UnitLowerTriangular{T, <:AbstractSparseMatrix}) = true
issparse{T}(S::UpperTriangular{T, <:AbstractSparseMatrix}) = true
issparse{T}(S::LinAlg.UnitUpperTriangular{T, <:AbstractSparseMatrix}) = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{<:Any, <:AbstractSparseMatrix} for these?

@@ -222,7 +222,7 @@ function reinterpret{T,Tv,Ti,N}(::Type{T}, a::SparseMatrixCSC{Tv,Ti}, dims::NTup
return SparseMatrixCSC(mS, nS, colptr, rowval, nzval)
end

function copy{T,P<:SparseMatrixCSC}(ra::ReshapedArray{T,2,P})
function copy{T}(ra::ReshapedArray{T,2,<:SparseMatrixCSC})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T can be <:Any (unless ReshapedArray has constraints I guess)

hcat{Tv,Ti<:Integer}(X::SparseVector{Tv,Ti}...) = _absspvec_hcat(X...)
hcat{Tv,Ti<:Integer}(X::AbstractSparseVector{Tv,Ti}...) = _absspvec_hcat(X...)
hcat{Tv}(X::SparseVector{Tv,<:Integer}...) = _absspvec_hcat(X...)
hcat{Tv}(X::AbstractSparseVector{Tv,<:Integer}...) = _absspvec_hcat(X...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this allow each argument to have a different index type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes:

julia> f(::(Vector{T} where T<:Real)...) = 1
f (generic function with 1 method)

julia> f([1,2,3], [4.0])
1

@stevengj
Copy link
Member

stevengj commented Feb 5, 2017

These lines can now probably be improved to:

rcum_promote_type{T,N}(op, ::Type{<:AbstractArray{T,N}}) = Array{rcum_promote_type(op,T), N}

@pabloferz
Copy link
Contributor Author

These lines can now probably be improved

Done

@deprecate big{T<:Integer,N}(x::AbstractArray{Complex{Rational{T}},N}) big.(A)
@deprecate big(A::AbstractArray{<:Complex{<:Integer}}) big.(A)
@deprecate big(A::AbstractArray{<:Complex{<:AbstractFloat}}) big.(A)
@deprecate big(x::AbstractArray{<:Complex{<:Rational{<:Integer}}}) big.(A)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do these end up needing multiple levels of <: ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An AbstractArray{Complex{<:Integer}} would be an array of elements that can hold any integer complex type (e.g. the elements can be Complex{Int}, Complex{Int8}, etcetera in the same array).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AbstractArray{Complex{<:Integer}} is equivalent to AbstractArray{Complex{T} where T<:Integer} so it matches AbstratctArrays with a UnionAll element type (Complex{T} where T<:Integer).

foo(::AbstractArray{Complex{<:Integer}}) = 1
bar(::AbstractArray{<:Complex{<:Integer}}) = 2
foo([1im]) #MethodError
bar([1im]) == 2
foo((Complex{T} where T<:Integer)[1im]) == 1
bar((Complex{T} where T<:Integer)[1im]) == 2
# since AbstractArray{Complex{T} where T<:Integer} <: (AbstractArray{T} where T<:(Complex{I} where I))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnionAll element types don't seem like they're going to occur in practice

Copy link
Contributor Author

@pabloferz pabloferz Feb 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but AbstractArray{<:Complex{<:Integer}} will handle the cases we care about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the problem not the number of wheres we need, but the placement? so AbstractArray{Complex{<:Integer}} is AbstractArray{Complex{T} where T<:Integer} but we actually want AbstractArray{Complex{T}} where T<:Integer ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had R = AbstractArray{Complex{I},N} where N where I<:Integer and with this we have S = AbstractArray{C,N} where N where C<:(Complex{I} where I<:Integer), but R <: S. This will handle a little more cases (which we almost surely won't be seeing), but I don't see the harm in doing so.

f = deserialize_rr(s,t)
Future(f.where, RRID(f.whence, f.id), f.v) # ctor adds to client_refs table
end

function deserialize{T<:RemoteChannel}(s::AbstractSerializer, t::Type{T})
function deserialize(s::AbstractSerializer, t::Type{<:RemoteChannel})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you had also converted function deserialize(s::ClusterSerializer, t::Type{T}) where T <: CapturedException in an earlier version, which is now in clusterserialize.jl

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed it with all the moving from that PR. Will change it again.

@pabloferz pabloferz force-pushed the pz/newparamsyntax branch 2 times, most recently from 66a7209 to 6a7b225 Compare February 6, 2017 20:39
@pabloferz pabloferz merged commit 79471c1 into JuliaLang:master Feb 9, 2017
@pabloferz pabloferz deleted the pz/newparamsyntax branch February 9, 2017 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging this pull request may close these issues.