/// <summary> /// Returns a clone of this matrix. /// </summary> /// <param name="values">If true (default), the values are copied.</param> public CompressedColumnStorage <T> Clone(bool values = true) { int m = this.RowCount; int n = this.ColumnCount; int nnz = this.NonZerosCount; var ap = this.ColumnPointers; var ai = this.RowIndices; var result = CompressedColumnStorage <T> .Create(m, n, values?nnz : 0); if (values) { Buffer.BlockCopy(ap, 0, result.ColumnPointers, 0, (m + 1) * Constants.SizeOfInt); Buffer.BlockCopy(ai, 0, result.RowIndices, 0, nnz * Constants.SizeOfInt); Array.Copy(this.Values, 0, result.Values, 0, nnz); } else { result.RowIndices = new int[nnz]; Buffer.BlockCopy(ap, 0, result.ColumnPointers, 0, (m + 1) * Constants.SizeOfInt); Buffer.BlockCopy(ai, 0, result.RowIndices, 0, nnz * Constants.SizeOfInt); } return(result); }
/// <summary> /// Returns the transpose of this matrix. /// </summary> public virtual CompressedColumnStorage <T> Transpose() { var result = CompressedColumnStorage <T> .Create(ncols, nrows, this.NonZerosCount); this.Transpose(result); return(result); }
/// <summary> /// Convert a coordinate storage to compressed sparse column (CSC) format. /// </summary> /// <param name="storage">Coordinate storage.</param> /// <param name="cleanup">Remove and sum duplicate entries.</param> /// <returns>Compressed sparse column storage.</returns> public static CompressedColumnStorage <T> ToCompressedColumnStorage <T>(CoordinateStorage <T> storage, bool cleanup = true) where T : struct, IEquatable <T>, IFormattable { int nrows = storage.RowCount; int ncols = storage.ColumnCount; var values = storage.Values; var rowind = storage.RowIndices; var colind = storage.ColumnIndices; int p, k, nz = storage.NonZerosCount; var columnPointers = new int[ncols + 1]; var columnCounts = new int[ncols]; for (k = 0; k < nz; k++) { // Count columns columnCounts[colind[k]]++; } // Get row pointers int valueCount = Helper.CumulativeSum(columnPointers, columnCounts, ncols); var result = CompressedColumnStorage <T> .Create(nrows, ncols); var rowIndices = new int[valueCount]; var storageValues = new T[valueCount]; for (k = 0; k < nz; k++) { p = columnCounts[colind[k]]++; rowIndices[p] = rowind[k]; storageValues[p] = values[k]; } result.RowIndices = rowIndices; result.ColumnPointers = columnPointers; result.Values = storageValues; result.SortIndices(); if (cleanup) { result.Cleanup(); } return(result); }