Hi, Before consider this, let's talk about your problem and why do you want to use these. If your application isn't huge then I think MPI-based matrix package could be much helpful to you since Hama concept also is the large-scale, not high performance for small matrices.
And, Have you tried to subscribe/mail here: http://incubator.apache.org/hama/mailing_lists.html On Fri, May 22, 2009 at 4:51 PM, ykj <[email protected]> wrote: > > > Currently in Hama, eigenvalue decomposition is not implement.So In STEP 4, > it is hard to migrate it.so I > > work out an idea to bypass it. before Step 4, I can let L be > denseMatrix.when I come to Step 4, I can > transform L into submatrix.in Jama,eigenvalue decomposition is support > although it is not parallel computing.So I can get eigValues ,eigVectors > values.But after that in step 5,It need to sort two matrix. > > I want to use the hbase sort function.so Hwo can transform this two > submatrix into two densematrix? > > or other way ? > /** > * STEP 4 > * Calculate the eigen values and vectors of this > covariance matrix > * > * % Get the eigenvectors (columns of Vectors) > and eigenvalues (diag of > Values) > */ > EigenvalueDecomposition eigen = L.eig(); > eigValues = eigen.getD(); > eigVectors = eigen.getV(); > > > /** > * STEP 5 > * % Sort the vectors/values according to size of > eigenvalue > */ > Matrix[] eigDVSorted = sortem(eigValues, eigVectors); > eigValues = eigDVSorted[0]; > eigVectors = eigDVSorted[1]; > > > /** > * STEP 6 > * % Convert the eigenvectors of A'*A into > eigenvectors of A*A' > */ > > eigVectors = A.times(eigVectors); > > > /** > * STEP 7 > * % Get the eigenvalues out of the diagonal > matrix and > * % normalize them so the evalues are > specifically for cov(A'), not > A*A'. > */ > double[] values = diag(eigValues); > for(int i = 0; i < values.length; i++) > values[i] /= A.getColumnDimension() - 1; > > > /** > * STEP 8 > * % Normalize Vectors to unit length, kill > vectors corr. to tiny > evalues > */ > numEigenVecs = 0; > for(int i = 0; i < eigVectors.getColumnDimension(); i++) { > Matrix tmp; > if (values[i] < 0.0001) > { > tmp = new > Matrix(eigVectors.getRowDimension(),1); > } > else > { > tmp = > eigVectors.getMatrix(0,eigVectors.getRowDimension()-1,i,i).times( > 1 / eigVectors.getMatrix(0, > eigVectors.getRowDimension() - 1, i, > i).normF()); > numEigenVecs++; > } > > eigVectors.setMatrix(0,eigVectors.getRowDimension()-1,i,i,tmp); > //eigVectors.timesEquals(1 / eigVectors.getMatrix(0, > eigVectors.getRowDimension() - 1, i, i).normInf()); > } > eigVectors = > eigVectors.getMatrix(0,eigVectors.getRowDimension() - 1, 0, > numEigenVecs - 1); > > trained = true; > > > /*System.out.println("There are " + numGood + " > eigenVectors\n\nEigenVectorSize"); > System.out.println(eigVectors.getRowDimension()); > System.out.println(eigVectors.getColumnDimension()); > try { > PrintWriter pw = new PrintWriter("c:\\tmp\\test.txt"); > eigVectors.print(pw, 8, 4); > pw.flush(); > pw.close(); > } catch (Exception e) { > e.printStackTrace(); > } > > int width = pics[0].img.getWidth(null); > BufferedImage biAvg = > imageFromMatrix(bigAvg.getArrayCopy()[0], width); > > try { > saveImage(new File("c:\\tmp\\test.jpg"), biAvg); > } catch (IOException e1) { > e1.printStackTrace(); > }*/ > } > > /** > * Returns a number of eigenFace values to be used in a feature space > * @param pic > * @param number number of eigen feature values. > * @return will be of length number or this.getNumEigenVecs whichever > is > the smaller > */ > public double[] getEigenFaces(Picture pic, int number) > { > if (number > numEigenVecs) //adjust the number to > the maxium number of > eigen vectors availiable > number = numEigenVecs; > > double[] ret = new double[number]; > > double[] pixels = pic.getImagePixels(); > Matrix face = new Matrix(pixels, pixels.length); > Matrix Vecs = > eigVectors.getMatrix(0,eigVectors.getRowDimension()-1, 0, > number-1).transpose(); > > Matrix rslt = Vecs.times(face); > > for (int i=0; i<number; i++) > { > ret[i] = rslt.get(i,0); > } > > return ret; > } > > > > /** > * Gets the diagonal of a matrix > * @param M matrix > * @return > */ > private double[] diag(Matrix M) { > double[] dvec = new double[M.getColumnDimension()]; > for(int i = 0; i < M.getColumnDimension(); i++) > dvec[i] = M.get(i, i); > return dvec; > > } > /** > * Sorts the Eigenvalues and vectors in decending order > * > * @param D = eigen Values > * @param V = eigen Vectors > * @return > */ > private Matrix[] sortem(Matrix D, Matrix V) { > //dvec = diag(D); // get diagonal components > double[] dvec = diag(D); > > //NV = zeros(size(V)); > > > //[dvec,index_dv] = sort(dvec); // sort dvec, maintain index in > index_dv > > class di_pair{ double value; int index; }; > di_pair[] dvec_indexed = new di_pair[dvec.length]; > for(int i = 0; i < dvec_indexed.length; i++) { > dvec_indexed[i] = new di_pair(); > dvec_indexed[i].index = i; > dvec_indexed[i].value = dvec[i]; > } > > Comparator di_pair_sort = new Comparator() { > public int compare(Object arg0, Object arg1) { > di_pair lt = (di_pair)arg0; > di_pair rt = (di_pair)arg1; > double dif = (lt.value - rt.value); > if(dif > 0) return -1; > if(dif < 0) return 1; > else return 0; > } > }; > Arrays.sort(dvec_indexed, di_pair_sort); > > //index_dv = flipud(index_dv); > //for i = 1:size(D,1) > // ND(i,i) = D(index_dv(i),index_dv(i)); > // NV(:,i) = V(:,index_dv(i)); > //end; > > Matrix D2 = new Matrix(D.getRowDimension(), > D.getColumnDimension()); > Matrix V2 = new Matrix(V.getRowDimension(), > V.getColumnDimension()); > > for(int i = 0; i < dvec_indexed.length; i++) { > D2.set(i, i, D.get(dvec_indexed[i].index, > dvec_indexed[i].index)); > int height = V.getRowDimension() - 1; > Matrix tmp = > V.getMatrix(dvec_indexed[i].index,dvec_indexed[i].index,0,height); > V2.setMatrix(i, i,0,height, tmp); > } > //TODO : Not sure why, but this has to be flipped - check this out > maybe? > Matrix V3 = new Matrix(V.getRowDimension(), > V.getColumnDimension()); > for (int i=0; i<V3.getRowDimension(); i++) > { > for (int j=0; j< V3.getColumnDimension(); j++) > { > V3.set(i,j,V2.get(V3.getRowDimension() - i - 1, > V3.getColumnDimension() - j - 1)); > } > } > > return new Matrix[] { D2, V3 }; > } > > > public boolean isTrained() { > return trained; > } > public int getNumEigenVecs() { > return numEigenVecs; > } > } > > > any advice will be appreciated.thanks in advance. > > > -- > View this message in context: > http://www.nabble.com/Hama--Problem-tp23630187p23666077.html > Sent from the Hadoop core-user mailing list archive at Nabble.com. > > -- Best Regards, Edward J. Yoon @ NHN, corp. [email protected] http://blog.udanax.org
