arm_mat_ldlt_f64.c 4.7 KB

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