fn_interp2.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_interp2
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. interp2_helper_nearest(const Mat<eT>& XG, const Mat<eT>& ZG, const Mat<eT>& XI, Mat<eT>& ZI, const eT extrap_val, const uword mode)
  21. {
  22. arma_extra_debug_sigprint();
  23. const eT XG_min = XG.min();
  24. const eT XG_max = XG.max();
  25. // mode = 0: interpolate across rows (eg. expand in vertical direction)
  26. // mode = 1: interpolate across columns (eg. expand in horizontal direction)
  27. if(mode == 0) { ZI.set_size(XI.n_elem, ZG.n_cols); }
  28. if(mode == 1) { ZI.set_size(ZG.n_rows, XI.n_elem); }
  29. const eT* XG_mem = XG.memptr();
  30. const eT* XI_mem = XI.memptr();
  31. const uword NG = XG.n_elem;
  32. const uword NI = XI.n_elem;
  33. uword best_j = 0;
  34. for(uword i=0; i<NI; ++i)
  35. {
  36. eT best_err = Datum<eT>::inf;
  37. const eT XI_val = XI_mem[i];
  38. if((XI_val < XG_min) || (XI_val > XG_max))
  39. {
  40. if(mode == 0) { ZI.row(i).fill(extrap_val); }
  41. if(mode == 1) { ZI.col(i).fill(extrap_val); }
  42. }
  43. else
  44. {
  45. // XG and XI are guaranteed to be sorted in ascending manner,
  46. // so start searching XG from last known optimum position
  47. for(uword j=best_j; j<NG; ++j)
  48. {
  49. const eT tmp = XG_mem[j] - XI_val;
  50. const eT err = (tmp >= eT(0)) ? tmp : -tmp;
  51. if(err >= best_err)
  52. {
  53. // error is going up, so we have found the optimum position
  54. break;
  55. }
  56. else
  57. {
  58. best_err = err;
  59. best_j = j; // remember the optimum position
  60. }
  61. }
  62. if(mode == 0) { ZI.row(i) = ZG.row(best_j); }
  63. if(mode == 1) { ZI.col(i) = ZG.col(best_j); }
  64. }
  65. }
  66. }
  67. template<typename eT>
  68. inline
  69. void
  70. interp2_helper_linear(const Mat<eT>& XG, const Mat<eT>& ZG, const Mat<eT>& XI, Mat<eT>& ZI, const eT extrap_val, const uword mode)
  71. {
  72. arma_extra_debug_sigprint();
  73. const eT XG_min = XG.min();
  74. const eT XG_max = XG.max();
  75. // mode = 0: interpolate across rows (eg. expand in vertical direction)
  76. // mode = 1: interpolate across columns (eg. expand in horizontal direction)
  77. if(mode == 0) { ZI.set_size(XI.n_elem, ZG.n_cols); }
  78. if(mode == 1) { ZI.set_size(ZG.n_rows, XI.n_elem); }
  79. const eT* XG_mem = XG.memptr();
  80. const eT* XI_mem = XI.memptr();
  81. const uword NG = XG.n_elem;
  82. const uword NI = XI.n_elem;
  83. uword a_best_j = 0;
  84. uword b_best_j = 0;
  85. for(uword i=0; i<NI; ++i)
  86. {
  87. const eT XI_val = XI_mem[i];
  88. if((XI_val < XG_min) || (XI_val > XG_max))
  89. {
  90. if(mode == 0) { ZI.row(i).fill(extrap_val); }
  91. if(mode == 1) { ZI.col(i).fill(extrap_val); }
  92. }
  93. else
  94. {
  95. // XG and XI are guaranteed to be sorted in ascending manner,
  96. // so start searching XG from last known optimum position
  97. eT a_best_err = Datum<eT>::inf;
  98. eT b_best_err = Datum<eT>::inf;
  99. for(uword j=a_best_j; j<NG; ++j)
  100. {
  101. const eT tmp = XG_mem[j] - XI_val;
  102. const eT err = (tmp >= eT(0)) ? tmp : -tmp;
  103. if(err >= a_best_err)
  104. {
  105. break;
  106. }
  107. else
  108. {
  109. a_best_err = err;
  110. a_best_j = j;
  111. }
  112. }
  113. if( (XG_mem[a_best_j] - XI_val) <= eT(0) )
  114. {
  115. // a_best_j is to the left of the interpolated position
  116. b_best_j = ( (a_best_j+1) < NG) ? (a_best_j+1) : a_best_j;
  117. }
  118. else
  119. {
  120. // a_best_j is to the right of the interpolated position
  121. b_best_j = (a_best_j >= 1) ? (a_best_j-1) : a_best_j;
  122. }
  123. b_best_err = std::abs( XG_mem[b_best_j] - XI_val );
  124. if(a_best_j > b_best_j)
  125. {
  126. std::swap(a_best_j, b_best_j );
  127. std::swap(a_best_err, b_best_err);
  128. }
  129. const eT weight = (a_best_err > eT(0)) ? (a_best_err / (a_best_err + b_best_err)) : eT(0);
  130. if(mode == 0) { ZI.row(i) = (eT(1) - weight)*ZG.row(a_best_j) + (weight)*ZG.row(b_best_j); }
  131. if(mode == 1) { ZI.col(i) = (eT(1) - weight)*ZG.col(a_best_j) + (weight)*ZG.col(b_best_j); }
  132. }
  133. }
  134. }
  135. template<typename T1, typename T2, typename T3, typename T4, typename T5>
  136. inline
  137. typename
  138. enable_if2< is_real<typename T1::elem_type>::value, void >::result
  139. interp2
  140. (
  141. const Base<typename T1::elem_type, T1>& X,
  142. const Base<typename T1::elem_type, T2>& Y,
  143. const Base<typename T1::elem_type, T3>& Z,
  144. const Base<typename T1::elem_type, T4>& XI,
  145. const Base<typename T1::elem_type, T5>& YI,
  146. Mat<typename T1::elem_type>& ZI,
  147. const char* method = "linear",
  148. const typename T1::elem_type extrap_val = Datum<typename T1::elem_type>::nan
  149. )
  150. {
  151. arma_extra_debug_sigprint();
  152. typedef typename T1::elem_type eT;
  153. const char sig = (method != NULL) ? method[0] : char(0);
  154. arma_debug_check( ((sig != 'n') && (sig != 'l')), "interp2(): unsupported interpolation type" );
  155. const quasi_unwrap<T1> UXG( X.get_ref() );
  156. const quasi_unwrap<T2> UYG( Y.get_ref() );
  157. const quasi_unwrap<T3> UZG( Z.get_ref() );
  158. const quasi_unwrap<T4> UXI( XI.get_ref() );
  159. const quasi_unwrap<T5> UYI( YI.get_ref() );
  160. arma_debug_check( (UXG.M.is_vec() == false), "interp2(): X must resolve to a vector" );
  161. arma_debug_check( (UYG.M.is_vec() == false), "interp2(): Y must resolve to a vector" );
  162. arma_debug_check( (UXI.M.is_vec() == false), "interp2(): XI must resolve to a vector" );
  163. arma_debug_check( (UYI.M.is_vec() == false), "interp2(): YI must resolve to a vector" );
  164. arma_debug_check( (UXG.M.n_elem < 2), "interp2(): X must have at least two unique elements" );
  165. arma_debug_check( (UYG.M.n_elem < 2), "interp2(): Y must have at least two unique elements" );
  166. arma_debug_check( (UXG.M.n_elem != UZG.M.n_cols), "interp2(): number of elements in X must equal the number of columns in Z" );
  167. arma_debug_check( (UYG.M.n_elem != UZG.M.n_rows), "interp2(): number of elements in Y must equal the number of rows in Z" );
  168. arma_debug_check( (UXG.M.is_sorted("strictascend") == false), "interp2(): X must be monotonically increasing" );
  169. arma_debug_check( (UYG.M.is_sorted("strictascend") == false), "interp2(): Y must be monotonically increasing" );
  170. arma_debug_check( (UXI.M.is_sorted("strictascend") == false), "interp2(): XI must be monotonically increasing" );
  171. arma_debug_check( (UYI.M.is_sorted("strictascend") == false), "interp2(): YI must be monotonically increasing" );
  172. Mat<eT> tmp;
  173. if( UXG.is_alias(ZI) || UXI.is_alias(ZI) )
  174. {
  175. Mat<eT> out;
  176. if(sig == 'n')
  177. {
  178. interp2_helper_nearest(UYG.M, UZG.M, UYI.M, tmp, extrap_val, 0);
  179. interp2_helper_nearest(UXG.M, tmp, UXI.M, out, extrap_val, 1);
  180. }
  181. else
  182. if(sig == 'l')
  183. {
  184. interp2_helper_linear(UYG.M, UZG.M, UYI.M, tmp, extrap_val, 0);
  185. interp2_helper_linear(UXG.M, tmp, UXI.M, out, extrap_val, 1);
  186. }
  187. ZI.steal_mem(out);
  188. }
  189. else
  190. {
  191. if(sig == 'n')
  192. {
  193. interp2_helper_nearest(UYG.M, UZG.M, UYI.M, tmp, extrap_val, 0);
  194. interp2_helper_nearest(UXG.M, tmp, UXI.M, ZI, extrap_val, 1);
  195. }
  196. else
  197. if(sig == 'l')
  198. {
  199. interp2_helper_linear(UYG.M, UZG.M, UYI.M, tmp, extrap_val, 0);
  200. interp2_helper_linear(UXG.M, tmp, UXI.M, ZI, extrap_val, 1);
  201. }
  202. }
  203. }
  204. //! @}