public void setRow(int rowID, SparseVector rowVec) { if (rowVec != null && rowVec.getCount() != 0) { matrix[rowID] = rowVec; } }
public SparseVector getColMaxVec() { SparseVector newVec = new SparseVector(); foreach (int rowID in matrix.Keys) { SparseVector rowVec = (SparseVector)matrix[rowID]; if (rowVec != null && rowVec.getCount() != 0) { newVec[rowID] = rowVec.getMax(); } } return(newVec); }
public SparseVector getSumofRows()//correct { SparseVector newVec = new SparseVector(); foreach (int rowID in matrix.Keys) { SparseVector rowVec = (SparseVector)matrix[rowID]; if (rowVec != null && rowVec.getCount() != 0) { newVec[rowID] = rowVec.getSum(); } } return(newVec); }
public SparseMatrix getSubMatrix(HashSet <int> rowIDSet, HashSet <int> colIDSet) { SparseMatrix submatrix = new SparseMatrix(); foreach (int ID in rowIDSet) { SparseVector rowVec = getRow(ID); SparseVector subRowVec = rowVec.getSubVec(colIDSet); if (subRowVec.getCount() > 0) { submatrix.setRow(ID, rowVec); } } return(submatrix); }
public SparseMatrix rowNormalize() { SparseMatrix newMat = new SparseMatrix(); foreach (int rowID in matrix.Keys) { SparseVector rowVec = matrix[rowID]; if (rowVec != null && rowVec.getCount() != 0) { double sum = rowVec.getSum(); SparseVector newVec = rowVec / sum; newMat.setRow(rowID, newVec); } } return(newMat); }
public static double dotprod(SparseVector v1, SparseVector v2) { double res = 0; //compare the count of v1 and v2, use the smaller one as base if (v1.getCount() < v2.getCount()) { foreach (KeyValuePair <int, double> kvp in v1.vector) { res += kvp.Value * v2[kvp.Key]; } } else { foreach (KeyValuePair <int, double> kvp in v2.vector) { res += v1[kvp.Key] * kvp.Value; } } return(res); }
//write public void store(string file) { FileStream fs_IDrow = File.Create(file + ".IDrow"); FileStream fs_colval = File.Create(file + ".colval"); StreamWriter sw_IDrow = new StreamWriter(fs_IDrow); BinaryWriter bw_colval = new BinaryWriter(fs_colval); int count = 0; foreach (KeyValuePair <int, SparseVector> kvp in matrix) { SparseVector rowVec = kvp.Value; count = count + rowVec.getCount(); sw_IDrow.WriteLine(kvp.Key + "\t" + count); foreach (KeyValuePair <int, double> kvprow in rowVec.vector) { bw_colval.Write(kvprow.Key); bw_colval.Write(kvprow.Value); } } sw_IDrow.Close(); bw_colval.Close(); }
public static double dotprod(SparseVector v1, SparseVector v2) { double res = 0; //compare the count of v1 and v2, use the smaller one as base if (v1.getCount() < v2.getCount()) { foreach (KeyValuePair<int, double> kvp in v1.vector) { res += kvp.Value * v2[kvp.Key]; } } else { foreach (KeyValuePair<int, double> kvp in v2.vector) { res += v1[kvp.Key] * kvp.Value; } } return res; }