qr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*Daala video codec
  2. Copyright (c) 2007-2014 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #include <errno.h>
  21. #include <float.h>
  22. #include <math.h>
  23. #include <string.h>
  24. #include "qr.h"
  25. #include "matidx.h"
  26. /*Compute the maximum magnitude of any element in a vector.*/
  27. static double vinfnorm(const double *v, int n) {
  28. double d;
  29. int i;
  30. d = 0;
  31. for (i = 0; i < n; i++) {
  32. double avi;
  33. avi = fabs(v[i]);
  34. d = avi > d ? avi : d;
  35. }
  36. return d;
  37. }
  38. /*Compute the norm of a vector without destructive underflow.*/
  39. static double v2norm(const double *v, int n) {
  40. double s;
  41. int sb;
  42. s = frexp(vinfnorm(v, n), &sb);
  43. if (s > 0 && s <= 1) {
  44. double vis;
  45. double d2;
  46. int i;
  47. d2 = 0;
  48. s = ldexp(1.0, -sb);
  49. for (i = 0; i < n; i++) {
  50. vis = v[i]*s;
  51. d2 += vis*vis;
  52. }
  53. return ldexp(sqrt(d2), sb);
  54. }
  55. else return s;
  56. }
  57. /*Compute the dot product of two vectors.*/
  58. static double vdot(const double *u, const double *v, int n) {
  59. double d;
  60. int i;
  61. d = 0;
  62. for (i = 0; i < n; i++) d += u[i]*v[i];
  63. return d;
  64. }
  65. int qrdecomp_hh(double *aat, int aat_stride, double *d,
  66. double *qqt, int qqt_stride, double *rr, int n, int m) {
  67. int rank;
  68. int i;
  69. int j;
  70. int k;
  71. int l;
  72. rank = 0;
  73. l = m < n ? m : n;
  74. for (k = 0; k < l; k++) {
  75. double *aatk;
  76. double d2;
  77. aatk = aat + k*aat_stride;
  78. d2 = v2norm(aatk + k, m - k);
  79. if (d2 != 0) {
  80. double e;
  81. double s;
  82. if (aatk[k] < 0) d2 = -d2;
  83. for (i = k; i < m; i++) aatk[i] /= d2;
  84. e = ++aatk[k];
  85. for (j = k + 1; j < n; j++) {
  86. double *aatj;
  87. aatj = aat + j*aat_stride;
  88. s = -vdot(aatk + k, aatj + k, m - k)/e;
  89. for (i = k; i < m; i++) aatj[i] += s*aatk[i];
  90. if (rr != NULL) rr[UT_IDX(k, j, n)] = aatj[k];
  91. }
  92. rank++;
  93. }
  94. d[k] = -d2;
  95. if (rr != NULL) rr[UT_IDX(k, k, n)] = d[k];
  96. }
  97. /*Uncomment (along with code below for Q) to compute the _unique_
  98. factorization with the diagonal of R strictly non-negative.
  99. Unfortunately, this will not match the encoded Q and R in qrt, preventing
  100. the user from mixing and matching the explicit and implicit
  101. decompositions.*/
  102. /*if(rr != NULL) {
  103. for (k = 0; k < l; k++) {
  104. if (d[i] < 0) {
  105. for(j = k; j < n; j++) rr[UT_IDX(k, j, n)] = -rr[UT_IDX(k, j, n)];
  106. }
  107. }
  108. }*/
  109. if(qqt != NULL) {
  110. for (k = l; k-- > 0;) {
  111. double *aatk;
  112. double *qqtj;
  113. double e;
  114. aatk = aat + k*aat_stride;
  115. qqtj = qqt + k*qqt_stride;
  116. memset(qqtj, 0, k*sizeof(*qqtj));
  117. for (i = k; i < m; i++) qqtj[i] = -aatk[i];
  118. qqtj[k]++;
  119. e = aatk[k];
  120. if(e != 0)for(j = k + 1; j < l; j++) {
  121. double s;
  122. qqtj = qqt + j*qqt_stride;
  123. s = -vdot(aatk + k, qqtj + k, m - k)/e;
  124. for (i = k; i < m; i++) qqtj[i] += s*aatk[i];
  125. }
  126. }
  127. /*Uncomment (along with code above for R) to compute the _unique_
  128. factorization with the diagonal of R strictly non-negative.
  129. Unfortunately, this will not match the encoded Q and R in qrt, preventing
  130. the user from mixing and matching the explicit and implicit
  131. decompositions.*/
  132. /*for (k = 0; k < l; k++) if(d[k] < 0) {
  133. double *qqtk;
  134. qqtk = qqt + k*qqt_stride;
  135. for (i = 0; i < m; i++) qqtk[i] = -qqtk[i];
  136. }*/
  137. }
  138. return rank;
  139. }
  140. void qrsolve_hh(double *qrt, int qrt_stride, double *d,
  141. double *bbt, int bbt_stride, int n,int m,int l) {
  142. int i;
  143. int j;
  144. int k;
  145. /*Compose with Q^T.*/
  146. for (k = 0; k < n; k++) {
  147. double *qrtk;
  148. double e;
  149. qrtk = qrt + k*qrt_stride;
  150. e = qrtk[k];
  151. for (j = 0; j < l; j++) {
  152. double *bbtj;
  153. double s;
  154. bbtj = bbt + j*bbt_stride;
  155. s = -vdot(qrtk + k, bbtj + k, m - k)/e;
  156. for (i = k; i < m; i++) bbtj[i] += s*qrtk[i];
  157. }
  158. }
  159. /*Back-substitute through R.*/
  160. for (k = n; k-- > 0;) {
  161. double *qrtk;
  162. qrtk = qrt + k*qrt_stride;
  163. for (j = 0; j < l; j++) {
  164. double *bbtj;
  165. bbtj = bbt + j*bbt_stride;
  166. bbtj[k] /= d[k];
  167. for (i = 0; i < k; i++) bbtj[i] -= bbtj[k]*qrtk[i];
  168. }
  169. }
  170. }