Esempio n. 1
0
        static void Main(string[] args)
        {
            //创建二维数组[2004...2009][1...4]
            int[] lowerBounds = { 2005, 1 };
            int[] lengths     = { 5, 4 };
            decimal[,] quarterlyRevenue = (decimal[, ])Array.CreateInstance(typeof(decimal), lengths, lowerBounds);

            Console.WriteLine("{0,4} {1,9} {2,9} {3,9} {4,9}", "Year", "Q1", "Q2", "Q3", "Q4");

            int firstYear    = quarterlyRevenue.GetLowerBound(0);
            int lastYear     = quarterlyRevenue.GetUpperBound(0);
            int firstQuarter = quarterlyRevenue.GetLowerBound(1);
            int lastQuarter  = quarterlyRevenue.GetUpperBound(1);

            for (int year = firstYear; year <= lastYear; year++)
            {
                Console.Write(year + " ");
                for (int quarter = firstQuarter; quarter <= lastQuarter; quarter++)
                {
                    Console.Write("{0,9:C}", quarterlyRevenue[year, quarter]);
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
Esempio n. 2
0
        public NMatrix(decimal[,] data)
        {
            Data = data;

            Rows    = Data.GetUpperBound(0) - Data.GetLowerBound(0) + 1;
            Columns = Data.GetUpperBound(1) - Data.GetLowerBound(1) + 1;
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            int[] lowerBounds = { 2005, 1 };
            int[] lengths     = { 5, 4 };

            //利用Array类型的静态方法,可以创建下标起始不为0的数组
            decimal[,] quarterlyRevenue = (decimal[, ])Array.CreateInstance(typeof(decimal), lengths, lowerBounds);

            Console.WriteLine("{0,4}{1,9}{2,9}{3,9}{4,9}", "Year", "Q1", "Q2", "Q3", "Q4");

            int firstYear = quarterlyRevenue.GetLowerBound(0);
            int lastYear  = quarterlyRevenue.GetUpperBound(0);
            int firstQ    = quarterlyRevenue.GetLowerBound(1);
            int lastQ     = quarterlyRevenue.GetUpperBound(1);

            for (int year = firstYear; year <= lastYear; year++)
            {
                Console.Write(year + " ");
                for (int quarter = firstQ; quarter <= lastQ; quarter++)
                {
                    Console.Write("{0, 9:C}", quarterlyRevenue[year, quarter]);
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
        /// <summary>
        ///     This method finds the smallest value in the given distance array.  If the smallest value appears more than once,
        ///     then the lower index is returned.
        /// </summary>
        /// <param name="distances">The pre-calculated distance matrix.</param>
        /// <returns>The first smallest distance index.</returns>
        public static int[] FirstSmallestDistanceIndex(decimal[,] distances)
        {
            if (ParameterValidation.IsDecimalArrayNullOrEmpty(distances))
            {
                return(null);
            }

            var indexSmallest = new[] { 0, 0 };

            //var indexSmallestA = 0;
            //var indexSmallestB = 0;

            for (int indexA = distances.GetLowerBound(0); indexA <= distances.GetUpperBound(0); indexA++)
            {
                for (int indexB = distances.GetLowerBound(1); indexB <= distances.GetUpperBound(1); indexB++)
                {
                    if ((indexA != indexB) && ((indexSmallest[0] == indexSmallest[1]) || (Math.Abs(distances[indexA, indexB]) < Math.Abs(distances[indexSmallest[0], indexSmallest[1]]))))
                    {
                        indexSmallest[0] = indexA;
                        indexSmallest[1] = indexB;
                    }
                }
            }

            return(indexSmallest); // new int[] { indexSmallestA, indexSmallestB };
        }
        /// <summary>
        ///     This method finds the next smallest value in the array, after the value found in the index specified.  If the same
        ///     value exists multiple times, the lower index is returned.
        /// </summary>
        /// <param name="distances">The pre-calculated distance matrix.</param>
        /// <param name="indexLast"></param>
        /// <returns>The next smallest distance index from the last indexes.</returns>
        public static int[] NextSmallestDistanceIndex(decimal[,] distances, int[] indexLast)
        {
            if (ParameterValidation.IsDecimalArrayNullOrEmpty(distances))
            {
                throw new ArgumentOutOfRangeException(nameof(distances));
            }

            if (ParameterValidation.IsIntArrayNullOrEmpty(indexLast))
            {
                throw new ArgumentOutOfRangeException(nameof(indexLast));
            }

            var indexBest = new[] { -1, -1 };

            //var indexBestA = -1;
            //var indexBestB = -1;

            if ((indexLast[0] == -1) || (indexLast[1] == -1))
            {
                return(FirstSmallestDistanceIndex(distances));
            }

            for (int indexA = distances.GetLowerBound(0); indexA <= distances.GetUpperBound(0); indexA++)
            {
                for (int indexB = distances.GetLowerBound(1); indexB <= distances.GetUpperBound(1); indexB++)
                {
                    if
                    //// Not the same object twice / no distance measurement ... zero.
                    ((indexA != indexB)
                     &&
                     //// Not the same indexes the other way around as last time (at least one value changed).
                     (indexLast[0] != indexB || indexLast[1] != indexA)
                     &&
                     //// Not the same indexes the same way around as last time (at least one value changed).
                     (indexLast[0] != indexA || indexLast[1] != indexB)
                     &&
                     //// Distance can be the same as the last one - if the index is higher.
                     ((Math.Abs(distances[indexA, indexB]) == Math.Abs(distances[indexLast[0], indexLast[1]]) && (indexA > indexLast[0] || (indexA >= indexLast[0] && indexB > indexLast[1])))
                      ||
                      //// Distance can be more, but only if it is less than the best found so far.
                      (Math.Abs(distances[indexA, indexB]) > Math.Abs(distances[indexLast[0], indexLast[1]]) && (indexBest[0] == -1 || indexBest[1] == -1 || Math.Abs(distances[indexA, indexB]) < Math.Abs(distances[indexBest[0], indexBest[1]])))))
                    {
                        indexBest[0] = indexA;
                        indexBest[1] = indexB;

                        if (distances[indexA, indexB] == distances[indexLast[0], indexLast[1]])
                        {
                            // If the distance is the same, then it is impossible to find a lower one, so break out, also, if no break, indexes may be skipped.
                            break;
                        }
                    }
                }
            }

            return(indexBest); //new int[] { indexBestA, indexBestB };
        }
Esempio n. 6
0
        protected static double[,] DecimalsToDoubles(decimal[,] array)
        {
            int rows = array.GetUpperBound(0) - array.GetLowerBound(0) + 1;
            int cols = array.GetUpperBound(1) - array.GetLowerBound(1) + 1;

            double[,] result = new double[rows, cols];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    result[i, j] = (double)array[i, j];
                }
            }

            return(result);
        }
Esempio n. 7
0
        public static void MainTestArray()
        {
            var names    = new[] { 1, 2, 0, 5, 4 };
            var namesdes = new int[names.Length];

            System.Buffer.BlockCopy(names, 0, namesdes, 1, names.Length);
            var des = new string[namesdes.Length];

            foreach (var val in namesdes)
            {
                //Console.WriteLine(val);
            }

            var arr = new object[10];

            var arrs = new object[10, 8];

            //Console.WriteLine(namesdes.Aggregate((a, b) => a.ToString() + "," + b.ToString()));
            //Console.WriteLine(FileAttributes.Archive | FileAttributes.Hidden | FileAttributes.Normal);
            //Console.WriteLine(FileAttributes.Archive & FileAttributes.Archive);

            decimal[,] darr = (decimal[, ])Array.CreateInstance(typeof(decimal), new int[] { 5, 4 }, new[] { 2005, 1 });
            Console.WriteLine("{0,4},{1,9},{2,9},{3,9},{4,9}", "Year", "Q1", "Q2", "Q3", "Q4");
            int firstYear    = darr.GetLowerBound(0);
            int lastYear     = darr.GetUpperBound(0);
            int firstQuarter = darr.GetLowerBound(1);
            int lastQuarter  = darr.GetUpperBound(1);

            for (int i = firstYear; i <= lastYear; i++)
            {
                Console.Write(i + " ");
                for (int j = firstQuarter; j <= lastQuarter; j++)
                {
                    Console.Write("{0,1:C} ", darr[i, j]);
                }
                Console.WriteLine();
            }

            int[,] a1 = new int[10000, 10000];
            int[][] a2 = new int[10000][];
            for (int i = 0; i < 10000; i++)
            {
                a2[i] = new int[10000];
            }
            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < 10; i++)
            {
                Safe2DimArrayAccess(a1);
            }
            Console.WriteLine("{0}:Safe2DimArrayAccess", sw.Elapsed);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < 10; i++)
            {
                SafeJaggedArrayAccess(a2);
            }
            Console.WriteLine("{0}:SafeJaggedArrayAccess", sw.Elapsed);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < 10; i++)
            {
                UnSafe2DimArrayAccess(a1);
            }
            Console.WriteLine("{0}:UnSafe2DimArrayAccess", sw.Elapsed);

            StackallocDemo();
            InlineArrayDemo();
            Console.Read();
        }