fn_speye.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup fn_speye
  16. //! @{
  17. //! Generate a sparse matrix with the values along the main diagonal set to one
  18. template<typename obj_type>
  19. arma_warn_unused
  20. inline
  21. obj_type
  22. speye(const uword n_rows, const uword n_cols, const typename arma_SpMat_SpCol_SpRow_only<obj_type>::result* junk = NULL)
  23. {
  24. arma_extra_debug_sigprint();
  25. arma_ignore(junk);
  26. if(is_SpCol<obj_type>::value)
  27. {
  28. arma_debug_check( (n_cols != 1), "speye(): incompatible size" );
  29. }
  30. else
  31. if(is_SpRow<obj_type>::value)
  32. {
  33. arma_debug_check( (n_rows != 1), "speye(): incompatible size" );
  34. }
  35. obj_type out;
  36. out.eye(n_rows, n_cols);
  37. return out;
  38. }
  39. template<typename obj_type>
  40. arma_warn_unused
  41. inline
  42. obj_type
  43. speye(const SizeMat& s, const typename arma_SpMat_SpCol_SpRow_only<obj_type>::result* junk = NULL)
  44. {
  45. arma_extra_debug_sigprint();
  46. arma_ignore(junk);
  47. return speye<obj_type>(s.n_rows, s.n_cols);
  48. }
  49. // Convenience shortcut method (no template parameter necessary)
  50. arma_warn_unused
  51. inline
  52. sp_mat
  53. speye(const uword n_rows, const uword n_cols)
  54. {
  55. arma_extra_debug_sigprint();
  56. sp_mat out;
  57. out.eye(n_rows, n_cols);
  58. return out;
  59. }
  60. arma_warn_unused
  61. inline
  62. sp_mat
  63. speye(const SizeMat& s)
  64. {
  65. arma_extra_debug_sigprint();
  66. sp_mat out;
  67. out.eye(s.n_rows, s.n_cols);
  68. return out;
  69. }
  70. //! @}