Search.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace MathPHP;
  3. use MathPHP\Exception;
  4. /**
  5. * Search
  6. * Various functions to find specific indices in an array.
  7. */
  8. class Search
  9. {
  10. /**
  11. * Search Sorted
  12. * Find the array indices where items should be inserted to maintain sorted order.
  13. *
  14. * Inspired by and similar to Python NumPy's searchsorted
  15. *
  16. * @param float[]|int[] $haystack Sorted array with standard increasing numerical array keys
  17. * @param float $needle Item wanting to insert
  18. *
  19. * @return int Index of where you would insert the needle and maintain sorted order
  20. */
  21. public static function sorted(array $haystack, float $needle): int
  22. {
  23. if (empty($haystack)) {
  24. return 0;
  25. }
  26. $index = 0;
  27. foreach ($haystack as $i => $val) {
  28. if ($needle > $val) {
  29. $index++;
  30. } else {
  31. return $index;
  32. }
  33. }
  34. return $index;
  35. }
  36. /**
  37. * ArgMax
  38. * Find the array index of the maximum value.
  39. *
  40. * In case of the maximum value appearing multiple times, the index of the first occurrence is returned.
  41. * In the case NAN is present, the index of the first NAN is returned.
  42. *
  43. * Inspired by and similar to Python NumPy's argmax
  44. *
  45. * @param float[]|int[] $values
  46. *
  47. * @return int Index of the first occurrence of the maximum value
  48. *
  49. * @throws Exception\BadDataException if the array of values is empty
  50. */
  51. public static function argMax(array $values): int
  52. {
  53. if (empty($values)) {
  54. throw new Exception\BadDataException('Cannot find the argMax of an empty array');
  55. }
  56. // Special case: NAN wins if present
  57. $nanPresent = \array_filter(
  58. $values,
  59. function ($value) {
  60. return \is_float($value) && \is_nan($value);
  61. }
  62. );
  63. if (\count($nanPresent) > 0) {
  64. foreach ($values as $i => $v) {
  65. if (\is_nan($v)) {
  66. return $i;
  67. }
  68. }
  69. }
  70. // Standard case: Find max and return index
  71. return self::baseArgMax($values);
  72. }
  73. /**
  74. * NanArgMax
  75. * Find the array index of the maximum value, ignoring NANs
  76. *
  77. * In case of the maximum value appearing multiple times, the index of the first occurrence is returned.
  78. *
  79. * Inspired by and similar to Python NumPy's nanargmax
  80. *
  81. * @param float[]|int[] $values
  82. *
  83. * @return int Index of the first occurrence of the maximum value
  84. *
  85. * @throws Exception\BadDataException if the array of values is empty
  86. * @throws Exception\BadDataException if the array only contains NANs
  87. */
  88. public static function nanArgMax(array $values): int
  89. {
  90. if (empty($values)) {
  91. throw new Exception\BadDataException('Cannot find the argMax of an empty array');
  92. }
  93. $valuesWithoutNans = \array_filter(
  94. $values,
  95. function ($value) {
  96. return !\is_nan($value);
  97. }
  98. );
  99. if (\count($valuesWithoutNans) === 0) {
  100. throw new Exception\BadDataException('Array of all NANs has no nanArgMax');
  101. }
  102. return self::baseArgMax($valuesWithoutNans);
  103. }
  104. /**
  105. * Base argMax calculation
  106. * Find the array index of the maximum value.
  107. *
  108. * In case of the maximum value appearing multiple times, the index of the first occurrence is returned.
  109. *
  110. * @param float[]|int[] $values
  111. *
  112. * @return int Index of the first occurrence of the maximum value
  113. *
  114. * @throws \LogicException if the array of values is empty - should never happen
  115. */
  116. private static function baseArgMax(array $values): int
  117. {
  118. $max = \max($values);
  119. foreach ($values as $i => $v) {
  120. if ($v === $max) {
  121. return $i;
  122. }
  123. }
  124. throw new \LogicException('argMax values is empty--should not happen');
  125. }
  126. /**
  127. * ArgMin
  128. * Find the array index of the minimum value.
  129. *
  130. * In case of the minimum value appearing multiple times, the index of the first occurrence is returned.
  131. * In the case NAN is present, the index of the first NAN is returned.
  132. *
  133. * Inspired by and similar to Python NumPy's argmin
  134. *
  135. * @param float[]|int[] $values
  136. *
  137. * @return int Index of the first occurrence of the minimum value
  138. *
  139. * @throws Exception\BadDataException if the array of values is empty
  140. */
  141. public static function argMin(array $values): int
  142. {
  143. if (empty($values)) {
  144. throw new Exception\BadDataException('Cannot find the argMin of an empty array');
  145. }
  146. // Special case: NAN wins if present
  147. $nanPresent = \array_filter(
  148. $values,
  149. function ($value) {
  150. return \is_float($value) && \is_nan($value);
  151. }
  152. );
  153. if (\count($nanPresent) > 0) {
  154. foreach ($values as $i => $v) {
  155. if (\is_nan($v)) {
  156. return $i;
  157. }
  158. }
  159. }
  160. // Standard case: Find max and return index
  161. return self::baseArgMin($values);
  162. }
  163. /**
  164. * NanArgMin
  165. * Find the array index of the minimum value, ignoring NANs
  166. *
  167. * In case of the minimum value appearing multiple times, the index of the first occurrence is returned.
  168. *
  169. * Inspired by and similar to Python NumPy's nanargin
  170. *
  171. * @param float[]|int[] $values
  172. *
  173. * @return int Index of the first occurrence of the minimum value
  174. *
  175. * @throws Exception\BadDataException if the array of values is empty
  176. * @throws Exception\BadDataException if the array only contains NANs
  177. */
  178. public static function nanArgMin(array $values): int
  179. {
  180. if (empty($values)) {
  181. throw new Exception\BadDataException('Cannot find the nanArgMin of an empty array');
  182. }
  183. $valuesWithoutNans = \array_filter(
  184. $values,
  185. function ($value) {
  186. return !\is_nan($value);
  187. }
  188. );
  189. if (\count($valuesWithoutNans) === 0) {
  190. throw new Exception\BadDataException('Array of all NANs has no nanArgMax');
  191. }
  192. return self::baseArgMin($valuesWithoutNans);
  193. }
  194. /**
  195. * Base argMin calculation
  196. * Find the array index of the minimum value.
  197. *
  198. * In case of the maximum value appearing multiple times, the index of the first occurrence is returned.
  199. *
  200. * @param float[]|int[] $values
  201. *
  202. * @return int Index of the first occurrence of the minimum value
  203. *
  204. * @throws \LogicException if the array of values is empty - should never happen
  205. */
  206. private static function baseArgMin(array $values): int
  207. {
  208. $max = \min($values);
  209. foreach ($values as $i => $v) {
  210. if ($v === $max) {
  211. return $i;
  212. }
  213. }
  214. throw new \LogicException('argMin values is empty--should not happen');
  215. }
  216. /**
  217. * NonZero
  218. * Find the array indices of the scalar values that are non-zero.
  219. *
  220. * Considered 0:
  221. * int 0, -0
  222. * float 0.0, -0.0
  223. * string 0, -0, 0.0, -0.0
  224. * bool false
  225. *
  226. * Inspired by Python NumPy's nonzero
  227. *
  228. * @param float[]|int[] $values
  229. *
  230. * @return int[]
  231. */
  232. public static function nonZero(array $values): array
  233. {
  234. $indices = [];
  235. foreach ($values as $i => $v) {
  236. if (!\is_scalar($v)) {
  237. continue;
  238. }
  239. if ($v != 0) {
  240. $indices[] = $i;
  241. }
  242. }
  243. return $indices;
  244. }
  245. }