glue_trapz_meat.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 glue_trapz
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. glue_trapz::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_trapz>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const uword dim = in.aux_uword;
  25. const quasi_unwrap<T1> UX(in.A);
  26. const quasi_unwrap<T2> UY(in.B);
  27. if( UX.is_alias(out) || UY.is_alias(out) )
  28. {
  29. Mat<eT> tmp;
  30. glue_trapz::apply_noalias(tmp, UX.M, UY.M, dim);
  31. out.steal_mem(tmp);
  32. }
  33. else
  34. {
  35. glue_trapz::apply_noalias(out, UX.M, UY.M, dim);
  36. }
  37. }
  38. template<typename eT>
  39. inline
  40. void
  41. glue_trapz::apply_noalias(Mat<eT>& out, const Mat<eT>& X, const Mat<eT>& Y, const uword dim)
  42. {
  43. arma_extra_debug_sigprint();
  44. arma_debug_check( (dim > 1), "trapz(): argument 'dim' must be 0 or 1" );
  45. arma_debug_check( ((X.is_vec() == false) && (X.is_empty() == false)), "trapz(): argument 'X' must be a vector" );
  46. const uword N = X.n_elem;
  47. if(dim == 0)
  48. {
  49. arma_debug_check( (N != Y.n_rows), "trapz(): length of X must equal the number of rows in Y when dim=0" );
  50. }
  51. else
  52. if(dim == 1)
  53. {
  54. arma_debug_check( (N != Y.n_cols), "trapz(): length of X must equal the number of columns in Y when dim=1" );
  55. }
  56. if(N <= 1)
  57. {
  58. if(dim == 0) { out.zeros(1, Y.n_cols); }
  59. else if(dim == 1) { out.zeros(Y.n_rows, 1); }
  60. return;
  61. }
  62. const Col<eT> vec_X( const_cast<eT*>(X.memptr()), X.n_elem, false, true );
  63. const Col<eT> diff_X = diff(vec_X);
  64. if(dim == 0)
  65. {
  66. const Row<eT> diff_X_t( const_cast<eT*>(diff_X.memptr()), diff_X.n_elem, false, true );
  67. out = diff_X_t * (0.5 * (Y.rows(0, N-2) + Y.rows(1, N-1)));
  68. }
  69. else
  70. if(dim == 1)
  71. {
  72. out = (0.5 * (Y.cols(0, N-2) + Y.cols(1, N-1))) * diff_X;
  73. }
  74. }
  75. template<typename T1>
  76. inline
  77. void
  78. op_trapz::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trapz>& in)
  79. {
  80. arma_extra_debug_sigprint();
  81. typedef typename T1::elem_type eT;
  82. const uword dim = in.aux_uword_a;
  83. const quasi_unwrap<T1> UY(in.m);
  84. if(UY.is_alias(out))
  85. {
  86. Mat<eT> tmp;
  87. op_trapz::apply_noalias(tmp, UY.M, dim);
  88. out.steal_mem(tmp);
  89. }
  90. else
  91. {
  92. op_trapz::apply_noalias(out, UY.M, dim);
  93. }
  94. }
  95. template<typename eT>
  96. inline
  97. void
  98. op_trapz::apply_noalias(Mat<eT>& out, const Mat<eT>& Y, const uword dim)
  99. {
  100. arma_extra_debug_sigprint();
  101. arma_debug_check( (dim > 1), "trapz(): argument 'dim' must be 0 or 1" );
  102. uword N = 0;
  103. if(dim == 0) { N = Y.n_rows; }
  104. else if(dim == 1) { N = Y.n_cols; }
  105. if(N <= 1)
  106. {
  107. if(dim == 0) { out.zeros(1, Y.n_cols); }
  108. else if(dim == 1) { out.zeros(Y.n_rows, 1); }
  109. return;
  110. }
  111. if(dim == 0)
  112. {
  113. out = sum( (0.5 * (Y.rows(0, N-2) + Y.rows(1, N-1))), 0 );
  114. }
  115. else
  116. if(dim == 1)
  117. {
  118. out = sum( (0.5 * (Y.cols(0, N-2) + Y.cols(1, N-1))), 1 );
  119. }
  120. }
  121. //! @}