算法系列-----矩阵(九)-------------矩阵SVD---奇异值分解

这个概念如果不懂,我觉得去维基百科看看最好,

地址:http://zh.wikipedia.org/wiki/奇异值分解


我这里也是引用别人的测试,调用了别人的包,给大家看看

/*************************************************************************
 *  Compilation:  javac -classpath .:jama.jar SVD.java
 *  Execution:    java  -classpath .:jama.jar SVD
 *  Dependencies: jama.jar
 *  
 *  Test client for computing singular values of a matrix.
 *  
 *       http://math.nist.gov/javanumerics/jama/
 *       http://math.nist.gov/javanumerics/jama/Jama-1.0.1.jar
 *
 *************************************************************************/

import Jama.Matrix;
import Jama.SingularValueDecomposition;

public class SVD {
   public static void main(String[] args) { 

      // create M-by-N matrix that doesn't have full rank
      int M = 8, N = 5;
      Matrix B = Matrix.random(5, 3);
      Matrix A = Matrix.random(M, N).times(B).times(B.transpose());
      System.out.print("A = ");
      A.print(9, 6);

      // compute the singular vallue decomposition
      System.out.println("A = U S V^T");
      System.out.println();
      SingularValueDecomposition s = A.svd();
      System.out.print("U = ");
      Matrix U = s.getU();
      U.print(9, 6);
      System.out.print("Sigma = ");
      Matrix S = s.getS();
      S.print(9, 6);
      System.out.print("V = ");
      Matrix V = s.getV();
      V.print(9, 6);
      System.out.println("rank = " + s.rank());
      System.out.println("condition number = " + s.cond());
      System.out.println("2-norm = " + s.norm2());

      // print out singular values
      System.out.print("singular values = ");
      Matrix svalues = new Matrix(s.getSingularValues(), 1);
      svalues.print(9, 6);
   }

}
上面给出了jar包下载地址:

Jama-1.0.1.jar
直接下载:
http://download.csdn.net/detail/u014737138/8025225
测试结果:

A = 
   1.187774   1.576314   0.637323   0.995715   1.461993
   1.836872   2.681370   1.301304   1.485940   2.327313
   3.482730   4.907988   2.185064   2.871390   4.402124
   2.713190   3.884867   1.797739   2.218906   3.435112
   3.541166   4.990545   2.281509   2.902963   4.434988
   3.644364   4.970195   2.127232   3.025903   4.523168
   2.886252   3.979685   1.736969   2.387951   3.596824
   1.958339   2.795911   1.277497   1.606028   2.483787

A = U S V^T

U = 
   0.145799  -0.378915   0.027220  -0.863638  -0.048140
   0.238540   0.531337   0.312952  -0.070249   0.052043
   0.443415   0.015206  -0.694747  -0.030124   0.506162
   0.348559   0.347829  -0.150981   0.045055  -0.116842
   0.450236   0.219763   0.492038  -0.155941   0.238693
   0.454530  -0.554452   0.202501   0.469502  -0.005552
   0.362347  -0.253695   0.128220   0.016471  -0.278724
   0.251250   0.181149  -0.310327  -0.035153  -0.768394

Sigma = 
  18.680992   0.000000   0.000000   0.000000   0.000000
   0.000000   0.292309   0.000000   0.000000   0.000000
   0.000000   0.000000   0.035702   0.000000   0.000000
   0.000000   0.000000   0.000000   0.000000   0.000000
   0.000000   0.000000   0.000000   0.000000   0.000000

V = 
   0.422356  -0.332743   0.578485  -0.413839   0.452757
   0.591529   0.311928  -0.270772  -0.522598  -0.454279
   0.264617   0.756644   0.412612   0.405020   0.152242
   0.347810  -0.434221   0.311891   0.489681  -0.594489
   0.529826  -0.175855  -0.569660   0.389615   0.460486

rank = 3
condition number = 3.3032712102739668E16
2-norm = 18.68099151701797
singular values = 
  18.680992   0.292309   0.035702   0.000000   0.000000



你可能感兴趣的:(算法,矩阵)