# How to preserve custom type after broadcasting?

**URL:** <https://discourse.julialang.org/t/how-to-preserve-custom-type-after-broadcasting/21047>\
**Category:** General Usage\
**Created:** [February 21, 2019, 1:36pm UTC](https://discourse.julialang.org/t/how-to-preserve-custom-type-after-broadcasting/21047 "2019-02-21T13:36:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [February 21, 2019, 1:36pm UTC](https://discourse.julialang.org/t/how-to-preserve-custom-type-after-broadcasting/21047/1 "2019-02-21T13:36:25Z")

</div>

I’d like to preserve the custom type after it has been broadcasted. Currently I have the following:

```julia
struct SigmaPoints{T} <: AbstractMatrix{T}
    x0::Array{T, 1}
    xi::Array{T, 2}
end
Base.size(S::SigmaPoints) = (size(S.xi, 1), size(S.xi, 2) + 1)
Base.getindex(S::SigmaPoints{T}, inds::Vararg{Int,2}) where {T} = if inds[2] == 1 S.x0[inds[1]] else S.xi[inds[1], inds[2] - 1] end
Base.setindex!(S::SigmaPoints{T}, val, inds::Vararg{Int,2}) where {T} = if inds[2] == 1 S.x0[inds[1]] = val else S.xi[inds[1], inds[2] - 1] = val end

```

`SigmaPoints` is essentially a matrix where the first column is saved in `x0` instead of `xi`

```julia
julia> S = SigmaPoints(ones(2), zeros(2,4))
4×5 SigmaPoints{Float64}:
 1.0 0.0 0.0 0.0 0.0
 1.0 0.0 0.0 0.0 0.0
julia> S .+ ones(2)
4×5 Array{Float64,2}:
 2.0 1.0 1.0 1.0 1.0
 2.0 1.0 1.0 1.0 1.0

```

Mathematically that’s correct. However, I’d like to preserve the `SigmaPoints` type for basic operations (e.g. multiplications, additions, etc) with scalars and vectors.  
I have read the [manual](https://docs.julialang.org/en/latest/manual/interfaces/#Selecting-an-appropriate-output-array-1), but I am struggling to map the example to this case.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 21, 2019, 2:27pm UTC](https://discourse.julialang.org/t/how-to-preserve-custom-type-after-broadcasting/21047/2 "2019-02-21T14:27:53Z")

</div>

Define `BroadcastStyle` and `similar` for your type:

```julia
Base.BroadcastStyle(::Type{<:SigmaPoints}) = Broadcast.ArrayStyle{SigmaPoints}()

Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{SigmaPoints}}, ::Type{T}) where T =
    SigmaPoints(similar(bc.args[1].x0), similar(bc.args[1].xi))

```

And then:

```julia
julia> SigmaPoints(ones(2), zeros(2,4)) .+ ones(2)
2×5 SigmaPoints{Float64}:
 2.0 1.0 1.0 1.0 1.0
 2.0 1.0 1.0 1.0 1.0

```
