svd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*Daala video codec
  2. Copyright (c) 2005-2008 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. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <float.h>
  24. #include <math.h>
  25. #include <string.h>
  26. #include "svd.h"
  27. #include "pythag.h"
  28. /*Computes the singular value decomposition of a matrix into matrices
  29. U\Sigma V^T, where U and V are orthonormal and \Sigma is diagonal.
  30. _w: An _nrows by (_nrows+_ncols) matrix of storage for input and output.
  31. On input, the first _nrows rows contain the input matrix.
  32. On output, the first _nrows rows contain the columns of U, each
  33. scaled by the corresponding singular value.
  34. The caller must divide by the appropriate value to obtain a unit
  35. vector, if one is desired.
  36. The next _ncols rows contain the rows of V (_not_ V^T).
  37. _s: On output contains the squares of the _ncols singular values.
  38. _nrows: The number of rows of the matrix.
  39. _ncols: The number of columns of the matrix.
  40. Return: The estimated column rank of the matrix.*/
  41. int svd(double **_w,double *_s,int _nrows,int _ncols){
  42. double e2;
  43. double tol;
  44. int i;
  45. int j;
  46. int col_rank;
  47. int slimit;
  48. int sweepi;
  49. int nrots;
  50. e2=8*_nrows*DBL_EPSILON*DBL_EPSILON;
  51. tol=0.125*DBL_EPSILON;
  52. for(i=0;i<_ncols;i++)for(j=0;j<_ncols;j++)_w[i+_nrows][j]=i==j;
  53. col_rank=_ncols;
  54. slimit=_ncols>24?_ncols>>2:6;
  55. sweepi=0;
  56. do{
  57. int k;
  58. nrots=0;
  59. for(j=0;j<col_rank-1;j++)for(k=j+1;k<col_rank;k++){
  60. double vt;
  61. double ee;
  62. double ff;
  63. double gg;
  64. double c;
  65. double s;
  66. double x;
  67. double y;
  68. ff=ee=gg=0;
  69. for(i=0;i<_nrows;i++){
  70. ee+=_w[i][j]*_w[i][j];
  71. ff+=_w[i][j]*_w[i][k];
  72. gg+=_w[i][k]*_w[i][k];
  73. }
  74. _s[j]=ee;
  75. _s[k]=gg;
  76. if(ee>=gg){
  77. if(ee<=e2*_s[0]||fabs(ff)<=tol*ee)continue;
  78. ff/=ee;
  79. gg=1-gg/ee;
  80. vt=pythag(2*ff,gg);
  81. c=sqrt(fabs(0.5*(1+gg/vt)));
  82. s=ff/(vt*c);
  83. }
  84. else{
  85. ff/=gg;
  86. ee=ee/gg-1;
  87. vt=pythag(2*ff,ee);
  88. s=sqrt(fabs(0.5*(1-ee/vt)));
  89. if(ff<0)s=-s;
  90. c=ff/(vt*s);
  91. }
  92. for(i=0;i<_nrows+_ncols;i++){
  93. x=_w[i][j];
  94. y=_w[i][k];
  95. _w[i][j]=x*c+y*s;
  96. _w[i][k]=y*c-x*s;
  97. }
  98. nrots++;
  99. }
  100. while(col_rank>1&&_s[col_rank-1]<=_s[0]*tol+tol*tol)col_rank--;
  101. }
  102. while(nrots>0&&++sweepi<=slimit);
  103. return col_rank-(col_rank>0&&_s[0]<=0);
  104. }
  105. /*Computes the Moore-Penrose pseudoinverse of a matrix using an SVD.
  106. _w: An _nrows by (_nrows+_ncols) matrix of storage for input and output.
  107. On input, the first _nrows rows contain the input matrix.
  108. On output, the first _nrows rows contain the transpose of the
  109. pseudoinverse.
  110. The next _ncols rows are temporary storage.
  111. On output the contents are undefined.
  112. _s: _ncols temporary values.
  113. On output the contents are undefined.
  114. _nrows: The number of rows of the matrix.
  115. _ncols: The number of columns of the matrix.
  116. Return: The estimated column rank of the matrix.*/
  117. int svd_pseudoinverse(double **_w,double *_s,int _nrows,int _ncols){
  118. int rank;
  119. int i;
  120. int j;
  121. int k;
  122. rank=svd(_w,_s,_nrows,_ncols);
  123. /*There are generally far fewer columns than rows.
  124. Fold the singular value inverses into V.*/
  125. for(j=0;j<_ncols;j++)for(i=0;i<rank;i++)_w[_nrows+j][i]/=_s[i];
  126. /*Recompose the remaining matrices.*/
  127. for(i=0;i<_nrows;i++){
  128. memset(_s,0,_ncols*sizeof(*_s));
  129. for(j=0;j<_ncols;j++)for(k=0;k<rank;k++)_s[j]+=_w[i][k]*_w[_nrows+j][k];
  130. memcpy(_w[i],_s,_ncols*sizeof(*_s));
  131. }
  132. return rank;
  133. }