# How to automatically produce this kind of matrix in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-automatically-produce-this-kind-of-matrix-in-julia/127948>\
**Category:** General Usage\
**Created:** [April 10, 2025, 8:08pm UTC](https://discourse.julialang.org/t/how-to-automatically-produce-this-kind-of-matrix-in-julia/127948 "2025-04-10T20:08:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 10, 2025, 8:08pm UTC](https://discourse.julialang.org/t/how-to-automatically-produce-this-kind-of-matrix-in-julia/127948/1 "2025-04-10T20:08:46Z")

</div>

Hello!

I am unsure of how to make a proper MWE, so I will explain what I would like to achieve, which is the automatic construction of this matrix:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/8/d882206aa29edc1ee21dc33308fc62b0f000e03a.png) [Source: [https://dual.sphysics.org/7thworkshop/workshop/program/7DSW\_mDBC.pdf](https://dual.sphysics.org/7thworkshop/workshop/program/7DSW_mDBC.pdf)]

My code needs to adapt, in the sense that if it is a 2D case, then the last row and column from the matrix A is neglected. Currently what I do is using `StaticArrays`, `SMatrix` functionality, and construct column per column the matrix as shown below.

I have had to use an `if` statement, I wonder if I could do this without an `if` statement in a smart way?

Kind regards

EDIT: Code below is just to explain how I am handling it right now

```julia
                # Filling the Aᵧ matrix is done in column-major order
                xⱼᵢ = -xᵢⱼ

                if Dimensions == 2
                    Aᵧ[i] += SMatrix{DimensionsPlus, DimensionsPlus, FloatType, DimensionsPlus*DimensionsPlus}(
                        # First row
                        VⱼWᵢⱼ, (Vⱼ * ∇ᵢWᵢⱼ)...,
                        # Second row
                        xⱼᵢ[1] * VⱼWᵢⱼ, (xⱼᵢ[1] * Vⱼ * ∇ᵢWᵢⱼ)...,
                        # Third row
                        xⱼᵢ[2] * VⱼWᵢⱼ, (xⱼᵢ[2] * Vⱼ * ∇ᵢWᵢⱼ)...,
                    )
                elseif Dimensions == 3
                    Aᵧ[i] += SMatrix{DimensionsPlus, DimensionsPlus, FloatType, DimensionsPlus*DimensionsPlus}(
                        # First row
                        VⱼWᵢⱼ, (Vⱼ * ∇ᵢWᵢⱼ)...,
                        # Second row
                        xⱼᵢ[1] * VⱼWᵢⱼ, (xⱼᵢ[1] * Vⱼ * ∇ᵢWᵢⱼ)...,
                        # Third row
                        xⱼᵢ[2] * VⱼWᵢⱼ, (xⱼᵢ[2] * Vⱼ * ∇ᵢWᵢⱼ)...,
                        # Fourth row
                        xⱼᵢ[3] * VⱼWᵢⱼ, (xⱼᵢ[3] * Vⱼ * ∇ᵢWᵢⱼ)...,
                    )

```

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [April 11, 2025, 7:03am UTC](https://discourse.julialang.org/t/how-to-automatically-produce-this-kind-of-matrix-in-julia/127948/2 "2025-04-11T07:03:30Z")

</div>

Isn’t this matrix the product A'B of two matrices A and B, where the different values `j` go on the rows of the matrices and A contains rows W\_{gj}, \partial\_xW\_{gj}, \partial\_yW\_{gj},\partial\_zW\_{gj}, so it is the `hcat` of a 1 column matrix (W\_{gj})\_j and a derivative matrix (\partial\_i W\_{gj})\_{j, i \in \{x, y, z\}}? This then works nicely as soon as you can get the derivative matrix automatically? Similarly, the matrix B has rows V\_j, (x\_j-x\_g)V\_j,\ldots. Would that help?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 11, 2025, 1:15pm UTC](https://discourse.julialang.org/t/how-to-automatically-produce-this-kind-of-matrix-in-julia/127948/3 "2025-04-11T13:15:00Z")

</div>

Thanks a lot!

I managed to do it like this:

```julia
                # Filling the Aᵧ matrix is done in column-major order
                xⱼᵢ = -xᵢⱼ
                first_column = [VⱼWᵢⱼ; Vⱼ * ∇ᵢWᵢⱼ]
                Aᵧ[i] += SMatrix{DimensionsPlus, DimensionsPlus, FloatType, DimensionsPlus*DimensionsPlus}(
                    first_column...,
                    ((xⱼᵢ * first_column')')...
                )

```

Kind regards
