my personal solution, with checks for x to be square matrix and checks for x changed since last
calculation of the inverse