arm_mat_solve_upper_triangular_f64.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_solve_upper_triangular_f64.c
  4. * Description: Solve linear system UT X = A with UT upper 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 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_NEON) && !defined(ARM_MATH_AUTOVECTORIZE) && defined(__aarch64__)
  44. arm_status arm_mat_solve_upper_triangular_f64(
  45. const arm_matrix_instance_f64 * ut,
  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 ((ut->numRows != ut->numCols) ||
  53. (ut->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. int i,j,k,n,cols;
  62. n = dst->numRows;
  63. cols = dst->numCols;
  64. float64_t *pX = dst->pData;
  65. float64_t *pUT = ut->pData;
  66. float64_t *pA = a->pData;
  67. float64_t *ut_row;
  68. float64_t *a_col;
  69. float64_t invUT;
  70. float64x2_t vecA;
  71. float64x2_t vecX;
  72. for(i=n-1; i >= 0 ; i--)
  73. {
  74. for(j=0; j+1 < cols; j +=2)
  75. {
  76. vecA = vld1q_f64(&pA[i * cols + j]);
  77. for(k=n-1; k > i; k--)
  78. {
  79. vecX = vld1q_f64(&pX[cols*k+j]);
  80. vecA = vfmsq_f64(vecA,vdupq_n_f64(pUT[n*i + k]),vecX);
  81. }
  82. if (pUT[n*i + i]==0.0)
  83. {
  84. return(ARM_MATH_SINGULAR);
  85. }
  86. invUT = 1.0 / pUT[n*i + i];
  87. vecA = vmulq_f64(vecA,vdupq_n_f64(invUT));
  88. vst1q_f64(&pX[i*cols+j],vecA);
  89. }
  90. for(; j < cols; j ++)
  91. {
  92. a_col = &pA[j];
  93. ut_row = &pUT[n*i];
  94. float64_t tmp=a_col[i * cols];
  95. for(k=n-1; k > i; k--)
  96. {
  97. tmp -= ut_row[k] * pX[cols*k+j];
  98. }
  99. if (ut_row[i]==0.0)
  100. {
  101. return(ARM_MATH_SINGULAR);
  102. }
  103. tmp = tmp / ut_row[i];
  104. pX[i*cols+j] = tmp;
  105. }
  106. }
  107. status = ARM_MATH_SUCCESS;
  108. }
  109. /* Return to application */
  110. return (status);
  111. }
  112. #else
  113. arm_status arm_mat_solve_upper_triangular_f64(
  114. const arm_matrix_instance_f64 * ut,
  115. const arm_matrix_instance_f64 * a,
  116. arm_matrix_instance_f64 * dst)
  117. {
  118. arm_status status; /* status of matrix inverse */
  119. #ifdef ARM_MATH_MATRIX_CHECK
  120. /* Check for matrix mismatch condition */
  121. if ((ut->numRows != ut->numCols) ||
  122. (ut->numRows != a->numRows) )
  123. {
  124. /* Set status as ARM_MATH_SIZE_MISMATCH */
  125. status = ARM_MATH_SIZE_MISMATCH;
  126. }
  127. else
  128. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  129. {
  130. int i,j,k,n,cols;
  131. float64_t *pX = dst->pData;
  132. float64_t *pUT = ut->pData;
  133. float64_t *pA = a->pData;
  134. float64_t *ut_row;
  135. float64_t *a_col;
  136. n = dst->numRows;
  137. cols = dst->numCols;
  138. for(j=0; j < cols; j ++)
  139. {
  140. a_col = &pA[j];
  141. for(i=n-1; i >= 0 ; i--)
  142. {
  143. float64_t tmp=a_col[i * cols];
  144. ut_row = &pUT[n*i];
  145. for(k=n-1; k > i; k--)
  146. {
  147. tmp -= ut_row[k] * pX[cols*k+j];
  148. }
  149. if (ut_row[i]==0.0)
  150. {
  151. return(ARM_MATH_SINGULAR);
  152. }
  153. tmp = tmp / ut_row[i];
  154. pX[i*cols+j] = tmp;
  155. }
  156. }
  157. status = ARM_MATH_SUCCESS;
  158. }
  159. /* Return to application */
  160. return (status);
  161. }
  162. #endif
  163. /**
  164. @} end of MatrixInv group
  165. */