Is it better to compute $A^tA$ once and then $Ax$ several times or compute $y=Ax$ and then $A^ty$ every time?

50 Views Asked by At

So I have this algorithm which given a matrix $A$ it assigns $A=A^tA$ outside the loop and then on the algorithm loop it solves multiple instances of $Ax$ for different $x$s, (meaning that it's actually computing $A^tAx$, because of the initial assignment)

However my professor told me that the computation $A^tA$ may be expensive and that I should aim to do $y=Ax$ and solve $A^ty$ inside the loop, however it would turn into two systems for each time I wanted to solve it for $x$ instead of just one.

The way I was doing before I computed the matrix $A^tA$ just once and then solved only one system for each instance of $x$ on the loop.

My question is, is $A^tA$ that expensive for doubling the amount of systems to make-up for it? Or is there a way to know when it is expensive, so that I put an if on the beginning of the algorithm and then solve it computing $A^tA$ when it's cheap and solve the other way when it's not?