fun10_histcounts.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // Academic License - for use in teaching, academic research, and meeting
  3. // course requirements at degree granting institutions only. Not for
  4. // government, commercial, or other organizational use.
  5. // File: histcounts.cpp
  6. //
  7. // MATLAB Coder version : 5.3
  8. // C/C++ source code generated on : 27-Mar-2023 15:34:32
  9. //
  10. // Include Files
  11. #include "fun10_histcounts.h"
  12. #include "rt_nonfinite.h"
  13. #include "coder_array.h"
  14. #include <cmath>
  15. #include <cstring>
  16. #include <math.h>
  17. // Function Declarations
  18. static double rt_powd_snf(double u0, double u1);
  19. // Function Definitions
  20. //
  21. // Arguments : double u0
  22. // double u1
  23. // Return Type : double
  24. //
  25. static double rt_powd_snf(double u0, double u1)
  26. {
  27. double y;
  28. if (std::isnan(u0) || std::isnan(u1)) {
  29. y = rtNaN;
  30. } else {
  31. double d;
  32. double d1;
  33. d = std::abs(u0);
  34. d1 = std::abs(u1);
  35. if (std::isinf(u1)) {
  36. if (d == 1.0) {
  37. y = 1.0;
  38. } else if (d > 1.0) {
  39. if (u1 > 0.0) {
  40. y = rtInf;
  41. } else {
  42. y = 0.0;
  43. }
  44. } else if (u1 > 0.0) {
  45. y = 0.0;
  46. } else {
  47. y = rtInf;
  48. }
  49. } else if (d1 == 0.0) {
  50. y = 1.0;
  51. } else if (d1 == 1.0) {
  52. if (u1 > 0.0) {
  53. y = u0;
  54. } else {
  55. y = 1.0 / u0;
  56. }
  57. } else if (u1 == 2.0) {
  58. y = u0 * u0;
  59. } else if ((u1 == 0.5) && (u0 >= 0.0)) {
  60. y = std::sqrt(u0);
  61. } else if ((u0 < 0.0) && (u1 > std::floor(u1))) {
  62. y = rtNaN;
  63. } else {
  64. y = std::pow(u0, u1);
  65. }
  66. }
  67. return y;
  68. }
  69. //
  70. // Arguments : const ::coder::array<double, 2U> &x
  71. // double n_data[]
  72. // int n_size[2]
  73. // double edges_data[]
  74. // int edges_size[2]
  75. // Return Type : void
  76. //
  77. namespace coder {
  78. void histcounts(const ::coder::array<double, 2U> &x, double n_data[],
  79. int n_size[2], double edges_data[], int edges_size[2])
  80. {
  81. double HighLimit;
  82. double LowLimit;
  83. double RawBinWidth_tmp;
  84. double epsxScale;
  85. double leftEdge;
  86. double xScale;
  87. int ni_data[100];
  88. int high_i;
  89. int i;
  90. int k;
  91. int low_i;
  92. int low_ip1;
  93. int mid_i;
  94. int nx;
  95. nx = x.size(1);
  96. k = 0;
  97. while ((k + 1 <= nx) && (std::isinf(x[k]) || std::isnan(x[k]))) {
  98. k++;
  99. }
  100. if (k + 1 > x.size(1)) {
  101. LowLimit = 0.0;
  102. low_i = 0;
  103. } else {
  104. LowLimit = x[k];
  105. low_i = 1;
  106. }
  107. HighLimit = LowLimit;
  108. i = k + 2;
  109. for (low_ip1 = i; low_ip1 <= nx; low_ip1++) {
  110. RawBinWidth_tmp = x[low_ip1 - 1];
  111. if ((!std::isinf(RawBinWidth_tmp)) && (!std::isnan(RawBinWidth_tmp))) {
  112. if (RawBinWidth_tmp < LowLimit) {
  113. LowLimit = RawBinWidth_tmp;
  114. } else if (RawBinWidth_tmp > HighLimit) {
  115. HighLimit = RawBinWidth_tmp;
  116. }
  117. low_i++;
  118. }
  119. }
  120. if (low_i > 0) {
  121. boolean_T b;
  122. boolean_T b1;
  123. xScale = std::fmax(std::abs(LowLimit), std::abs(HighLimit));
  124. b = !std::isinf(xScale);
  125. b1 = !std::isnan(xScale);
  126. if (b && b1) {
  127. if (xScale <= 2.2250738585072014E-308) {
  128. epsxScale = 4.94065645841247E-324;
  129. } else {
  130. frexp(xScale, &high_i);
  131. epsxScale = std::ldexp(1.0, high_i - 53);
  132. }
  133. } else {
  134. epsxScale = rtNaN;
  135. }
  136. RawBinWidth_tmp = HighLimit - LowLimit;
  137. leftEdge = std::fmax(RawBinWidth_tmp / 100.0, epsxScale);
  138. if (RawBinWidth_tmp >
  139. std::fmax(std::sqrt(epsxScale), 2.2250738585072014E-308)) {
  140. xScale = rt_powd_snf(10.0, std::floor(std::log10(leftEdge)));
  141. epsxScale = xScale * std::floor(leftEdge / xScale);
  142. leftEdge = std::fmax(
  143. std::fmin(epsxScale * std::floor(LowLimit / epsxScale), LowLimit),
  144. -1.7976931348623157E+308);
  145. xScale = (HighLimit - leftEdge) / 100.0;
  146. epsxScale = rt_powd_snf(
  147. 10.0, std::floor(std::log10((HighLimit - leftEdge) / 99.0 - xScale)));
  148. epsxScale *= std::ceil(xScale / epsxScale);
  149. xScale = std::fmin(std::fmax(leftEdge + 100.0 * epsxScale, HighLimit),
  150. 1.7976931348623157E+308);
  151. } else {
  152. if (b && b1) {
  153. if (xScale <= 2.2250738585072014E-308) {
  154. xScale = 4.94065645841247E-324;
  155. } else {
  156. frexp(xScale, &mid_i);
  157. xScale = std::ldexp(1.0, mid_i - 53);
  158. }
  159. } else {
  160. xScale = rtNaN;
  161. }
  162. xScale = std::fmax(1.0, std::ceil(100.0 * xScale));
  163. leftEdge = std::floor(2.0 * (LowLimit - xScale / 4.0)) / 2.0;
  164. xScale = std::ceil(2.0 * (HighLimit + xScale / 4.0)) / 2.0;
  165. epsxScale = (xScale - leftEdge) / 100.0;
  166. }
  167. if ((!std::isinf(epsxScale)) && (!std::isnan(epsxScale))) {
  168. edges_size[0] = 1;
  169. edges_size[1] = 101;
  170. std::memset(&edges_data[0], 0, 101U * sizeof(double));
  171. edges_data[0] = leftEdge;
  172. for (low_i = 0; low_i < 99; low_i++) {
  173. edges_data[low_i + 1] =
  174. leftEdge + (static_cast<double>(low_i) + 1.0) * epsxScale;
  175. }
  176. edges_data[100] = xScale;
  177. } else {
  178. edges_size[0] = 1;
  179. edges_size[1] = 101;
  180. edges_data[100] = xScale;
  181. edges_data[0] = leftEdge;
  182. if (leftEdge == -xScale) {
  183. xScale /= 100.0;
  184. for (k = 0; k < 99; k++) {
  185. edges_data[k + 1] =
  186. (2.0 * (static_cast<double>(k) + 2.0) - 102.0) * xScale;
  187. }
  188. edges_data[50] = 0.0;
  189. } else if (((leftEdge < 0.0) != (xScale < 0.0)) &&
  190. ((std::abs(leftEdge) > 8.9884656743115785E+307) ||
  191. (std::abs(xScale) > 8.9884656743115785E+307))) {
  192. epsxScale = leftEdge / 100.0;
  193. xScale /= 100.0;
  194. for (k = 0; k < 99; k++) {
  195. edges_data[k + 1] =
  196. (leftEdge + xScale * (static_cast<double>(k) + 1.0)) -
  197. epsxScale * (static_cast<double>(k) + 1.0);
  198. }
  199. } else {
  200. epsxScale = (xScale - leftEdge) / 100.0;
  201. for (k = 0; k < 99; k++) {
  202. edges_data[k + 1] =
  203. leftEdge + (static_cast<double>(k) + 1.0) * epsxScale;
  204. }
  205. }
  206. }
  207. } else {
  208. edges_size[0] = 1;
  209. edges_size[1] = 101;
  210. for (k = 0; k < 101; k++) {
  211. edges_data[k] = k;
  212. }
  213. }
  214. std::memset(&ni_data[0], 0, 100U * sizeof(int));
  215. nx = x.size(1);
  216. leftEdge = edges_data[0];
  217. xScale = edges_data[1] - edges_data[0];
  218. for (k = 0; k < nx; k++) {
  219. RawBinWidth_tmp = x[k];
  220. if ((RawBinWidth_tmp >= leftEdge) && (RawBinWidth_tmp <= edges_data[100])) {
  221. boolean_T guard1{false};
  222. epsxScale = std::ceil((RawBinWidth_tmp - leftEdge) / xScale);
  223. guard1 = false;
  224. if ((epsxScale >= 1.0) && (epsxScale < 101.0)) {
  225. i = static_cast<int>(epsxScale);
  226. if ((RawBinWidth_tmp >= edges_data[i - 1]) &&
  227. (RawBinWidth_tmp < edges_data[i])) {
  228. ni_data[i - 1]++;
  229. } else {
  230. guard1 = true;
  231. }
  232. } else {
  233. guard1 = true;
  234. }
  235. if (guard1) {
  236. low_i = 1;
  237. low_ip1 = 2;
  238. high_i = 101;
  239. while (high_i > low_ip1) {
  240. mid_i = (low_i >> 1) + (high_i >> 1);
  241. if (((low_i & 1) == 1) && ((high_i & 1) == 1)) {
  242. mid_i++;
  243. }
  244. if (x[k] >= edges_data[mid_i - 1]) {
  245. low_i = mid_i;
  246. low_ip1 = mid_i + 1;
  247. } else {
  248. high_i = mid_i;
  249. }
  250. }
  251. ni_data[low_i - 1]++;
  252. }
  253. }
  254. }
  255. n_size[0] = 1;
  256. n_size[1] = 100;
  257. for (i = 0; i < 100; i++) {
  258. n_data[i] = ni_data[i];
  259. }
  260. }
  261. } // namespace coder
  262. //
  263. // File trailer for histcounts.cpp
  264. //
  265. // [EOF]
  266. //