On Thu, 2016-07-14 at 22:53, Madeleine Udell <madeleine.ud...@gmail.com> 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
> <https://github.com/JuliaLang/julia/blob/master/base/linalg/dense.jl#L134>
> 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

Reply via email to