arm_mat_ldlt_f64.c 4.7 KB

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