# Zygote.jl: DimensionMismatch: matrix is not square error

**URL:** https://discourse.julialang.org/t/zygote-jl-dimensionmismatch-matrix-is-not-square-error/58839
**Category:** General Usage
**Tags:** question, package, zygote
**Created:** [April 8, 2021, 2:46pm UTC](https://discourse.julialang.org/t/zygote-jl-dimensionmismatch-matrix-is-not-square-error/58839 "2021-04-08T14:46:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Richard-Li](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Richard-Li](https://discourse.julialang.org/u/Richard-Li)
#### Post date: [April 8, 2021, 2:46pm UTC](https://discourse.julialang.org/t/zygote-jl-dimensionmismatch-matrix-is-not-square-error/58839/1 "2021-04-08T14:46:38Z")

</div>

Hi, I am using Zygote.jl. I’d like to change a matrix from 3x3 to 5x3 like this MWE:

```julia
using Zygote
using LinearAlgebra

function test( c )
    m = rand(3,3) * c
    @show m 
    M = Matrix(1.0I, 5, 3) * m
    @show M 
    return sum(M)
end 

gradient(test, 0.5)

```

Then I get

```julia
ERROR: LoadError: DimensionMismatch("matrix is not square: dimensions are (5, 3)")
Stacktrace:
 [1] checksquare at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/LinearAlgebra.jl:223 [inlined]
 [2] tr at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/dense.jl:331 [inlined]
 [3] #827 at /root/.julia/packages/Zygote/CgsVi/src/lib/array.jl:686 [inlined]
 [4] #3238#back at /root/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59 [inlined]
 [5] test at /root/codes/test_zygote/test_chi.jl:7 [inlined]
 [6] (::typeof(∂(test)))(::Float64) at /root/.julia/packages/Zygote/CgsVi/src/compiler/interface2.jl:0
 [7] (::Zygote.var"#41#42"{typeof(∂(test))})(::Float64) at /root/.julia/packages/Zygote/CgsVi/src/compiler/interface.jl:41
 [8] gradient(::Function, ::Float64) at /root/.julia/packages/Zygote/CgsVi/src/compiler/interface.jl:59
 [9] top-level scope at /root/codes/test_zygote/test_chi.jl:12
in expression starting at /root/codes/test_zygote/test_chi.jl:12

```

Is Zygote.jl currently not supporting differentiation through non-square matrix multiplication?

My Zygote version is v0.6.8, and julia version is 1.5.3.

Thanks for any reply.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [April 8, 2021, 3:02pm UTC](https://discourse.julialang.org/t/zygote-jl-dimensionmismatch-matrix-is-not-square-error/58839/2 "2021-04-08T15:02:46Z")

</div>

The bug is in `gradient(c -> sum(Matrix(c*I, 5, 3)), 1)`, because that tries to compute `tr(Matrix(I, 5, 3))`.

But you don’t need this here, you can construct `const m53 = Matrix(1.0I, 5, 3)` once outside the gradient call.

---

<div class="post-metadata">

### Author: ![Richard-Li](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Richard-Li](https://discourse.julialang.org/u/Richard-Li)
#### Post date: [April 8, 2021, 3:27pm UTC](https://discourse.julialang.org/t/zygote-jl-dimensionmismatch-matrix-is-not-square-error/58839/3 "2021-04-08T15:27:55Z")

</div>

Thanks a lot! @mcabbott  
I can use `@ignore` to tell Zygote to ignore the gradient of this non-square matrix. This is the working version:

```julia
using Zygote
using LinearAlgebra

function test( c )
    m = rand(3,3) * c
    @show m 

    M = Zygote.@ignore Matrix(1.0I, 5, 3)
    M = M * m
    @show M 
    return sum(M)
end 

gradient(test, 0.5)

```
