Re: [julia-users] Inherit from AbstractMatrix

2016-07-15 Thread Tim Holy
Hi Madeleine,

Is there any chance you have two versions of julia installed, and are running 
the wrong one? (Confession: I make that mistake surprisingly frequently!) The 
reason I ask is that on current master:

julia> methodswith(Array, kron)
0-element Array{Method,1}

In master (which is what you linked to) they have all been abstract-ized, yet 
in the julia session you showed, the "closest candidates" list includes 
several methods specific for `Array`.

Best,
--Tim

On Thursday, July 14, 2016 1:53:28 PM CDT Madeleine Udell wrote:
> Convex.jl defines an abstract type AbstractExpr, from which all important
> concrete types in the package are descended. We've defined a bunch of
> functions that allow users to treat AbstractExprs as though they were
> matrices: indexing, hcat, vcat, multiplication, addition, subtraction, etc.
> Can we use these to automatically get more advanced functions, like (for
> example) kron?
> 
> The code for kron
> 
> uses only indexing and multiplication operators. But I'm not sure how to
> make AbstractExpr (and its subtypes) subtype AbstractMatrix{Float64} or
> subtype AbstractArray{Float64,2} to make this transition seamless.
> 
> Simply defining
> 
> abstract AbstractExpr <: AbstractArray{Float64,2}
> 
> results in a method error: (Variable is defined as a subtype of
> AbstractExpr)
> 
> julia> kron(A,B)
> ERROR: MethodError: `kron` has no method matching kron(::Array{Float64,2},
> 
> ::Convex.Variable)
> 
> Closest candidates are:
>   kron(::Any, ::Any, ::Any, ::Any...)
>   kron{T,S}(::Array{T,2}, ::Array{S,2})
>   kron(::Union{Array{T,1},Array{T,2}}, ::Number)
> 
> 1) Is it possible to have Convex's AbstractExpr inherit from AbstractArray?
> What other methods would we need to implement to make this work?
> 2) Is it a terrible idea, for reasons I haven't thought of?
> 
> Thanks!
> Madeleine




Re: [julia-users] Inherit from AbstractMatrix

2016-07-15 Thread Mauro
On Thu, 2016-07-14 at 22:53, Madeleine Udell  wrote:
> Convex.jl defines an abstract type AbstractExpr, from which all important
> concrete types in the package are descended. We've defined a bunch of
> functions that allow users to treat AbstractExprs as though they were
> matrices: indexing, hcat, vcat, multiplication, addition, subtraction, etc.
> Can we use these to automatically get more advanced functions, like (for
> example) kron?

Yes, that should work, barring any not generically defined methods.  See
here for some description of the needed interface for a AbstractArray:
http://docs.julialang.org/en/release-0.4/manual/interfaces/#abstract-arrays

> The code for kron
> 
> uses only indexing and multiplication operators. But I'm not sure how to
> make AbstractExpr (and its subtypes) subtype AbstractMatrix{Float64} or
> subtype AbstractArray{Float64,2} to make this transition seamless.

In Julia-0.5 this should work as the definition is generically defined:

kron{T,S}(a::AbstractMatrix{T}, b::AbstractMatrix{S})

But that was not the case in 0.4.  Did you run your example in 0.4?

My understanding is that the aim is to have AbstractArray fallback
methods for (almost) all matrix functions.  If you find one which
doesn't have one you should probably file an issue/PR.

> Simply defining
>
> abstract AbstractExpr <: AbstractArray{Float64,2}
>
> results in a method error: (Variable is defined as a subtype of
> AbstractExpr)
>
> julia> kron(A,B)
> ERROR: MethodError: `kron` has no method matching kron(::Array{Float64,2},
> ::Convex.Variable)
> Closest candidates are:
>   kron(::Any, ::Any, ::Any, ::Any...)
>   kron{T,S}(::Array{T,2}, ::Array{S,2})
>   kron(::Union{Array{T,1},Array{T,2}}, ::Number)
>
> 1) Is it possible to have Convex's AbstractExpr inherit from AbstractArray?
> What other methods would we need to implement to make this work?
> 2) Is it a terrible idea, for reasons I haven't thought of?
>
> Thanks!
> Madeleine