# ERROR: MethodError: no method matching cos(::Array{Int64,1})

**URL:** https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217
**Category:** New to Julia
**Created:** [June 13, 2019, 3:06am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217 "2019-06-13T03:06:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![JDA](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@JDA](https://discourse.julialang.org/u/JDA)
#### Post date: [June 13, 2019, 3:06am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217/1 "2019-06-13T03:06:45Z")

</div>

What is the reason for this ?

---

<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: [June 13, 2019, 3:55am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217/2 "2019-06-13T03:55:53Z")

</div>

You want `cos.(array)`, not `cos(array)`, for the elementwise cosine.

See [https://docs.julialang.org/en/latest/manual/functions/#man-vectorized-1](https://docs.julialang.org/en/latest/manual/functions/#man-vectorized-1) and [More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots)

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [June 13, 2019, 4:33am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217/3 "2019-06-13T04:33:12Z")

</div>

Since you ask for a reason:

A function like cosine is meaningful for certain arrays (e.g. as a power series for matrices) and using the notation `cos(A)` for something other that that when `A` is a matrix is simply incoherent. For a vector argument, `cos(v)` is mathematically meaningless but primitive languages saw fit to abuse the notation for element-wise application. The incoherent precedent of other languages seduced even the Julia developers for a few years, but now `cos(A::Matrix)` does the mathematically sensible thing in Julia; the vector case errors out; and we have a well-designed alternative for element-wise application.

Can you think of an improvement to Julia documentation that would have helped you to avoid this misapprehension?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 13, 2019, 6:39am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217/4 "2019-06-13T06:39:28Z")

</div>

> [@Ralph\_Smith](#):
>
> Can you think of an improvement to Julia documentation that would have helped you to avoid this misapprehension?

Make it self-aware and nag the user until it is read.

---

<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: [June 13, 2019, 7:46am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217/5 "2019-06-13T07:46:55Z")

</div>

> [@Ralph\_Smith](#):
>
> Can you think of an improvement to Julia documentation that would have helped you to avoid this misapprehension?

`cos(::AbstractVector)` could be overloaded to return an error message pointing to `cos.(x)`. Though it doesn’t help if they try to apply it to a square matrix.

---

<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: [June 13, 2019, 12:26pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217/6 "2019-06-13T12:26:51Z")

</div>

> [@Ralph\_Smith](#):
>
> For a vector argument, `cos(v)` is mathematically meaningless

Math is flexible — you can define symbols to mean whatever you want. Julia uses the dot notation for elementwise calls for more pragmatic reasons outlined in my blog post linked above.

- `f.(x)` means that the caller can use _any_ function elementwise — it’s not up to the function author to decide in advance which functions are useful to vectorize.
- `f.(x)` exposes the elementwise nature of the call to Julia at the syntactic level, allowing Julia to provide a syntactic guarantee that nested calls like `f.(g.(x))` are fused into a single `broadcast` call.

As a nice side effect, this frees up the name of functions like `cos(x)` so that we can use the same name for the matrix functions.

The tradeoff is that users coming from Matlab, Numpy, and similar are surprised at first by how Julia vectorization works — if you are interested, there was a long debate about this in [#23233](https://github.com/JuliaLang/julia/pull/23233).

> [@dlfivefifty](#):
>
> `cos(::AbstractVector)` could be overloaded to return an error message pointing to `cos.(x)`

Yes, keeping deprecation messages indefinitely for common vectorized functions was definitely proposed when we made the dot-call changeover, but I can’t find the discussion at the moment.

---

<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: [June 13, 2019, 12:28pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-cos-array-int64-1/25217/7 "2019-06-13T12:28:58Z")

</div>

> [@stevengj](#):
>
> keeping deprecation messages indefinitely

Actually I meant a standard error, not a deprecation warning. (The fact that it used to work just adds noise to the message.)
