本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Double.SparseMatrix.SetRow方法的典型用法代码示例。如果您正苦于以下问题:C# SparseMatrix.SetRow方法的具体用法?C# SparseMatrix.SetRow怎么用?C# SparseMatrix.SetRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MathNet.Numerics.LinearAlgebra.Double.SparseMatrix
的用法示例。
在下文中一共展示了SparseMatrix.SetRow方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SpeedOfGetRow
/// <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();
}
示例2: InsertRow
/// <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;
}
示例3: SpeedOfAccessRandomElement
/// <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();
}
示例4: SeesCorrectSubmatrixWhenTrainingWith0_5Coverage
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]);
}
}
}
}
}
示例5: SeesCorrectSubmatrixWhenCloneTrainingWith0Coverage
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]);
}
}
}