That's quite a neat trick. You do need to be careful if the same matrix appears more than once in your einsum call though.