Universal matrix implementation