/// <summary> /// Sets all entries in a row to 0 /// </summary> /// <param name="i">row index in global indices</param> static public void ClearRow(this IMutableMatrixEx M, int i) { int[] ColIdx = null; double[] Values = null; int L = M.GetRow(i, ref ColIdx, ref Values); Array.Clear(Values, 0, L); Debug.Assert(ColIdx.Length >= L); Debug.Assert(Values.Length >= L); if (ColIdx.Length > L) { Array.Resize(ref ColIdx, L); } if (Values.Length > L) { Array.Resize(ref Values, L); } M.SetValues(i, ColIdx, Values); }
/// <summary> /// accumulates a dense matrix <paramref name="FullMtx"/> to a sparse matrix /// -- certainly, only adviseable for small matrices. /// </summary> static public void AccDenseMatrix(this IMutableMatrixEx tis, double alpha, IMatrix FullMtx) { if (tis.RowPartitioning.LocalLength != FullMtx.NoOfRows) { throw new ArgumentException("Mismatch in number of rows."); } if (tis.ColPartition.TotalLength != FullMtx.NoOfCols) { throw new ArgumentException("Mismatch in number of columns."); } int i0 = tis.RowPartitioning.i0; int I = tis.RowPartitioning.LocalLength; int J = tis.ColPartition.TotalLength; int[] col = null; double[] val = null; for (int i = 0; i < I; i++) { int Lr = tis.GetRow(i + i0, ref col, ref val); var oldRow = new MsrMatrix.MatrixEntry[Lr]; for (int lr = 0; lr < Lr; lr++) { oldRow[i].m_ColIndex = col[lr]; oldRow[i].Value = val[lr]; } List <int> NewColIdx = new List <int>(J); List <double> NewVals = new List <double>(J); for (int j = 0; j < J; j++) { double FMij = FullMtx[i, j]; if (FMij != 0.0) { NewVals.Add(alpha * FMij); NewColIdx.Add(j); } } Array.Sort <MsrMatrix.MatrixEntry>(oldRow); int k1 = 0, k2 = 0, K1 = oldRow.Length, K2 = NewVals.Count; while (k1 < K1 && k2 < K2) { int j1 = oldRow[k1].m_ColIndex; int j2 = NewColIdx[k2]; if (j1 < 0) { // should also chrash in RELEASE, therefor -> Exception. throw new ApplicationException("expecting a row without un-allocated entries."); } if (j1 > j2) { // k2++; // new row neds to catch up } else if (j1 < j2) { k1++; } else { NewVals[k2] += oldRow[k1].Value; k1++; k2++; } } tis.SetValues(i + i0, NewColIdx.ToArray(), NewVals.ToArray()); } }