arm_mat_solve_upper_triangular_f32.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_solve_upper_triangular_f32.c
  4. * Description: Solve linear system UT X = A with UT upper triangular matrix
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "dsp/matrix_functions.h"
  29. /**
  30. @ingroup groupMatrix
  31. */
  32. /**
  33. @addtogroup MatrixInv
  34. @{
  35. */
  36. /**
  37. * @brief Solve UT . X = A where UT is an upper triangular matrix
  38. * @param[in] ut The upper triangular matrix
  39. * @param[in] a The matrix a
  40. * @param[out] dst The solution X of UT . X = A
  41. * @return The function returns ARM_MATH_SINGULAR, if the system can't be solved.
  42. */
  43. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. arm_status arm_mat_solve_upper_triangular_f32(
  46. const arm_matrix_instance_f32 * ut,
  47. const arm_matrix_instance_f32 * a,
  48. arm_matrix_instance_f32 * dst)
  49. {
  50. arm_status status; /* status of matrix inverse */
  51. #ifdef ARM_MATH_MATRIX_CHECK
  52. /* Check for matrix mismatch condition */
  53. if ((ut->numRows != ut->numCols) ||
  54. (ut->numRows != a->numRows) )
  55. {
  56. /* Set status as ARM_MATH_SIZE_MISMATCH */
  57. status = ARM_MATH_SIZE_MISMATCH;
  58. }
  59. else
  60. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  61. {
  62. int i,j,k,n,cols;
  63. n = dst->numRows;
  64. cols = dst->numCols;
  65. float32_t *pX = dst->pData;
  66. float32_t *pUT = ut->pData;
  67. float32_t *pA = a->pData;
  68. float32_t *ut_row;
  69. float32_t *a_col;
  70. float32_t invUT;
  71. f32x4_t vecA;
  72. f32x4_t vecX;
  73. for(i=n-1; i >= 0 ; i--)
  74. {
  75. for(j=0; j+3 < cols; j +=4)
  76. {
  77. vecA = vld1q_f32(&pA[i * cols + j]);
  78. for(k=n-1; k > i; k--)
  79. {
  80. vecX = vld1q_f32(&pX[cols*k+j]);
  81. vecA = vfmsq(vecA,vdupq_n_f32(pUT[n*i + k]),vecX);
  82. }
  83. if (pUT[n*i + i]==0.0f)
  84. {
  85. return(ARM_MATH_SINGULAR);
  86. }
  87. invUT = 1.0f / pUT[n*i + i];
  88. vecA = vmulq(vecA,vdupq_n_f32(invUT));
  89. vst1q(&pX[i*cols+j],vecA);
  90. }
  91. for(; j < cols; j ++)
  92. {
  93. a_col = &pA[j];
  94. ut_row = &pUT[n*i];
  95. float32_t tmp=a_col[i * cols];
  96. for(k=n-1; k > i; k--)
  97. {
  98. tmp -= ut_row[k] * pX[cols*k+j];
  99. }
  100. if (ut_row[i]==0.0f)
  101. {
  102. return(ARM_MATH_SINGULAR);
  103. }
  104. tmp = tmp / ut_row[i];
  105. pX[i*cols+j] = tmp;
  106. }
  107. }
  108. status = ARM_MATH_SUCCESS;
  109. }
  110. /* Return to application */
  111. return (status);
  112. }
  113. #else
  114. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  115. arm_status arm_mat_solve_upper_triangular_f32(
  116. const arm_matrix_instance_f32 * ut,
  117. const arm_matrix_instance_f32 * a,
  118. arm_matrix_instance_f32 * dst)
  119. {
  120. arm_status status; /* status of matrix inverse */
  121. #ifdef ARM_MATH_MATRIX_CHECK
  122. /* Check for matrix mismatch condition */
  123. if ((ut->numRows != ut->numCols) ||
  124. (ut->numRows != a->numRows) )
  125. {
  126. /* Set status as ARM_MATH_SIZE_MISMATCH */
  127. status = ARM_MATH_SIZE_MISMATCH;
  128. }
  129. else
  130. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  131. {
  132. int i,j,k,n,cols;
  133. n = dst->numRows;
  134. cols = dst->numCols;
  135. float32_t *pX = dst->pData;
  136. float32_t *pUT = ut->pData;
  137. float32_t *pA = a->pData;
  138. float32_t *ut_row;
  139. float32_t *a_col;
  140. float32_t invUT;
  141. f32x4_t vecA;
  142. f32x4_t vecX;
  143. for(i=n-1; i >= 0 ; i--)
  144. {
  145. for(j=0; j+3 < cols; j +=4)
  146. {
  147. vecA = vld1q_f32(&pA[i * cols + j]);
  148. for(k=n-1; k > i; k--)
  149. {
  150. vecX = vld1q_f32(&pX[cols*k+j]);
  151. vecA = vfmsq_f32(vecA,vdupq_n_f32(pUT[n*i + k]),vecX);
  152. }
  153. if (pUT[n*i + i]==0.0f)
  154. {
  155. return(ARM_MATH_SINGULAR);
  156. }
  157. invUT = 1.0f / pUT[n*i + i];
  158. vecA = vmulq_f32(vecA,vdupq_n_f32(invUT));
  159. vst1q_f32(&pX[i*cols+j],vecA);
  160. }
  161. for(; j < cols; j ++)
  162. {
  163. a_col = &pA[j];
  164. ut_row = &pUT[n*i];
  165. float32_t tmp=a_col[i * cols];
  166. for(k=n-1; k > i; k--)
  167. {
  168. tmp -= ut_row[k] * pX[cols*k+j];
  169. }
  170. if (ut_row[i]==0.0f)
  171. {
  172. return(ARM_MATH_SINGULAR);
  173. }
  174. tmp = tmp / ut_row[i];
  175. pX[i*cols+j] = tmp;
  176. }
  177. }
  178. status = ARM_MATH_SUCCESS;
  179. }
  180. /* Return to application */
  181. return (status);
  182. }
  183. #else
  184. arm_status arm_mat_solve_upper_triangular_f32(
  185. const arm_matrix_instance_f32 * ut,
  186. const arm_matrix_instance_f32 * a,
  187. arm_matrix_instance_f32 * dst)
  188. {
  189. arm_status status; /* status of matrix inverse */
  190. #ifdef ARM_MATH_MATRIX_CHECK
  191. /* Check for matrix mismatch condition */
  192. if ((ut->numRows != ut->numCols) ||
  193. (ut->numRows != a->numRows) )
  194. {
  195. /* Set status as ARM_MATH_SIZE_MISMATCH */
  196. status = ARM_MATH_SIZE_MISMATCH;
  197. }
  198. else
  199. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  200. {
  201. int i,j,k,n,cols;
  202. float32_t *pX = dst->pData;
  203. float32_t *pUT = ut->pData;
  204. float32_t *pA = a->pData;
  205. float32_t *ut_row;
  206. float32_t *a_col;
  207. n = dst->numRows;
  208. cols = dst->numCols;
  209. for(j=0; j < cols; j ++)
  210. {
  211. a_col = &pA[j];
  212. for(i=n-1; i >= 0 ; i--)
  213. {
  214. float32_t tmp=a_col[i * cols];
  215. ut_row = &pUT[n*i];
  216. for(k=n-1; k > i; k--)
  217. {
  218. tmp -= ut_row[k] * pX[cols*k+j];
  219. }
  220. if (ut_row[i]==0.0f)
  221. {
  222. return(ARM_MATH_SINGULAR);
  223. }
  224. tmp = tmp / ut_row[i];
  225. pX[i*cols+j] = tmp;
  226. }
  227. }
  228. status = ARM_MATH_SUCCESS;
  229. }
  230. /* Return to application */
  231. return (status);
  232. }
  233. #endif /* #if defined(ARM_MATH_NEON) */
  234. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  235. /**
  236. @} end of MatrixInv group
  237. */