1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_inverse_f64.c
4 * Description: Floating-point matrix inverse
6 * $Date: 27. January 2017
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.
32 * @ingroup groupMatrix
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
44 * Matrix inversion is numerically sensitive and the CMSIS DSP library only supports matrix
45 * inversion of floating-point matrices.
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"
58 * @addtogroup MatrixInv
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_f64(
74 const arm_matrix_instance_f64
* pSrc
,
75 arm_matrix_instance_f64
* pDst
)
77 float64_t
*pIn
= pSrc
->pData
; /* input data matrix pointer */
78 float64_t
*pOut
= pDst
->pData
; /* output data matrix pointer */
79 float64_t
*pInT1
, *pInT2
; /* Temporary input data matrix pointer */
80 float64_t
*pOutT1
, *pOutT2
; /* Temporary output data matrix pointer */
81 float64_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 float64_t maxC
; /* maximum value in the column */
88 /* Run the below code for Cortex-M4 and Cortex-M3 */
90 float64_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
;
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:
117 * | a11 a12 | 1 0 | | X11 X12 |
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 */
148 /* Loop over the number of rows */
151 /* Making the destination matrix as identity matrix */
154 /* Writing all zeroes in lower triangle of the destination matrix */
155 j
= numRows
- rowCnt
;
162 /* Writing all ones in the diagonal of the destination matrix */
165 /* Writing all zeroes in upper triangle of the destination matrix */
173 /* Decrement the loop counter */
177 /* Loop over the number of columns of the input matrix.
178 All the elements in each column are processed by the row operations */
181 /* Index modifier to navigate through the columns */
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 */
202 /* Grab the most significant value from column l */
204 for (i
= l
; i
< numRows
; i
++)
206 maxC
= *pInT1
> 0 ? (*pInT1
> maxC
? *pInT1
: maxC
) : (-*pInT1
> maxC
? -*pInT1
: maxC
);
210 /* Update the status if the matrix is singular */
213 return ARM_MATH_SINGULAR
;
219 /* Destination pointer modifier */
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);
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 */
244 /* Exchange the row elements of the input matrix */
249 /* Decrement the loop counter */
253 /* Loop over number of columns of the destination matrix */
258 /* Exchange the row elements of the destination matrix */
263 /* Decrement the loop counter */
267 /* Flag to indicate whether exchange is done or not */
270 /* Break after exchange is done */
274 /* Update the destination pointer modifier */
277 /* Decrement the loop counter */
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 */
294 pInT2
= pPivotRowDst
;
296 /* Pivot element of the row */
299 /* Loop over number of columns
300 * to the right of the pilot element */
305 /* Divide each element of the row of the input matrix
306 * by the pivot element */
310 /* Decrement the loop counter */
314 /* Loop over number of columns of the destination matrix */
319 /* Divide each element of the row of the destination matrix
320 * by the pivot element */
324 /* Decrement the loop counter */
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 */
335 /* index used to check for pivot element */
338 /* Loop over number of rows */
339 /* to be replaced by the sum of that row and a multiple of row i */
344 /* Check for the pivot element */
347 /* If the processing element is the pivot element,
348 only the columns to the right are to be processed */
349 pInT1
+= numCols
- l
;
355 /* Element of the reference row */
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 */
368 /* Replace the element by the sum of that row
369 and a multiple of the reference row */
371 *pInT1
++ = in1
- (in
* *pPRT_in
++);
373 /* Decrement the loop counter */
377 /* Loop over the number of columns to
378 replace the elements in the destination matrix */
383 /* Replace the element by the sum of that row
384 and a multiple of the reference row */
386 *pInT2
++ = in1
- (in
* *pPRT_pDst
++);
388 /* Decrement the loop counter */
394 /* Increment the temporary input pointer */
397 /* Decrement the loop counter */
400 /* Increment the pivot index */
404 /* Increment the input pointer */
407 /* Decrement the loop counter */
410 /* Increment the index modifier */
417 /* Run the below code for Cortex-M0 */
419 float64_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
;
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:
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 */
474 /* Loop over the number of rows */
477 /* Making the destination matrix as identity matrix */
480 /* Writing all zeroes in lower triangle of the destination matrix */
481 j
= numRows
- rowCnt
;
488 /* Writing all ones in the diagonal of the destination matrix */
491 /* Writing all zeroes in upper triangle of the destination matrix */
499 /* Decrement the loop counter */
503 /* Loop over the number of columns of the input matrix.
504 All the elements in each column are processed by the row operations */
507 /* Index modifier to navigate through the columns */
509 //for(loopCnt = 0U; loopCnt < numCols; loopCnt++)
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 */
528 /* Destination pointer modifier */
531 /* Check if the pivot element is zero */
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 */
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 */
555 for (j
= 0U; j
< numCols
; j
++)
562 /* Flag to indicate whether exchange is done or not */
565 /* Break after exchange is done */
569 /* Update the destination pointer modifier */
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 */
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
;
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
;
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 */
615 for (i
= 0U; i
< numRows
; i
++)
617 /* Check for the pivot element */
620 /* If the processing element is the pivot element,
621 only the columns to the right are to be processed */
622 pInT1
+= numCols
- l
;
627 /* Element of the reference row */
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
++);
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
++);
654 /* Increment the temporary input pointer */
657 /* Increment the input pointer */
660 /* Decrement the loop counter */
662 /* Increment the index modifier */
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
))
675 for (i
= 0; i
< numRows
* numCols
; i
++)
681 if (i
== numRows
* numCols
)
682 status
= ARM_MATH_SINGULAR
;
685 /* Return to application */
690 * @} end of MatrixInv group