Removing code duplication by proper templating&specialization.
Merge pull request #1525 from tklein23/fix_sparse_templates

Removing code duplication by proper templating&specialization.