Memory efficient, on the fly computation of scalar matrix