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