arm_mat_solve_lower_triangular_f64.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_solve_lower_triangular_f64.c
  4. * Description: Solve linear system LT X = A with LT lower triangular matrix
  5. *
  6. * $Date: 10 August 2022
  7. * $Revision: V1.9.1
  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 LT . X = A where LT is a lower triangular matrix
  38. * @param[in] lt The lower triangular matrix
  39. * @param[in] a The matrix a
  40. * @param[out] dst The solution X of LT . X = A
  41. * @return The function returns ARM_MATH_SINGULAR, if the system can't be solved.
  42. */
  43. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE) && defined(__aarch64__)
  44. arm_status arm_mat_solve_lower_triangular_f64(
  45. const arm_matrix_instance_f64 * lt,
  46. const arm_matrix_instance_f64 * a,
  47. arm_matrix_instance_f64 * dst)
  48. {
  49. arm_status status; /* status of matrix inverse */
  50. #ifdef ARM_MATH_MATRIX_CHECK
  51. /* Check for matrix mismatch condition */
  52. if ((lt->numRows != lt->numCols) ||
  53. (lt->numRows != a->numRows) )
  54. {
  55. /* Set status as ARM_MATH_SIZE_MISMATCH */
  56. status = ARM_MATH_SIZE_MISMATCH;
  57. }
  58. else
  59. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  60. {
  61. /* a1 b1 c1 x1 = a1
  62. b2 c2 x2 a2
  63. c3 x3 a3
  64. x3 = a3 / c3
  65. x2 = (a2 - c2 x3) / b2
  66. */
  67. int i,j,k,n,cols;
  68. n = dst->numRows;
  69. cols = dst->numCols;
  70. float64_t *pX = dst->pData;
  71. float64_t *pLT = lt->pData;
  72. float64_t *pA = a->pData;
  73. float64_t *lt_row;
  74. float64_t *a_col;
  75. float64_t invLT;
  76. float64x2_t vecA;
  77. float64x2_t vecX;
  78. for(i=0; i < n ; i++)
  79. {
  80. for(j=0; j+1 < cols; j += 2)
  81. {
  82. vecA = vld1q_f64(&pA[i * cols + j]);
  83. for(k=0; k < i; k++)
  84. {
  85. vecX = vld1q_f64(&pX[cols*k+j]);
  86. vecA = vfmsq_f64(vecA,vdupq_n_f64(pLT[n*i + k]),vecX);
  87. }
  88. if (pLT[n*i + i]==0.0)
  89. {
  90. return(ARM_MATH_SINGULAR);
  91. }
  92. invLT = 1.0 / pLT[n*i + i];
  93. vecA = vmulq_f64(vecA,vdupq_n_f64(invLT));
  94. vst1q_f64(&pX[i*cols+j],vecA);
  95. }
  96. for(; j < cols; j ++)
  97. {
  98. a_col = &pA[j];
  99. lt_row = &pLT[n*i];
  100. float64_t tmp=a_col[i * cols];
  101. for(k=0; k < i; k++)
  102. {
  103. tmp -= lt_row[k] * pX[cols*k+j];
  104. }
  105. if (lt_row[i]==0.0)
  106. {
  107. return(ARM_MATH_SINGULAR);
  108. }
  109. tmp = tmp / lt_row[i];
  110. pX[i*cols+j] = tmp;
  111. }
  112. }
  113. status = ARM_MATH_SUCCESS;
  114. }
  115. /* Return to application */
  116. return (status);
  117. }
  118. #else
  119. arm_status arm_mat_solve_lower_triangular_f64(
  120. const arm_matrix_instance_f64 * lt,
  121. const arm_matrix_instance_f64 * a,
  122. arm_matrix_instance_f64 * dst)
  123. {
  124. arm_status status; /* status of matrix inverse */
  125. #ifdef ARM_MATH_MATRIX_CHECK
  126. /* Check for matrix mismatch condition */
  127. if ((lt->numRows != lt->numCols) ||
  128. (lt->numRows != a->numRows) )
  129. {
  130. /* Set status as ARM_MATH_SIZE_MISMATCH */
  131. status = ARM_MATH_SIZE_MISMATCH;
  132. }
  133. else
  134. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  135. {
  136. /* a1 b1 c1 x1 = a1
  137. b2 c2 x2 a2
  138. c3 x3 a3
  139. x3 = a3 / c3
  140. x2 = (a2 - c2 x3) / b2
  141. */
  142. int i,j,k,n,cols;
  143. float64_t *pX = dst->pData;
  144. float64_t *pLT = lt->pData;
  145. float64_t *pA = a->pData;
  146. float64_t *lt_row;
  147. float64_t *a_col;
  148. n = dst->numRows;
  149. cols = dst->numCols;
  150. for(j=0; j < cols; j ++)
  151. {
  152. a_col = &pA[j];
  153. for(i=0; i < n ; i++)
  154. {
  155. float64_t tmp=a_col[i * cols];
  156. lt_row = &pLT[n*i];
  157. for(k=0; k < i; k++)
  158. {
  159. tmp -= lt_row[k] * pX[cols*k+j];
  160. }
  161. if (lt_row[i]==0.0)
  162. {
  163. return(ARM_MATH_SINGULAR);
  164. }
  165. tmp = tmp / lt_row[i];
  166. pX[i*cols+j] = tmp;
  167. }
  168. }
  169. status = ARM_MATH_SUCCESS;
  170. }
  171. /* Return to application */
  172. return (status);
  173. }
  174. #endif
  175. /**
  176. @} end of MatrixInv group
  177. */