# Tricks to compile away step in calculation?

**URL:** <https://discourse.julialang.org/t/tricks-to-compile-away-step-in-calculation/105760>\
**Category:** General Usage\
**Created:** [November 3, 2023, 2:55pm UTC](https://discourse.julialang.org/t/tricks-to-compile-away-step-in-calculation/105760 "2023-11-03T14:55:12Z")\
**Posts on this page:** 1\
**Showing post:** 20

<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:** [November 8, 2023, 9:53pm UTC](https://discourse.julialang.org/t/tricks-to-compile-away-step-in-calculation/105760/20 "2023-11-08T21:53:04Z")

</div>

> [@croberts](#):
>
> `my_pinv(X) = (X'X)^-1 * X' # yes I know pinv is faster`

Aside: No, `pinv` is considerably slower than this, but it is more accurate. See [Efficient way of doing linear regression - #33 by stevengj](https://discourse.julialang.org/t/efficient-way-of-doing-linear-regression/31232/33)

> [@croberts](#):
>
> You know what, maybe a general solution could be produced using metaprogramming.

I think you’re making life complicated for yourself. Just compute the pseudo-inverse (or whatever) once and store it in a variable to re-use it. The code will be a lot clearer and simpler than trying for some magic syntax.

Expecting the compiler to figure out that a complicated calculation like this can be re-used is unrealistic and likely to be fragile even if it worked in some cases — to do an optimization like this, the compiler needs to be able to _prove_ that it can never change any results, which is more delicate than you seem to realize. This is true in any language, not just Julia.

---

_[View the full topic](https://discourse.julialang.org/t/tricks-to-compile-away-step-in-calculation/105760)._
