use sparse matrix to accelerate SA
SA is inlined in extract (temporary solution...)
improved cost function