Matrix multiplication with optimizations