# What exactly is an abstract array? (Deciding how to dispatch)

**URL:** <https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256>\
**Category:** Offtopic\
**Tags:** question\
**Created:** [January 3, 2017, 7:51am UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256 "2017-01-03T07:51:49Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [January 3, 2017, 7:51am UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/1 "2017-01-03T07:51:49Z")

</div>

I am trying to find out what the going definition for an `AbstractArray` is in order to properly choose how to dispatch. As usual, this is best understood by an example. Let’s look at two different ways of writing “the same code”.

```julia
for i in number_of_iterations
  du = f(t,u)
  u += dt*du
  t += dt
end

```

```julia
# Make a cache for du
for i in number_of_iterations
  f!(t,u,du) # update `du` inplace
  u .+= dt.*du
  t += dt
end

```

(Yes, for those who know differential equations, this is the Euler method)

If one is doing standard computing and working on arrays, the second method is clearly better because it gets rid of the temporary arrays. However, since I want to provide both support for a broad array of types and good performance, I provide both versions for each method (and there are some good examples which need the first method).

And that’s where the problem comes in. If I only had one method, I would duck type it and leave it for people to see if it works on their weird type. I am kind of thinking that the way to distinguish between the two is by `AbstractArray`, but I am wondering if that is specific enough. Specifically, the second one will work on (and be faster) any mutable type which implements broadcasting. Is that what an `AbstractArray` is? Or is there a slightly more general trait I can be dispatching on?

[@dlfivefifty would you mind chiming in on how a `Fun` fits in? Are they more array-like and should be using the second version in some form?]

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 3, 2017, 8:21am UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/2 "2017-01-03T08:21:45Z")

</div>

Seems easier to just have this decision be made from a user option instead of trying to use dispatch for it. Maybe an `inplace = true` kw or something.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 3, 2017, 10:50am UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/3 "2017-01-03T10:50:36Z")

</div>

why not just a version with or without `!`? (just curious)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 3, 2017, 1:13pm UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/4 "2017-01-03T13:13:33Z")

</div>

An `AbstractArray` can be read-only. e.g. a `Range` like `1:3` is an `AbstractArray` but is immutable. For the methods an array type should implement, see: [http://docs.julialang.org/en/latest/manual/arrays.html#Implementation-1](http://docs.julialang.org/en/latest/manual/arrays.html#Implementation-1)

To get a mutable array of the same shape/eltype as a given `A::AbstractArray`, you use `similar(A)`, or `copy!(similar(A), A)` to make a mutable copy. In your case, the usual style would be to define both an out-of-place function `foo` and an in-place function `foo!`, e.g.:

```julia
function foo!(u)
    for ...
       u .+= ....
    end
    return u
end
foo(u::AbstractArray) = foo!(copy!(similar(u), u))
foo(u) = foo!(collect(u)) # handle arbitrary iterable u

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 3, 2017, 1:39pm UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/5 "2017-01-03T13:39:33Z")

</div>

That last version, with defining the `foo!` type and then another version that does `foo` on a `copy` seems really ideomatic in julia code, at least it is a design pattern I have encountered numerous times.

Is the above way of coding this functionality the preferred / most ideomatic? I am wondering whether just a short function `mcopy(u) = copy!(similar(u, u))` would be useful, or even a macro that could be applied to `foo!` that would produce the relevant `foo`s? I could definitely see myself tripping over not realizing that an array may be immutable, i.e. I have expressions like `foo(u) = foo!(copy(u))` many places in my code.

---

<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:** [January 3, 2017, 5:43pm UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/6 "2017-01-03T17:43:43Z")

</div>

That really convinces me that I was trying to be too fancy and should choose the style based off of the user input. I already place a type-parameter `inplace` on the problem definition when it’s created (by counting args of the dispatch with the most args, the very much so most common case is to have only one dispatch or have many which add things like Jacobians so that heuristic works very well here). As @kristoffer.carlsson suggests, I should just use that to dispatch instead of some other criteria. Normally I would use something like @stevengj’s proposal, but in this case I want to guerentee that output types match the user’s input types which that violates (but in most places, the convenience is good).

Also, to truly do this for AbstractArrays, you need something more than `copy!`. I have `recursivecopy!` in RecursiveArrayTools.jl for doing something like this, because the above breaks on `Vector{Vector}` etc. I guess that’s another reason why users should be able to choose the form themselves.

Thanks for steering me in the new direction. I think I got stuck for a bit on one silly idea!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 3, 2017, 8:29pm UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/7 "2017-01-03T20:29:22Z")

</div>

> [@mkborregaard](#):
>
> I am wondering whether just a short function `mcopy(u) = copy!(similar(u), u))` would be useful

Actually, there is a function `Base.copymutable(u)` that does exactly this, with the bonus that it gives `collect(u)` for generic iterable types. (See [fix and consolidate uses of copy(a) and copy(similar(a),a) in to copymutable(a) by stevengj · Pull Request #16620 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/16620)) Maybe it should be exported?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [January 3, 2017, 9:49pm UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/8 "2017-01-03T21:49:53Z")

</div>

> [@ChrisRackauckas](#):
>
> [@dlfivefifty would you mind chiming in on how a Fun fits in? Are they more array-like and should be using the second version in some form?]

`Fun`s should work with either form. They are not “array-like” however as there is no `getindex`.

(Originally, as in chebfun, they were treated as continuously indexed vectors, so `getindex` was used for function evaluation, and `.*` was mandatory for multiplication. This analogy was dropped as it wasn’t very intuitive and overloading array interface routines caused issues.)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 3, 2017, 10:25pm UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/9 "2017-01-03T22:25:54Z")

</div>

If (as suggested I think?) that a preferred design pattern is to generally aim to define an inplace `foo!(u)` and then define a `foo(u) = foo!(copymutable(u))`, then yes, it would be a great convenience if that was exported 🙂

---

<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:** [January 4, 2017, 3:45am UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/10 "2017-01-04T03:45:09Z")

</div>

> [@dlfivefifty](#):
>
> Funs should work with either form. They are not “array-like” however as there is no getindex.

Oh okay, so they don’t index but still broadcast? Anything that can index can broadcast, right? So boradcast is strictly more general?

[Yeah, the decision is to choose the form via the user given function. This is too subtle to accuracy predict.]

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [January 4, 2017, 3:55am UTC](https://discourse.julialang.org/t/what-exactly-is-an-abstract-array-deciding-how-to-dispatch/1256/11 "2017-01-04T03:55:01Z")

</div>

> [@ChrisRackauckas](#):
>
> Oh okay, so they don’t index but still broadcast?

Correct. The support for broadcasting was thrown in per @stevengj’s request, so probably a bit more thought could go into it. For example, boh the broadcasting and non-broadcasting syntax (`exp(f)` vs `exp.(f)`) mean the same thing though they use different algorithms.
