Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / CMSIS / DSP / Source / MatrixFunctions / arm_mat_inverse_f32.c
blobb82373a39e8dd2eb4f163887ab58007b663beea2
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_inverse_f32.c
4 * Description: Floating-point matrix inverse
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
29 #include "arm_math.h"
31 /**
32 * @ingroup groupMatrix
35 /**
36 * @defgroup MatrixInv Matrix Inverse
38 * Computes the inverse of a matrix.
40 * The inverse is defined only if the input matrix is square and non-singular (the determinant
41 * is non-zero). The function checks that the input and output matrices are square and of the
42 * same size.
44 * Matrix inversion is numerically sensitive and the CMSIS DSP library only supports matrix
45 * inversion of floating-point matrices.
47 * \par Algorithm
48 * The Gauss-Jordan method is used to find the inverse.
49 * The algorithm performs a sequence of elementary row-operations until it
50 * reduces the input matrix to an identity matrix. Applying the same sequence
51 * of elementary row-operations to an identity matrix yields the inverse matrix.
52 * If the input matrix is singular, then the algorithm terminates and returns error status
53 * <code>ARM_MATH_SINGULAR</code>.
54 * \image html MatrixInverse.gif "Matrix Inverse of a 3 x 3 matrix using Gauss-Jordan Method"
57 /**
58 * @addtogroup MatrixInv
59 * @{
62 /**
63 * @brief Floating-point matrix inverse.
64 * @param[in] *pSrc points to input matrix structure
65 * @param[out] *pDst points to output matrix structure
66 * @return The function returns
67 * <code>ARM_MATH_SIZE_MISMATCH</code> if the input matrix is not square or if the size
68 * of the output matrix does not match the size of the input matrix.
69 * If the input matrix is found to be singular (non-invertible), then the function returns
70 * <code>ARM_MATH_SINGULAR</code>. Otherwise, the function returns <code>ARM_MATH_SUCCESS</code>.
73 arm_status arm_mat_inverse_f32(
74 const arm_matrix_instance_f32 * pSrc,
75 arm_matrix_instance_f32 * pDst)
77 float32_t *pIn = pSrc->pData; /* input data matrix pointer */
78 float32_t *pOut = pDst->pData; /* output data matrix pointer */
79 float32_t *pInT1, *pInT2; /* Temporary input data matrix pointer */
80 float32_t *pOutT1, *pOutT2; /* Temporary output data matrix pointer */
81 float32_t *pPivotRowIn, *pPRT_in, *pPivotRowDst, *pPRT_pDst; /* Temporary input and output data matrix pointer */
82 uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */
83 uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */
85 #if defined (ARM_MATH_DSP)
86 float32_t maxC; /* maximum value in the column */
88 /* Run the below code for Cortex-M4 and Cortex-M3 */
90 float32_t Xchg, in = 0.0f, in1; /* Temporary input values */
91 uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */
92 arm_status status; /* status of matrix inverse */
94 #ifdef ARM_MATH_MATRIX_CHECK
97 /* Check for matrix mismatch condition */
98 if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols)
99 || (pSrc->numRows != pDst->numRows))
101 /* Set status as ARM_MATH_SIZE_MISMATCH */
102 status = ARM_MATH_SIZE_MISMATCH;
104 else
105 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
109 /*--------------------------------------------------------------------------------------------------------------
110 * Matrix Inverse can be solved using elementary row operations.
112 * Gauss-Jordan Method:
114 * 1. First combine the identity matrix and the input matrix separated by a bar to form an
115 * augmented matrix as follows:
116 * _ _ _ _
117 * | a11 a12 | 1 0 | | X11 X12 |
118 * | | | = | |
119 * |_ a21 a22 | 0 1 _| |_ X21 X21 _|
121 * 2. In our implementation, pDst Matrix is used as identity matrix.
123 * 3. Begin with the first row. Let i = 1.
125 * 4. Check to see if the pivot for column i is the greatest of the column.
126 * The pivot is the element of the main diagonal that is on the current row.
127 * For instance, if working with row i, then the pivot element is aii.
128 * If the pivot is not the most significant of the columns, exchange that row with a row
129 * below it that does contain the most significant value in column i. If the most
130 * significant value of the column is zero, then an inverse to that matrix does not exist.
131 * The most significant value of the column is the absolute maximum.
133 * 5. Divide every element of row i by the pivot.
135 * 6. For every row below and row i, replace that row with the sum of that row and
136 * a multiple of row i so that each new element in column i below row i is zero.
138 * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros
139 * for every element below and above the main diagonal.
141 * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc).
142 * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst).
143 *----------------------------------------------------------------------------------------------------------------*/
145 /* Working pointer for destination matrix */
146 pOutT1 = pOut;
148 /* Loop over the number of rows */
149 rowCnt = numRows;
151 /* Making the destination matrix as identity matrix */
152 while (rowCnt > 0U)
154 /* Writing all zeroes in lower triangle of the destination matrix */
155 j = numRows - rowCnt;
156 while (j > 0U)
158 *pOutT1++ = 0.0f;
159 j--;
162 /* Writing all ones in the diagonal of the destination matrix */
163 *pOutT1++ = 1.0f;
165 /* Writing all zeroes in upper triangle of the destination matrix */
166 j = rowCnt - 1U;
167 while (j > 0U)
169 *pOutT1++ = 0.0f;
170 j--;
173 /* Decrement the loop counter */
174 rowCnt--;
177 /* Loop over the number of columns of the input matrix.
178 All the elements in each column are processed by the row operations */
179 loopCnt = numCols;
181 /* Index modifier to navigate through the columns */
182 l = 0U;
184 while (loopCnt > 0U)
186 /* Check if the pivot element is zero..
187 * If it is zero then interchange the row with non zero row below.
188 * If there is no non zero element to replace in the rows below,
189 * then the matrix is Singular. */
191 /* Working pointer for the input matrix that points
192 * to the pivot element of the particular row */
193 pInT1 = pIn + (l * numCols);
195 /* Working pointer for the destination matrix that points
196 * to the pivot element of the particular row */
197 pOutT1 = pOut + (l * numCols);
199 /* Temporary variable to hold the pivot value */
200 in = *pInT1;
202 /* Grab the most significant value from column l */
203 maxC = 0;
204 for (i = l; i < numRows; i++)
206 maxC = *pInT1 > 0 ? (*pInT1 > maxC ? *pInT1 : maxC) : (-*pInT1 > maxC ? -*pInT1 : maxC);
207 pInT1 += numCols;
210 /* Update the status if the matrix is singular */
211 if (maxC == 0.0f)
213 return ARM_MATH_SINGULAR;
216 /* Restore pInT1 */
217 pInT1 = pIn;
219 /* Destination pointer modifier */
220 k = 1U;
222 /* Check if the pivot element is the most significant of the column */
223 if ( (in > 0.0f ? in : -in) != maxC)
225 /* Loop over the number rows present below */
226 i = numRows - (l + 1U);
228 while (i > 0U)
230 /* Update the input and destination pointers */
231 pInT2 = pInT1 + (numCols * l);
232 pOutT2 = pOutT1 + (numCols * k);
234 /* Look for the most significant element to
235 * replace in the rows below */
236 if ((*pInT2 > 0.0f ? *pInT2: -*pInT2) == maxC)
238 /* Loop over number of columns
239 * to the right of the pilot element */
240 j = numCols - l;
242 while (j > 0U)
244 /* Exchange the row elements of the input matrix */
245 Xchg = *pInT2;
246 *pInT2++ = *pInT1;
247 *pInT1++ = Xchg;
249 /* Decrement the loop counter */
250 j--;
253 /* Loop over number of columns of the destination matrix */
254 j = numCols;
256 while (j > 0U)
258 /* Exchange the row elements of the destination matrix */
259 Xchg = *pOutT2;
260 *pOutT2++ = *pOutT1;
261 *pOutT1++ = Xchg;
263 /* Decrement the loop counter */
264 j--;
267 /* Flag to indicate whether exchange is done or not */
268 flag = 1U;
270 /* Break after exchange is done */
271 break;
274 /* Update the destination pointer modifier */
275 k++;
277 /* Decrement the loop counter */
278 i--;
282 /* Update the status if the matrix is singular */
283 if ((flag != 1U) && (in == 0.0f))
285 return ARM_MATH_SINGULAR;
288 /* Points to the pivot row of input and destination matrices */
289 pPivotRowIn = pIn + (l * numCols);
290 pPivotRowDst = pOut + (l * numCols);
292 /* Temporary pointers to the pivot row pointers */
293 pInT1 = pPivotRowIn;
294 pInT2 = pPivotRowDst;
296 /* Pivot element of the row */
297 in = *pPivotRowIn;
299 /* Loop over number of columns
300 * to the right of the pilot element */
301 j = (numCols - l);
303 while (j > 0U)
305 /* Divide each element of the row of the input matrix
306 * by the pivot element */
307 in1 = *pInT1;
308 *pInT1++ = in1 / in;
310 /* Decrement the loop counter */
311 j--;
314 /* Loop over number of columns of the destination matrix */
315 j = numCols;
317 while (j > 0U)
319 /* Divide each element of the row of the destination matrix
320 * by the pivot element */
321 in1 = *pInT2;
322 *pInT2++ = in1 / in;
324 /* Decrement the loop counter */
325 j--;
328 /* Replace the rows with the sum of that row and a multiple of row i
329 * so that each new element in column i above row i is zero.*/
331 /* Temporary pointers for input and destination matrices */
332 pInT1 = pIn;
333 pInT2 = pOut;
335 /* index used to check for pivot element */
336 i = 0U;
338 /* Loop over number of rows */
339 /* to be replaced by the sum of that row and a multiple of row i */
340 k = numRows;
342 while (k > 0U)
344 /* Check for the pivot element */
345 if (i == l)
347 /* If the processing element is the pivot element,
348 only the columns to the right are to be processed */
349 pInT1 += numCols - l;
351 pInT2 += numCols;
353 else
355 /* Element of the reference row */
356 in = *pInT1;
358 /* Working pointers for input and destination pivot rows */
359 pPRT_in = pPivotRowIn;
360 pPRT_pDst = pPivotRowDst;
362 /* Loop over the number of columns to the right of the pivot element,
363 to replace the elements in the input matrix */
364 j = (numCols - l);
366 while (j > 0U)
368 /* Replace the element by the sum of that row
369 and a multiple of the reference row */
370 in1 = *pInT1;
371 *pInT1++ = in1 - (in * *pPRT_in++);
373 /* Decrement the loop counter */
374 j--;
377 /* Loop over the number of columns to
378 replace the elements in the destination matrix */
379 j = numCols;
381 while (j > 0U)
383 /* Replace the element by the sum of that row
384 and a multiple of the reference row */
385 in1 = *pInT2;
386 *pInT2++ = in1 - (in * *pPRT_pDst++);
388 /* Decrement the loop counter */
389 j--;
394 /* Increment the temporary input pointer */
395 pInT1 = pInT1 + l;
397 /* Decrement the loop counter */
398 k--;
400 /* Increment the pivot index */
401 i++;
404 /* Increment the input pointer */
405 pIn++;
407 /* Decrement the loop counter */
408 loopCnt--;
410 /* Increment the index modifier */
411 l++;
415 #else
417 /* Run the below code for Cortex-M0 */
419 float32_t Xchg, in = 0.0f; /* Temporary input values */
420 uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */
421 arm_status status; /* status of matrix inverse */
423 #ifdef ARM_MATH_MATRIX_CHECK
425 /* Check for matrix mismatch condition */
426 if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols)
427 || (pSrc->numRows != pDst->numRows))
429 /* Set status as ARM_MATH_SIZE_MISMATCH */
430 status = ARM_MATH_SIZE_MISMATCH;
432 else
433 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
436 /*--------------------------------------------------------------------------------------------------------------
437 * Matrix Inverse can be solved using elementary row operations.
439 * Gauss-Jordan Method:
441 * 1. First combine the identity matrix and the input matrix separated by a bar to form an
442 * augmented matrix as follows:
443 * _ _ _ _ _ _ _ _
444 * | | a11 a12 | | | 1 0 | | | X11 X12 |
445 * | | | | | | | = | |
446 * |_ |_ a21 a22 _| | |_0 1 _| _| |_ X21 X21 _|
448 * 2. In our implementation, pDst Matrix is used as identity matrix.
450 * 3. Begin with the first row. Let i = 1.
452 * 4. Check to see if the pivot for row i is zero.
453 * The pivot is the element of the main diagonal that is on the current row.
454 * For instance, if working with row i, then the pivot element is aii.
455 * If the pivot is zero, exchange that row with a row below it that does not
456 * contain a zero in column i. If this is not possible, then an inverse
457 * to that matrix does not exist.
459 * 5. Divide every element of row i by the pivot.
461 * 6. For every row below and row i, replace that row with the sum of that row and
462 * a multiple of row i so that each new element in column i below row i is zero.
464 * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros
465 * for every element below and above the main diagonal.
467 * 8. Now an identical matrix is formed to the left of the bar(input matrix, src).
468 * Therefore, the matrix to the right of the bar is our solution(dst matrix, dst).
469 *----------------------------------------------------------------------------------------------------------------*/
471 /* Working pointer for destination matrix */
472 pOutT1 = pOut;
474 /* Loop over the number of rows */
475 rowCnt = numRows;
477 /* Making the destination matrix as identity matrix */
478 while (rowCnt > 0U)
480 /* Writing all zeroes in lower triangle of the destination matrix */
481 j = numRows - rowCnt;
482 while (j > 0U)
484 *pOutT1++ = 0.0f;
485 j--;
488 /* Writing all ones in the diagonal of the destination matrix */
489 *pOutT1++ = 1.0f;
491 /* Writing all zeroes in upper triangle of the destination matrix */
492 j = rowCnt - 1U;
493 while (j > 0U)
495 *pOutT1++ = 0.0f;
496 j--;
499 /* Decrement the loop counter */
500 rowCnt--;
503 /* Loop over the number of columns of the input matrix.
504 All the elements in each column are processed by the row operations */
505 loopCnt = numCols;
507 /* Index modifier to navigate through the columns */
508 l = 0U;
509 //for(loopCnt = 0U; loopCnt < numCols; loopCnt++)
510 while (loopCnt > 0U)
512 /* Check if the pivot element is zero..
513 * If it is zero then interchange the row with non zero row below.
514 * If there is no non zero element to replace in the rows below,
515 * then the matrix is Singular. */
517 /* Working pointer for the input matrix that points
518 * to the pivot element of the particular row */
519 pInT1 = pIn + (l * numCols);
521 /* Working pointer for the destination matrix that points
522 * to the pivot element of the particular row */
523 pOutT1 = pOut + (l * numCols);
525 /* Temporary variable to hold the pivot value */
526 in = *pInT1;
528 /* Destination pointer modifier */
529 k = 1U;
531 /* Check if the pivot element is zero */
532 if (*pInT1 == 0.0f)
534 /* Loop over the number rows present below */
535 for (i = (l + 1U); i < numRows; i++)
537 /* Update the input and destination pointers */
538 pInT2 = pInT1 + (numCols * l);
539 pOutT2 = pOutT1 + (numCols * k);
541 /* Check if there is a non zero pivot element to
542 * replace in the rows below */
543 if (*pInT2 != 0.0f)
545 /* Loop over number of columns
546 * to the right of the pilot element */
547 for (j = 0U; j < (numCols - l); j++)
549 /* Exchange the row elements of the input matrix */
550 Xchg = *pInT2;
551 *pInT2++ = *pInT1;
552 *pInT1++ = Xchg;
555 for (j = 0U; j < numCols; j++)
557 Xchg = *pOutT2;
558 *pOutT2++ = *pOutT1;
559 *pOutT1++ = Xchg;
562 /* Flag to indicate whether exchange is done or not */
563 flag = 1U;
565 /* Break after exchange is done */
566 break;
569 /* Update the destination pointer modifier */
570 k++;
574 /* Update the status if the matrix is singular */
575 if ((flag != 1U) && (in == 0.0f))
577 return ARM_MATH_SINGULAR;
580 /* Points to the pivot row of input and destination matrices */
581 pPivotRowIn = pIn + (l * numCols);
582 pPivotRowDst = pOut + (l * numCols);
584 /* Temporary pointers to the pivot row pointers */
585 pInT1 = pPivotRowIn;
586 pOutT1 = pPivotRowDst;
588 /* Pivot element of the row */
589 in = *(pIn + (l * numCols));
591 /* Loop over number of columns
592 * to the right of the pilot element */
593 for (j = 0U; j < (numCols - l); j++)
595 /* Divide each element of the row of the input matrix
596 * by the pivot element */
597 *pInT1 = *pInT1 / in;
598 pInT1++;
600 for (j = 0U; j < numCols; j++)
602 /* Divide each element of the row of the destination matrix
603 * by the pivot element */
604 *pOutT1 = *pOutT1 / in;
605 pOutT1++;
608 /* Replace the rows with the sum of that row and a multiple of row i
609 * so that each new element in column i above row i is zero.*/
611 /* Temporary pointers for input and destination matrices */
612 pInT1 = pIn;
613 pOutT1 = pOut;
615 for (i = 0U; i < numRows; i++)
617 /* Check for the pivot element */
618 if (i == l)
620 /* If the processing element is the pivot element,
621 only the columns to the right are to be processed */
622 pInT1 += numCols - l;
623 pOutT1 += numCols;
625 else
627 /* Element of the reference row */
628 in = *pInT1;
630 /* Working pointers for input and destination pivot rows */
631 pPRT_in = pPivotRowIn;
632 pPRT_pDst = pPivotRowDst;
634 /* Loop over the number of columns to the right of the pivot element,
635 to replace the elements in the input matrix */
636 for (j = 0U; j < (numCols - l); j++)
638 /* Replace the element by the sum of that row
639 and a multiple of the reference row */
640 *pInT1 = *pInT1 - (in * *pPRT_in++);
641 pInT1++;
643 /* Loop over the number of columns to
644 replace the elements in the destination matrix */
645 for (j = 0U; j < numCols; j++)
647 /* Replace the element by the sum of that row
648 and a multiple of the reference row */
649 *pOutT1 = *pOutT1 - (in * *pPRT_pDst++);
650 pOutT1++;
654 /* Increment the temporary input pointer */
655 pInT1 = pInT1 + l;
657 /* Increment the input pointer */
658 pIn++;
660 /* Decrement the loop counter */
661 loopCnt--;
662 /* Increment the index modifier */
663 l++;
667 #endif /* #if defined (ARM_MATH_DSP) */
669 /* Set status as ARM_MATH_SUCCESS */
670 status = ARM_MATH_SUCCESS;
672 if ((flag != 1U) && (in == 0.0f))
674 pIn = pSrc->pData;
675 for (i = 0; i < numRows * numCols; i++)
677 if (pIn[i] != 0.0f)
678 break;
681 if (i == numRows * numCols)
682 status = ARM_MATH_SINGULAR;
685 /* Return to application */
686 return (status);
690 * @} end of MatrixInv group