fn_n_unique.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_n_unique
  16. //! @{
  17. //! \brief
  18. //! Get the number of unique nonzero elements in two sparse matrices.
  19. //! This is very useful for determining the amount of memory necessary before
  20. //! a sparse matrix operation on two matrices.
  21. template<typename T1, typename T2, typename op_n_unique_type>
  22. inline
  23. uword
  24. n_unique
  25. (
  26. const SpBase<typename T1::elem_type, T1>& x,
  27. const SpBase<typename T2::elem_type, T2>& y,
  28. const op_n_unique_type junk
  29. )
  30. {
  31. arma_extra_debug_sigprint();
  32. const SpProxy<T1> pa(x.get_ref());
  33. const SpProxy<T2> pb(y.get_ref());
  34. return n_unique(pa,pb,junk);
  35. }
  36. template<typename T1, typename T2, typename op_n_unique_type>
  37. arma_hot
  38. inline
  39. uword
  40. n_unique
  41. (
  42. const SpProxy<T1>& pa,
  43. const SpProxy<T2>& pb,
  44. const op_n_unique_type junk
  45. )
  46. {
  47. arma_extra_debug_sigprint();
  48. arma_ignore(junk);
  49. typename SpProxy<T1>::const_iterator_type x_it = pa.begin();
  50. typename SpProxy<T1>::const_iterator_type x_it_end = pa.end();
  51. typename SpProxy<T2>::const_iterator_type y_it = pb.begin();
  52. typename SpProxy<T2>::const_iterator_type y_it_end = pb.end();
  53. uword total_n_nonzero = 0;
  54. while( (x_it != x_it_end) || (y_it != y_it_end) )
  55. {
  56. if(x_it == y_it)
  57. {
  58. if(op_n_unique_type::eval((*x_it), (*y_it)) != typename T1::elem_type(0))
  59. {
  60. ++total_n_nonzero;
  61. }
  62. ++x_it;
  63. ++y_it;
  64. }
  65. else
  66. {
  67. if((x_it.col() < y_it.col()) || ((x_it.col() == y_it.col()) && (x_it.row() < y_it.row()))) // if y is closer to the end
  68. {
  69. if(op_n_unique_type::eval((*x_it), typename T1::elem_type(0)) != typename T1::elem_type(0))
  70. {
  71. ++total_n_nonzero;
  72. }
  73. ++x_it;
  74. }
  75. else // x is closer to the end
  76. {
  77. if(op_n_unique_type::eval(typename T1::elem_type(0), (*y_it)) != typename T1::elem_type(0))
  78. {
  79. ++total_n_nonzero;
  80. }
  81. ++y_it;
  82. }
  83. }
  84. }
  85. return total_n_nonzero;
  86. }
  87. // Simple operators.
  88. struct op_n_unique_add
  89. {
  90. template<typename eT> inline static eT eval(const eT& l, const eT& r) { return (l + r); }
  91. };
  92. struct op_n_unique_sub
  93. {
  94. template<typename eT> inline static eT eval(const eT& l, const eT& r) { return (l - r); }
  95. };
  96. struct op_n_unique_mul
  97. {
  98. template<typename eT> inline static eT eval(const eT& l, const eT& r) { return (l * r); }
  99. };
  100. struct op_n_unique_count
  101. {
  102. template<typename eT> inline static eT eval(const eT&, const eT&) { return eT(1); }
  103. };
  104. //! @}