enyan's reduce matrix functions