# Matrix Free Differentiation

**URL:** <https://discourse.julialang.org/t/matrix-free-differentiation/107134>\
**Category:** General Usage\
**Tags:** cartesianindices\
**Created:** [December 4, 2023, 10:40pm UTC](https://discourse.julialang.org/t/matrix-free-differentiation/107134 "2023-12-04T22:40:51Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 5, 2023, 12:23am UTC](https://discourse.julialang.org/t/matrix-free-differentiation/107134/3 "2023-12-05T00:23:06Z")

</div>

> [@stillyslalom](#):
>
> Your computational kernel should specialize on the dimensionality and order of your operators, and that specialization will be made easier if you use [static arrays](https://github.com/JuliaArrays/StaticArrays.jl) for your operators, and pass those in as arguments as well.

Alternatively, see this post for an example of a fast dimension-agnostic matrix-free finite-difference stencil: [Seemingly unnecessary allocation within for loop - #8 by stevengj](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556/8)

Note that, as mentioned in this post, you are typically better off using ghost cells / padding to implement your boundary conditions than putting `if` statements inside critical inner loops. And for something performance-critical like the difference operation, you really benefit from hard-coding/unrolling it.

See also some related discussions:

- [Automatic function generation for finite-difference stencils using metaprogramming](https://discourse.julialang.org/t/automatic-function-generation-for-finite-difference-stencils-using-metaprogramming/88928)
- [[ANN] Stencils.jl for fast small/direct stencils on CPU/GPU](https://discourse.julialang.org/t/ann-stencils-jl-for-fast-small-direct-stencils-on-cpu-gpu/106579)
- [Generating finite-difference stencils](https://discourse.julialang.org/t/generating-finite-difference-stencils/85876)
- [Code Optimization for Differential Equations · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/faster_ode_example/)

---

_[View the full topic](https://discourse.julialang.org/t/matrix-free-differentiation/107134)._
