Let us now consider a uniform (m+2)×(n+2) grid with spacing h in both the x and y directions.
We set xi=ih for i=0,1,…,m+1 and yj=jh for j=0,1,…,n+1.
We will assume that u is zero on the boundary of the domain, i.e.,
u(x0,⋅)=u(xm+1,⋅)=u(⋅,y0)=u(⋅,yn+1)=0.
Now let D∈Rm×n be a matrix where Di,j is the finite difference approximation
of Δu(xi,yj), i.e.,
We now let U∈Rm×n be the matrix with entries Ui,j=u(xi,yj)
and we seek an efficient way to compute D from U.
One way to do this is to see that D can come from U by sweeping through the grid and applying a
five-point stencil to each point:
But how to do this efficiently using matrix operations?
We definitely want to avoid looping over the grid ourselves.
Instead, we can split the five-point stencil into two parts, one horizontal and one vertical:
Note how the first term, LmU, applies the three-point stencil to each row of U
and that the second term, ULnT, applies the stencil to each column of U
(transposing Ln is strictly not necessary here as Ln is symmetric).
Note also how L contains 3 non-zero values per row (except for the first and last rows),
so assuming that it is represented in a sparse
(compressed row)
format, the matrix-matrix product LmU requires roughly 6mn floating point operations
(3 multiplications and 3 additions per output entry) and likewise for ULnT.
That is a total of 12mn operations to compute D from U.
Another approach is to use the identities related to the vec and
Kronecker product operators as seen in an
earlier post.
Using these, we get
where ⊕ denotes the Kronecker sum
(available in SciPy as kronsum).
It contains (mostly) 5 non-zero values for each of its mn rows, so the matrix-vector product
Lm×nvec(U) requires roughly 10mn floating point operations.
Note that the approach of splitting the five-point stencil into two parts can also be done
using a two-point stencil 1⋄0⋄1 in one direction and 1⋄−4⋄1
in the other.
This will reduce the number of floating point operations down to 4mn+6mn=10mn,
just like the Kronecker sum approach.
Feel free to leave any question, correction or comment in this
Mastodon thread.