wrote matrix functions