# Inheritance for Matrix Wrapper Type

**URL:** <https://discourse.julialang.org/t/inheritance-for-matrix-wrapper-type/3346>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 23, 2017, 3:04pm UTC](https://discourse.julialang.org/t/inheritance-for-matrix-wrapper-type/3346 "2017-04-23T15:04:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 23, 2017, 3:04pm UTC](https://discourse.julialang.org/t/inheritance-for-matrix-wrapper-type/3346/1 "2017-04-23T15:04:44Z")

</div>

I want to define a matrix wrapper type in order to store more information than just the content of the matrix itself:

```julia
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.

Thanks

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 25, 2017, 6:04pm UTC](https://discourse.julialang.org/t/inheritance-for-matrix-wrapper-type/3346/2 "2017-04-25T18:04:42Z")

</div>

I guess there isn’t a way…

On a related note, I was wondering why a wrapper like that has such a huge difference on timing:

```julia
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)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 25, 2017, 6:08pm UTC](https://discourse.julialang.org/t/inheritance-for-matrix-wrapper-type/3346/3 "2017-04-25T18:08:18Z")

</div>

Is that not timed twice? That seems like a huge jump in allocations, indicating something was being compiled or not type-stable in the first timing.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 25, 2017, 6:19pm UTC](https://discourse.julialang.org/t/inheritance-for-matrix-wrapper-type/3346/4 "2017-04-25T18:19:18Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 25, 2017, 6:47pm UTC](https://discourse.julialang.org/t/inheritance-for-matrix-wrapper-type/3346/5 "2017-04-25T18:47:25Z")

</div>

Managed to figure it out, it’s because I had an abstract type on `data`…
