inlined matrix operations for speed