arm_mat_inverse_f16.c 8.5 KB

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