private static TOutput[][] JaggedCreateAs <TInput, TOutput>(TInput[,] matrix)
        {
            var r = new TOutput[matrix.GetLength(0)][];

            for (int i = 0; i < r.Length; i++)
            {
                r[i] = new TOutput[matrix.GetLength(1)];
            }
            return(r);
        }
예제 #2
0
        public static TOutput[][] CreateAs <TInput, TOutput>(TInput[,] matrix)
        {
            int rows = matrix.GetLength(0);
            int cols = matrix.GetLength(1);
            var r    = new TOutput[rows][];

            for (int i = 0; i < r.Length; i++)
            {
                r[i] = new TOutput[cols];
            }
            return(r);
        }
 /// <summary>
 /// Extension method to convert an entire two-dimensional array into a different type.
 /// </summary>
 /// <typeparam name="TInput"></typeparam>
 /// <typeparam name="TOutput"></typeparam>
 /// <param name="mArray"></param>
 /// <param name="converter"></param>
 /// <returns></returns>
 public static TOutput[,] ConvertAll <TInput, TOutput>(this TInput[,] mArray, Converter <TInput, TOutput> converter)
 {
     TOutput[,] result = new TOutput[mArray.GetLength(0), mArray.GetLength(1)];
     for (int y = 0; y < mArray.GetLength(1); y++)
     {
         for (int x = 0; x < mArray.GetLength(0); x++)
         {
             result[x, y] = converter.Invoke(mArray[x, y]);
         }
     }
     return(result);
 }
예제 #4
0
        public static IEnumerable <TInput> Flatten <TInput>(this TInput[,] items)
        {
            int d0 = items.GetLength(0);
            int d1 = items.GetLength(1);

            for (int i0 = 0; i0 < d0; i0 += 1)
            {
                for (int i1 = 0; i1 < d1; i1 += 1)
                {
                    yield return(items[i0, i1]);
                }
            }
        }
예제 #5
0
        public static TOutput[,] ExtractArray <TInput, TOutput>(this TInput[,] input, Converter <TInput, TOutput> converter)
        {
            TOutput[,] output = new TOutput[input.GetLength(0), input.GetLength(1)];
            for (int x = 0; x < input.GetLength(0); x++)
            {
                for (int y = 0; y < input.GetLength(1); y++)
                {
                    output[x, y] = converter(input[x, y]);
                }
            }

            return(output);
        }
예제 #6
0
        public static TOutput[,] Select <TInput, TOutput>(this TInput[,] items, Func <TInput, TOutput> f)
        {
            int d0 = items.GetLength(0);
            int d1 = items.GetLength(1);

            TOutput[,] result = new TOutput[d0, d1];
            for (int i0 = 0; i0 < d0; i0 += 1)
            {
                for (int i1 = 0; i1 < d1; i1 += 1)
                {
                    result[i0, i1] = f(items[i0, i1]);
                }
            }
            return(result);
        }
예제 #7
0
        /// <summary>
        ///   Converts the values of a matrix using the given converter expression.
        /// </summary>
        /// <typeparam name="TInput">The type of the input.</typeparam>
        /// <typeparam name="TOutput">The type of the output.</typeparam>
        /// <param name="matrix">The vector to be converted.</param>
        /// <param name="converter">The converter function.</param>
        ///
        public static TOutput[,] Convert <TInput, TOutput>(this TInput[,] matrix, Converter <TInput, TOutput> converter)
        {
            int rows = matrix.GetLength(0);
            int cols = matrix.GetLength(1);

            TOutput[,] result = new TOutput[rows, cols];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    result[i, j] = converter(matrix[i, j]);
                }
            }

            return(result);
        }
예제 #8
0
        public static TOutput[,] ConvertAll <TInput, TOutput>(TInput[,] data, Converter <TInput, TOutput> converter)
        {
            var output = new TOutput[data.GetLength(0), data.GetLength(1)];

            for (var i = 0; i < data.GetLength(0); i++)
            {
                for (var j = 0; j < data.GetLength(1); j++)
                {
                    var temp = data[i, j];
                    if (CheckElement <TInput>(temp))
                    {
                        throw new ArgumentException();
                    }
                    output[i, j] = converter(temp);
                }
            }

            return(output);
        }
        /// <summary>
        /// Convert a 2D array from one type to another
        /// </summary>
        /// <typeparam name="TInput">Input type</typeparam>
        /// <typeparam name="TOutput">Output type</typeparam>
        /// <param name="inputArray">2D input array</param>
        /// <param name="converter">Conversion method</param>
        /// <returns>The converted array</returns>
        public static TOutput[,] ConvertAll2D <TInput, TOutput>(this TInput[,] inputArray, Converter <TInput, TOutput> converter)
        {
            // Get dimensions
            var width  = inputArray.GetLength(0);
            var height = inputArray.GetLength(1);

            // Make new 2D array
            TOutput[,] convertedArray = new TOutput[width, height];

            // Add data into array
            Parallel.For(0, width, i =>
            {
                for (var j = 0; j < height; j++)
                {
                    // Convert the data using the conversion method supplied, ensuring to cast the value to specified type
                    convertedArray[i, j] = converter(inputArray[i, j]);
                }
            });

            // Return the array
            return(convertedArray);
        }
 private static TOutput[,] MatrixCreateAs <TInput, TOutput>(TInput[,] matrix)
 {
     return(new TOutput[matrix.GetLength(0), matrix.GetLength(1)]);
 }