public SparseMatrix mutiplyMatrix(SparseMatrix a){ int m = this.size()[0]; int n = a.size()[1]; int dim[]={m,n}; SparseMatrix matrix = new SparseMatrix(dim); TLongFloatIterator it = this.vector.iterator(); TLongFloatIterator ita = a.vector.iterator(); for (int i = this.vector.size(); i-- > 0;) { it.advance(); ita = a.vector.iterator(); for(int j = a.vector.size(); j-- > 0;) { ita.advance(); if(this.getIndices(it.key())[1]==a.getIndices(ita.key())[0]) { int []indices = {this.getIndices(it.key())[0],a.getIndices(ita.key())[1]}; matrix.set(indices, matrix.elementAt(indices)+it.value()*ita.value()); } } } return matrix; }
SparseMatrix updateW() { int[] dimVWH = { m, n }; int[] dimWVWHH = { m, r }; SparseMatrix matrixVWH = new SparseMatrix(dimVWH); SparseMatrix matrixWVWHH = new SparseMatrix(dimWVWHH); SparseMatrix matrixWH = w.mutiplyMatrix(h); TLongFloatIterator itV = v.vector.iterator(); TLongFloatIterator itW = w.vector.iterator(); for (int i = v.vector.size(); i-- > 0;) { itV.advance(); matrixVWH.set(itV.key(), itV.value() / (matrixWH.elementAt(itV.key()) + eps)); } SparseMatrix matrixTranH = h.trans(); SparseMatrix matrixVWHH = matrixVWH.mutiplyMatrix(matrixTranH); for (int i = w.vector.size(); i-- > 0;) { itW.advance(); matrixWVWHH.set(itW.key(), itW.value() * matrixVWHH.elementAt(itW.key())); } return matrixWVWHH; }
/** * 矩阵归一化 * * @param matrix * @return 归一化后矩阵 */ SparseMatrix normalized(SparseMatrix matrix) { int ySize = matrix.size()[1]; float ySum[] = new float[ySize]; TLongFloatIterator it = matrix.vector.iterator(); for (int i = matrix.vector.size(); i-- > 0;) { it.advance(); ySum[matrix.getIndices(it.key())[1]] += it.value(); } it = matrix.vector.iterator(); for (int i = matrix.vector.size(); i-- > 0;) { it.advance(); matrix.set(it.key(), it.value() / (ySum[matrix.getIndices(it.key())[1]] + eps)); } return matrix; }
/** * @param mat * Sep 6, 2009 */ public void minus(SparseMatrix mat) { TLongFloatIterator it = mat.vector.iterator(); for (int i = mat.vector.size(); i-- > 0;) { it.advance(); vector.put(it.key(),vector.get(it.key()) - it.value()); } }
public void add(SparseMatrix mat) { TLongFloatIterator it = mat.vector.iterator(); for (int i = mat.vector.size(); i-- > 0;) { it.advance(); vector.put(it.key(),vector.get(it.key()) + it.value()); } }
public float l1Norm() { float norm = 0; TLongFloatIterator it = vector.iterator(); for (int i = vector.size(); i-- > 0;) { it.advance(); norm += Math.abs(it.value()); } return norm; }
public float l2Norm() { float norm = 0; TLongFloatIterator it = vector.iterator(); for (int i = vector.size(); i-- > 0;) { it.advance(); norm += it.value()*it.value(); } return (float) Math.sqrt(norm); }
public float infinityNorm() { float norm = 0; TLongFloatIterator it = vector.iterator(); for (int i = vector.size(); i-- > 0;) { it.advance(); if (Math.abs(it.value()) > norm) norm = Math.abs(it.value()); } it=null; return norm; }
/** * @return * Sep 6, 2009 */ public SparseMatrix trans() { int []newdim = {dim[1],dim[0]}; SparseMatrix newmat = new SparseMatrix(newdim); TLongFloatIterator itW = vector.iterator(); for (int i = vector.size(); i-- > 0;) { itW.advance(); int x = getIndices(itW.key())[0]; int y = getIndices(itW.key())[1]; int []TranWIndices = {y,x}; newmat.set(TranWIndices,itW.value()); } return newmat; }
SparseMatrix updateH() { int[] dimWH = { m, n }; int[] dimVWH = { m, n }; int[] dimHWVWH = { r, n }; SparseMatrix matrixWH = new SparseMatrix(dimWH); SparseMatrix matrixVWH = new SparseMatrix(dimVWH); SparseMatrix matrixHWVWH = new SparseMatrix(dimHWVWH); matrixWH = w.mutiplyMatrix(h); TLongFloatIterator itV = v.vector.iterator(); TLongFloatIterator itH = h.vector.iterator(); for (int i = v.vector.size(); i-- > 0;) { itV.advance(); matrixVWH.set(itV.key(), itV.value() / (matrixWH.elementAt(itV.key()) + eps)); } SparseMatrix matrixTranW = w.trans(); SparseMatrix matrixWVWH = matrixTranW.mutiplyMatrix(matrixVWH); for (int i = h.vector.size(); i-- > 0;) { itH.advance(); matrixHWVWH.set(itH.key(), itH.value() * matrixWVWH.elementAt(itH.key())); } return matrixHWVWH; }
public TLongFloatIterator iterator() { return container.iterator(); }