fn_sum.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_sum
  16. //! @{
  17. template<typename T1>
  18. arma_warn_unused
  19. inline
  20. typename enable_if2< is_arma_type<T1>::value && resolves_to_vector<T1>::yes, typename T1::elem_type >::result
  21. sum(const T1& X)
  22. {
  23. arma_extra_debug_sigprint();
  24. return accu(X);
  25. }
  26. template<typename T1>
  27. arma_warn_unused
  28. arma_inline
  29. typename enable_if2< is_arma_type<T1>::value && resolves_to_vector<T1>::no, const Op<T1, op_sum> >::result
  30. sum(const T1& X)
  31. {
  32. arma_extra_debug_sigprint();
  33. return Op<T1, op_sum>(X, 0, 0);
  34. }
  35. template<typename T1>
  36. arma_warn_unused
  37. arma_inline
  38. typename enable_if2< is_arma_type<T1>::value, const Op<T1, op_sum> >::result
  39. sum(const T1& X, const uword dim)
  40. {
  41. arma_extra_debug_sigprint();
  42. return Op<T1, op_sum>(X, dim, 0);
  43. }
  44. template<typename T>
  45. arma_warn_unused
  46. arma_inline
  47. typename arma_scalar_only<T>::result
  48. sum(const T& x)
  49. {
  50. return x;
  51. }
  52. //! sum of cube
  53. template<typename T1>
  54. arma_warn_unused
  55. arma_inline
  56. const OpCube<T1, op_sum>
  57. sum
  58. (
  59. const BaseCube<typename T1::elem_type,T1>& X,
  60. const uword dim = 0
  61. )
  62. {
  63. arma_extra_debug_sigprint();
  64. return OpCube<T1, op_sum>(X.get_ref(), dim, 0);
  65. }
  66. //! sum of sparse object
  67. template<typename T1>
  68. arma_warn_unused
  69. inline
  70. typename
  71. enable_if2
  72. <
  73. is_arma_sparse_type<T1>::value && resolves_to_sparse_vector<T1>::yes,
  74. typename T1::elem_type
  75. >::result
  76. sum(const T1& x)
  77. {
  78. arma_extra_debug_sigprint();
  79. // sum elements
  80. return accu(x);
  81. }
  82. template<typename T1>
  83. arma_warn_unused
  84. inline
  85. typename
  86. enable_if2
  87. <
  88. is_arma_sparse_type<T1>::value && resolves_to_sparse_vector<T1>::no,
  89. const SpOp<T1,spop_sum>
  90. >::result
  91. sum(const T1& x)
  92. {
  93. arma_extra_debug_sigprint();
  94. return SpOp<T1,spop_sum>(x, 0, 0);
  95. }
  96. template<typename T1>
  97. arma_warn_unused
  98. inline
  99. typename
  100. enable_if2
  101. <
  102. is_arma_sparse_type<T1>::value,
  103. const SpOp<T1,spop_sum>
  104. >::result
  105. sum(const T1& x, const uword dim)
  106. {
  107. arma_extra_debug_sigprint();
  108. return SpOp<T1,spop_sum>(x, dim, 0);
  109. }
  110. //! @}