fn_max.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_max
  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. max(const T1& X)
  22. {
  23. arma_extra_debug_sigprint();
  24. return op_max::max(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_max> >::result
  30. max(const T1& X)
  31. {
  32. arma_extra_debug_sigprint();
  33. return Op<T1, op_max>(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_max> >::result
  39. max(const T1& X, const uword dim)
  40. {
  41. arma_extra_debug_sigprint();
  42. return Op<T1, op_max>(X, dim, 0);
  43. }
  44. template<typename T>
  45. arma_warn_unused
  46. arma_inline
  47. typename arma_scalar_only<T>::result
  48. max(const T& x)
  49. {
  50. return x;
  51. }
  52. //! element-wise maximum
  53. template<typename T1, typename T2>
  54. arma_warn_unused
  55. arma_inline
  56. typename
  57. enable_if2
  58. <
  59. ( is_arma_type<T1>::value && is_arma_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value ),
  60. const Glue<T1, T2, glue_max>
  61. >::result
  62. max
  63. (
  64. const T1& X,
  65. const T2& Y
  66. )
  67. {
  68. arma_extra_debug_sigprint();
  69. return Glue<T1, T2, glue_max>(X, Y);
  70. }
  71. template<typename T1>
  72. arma_warn_unused
  73. arma_inline
  74. const OpCube<T1, op_max>
  75. max
  76. (
  77. const BaseCube<typename T1::elem_type, T1>& X,
  78. const uword dim = 0
  79. )
  80. {
  81. arma_extra_debug_sigprint();
  82. return OpCube<T1, op_max>(X.get_ref(), dim, 0);
  83. }
  84. template<typename T1, typename T2>
  85. arma_warn_unused
  86. arma_inline
  87. const GlueCube<T1, T2, glue_max>
  88. max
  89. (
  90. const BaseCube<typename T1::elem_type, T1>& X,
  91. const BaseCube<typename T1::elem_type, T2>& Y
  92. )
  93. {
  94. arma_extra_debug_sigprint();
  95. return GlueCube<T1, T2, glue_max>(X.get_ref(), Y.get_ref());
  96. }
  97. template<typename T1>
  98. arma_warn_unused
  99. inline
  100. typename
  101. enable_if2
  102. <
  103. is_arma_sparse_type<T1>::value && resolves_to_sparse_vector<T1>::yes,
  104. typename T1::elem_type
  105. >::result
  106. max(const T1& x)
  107. {
  108. arma_extra_debug_sigprint();
  109. return spop_max::vector_max(x);
  110. }
  111. template<typename T1>
  112. arma_warn_unused
  113. inline
  114. typename
  115. enable_if2
  116. <
  117. is_arma_sparse_type<T1>::value && resolves_to_sparse_vector<T1>::no,
  118. const SpOp<T1, spop_max>
  119. >::result
  120. max(const T1& X)
  121. {
  122. arma_extra_debug_sigprint();
  123. return SpOp<T1, spop_max>(X, 0, 0);
  124. }
  125. template<typename T1>
  126. arma_warn_unused
  127. inline
  128. typename
  129. enable_if2
  130. <
  131. is_arma_sparse_type<T1>::value,
  132. const SpOp<T1, spop_max>
  133. >::result
  134. max(const T1& X, const uword dim)
  135. {
  136. arma_extra_debug_sigprint();
  137. return SpOp<T1, spop_max>(X, dim, 0);
  138. }
  139. // elementwise sparse max
  140. template<typename T1, typename T2>
  141. arma_warn_unused
  142. inline
  143. typename
  144. enable_if2
  145. <
  146. (is_arma_sparse_type<T1>::value && is_arma_sparse_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
  147. const SpGlue<T1, T2, spglue_max>
  148. >::result
  149. max(const T1& x, const T2& y)
  150. {
  151. arma_extra_debug_sigprint();
  152. return SpGlue<T1, T2, spglue_max>(x, y);
  153. }
  154. //! elementwise max of dense and sparse objects with the same element type
  155. template<typename T1, typename T2>
  156. inline
  157. typename
  158. enable_if2
  159. <
  160. (is_arma_type<T1>::value && is_arma_sparse_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
  161. Mat<typename T1::elem_type>
  162. >::result
  163. max
  164. (
  165. const T1& x,
  166. const T2& y
  167. )
  168. {
  169. arma_extra_debug_sigprint();
  170. Mat<typename T1::elem_type> out;
  171. spglue_max::dense_sparse_max(out, x, y);
  172. return out;
  173. }
  174. //! elementwise max of sparse and dense objects with the same element type
  175. template<typename T1, typename T2>
  176. inline
  177. typename
  178. enable_if2
  179. <
  180. (is_arma_sparse_type<T1>::value && is_arma_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
  181. Mat<typename T1::elem_type>
  182. >::result
  183. max
  184. (
  185. const T1& x,
  186. const T2& y
  187. )
  188. {
  189. arma_extra_debug_sigprint();
  190. Mat<typename T1::elem_type> out;
  191. // Just call the other order (these operations are commutative)
  192. // TODO: if there is a matrix size mismatch, the debug assert will print the matrix sizes in wrong order
  193. spglue_max::dense_sparse_max(out, y, x);
  194. return out;
  195. }
  196. arma_warn_unused
  197. inline
  198. uword
  199. max(const SizeMat& s)
  200. {
  201. return (std::max)(s.n_rows, s.n_cols);
  202. }
  203. arma_warn_unused
  204. inline
  205. uword
  206. max(const SizeCube& s)
  207. {
  208. return (std::max)( (std::max)(s.n_rows, s.n_cols), s.n_slices );
  209. }
  210. //! @}