VEC.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #pragma once
  2. /* VEC.h
  3. *
  4. * Copyright (C) 2017,2018 Paul Boersma
  5. *
  6. * This code is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This code is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. From here on alphabetical order.
  21. */
  22. inline void VECadd_inplace (const VEC& x, double addend) noexcept {
  23. //for (integer i = 1; i <= x.size; i ++)
  24. // x [i] += addend;
  25. for (double& element : x) element += addend;
  26. }
  27. inline VEC operator+= (const VEC& x, double addend) noexcept {
  28. //for (integer i = 1; i <= x.size; i ++)
  29. // x [i] += addend;
  30. for (double& element : x) element += addend;
  31. return x;
  32. }
  33. inline void VECadd_inplace (const VEC& x, const constVEC& y) noexcept {
  34. Melder_assert (y.size == x.size);
  35. for (integer i = 1; i <= x.size; i ++)
  36. x [i] += y [i];
  37. }
  38. inline VEC operator+= (const VEC& x, const constVEC& y) noexcept {
  39. Melder_assert (y.size == x.size);
  40. for (integer i = 1; i <= x.size; i ++)
  41. x [i] += y [i];
  42. return x;
  43. }
  44. inline void VECadd_preallocated (const VEC& target, const constVEC& x, double addend) noexcept {
  45. Melder_assert (x.size == target.size);
  46. for (integer i = 1; i <= x.size; i ++)
  47. target [i] = x [i] + addend;
  48. }
  49. inline autoVEC VECadd (const constVEC& x, double addend) {
  50. autoVEC result = VECraw (x.size);
  51. VECadd_preallocated (result.get(), x, addend);
  52. return result;
  53. }
  54. extern void VECadd_macfast_ (const VEC& target, const constVEC& x, const constVEC& y) noexcept;
  55. inline void VECadd_preallocated (const VEC& target, const constVEC& x, const constVEC& y) noexcept {
  56. integer n = target.size;
  57. Melder_assert (x.size == n);
  58. Melder_assert (y.size == n);
  59. #if defined (macintosh)
  60. if (n >= 64)
  61. return VECadd_macfast_ (target, x, y);
  62. #endif
  63. for (integer i = 1; i <= n; i ++)
  64. target [i] = x [i] + y [i];
  65. }
  66. inline autoVEC VECadd (const constVEC& x, const constVEC& y) {
  67. autoVEC result = VECraw (x.size);
  68. VECadd_preallocated (result.get(), x, y);
  69. return result;
  70. }
  71. inline void VECcentre_inplace (const VEC& x, double *out_mean = nullptr) noexcept {
  72. double xmean = NUMmean (x);
  73. for (integer i = 1; i <= x.size; i ++)
  74. x [i] -= xmean;
  75. if (out_mean)
  76. *out_mean = xmean;
  77. }
  78. inline void VECcolumn_preallocated (const VEC& target, const constMAT& source, integer columnNumber) noexcept {
  79. Melder_assert (source.nrow == target.size);
  80. Melder_assert (columnNumber >= 1 && columnNumber <= source.ncol);
  81. for (integer irow = 1; irow <= target.size; irow ++)
  82. target [irow] = source [irow] [columnNumber];
  83. }
  84. inline autoVEC VECcolumn (const constMAT& source, integer columnNumber) {
  85. autoVEC target = VECraw (source.nrow);
  86. VECcolumn_preallocated (target.get(), source, columnNumber);
  87. return target;
  88. }
  89. inline void VECcolumnMeans_preallocated (const VEC& target, const constMAT& x) noexcept {
  90. Melder_assert (target.size == x.ncol);
  91. for (integer icol = 1; icol <= x.ncol; icol ++)
  92. target [icol] = NUMcolumnMean (x, icol);
  93. }
  94. extern void VECmul_preallocated (const VEC& target, const constVEC& vec, const constMAT& mat) noexcept;
  95. extern void VECmul_preallocated (const VEC& target, const constMAT& mat, const constVEC& vec) noexcept;
  96. extern autoVEC VECmul (const constVEC& vec, const constMAT& mat) noexcept;
  97. extern autoVEC VECmul (const constMAT& mat, const constVEC& vec) noexcept;
  98. inline void VECmultiply_inplace (const VEC& x, double factor) noexcept {
  99. for (integer i = 1; i <= x.size; i ++)
  100. x [i] *= factor;
  101. }
  102. inline autoVEC VECrandomGauss (integer size, double mu, double sigma) {
  103. autoVEC result = VECraw (size);
  104. for (integer i = 1; i <= size; i ++)
  105. result [i] = NUMrandomGauss (mu, sigma);
  106. return result;
  107. }
  108. inline autoVEC VECrandomUniform (integer size, double lowest, double highest) {
  109. autoVEC result = VECraw (size);
  110. for (integer i = 1; i <= size; i ++)
  111. result [i] = NUMrandomUniform (lowest, highest);
  112. return result;
  113. }
  114. inline void VECsin_inplace (const VEC& x) noexcept {
  115. for (double& element : x) element = sin (element);
  116. }
  117. extern void VECsort_inplace (const VEC& x) noexcept;
  118. inline void VECsubtract_inplace (const VEC& x, double number) noexcept {
  119. for (integer i = 1; i <= x.size; i ++)
  120. x [i] -= number;
  121. }
  122. inline void VECsubtractReversed_inplace (const VEC& x, double number) noexcept {
  123. for (integer i = 1; i <= x.size; i ++)
  124. x [i] = number - x [i];
  125. }
  126. inline void VECsubtract_inplace (const VEC& x, const constVEC& y) noexcept {
  127. Melder_assert (x.size == y.size);
  128. for (integer i = 1; i <= x.size; i ++)
  129. x [i] -= y [i];
  130. }
  131. inline void VECsubtractReversed_inplace (const VEC& x, const constVEC& y) noexcept {
  132. Melder_assert (x.size == y.size);
  133. for (integer i = 1; i <= x.size; i ++)
  134. x [i] = y [i] - x [i];
  135. }
  136. inline autoVEC VECsubtract (const constVEC& x, double y) {
  137. autoVEC result = VECraw (x.size);
  138. for (integer i = 1; i <= x.size; i ++)
  139. result [i] = x [i] - y;
  140. return result;
  141. }
  142. inline autoVEC VECsubtract (double x, const constVEC& y) {
  143. autoVEC result = VECraw (y.size);
  144. for (integer i = 1; i <= y.size; i ++)
  145. result [i] = x - y [i];
  146. return result;
  147. }
  148. inline autoVEC VECsubtract (const constVEC& x, const constVEC& y) {
  149. Melder_assert (x.size == y.size);
  150. autoVEC result = VECraw (x.size);
  151. for (integer i = 1; i <= x.size; i ++)
  152. result [i] = x [i] - y [i];
  153. return result;
  154. }
  155. inline autoVEC VECsumPerRow (const constMAT& x) {
  156. autoVEC result = VECraw (x.nrow);
  157. for (integer irow = 1; irow <= x.nrow; irow ++)
  158. result [irow] = NUMrowSum (x, irow);
  159. return result;
  160. }
  161. inline autoVEC VECsumPerColumn (const constMAT& x) {
  162. autoVEC result = VECraw (x.ncol);
  163. for (integer icol = 1; icol <= x.ncol; icol ++)
  164. result [icol] = NUMcolumnSum (x, icol);
  165. return result;
  166. }
  167. inline autoVEC VECto (integer to) {
  168. autoVEC result = VECraw (to);
  169. for (integer i = 1; i <= to; i ++)
  170. result [i] = (double) i;
  171. return result;
  172. }
  173. /* End of file VEC.h */