add documentation
reduce memory usage by avoiding matrix copying