A User-Friendly Hybrid Sparse Matrix Class in C++

  • Conrad SandersonEmail author
  • Ryan Curtin
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 10931)


When implementing functionality which requires sparse matrices, there are numerous storage formats to choose from, each with advantages and disadvantages. To achieve good performance, several formats may need to be used in one program, requiring explicit selection and conversion between the formats. This can be both tedious and error-prone, especially for non-expert users. Motivated by this issue, we present a user-friendly sparse matrix class for the C++ language, with a high-level application programming interface deliberately similar to the widely used MATLAB language. The class internally uses two main approaches to achieve efficient execution: (i) a hybrid storage framework, which automatically and seamlessly switches between three underlying storage formats (compressed sparse column, coordinate list, Red-Black tree) depending on which format is best suited for specific operations, and (ii) template-based meta-programming to automatically detect and optimise execution of common expression patterns. To facilitate relatively quick conversion of research code into production environments, the class and its associated functions provide a suite of essential sparse linear algebra functionality (eg., arithmetic operations, submatrix manipulation) as well as high-level functions for sparse eigendecompositions and linear equation solvers. The latter are achieved by providing easy-to-use abstractions of the low-level ARPACK and SuperLU libraries. The source code is open and provided under the permissive Apache 2.0 license, allowing unencumbered use in commercial products.


Numerical linear algebra Sparse matrix C++ language 


  1. 1.
    Anderson, E., Bai, Z., Bischof, C., Blackford, S., Demmel, J., Dongarra, J., Du Croz, J., Greenbaum, A., Hammarling, S., et al.: LAPACK Users’ Guide. SIAM, Philadelphia (1999)CrossRefGoogle Scholar
  2. 2.
    Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms, 3rd edn. MIT Press, Cambridge (2009)zbMATHGoogle Scholar
  3. 3.
    Curtin, R., Cline, J., Slagle, N., March, W., Ram, P., Mehta, N., Gray, A.: MLPACK: a scalable C++ machine learning library. J. Mach. Learn. Res. 14, 801–805 (2013)MathSciNetzbMATHGoogle Scholar
  4. 4.
    Duff, I.S., Erisman, A.M., Reid, J.K.: Direct Methods for Sparse Matrices, 2nd edn. Oxford University Press, Oxford (2017)CrossRefGoogle Scholar
  5. 5.
    Eaton, J.W., Bateman, D., Hauberg, S., Wehbring, R.: GNU Octave 4.2 Reference Manual. Samurai Media Limited (2017)Google Scholar
  6. 6.
    Eddelbuettel, D., Sanderson, C.: RcppArmadillo: accelerating R with high-performance C++ linear algebra. Comput. Stat. Data Anal. 71, 1054–1063 (2014)MathSciNetCrossRefGoogle Scholar
  7. 7.
    Lehoucq, R.B., Sorensen, D.C., Yang, C.: ARPACK Users’ Guide: Solution of Large-Scale Eigenvalue Problems with Implicitly Restarted Arnoldi Methods. SIAM, Philadelphia (1998)CrossRefGoogle Scholar
  8. 8.
    Li, X.S.: An overview of SuperLU: algorithms, implementation, and user interface. ACM Trans. Mathe. Softw. (TOMS) 31(3), 302–325 (2005)MathSciNetCrossRefGoogle Scholar
  9. 9.
    MathWorks: MATLAB Documentation - Accessing Sparse Matrices (2018).
  10. 10.
    Nunez-Iglesias, J., van der Walt, S., Dashnow, H.: Elegant SciPy: The Art of Scientific Python. O’Reilly Media (2017)Google Scholar
  11. 11.
    Rosen, L.: Open Source Licensing. Prentice Hall, Upper Saddle River (2004)Google Scholar
  12. 12.
    Saad, Y.: SPARSKIT: A basic tool kit for sparse matrix computations. Technical report, NASA-CR-185876, NASA Ames Research Center (1990)Google Scholar
  13. 13.
    Sanderson, C., Curtin, R.: Armadillo: a template-based C++ library for linear algebra. J. Open Source Softw. 1, 26 (2016)CrossRefGoogle Scholar
  14. 14.
    Stroustrup, B.: The C++ Programming Language, 4th edn. Addison-Wesley, Boston (2013)zbMATHGoogle Scholar
  15. 15.
    Vandevoorde, D., Josuttis, N.M.: C++ Templates: The Complete Guide, 2nd edn. Addison-Wesley, Boston (2017)Google Scholar

Copyright information

© Springer International Publishing AG, part of Springer Nature 2018

Authors and Affiliations

  1. 1.Data61, CSIROBrisbaneAustralia
  2. 2.Symantec CorporationAtlantaUSA
  3. 3.University of QueenslandBrisbaneAustralia
  4. 4.Arroyo ConsortiumSan FranciscoUSA

Personalised recommendations