/// <summary> /// Creates a new <see cref="SparseMatrix"/> and inserts the given row at the given index. /// </summary> /// <param name="rowIndex">The index of where to insert the row.</param> /// <param name="row">The row to insert.</param> /// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns> /// <exception cref="ArgumentNullException">If <paramref name="row"/> is <see langword="null" />. </exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is < zero or > the number of rows.</exception> /// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception> public override Matrix <double> InsertRow(int rowIndex, Vector <double> row) { if (row == null) { throw new ArgumentNullException("row"); } if (rowIndex < 0 || rowIndex > RowCount) { throw new ArgumentOutOfRangeException("rowIndex"); } if (row.Count != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } var result = new SparseMatrix(RowCount + 1, ColumnCount); for (var i = 0; i < rowIndex; i++) { result.At(i, i, At(i, i)); } result.SetRow(rowIndex, row); for (var i = rowIndex + 1; i < result.RowCount; i++) { result.At(i, i - 1, At(i - 1, i - 1)); } return(result); }
/// <summary> /// List indexing is 2000x faster than Matrix.Row() or enumeration. /// </summary> public static void SpeedOfGetRow() { SparseMatrix myMatrix = new SparseMatrix(1000, 1000); SparseVector myVector = SparseVector.OfVector(Vector.Build.Random(1000)); myVector.CoerceZero(1.8); for (int i = 0; i < 1000; i++) { myMatrix.SetRow(i, myVector); } List<Vector<double>> myList = new List<Vector<double>>(myMatrix.EnumerateRows()); Utils.StartTimer(); for (int repeat = 0; repeat < 10; repeat++) { for (int i = 0; i < 1000; i++) { double foo = myMatrix.Row(i)[0]; } } Utils.StopTimer(); Utils.StartTimer(); for (int repeat = 0; repeat < 10; repeat++) { foreach(var row in myMatrix.EnumerateRowsIndexed()) { double foo = row.Item2[0]; } } Utils.StopTimer(); Utils.StartTimer(); for (int repeat = 0; repeat < 10; repeat++) { for (int i = 0; i < 1000; i++) { double foo = myList[i][0]; } } Utils.StopTimer(); }
/// <summary> /// Creates a new <see cref="SparseMatrix"/> and inserts the given row at the given index. /// </summary> /// <param name="rowIndex">The index of where to insert the row.</param> /// <param name="row">The row to insert.</param> /// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns> /// <exception cref="ArgumentNullException">If <paramref name="row"/> is <see langword="null" />. </exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is < zero or > the number of rows.</exception> /// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception> public override Matrix<double> InsertRow(int rowIndex, Vector<double> row) { if (row == null) { throw new ArgumentNullException("row"); } if (rowIndex < 0 || rowIndex > RowCount) { throw new ArgumentOutOfRangeException("rowIndex"); } if (row.Count != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } var result = new SparseMatrix(RowCount + 1, ColumnCount); for (var i = 0; i < rowIndex; i++) { result.At(i, i, At(i, i)); } result.SetRow(rowIndex, row); for (var i = rowIndex + 1; i < result.RowCount; i++) { result.At(i, i - 1, At(i - 1, i - 1)); } return result; }
/// <summary> /// 2D nested list is 10x faster than SparseMatrix and List<SparseVector> /// and 100x faster than Dictionary. However, 2D nested list is only for dense case. /// Note that Nested Dictionary is faster than Tuple dictionary! /// it is the second fastest only 4 times slower than 2D list. /// </summary> public static void SpeedOfAccessRandomElement() { SparseMatrix myMatrix = new SparseMatrix(1000, 1000); SparseVector myVector = SparseVector.OfVector(Vector.Build.Random(1000)); myVector.CoerceZero(1.8); for (int i = 0; i < 1000; i++) { myMatrix.SetRow(i, myVector); } List<Vector<double>> myList = new List<Vector<double>>(myMatrix.EnumerateRows()); List<List<double>> my2DList = new List<List<double>>(); Dictionary<Tuple<int, int>, double> myDict = new Dictionary<Tuple<int, int>, double>(); Dictionary<int, Dictionary<int, double>> myDict2 = new Dictionary<int, Dictionary<int, double>>(); for (int i = 0; i < 1000; i++) { myDict2[i] = new Dictionary<int, double>(); for (int j = 0; j < 1000; j++) { myDict[new Tuple<int, int>(i, j)] = i; myDict2[i][j] = i; } } for (int i = 0; i < 1000; i++) { my2DList.Add(new List<double>()); for (int j = 0; j < 1000; j++) { my2DList[i].Add(i); } } Utils.StartTimer(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { double foo = myDict[new Tuple<int, int>(i, j)]; myDict[new Tuple<int, int>(i, j)] = foo+1; } } Utils.StopTimer(); Utils.StartTimer(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { double foo = myDict2[i][j]; myDict2[i][j] = foo + 1; } } Utils.StopTimer(); Utils.StartTimer(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { double foo = myMatrix[i, j]; myMatrix[i, j] = foo + 1; } } Utils.StopTimer(); Utils.StartTimer(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { double foo = myList[i][j]; myList[i][j] = foo + 1; } } Utils.StopTimer(); Utils.StartTimer(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { double foo = my2DList[i][j]; my2DList[i][j] = foo + 1; } } Utils.StopTimer(); Utils.Pause(); }
public void SeesCorrectSubmatrixWhenTrainingWith0_5Coverage() { var inputsize = 10; var size = (uint)inputsize / 2; var coverage = 0.5; var layer = new Spatial2DLayer(SpatialLayerType.Gaussian, size, size, coverage, true, 1000); var list = from i in Enumerable.Range(0, inputsize) select (double)i; var matrix = new SparseMatrix(inputsize, inputsize); for (int i = 0; i < matrix.RowCount; ++i) { matrix.SetRow(i, list.ToArray()); } // for each node in the 2D layer for (int nodeRow = 0; nodeRow < layer.Height; nodeRow++) { for (int nodeCol = 0; nodeCol < layer.Width; nodeCol++) { // Act var subMatrix = layer.GetSubMatrixForNodeAt(nodeRow, nodeCol, matrix); // check if its 2D input is ok var width = inputsize / size + coverage * (inputsize - inputsize / size); var delta = (inputsize - width) / (size - 1); for (int i = 0; i < (int)width; i++) { for (int j = 0; j < (int)width; j++) { Assert.AreEqual(nodeCol * delta + j, subMatrix[i, j]); } } } } }
public void SeesCorrectSubmatrixWhenCloneTrainingWith0Coverage() { var inputsize = 10; var size = (uint)inputsize / 2; var coverage = 0.0; var layer = new Spatial2DLayer(SpatialLayerType.Gaussian, size, size, coverage, true, 1000); var list = from i in Enumerable.Range(0, inputsize) select (double)i; var matrix = new SparseMatrix(inputsize, inputsize); for (int i = 0; i < matrix.RowCount; ++i) { matrix.SetRow(i, list.ToArray()); } // Act var subMatrix = layer.GetSubMatrixForNodeAt(layer.ClonedNodeRow, layer.ClonedNodeCol, matrix); // var width = inputsize / size + coverage * (inputsize - inputsize / size); var delta = (inputsize - width) / (size - 1); var centerRow = layer.Height / 2; var centerCol = layer.Width / 2; for (int i = 0; i < (int)width; i++) { for (int j = 0; j < (int)width; j++) { Assert.AreEqual(centerCol * delta + j, subMatrix[i, j]); } } }