refactor the matrix code out of lib.rs