コード例 #1
0
        //TODO: unit-test
        /// <summary>
        ///     Creates a new instance of <see cref="JuggedMatrix{T}"/> which contains elements from <see cref="IEnumerable{T}"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static JuggedMatrix <T> CreateJuggedMatrix <T>(this IEnumerable <IEnumerable <T> > source)
        {
            if (ReferenceEquals(source, null))
            {
                throw new ArgumentNullException(nameof(source));
            }

            var enumerables = source as IEnumerable <T>[] ?? source.ToArray();

            if (enumerables.Any(item => ReferenceEquals(item, null)))
            {
                throw new ArgumentNullException(nameof(source) + ": one of enumerables was null");
            }

            var columnsCounts = enumerables.Select(row => row.Count());
            var matrix        = new JuggedMatrix <T>(enumerables.Length, columnsCounts);

            var rowIndex    = 0;
            var columnIndex = 0;

            foreach (var row in enumerables)
            {
                foreach (var item in row)
                {
                    matrix[rowIndex, columnIndex] = item;
                    columnIndex++;
                }

                rowIndex++;
                columnIndex = 0;
            }

            return(matrix);
        }
コード例 #2
0
        /// <summary>
        ///     Returns the subtraction <see cref="IJuggedMatrix{T}"/> <paramref name="first"/> and
        ///     <see cref="IJuggedMatrix{T}"/> <paramref name="second"/>
        /// </summary>
        /// <param name="first">First matrix</param>
        /// <param name="second">Second matrix</param>
        /// <returns>
        ///     New <see cref="IJuggedMatrix{T}"/> - the result subtraction of matrices <paramref name="first"/> and
        ///     <paramref name="second"/>
        /// </returns>
        public static IJuggedMatrix <int> Sub(this IJuggedMatrix <int> first, IJuggedMatrix <int> second)
        {
            if (first.RowsCount != second.RowsCount)
            {
                throw new
                      MatrixInvalidOperationException("Can't sub second JuggedMatrix to first. Count of rows should be the same");
            }

            for (var i = 0; i < first.RowsCount; i++)
            {
                if (first.ElementsInRow(i) != second.ElementsInRow(i))
                {
                    throw new
                          MatrixInvalidOperationException("Can't add second JuggedMatrix to first. Count of columns should be the same");
                }
            }

            var juggedMatrix = new JuggedMatrix <int>(first.RowsCount,
                                                      first.CountOnEachRow()
                                                      .ToArray());

            for (var i = 0; i < first.RowsCount; i++)
            {
                for (var j = 0; j < first.ElementsInRow(i); j++)
                {
                    juggedMatrix[i, j] = first[i, j] - second[i, j];
                }
            }

            return(juggedMatrix);
        }
コード例 #3
0
        /// <summary>
        ///     Creates a new <see cref="IJuggedMatrix{T}"/> matrix without column at <paramref name="columnIndex"/>
        /// </summary>
        /// <param name="juggedMatrix"></param>
        /// <param name="columnIndex">Column index to remove</param>
        /// <returns>A new <see cref="IJuggedMatrix{T}"/> matrix without column at <paramref name="columnIndex"/></returns>
        public static IJuggedMatrix <T> CreateMatrixWithoutColumn <T>(this IJuggedMatrix <T> juggedMatrix, int columnIndex)
        {
            if (ReferenceEquals(juggedMatrix, null))
            {
                throw new ArgumentNullException(nameof(juggedMatrix));
            }

            var maxColumn = juggedMatrix.Max(x => x.Count());

            if (columnIndex < 0)
            {
                throw new ArgumentException("Column index is < 0");
            }
            if (columnIndex >= maxColumn)
            {
                throw new ArgumentException("Column index is out of range in matrix");
            }

            var tempMatrix = new T[juggedMatrix.RowsCount][];

            for (var i = 0; i < juggedMatrix.RowsCount; i++)
            {
                var currentColumn = 0;
                var elementsInRow = juggedMatrix.ElementsInRow(i);

                if (columnIndex < elementsInRow)
                {
                    tempMatrix[i] = new T[elementsInRow - 1];
                }
                else
                {
                    tempMatrix[i] = new T[elementsInRow];
                }

                for (var j = 0; j < elementsInRow; j++)
                {
                    if (j == columnIndex)
                    {
                        continue;
                    }

                    tempMatrix[i][currentColumn] = juggedMatrix[i, j];
                    currentColumn++;
                }
            }

            var newMatrix = new JuggedMatrix <T>(tempMatrix);

            return(newMatrix);
        }
コード例 #4
0
        /// <summary>
        ///     Returns the multiplication <see cref="IJuggedMatrix{T}"/> <paramref name="matrix"/> on <see cref="decimal"/>
        ///     <paramref name="number"/>
        /// </summary>
        /// <param name="matrix">First</param>
        /// <param name="number"></param>
        /// <returns>
        ///     New <see cref="IJuggedMatrix{T}"/> - the result multiplication of matrix <paramref name="matrix"/> on
        ///     <paramref name="number"/>
        /// </returns>
        public static IJuggedMatrix <decimal> MulOnNumber(this IJuggedMatrix <decimal> matrix, decimal number)
        {
            var juggedMatrix = new JuggedMatrix <decimal>(matrix.RowsCount,
                                                          matrix.CountOnEachRow()
                                                          .ToArray());

            for (var i = 0; i < matrix.RowsCount; i++)
            {
                for (var j = 0; j < matrix.ElementsInRow(i); j++)
                {
                    juggedMatrix[i, j] = matrix[i, j] * number;
                }
            }

            return(juggedMatrix);
        }
コード例 #5
0
        //TODO: unit-test
        /// <summary>
        ///     Creates a new instance of <see cref="JuggedMatrix{T}"/> from <see cref="IMatrix{T}"/>
        /// </summary>
        /// <typeparam name="T">ANY</typeparam>
        /// <param name="matrix">Initial matrix</param>
        /// <returns>A new instance of <see cref="JuggedMatrix{T}"/></returns>
        public static JuggedMatrix <T> ToJuggedMatrix <T>(this IMatrix <T> matrix)
        {
            var juggedMatrix = new JuggedMatrix <T>(matrix.RowsCount,
                                                    Enumerable.Repeat(matrix.ColumnsCount, matrix.RowsCount)
                                                    .ToArray());

            for (var i = 0; i < juggedMatrix.RowsCount; i++)
            {
                for (var j = 0; j < matrix.ColumnsCount; j++)
                {
                    juggedMatrix[i, j] = matrix[i, j];
                }
            }

            return(juggedMatrix);
        }
コード例 #6
0
        /// <summary>
        ///     Returns the multiplication <see cref="IJuggedMatrix{T}"/> <paramref name="matrix"/> on <see cref="int"/>
        ///     <paramref name="number"/>
        /// </summary>
        /// <param name="matrix">First</param>
        /// <param name="number"></param>
        /// <returns>
        ///     New <see cref="IJuggedMatrix{T}"/> - the result multiplication of matrix <paramref name="matrix"/> on
        ///     <paramref name="number"/>
        /// </returns>
        public static IJuggedMatrix <string> MulOnNumber(this IJuggedMatrix <string> matrix, int number)
        {
            var juggedMatrix = new JuggedMatrix <string>(matrix.RowsCount,
                                                         matrix.CountOnEachRow()
                                                         .ToArray());

            for (var i = 0; i < matrix.RowsCount; i++)
            {
                for (var j = 0; j < matrix.ElementsInRow(i); j++)
                {
                    juggedMatrix[i, j] = matrix[i, j]
                                         .Concat(number);
                }
            }

            return(juggedMatrix);
        }
コード例 #7
0
        /// <summary>
        ///     Creates a new <see cref="IJuggedMatrix{T}"/> matrix without row at <paramref name="rowIndex"/>
        /// </summary>
        /// <param name="juggedMatrix"></param>
        /// <param name="rowIndex">Column index to remove</param>
        /// <returns>A new <see cref="IJuggedMatrix{T}"/> matrix without row at <paramref name="rowIndex"/></returns>
        public static IJuggedMatrix <T> CreateMatrixWithoutRow <T>(this IJuggedMatrix <T> juggedMatrix, int rowIndex)
        {
            if (ReferenceEquals(juggedMatrix, null))
            {
                throw new ArgumentNullException(nameof(juggedMatrix));
            }
            if (rowIndex < 0)
            {
                throw new ArgumentException("Row index is < 0");
            }
            if (rowIndex >= juggedMatrix.RowsCount)
            {
                throw new ArgumentException("Row index is out of range in matrix");
            }

            var tempMatrix = new T[juggedMatrix.RowsCount - 1][];
            var currentRow = 0;

            for (var i = 0; i < juggedMatrix.RowsCount; i++)
            {
                if (i == rowIndex)
                {
                    continue;
                }

                var elementsInRow = juggedMatrix.ElementsInRow(i);
                tempMatrix[currentRow] = new T[elementsInRow];

                for (var j = 0; j < elementsInRow; j++)
                {
                    tempMatrix[currentRow][j] = juggedMatrix[i, j];
                }

                currentRow++;
            }

            var newMatrix = new JuggedMatrix <T>(tempMatrix);

            return(newMatrix);
        }
コード例 #8
0
        /// <summary>
        ///     Returns the multiplication <see cref="IJuggedMatrix{T}"/> <paramref name="juggedMatrix"/> on <see cref="long"/>
        ///     <paramref name="number"/>
        /// </summary>
        /// <param name="juggedMatrix">First</param>
        /// <param name="number"></param>
        /// <returns>
        ///     New <see cref="IJuggedMatrix{T}"/> - the result multiplication of matrix <paramref name="juggedMatrix"/> on
        ///     <paramref name="number"/>
        /// </returns>
        public static IJuggedMatrix <long?> MulOnNumber(this IJuggedMatrix <long?> juggedMatrix, long number)
        {
            var newJuggedMatrix =
                new JuggedMatrix <long?>(juggedMatrix.RowsCount,
                                         juggedMatrix.CountOnEachRow()
                                         .ToArray());

            for (var i = 0; i < juggedMatrix.RowsCount; i++)
            {
                for (var j = 0; j < juggedMatrix.ElementsInRow(i); j++)
                {
                    if (juggedMatrix[i, j] != null)
                    {
                        newJuggedMatrix[i, j] = juggedMatrix[i, j]
                                                .Value
                                                * number;
                    }
                }
            }

            return(newJuggedMatrix);
        }
コード例 #9
0
        //TODO: unit-test
        /// <summary>
        ///     Creates a new instance of <see cref="Matrix{T}"/> from <see cref="JuggedMatrix{T}"/>
        /// </summary>
        /// <typeparam name="T">ANY</typeparam>
        /// <param name="juggedMatrix">Initial matrix</param>
        /// <returns>A new instance of <see cref="IMatrix{T}"/></returns>
        public static Matrix <T> ToMatrix <T>(this JuggedMatrix <T> juggedMatrix)
        {
            var matrix = new Matrix <T>(juggedMatrix.RowsCount,
                                        juggedMatrix.CountOnEachRow()
                                        .Max());

            for (var i = 0; i < juggedMatrix.RowsCount; i++)
            {
                for (var j = 0; j < matrix.ColumnsCount; j++)
                {
                    if (juggedMatrix.ElementsInRow(i) < matrix.ColumnsCount)
                    {
                        matrix[i, j] = juggedMatrix[i, j];
                    }
                    else
                    {
                        matrix[i, j] = default;
                    }
                }
            }

            return(matrix);
        }
コード例 #10
0
        //TODO: unit-test
        /// <summary>
        ///     Converts a vanilla .NET matrix <see langword="T"/>[][] to <see cref="JuggedMatrix{T}"/>
        /// </summary>
        /// <typeparam name="T">ANY</typeparam>
        /// <param name="matrix">Initial matrix to convert</param>
        /// <returns><see cref="JuggedMatrix{T}"/>, made on base <paramref name="matrix"/></returns>
        public static JuggedMatrix <T> ToJuggedMatrix <T>(this T[][] matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }

            if (matrix.Any(x => x == null))
            {
                throw new NullReferenceException(nameof(matrix) + " one or more rows are null");
            }

            var newJuggedMatrix = new JuggedMatrix <T>(matrix.Length, matrix.Select(row => row.Length));

            for (var i = 0; i < matrix.Length; i++)
            {
                for (var j = 0; j < newJuggedMatrix.ElementsInRow(i); j++)
                {
                    newJuggedMatrix[i, j] = matrix[i][j];
                }
            }

            return(newJuggedMatrix);
        }