예제 #1
0
        public void RemoveATriplet(int row, int col)
        {
            Triplet curr = this.GetTriplet(row, col);

            if (curr != null)
            {
                triplets.Remove(curr);
                rowTriplets[row].Remove(curr);
                colTriplets[col].Remove(curr);
            }
        }
예제 #2
0
        static public SparseMatrix operator *(SparseMatrix mat1, SparseMatrix mat2)
        {
            if (mat1 == null || mat2 == null)
            {
                return(null);
            }
            int m = mat1.NRow, n = mat2.NCol;

            if (mat1.NCol != mat2.NRow)
            {
                Console.WriteLine("Dimension not matched.");
                return(null);
            }
            SparseMatrix   mat = new SparseMatrix(m, n);
            List <Triplet> newTriplets = new List <Triplet>();

            for (int r = 0; r < m; ++r)
            {
                List <Triplet> row1 = mat1.GetRowTriplets(r);
                for (int c = 0; c < n; ++c)
                {
                    List <Triplet> col2 = mat2.GetColumnTriplets(c);
                    double         sum = 0.0;
                    int            n1 = 0, n2 = 0;
                    while (n1 < row1.Count && n2 < col2.Count)
                    {
                        int c1 = row1[n1].col;
                        int r2 = col2[n2].row;
                        if (c1 < r2)
                        {
                            ++n1;
                        }
                        else if (r2 < c1)
                        {
                            ++n2;
                        }
                        else
                        {
                            sum += row1[n1].value * col2[n2].value;
                            ++n1;
                            ++n2;
                        }
                    }                    //while
                    if (Math.Abs(sum) > 1e-6)
                    {
                        Triplet triplet = new Triplet(r, c, sum);
                        newTriplets.Add(triplet);
                    }
                }
            }            //for-each-row
            mat = new SparseMatrix(newTriplets);
            return(mat);
        }
예제 #3
0
 public void AddTriplet(Triplet triplet)
 {
     if (triplets == null)             // do not initialize here, as row/col is unknown
     {
         throw new NullReferenceException();
     }
     if (triplet.row < 0 || triplet.row >= nRows || triplet.col < 0 || triplet.col >= nCols)
     {
         throw new IndexOutOfRangeException();
     }
     triplets.Add(triplet);
     rowTriplets[triplet.row].Add(triplet);
     rowTriplets[triplet.row].Sort(new Triplet.TripletRowComparer());
     colTriplets[triplet.col].Add(triplet);
     colTriplets[triplet.col].Sort(new Triplet.TripletColumnComparer());
 }
예제 #4
0
        public void AddTriplet(int row, int col, double value)
        {
            Triplet triplet = new Triplet(row, col, value);
            Triplet curr    = this.GetTriplet(row, col);

            if (curr == null)
            {
                this.AddTriplet(triplet);
            }
            else
            {
                curr.value = value;
            }
            //if (value == 0)
            //{
            //    // remove
            //    triplets.Remove(triplet);
            //    rowTriplets[row].Remove(triplet);
            //    colTriplets[col].Remove(triplet);
            //}
        }
예제 #5
0
        // operators
        static public SparseMatrix operator +(SparseMatrix mat1, SparseMatrix mat2)
        {
            if (mat1 == null || mat2 == null)
            {
                return(null);
            }
            int m = mat1.NRow, n = mat1.NCol;

            if (m != mat2.NRow || n != mat2.NCol)
            {
                Console.WriteLine("Dimension not matched.");
                return(null);
            }
            SparseMatrix   mat = new SparseMatrix(m, n);
            List <Triplet> newTriplets = new List <Triplet>();

            for (int r = 0; r < m; ++r)
            {
                List <Triplet> trips = new List <Triplet>();
                List <Triplet> row1 = mat1.GetRowTriplets(r);
                List <Triplet> row2 = mat2.GetRowTriplets(r);
                int            c1 = 0, c2 = 0;
                while (c1 < row1.Count && c2 < row2.Count)
                {
                    Triplet t1 = null, t2 = null;
                    t1 = row1[c1];
                    t2 = row2[c2];
                    if (t1 != null && t2 != null)
                    {
                        if (t1.col < t2.col)
                        {
                            trips.Add(new Triplet(t1));
                            ++c1;
                        }
                        else if (t2.col < t1.col)
                        {
                            trips.Add(new Triplet(t2));
                            ++c2;
                        }
                        else
                        {
                            trips.Add(new Triplet(r, c1, t1.value + t2.value));
                            ++c1;
                            ++c2;
                        }
                    }
                }                //while
                while (c1 < row1.Count)
                {
                    trips.Add(new Triplet(row1[c1]));
                    ++c1;
                }
                while (c2 < row2.Count)
                {
                    trips.Add(new Triplet(row2[c2]));
                    ++c2;
                }
                newTriplets.AddRange(trips);
            }            //for-each-row
            mat = new SparseMatrix(newTriplets, m, n);
            return(mat);
        }
예제 #6
0
 public Triplet(Triplet t)
 {
     this.row   = t.row;
     this.col   = t.col;
     this.value = t.value;
 }