fn_regspace.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_regspace
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. internal_regspace_default_delta
  21. (
  22. Mat<eT>& x,
  23. const typename Mat<eT>::pod_type start,
  24. const typename Mat<eT>::pod_type end
  25. )
  26. {
  27. arma_extra_debug_sigprint();
  28. typedef typename Mat<eT>::pod_type T;
  29. const bool ascend = (start <= end);
  30. const uword N = uword(1) + uword((ascend) ? (end-start) : (start-end));
  31. x.set_size(N);
  32. eT* x_mem = x.memptr();
  33. if(ascend)
  34. {
  35. for(uword i=0; i < N; ++i) { x_mem[i] = eT(start + T(i)); }
  36. }
  37. else
  38. {
  39. for(uword i=0; i < N; ++i) { x_mem[i] = eT(start - T(i)); }
  40. }
  41. }
  42. template<typename eT, typename sT>
  43. inline
  44. typename enable_if2< (is_signed<sT>::value == true), void >::result
  45. internal_regspace_var_delta
  46. (
  47. Mat<eT>& x,
  48. const typename Mat<eT>::pod_type start,
  49. const sT delta,
  50. const typename Mat<eT>::pod_type end
  51. )
  52. {
  53. arma_extra_debug_sigprint();
  54. arma_extra_debug_print("internal_regspace_var_delta(): signed version");
  55. typedef typename Mat<eT>::pod_type T;
  56. if( ((start < end) && (delta < sT(0))) || ((start > end) && (delta > sT(0))) || (delta == sT(0)) ) { return; }
  57. const bool ascend = (start <= end);
  58. const T inc = (delta < sT(0)) ? T(-delta) : T(delta);
  59. const T M = ((ascend) ? T(end-start) : T(start-end)) / T(inc);
  60. const uword N = uword(1) + ( (is_non_integral<T>::value) ? uword(std::floor(double(M))) : uword(M) );
  61. x.set_size(N);
  62. eT* x_mem = x.memptr();
  63. if(ascend)
  64. {
  65. for(uword i=0; i < N; ++i) { x_mem[i] = eT( start + T(i*inc) ); }
  66. }
  67. else
  68. {
  69. for(uword i=0; i < N; ++i) { x_mem[i] = eT( start - T(i*inc) ); }
  70. }
  71. }
  72. template<typename eT, typename uT>
  73. inline
  74. typename enable_if2< (is_signed<uT>::value == false), void >::result
  75. internal_regspace_var_delta
  76. (
  77. Mat<eT>& x,
  78. const typename Mat<eT>::pod_type start,
  79. const uT delta,
  80. const typename Mat<eT>::pod_type end
  81. )
  82. {
  83. arma_extra_debug_sigprint();
  84. arma_extra_debug_print("internal_regspace_var_delta(): unsigned version");
  85. typedef typename Mat<eT>::pod_type T;
  86. if( ((start > end) && (delta > uT(0))) || (delta == uT(0)) ) { return; }
  87. const bool ascend = (start <= end);
  88. const T inc = T(delta);
  89. const T M = ((ascend) ? T(end-start) : T(start-end)) / T(inc);
  90. const uword N = uword(1) + ( (is_non_integral<T>::value) ? uword(std::floor(double(M))) : uword(M) );
  91. x.set_size(N);
  92. eT* x_mem = x.memptr();
  93. if(ascend)
  94. {
  95. for(uword i=0; i < N; ++i) { x_mem[i] = eT( start + T(i*inc) ); }
  96. }
  97. else
  98. {
  99. for(uword i=0; i < N; ++i) { x_mem[i] = eT( start - T(i*inc) ); }
  100. }
  101. }
  102. template<typename vec_type, typename sT>
  103. inline
  104. typename enable_if2< is_Mat<vec_type>::value && (is_signed<sT>::value == true), vec_type >::result
  105. regspace
  106. (
  107. const typename vec_type::pod_type start,
  108. const sT delta,
  109. const typename vec_type::pod_type end
  110. )
  111. {
  112. arma_extra_debug_sigprint();
  113. arma_extra_debug_print("regspace(): signed version");
  114. vec_type x;
  115. if( ((delta == sT(+1)) && (start <= end)) || ((delta == sT(-1)) && (start > end)) )
  116. {
  117. internal_regspace_default_delta(x, start, end);
  118. }
  119. else
  120. {
  121. internal_regspace_var_delta(x, start, delta, end);
  122. }
  123. if(x.n_elem == 0)
  124. {
  125. if(is_Mat_only<vec_type>::value) { x.set_size(1,0); }
  126. }
  127. return x;
  128. }
  129. template<typename vec_type, typename sT>
  130. inline
  131. vec_type
  132. regspace
  133. (
  134. const typename vec_type::pod_type start,
  135. const sT delta,
  136. const typename vec_type::pod_type end
  137. )
  138. {
  139. arma_extra_debug_sigprint();
  140. arma_extra_debug_print("regspace(): signed version");
  141. vec_type x;
  142. if( ((delta == sT(+1)) && (start <= end)) || ((delta == sT(-1)) && (start > end)) )
  143. {
  144. internal_regspace_default_delta(x, start, end);
  145. }
  146. else
  147. {
  148. internal_regspace_var_delta(x, start, delta, end);
  149. }
  150. if(x.n_elem == 0)
  151. {
  152. if(is_Mat_only<vec_type>::value) { x.set_size(1,0); }
  153. }
  154. return x;
  155. }
  156. template<typename vec_type, typename uT>
  157. inline
  158. typename enable_if2< is_Mat<vec_type>::value && (is_signed<uT>::value == false), vec_type >::result
  159. regspace
  160. (
  161. const typename vec_type::pod_type start,
  162. const uT delta,
  163. const typename vec_type::pod_type end
  164. )
  165. {
  166. arma_extra_debug_sigprint();
  167. arma_extra_debug_print("regspace(): unsigned version");
  168. vec_type x;
  169. if( (delta == uT(+1)) && (start <= end) )
  170. {
  171. internal_regspace_default_delta(x, start, end);
  172. }
  173. else
  174. {
  175. internal_regspace_var_delta(x, start, delta, end);
  176. }
  177. if(x.n_elem == 0)
  178. {
  179. if(is_Mat_only<vec_type>::value) { x.set_size(1,0); }
  180. }
  181. return x;
  182. }
  183. template<typename vec_type>
  184. arma_warn_unused
  185. inline
  186. typename
  187. enable_if2
  188. <
  189. is_Mat<vec_type>::value,
  190. vec_type
  191. >::result
  192. regspace
  193. (
  194. const typename vec_type::pod_type start,
  195. const typename vec_type::pod_type end
  196. )
  197. {
  198. arma_extra_debug_sigprint();
  199. vec_type x;
  200. internal_regspace_default_delta(x, start, end);
  201. if(x.n_elem == 0)
  202. {
  203. if(is_Mat_only<vec_type>::value) { x.set_size(1,0); }
  204. }
  205. return x;
  206. }
  207. arma_warn_unused
  208. inline
  209. vec
  210. regspace(const double start, const double delta, const double end)
  211. {
  212. arma_extra_debug_sigprint();
  213. return regspace<vec>(start, delta, end);
  214. }
  215. arma_warn_unused
  216. inline
  217. vec
  218. regspace(const double start, const double end)
  219. {
  220. arma_extra_debug_sigprint();
  221. return regspace<vec>(start, end);
  222. }
  223. //! @}