arm_mat_inverse_f32.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_inverse_f32.c
  4. * Description: Floating-point matrix inverse
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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 "arm_math.h"
  29. /**
  30. @ingroup groupMatrix
  31. */
  32. /**
  33. @defgroup MatrixInv Matrix Inverse
  34. Computes the inverse of a matrix.
  35. The inverse is defined only if the input matrix is square and non-singular (the determinant is non-zero).
  36. The function checks that the input and output matrices are square and of the same size.
  37. Matrix inversion is numerically sensitive and the CMSIS DSP library only supports matrix
  38. inversion of floating-point matrices.
  39. @par Algorithm
  40. The Gauss-Jordan method is used to find the inverse.
  41. The algorithm performs a sequence of elementary row-operations until it
  42. reduces the input matrix to an identity matrix. Applying the same sequence
  43. of elementary row-operations to an identity matrix yields the inverse matrix.
  44. If the input matrix is singular, then the algorithm terminates and returns error status
  45. <code>ARM_MATH_SINGULAR</code>.
  46. \image html MatrixInverse.gif "Matrix Inverse of a 3 x 3 matrix using Gauss-Jordan Method"
  47. */
  48. /**
  49. @addtogroup MatrixInv
  50. @{
  51. */
  52. /**
  53. @brief Floating-point matrix inverse.
  54. @param[in] pSrc points to input matrix structure
  55. @param[out] pDst points to output matrix structure
  56. @return execution status
  57. - \ref ARM_MATH_SUCCESS : Operation successful
  58. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  59. - \ref ARM_MATH_SINGULAR : Input matrix is found to be singular (non-invertible)
  60. */
  61. #if defined(ARM_MATH_NEON)
  62. arm_status arm_mat_inverse_f32(
  63. const arm_matrix_instance_f32 * pSrc,
  64. arm_matrix_instance_f32 * pDst)
  65. {
  66. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  67. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  68. float32_t *pInT1, *pInT2; /* Temporary input data matrix pointer */
  69. float32_t *pOutT1, *pOutT2; /* Temporary output data matrix pointer */
  70. float32_t *pPivotRowIn, *pPRT_in, *pPivotRowDst, *pPRT_pDst; /* Temporary input and output data matrix pointer */
  71. uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */
  72. uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */
  73. float32_t maxC; /* maximum value in the column */
  74. float32_t Xchg, in = 0.0f, in1; /* Temporary input values */
  75. uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */
  76. arm_status status; /* status of matrix inverse */
  77. float32x4_t vec1;
  78. float32x4_t vec2;
  79. float32x4_t tmpV;
  80. #ifdef ARM_MATH_MATRIX_CHECK
  81. /* Check for matrix mismatch condition */
  82. if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols)
  83. || (pSrc->numRows != pDst->numRows))
  84. {
  85. /* Set status as ARM_MATH_SIZE_MISMATCH */
  86. status = ARM_MATH_SIZE_MISMATCH;
  87. }
  88. else
  89. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  90. {
  91. /*--------------------------------------------------------------------------------------------------------------
  92. * Matrix Inverse can be solved using elementary row operations.
  93. *
  94. * Gauss-Jordan Method:
  95. *
  96. * 1. First combine the identity matrix and the input matrix separated by a bar to form an
  97. * augmented matrix as follows:
  98. * _ _ _ _
  99. * | a11 a12 | 1 0 | | X11 X12 |
  100. * | | | = | |
  101. * |_ a21 a22 | 0 1 _| |_ X21 X21 _|
  102. *
  103. * 2. In our implementation, pDst Matrix is used as identity matrix.
  104. *
  105. * 3. Begin with the first row. Let i = 1.
  106. *
  107. * 4. Check to see if the pivot for column i is the greatest of the column.
  108. * The pivot is the element of the main diagonal that is on the current row.
  109. * For instance, if working with row i, then the pivot element is aii.
  110. * If the pivot is not the most significant of the columns, exchange that row with a row
  111. * below it that does contain the most significant value in column i. If the most
  112. * significant value of the column is zero, then an inverse to that matrix does not exist.
  113. * The most significant value of the column is the absolute maximum.
  114. *
  115. * 5. Divide every element of row i by the pivot.
  116. *
  117. * 6. For every row below and row i, replace that row with the sum of that row and
  118. * a multiple of row i so that each new element in column i below row i is zero.
  119. *
  120. * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros
  121. * for every element below and above the main diagonal.
  122. *
  123. * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc).
  124. * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst).
  125. *----------------------------------------------------------------------------------------------------------------*/
  126. /* Working pointer for destination matrix */
  127. pOutT1 = pOut;
  128. /* Loop over the number of rows */
  129. rowCnt = numRows;
  130. /* Making the destination matrix as identity matrix */
  131. while (rowCnt > 0U)
  132. {
  133. /* Writing all zeroes in lower triangle of the destination matrix */
  134. j = numRows - rowCnt;
  135. while (j > 0U)
  136. {
  137. *pOutT1++ = 0.0f;
  138. j--;
  139. }
  140. /* Writing all ones in the diagonal of the destination matrix */
  141. *pOutT1++ = 1.0f;
  142. /* Writing all zeroes in upper triangle of the destination matrix */
  143. j = rowCnt - 1U;
  144. while (j > 0U)
  145. {
  146. *pOutT1++ = 0.0f;
  147. j--;
  148. }
  149. /* Decrement the loop counter */
  150. rowCnt--;
  151. }
  152. /* Loop over the number of columns of the input matrix.
  153. All the elements in each column are processed by the row operations */
  154. loopCnt = numCols;
  155. /* Index modifier to navigate through the columns */
  156. l = 0U;
  157. while (loopCnt > 0U)
  158. {
  159. /* Check if the pivot element is zero..
  160. * If it is zero then interchange the row with non zero row below.
  161. * If there is no non zero element to replace in the rows below,
  162. * then the matrix is Singular. */
  163. /* Working pointer for the input matrix that points
  164. * to the pivot element of the particular row */
  165. pInT1 = pIn + (l * numCols);
  166. /* Working pointer for the destination matrix that points
  167. * to the pivot element of the particular row */
  168. pOutT1 = pOut + (l * numCols);
  169. /* Temporary variable to hold the pivot value */
  170. in = *pInT1;
  171. /* Grab the most significant value from column l */
  172. maxC = 0;
  173. for (i = l; i < numRows; i++)
  174. {
  175. maxC = *pInT1 > 0 ? (*pInT1 > maxC ? *pInT1 : maxC) : (-*pInT1 > maxC ? -*pInT1 : maxC);
  176. pInT1 += numCols;
  177. }
  178. /* Update the status if the matrix is singular */
  179. if (maxC == 0.0f)
  180. {
  181. return ARM_MATH_SINGULAR;
  182. }
  183. /* Restore pInT1 */
  184. pInT1 = pIn;
  185. /* Destination pointer modifier */
  186. k = 1U;
  187. /* Check if the pivot element is the most significant of the column */
  188. if ( (in > 0.0f ? in : -in) != maxC)
  189. {
  190. /* Loop over the number rows present below */
  191. i = numRows - (l + 1U);
  192. while (i > 0U)
  193. {
  194. /* Update the input and destination pointers */
  195. pInT2 = pInT1 + (numCols * l);
  196. pOutT2 = pOutT1 + (numCols * k);
  197. /* Look for the most significant element to
  198. * replace in the rows below */
  199. if ((*pInT2 > 0.0f ? *pInT2: -*pInT2) == maxC)
  200. {
  201. /* Loop over number of columns
  202. * to the right of the pilot element */
  203. j = numCols - l;
  204. while (j > 0U)
  205. {
  206. /* Exchange the row elements of the input matrix */
  207. Xchg = *pInT2;
  208. *pInT2++ = *pInT1;
  209. *pInT1++ = Xchg;
  210. /* Decrement the loop counter */
  211. j--;
  212. }
  213. /* Loop over number of columns of the destination matrix */
  214. j = numCols;
  215. while (j > 0U)
  216. {
  217. /* Exchange the row elements of the destination matrix */
  218. Xchg = *pOutT2;
  219. *pOutT2++ = *pOutT1;
  220. *pOutT1++ = Xchg;
  221. /* Decrement the loop counter */
  222. j--;
  223. }
  224. /* Flag to indicate whether exchange is done or not */
  225. flag = 1U;
  226. /* Break after exchange is done */
  227. break;
  228. }
  229. /* Update the destination pointer modifier */
  230. k++;
  231. /* Decrement the loop counter */
  232. i--;
  233. }
  234. }
  235. /* Update the status if the matrix is singular */
  236. if ((flag != 1U) && (in == 0.0f))
  237. {
  238. return ARM_MATH_SINGULAR;
  239. }
  240. /* Points to the pivot row of input and destination matrices */
  241. pPivotRowIn = pIn + (l * numCols);
  242. pPivotRowDst = pOut + (l * numCols);
  243. /* Temporary pointers to the pivot row pointers */
  244. pInT1 = pPivotRowIn;
  245. pInT2 = pPivotRowDst;
  246. /* Pivot element of the row */
  247. in = *pPivotRowIn;
  248. tmpV = vdupq_n_f32(1.0/in);
  249. /* Loop over number of columns
  250. * to the right of the pilot element */
  251. j = (numCols - l) >> 2;
  252. while (j > 0U)
  253. {
  254. /* Divide each element of the row of the input matrix
  255. * by the pivot element */
  256. vec1 = vld1q_f32(pInT1);
  257. vec1 = vmulq_f32(vec1, tmpV);
  258. vst1q_f32(pInT1, vec1);
  259. pInT1 += 4;
  260. /* Decrement the loop counter */
  261. j--;
  262. }
  263. /* Tail */
  264. j = (numCols - l) & 3;
  265. while (j > 0U)
  266. {
  267. /* Divide each element of the row of the input matrix
  268. * by the pivot element */
  269. in1 = *pInT1;
  270. *pInT1++ = in1 / in;
  271. /* Decrement the loop counter */
  272. j--;
  273. }
  274. /* Loop over number of columns of the destination matrix */
  275. j = numCols >> 2;
  276. while (j > 0U)
  277. {
  278. /* Divide each element of the row of the destination matrix
  279. * by the pivot element */
  280. vec1 = vld1q_f32(pInT2);
  281. vec1 = vmulq_f32(vec1, tmpV);
  282. vst1q_f32(pInT2, vec1);
  283. pInT2 += 4;
  284. /* Decrement the loop counter */
  285. j--;
  286. }
  287. /* Tail */
  288. j = numCols & 3;
  289. while (j > 0U)
  290. {
  291. /* Divide each element of the row of the destination matrix
  292. * by the pivot element */
  293. in1 = *pInT2;
  294. *pInT2++ = in1 / in;
  295. /* Decrement the loop counter */
  296. j--;
  297. }
  298. /* Replace the rows with the sum of that row and a multiple of row i
  299. * so that each new element in column i above row i is zero.*/
  300. /* Temporary pointers for input and destination matrices */
  301. pInT1 = pIn;
  302. pInT2 = pOut;
  303. /* index used to check for pivot element */
  304. i = 0U;
  305. /* Loop over number of rows */
  306. /* to be replaced by the sum of that row and a multiple of row i */
  307. k = numRows;
  308. while (k > 0U)
  309. {
  310. /* Check for the pivot element */
  311. if (i == l)
  312. {
  313. /* If the processing element is the pivot element,
  314. only the columns to the right are to be processed */
  315. pInT1 += numCols - l;
  316. pInT2 += numCols;
  317. }
  318. else
  319. {
  320. /* Element of the reference row */
  321. in = *pInT1;
  322. tmpV = vdupq_n_f32(in);
  323. /* Working pointers for input and destination pivot rows */
  324. pPRT_in = pPivotRowIn;
  325. pPRT_pDst = pPivotRowDst;
  326. /* Loop over the number of columns to the right of the pivot element,
  327. to replace the elements in the input matrix */
  328. j = (numCols - l) >> 2;
  329. while (j > 0U)
  330. {
  331. /* Replace the element by the sum of that row
  332. and a multiple of the reference row */
  333. vec1 = vld1q_f32(pInT1);
  334. vec2 = vld1q_f32(pPRT_in);
  335. vec1 = vmlsq_f32(vec1, tmpV, vec2);
  336. vst1q_f32(pInT1, vec1);
  337. pPRT_in += 4;
  338. pInT1 += 4;
  339. /* Decrement the loop counter */
  340. j--;
  341. }
  342. /* Tail */
  343. j = (numCols - l) & 3;
  344. while (j > 0U)
  345. {
  346. /* Replace the element by the sum of that row
  347. and a multiple of the reference row */
  348. in1 = *pInT1;
  349. *pInT1++ = in1 - (in * *pPRT_in++);
  350. /* Decrement the loop counter */
  351. j--;
  352. }
  353. /* Loop over the number of columns to
  354. replace the elements in the destination matrix */
  355. j = numCols >> 2;
  356. while (j > 0U)
  357. {
  358. /* Replace the element by the sum of that row
  359. and a multiple of the reference row */
  360. vec1 = vld1q_f32(pInT2);
  361. vec2 = vld1q_f32(pPRT_pDst);
  362. vec1 = vmlsq_f32(vec1, tmpV, vec2);
  363. vst1q_f32(pInT2, vec1);
  364. pPRT_pDst += 4;
  365. pInT2 += 4;
  366. /* Decrement the loop counter */
  367. j--;
  368. }
  369. /* Tail */
  370. j = numCols & 3;
  371. while (j > 0U)
  372. {
  373. /* Replace the element by the sum of that row
  374. and a multiple of the reference row */
  375. in1 = *pInT2;
  376. *pInT2++ = in1 - (in * *pPRT_pDst++);
  377. /* Decrement the loop counter */
  378. j--;
  379. }
  380. }
  381. /* Increment the temporary input pointer */
  382. pInT1 = pInT1 + l;
  383. /* Decrement the loop counter */
  384. k--;
  385. /* Increment the pivot index */
  386. i++;
  387. }
  388. /* Increment the input pointer */
  389. pIn++;
  390. /* Decrement the loop counter */
  391. loopCnt--;
  392. /* Increment the index modifier */
  393. l++;
  394. }
  395. /* Set status as ARM_MATH_SUCCESS */
  396. status = ARM_MATH_SUCCESS;
  397. if ((flag != 1U) && (in == 0.0f))
  398. {
  399. pIn = pSrc->pData;
  400. for (i = 0; i < numRows * numCols; i++)
  401. {
  402. if (pIn[i] != 0.0f)
  403. break;
  404. }
  405. if (i == numRows * numCols)
  406. status = ARM_MATH_SINGULAR;
  407. }
  408. }
  409. /* Return to application */
  410. return (status);
  411. }
  412. #else
  413. arm_status arm_mat_inverse_f32(
  414. const arm_matrix_instance_f32 * pSrc,
  415. arm_matrix_instance_f32 * pDst)
  416. {
  417. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  418. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  419. float32_t *pInT1, *pInT2; /* Temporary input data matrix pointer */
  420. float32_t *pOutT1, *pOutT2; /* Temporary output data matrix pointer */
  421. float32_t *pPivotRowIn, *pPRT_in, *pPivotRowDst, *pPRT_pDst; /* Temporary input and output data matrix pointer */
  422. uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */
  423. uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */
  424. #if defined (ARM_MATH_DSP)
  425. float32_t maxC; /* maximum value in the column */
  426. float32_t Xchg, in = 0.0f, in1; /* Temporary input values */
  427. uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */
  428. arm_status status; /* status of matrix inverse */
  429. #ifdef ARM_MATH_MATRIX_CHECK
  430. /* Check for matrix mismatch condition */
  431. if ((pSrc->numRows != pSrc->numCols) ||
  432. (pDst->numRows != pDst->numCols) ||
  433. (pSrc->numRows != pDst->numRows) )
  434. {
  435. /* Set status as ARM_MATH_SIZE_MISMATCH */
  436. status = ARM_MATH_SIZE_MISMATCH;
  437. }
  438. else
  439. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  440. {
  441. /*--------------------------------------------------------------------------------------------------------------
  442. * Matrix Inverse can be solved using elementary row operations.
  443. *
  444. * Gauss-Jordan Method:
  445. *
  446. * 1. First combine the identity matrix and the input matrix separated by a bar to form an
  447. * augmented matrix as follows:
  448. * _ _ _ _
  449. * | a11 a12 | 1 0 | | X11 X12 |
  450. * | | | = | |
  451. * |_ a21 a22 | 0 1 _| |_ X21 X21 _|
  452. *
  453. * 2. In our implementation, pDst Matrix is used as identity matrix.
  454. *
  455. * 3. Begin with the first row. Let i = 1.
  456. *
  457. * 4. Check to see if the pivot for column i is the greatest of the column.
  458. * The pivot is the element of the main diagonal that is on the current row.
  459. * For instance, if working with row i, then the pivot element is aii.
  460. * If the pivot is not the most significant of the columns, exchange that row with a row
  461. * below it that does contain the most significant value in column i. If the most
  462. * significant value of the column is zero, then an inverse to that matrix does not exist.
  463. * The most significant value of the column is the absolute maximum.
  464. *
  465. * 5. Divide every element of row i by the pivot.
  466. *
  467. * 6. For every row below and row i, replace that row with the sum of that row and
  468. * a multiple of row i so that each new element in column i below row i is zero.
  469. *
  470. * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros
  471. * for every element below and above the main diagonal.
  472. *
  473. * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc).
  474. * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst).
  475. *----------------------------------------------------------------------------------------------------------------*/
  476. /* Working pointer for destination matrix */
  477. pOutT1 = pOut;
  478. /* Loop over the number of rows */
  479. rowCnt = numRows;
  480. /* Making the destination matrix as identity matrix */
  481. while (rowCnt > 0U)
  482. {
  483. /* Writing all zeroes in lower triangle of the destination matrix */
  484. j = numRows - rowCnt;
  485. while (j > 0U)
  486. {
  487. *pOutT1++ = 0.0f;
  488. j--;
  489. }
  490. /* Writing all ones in the diagonal of the destination matrix */
  491. *pOutT1++ = 1.0f;
  492. /* Writing all zeroes in upper triangle of the destination matrix */
  493. j = rowCnt - 1U;
  494. while (j > 0U)
  495. {
  496. *pOutT1++ = 0.0f;
  497. j--;
  498. }
  499. /* Decrement loop counter */
  500. rowCnt--;
  501. }
  502. /* Loop over the number of columns of the input matrix.
  503. All the elements in each column are processed by the row operations */
  504. loopCnt = numCols;
  505. /* Index modifier to navigate through the columns */
  506. l = 0U;
  507. while (loopCnt > 0U)
  508. {
  509. /* Check if the pivot element is zero..
  510. * If it is zero then interchange the row with non zero row below.
  511. * If there is no non zero element to replace in the rows below,
  512. * then the matrix is Singular. */
  513. /* Working pointer for the input matrix that points
  514. * to the pivot element of the particular row */
  515. pInT1 = pIn + (l * numCols);
  516. /* Working pointer for the destination matrix that points
  517. * to the pivot element of the particular row */
  518. pOutT1 = pOut + (l * numCols);
  519. /* Temporary variable to hold the pivot value */
  520. in = *pInT1;
  521. /* Grab the most significant value from column l */
  522. maxC = 0;
  523. for (i = l; i < numRows; i++)
  524. {
  525. maxC = *pInT1 > 0 ? (*pInT1 > maxC ? *pInT1 : maxC) : (-*pInT1 > maxC ? -*pInT1 : maxC);
  526. pInT1 += numCols;
  527. }
  528. /* Update the status if the matrix is singular */
  529. if (maxC == 0.0f)
  530. {
  531. return ARM_MATH_SINGULAR;
  532. }
  533. /* Restore pInT1 */
  534. pInT1 = pIn;
  535. /* Destination pointer modifier */
  536. k = 1U;
  537. /* Check if the pivot element is the most significant of the column */
  538. if ( (in > 0.0f ? in : -in) != maxC)
  539. {
  540. /* Loop over the number rows present below */
  541. i = numRows - (l + 1U);
  542. while (i > 0U)
  543. {
  544. /* Update the input and destination pointers */
  545. pInT2 = pInT1 + (numCols * l);
  546. pOutT2 = pOutT1 + (numCols * k);
  547. /* Look for the most significant element to
  548. * replace in the rows below */
  549. if ((*pInT2 > 0.0f ? *pInT2: -*pInT2) == maxC)
  550. {
  551. /* Loop over number of columns
  552. * to the right of the pilot element */
  553. j = numCols - l;
  554. while (j > 0U)
  555. {
  556. /* Exchange the row elements of the input matrix */
  557. Xchg = *pInT2;
  558. *pInT2++ = *pInT1;
  559. *pInT1++ = Xchg;
  560. /* Decrement the loop counter */
  561. j--;
  562. }
  563. /* Loop over number of columns of the destination matrix */
  564. j = numCols;
  565. while (j > 0U)
  566. {
  567. /* Exchange the row elements of the destination matrix */
  568. Xchg = *pOutT2;
  569. *pOutT2++ = *pOutT1;
  570. *pOutT1++ = Xchg;
  571. /* Decrement loop counter */
  572. j--;
  573. }
  574. /* Flag to indicate whether exchange is done or not */
  575. flag = 1U;
  576. /* Break after exchange is done */
  577. break;
  578. }
  579. /* Update the destination pointer modifier */
  580. k++;
  581. /* Decrement loop counter */
  582. i--;
  583. }
  584. }
  585. /* Update the status if the matrix is singular */
  586. if ((flag != 1U) && (in == 0.0f))
  587. {
  588. return ARM_MATH_SINGULAR;
  589. }
  590. /* Points to the pivot row of input and destination matrices */
  591. pPivotRowIn = pIn + (l * numCols);
  592. pPivotRowDst = pOut + (l * numCols);
  593. /* Temporary pointers to the pivot row pointers */
  594. pInT1 = pPivotRowIn;
  595. pInT2 = pPivotRowDst;
  596. /* Pivot element of the row */
  597. in = *pPivotRowIn;
  598. /* Loop over number of columns
  599. * to the right of the pilot element */
  600. j = (numCols - l);
  601. while (j > 0U)
  602. {
  603. /* Divide each element of the row of the input matrix
  604. * by the pivot element */
  605. in1 = *pInT1;
  606. *pInT1++ = in1 / in;
  607. /* Decrement the loop counter */
  608. j--;
  609. }
  610. /* Loop over number of columns of the destination matrix */
  611. j = numCols;
  612. while (j > 0U)
  613. {
  614. /* Divide each element of the row of the destination matrix
  615. * by the pivot element */
  616. in1 = *pInT2;
  617. *pInT2++ = in1 / in;
  618. /* Decrement the loop counter */
  619. j--;
  620. }
  621. /* Replace the rows with the sum of that row and a multiple of row i
  622. * so that each new element in column i above row i is zero.*/
  623. /* Temporary pointers for input and destination matrices */
  624. pInT1 = pIn;
  625. pInT2 = pOut;
  626. /* index used to check for pivot element */
  627. i = 0U;
  628. /* Loop over number of rows */
  629. /* to be replaced by the sum of that row and a multiple of row i */
  630. k = numRows;
  631. while (k > 0U)
  632. {
  633. /* Check for the pivot element */
  634. if (i == l)
  635. {
  636. /* If the processing element is the pivot element,
  637. only the columns to the right are to be processed */
  638. pInT1 += numCols - l;
  639. pInT2 += numCols;
  640. }
  641. else
  642. {
  643. /* Element of the reference row */
  644. in = *pInT1;
  645. /* Working pointers for input and destination pivot rows */
  646. pPRT_in = pPivotRowIn;
  647. pPRT_pDst = pPivotRowDst;
  648. /* Loop over the number of columns to the right of the pivot element,
  649. to replace the elements in the input matrix */
  650. j = (numCols - l);
  651. while (j > 0U)
  652. {
  653. /* Replace the element by the sum of that row
  654. and a multiple of the reference row */
  655. in1 = *pInT1;
  656. *pInT1++ = in1 - (in * *pPRT_in++);
  657. /* Decrement the loop counter */
  658. j--;
  659. }
  660. /* Loop over the number of columns to
  661. replace the elements in the destination matrix */
  662. j = numCols;
  663. while (j > 0U)
  664. {
  665. /* Replace the element by the sum of that row
  666. and a multiple of the reference row */
  667. in1 = *pInT2;
  668. *pInT2++ = in1 - (in * *pPRT_pDst++);
  669. /* Decrement loop counter */
  670. j--;
  671. }
  672. }
  673. /* Increment temporary input pointer */
  674. pInT1 = pInT1 + l;
  675. /* Decrement loop counter */
  676. k--;
  677. /* Increment pivot index */
  678. i++;
  679. }
  680. /* Increment the input pointer */
  681. pIn++;
  682. /* Decrement the loop counter */
  683. loopCnt--;
  684. /* Increment the index modifier */
  685. l++;
  686. }
  687. #else
  688. float32_t Xchg, in = 0.0f; /* Temporary input values */
  689. uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */
  690. arm_status status; /* status of matrix inverse */
  691. #ifdef ARM_MATH_MATRIX_CHECK
  692. /* Check for matrix mismatch condition */
  693. if ((pSrc->numRows != pSrc->numCols) ||
  694. (pDst->numRows != pDst->numCols) ||
  695. (pSrc->numRows != pDst->numRows) )
  696. {
  697. /* Set status as ARM_MATH_SIZE_MISMATCH */
  698. status = ARM_MATH_SIZE_MISMATCH;
  699. }
  700. else
  701. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  702. {
  703. /*--------------------------------------------------------------------------------------------------------------
  704. * Matrix Inverse can be solved using elementary row operations.
  705. *
  706. * Gauss-Jordan Method:
  707. *
  708. * 1. First combine the identity matrix and the input matrix separated by a bar to form an
  709. * augmented matrix as follows:
  710. * _ _ _ _ _ _ _ _
  711. * | | a11 a12 | | | 1 0 | | | X11 X12 |
  712. * | | | | | | | = | |
  713. * |_ |_ a21 a22 _| | |_0 1 _| _| |_ X21 X21 _|
  714. *
  715. * 2. In our implementation, pDst Matrix is used as identity matrix.
  716. *
  717. * 3. Begin with the first row. Let i = 1.
  718. *
  719. * 4. Check to see if the pivot for row i is zero.
  720. * The pivot is the element of the main diagonal that is on the current row.
  721. * For instance, if working with row i, then the pivot element is aii.
  722. * If the pivot is zero, exchange that row with a row below it that does not
  723. * contain a zero in column i. If this is not possible, then an inverse
  724. * to that matrix does not exist.
  725. *
  726. * 5. Divide every element of row i by the pivot.
  727. *
  728. * 6. For every row below and row i, replace that row with the sum of that row and
  729. * a multiple of row i so that each new element in column i below row i is zero.
  730. *
  731. * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros
  732. * for every element below and above the main diagonal.
  733. *
  734. * 8. Now an identical matrix is formed to the left of the bar(input matrix, src).
  735. * Therefore, the matrix to the right of the bar is our solution(dst matrix, dst).
  736. *----------------------------------------------------------------------------------------------------------------*/
  737. /* Working pointer for destination matrix */
  738. pOutT1 = pOut;
  739. /* Loop over the number of rows */
  740. rowCnt = numRows;
  741. /* Making the destination matrix as identity matrix */
  742. while (rowCnt > 0U)
  743. {
  744. /* Writing all zeroes in lower triangle of the destination matrix */
  745. j = numRows - rowCnt;
  746. while (j > 0U)
  747. {
  748. *pOutT1++ = 0.0f;
  749. j--;
  750. }
  751. /* Writing all ones in the diagonal of the destination matrix */
  752. *pOutT1++ = 1.0f;
  753. /* Writing all zeroes in upper triangle of the destination matrix */
  754. j = rowCnt - 1U;
  755. while (j > 0U)
  756. {
  757. *pOutT1++ = 0.0f;
  758. j--;
  759. }
  760. /* Decrement loop counter */
  761. rowCnt--;
  762. }
  763. /* Loop over the number of columns of the input matrix.
  764. All the elements in each column are processed by the row operations */
  765. loopCnt = numCols;
  766. /* Index modifier to navigate through the columns */
  767. l = 0U;
  768. while (loopCnt > 0U)
  769. {
  770. /* Check if the pivot element is zero..
  771. * If it is zero then interchange the row with non zero row below.
  772. * If there is no non zero element to replace in the rows below,
  773. * then the matrix is Singular. */
  774. /* Working pointer for the input matrix that points
  775. * to the pivot element of the particular row */
  776. pInT1 = pIn + (l * numCols);
  777. /* Working pointer for the destination matrix that points
  778. * to the pivot element of the particular row */
  779. pOutT1 = pOut + (l * numCols);
  780. /* Temporary variable to hold the pivot value */
  781. in = *pInT1;
  782. /* Destination pointer modifier */
  783. k = 1U;
  784. /* Check if the pivot element is zero */
  785. if (*pInT1 == 0.0f)
  786. {
  787. /* Loop over the number rows present below */
  788. for (i = (l + 1U); i < numRows; i++)
  789. {
  790. /* Update the input and destination pointers */
  791. pInT2 = pInT1 + (numCols * l);
  792. pOutT2 = pOutT1 + (numCols * k);
  793. /* Check if there is a non zero pivot element to
  794. * replace in the rows below */
  795. if (*pInT2 != 0.0f)
  796. {
  797. /* Loop over number of columns
  798. * to the right of the pilot element */
  799. for (j = 0U; j < (numCols - l); j++)
  800. {
  801. /* Exchange the row elements of the input matrix */
  802. Xchg = *pInT2;
  803. *pInT2++ = *pInT1;
  804. *pInT1++ = Xchg;
  805. }
  806. for (j = 0U; j < numCols; j++)
  807. {
  808. Xchg = *pOutT2;
  809. *pOutT2++ = *pOutT1;
  810. *pOutT1++ = Xchg;
  811. }
  812. /* Flag to indicate whether exchange is done or not */
  813. flag = 1U;
  814. /* Break after exchange is done */
  815. break;
  816. }
  817. /* Update the destination pointer modifier */
  818. k++;
  819. }
  820. }
  821. /* Update the status if the matrix is singular */
  822. if ((flag != 1U) && (in == 0.0f))
  823. {
  824. return ARM_MATH_SINGULAR;
  825. }
  826. /* Points to the pivot row of input and destination matrices */
  827. pPivotRowIn = pIn + (l * numCols);
  828. pPivotRowDst = pOut + (l * numCols);
  829. /* Temporary pointers to the pivot row pointers */
  830. pInT1 = pPivotRowIn;
  831. pOutT1 = pPivotRowDst;
  832. /* Pivot element of the row */
  833. in = *(pIn + (l * numCols));
  834. /* Loop over number of columns
  835. * to the right of the pilot element */
  836. for (j = 0U; j < (numCols - l); j++)
  837. {
  838. /* Divide each element of the row of the input matrix
  839. * by the pivot element */
  840. *pInT1 = *pInT1 / in;
  841. pInT1++;
  842. }
  843. for (j = 0U; j < numCols; j++)
  844. {
  845. /* Divide each element of the row of the destination matrix
  846. * by the pivot element */
  847. *pOutT1 = *pOutT1 / in;
  848. pOutT1++;
  849. }
  850. /* Replace the rows with the sum of that row and a multiple of row i
  851. * so that each new element in column i above row i is zero.*/
  852. /* Temporary pointers for input and destination matrices */
  853. pInT1 = pIn;
  854. pOutT1 = pOut;
  855. for (i = 0U; i < numRows; i++)
  856. {
  857. /* Check for the pivot element */
  858. if (i == l)
  859. {
  860. /* If the processing element is the pivot element,
  861. only the columns to the right are to be processed */
  862. pInT1 += numCols - l;
  863. pOutT1 += numCols;
  864. }
  865. else
  866. {
  867. /* Element of the reference row */
  868. in = *pInT1;
  869. /* Working pointers for input and destination pivot rows */
  870. pPRT_in = pPivotRowIn;
  871. pPRT_pDst = pPivotRowDst;
  872. /* Loop over the number of columns to the right of the pivot element,
  873. to replace the elements in the input matrix */
  874. for (j = 0U; j < (numCols - l); j++)
  875. {
  876. /* Replace the element by the sum of that row
  877. and a multiple of the reference row */
  878. *pInT1 = *pInT1 - (in * *pPRT_in++);
  879. pInT1++;
  880. }
  881. /* Loop over the number of columns to
  882. replace the elements in the destination matrix */
  883. for (j = 0U; j < numCols; j++)
  884. {
  885. /* Replace the element by the sum of that row
  886. and a multiple of the reference row */
  887. *pOutT1 = *pOutT1 - (in * *pPRT_pDst++);
  888. pOutT1++;
  889. }
  890. }
  891. /* Increment temporary input pointer */
  892. pInT1 = pInT1 + l;
  893. }
  894. /* Increment the input pointer */
  895. pIn++;
  896. /* Decrement the loop counter */
  897. loopCnt--;
  898. /* Increment the index modifier */
  899. l++;
  900. }
  901. #endif /* #if defined (ARM_MATH_DSP) */
  902. /* Set status as ARM_MATH_SUCCESS */
  903. status = ARM_MATH_SUCCESS;
  904. if ((flag != 1U) && (in == 0.0f))
  905. {
  906. pIn = pSrc->pData;
  907. for (i = 0; i < numRows * numCols; i++)
  908. {
  909. if (pIn[i] != 0.0f)
  910. break;
  911. }
  912. if (i == numRows * numCols)
  913. status = ARM_MATH_SINGULAR;
  914. }
  915. }
  916. /* Return to application */
  917. return (status);
  918. }
  919. #endif /* #if defined(ARM_MATH_NEON) */
  920. /**
  921. @} end of MatrixInv group
  922. */