arm_mat_ldlt_f64.c 4.5 KB

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