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