Writing a new type for a specific kind of array (constructor error)

I’m trying to define a new type for array with the following conditions:

  • It must be square and two-dimensional
  • The elements of each column must sum to 1
  • Ideally it would be a subtype of two-dimensional arrays so that I can use the already-written functions for finding the inverse matrix, eigenvalues and eigenvectors, etc.

There will eventually be functions written which are specific to this kind of array, and I have a nice use case for them, but right now I’m struggling to even get the constructor working and would really appreciate some help.

Here is the MWE:

struct ProbMatrix{T<:Real} <: AbstractArray{T,2}
        data

        function ProbMatrix{T}(data::Array{T,2}) where T<:Real
                if ndims(data) != 2
                        error("Probability matrix needs to be two-dimensional")
                end
                if size(data,1) != size(data,2)
                        error("Probability matrix needs to be square.")
                end
                for col_num in 1:size(data,1)
                        if sum(data[:,col_num]) != 1
                                error("Each column of a probability matrix must sum to one.")
                        end
                end
                new{T}(data)
        end
end

Mat = [0.8 0.05 ; 0.2 0.95]

println(inv(Mat))

A = ProbMatrix(Mat)

println(inv(A))

The following error is raised:

ERROR: LoadError: MethodError: no method matching ProbMatrix(::Array{Float64,2})

…which I don’t understand as I thought I had quite specifically defined this case?

Welcome @gpownall !

Here you go:

struct ProbMatrix{T<:Real} <: AbstractArray{T,2}
        data
        function ProbMatrix(data::Array{T,2}) where T<:Real
                if ndims(data) != 2
                        error("Probability matrix needs to be two-dimensional")
                end
                if size(data,1) != size(data,2)
                        error("Probability matrix needs to be square.")
                end
                for col_num in 1:size(data,1)
                        if sum(data[:,col_num]) != 1
                                error("Each column of a probability matrix must sum to one.")
                        end
                end
                new{T}(data)
        end
end

Base.size(m::ProbMatrix{T}) where T = size(m.data)

Mat = [0.8 0.05 ; 0.2 0.95]

println(inv(Mat))

A = ProbMatrix(Mat);

As your struct is subtyped AbstractArray{T,2} , size needs to be defined for the new type.

There is more, you have to define, e.g. getindex . You get the error if you omit the ; in the last line:

julia> A = ProbMatrix(Mat)
2×2 ProbMatrix{Float64}:
Error showing value of type ProbMatrix{Float64}:
ERROR: getindex not defined for ProbMatrix{Float64}

We had another similar thread, where you can find a few details about what was wrong in general: Inner constructor – checking for equal lengths

1 Like

You only defined ProbMatrix{T}(::Array{T,2}), but you’re trying to call ProbMatrix(::Array{T,2}). The easiest way to solve this is to just add an outer constructor.

ProbMatrix(data::Array{T,2}) where {T} = ProbMatrix{T}(data)

But even if you define this you’ll notice that you can’t actually print your array. That’s because you need to define a handful of other methods to adopt the AbstractArray interface.

Also I think you should replace Array{T,2} with AbstractArray{T,2}, but that’s a separate issue.

1 Like

Understood, thanks!