# Package to avoid allocations in some functions when using StaticArrays.jl

**URL:** <https://discourse.julialang.org/t/package-to-avoid-allocations-in-some-functions-when-using-staticarrays-jl/114539>\
**Category:** Performance\
**Tags:** package, memory-allocation\
**Created:** [May 22, 2024, 12:48am UTC](https://discourse.julialang.org/t/package-to-avoid-allocations-in-some-functions-when-using-staticarrays-jl/114539 "2024-05-22T00:48:06Z")\
**Posts on this page:** 1\
**Showing post:** 8

<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:** [May 22, 2024, 7:16pm UTC](https://discourse.julialang.org/t/package-to-avoid-allocations-in-some-functions-when-using-staticarrays-jl/114539/8 "2024-05-22T19:16:17Z")

</div>

> [@Ronis\_BR](#):
>
> However, for embedded systems that must work continuously, it is much safe to use the pseudo-inverse `pinv` because the numerical errors can lead to singular matrices (we are using `Float32`), especially when the KF has converged and the covariance is small.

Have you considered a Tikhonov-regularized Kalman filter? There seem to be several published variants of this notion, but my guess (caveat: without reading the papers) is that they are all conceptually centered on replacing a truncated pseudo-inverse A^+ (where you drop singular values smaller than \sigma\_0, i.e. “truncate” the SVD) with a Tikhonov regularization (A^T A + \sigma\_0^2 I)^{-1} A^T.

Alternatively, you can compute an analogue of your pseudo-inverse with a “truncated QR” (column-pivoted QR where you simply drop columns where the diagonal elements of R become too small). Then you are replacing _one_ SVD with _one_ QR factorization, which should definitely be faster (even if it is [theoretically less reliable](https://discourse.julialang.org/t/how-to-find-the-linearly-independent-columns-rows-of-a-matrix/109772/25) than SVD).

(Note that in any of these cases, you may not need to compute any matrix inverse explicitly — my recollection is that you just need to multiply it by another matrix in the Kalman algorithm, which can usually be done more quickly than explicitly forming the inverse and then multiplying it.)

---

_[View the full topic](https://discourse.julialang.org/t/package-to-avoid-allocations-in-some-functions-when-using-staticarrays-jl/114539)._
