arm_mat_inverse_f64.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_inverse_f64.c
  4. * Description: Floating-point matrix inverse
  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. /**
  31. @ingroup groupMatrix
  32. */
  33. /**
  34. @addtogroup MatrixInv
  35. @{
  36. */
  37. /**
  38. @brief Floating-point (64 bit) matrix inverse.
  39. @param[in] pSrc points to input matrix structure. The source matrix is modified by the function.
  40. @param[out] pDst points to output matrix structure
  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_SINGULAR : Input matrix is found to be singular (non-invertible)
  45. */
  46. arm_status arm_mat_inverse_f64(
  47. const arm_matrix_instance_f64 * pSrc,
  48. arm_matrix_instance_f64 * pDst)
  49. {
  50. float64_t *pIn = pSrc->pData; /* input data matrix pointer */
  51. float64_t *pOut = pDst->pData; /* output data matrix pointer */
  52. float64_t *pTmp;
  53. uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */
  54. uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */
  55. float64_t pivot = 0.0, newPivot=0.0; /* Temporary input values */
  56. uint32_t selectedRow,pivotRow,i, rowNb, rowCnt, flag = 0U, j,column; /* loop counters */
  57. arm_status status; /* status of matrix inverse */
  58. #ifdef ARM_MATH_MATRIX_CHECK
  59. /* Check for matrix mismatch condition */
  60. if ((pSrc->numRows != pSrc->numCols) ||
  61. (pDst->numRows != pDst->numCols) ||
  62. (pSrc->numRows != pDst->numRows) )
  63. {
  64. /* Set status as ARM_MATH_SIZE_MISMATCH */
  65. status = ARM_MATH_SIZE_MISMATCH;
  66. }
  67. else
  68. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  69. {
  70. /*--------------------------------------------------------------------------------------------------------------
  71. * Matrix Inverse can be solved using elementary row operations.
  72. *
  73. * Gauss-Jordan Method:
  74. *
  75. * 1. First combine the identity matrix and the input matrix separated by a bar to form an
  76. * augmented matrix as follows:
  77. * _ _ _ _
  78. * | a11 a12 | 1 0 | | X11 X12 |
  79. * | | | = | |
  80. * |_ a21 a22 | 0 1 _| |_ X21 X21 _|
  81. *
  82. * 2. In our implementation, pDst Matrix is used as identity matrix.
  83. *
  84. * 3. Begin with the first row. Let i = 1.
  85. *
  86. * 4. Check to see if the pivot for row i is zero.
  87. * The pivot is the element of the main diagonal that is on the current row.
  88. * For instance, if working with row i, then the pivot element is aii.
  89. * If the pivot is zero, exchange that row with a row below it that does not
  90. * contain a zero in column i. If this is not possible, then an inverse
  91. * to that matrix does not exist.
  92. *
  93. * 5. Divide every element of row i by the pivot.
  94. *
  95. * 6. For every row below and row i, replace that row with the sum of that row and
  96. * a multiple of row i so that each new element in column i below row i is zero.
  97. *
  98. * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros
  99. * for every element below and above the main diagonal.
  100. *
  101. * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc).
  102. * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst).
  103. *----------------------------------------------------------------------------------------------------------------*/
  104. /* Working pointer for destination matrix */
  105. pTmp = pOut;
  106. /* Loop over the number of rows */
  107. rowCnt = numRows;
  108. /* Making the destination matrix as identity matrix */
  109. while (rowCnt > 0U)
  110. {
  111. /* Writing all zeroes in lower triangle of the destination matrix */
  112. j = numRows - rowCnt;
  113. while (j > 0U)
  114. {
  115. *pTmp++ = 0.0;
  116. j--;
  117. }
  118. /* Writing all ones in the diagonal of the destination matrix */
  119. *pTmp++ = 1.0;
  120. /* Writing all zeroes in upper triangle of the destination matrix */
  121. j = rowCnt - 1U;
  122. while (j > 0U)
  123. {
  124. *pTmp++ = 0.0;
  125. j--;
  126. }
  127. /* Decrement loop counter */
  128. rowCnt--;
  129. }
  130. /* Loop over the number of columns of the input matrix.
  131. All the elements in each column are processed by the row operations */
  132. /* Index modifier to navigate through the columns */
  133. for(column = 0U; column < numCols; column++)
  134. {
  135. /* Check if the pivot element is zero..
  136. * If it is zero then interchange the row with non zero row below.
  137. * If there is no non zero element to replace in the rows below,
  138. * then the matrix is Singular. */
  139. pivotRow = column;
  140. /* Temporary variable to hold the pivot value */
  141. pTmp = ELEM(pSrc,column,column) ;
  142. pivot = *pTmp;
  143. selectedRow = column;
  144. /* Loop over the number rows present below */
  145. for (rowNb = column+1; rowNb < numRows; rowNb++)
  146. {
  147. /* Update the input and destination pointers */
  148. pTmp = ELEM(pSrc,rowNb,column);
  149. newPivot = *pTmp;
  150. if (fabs(newPivot) > fabs(pivot))
  151. {
  152. selectedRow = rowNb;
  153. pivot = newPivot;
  154. }
  155. }
  156. /* Check if there is a non zero pivot element to
  157. * replace in the rows below */
  158. if ((pivot != 0.0) && (selectedRow != column))
  159. {
  160. /* Loop over number of columns
  161. * to the right of the pilot element */
  162. SWAP_ROWS_F64(pSrc,column, pivotRow,selectedRow);
  163. SWAP_ROWS_F64(pDst,0, pivotRow,selectedRow);
  164. /* Flag to indicate whether exchange is done or not */
  165. flag = 1U;
  166. }
  167. /* Update the status if the matrix is singular */
  168. if ((flag != 1U) && (pivot == 0.0))
  169. {
  170. return ARM_MATH_SINGULAR;
  171. }
  172. /* Pivot element of the row */
  173. pivot = 1.0 / pivot;
  174. SCALE_ROW_F64(pSrc,column,pivot,pivotRow);
  175. SCALE_ROW_F64(pDst,0,pivot,pivotRow);
  176. /* Replace the rows with the sum of that row and a multiple of row i
  177. * so that each new element in column i above row i is zero.*/
  178. rowNb = 0;
  179. for (;rowNb < pivotRow; rowNb++)
  180. {
  181. pTmp = ELEM(pSrc,rowNb,column) ;
  182. pivot = *pTmp;
  183. MAS_ROW_F64(column,pSrc,rowNb,pivot,pSrc,pivotRow);
  184. MAS_ROW_F64(0 ,pDst,rowNb,pivot,pDst,pivotRow);
  185. }
  186. for (rowNb = pivotRow + 1; rowNb < numRows; rowNb++)
  187. {
  188. pTmp = ELEM(pSrc,rowNb,column) ;
  189. pivot = *pTmp;
  190. MAS_ROW_F64(column,pSrc,rowNb,pivot,pSrc,pivotRow);
  191. MAS_ROW_F64(0 ,pDst,rowNb,pivot,pDst,pivotRow);
  192. }
  193. }
  194. /* Set status as ARM_MATH_SUCCESS */
  195. status = ARM_MATH_SUCCESS;
  196. if ((flag != 1U) && (pivot == 0.0))
  197. {
  198. pIn = pSrc->pData;
  199. for (i = 0; i < numRows * numCols; i++)
  200. {
  201. if (pIn[i] != 0.0)
  202. break;
  203. }
  204. if (i == numRows * numCols)
  205. status = ARM_MATH_SINGULAR;
  206. }
  207. }
  208. /* Return to application */
  209. return (status);
  210. }
  211. /**
  212. @} end of MatrixInv group
  213. */