Exemplo n.º 1
0
 public void setRow(int rowID, SparseVector rowVec)
 {
     if (rowVec != null && rowVec.getCount() != 0)
     {
         matrix[rowID] = rowVec;
     }
 }
Exemplo n.º 2
0
 public void setRow(int rowID, SparseVector rowVec)
 {
     if (rowVec != null && rowVec.getCount() != 0)
     {
         matrix[rowID] = rowVec;
     }
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        //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();
        }
Exemplo n.º 9
0
        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;
        }