arm_mat_ldlt_f32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_ldl_f32.c
  4. * Description: Floating-point LDL decomposition
  5. *
  6. *
  7. * Target Processor: Cortex-M cores
  8. * -------------------------------------------------------------------- */
  9. /*
  10. * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #include "dsp/matrix_functions.h"
  27. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  28. /// @private
  29. #define SWAP_ROWS(A,i,j) \
  30. { \
  31. int cnt = n; \
  32. \
  33. for(int w=0;w < n; w+=4) \
  34. { \
  35. f32x4_t tmpa,tmpb; \
  36. mve_pred16_t p0 = vctp32q(cnt); \
  37. \
  38. tmpa=vldrwq_z_f32(&A[i*n + w],p0);\
  39. tmpb=vldrwq_z_f32(&A[j*n + w],p0);\
  40. \
  41. vstrwq_p(&A[i*n + w], tmpb, p0); \
  42. vstrwq_p(&A[j*n + w], tmpa, p0); \
  43. \
  44. cnt -= 4; \
  45. } \
  46. }
  47. /// @private
  48. #define SWAP_COLS(A,i,j) \
  49. for(int w=0;w < n; w++) \
  50. { \
  51. float32_t tmp; \
  52. tmp = A[w*n + i]; \
  53. A[w*n + i] = A[w*n + j];\
  54. A[w*n + j] = tmp; \
  55. }
  56. /**
  57. @ingroup groupMatrix
  58. */
  59. /**
  60. @addtogroup MatrixChol
  61. @{
  62. */
  63. /**
  64. * @brief Floating-point LDL^t decomposition of positive semi-definite matrix.
  65. * @param[in] pSrc points to the instance of the input floating-point matrix structure.
  66. * @param[out] pl points to the instance of the output floating-point triangular matrix structure.
  67. * @param[out] pd points to the instance of the output floating-point diagonal matrix structure.
  68. * @param[out] pp points to the instance of the output floating-point permutation vector.
  69. * @return The function returns ARM_MATH_SIZE_MISMATCH, if the dimensions do not match.
  70. * @return execution status
  71. - \ref ARM_MATH_SUCCESS : Operation successful
  72. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  73. - \ref ARM_MATH_DECOMPOSITION_FAILURE : Input matrix cannot be decomposed
  74. * @par
  75. * Computes the LDL^t decomposition of a matrix A such that P A P^t = L D L^t.
  76. */
  77. arm_status arm_mat_ldlt_f32(
  78. const arm_matrix_instance_f32 * pSrc,
  79. arm_matrix_instance_f32 * pl,
  80. arm_matrix_instance_f32 * pd,
  81. uint16_t * pp)
  82. {
  83. arm_status status; /* status of matrix inverse */
  84. #ifdef ARM_MATH_MATRIX_CHECK
  85. /* Check for matrix mismatch condition */
  86. if ((pSrc->numRows != pSrc->numCols) ||
  87. (pl->numRows != pl->numCols) ||
  88. (pd->numRows != pd->numCols) ||
  89. (pp->numRows != pp->numCols) ||
  90. (pl->numRows != pl->numRows) )
  91. {
  92. /* Set status as ARM_MATH_SIZE_MISMATCH */
  93. status = ARM_MATH_SIZE_MISMATCH;
  94. }
  95. else
  96. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  97. {
  98. const int n=pSrc->numRows;
  99. int fullRank = 1, diag,k;
  100. float32_t *pA;
  101. memcpy(pl->pData,pSrc->pData,n*n*sizeof(float32_t));
  102. pA = pl->pData;
  103. int cnt = n;
  104. uint16x8_t vecP;
  105. for(int k=0;k < n; k+=8)
  106. {
  107. mve_pred16_t p0;
  108. p0 = vctp16q(cnt);
  109. vecP = vidupq_u16((uint16_t)k, 1);
  110. vstrhq_p(&pp[k], vecP, p0);
  111. cnt -= 8;
  112. }
  113. for(k=0;k < n; k++)
  114. {
  115. /* Find pivot */
  116. float32_t m=F32_MIN,a;
  117. int j=k;
  118. for(int r=k;r<n;r++)
  119. {
  120. if (pA[r*n+r] > m)
  121. {
  122. m = pA[r*n+r];
  123. j = r;
  124. }
  125. }
  126. if(j != k)
  127. {
  128. SWAP_ROWS(pA,k,j);
  129. SWAP_COLS(pA,k,j);
  130. }
  131. pp[k] = j;
  132. a = pA[k*n+k];
  133. if (fabs(a) < 1.0e-8)
  134. {
  135. fullRank = 0;
  136. break;
  137. }
  138. float32_t invA;
  139. invA = 1.0f / a;
  140. int32x4_t vecOffs;
  141. int w;
  142. vecOffs = vidupq_u32((uint32_t)0, 1);
  143. vecOffs = vmulq_n_s32(vecOffs,n);
  144. for(w=k+1; w<n; w+=4)
  145. {
  146. int cnt = n - k - 1;
  147. f32x4_t vecX;
  148. f32x4_t vecA;
  149. f32x4_t vecW0,vecW1, vecW2, vecW3;
  150. mve_pred16_t p0;
  151. vecW0 = vdupq_n_f32(pA[(w + 0)*n+k]);
  152. vecW1 = vdupq_n_f32(pA[(w + 1)*n+k]);
  153. vecW2 = vdupq_n_f32(pA[(w + 2)*n+k]);
  154. vecW3 = vdupq_n_f32(pA[(w + 3)*n+k]);
  155. for(int x=k+1;x<n;x += 4)
  156. {
  157. p0 = vctp32q(cnt);
  158. //pA[w*n+x] = pA[w*n+x] - pA[w*n+k] * (pA[x*n+k] * invA);
  159. vecX = vldrwq_gather_shifted_offset_z_f32(&pA[x*n+k], vecOffs, p0);
  160. vecX = vmulq_m_n_f32(vuninitializedq_f32(),vecX,invA,p0);
  161. vecA = vldrwq_z_f32(&pA[(w + 0)*n+x],p0);
  162. vecA = vfmsq_m(vecA, vecW0, vecX, p0);
  163. vstrwq_p(&pA[(w + 0)*n+x], vecA, p0);
  164. vecA = vldrwq_z_f32(&pA[(w + 1)*n+x],p0);
  165. vecA = vfmsq_m(vecA, vecW1, vecX, p0);
  166. vstrwq_p(&pA[(w + 1)*n+x], vecA, p0);
  167. vecA = vldrwq_z_f32(&pA[(w + 2)*n+x],p0);
  168. vecA = vfmsq_m(vecA, vecW2, vecX, p0);
  169. vstrwq_p(&pA[(w + 2)*n+x], vecA, p0);
  170. vecA = vldrwq_z_f32(&pA[(w + 3)*n+x],p0);
  171. vecA = vfmsq_m(vecA, vecW3, vecX, p0);
  172. vstrwq_p(&pA[(w + 3)*n+x], vecA, p0);
  173. cnt -= 4;
  174. }
  175. }
  176. for(; w<n; w++)
  177. {
  178. int cnt = n - k - 1;
  179. f32x4_t vecA,vecX,vecW;
  180. mve_pred16_t p0;
  181. vecW = vdupq_n_f32(pA[w*n+k]);
  182. for(int x=k+1;x<n;x += 4)
  183. {
  184. p0 = vctp32q(cnt);
  185. //pA[w*n+x] = pA[w*n+x] - pA[w*n+k] * (pA[x*n+k] * invA);
  186. vecA = vldrwq_z_f32(&pA[w*n+x],p0);
  187. vecX = vldrwq_gather_shifted_offset_z_f32(&pA[x*n+k], vecOffs, p0);
  188. vecX = vmulq_m_n_f32(vuninitializedq_f32(),vecX,invA,p0);
  189. vecA = vfmsq_m(vecA, vecW, vecX, p0);
  190. vstrwq_p(&pA[w*n+x], vecA, p0);
  191. cnt -= 4;
  192. }
  193. }
  194. for(int w=k+1;w<n;w++)
  195. {
  196. pA[w*n+k] = pA[w*n+k] * invA;
  197. }
  198. }
  199. diag=k;
  200. if (!fullRank)
  201. {
  202. diag--;
  203. for(int row=0; row < n;row++)
  204. {
  205. mve_pred16_t p0;
  206. int cnt= n-k;
  207. f32x4_t zero=vdupq_n_f32(0.0f);
  208. for(int col=k; col < n;col += 4)
  209. {
  210. p0 = vctp32q(cnt);
  211. vstrwq_p(&pl->pData[row*n+col], zero, p0);
  212. cnt -= 4;
  213. }
  214. }
  215. }
  216. for(int row=0; row < n;row++)
  217. {
  218. mve_pred16_t p0;
  219. int cnt= n-row-1;
  220. f32x4_t zero=vdupq_n_f32(0.0f);
  221. for(int col=row+1; col < n;col+=4)
  222. {
  223. p0 = vctp32q(cnt);
  224. vstrwq_p(&pl->pData[row*n+col], zero, p0);
  225. cnt -= 4;
  226. }
  227. }
  228. for(int d=0; d < diag;d++)
  229. {
  230. pd->pData[d*n+d] = pl->pData[d*n+d];
  231. pl->pData[d*n+d] = 1.0;
  232. }
  233. status = ARM_MATH_SUCCESS;
  234. }
  235. /* Return to application */
  236. return (status);
  237. }
  238. #else
  239. /// @private
  240. #define SWAP_ROWS(A,i,j) \
  241. for(int w=0;w < n; w++) \
  242. { \
  243. float32_t tmp; \
  244. tmp = A[i*n + w]; \
  245. A[i*n + w] = A[j*n + w];\
  246. A[j*n + w] = tmp; \
  247. }
  248. /// @private
  249. #define SWAP_COLS(A,i,j) \
  250. for(int w=0;w < n; w++) \
  251. { \
  252. float32_t tmp; \
  253. tmp = A[w*n + i]; \
  254. A[w*n + i] = A[w*n + j];\
  255. A[w*n + j] = tmp; \
  256. }
  257. /**
  258. @ingroup groupMatrix
  259. */
  260. /**
  261. @addtogroup MatrixChol
  262. @{
  263. */
  264. /**
  265. * @brief Floating-point LDL^t decomposition of positive semi-definite matrix.
  266. * @param[in] pSrc points to the instance of the input floating-point matrix structure.
  267. * @param[out] pl points to the instance of the output floating-point triangular matrix structure.
  268. * @param[out] pd points to the instance of the output floating-point diagonal matrix structure.
  269. * @param[out] pp points to the instance of the output floating-point permutation vector.
  270. * @return The function returns ARM_MATH_SIZE_MISMATCH, if the dimensions do not match.
  271. * @return execution status
  272. - \ref ARM_MATH_SUCCESS : Operation successful
  273. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  274. - \ref ARM_MATH_DECOMPOSITION_FAILURE : Input matrix cannot be decomposed
  275. * @par
  276. * Computes the LDL^t decomposition of a matrix A such that P A P^t = L D L^t.
  277. */
  278. arm_status arm_mat_ldlt_f32(
  279. const arm_matrix_instance_f32 * pSrc,
  280. arm_matrix_instance_f32 * pl,
  281. arm_matrix_instance_f32 * pd,
  282. uint16_t * pp)
  283. {
  284. arm_status status; /* status of matrix inverse */
  285. #ifdef ARM_MATH_MATRIX_CHECK
  286. /* Check for matrix mismatch condition */
  287. if ((pSrc->numRows != pSrc->numCols) ||
  288. (pl->numRows != pl->numCols) ||
  289. (pd->numRows != pd->numCols) ||
  290. (pp->numRows != pp->numCols) ||
  291. (pl->numRows != pl->numRows) )
  292. {
  293. /* Set status as ARM_MATH_SIZE_MISMATCH */
  294. status = ARM_MATH_SIZE_MISMATCH;
  295. }
  296. else
  297. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  298. {
  299. const int n=pSrc->numRows;
  300. int fullRank = 1, diag,k;
  301. float32_t *pA;
  302. memcpy(pl->pData,pSrc->pData,n*n*sizeof(float32_t));
  303. pA = pl->pData;
  304. for(int k=0;k < n; k++)
  305. {
  306. pp[k] = k;
  307. }
  308. for(k=0;k < n; k++)
  309. {
  310. /* Find pivot */
  311. float32_t m=F32_MIN,a;
  312. int j=k;
  313. for(int r=k;r<n;r++)
  314. {
  315. if (pA[r*n+r] > m)
  316. {
  317. m = pA[r*n+r];
  318. j = r;
  319. }
  320. }
  321. if(j != k)
  322. {
  323. SWAP_ROWS(pA,k,j);
  324. SWAP_COLS(pA,k,j);
  325. }
  326. pp[k] = j;
  327. a = pA[k*n+k];
  328. if (fabs(a) < 1.0e-8)
  329. {
  330. fullRank = 0;
  331. break;
  332. }
  333. for(int w=k+1;w<n;w++)
  334. {
  335. for(int x=k+1;x<n;x++)
  336. {
  337. pA[w*n+x] = pA[w*n+x] - pA[w*n+k] * pA[x*n+k] / a;
  338. }
  339. }
  340. for(int w=k+1;w<n;w++)
  341. {
  342. pA[w*n+k] = pA[w*n+k] / a;
  343. }
  344. }
  345. diag=k;
  346. if (!fullRank)
  347. {
  348. diag--;
  349. for(int row=0; row < n;row++)
  350. {
  351. for(int col=k; col < n;col++)
  352. {
  353. pl->pData[row*n+col]=0.0;
  354. }
  355. }
  356. }
  357. for(int row=0; row < n;row++)
  358. {
  359. for(int col=row+1; col < n;col++)
  360. {
  361. pl->pData[row*n+col] = 0.0;
  362. }
  363. }
  364. for(int d=0; d < diag;d++)
  365. {
  366. pd->pData[d*n+d] = pl->pData[d*n+d];
  367. pl->pData[d*n+d] = 1.0;
  368. }
  369. status = ARM_MATH_SUCCESS;
  370. }
  371. /* Return to application */
  372. return (status);
  373. }
  374. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  375. /**
  376. @} end of MatrixChol group
  377. */