Change way dense and spare matrix are constructed