예제 #1
0
        /// <summary>
        /// Creates matrix with default values for <see cref="Tsource" />
        /// </summary>
        /// <param name="rows">Number of rows</param>
        /// <param name="columns">Number of columns</param>
        public Matrix(int rows, int columns) : this()
        {
            Tsource[][] arr;

            if (rows == 0 || columns == 0)
            {
                arr = new Tsource[0][];
            }
            else
            {
                arr = new Tsource[rows][];

                if (MatrixOperationsSettings.CheckIsParallelModeUseful(rows))
                {
                    Parallel.For(0, rows, rowIndex => arr[rowIndex] = new Tsource[columns]);
                }
                else
                {
                    for (int rowIndex = 0; rowIndex < rows; rowIndex++)
                    {
                        arr[rowIndex] = new Tsource[columns];
                    }
                }
            }

            this.value = arr;
        }
예제 #2
0
        /// <summary>
        /// Copies values from other matrix to new instance
        /// </summary>
        /// <param name="matrix"></param>
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentException" />
        public Matrix(Matrix <Tsource> matrix) : this()
        {
            if (matrix == null)
            {
                throw new ArgumentNullException();
            }

            if (matrix.value == null)
            {
                this.value = null;
                return;
            }

            if (matrix.value.Where(row => row == null).Count() < 0)
            {
                throw new ArgumentException("Some rows in matrix is null.");
            }

            if (matrix.value.Length < 1 || matrix.value[0].Length < 1)
            {
                this.value = new Tsource[0][];
                return;
            }

            this.value = new Tsource[matrix.value.Length][];

            if (MatrixOperationsSettings.CheckIsParallelModeUseful(matrix.value.Length))
            {
                Parallel.For(0, this.value.Length, index =>
                {
                    this.value[index] = new Tsource[matrix.value[index].Length];
                    for (int column = 0; column < this.value[index].Length; column++)
                    {
                        this.value[index][column] = matrix.value[index][column];
                    }
                });
            }
            else
            {
                for (int row = 0; row < this.value.Length; row++)
                {
                    this.value[row] = new Tsource[matrix.value[row].Length];
                    for (int column = 0; column < this.value[row].Length; column++)
                    {
                        this.value[row][column] = matrix.value[row][column];
                    }
                }
            }
        }