# Seemingly unnecessary allocation within for loop

**URL:** https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556
**Category:** Performance
**Tags:** memory-allocation, loops, cartesianindices
**Created:** [December 4, 2021, 12:20am UTC](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556 "2021-12-04T00:20:09Z")
**Posts on this page:** 1
**Showing post:** 7

<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 4, 2021, 2:03pm UTC](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556/7 "2021-12-04T14:03:11Z")

</div>

> [@mjg](#):
>
> I’m trying to create a simulation of an elastic string generalized to an arbitrary number of dimensions.

Also, your equations look wrong to me. It seems like you are trying to model a scalar wave equation \ddot{x} = \nabla^2 x, but your equations are not quite a discretization of this equation. For example, the first term in the first equation should be multiplied by twice the number of dimensions.

In general, I would look into discretization schemes for hyperbolic equations (e.g. the leapfrog scheme I mentioned), because there are lots of ways to go wrong here. Or you could use DifferentialEquations.jl to handle the time integration (i.e. “method of lines”).

Note that a useful technique is to use “ghost cells” to handle boundary conditions, as described here: [Finite difference Laplacian with five-point stencil - #2 by stevengj](https://discourse.julialang.org/t/finite-difference-laplacian-with-five-point-stencil/25014/2)

---

_[View the full topic](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556)._
