arm_mat_ldlt_f64.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_ldl_f64.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. #include <math.h>
  30. /// @private
  31. #define SWAP_ROWS_F64(A,i,j) \
  32. { \
  33. int w; \
  34. for(w=0;w < n; w++) \
  35. { \
  36. float64_t tmp; \
  37. tmp = A[i*n + w]; \
  38. A[i*n + w] = A[j*n + w];\
  39. A[j*n + w] = tmp; \
  40. } \
  41. }
  42. /// @private
  43. #define SWAP_COLS_F64(A,i,j) \
  44. { \
  45. int w; \
  46. for(w=0;w < n; w++) \
  47. { \
  48. float64_t tmp; \
  49. tmp = A[w*n + i]; \
  50. A[w*n + i] = A[w*n + j];\
  51. A[w*n + j] = tmp; \
  52. } \
  53. }
  54. /**
  55. @ingroup groupMatrix
  56. */
  57. /**
  58. @addtogroup MatrixChol
  59. @{
  60. */
  61. /**
  62. * @brief Floating-point LDL^t decomposition of positive semi-definite matrix.
  63. * @param[in] pSrc points to the instance of the input floating-point matrix structure.
  64. * @param[out] pl points to the instance of the output floating-point triangular matrix structure.
  65. * @param[out] pd points to the instance of the output floating-point diagonal matrix structure.
  66. * @param[out] pp points to the instance of the output floating-point permutation vector.
  67. * @return The function returns ARM_MATH_SIZE_MISMATCH, if the dimensions do not match.
  68. * @return execution status
  69. - \ref ARM_MATH_SUCCESS : Operation successful
  70. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  71. - \ref ARM_MATH_DECOMPOSITION_FAILURE : Input matrix cannot be decomposed
  72. * @par
  73. * Computes the LDL^t decomposition of a matrix A such that P A P^t = L D L^t.
  74. */
  75. arm_status arm_mat_ldlt_f64(
  76. const arm_matrix_instance_f64 * pSrc,
  77. arm_matrix_instance_f64 * pl,
  78. arm_matrix_instance_f64 * pd,
  79. uint16_t * pp)
  80. {
  81. arm_status status; /* status of matrix inverse */
  82. #ifdef ARM_MATH_MATRIX_CHECK
  83. /* Check for matrix mismatch condition */
  84. if ((pSrc->numRows != pSrc->numCols) ||
  85. (pl->numRows != pl->numCols) ||
  86. (pd->numRows != pd->numCols) ||
  87. (pl->numRows != pd->numRows) )
  88. {
  89. /* Set status as ARM_MATH_SIZE_MISMATCH */
  90. status = ARM_MATH_SIZE_MISMATCH;
  91. }
  92. else
  93. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  94. {
  95. const int n=pSrc->numRows;
  96. int fullRank = 1, diag,k;
  97. float64_t *pA;
  98. memset(pd->pData,0,sizeof(float64_t)*n*n);
  99. memcpy(pl->pData,pSrc->pData,n*n*sizeof(float64_t));
  100. pA = pl->pData;
  101. for(k=0;k < n; k++)
  102. {
  103. pp[k] = k;
  104. }
  105. for(k=0;k < n; k++)
  106. {
  107. /* Find pivot */
  108. float64_t m=F64_MIN,a;
  109. int w,r,j=k;
  110. for(r=k;r<n;r++)
  111. {
  112. if (pA[r*n+r] > m)
  113. {
  114. m = pA[r*n+r];
  115. j = r;
  116. }
  117. }
  118. if(j != k)
  119. {
  120. SWAP_ROWS_F64(pA,k,j);
  121. SWAP_COLS_F64(pA,k,j);
  122. }
  123. pp[k] = j;
  124. a = pA[k*n+k];
  125. if (fabs(a) < 1.0e-18)
  126. {
  127. fullRank = 0;
  128. break;
  129. }
  130. for(w=k+1;w<n;w++)
  131. {
  132. int x;
  133. for(x=k+1;x<n;x++)
  134. {
  135. pA[w*n+x] = pA[w*n+x] - pA[w*n+k] * pA[x*n+k] / a;
  136. }
  137. }
  138. for(w=k+1;w<n;w++)
  139. {
  140. pA[w*n+k] = pA[w*n+k] / a;
  141. }
  142. }
  143. diag=k;
  144. if (!fullRank)
  145. {
  146. diag--;
  147. {
  148. int row;
  149. for(row=0; row < n;row++)
  150. {
  151. int col;
  152. for(col=k; col < n;col++)
  153. {
  154. pl->pData[row*n+col]=0.0;
  155. }
  156. }
  157. }
  158. }
  159. {
  160. int row;
  161. for(row=0; row < n;row++)
  162. {
  163. int col;
  164. for(col=row+1; col < n;col++)
  165. {
  166. pl->pData[row*n+col] = 0.0;
  167. }
  168. }
  169. }
  170. {
  171. int d;
  172. for(d=0; d < diag;d++)
  173. {
  174. pd->pData[d*n+d] = pl->pData[d*n+d];
  175. pl->pData[d*n+d] = 1.0;
  176. }
  177. }
  178. status = ARM_MATH_SUCCESS;
  179. }
  180. /* Return to application */
  181. return (status);
  182. }
  183. /**
  184. @} end of MatrixChol group
  185. */