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