The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/MatrixFunctions/arm_mat_inverse_f32.c@3:4098b9d3d571, 2018-06-21 (annotated)
- Committer:
- xorjoep
- Date:
- Thu Jun 21 11:56:27 2018 +0000
- Revision:
- 3:4098b9d3d571
- Parent:
- 1:24714b45cd1b
headers is a folder not a library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xorjoep | 1:24714b45cd1b | 1 | /* ---------------------------------------------------------------------- |
xorjoep | 1:24714b45cd1b | 2 | * Project: CMSIS DSP Library |
xorjoep | 1:24714b45cd1b | 3 | * Title: arm_mat_inverse_f32.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Floating-point matrix inverse |
xorjoep | 1:24714b45cd1b | 5 | * |
xorjoep | 1:24714b45cd1b | 6 | * $Date: 27. January 2017 |
xorjoep | 1:24714b45cd1b | 7 | * $Revision: V.1.5.1 |
xorjoep | 1:24714b45cd1b | 8 | * |
xorjoep | 1:24714b45cd1b | 9 | * Target Processor: Cortex-M cores |
xorjoep | 1:24714b45cd1b | 10 | * -------------------------------------------------------------------- */ |
xorjoep | 1:24714b45cd1b | 11 | /* |
xorjoep | 1:24714b45cd1b | 12 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. |
xorjoep | 1:24714b45cd1b | 13 | * |
xorjoep | 1:24714b45cd1b | 14 | * SPDX-License-Identifier: Apache-2.0 |
xorjoep | 1:24714b45cd1b | 15 | * |
xorjoep | 1:24714b45cd1b | 16 | * Licensed under the Apache License, Version 2.0 (the License); you may |
xorjoep | 1:24714b45cd1b | 17 | * not use this file except in compliance with the License. |
xorjoep | 1:24714b45cd1b | 18 | * You may obtain a copy of the License at |
xorjoep | 1:24714b45cd1b | 19 | * |
xorjoep | 1:24714b45cd1b | 20 | * www.apache.org/licenses/LICENSE-2.0 |
xorjoep | 1:24714b45cd1b | 21 | * |
xorjoep | 1:24714b45cd1b | 22 | * Unless required by applicable law or agreed to in writing, software |
xorjoep | 1:24714b45cd1b | 23 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
xorjoep | 1:24714b45cd1b | 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
xorjoep | 1:24714b45cd1b | 25 | * See the License for the specific language governing permissions and |
xorjoep | 1:24714b45cd1b | 26 | * limitations under the License. |
xorjoep | 1:24714b45cd1b | 27 | */ |
xorjoep | 1:24714b45cd1b | 28 | |
xorjoep | 1:24714b45cd1b | 29 | #include "arm_math.h" |
xorjoep | 1:24714b45cd1b | 30 | |
xorjoep | 1:24714b45cd1b | 31 | /** |
xorjoep | 1:24714b45cd1b | 32 | * @ingroup groupMatrix |
xorjoep | 1:24714b45cd1b | 33 | */ |
xorjoep | 1:24714b45cd1b | 34 | |
xorjoep | 1:24714b45cd1b | 35 | /** |
xorjoep | 1:24714b45cd1b | 36 | * @defgroup MatrixInv Matrix Inverse |
xorjoep | 1:24714b45cd1b | 37 | * |
xorjoep | 1:24714b45cd1b | 38 | * Computes the inverse of a matrix. |
xorjoep | 1:24714b45cd1b | 39 | * |
xorjoep | 1:24714b45cd1b | 40 | * The inverse is defined only if the input matrix is square and non-singular (the determinant |
xorjoep | 1:24714b45cd1b | 41 | * is non-zero). The function checks that the input and output matrices are square and of the |
xorjoep | 1:24714b45cd1b | 42 | * same size. |
xorjoep | 1:24714b45cd1b | 43 | * |
xorjoep | 1:24714b45cd1b | 44 | * Matrix inversion is numerically sensitive and the CMSIS DSP library only supports matrix |
xorjoep | 1:24714b45cd1b | 45 | * inversion of floating-point matrices. |
xorjoep | 1:24714b45cd1b | 46 | * |
xorjoep | 1:24714b45cd1b | 47 | * \par Algorithm |
xorjoep | 1:24714b45cd1b | 48 | * The Gauss-Jordan method is used to find the inverse. |
xorjoep | 1:24714b45cd1b | 49 | * The algorithm performs a sequence of elementary row-operations until it |
xorjoep | 1:24714b45cd1b | 50 | * reduces the input matrix to an identity matrix. Applying the same sequence |
xorjoep | 1:24714b45cd1b | 51 | * of elementary row-operations to an identity matrix yields the inverse matrix. |
xorjoep | 1:24714b45cd1b | 52 | * If the input matrix is singular, then the algorithm terminates and returns error status |
xorjoep | 1:24714b45cd1b | 53 | * <code>ARM_MATH_SINGULAR</code>. |
xorjoep | 1:24714b45cd1b | 54 | * \image html MatrixInverse.gif "Matrix Inverse of a 3 x 3 matrix using Gauss-Jordan Method" |
xorjoep | 1:24714b45cd1b | 55 | */ |
xorjoep | 1:24714b45cd1b | 56 | |
xorjoep | 1:24714b45cd1b | 57 | /** |
xorjoep | 1:24714b45cd1b | 58 | * @addtogroup MatrixInv |
xorjoep | 1:24714b45cd1b | 59 | * @{ |
xorjoep | 1:24714b45cd1b | 60 | */ |
xorjoep | 1:24714b45cd1b | 61 | |
xorjoep | 1:24714b45cd1b | 62 | /** |
xorjoep | 1:24714b45cd1b | 63 | * @brief Floating-point matrix inverse. |
xorjoep | 1:24714b45cd1b | 64 | * @param[in] *pSrc points to input matrix structure |
xorjoep | 1:24714b45cd1b | 65 | * @param[out] *pDst points to output matrix structure |
xorjoep | 1:24714b45cd1b | 66 | * @return The function returns |
xorjoep | 1:24714b45cd1b | 67 | * <code>ARM_MATH_SIZE_MISMATCH</code> if the input matrix is not square or if the size |
xorjoep | 1:24714b45cd1b | 68 | * of the output matrix does not match the size of the input matrix. |
xorjoep | 1:24714b45cd1b | 69 | * If the input matrix is found to be singular (non-invertible), then the function returns |
xorjoep | 1:24714b45cd1b | 70 | * <code>ARM_MATH_SINGULAR</code>. Otherwise, the function returns <code>ARM_MATH_SUCCESS</code>. |
xorjoep | 1:24714b45cd1b | 71 | */ |
xorjoep | 1:24714b45cd1b | 72 | |
xorjoep | 1:24714b45cd1b | 73 | arm_status arm_mat_inverse_f32( |
xorjoep | 1:24714b45cd1b | 74 | const arm_matrix_instance_f32 * pSrc, |
xorjoep | 1:24714b45cd1b | 75 | arm_matrix_instance_f32 * pDst) |
xorjoep | 1:24714b45cd1b | 76 | { |
xorjoep | 1:24714b45cd1b | 77 | float32_t *pIn = pSrc->pData; /* input data matrix pointer */ |
xorjoep | 1:24714b45cd1b | 78 | float32_t *pOut = pDst->pData; /* output data matrix pointer */ |
xorjoep | 1:24714b45cd1b | 79 | float32_t *pInT1, *pInT2; /* Temporary input data matrix pointer */ |
xorjoep | 1:24714b45cd1b | 80 | float32_t *pOutT1, *pOutT2; /* Temporary output data matrix pointer */ |
xorjoep | 1:24714b45cd1b | 81 | float32_t *pPivotRowIn, *pPRT_in, *pPivotRowDst, *pPRT_pDst; /* Temporary input and output data matrix pointer */ |
xorjoep | 1:24714b45cd1b | 82 | uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */ |
xorjoep | 1:24714b45cd1b | 83 | uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */ |
xorjoep | 1:24714b45cd1b | 84 | |
xorjoep | 1:24714b45cd1b | 85 | #if defined (ARM_MATH_DSP) |
xorjoep | 1:24714b45cd1b | 86 | float32_t maxC; /* maximum value in the column */ |
xorjoep | 1:24714b45cd1b | 87 | |
xorjoep | 1:24714b45cd1b | 88 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
xorjoep | 1:24714b45cd1b | 89 | |
xorjoep | 1:24714b45cd1b | 90 | float32_t Xchg, in = 0.0f, in1; /* Temporary input values */ |
xorjoep | 1:24714b45cd1b | 91 | uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */ |
xorjoep | 1:24714b45cd1b | 92 | arm_status status; /* status of matrix inverse */ |
xorjoep | 1:24714b45cd1b | 93 | |
xorjoep | 1:24714b45cd1b | 94 | #ifdef ARM_MATH_MATRIX_CHECK |
xorjoep | 1:24714b45cd1b | 95 | |
xorjoep | 1:24714b45cd1b | 96 | |
xorjoep | 1:24714b45cd1b | 97 | /* Check for matrix mismatch condition */ |
xorjoep | 1:24714b45cd1b | 98 | if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols) |
xorjoep | 1:24714b45cd1b | 99 | || (pSrc->numRows != pDst->numRows)) |
xorjoep | 1:24714b45cd1b | 100 | { |
xorjoep | 1:24714b45cd1b | 101 | /* Set status as ARM_MATH_SIZE_MISMATCH */ |
xorjoep | 1:24714b45cd1b | 102 | status = ARM_MATH_SIZE_MISMATCH; |
xorjoep | 1:24714b45cd1b | 103 | } |
xorjoep | 1:24714b45cd1b | 104 | else |
xorjoep | 1:24714b45cd1b | 105 | #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ |
xorjoep | 1:24714b45cd1b | 106 | |
xorjoep | 1:24714b45cd1b | 107 | { |
xorjoep | 1:24714b45cd1b | 108 | |
xorjoep | 1:24714b45cd1b | 109 | /*-------------------------------------------------------------------------------------------------------------- |
xorjoep | 1:24714b45cd1b | 110 | * Matrix Inverse can be solved using elementary row operations. |
xorjoep | 1:24714b45cd1b | 111 | * |
xorjoep | 1:24714b45cd1b | 112 | * Gauss-Jordan Method: |
xorjoep | 1:24714b45cd1b | 113 | * |
xorjoep | 1:24714b45cd1b | 114 | * 1. First combine the identity matrix and the input matrix separated by a bar to form an |
xorjoep | 1:24714b45cd1b | 115 | * augmented matrix as follows: |
xorjoep | 1:24714b45cd1b | 116 | * _ _ _ _ |
xorjoep | 1:24714b45cd1b | 117 | * | a11 a12 | 1 0 | | X11 X12 | |
xorjoep | 1:24714b45cd1b | 118 | * | | | = | | |
xorjoep | 1:24714b45cd1b | 119 | * |_ a21 a22 | 0 1 _| |_ X21 X21 _| |
xorjoep | 1:24714b45cd1b | 120 | * |
xorjoep | 1:24714b45cd1b | 121 | * 2. In our implementation, pDst Matrix is used as identity matrix. |
xorjoep | 1:24714b45cd1b | 122 | * |
xorjoep | 1:24714b45cd1b | 123 | * 3. Begin with the first row. Let i = 1. |
xorjoep | 1:24714b45cd1b | 124 | * |
xorjoep | 1:24714b45cd1b | 125 | * 4. Check to see if the pivot for column i is the greatest of the column. |
xorjoep | 1:24714b45cd1b | 126 | * The pivot is the element of the main diagonal that is on the current row. |
xorjoep | 1:24714b45cd1b | 127 | * For instance, if working with row i, then the pivot element is aii. |
xorjoep | 1:24714b45cd1b | 128 | * If the pivot is not the most significant of the columns, exchange that row with a row |
xorjoep | 1:24714b45cd1b | 129 | * below it that does contain the most significant value in column i. If the most |
xorjoep | 1:24714b45cd1b | 130 | * significant value of the column is zero, then an inverse to that matrix does not exist. |
xorjoep | 1:24714b45cd1b | 131 | * The most significant value of the column is the absolute maximum. |
xorjoep | 1:24714b45cd1b | 132 | * |
xorjoep | 1:24714b45cd1b | 133 | * 5. Divide every element of row i by the pivot. |
xorjoep | 1:24714b45cd1b | 134 | * |
xorjoep | 1:24714b45cd1b | 135 | * 6. For every row below and row i, replace that row with the sum of that row and |
xorjoep | 1:24714b45cd1b | 136 | * a multiple of row i so that each new element in column i below row i is zero. |
xorjoep | 1:24714b45cd1b | 137 | * |
xorjoep | 1:24714b45cd1b | 138 | * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros |
xorjoep | 1:24714b45cd1b | 139 | * for every element below and above the main diagonal. |
xorjoep | 1:24714b45cd1b | 140 | * |
xorjoep | 1:24714b45cd1b | 141 | * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc). |
xorjoep | 1:24714b45cd1b | 142 | * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst). |
xorjoep | 1:24714b45cd1b | 143 | *----------------------------------------------------------------------------------------------------------------*/ |
xorjoep | 1:24714b45cd1b | 144 | |
xorjoep | 1:24714b45cd1b | 145 | /* Working pointer for destination matrix */ |
xorjoep | 1:24714b45cd1b | 146 | pOutT1 = pOut; |
xorjoep | 1:24714b45cd1b | 147 | |
xorjoep | 1:24714b45cd1b | 148 | /* Loop over the number of rows */ |
xorjoep | 1:24714b45cd1b | 149 | rowCnt = numRows; |
xorjoep | 1:24714b45cd1b | 150 | |
xorjoep | 1:24714b45cd1b | 151 | /* Making the destination matrix as identity matrix */ |
xorjoep | 1:24714b45cd1b | 152 | while (rowCnt > 0U) |
xorjoep | 1:24714b45cd1b | 153 | { |
xorjoep | 1:24714b45cd1b | 154 | /* Writing all zeroes in lower triangle of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 155 | j = numRows - rowCnt; |
xorjoep | 1:24714b45cd1b | 156 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 157 | { |
xorjoep | 1:24714b45cd1b | 158 | *pOutT1++ = 0.0f; |
xorjoep | 1:24714b45cd1b | 159 | j--; |
xorjoep | 1:24714b45cd1b | 160 | } |
xorjoep | 1:24714b45cd1b | 161 | |
xorjoep | 1:24714b45cd1b | 162 | /* Writing all ones in the diagonal of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 163 | *pOutT1++ = 1.0f; |
xorjoep | 1:24714b45cd1b | 164 | |
xorjoep | 1:24714b45cd1b | 165 | /* Writing all zeroes in upper triangle of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 166 | j = rowCnt - 1U; |
xorjoep | 1:24714b45cd1b | 167 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 168 | { |
xorjoep | 1:24714b45cd1b | 169 | *pOutT1++ = 0.0f; |
xorjoep | 1:24714b45cd1b | 170 | j--; |
xorjoep | 1:24714b45cd1b | 171 | } |
xorjoep | 1:24714b45cd1b | 172 | |
xorjoep | 1:24714b45cd1b | 173 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 174 | rowCnt--; |
xorjoep | 1:24714b45cd1b | 175 | } |
xorjoep | 1:24714b45cd1b | 176 | |
xorjoep | 1:24714b45cd1b | 177 | /* Loop over the number of columns of the input matrix. |
xorjoep | 1:24714b45cd1b | 178 | All the elements in each column are processed by the row operations */ |
xorjoep | 1:24714b45cd1b | 179 | loopCnt = numCols; |
xorjoep | 1:24714b45cd1b | 180 | |
xorjoep | 1:24714b45cd1b | 181 | /* Index modifier to navigate through the columns */ |
xorjoep | 1:24714b45cd1b | 182 | l = 0U; |
xorjoep | 1:24714b45cd1b | 183 | |
xorjoep | 1:24714b45cd1b | 184 | while (loopCnt > 0U) |
xorjoep | 1:24714b45cd1b | 185 | { |
xorjoep | 1:24714b45cd1b | 186 | /* Check if the pivot element is zero.. |
xorjoep | 1:24714b45cd1b | 187 | * If it is zero then interchange the row with non zero row below. |
xorjoep | 1:24714b45cd1b | 188 | * If there is no non zero element to replace in the rows below, |
xorjoep | 1:24714b45cd1b | 189 | * then the matrix is Singular. */ |
xorjoep | 1:24714b45cd1b | 190 | |
xorjoep | 1:24714b45cd1b | 191 | /* Working pointer for the input matrix that points |
xorjoep | 1:24714b45cd1b | 192 | * to the pivot element of the particular row */ |
xorjoep | 1:24714b45cd1b | 193 | pInT1 = pIn + (l * numCols); |
xorjoep | 1:24714b45cd1b | 194 | |
xorjoep | 1:24714b45cd1b | 195 | /* Working pointer for the destination matrix that points |
xorjoep | 1:24714b45cd1b | 196 | * to the pivot element of the particular row */ |
xorjoep | 1:24714b45cd1b | 197 | pOutT1 = pOut + (l * numCols); |
xorjoep | 1:24714b45cd1b | 198 | |
xorjoep | 1:24714b45cd1b | 199 | /* Temporary variable to hold the pivot value */ |
xorjoep | 1:24714b45cd1b | 200 | in = *pInT1; |
xorjoep | 1:24714b45cd1b | 201 | |
xorjoep | 1:24714b45cd1b | 202 | /* Grab the most significant value from column l */ |
xorjoep | 1:24714b45cd1b | 203 | maxC = 0; |
xorjoep | 1:24714b45cd1b | 204 | for (i = l; i < numRows; i++) |
xorjoep | 1:24714b45cd1b | 205 | { |
xorjoep | 1:24714b45cd1b | 206 | maxC = *pInT1 > 0 ? (*pInT1 > maxC ? *pInT1 : maxC) : (-*pInT1 > maxC ? -*pInT1 : maxC); |
xorjoep | 1:24714b45cd1b | 207 | pInT1 += numCols; |
xorjoep | 1:24714b45cd1b | 208 | } |
xorjoep | 1:24714b45cd1b | 209 | |
xorjoep | 1:24714b45cd1b | 210 | /* Update the status if the matrix is singular */ |
xorjoep | 1:24714b45cd1b | 211 | if (maxC == 0.0f) |
xorjoep | 1:24714b45cd1b | 212 | { |
xorjoep | 1:24714b45cd1b | 213 | return ARM_MATH_SINGULAR; |
xorjoep | 1:24714b45cd1b | 214 | } |
xorjoep | 1:24714b45cd1b | 215 | |
xorjoep | 1:24714b45cd1b | 216 | /* Restore pInT1 */ |
xorjoep | 1:24714b45cd1b | 217 | pInT1 = pIn; |
xorjoep | 1:24714b45cd1b | 218 | |
xorjoep | 1:24714b45cd1b | 219 | /* Destination pointer modifier */ |
xorjoep | 1:24714b45cd1b | 220 | k = 1U; |
xorjoep | 1:24714b45cd1b | 221 | |
xorjoep | 1:24714b45cd1b | 222 | /* Check if the pivot element is the most significant of the column */ |
xorjoep | 1:24714b45cd1b | 223 | if ( (in > 0.0f ? in : -in) != maxC) |
xorjoep | 1:24714b45cd1b | 224 | { |
xorjoep | 1:24714b45cd1b | 225 | /* Loop over the number rows present below */ |
xorjoep | 1:24714b45cd1b | 226 | i = numRows - (l + 1U); |
xorjoep | 1:24714b45cd1b | 227 | |
xorjoep | 1:24714b45cd1b | 228 | while (i > 0U) |
xorjoep | 1:24714b45cd1b | 229 | { |
xorjoep | 1:24714b45cd1b | 230 | /* Update the input and destination pointers */ |
xorjoep | 1:24714b45cd1b | 231 | pInT2 = pInT1 + (numCols * l); |
xorjoep | 1:24714b45cd1b | 232 | pOutT2 = pOutT1 + (numCols * k); |
xorjoep | 1:24714b45cd1b | 233 | |
xorjoep | 1:24714b45cd1b | 234 | /* Look for the most significant element to |
xorjoep | 1:24714b45cd1b | 235 | * replace in the rows below */ |
xorjoep | 1:24714b45cd1b | 236 | if ((*pInT2 > 0.0f ? *pInT2: -*pInT2) == maxC) |
xorjoep | 1:24714b45cd1b | 237 | { |
xorjoep | 1:24714b45cd1b | 238 | /* Loop over number of columns |
xorjoep | 1:24714b45cd1b | 239 | * to the right of the pilot element */ |
xorjoep | 1:24714b45cd1b | 240 | j = numCols - l; |
xorjoep | 1:24714b45cd1b | 241 | |
xorjoep | 1:24714b45cd1b | 242 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 243 | { |
xorjoep | 1:24714b45cd1b | 244 | /* Exchange the row elements of the input matrix */ |
xorjoep | 1:24714b45cd1b | 245 | Xchg = *pInT2; |
xorjoep | 1:24714b45cd1b | 246 | *pInT2++ = *pInT1; |
xorjoep | 1:24714b45cd1b | 247 | *pInT1++ = Xchg; |
xorjoep | 1:24714b45cd1b | 248 | |
xorjoep | 1:24714b45cd1b | 249 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 250 | j--; |
xorjoep | 1:24714b45cd1b | 251 | } |
xorjoep | 1:24714b45cd1b | 252 | |
xorjoep | 1:24714b45cd1b | 253 | /* Loop over number of columns of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 254 | j = numCols; |
xorjoep | 1:24714b45cd1b | 255 | |
xorjoep | 1:24714b45cd1b | 256 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 257 | { |
xorjoep | 1:24714b45cd1b | 258 | /* Exchange the row elements of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 259 | Xchg = *pOutT2; |
xorjoep | 1:24714b45cd1b | 260 | *pOutT2++ = *pOutT1; |
xorjoep | 1:24714b45cd1b | 261 | *pOutT1++ = Xchg; |
xorjoep | 1:24714b45cd1b | 262 | |
xorjoep | 1:24714b45cd1b | 263 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 264 | j--; |
xorjoep | 1:24714b45cd1b | 265 | } |
xorjoep | 1:24714b45cd1b | 266 | |
xorjoep | 1:24714b45cd1b | 267 | /* Flag to indicate whether exchange is done or not */ |
xorjoep | 1:24714b45cd1b | 268 | flag = 1U; |
xorjoep | 1:24714b45cd1b | 269 | |
xorjoep | 1:24714b45cd1b | 270 | /* Break after exchange is done */ |
xorjoep | 1:24714b45cd1b | 271 | break; |
xorjoep | 1:24714b45cd1b | 272 | } |
xorjoep | 1:24714b45cd1b | 273 | |
xorjoep | 1:24714b45cd1b | 274 | /* Update the destination pointer modifier */ |
xorjoep | 1:24714b45cd1b | 275 | k++; |
xorjoep | 1:24714b45cd1b | 276 | |
xorjoep | 1:24714b45cd1b | 277 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 278 | i--; |
xorjoep | 1:24714b45cd1b | 279 | } |
xorjoep | 1:24714b45cd1b | 280 | } |
xorjoep | 1:24714b45cd1b | 281 | |
xorjoep | 1:24714b45cd1b | 282 | /* Update the status if the matrix is singular */ |
xorjoep | 1:24714b45cd1b | 283 | if ((flag != 1U) && (in == 0.0f)) |
xorjoep | 1:24714b45cd1b | 284 | { |
xorjoep | 1:24714b45cd1b | 285 | return ARM_MATH_SINGULAR; |
xorjoep | 1:24714b45cd1b | 286 | } |
xorjoep | 1:24714b45cd1b | 287 | |
xorjoep | 1:24714b45cd1b | 288 | /* Points to the pivot row of input and destination matrices */ |
xorjoep | 1:24714b45cd1b | 289 | pPivotRowIn = pIn + (l * numCols); |
xorjoep | 1:24714b45cd1b | 290 | pPivotRowDst = pOut + (l * numCols); |
xorjoep | 1:24714b45cd1b | 291 | |
xorjoep | 1:24714b45cd1b | 292 | /* Temporary pointers to the pivot row pointers */ |
xorjoep | 1:24714b45cd1b | 293 | pInT1 = pPivotRowIn; |
xorjoep | 1:24714b45cd1b | 294 | pInT2 = pPivotRowDst; |
xorjoep | 1:24714b45cd1b | 295 | |
xorjoep | 1:24714b45cd1b | 296 | /* Pivot element of the row */ |
xorjoep | 1:24714b45cd1b | 297 | in = *pPivotRowIn; |
xorjoep | 1:24714b45cd1b | 298 | |
xorjoep | 1:24714b45cd1b | 299 | /* Loop over number of columns |
xorjoep | 1:24714b45cd1b | 300 | * to the right of the pilot element */ |
xorjoep | 1:24714b45cd1b | 301 | j = (numCols - l); |
xorjoep | 1:24714b45cd1b | 302 | |
xorjoep | 1:24714b45cd1b | 303 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 304 | { |
xorjoep | 1:24714b45cd1b | 305 | /* Divide each element of the row of the input matrix |
xorjoep | 1:24714b45cd1b | 306 | * by the pivot element */ |
xorjoep | 1:24714b45cd1b | 307 | in1 = *pInT1; |
xorjoep | 1:24714b45cd1b | 308 | *pInT1++ = in1 / in; |
xorjoep | 1:24714b45cd1b | 309 | |
xorjoep | 1:24714b45cd1b | 310 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 311 | j--; |
xorjoep | 1:24714b45cd1b | 312 | } |
xorjoep | 1:24714b45cd1b | 313 | |
xorjoep | 1:24714b45cd1b | 314 | /* Loop over number of columns of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 315 | j = numCols; |
xorjoep | 1:24714b45cd1b | 316 | |
xorjoep | 1:24714b45cd1b | 317 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 318 | { |
xorjoep | 1:24714b45cd1b | 319 | /* Divide each element of the row of the destination matrix |
xorjoep | 1:24714b45cd1b | 320 | * by the pivot element */ |
xorjoep | 1:24714b45cd1b | 321 | in1 = *pInT2; |
xorjoep | 1:24714b45cd1b | 322 | *pInT2++ = in1 / in; |
xorjoep | 1:24714b45cd1b | 323 | |
xorjoep | 1:24714b45cd1b | 324 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 325 | j--; |
xorjoep | 1:24714b45cd1b | 326 | } |
xorjoep | 1:24714b45cd1b | 327 | |
xorjoep | 1:24714b45cd1b | 328 | /* Replace the rows with the sum of that row and a multiple of row i |
xorjoep | 1:24714b45cd1b | 329 | * so that each new element in column i above row i is zero.*/ |
xorjoep | 1:24714b45cd1b | 330 | |
xorjoep | 1:24714b45cd1b | 331 | /* Temporary pointers for input and destination matrices */ |
xorjoep | 1:24714b45cd1b | 332 | pInT1 = pIn; |
xorjoep | 1:24714b45cd1b | 333 | pInT2 = pOut; |
xorjoep | 1:24714b45cd1b | 334 | |
xorjoep | 1:24714b45cd1b | 335 | /* index used to check for pivot element */ |
xorjoep | 1:24714b45cd1b | 336 | i = 0U; |
xorjoep | 1:24714b45cd1b | 337 | |
xorjoep | 1:24714b45cd1b | 338 | /* Loop over number of rows */ |
xorjoep | 1:24714b45cd1b | 339 | /* to be replaced by the sum of that row and a multiple of row i */ |
xorjoep | 1:24714b45cd1b | 340 | k = numRows; |
xorjoep | 1:24714b45cd1b | 341 | |
xorjoep | 1:24714b45cd1b | 342 | while (k > 0U) |
xorjoep | 1:24714b45cd1b | 343 | { |
xorjoep | 1:24714b45cd1b | 344 | /* Check for the pivot element */ |
xorjoep | 1:24714b45cd1b | 345 | if (i == l) |
xorjoep | 1:24714b45cd1b | 346 | { |
xorjoep | 1:24714b45cd1b | 347 | /* If the processing element is the pivot element, |
xorjoep | 1:24714b45cd1b | 348 | only the columns to the right are to be processed */ |
xorjoep | 1:24714b45cd1b | 349 | pInT1 += numCols - l; |
xorjoep | 1:24714b45cd1b | 350 | |
xorjoep | 1:24714b45cd1b | 351 | pInT2 += numCols; |
xorjoep | 1:24714b45cd1b | 352 | } |
xorjoep | 1:24714b45cd1b | 353 | else |
xorjoep | 1:24714b45cd1b | 354 | { |
xorjoep | 1:24714b45cd1b | 355 | /* Element of the reference row */ |
xorjoep | 1:24714b45cd1b | 356 | in = *pInT1; |
xorjoep | 1:24714b45cd1b | 357 | |
xorjoep | 1:24714b45cd1b | 358 | /* Working pointers for input and destination pivot rows */ |
xorjoep | 1:24714b45cd1b | 359 | pPRT_in = pPivotRowIn; |
xorjoep | 1:24714b45cd1b | 360 | pPRT_pDst = pPivotRowDst; |
xorjoep | 1:24714b45cd1b | 361 | |
xorjoep | 1:24714b45cd1b | 362 | /* Loop over the number of columns to the right of the pivot element, |
xorjoep | 1:24714b45cd1b | 363 | to replace the elements in the input matrix */ |
xorjoep | 1:24714b45cd1b | 364 | j = (numCols - l); |
xorjoep | 1:24714b45cd1b | 365 | |
xorjoep | 1:24714b45cd1b | 366 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 367 | { |
xorjoep | 1:24714b45cd1b | 368 | /* Replace the element by the sum of that row |
xorjoep | 1:24714b45cd1b | 369 | and a multiple of the reference row */ |
xorjoep | 1:24714b45cd1b | 370 | in1 = *pInT1; |
xorjoep | 1:24714b45cd1b | 371 | *pInT1++ = in1 - (in * *pPRT_in++); |
xorjoep | 1:24714b45cd1b | 372 | |
xorjoep | 1:24714b45cd1b | 373 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 374 | j--; |
xorjoep | 1:24714b45cd1b | 375 | } |
xorjoep | 1:24714b45cd1b | 376 | |
xorjoep | 1:24714b45cd1b | 377 | /* Loop over the number of columns to |
xorjoep | 1:24714b45cd1b | 378 | replace the elements in the destination matrix */ |
xorjoep | 1:24714b45cd1b | 379 | j = numCols; |
xorjoep | 1:24714b45cd1b | 380 | |
xorjoep | 1:24714b45cd1b | 381 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 382 | { |
xorjoep | 1:24714b45cd1b | 383 | /* Replace the element by the sum of that row |
xorjoep | 1:24714b45cd1b | 384 | and a multiple of the reference row */ |
xorjoep | 1:24714b45cd1b | 385 | in1 = *pInT2; |
xorjoep | 1:24714b45cd1b | 386 | *pInT2++ = in1 - (in * *pPRT_pDst++); |
xorjoep | 1:24714b45cd1b | 387 | |
xorjoep | 1:24714b45cd1b | 388 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 389 | j--; |
xorjoep | 1:24714b45cd1b | 390 | } |
xorjoep | 1:24714b45cd1b | 391 | |
xorjoep | 1:24714b45cd1b | 392 | } |
xorjoep | 1:24714b45cd1b | 393 | |
xorjoep | 1:24714b45cd1b | 394 | /* Increment the temporary input pointer */ |
xorjoep | 1:24714b45cd1b | 395 | pInT1 = pInT1 + l; |
xorjoep | 1:24714b45cd1b | 396 | |
xorjoep | 1:24714b45cd1b | 397 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 398 | k--; |
xorjoep | 1:24714b45cd1b | 399 | |
xorjoep | 1:24714b45cd1b | 400 | /* Increment the pivot index */ |
xorjoep | 1:24714b45cd1b | 401 | i++; |
xorjoep | 1:24714b45cd1b | 402 | } |
xorjoep | 1:24714b45cd1b | 403 | |
xorjoep | 1:24714b45cd1b | 404 | /* Increment the input pointer */ |
xorjoep | 1:24714b45cd1b | 405 | pIn++; |
xorjoep | 1:24714b45cd1b | 406 | |
xorjoep | 1:24714b45cd1b | 407 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 408 | loopCnt--; |
xorjoep | 1:24714b45cd1b | 409 | |
xorjoep | 1:24714b45cd1b | 410 | /* Increment the index modifier */ |
xorjoep | 1:24714b45cd1b | 411 | l++; |
xorjoep | 1:24714b45cd1b | 412 | } |
xorjoep | 1:24714b45cd1b | 413 | |
xorjoep | 1:24714b45cd1b | 414 | |
xorjoep | 1:24714b45cd1b | 415 | #else |
xorjoep | 1:24714b45cd1b | 416 | |
xorjoep | 1:24714b45cd1b | 417 | /* Run the below code for Cortex-M0 */ |
xorjoep | 1:24714b45cd1b | 418 | |
xorjoep | 1:24714b45cd1b | 419 | float32_t Xchg, in = 0.0f; /* Temporary input values */ |
xorjoep | 1:24714b45cd1b | 420 | uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */ |
xorjoep | 1:24714b45cd1b | 421 | arm_status status; /* status of matrix inverse */ |
xorjoep | 1:24714b45cd1b | 422 | |
xorjoep | 1:24714b45cd1b | 423 | #ifdef ARM_MATH_MATRIX_CHECK |
xorjoep | 1:24714b45cd1b | 424 | |
xorjoep | 1:24714b45cd1b | 425 | /* Check for matrix mismatch condition */ |
xorjoep | 1:24714b45cd1b | 426 | if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols) |
xorjoep | 1:24714b45cd1b | 427 | || (pSrc->numRows != pDst->numRows)) |
xorjoep | 1:24714b45cd1b | 428 | { |
xorjoep | 1:24714b45cd1b | 429 | /* Set status as ARM_MATH_SIZE_MISMATCH */ |
xorjoep | 1:24714b45cd1b | 430 | status = ARM_MATH_SIZE_MISMATCH; |
xorjoep | 1:24714b45cd1b | 431 | } |
xorjoep | 1:24714b45cd1b | 432 | else |
xorjoep | 1:24714b45cd1b | 433 | #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ |
xorjoep | 1:24714b45cd1b | 434 | { |
xorjoep | 1:24714b45cd1b | 435 | |
xorjoep | 1:24714b45cd1b | 436 | /*-------------------------------------------------------------------------------------------------------------- |
xorjoep | 1:24714b45cd1b | 437 | * Matrix Inverse can be solved using elementary row operations. |
xorjoep | 1:24714b45cd1b | 438 | * |
xorjoep | 1:24714b45cd1b | 439 | * Gauss-Jordan Method: |
xorjoep | 1:24714b45cd1b | 440 | * |
xorjoep | 1:24714b45cd1b | 441 | * 1. First combine the identity matrix and the input matrix separated by a bar to form an |
xorjoep | 1:24714b45cd1b | 442 | * augmented matrix as follows: |
xorjoep | 1:24714b45cd1b | 443 | * _ _ _ _ _ _ _ _ |
xorjoep | 1:24714b45cd1b | 444 | * | | a11 a12 | | | 1 0 | | | X11 X12 | |
xorjoep | 1:24714b45cd1b | 445 | * | | | | | | | = | | |
xorjoep | 1:24714b45cd1b | 446 | * |_ |_ a21 a22 _| | |_0 1 _| _| |_ X21 X21 _| |
xorjoep | 1:24714b45cd1b | 447 | * |
xorjoep | 1:24714b45cd1b | 448 | * 2. In our implementation, pDst Matrix is used as identity matrix. |
xorjoep | 1:24714b45cd1b | 449 | * |
xorjoep | 1:24714b45cd1b | 450 | * 3. Begin with the first row. Let i = 1. |
xorjoep | 1:24714b45cd1b | 451 | * |
xorjoep | 1:24714b45cd1b | 452 | * 4. Check to see if the pivot for row i is zero. |
xorjoep | 1:24714b45cd1b | 453 | * The pivot is the element of the main diagonal that is on the current row. |
xorjoep | 1:24714b45cd1b | 454 | * For instance, if working with row i, then the pivot element is aii. |
xorjoep | 1:24714b45cd1b | 455 | * If the pivot is zero, exchange that row with a row below it that does not |
xorjoep | 1:24714b45cd1b | 456 | * contain a zero in column i. If this is not possible, then an inverse |
xorjoep | 1:24714b45cd1b | 457 | * to that matrix does not exist. |
xorjoep | 1:24714b45cd1b | 458 | * |
xorjoep | 1:24714b45cd1b | 459 | * 5. Divide every element of row i by the pivot. |
xorjoep | 1:24714b45cd1b | 460 | * |
xorjoep | 1:24714b45cd1b | 461 | * 6. For every row below and row i, replace that row with the sum of that row and |
xorjoep | 1:24714b45cd1b | 462 | * a multiple of row i so that each new element in column i below row i is zero. |
xorjoep | 1:24714b45cd1b | 463 | * |
xorjoep | 1:24714b45cd1b | 464 | * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros |
xorjoep | 1:24714b45cd1b | 465 | * for every element below and above the main diagonal. |
xorjoep | 1:24714b45cd1b | 466 | * |
xorjoep | 1:24714b45cd1b | 467 | * 8. Now an identical matrix is formed to the left of the bar(input matrix, src). |
xorjoep | 1:24714b45cd1b | 468 | * Therefore, the matrix to the right of the bar is our solution(dst matrix, dst). |
xorjoep | 1:24714b45cd1b | 469 | *----------------------------------------------------------------------------------------------------------------*/ |
xorjoep | 1:24714b45cd1b | 470 | |
xorjoep | 1:24714b45cd1b | 471 | /* Working pointer for destination matrix */ |
xorjoep | 1:24714b45cd1b | 472 | pOutT1 = pOut; |
xorjoep | 1:24714b45cd1b | 473 | |
xorjoep | 1:24714b45cd1b | 474 | /* Loop over the number of rows */ |
xorjoep | 1:24714b45cd1b | 475 | rowCnt = numRows; |
xorjoep | 1:24714b45cd1b | 476 | |
xorjoep | 1:24714b45cd1b | 477 | /* Making the destination matrix as identity matrix */ |
xorjoep | 1:24714b45cd1b | 478 | while (rowCnt > 0U) |
xorjoep | 1:24714b45cd1b | 479 | { |
xorjoep | 1:24714b45cd1b | 480 | /* Writing all zeroes in lower triangle of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 481 | j = numRows - rowCnt; |
xorjoep | 1:24714b45cd1b | 482 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 483 | { |
xorjoep | 1:24714b45cd1b | 484 | *pOutT1++ = 0.0f; |
xorjoep | 1:24714b45cd1b | 485 | j--; |
xorjoep | 1:24714b45cd1b | 486 | } |
xorjoep | 1:24714b45cd1b | 487 | |
xorjoep | 1:24714b45cd1b | 488 | /* Writing all ones in the diagonal of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 489 | *pOutT1++ = 1.0f; |
xorjoep | 1:24714b45cd1b | 490 | |
xorjoep | 1:24714b45cd1b | 491 | /* Writing all zeroes in upper triangle of the destination matrix */ |
xorjoep | 1:24714b45cd1b | 492 | j = rowCnt - 1U; |
xorjoep | 1:24714b45cd1b | 493 | while (j > 0U) |
xorjoep | 1:24714b45cd1b | 494 | { |
xorjoep | 1:24714b45cd1b | 495 | *pOutT1++ = 0.0f; |
xorjoep | 1:24714b45cd1b | 496 | j--; |
xorjoep | 1:24714b45cd1b | 497 | } |
xorjoep | 1:24714b45cd1b | 498 | |
xorjoep | 1:24714b45cd1b | 499 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 500 | rowCnt--; |
xorjoep | 1:24714b45cd1b | 501 | } |
xorjoep | 1:24714b45cd1b | 502 | |
xorjoep | 1:24714b45cd1b | 503 | /* Loop over the number of columns of the input matrix. |
xorjoep | 1:24714b45cd1b | 504 | All the elements in each column are processed by the row operations */ |
xorjoep | 1:24714b45cd1b | 505 | loopCnt = numCols; |
xorjoep | 1:24714b45cd1b | 506 | |
xorjoep | 1:24714b45cd1b | 507 | /* Index modifier to navigate through the columns */ |
xorjoep | 1:24714b45cd1b | 508 | l = 0U; |
xorjoep | 1:24714b45cd1b | 509 | //for(loopCnt = 0U; loopCnt < numCols; loopCnt++) |
xorjoep | 1:24714b45cd1b | 510 | while (loopCnt > 0U) |
xorjoep | 1:24714b45cd1b | 511 | { |
xorjoep | 1:24714b45cd1b | 512 | /* Check if the pivot element is zero.. |
xorjoep | 1:24714b45cd1b | 513 | * If it is zero then interchange the row with non zero row below. |
xorjoep | 1:24714b45cd1b | 514 | * If there is no non zero element to replace in the rows below, |
xorjoep | 1:24714b45cd1b | 515 | * then the matrix is Singular. */ |
xorjoep | 1:24714b45cd1b | 516 | |
xorjoep | 1:24714b45cd1b | 517 | /* Working pointer for the input matrix that points |
xorjoep | 1:24714b45cd1b | 518 | * to the pivot element of the particular row */ |
xorjoep | 1:24714b45cd1b | 519 | pInT1 = pIn + (l * numCols); |
xorjoep | 1:24714b45cd1b | 520 | |
xorjoep | 1:24714b45cd1b | 521 | /* Working pointer for the destination matrix that points |
xorjoep | 1:24714b45cd1b | 522 | * to the pivot element of the particular row */ |
xorjoep | 1:24714b45cd1b | 523 | pOutT1 = pOut + (l * numCols); |
xorjoep | 1:24714b45cd1b | 524 | |
xorjoep | 1:24714b45cd1b | 525 | /* Temporary variable to hold the pivot value */ |
xorjoep | 1:24714b45cd1b | 526 | in = *pInT1; |
xorjoep | 1:24714b45cd1b | 527 | |
xorjoep | 1:24714b45cd1b | 528 | /* Destination pointer modifier */ |
xorjoep | 1:24714b45cd1b | 529 | k = 1U; |
xorjoep | 1:24714b45cd1b | 530 | |
xorjoep | 1:24714b45cd1b | 531 | /* Check if the pivot element is zero */ |
xorjoep | 1:24714b45cd1b | 532 | if (*pInT1 == 0.0f) |
xorjoep | 1:24714b45cd1b | 533 | { |
xorjoep | 1:24714b45cd1b | 534 | /* Loop over the number rows present below */ |
xorjoep | 1:24714b45cd1b | 535 | for (i = (l + 1U); i < numRows; i++) |
xorjoep | 1:24714b45cd1b | 536 | { |
xorjoep | 1:24714b45cd1b | 537 | /* Update the input and destination pointers */ |
xorjoep | 1:24714b45cd1b | 538 | pInT2 = pInT1 + (numCols * l); |
xorjoep | 1:24714b45cd1b | 539 | pOutT2 = pOutT1 + (numCols * k); |
xorjoep | 1:24714b45cd1b | 540 | |
xorjoep | 1:24714b45cd1b | 541 | /* Check if there is a non zero pivot element to |
xorjoep | 1:24714b45cd1b | 542 | * replace in the rows below */ |
xorjoep | 1:24714b45cd1b | 543 | if (*pInT2 != 0.0f) |
xorjoep | 1:24714b45cd1b | 544 | { |
xorjoep | 1:24714b45cd1b | 545 | /* Loop over number of columns |
xorjoep | 1:24714b45cd1b | 546 | * to the right of the pilot element */ |
xorjoep | 1:24714b45cd1b | 547 | for (j = 0U; j < (numCols - l); j++) |
xorjoep | 1:24714b45cd1b | 548 | { |
xorjoep | 1:24714b45cd1b | 549 | /* Exchange the row elements of the input matrix */ |
xorjoep | 1:24714b45cd1b | 550 | Xchg = *pInT2; |
xorjoep | 1:24714b45cd1b | 551 | *pInT2++ = *pInT1; |
xorjoep | 1:24714b45cd1b | 552 | *pInT1++ = Xchg; |
xorjoep | 1:24714b45cd1b | 553 | } |
xorjoep | 1:24714b45cd1b | 554 | |
xorjoep | 1:24714b45cd1b | 555 | for (j = 0U; j < numCols; j++) |
xorjoep | 1:24714b45cd1b | 556 | { |
xorjoep | 1:24714b45cd1b | 557 | Xchg = *pOutT2; |
xorjoep | 1:24714b45cd1b | 558 | *pOutT2++ = *pOutT1; |
xorjoep | 1:24714b45cd1b | 559 | *pOutT1++ = Xchg; |
xorjoep | 1:24714b45cd1b | 560 | } |
xorjoep | 1:24714b45cd1b | 561 | |
xorjoep | 1:24714b45cd1b | 562 | /* Flag to indicate whether exchange is done or not */ |
xorjoep | 1:24714b45cd1b | 563 | flag = 1U; |
xorjoep | 1:24714b45cd1b | 564 | |
xorjoep | 1:24714b45cd1b | 565 | /* Break after exchange is done */ |
xorjoep | 1:24714b45cd1b | 566 | break; |
xorjoep | 1:24714b45cd1b | 567 | } |
xorjoep | 1:24714b45cd1b | 568 | |
xorjoep | 1:24714b45cd1b | 569 | /* Update the destination pointer modifier */ |
xorjoep | 1:24714b45cd1b | 570 | k++; |
xorjoep | 1:24714b45cd1b | 571 | } |
xorjoep | 1:24714b45cd1b | 572 | } |
xorjoep | 1:24714b45cd1b | 573 | |
xorjoep | 1:24714b45cd1b | 574 | /* Update the status if the matrix is singular */ |
xorjoep | 1:24714b45cd1b | 575 | if ((flag != 1U) && (in == 0.0f)) |
xorjoep | 1:24714b45cd1b | 576 | { |
xorjoep | 1:24714b45cd1b | 577 | return ARM_MATH_SINGULAR; |
xorjoep | 1:24714b45cd1b | 578 | } |
xorjoep | 1:24714b45cd1b | 579 | |
xorjoep | 1:24714b45cd1b | 580 | /* Points to the pivot row of input and destination matrices */ |
xorjoep | 1:24714b45cd1b | 581 | pPivotRowIn = pIn + (l * numCols); |
xorjoep | 1:24714b45cd1b | 582 | pPivotRowDst = pOut + (l * numCols); |
xorjoep | 1:24714b45cd1b | 583 | |
xorjoep | 1:24714b45cd1b | 584 | /* Temporary pointers to the pivot row pointers */ |
xorjoep | 1:24714b45cd1b | 585 | pInT1 = pPivotRowIn; |
xorjoep | 1:24714b45cd1b | 586 | pOutT1 = pPivotRowDst; |
xorjoep | 1:24714b45cd1b | 587 | |
xorjoep | 1:24714b45cd1b | 588 | /* Pivot element of the row */ |
xorjoep | 1:24714b45cd1b | 589 | in = *(pIn + (l * numCols)); |
xorjoep | 1:24714b45cd1b | 590 | |
xorjoep | 1:24714b45cd1b | 591 | /* Loop over number of columns |
xorjoep | 1:24714b45cd1b | 592 | * to the right of the pilot element */ |
xorjoep | 1:24714b45cd1b | 593 | for (j = 0U; j < (numCols - l); j++) |
xorjoep | 1:24714b45cd1b | 594 | { |
xorjoep | 1:24714b45cd1b | 595 | /* Divide each element of the row of the input matrix |
xorjoep | 1:24714b45cd1b | 596 | * by the pivot element */ |
xorjoep | 1:24714b45cd1b | 597 | *pInT1 = *pInT1 / in; |
xorjoep | 1:24714b45cd1b | 598 | pInT1++; |
xorjoep | 1:24714b45cd1b | 599 | } |
xorjoep | 1:24714b45cd1b | 600 | for (j = 0U; j < numCols; j++) |
xorjoep | 1:24714b45cd1b | 601 | { |
xorjoep | 1:24714b45cd1b | 602 | /* Divide each element of the row of the destination matrix |
xorjoep | 1:24714b45cd1b | 603 | * by the pivot element */ |
xorjoep | 1:24714b45cd1b | 604 | *pOutT1 = *pOutT1 / in; |
xorjoep | 1:24714b45cd1b | 605 | pOutT1++; |
xorjoep | 1:24714b45cd1b | 606 | } |
xorjoep | 1:24714b45cd1b | 607 | |
xorjoep | 1:24714b45cd1b | 608 | /* Replace the rows with the sum of that row and a multiple of row i |
xorjoep | 1:24714b45cd1b | 609 | * so that each new element in column i above row i is zero.*/ |
xorjoep | 1:24714b45cd1b | 610 | |
xorjoep | 1:24714b45cd1b | 611 | /* Temporary pointers for input and destination matrices */ |
xorjoep | 1:24714b45cd1b | 612 | pInT1 = pIn; |
xorjoep | 1:24714b45cd1b | 613 | pOutT1 = pOut; |
xorjoep | 1:24714b45cd1b | 614 | |
xorjoep | 1:24714b45cd1b | 615 | for (i = 0U; i < numRows; i++) |
xorjoep | 1:24714b45cd1b | 616 | { |
xorjoep | 1:24714b45cd1b | 617 | /* Check for the pivot element */ |
xorjoep | 1:24714b45cd1b | 618 | if (i == l) |
xorjoep | 1:24714b45cd1b | 619 | { |
xorjoep | 1:24714b45cd1b | 620 | /* If the processing element is the pivot element, |
xorjoep | 1:24714b45cd1b | 621 | only the columns to the right are to be processed */ |
xorjoep | 1:24714b45cd1b | 622 | pInT1 += numCols - l; |
xorjoep | 1:24714b45cd1b | 623 | pOutT1 += numCols; |
xorjoep | 1:24714b45cd1b | 624 | } |
xorjoep | 1:24714b45cd1b | 625 | else |
xorjoep | 1:24714b45cd1b | 626 | { |
xorjoep | 1:24714b45cd1b | 627 | /* Element of the reference row */ |
xorjoep | 1:24714b45cd1b | 628 | in = *pInT1; |
xorjoep | 1:24714b45cd1b | 629 | |
xorjoep | 1:24714b45cd1b | 630 | /* Working pointers for input and destination pivot rows */ |
xorjoep | 1:24714b45cd1b | 631 | pPRT_in = pPivotRowIn; |
xorjoep | 1:24714b45cd1b | 632 | pPRT_pDst = pPivotRowDst; |
xorjoep | 1:24714b45cd1b | 633 | |
xorjoep | 1:24714b45cd1b | 634 | /* Loop over the number of columns to the right of the pivot element, |
xorjoep | 1:24714b45cd1b | 635 | to replace the elements in the input matrix */ |
xorjoep | 1:24714b45cd1b | 636 | for (j = 0U; j < (numCols - l); j++) |
xorjoep | 1:24714b45cd1b | 637 | { |
xorjoep | 1:24714b45cd1b | 638 | /* Replace the element by the sum of that row |
xorjoep | 1:24714b45cd1b | 639 | and a multiple of the reference row */ |
xorjoep | 1:24714b45cd1b | 640 | *pInT1 = *pInT1 - (in * *pPRT_in++); |
xorjoep | 1:24714b45cd1b | 641 | pInT1++; |
xorjoep | 1:24714b45cd1b | 642 | } |
xorjoep | 1:24714b45cd1b | 643 | /* Loop over the number of columns to |
xorjoep | 1:24714b45cd1b | 644 | replace the elements in the destination matrix */ |
xorjoep | 1:24714b45cd1b | 645 | for (j = 0U; j < numCols; j++) |
xorjoep | 1:24714b45cd1b | 646 | { |
xorjoep | 1:24714b45cd1b | 647 | /* Replace the element by the sum of that row |
xorjoep | 1:24714b45cd1b | 648 | and a multiple of the reference row */ |
xorjoep | 1:24714b45cd1b | 649 | *pOutT1 = *pOutT1 - (in * *pPRT_pDst++); |
xorjoep | 1:24714b45cd1b | 650 | pOutT1++; |
xorjoep | 1:24714b45cd1b | 651 | } |
xorjoep | 1:24714b45cd1b | 652 | |
xorjoep | 1:24714b45cd1b | 653 | } |
xorjoep | 1:24714b45cd1b | 654 | /* Increment the temporary input pointer */ |
xorjoep | 1:24714b45cd1b | 655 | pInT1 = pInT1 + l; |
xorjoep | 1:24714b45cd1b | 656 | } |
xorjoep | 1:24714b45cd1b | 657 | /* Increment the input pointer */ |
xorjoep | 1:24714b45cd1b | 658 | pIn++; |
xorjoep | 1:24714b45cd1b | 659 | |
xorjoep | 1:24714b45cd1b | 660 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 661 | loopCnt--; |
xorjoep | 1:24714b45cd1b | 662 | /* Increment the index modifier */ |
xorjoep | 1:24714b45cd1b | 663 | l++; |
xorjoep | 1:24714b45cd1b | 664 | } |
xorjoep | 1:24714b45cd1b | 665 | |
xorjoep | 1:24714b45cd1b | 666 | |
xorjoep | 1:24714b45cd1b | 667 | #endif /* #if defined (ARM_MATH_DSP) */ |
xorjoep | 1:24714b45cd1b | 668 | |
xorjoep | 1:24714b45cd1b | 669 | /* Set status as ARM_MATH_SUCCESS */ |
xorjoep | 1:24714b45cd1b | 670 | status = ARM_MATH_SUCCESS; |
xorjoep | 1:24714b45cd1b | 671 | |
xorjoep | 1:24714b45cd1b | 672 | if ((flag != 1U) && (in == 0.0f)) |
xorjoep | 1:24714b45cd1b | 673 | { |
xorjoep | 1:24714b45cd1b | 674 | pIn = pSrc->pData; |
xorjoep | 1:24714b45cd1b | 675 | for (i = 0; i < numRows * numCols; i++) |
xorjoep | 1:24714b45cd1b | 676 | { |
xorjoep | 1:24714b45cd1b | 677 | if (pIn[i] != 0.0f) |
xorjoep | 1:24714b45cd1b | 678 | break; |
xorjoep | 1:24714b45cd1b | 679 | } |
xorjoep | 1:24714b45cd1b | 680 | |
xorjoep | 1:24714b45cd1b | 681 | if (i == numRows * numCols) |
xorjoep | 1:24714b45cd1b | 682 | status = ARM_MATH_SINGULAR; |
xorjoep | 1:24714b45cd1b | 683 | } |
xorjoep | 1:24714b45cd1b | 684 | } |
xorjoep | 1:24714b45cd1b | 685 | /* Return to application */ |
xorjoep | 1:24714b45cd1b | 686 | return (status); |
xorjoep | 1:24714b45cd1b | 687 | } |
xorjoep | 1:24714b45cd1b | 688 | |
xorjoep | 1:24714b45cd1b | 689 | /** |
xorjoep | 1:24714b45cd1b | 690 | * @} end of MatrixInv group |
xorjoep | 1:24714b45cd1b | 691 | */ |