fun9_pinv.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // File: pinv.cpp
  3. //
  4. // MATLAB Coder version : 5.2
  5. // C/C++ source code generated on : 24-Mar-2023 11:40:08
  6. //
  7. // Include Files
  8. #include "fun9_pinv.h"
  9. #include "rt_nonfinite.h"
  10. #include "fun9_svd.h"
  11. #include "coder_array.h"
  12. #include <cmath>
  13. #include <math.h>
  14. // Function Definitions
  15. //
  16. // Arguments : const ::coder::array<double, 2U> &A
  17. // ::coder::array<double, 2U> &X
  18. // Return Type : void
  19. //
  20. namespace coder {
  21. void eml_pinv(const ::coder::array<double, 2U> &A,
  22. ::coder::array<double, 2U> &X)
  23. {
  24. array<double, 2U> U;
  25. array<double, 2U> V;
  26. array<double, 1U> s;
  27. int br;
  28. int i;
  29. int m;
  30. int n;
  31. int nx;
  32. m = A.size(0);
  33. n = A.size(1);
  34. X.set_size(A.size(1), A.size(0));
  35. nx = A.size(0) * A.size(1);
  36. for (i = 0; i < nx; i++) {
  37. X[i] = 0.0;
  38. }
  39. if ((A.size(0) != 0) && (A.size(1) != 0)) {
  40. int ar;
  41. boolean_T p;
  42. nx = A.size(0) * A.size(1);
  43. p = true;
  44. for (ar = 0; ar < nx; ar++) {
  45. if ((!p) || (std::isinf(A[ar]) || std::isnan(A[ar]))) {
  46. p = false;
  47. }
  48. }
  49. if (!p) {
  50. X.set_size(A.size(1), A.size(0));
  51. nx = A.size(0) * A.size(1);
  52. for (i = 0; i < nx; i++) {
  53. X[i] = rtNaN;
  54. }
  55. } else {
  56. double absx;
  57. int r;
  58. internal::svd(A, U, s, V);
  59. absx = std::abs(s[0]);
  60. if ((!std::isinf(absx)) && (!std::isnan(absx))) {
  61. if (absx <= 2.2250738585072014E-308) {
  62. absx = 4.94065645841247E-324;
  63. } else {
  64. frexp(absx, &br);
  65. absx = std::ldexp(1.0, br - 53);
  66. }
  67. } else {
  68. absx = rtNaN;
  69. }
  70. absx *= static_cast<double>(A.size(0));
  71. r = -1;
  72. ar = 0;
  73. while ((ar <= n - 1) && (s[ar] > absx)) {
  74. r++;
  75. ar++;
  76. }
  77. if (r + 1 > 0) {
  78. int cr;
  79. int i1;
  80. int ic;
  81. nx = 1;
  82. for (br = 0; br <= r; br++) {
  83. absx = 1.0 / s[br];
  84. i = nx + n;
  85. i1 = i - 1;
  86. for (ar = nx; ar <= i1; ar++) {
  87. V[ar - 1] = absx * V[ar - 1];
  88. }
  89. nx = i;
  90. }
  91. nx = A.size(1) * (A.size(0) - 1);
  92. for (cr = 0; n < 0 ? cr >= nx : cr <= nx; cr += n) {
  93. i = cr + 1;
  94. i1 = cr + n;
  95. for (ic = i; ic <= i1; ic++) {
  96. X[ic - 1] = 0.0;
  97. }
  98. }
  99. br = 0;
  100. for (cr = 0; n < 0 ? cr >= nx : cr <= nx; cr += n) {
  101. ar = -1;
  102. br++;
  103. i = br + m * r;
  104. for (int ib{br}; m < 0 ? ib >= i : ib <= i; ib += m) {
  105. int i2;
  106. i1 = cr + 1;
  107. i2 = cr + n;
  108. for (ic = i1; ic <= i2; ic++) {
  109. X[ic - 1] = X[ic - 1] + U[ib - 1] * V[(ar + ic) - cr];
  110. }
  111. ar += n;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. } // namespace coder
  119. //
  120. // File trailer for pinv.cpp
  121. //
  122. // [EOF]
  123. //