arm_mat_cholesky_f64.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_cholesky_f64.c
  4. * Description: Floating-point Cholesky 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. /**
  30. @ingroup groupMatrix
  31. */
  32. /**
  33. @addtogroup MatrixChol
  34. @{
  35. */
  36. /**
  37. * @brief Floating-point Cholesky decomposition of positive-definite matrix.
  38. * @param[in] pSrc points to the instance of the input floating-point matrix structure.
  39. * @param[out] pDst points to the instance of the output floating-point matrix structure.
  40. * @return The function returns ARM_MATH_SIZE_MISMATCH, if the dimensions do not match.
  41. * @return execution status
  42. - \ref ARM_MATH_SUCCESS : Operation successful
  43. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  44. - \ref ARM_MATH_DECOMPOSITION_FAILURE : Input matrix cannot be decomposed
  45. * @par
  46. * If the matrix is ill conditioned or only semi-definite, then it is better using the LDL^t decomposition.
  47. * The decomposition of A is returning a lower triangular matrix U such that A = U U^t
  48. */
  49. arm_status arm_mat_cholesky_f64(
  50. const arm_matrix_instance_f64 * pSrc,
  51. arm_matrix_instance_f64 * pDst)
  52. {
  53. arm_status status; /* status of matrix inverse */
  54. #ifdef ARM_MATH_MATRIX_CHECK
  55. /* Check for matrix mismatch condition */
  56. if ((pSrc->numRows != pSrc->numCols) ||
  57. (pDst->numRows != pDst->numCols) ||
  58. (pSrc->numRows != pDst->numRows) )
  59. {
  60. /* Set status as ARM_MATH_SIZE_MISMATCH */
  61. status = ARM_MATH_SIZE_MISMATCH;
  62. }
  63. else
  64. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  65. {
  66. int i,j,k;
  67. int n = pSrc->numRows;
  68. float64_t invSqrtVj;
  69. float64_t *pA,*pG;
  70. pA = pSrc->pData;
  71. pG = pDst->pData;
  72. for(i=0 ; i < n ; i++)
  73. {
  74. for(j=i ; j < n ; j++)
  75. {
  76. pG[j * n + i] = pA[j * n + i];
  77. for(k=0; k < i ; k++)
  78. {
  79. pG[j * n + i] = pG[j * n + i] - pG[i * n + k] * pG[j * n + k];
  80. }
  81. }
  82. if (pG[i * n + i] <= 0.0)
  83. {
  84. return(ARM_MATH_DECOMPOSITION_FAILURE);
  85. }
  86. invSqrtVj = 1.0/sqrt(pG[i * n + i]);
  87. for(j=i ; j < n ; j++)
  88. {
  89. pG[j * n + i] = pG[j * n + i] * invSqrtVj ;
  90. }
  91. }
  92. status = ARM_MATH_SUCCESS;
  93. }
  94. /* Return to application */
  95. return (status);
  96. }
  97. /**
  98. @} end of MatrixChol group
  99. */