当前位置: 首页>>代码示例>>C#>>正文


C# SparseMatrix.EnumerateRows方法代码示例

本文整理汇总了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();
        }
开发者ID:lawrencewu,项目名称:RecSys,代码行数:44,代码来源:ExperimentOfSpeed.cs

示例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();
        }
开发者ID:lawrencewu,项目名称:RecSys,代码行数:98,代码来源:ExperimentOfSpeed.cs


注:本文中的MathNet.Numerics.LinearAlgebra.Double.SparseMatrix.EnumerateRows方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。