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?