I want to define a matrix wrapper type in order to store more information than just the content of the matrix itself:
immutable MatWrap{T<:Number,S<:AbstractMatrix} <: AbstractMatrix{T}
data::S
otherstuff
end
Is there a way to automatically inherit all of the matrix operations, including optimized ones (e.g. if the matrix is Hermitian), without redefining all the A_mul_B and friends?
For example if I multiply A*B=C, C should be a MatWrap object.
On a related note, I was wondering why a wrapper like that has such a huge difference on timing:
import Base: *
using BenchmarkTools
immutable MatWrap{T<:Number}
data::Matrix{T}
stuff::Int
end
*(A::MatWrap,B::MatWrap) = MatWrap(A.data*B.data,A.stuff)
n = 10
A = MatWrap(rand(n,n),1);
B = MatWrap(rand(n,n),2);
@time for i=1:300000;A*B;end
@time for i=1:300000;A.data*B.data;end
0.615447 seconds (989.79 k allocations: 279.154 MB, 15.86% gc time)
0.142688 seconds (300.14 k allocations: 256.356 MB, 3.06% gc time)
It seems like precompiling by doing A*B in advance accounts for most of the time difference here. I was trying to get a reduced test case for a slightly more complex situation but that isn’t it…