# Status of exp and sqrt

**URL:** <https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092>\
**Category:** Internals & Design\
**Created:** [June 5, 2017, 2:04pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092 "2017-06-05T14:04:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)\
**Post date:** [June 5, 2017, 2:04pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/1 "2017-06-05T14:04:37Z")

</div>

Now that exp. and sqrt. exist, will expm and sqrtm be deprecated and replaced by exp and sqrt on matrices? Is the answer no because it would be too error-prone?

---

<div class="post-metadata">

**Author:** ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)\
**Post date:** [June 5, 2017, 2:06pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/2 "2017-06-05T14:06:24Z")

</div>

The `expm` and `sqrtm` functions are matrix operations, not elementwise operations. I expect they’ll be sticking around.

---

<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:** [June 5, 2017, 2:09pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/3 "2017-06-05T14:09:22Z")

</div>

Edit: Below is likely not what OP meant.

To be explicit:

```julia
julia> A = rand(2,2)
2×2 Array{Float64,2}:
 0.0692582 0.167208
 0.223925 0.360005

julia> expm(A'A)
2×2 Array{Float64,2}:
 1.06113 0.10272
 0.10272 1.17547

julia> exp.(A'A)
2×2 Array{Float64,2}:
 1.05648 1.09658
 1.09658 1.17065

julia> expm(A'A) ≈ exp.(A'A)
false

```

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [June 5, 2017, 2:12pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/4 "2017-06-05T14:12:03Z")

</div>

But having `exp.` to mean elementwise operations, wouldn’t it be correct for `exp` to mean matrix exponentiation (although possibly against all other languages)?

---

<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:** [June 5, 2017, 2:17pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/5 "2017-06-05T14:17:35Z")

</div>

Ah, that makes more sense. Hm, I think that would perhaps be error prone. On the other hand, it might enable generic code that will both work with matrices and scalars?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 5, 2017, 2:20pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/6 "2017-06-05T14:20:32Z")

</div>

Some prior discussion here: [https://github.com/JuliaLang/julia/issues/5840#issuecomment-237077416](https://github.com/JuliaLang/julia/issues/5840#issuecomment-237077416)

---

<div class="post-metadata">

**Author:** ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)\
**Post date:** [June 5, 2017, 3:02pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/7 "2017-06-05T15:02:45Z")

</div>

Thanks. Sorry for being unclear. Yes, I meant using exp for matrix exponentiation

---

<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:** [June 5, 2017, 3:16pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/8 "2017-06-05T15:16:04Z")

</div>

> [@kristoffer.carlsson](#):
>
> On the other hand, it might enable generic code that will both work with matrices and scalars?

Yes, exponential integrators, integration factor methods, and exponential time differencing methods would be nicer to write with this. You can get around this with a simple `if <: AbstractArray`, but if `exp` “just worked” then the code would be much nicer. So there’s at least a use case in DiffEqs (that I ran into quite recently).

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 5, 2017, 8:56pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/9 "2017-06-05T20:56:42Z")

</div>

It sounds tempting, but then what about fast vectorised versions of `exp` etc, see e.g. [AppleAccelerate.jl](https://github.com/JuliaMath/AppleAccelerate.jl)

Also note that `expm` and `sqrtm` can be applied to scalars, so the canonical way to write exponential integrators would probably be to use `expm` from the start?

---

<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:** [June 5, 2017, 9:03pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/10 "2017-06-05T21:03:35Z")

</div>

> [@cortner](#):
>
> It sounds tempting, but then what about fast vectorised versions of exp etc, see e.g. AppleAccelerate.jl

They can overload `broadcast` and `broadcast!`

---

<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:** [June 5, 2017, 9:04pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/11 "2017-06-05T21:04:59Z")

</div>

> [@cortner](#):
>
> Also note that expm and sqrtm can be applied to scalars, so the canonical way to write exponential integrators would probably be to use expm from the start?

I did not know that… you make a great point sir.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 5, 2017, 9:12pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/12 "2017-06-05T21:12:53Z")

</div>

Interesting- then maybe this could be a nice (if not crucial) change.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [June 6, 2017, 10:28am UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/13 "2017-06-06T10:28:29Z")

</div>

I think all these types of functions should work on matrices, on the guiding principal that if a Taylor (or similar) expansion works for both `Number` and square `Matrix`, then why _wouldn’t_ the same function apply to both?? Julia is meant to support generic mathematical programming, after all…

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [June 7, 2017, 9:22pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/14 "2017-06-07T21:22:06Z")

</div>

But then `expm` is merely a superset of `exp`?

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 7, 2017, 9:35pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/15 "2017-06-07T21:35:29Z")

</div>

Not really. Exp applied to a matrix is not the same as expm.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [June 7, 2017, 9:44pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/16 "2017-06-07T21:44:54Z")

</div>

@cortner If the element-wise `exp([1., 2.])` becomes deprecated, then `expm` will be a superset of `exp`.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 7, 2017, 10:05pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/17 "2017-06-07T22:05:16Z")

</div>

I guess you are right then - I am still thinking 0.5; maybe another argument in favour deprecating `expm`

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [June 8, 2017, 12:36pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/18 "2017-06-08T12:36:52Z")

</div>

IMHO it would be confusing for new users. People will frequently expect `exp(::AbstractMatrix)` to apply `exp` to each entry in the matrix. Better keep separate functions.

---

<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 8, 2017, 2:24pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/19 "2017-06-08T14:24:40Z")

</div>

At least while only a subset of functions have a “proper” matrix version, distinguishing the latter is a reasonable alternative, even if it comes at the cost of elegance.

Conversely, `expm` should only be merged to `exp` when `cos(::AbstractMatrix)` and similar throw an error.

---

<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:** [June 8, 2017, 2:41pm UTC](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092/20 "2017-06-08T14:41:57Z")

</div>

So 0.7 then (when the deprecations are removed)?

[Next page](https://discourse.julialang.org/t/status-of-exp-and-sqrt/4092.md?page=2)
