# Adjointing around Mutating Part

**URL:** https://discourse.julialang.org/t/adjointing-around-mutating-part/46582
**Category:** Machine Learning
**Tags:** zygote
**Created:** [September 14, 2020, 11:42am UTC](https://discourse.julialang.org/t/adjointing-around-mutating-part/46582 "2020-09-14T11:42:38Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![rakeshvar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakeshvar/32/3613_2.png) [@rakeshvar](https://discourse.julialang.org/u/rakeshvar)
#### Post date: [September 14, 2020, 11:42am UTC](https://discourse.julialang.org/t/adjointing-around-mutating-part/46582/1 "2020-09-14T11:42:38Z")

</div>

I have the following toy problem that imitates my more complex problem. (A follow up from [this question](https://discourse.julialang.org/t/flux-zygote-gradient-understanding-mutating-arrays-is-not-supported/46324).)

```julia
function triangulate(x::Matrix{T}) where T
    m, n = size(x)
    a = x[:,1] .* [1; zeros(T, m-1)]
    for i in 2:n
        a = accumulate(+, a) .* [x[1:i, i]; zeros(T, m-i)] # Offending Line
    end
    a
end

x1 = (1:4).*(1:4)'
gradient(x->triangulate(x)[end], x1)[1]    

```

This throws `Error: Mutating arrays is not supported.` I was wondering since the offending line is obvious in the above code. How can I extract that line as a method and write the custom adjoint for myself. How would the API for that look like?

This would save me from writing gradient for the entire function, to just writing it for that one line. Right?

Thanks.
