# I am confused using tanh(M)

**URL:** https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247
**Category:** New to Julia
**Created:** [December 13, 2019, 8:02pm UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247 "2019-12-13T20:02:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![floriano](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@floriano](https://discourse.julialang.org/u/floriano)
#### Post date: [December 13, 2019, 8:02pm UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247/1 "2019-12-13T20:02:52Z")

</div>

Hello,  
I am pritty new to Julia and try to find my first steps. I am a bit confused now.  
In Python I had this (simplyfied) piece of code:

```julia
import numpy as np
a = [[1,2], [3,4]]
M = np.array(a)
np.tanh(M)

```

It brings me the expected result:

```julia
array([[0.76159416, 0.96402758],
           [0.99505475, 0.9993293]])

```

Now I try my luck with Julia:

```julia
M = [1 2
3 4]
tanh(M)

```

Output:

```julia
2×2 Array{Float64,2}:
 -0.0320733 0.472079
  0.708118 0.676045

```

My question, what is calculated here?

However if I do:

```julia
tanh.(M)

```

I get the expected result again. I think I understand concept of “.” as the elementwise operator. But I thought tanh() and similar are always elementwise. If I am wrong, what is tanh(M) without “.” calculating?  
Thanks  
Floriano

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [December 13, 2019, 8:19pm UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247/2 "2019-12-13T20:19:35Z")

</div>

In julia, any operation on a matrix will not be element-wise unless you have a `.`. It turns out that for square matrices, you can take a hyperbolic tangent of a matrix. That’s the result you are seeing when you type `tanh(M)`.

---

<div class="post-metadata">

### Author: ![floriano](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@floriano](https://discourse.julialang.org/u/floriano)
#### Post date: [December 13, 2019, 8:20pm UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247/3 "2019-12-13T20:20:56Z")

</div>

thank you

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [December 21, 2019, 4:51pm UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247/5 "2019-12-21T16:51:33Z")

</div>

```julia
julia> tanh(1+2im)
1.1667362572409197 - 0.24345820118572525im

julia> tanh([1 -2; 2 1])
2×2 Array{Float64,2}:
  1.16674 0.243458
 -0.243458 1.16674 

```

---

<div class="post-metadata">

### Author: ![jonasaugust](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasaugust/32/11942_2.png) [@jonasaugust](https://discourse.julialang.org/u/jonasaugust)
#### Post date: [December 21, 2019, 5:34pm UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247/6 "2019-12-21T17:34:29Z")

</div>

As Oscar\_Smith said, the component-wise `tanh` and (diagonalizable, square) matrix `tanh` are different in general. The matrix `tanh` is derived from a power series expansion of the scalar tanh, where the scalar powers are replaced with matrix powers. It can be computed by taking the eigenvalue decomposition `U*diagm(evals)*U'` of the matrix, taking the component-wise `tanh` on the eigenvalues, and then reconstructing:

```julia
julia> A = [1 -2; 2 1]
2×2 Array{Int64,2}:
 1 -2
 2 1

julia> using LinearAlgebra

julia> evals, U = eigen(A)
Eigen{Complex{Float64},Complex{Float64},Array{Complex{Float64},2},Array{Complex{Float64},1}}
eigenvalues:
2-element Array{Complex{Float64},1}:
 1.0 - 2.0000000000000004im
 1.0 + 2.0000000000000004im
eigenvectors:
2×2 Array{Complex{Float64},2}:
       0.0+0.707107im 0.0-0.707107im
 -0.707107-0.0im -0.707107+0.0im     

julia> U*diagm(evals)*U'-A
2×2 Array{Complex{Float64},2}:
 -2.22045e-16+0.0im -4.44089e-16+0.0im
  4.44089e-16+0.0im 2.22045e-16+0.0im

julia> U*diagm(tanh.(evals))*U'-tanh(A)
2×2 Array{Complex{Float64},2}:
 -4.44089e-16+0.0im 2.22045e-16+0.0im
   -2.498e-16+0.0im 0.0+0.0im

julia> tanh.(A)-tanh(A)
2×2 Array{Float64,2}:
 -0.405142 -1.20749 
  1.20749 -0.405142

```

The last line gives an example where the component-wise and matrix versions of `tanh` differ.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 21, 2019, 6:14pm UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247/7 "2019-12-21T18:14:47Z")

</div>

> [@jonasaugust](#):
>
> It can be computed by taking the eigenvalue decomposition `U*diagm(evals)*U'` of the matrix, taking the component-wise `tanh` on the eigenvalues, and then reconstructing:

Provided that the matrix is diagonalizable, of course.

---

<div class="post-metadata">

### Author: ![jonasaugust](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasaugust/32/11942_2.png) [@jonasaugust](https://discourse.julialang.org/u/jonasaugust)
#### Post date: [December 22, 2019, 10:56am UTC](https://discourse.julialang.org/t/i-am-confused-using-tanh-m/32247/8 "2019-12-22T10:56:57Z")

</div>

Yes, thanks for reminding us that the power series derivation of `tanh(A)` requires diagonalizability of `A`. But my demo used the decomposition `A=U*diagm(evals)*U'`, so we need to further assume that `A` is unitarily diagonalizable for that to work, i.e., the transpose `U'` is also the inverse of `U`, which occurs when `A` is normal (`A*A' == A'*A`). It’s sufficient that `A` is (real) symmetric for normality to hold.
