# Using ForwardDiff.jl with BlockDiagonals.jl

**URL:** <https://discourse.julialang.org/t/using-forwarddiff-jl-with-blockdiagonals-jl/112396>\
**Category:** General Usage\
**Created:** [April 2, 2024, 2:29am UTC](https://discourse.julialang.org/t/using-forwarddiff-jl-with-blockdiagonals-jl/112396 "2024-04-02T02:29:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mizu49](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mizu49/32/46338_2.png) [@Mizu49](https://discourse.julialang.org/u/Mizu49)\
**Post date:** [April 2, 2024, 2:29am UTC](https://discourse.julialang.org/t/using-forwarddiff-jl-with-blockdiagonals-jl/112396/1 "2024-04-02T02:29:13Z")

</div>

I want to calcuate the jacobian for a vector calculated with the following function.

```julia
function test(A1, A2, x)

    A = BlockDiagonal([A1 * x, A2])
    return A * vcat(x, x)
    
end

```

However, when I calculate the jacobian with `ForwardDiff.jacobian`, I get an error.

```julia
J = ForwardDiff.jacobian((x) -> test(A1, A2, x), x)

```

```julia
ERROR: MethodError: no method matching BlockDiagonal(::Vector{Array})

```

How can I resolve this error?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [April 2, 2024, 4:58am UTC](https://discourse.julialang.org/t/using-forwarddiff-jl-with-blockdiagonals-jl/112396/2 "2024-04-02T04:58:17Z")

</div>

You don’t gave a runnable example, so I have to guess.

Does your method work if you just call it directly `test(A1, A2, x)`? To me it looks like you erroneously multiply matrix `A1` by vector `x` in the construction of the blovk-diagonal matrix `A`. This would explain the error as a `[vector, matrix]` ist of type `Vector{Array}` and indeed you need a `Vector{Matrix}` to construct a `BlockDiagonal`.
