# Differentiation matrix stack overflow

**URL:** https://discourse.julialang.org/t/differentiation-matrix-stack-overflow/68944
**Category:** Numerics
**Tags:** linearalgebra
**Created:** [September 29, 2021, 4:31pm UTC](https://discourse.julialang.org/t/differentiation-matrix-stack-overflow/68944 "2021-09-29T16:31:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![GianpieroPascale](https://avatars.discourse-cdn.com/v4/letter/g/e99b99/32.png) [@GianpieroPascale](https://discourse.julialang.org/u/GianpieroPascale)
#### Post date: [September 29, 2021, 4:31pm UTC](https://discourse.julialang.org/t/differentiation-matrix-stack-overflow/68944/1 "2021-09-29T16:31:05Z")

</div>

I’m trying to follow along with @stevengj’s [lecture notes](http://math.mit.edu/~stevenj/18.303/lecture-10.html) on 2D differentiation matrices, but I can’t get past the first example without Julia reporting a stack-overflow error. The piece of code in question is

```julia
# construct the (M+1)xM matrix D, not including the 1/dx factor
diff1(M) = [[1.0 zeros(1,M-1)]; diagm(ones(M-1),1) - eye(M) ]
diff1(5)

```

which on my machine results in

```julia
ERROR: StackOverflowError:
Stacktrace:
 [1] diagm(::Vector{Float64}, ::Int64) (repeats 79984 times)
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/8vlYQ/src/tangent_types/thunks.jl:66

```

Any ideas what could be the problem here? I’m using the 1.6.3 generic binary for 64-bit Linux.

---

<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: [September 29, 2021, 4:38pm UTC](https://discourse.julialang.org/t/differentiation-matrix-stack-overflow/68944/2 "2021-09-29T16:38:42Z")

</div>

Those notes are from quite an old version of Julia (0.4) — that’s not how the `diagm` function is called anymore. An updated version of that function would be simply:

```julia
using LinearAlgebra
diff1(M) = let o = ones(M); diagm(M+1, M, 0=>o, -1=>-o); end

```

(And here is a [sparse version](https://github.com/mitmath/18335/blob/spring21/notes/Nested-Dissection.ipynb) using `spdiagm`.)

---

<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: [September 29, 2021, 4:44pm UTC](https://discourse.julialang.org/t/differentiation-matrix-stack-overflow/68944/3 "2021-09-29T16:44:39Z")

</div>

A separate problem is that ChainRulesCore is engaging in [“type piracy”](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy) of the `diagm` function [here](https://github.com/JuliaDiff/ChainRulesCore.jl/blob/486922ddb2867c8f8ec7cb3d1c9efbe43c856ef0/src/tangent_types/thunks.jl#L62-L67), which is why you are getting a mysterious error message (instead of a `MethodError`, which would be more sensible) here. This should be fixed in ChainRulesCore.jl: [https://github.com/JuliaDiff/ChainRulesCore.jl/issues/472](https://github.com/JuliaDiff/ChainRulesCore.jl/issues/472)

---

<div class="post-metadata">

### Author: ![GianpieroPascale](https://avatars.discourse-cdn.com/v4/letter/g/e99b99/32.png) [@GianpieroPascale](https://discourse.julialang.org/u/GianpieroPascale)
#### Post date: [September 29, 2021, 5:14pm UTC](https://discourse.julialang.org/t/differentiation-matrix-stack-overflow/68944/4 "2021-09-29T17:14:40Z")

</div>

Thanks for the help, and for making these notes available!
