arm_householder_f32.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_householder_f32.c
  4. * Description: Floating-point Householder transform
  5. *
  6. * $Date: 15 June 2022
  7. * $Revision: V1.11.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2022 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. #include "dsp/basic_math_functions.h"
  30. #include "dsp/fast_math_functions.h"
  31. #include "dsp/matrix_utils.h"
  32. #include <math.h>
  33. /**
  34. @ingroup groupMatrix
  35. */
  36. /**
  37. @defgroup MatrixHouseholder Householder transform of a vector
  38. Computes the Householder transform of a vector x.
  39. The Householder transform of x is a vector v with
  40. \f[
  41. v_0 = 1
  42. \f]
  43. and a scalar \f$\beta\f$ such that:
  44. \f[
  45. P = I - \beta v v^T
  46. \f]
  47. is an orthogonal matrix and
  48. \f[
  49. P x = ||x||_2 e_1
  50. \f]
  51. So P is an hyperplane reflection such that the image of x
  52. is proportional to \f$e_1\f$.
  53. \f$e_1\f$ is the vector of coordinates:
  54. \f[
  55. \begin{pmatrix}
  56. 1 \\
  57. 0 \\
  58. \vdots \\
  59. \end{pmatrix}
  60. \f]
  61. If x is already proportional to \f$e_1\f$ then
  62. the matrix P should be the identity.
  63. Thus, \f$\beta\f$ should be 0 and in this case the vector v
  64. can also be null.
  65. But how do we detect that x is already proportional to
  66. \f$e_1\f$.
  67. If x
  68. \f[
  69. x =
  70. \begin{pmatrix}
  71. x_0 \\
  72. xr \\
  73. \end{pmatrix}
  74. \f]
  75. where \f$xr\f$ is a vector.
  76. The algorithm is computing the norm squared of this vector:
  77. \f[
  78. ||xr||^2
  79. \f]
  80. and this value is compared to a `threshold`. If the value
  81. is smaller than the `threshold`, the algorithm is
  82. returning 0 for \f$\beta\f$ and the householder vector.
  83. This `threshold` is an argument of the function.
  84. Default values are provided in the header
  85. `dsp/matrix_functions.h` like for instance
  86. `DEFAULT_HOUSEHOLDER_THRESHOLD_F32`
  87. */
  88. /**
  89. @addtogroup MatrixHouseholder
  90. @{
  91. */
  92. /**
  93. @brief Householder transform of a floating point vector.
  94. @param[in] pSrc points to the input vector.
  95. @param[in] threshold norm2 threshold.
  96. @param[in] blockSize dimension of the vector space.
  97. @param[out] pOut points to the output vector.
  98. @return beta return the scaling factor beta
  99. */
  100. float32_t arm_householder_f32(
  101. const float32_t * pSrc,
  102. const float32_t threshold,
  103. uint32_t blockSize,
  104. float32_t * pOut
  105. )
  106. {
  107. uint32_t i;
  108. float32_t epsilon;
  109. float32_t x1norm2,alpha;
  110. float32_t beta,tau,r;
  111. epsilon = threshold;
  112. alpha = pSrc[0];
  113. for(i=1; i < blockSize; i++)
  114. {
  115. pOut[i] = pSrc[i];
  116. }
  117. pOut[0] = 1.0f;
  118. arm_dot_prod_f32(pSrc+1,pSrc+1,blockSize-1,&x1norm2);
  119. if (x1norm2<=epsilon)
  120. {
  121. tau = 0.0f;
  122. memset(pOut,0,blockSize * sizeof(float32_t));
  123. }
  124. else
  125. {
  126. beta = alpha * alpha + x1norm2;
  127. (void)arm_sqrt_f32(beta,&beta);
  128. if (alpha > 0.0f)
  129. {
  130. beta = -beta;
  131. }
  132. r = 1.0f / (alpha -beta);
  133. arm_scale_f32(pOut,r,pOut,blockSize);
  134. pOut[0] = 1.0f;
  135. tau = (beta - alpha) / beta;
  136. }
  137. return(tau);
  138. }
  139. /**
  140. @} end of MatrixHouseholder group
  141. */