/// <summary>
        ///   Creates a new multidimensional matrix with the same shape as another matrix.
        /// </summary>
        ///
        public static Array CreateAs(Array matrix, Type type)
        {
            int[] outputShape = Matrix.GetShape(matrix, type);

            // multidimensional or jagged -> jagged
            return(Jagged.Create(elementType: type.GetInnerMostType(), shape: outputShape, value: type.GetDefaultValue()));
        }
        /// <summary>
        ///   Creates a square matrix matrix with random data.
        /// </summary>
        ///
        public static T[][] Random <T>(int size, IRandomNumberGenerator <T> generator,
                                       bool symmetric = false, T[][] result = null)
        {
            if (result is null)
            {
                result = Jagged.Create <T>(size, size);
            }

            if (!symmetric)
            {
                for (var i = 0; i < size; i++)
                {
                    result[i] = generator.Generate(size);
                }
            }
            else
            {
                for (var i = 0; i < size; i++)
                {
                    T[] row = generator.Generate(size / 2, result[i]);
                    for (int start = 0, end = size - 1; start < size / 2; start++, end--)
                    {
                        row[end] = row[start];
                    }
                }
            }

            return(result);
        }
        /// <summary>
        ///   Returns a block-diagonal matrix with the given matrices on its diagonal.
        /// </summary>
        ///
        public static T[][] Diagonal <T>(T[][][] blocks)
        {
            int rows = 0;
            int cols = 0;

            for (var i = 0; i < blocks.Length; i++)
            {
                rows += blocks[i].Rows();
                cols += blocks[i].Columns();
            }

            var result     = Jagged.Create <T>(rows, cols);
            int currentRow = 0;
            int currentCol = 0;

            for (var i = 0; i < blocks.Length; i++)
            {
                for (var r = 0; r < blocks[i].Length; r++)
                {
                    for (var c = 0; c < blocks[i][r].Length; c++)
                    {
                        result[currentRow + r][currentCol + c] = blocks[i][r][c];
                    }
                }

                currentRow = blocks[i].Length;
                currentCol = blocks[i][0].Length;
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Sets a region of a matrix to the given values.
        /// </summary>
        ///
        /// <param name="destination">The matrix where elements will be set.</param>
        /// <param name="value">The matrix of values to which matrix elements will be set.</param>
        /// <param name="startRow">Starting row index</param>
        /// <param name="endRow">End row index</param>
        /// <param name="startCol">Starting column index</param>
        /// <param name="endCol">End column index</param>
        ///
        public static void Set <T>(this T[][] destination, T value, int startRow, int endRow, int startCol, int endCol)
        {
            T[][] values   = Jagged.Create <T>(destination.Rows(), destination.Columns(), value);
            int   rowIndex = end(endRow, destination.Rows());

            int[] rowIndices = Vector.Range(startRow, rowIndex);
            int   colIndex   = end(endCol, destination.Columns());

            int[] columnIndices = Vector.Range(startCol, colIndex);
            set(destination, rowIndices, columnIndices, values, rowIndices, columnIndices);
        }
        /// <summary>
        ///   Creates a rows-by-cols matrix with random data.
        /// </summary>
        ///
        public static T[][] Random <T>(int rows, int cols,
                                       IRandomNumberGenerator <T> generator, T[][] result = null)
        {
            if (result is null)
            {
                result = Jagged.Create <T>(rows, cols);
            }

            for (var i = 0; i < rows; i++)
            {
                result[i] = generator.Generate(cols);
            }
            return(result);
        }
Exemplo n.º 6
0
 /// <summary>
 ///   Sets a region of a matrix to the given values.
 /// </summary>
 ///
 /// <param name="destination">The matrix where elements will be set.</param>
 /// <param name="value">The matrix of values to which matrix elements will be set.</param>
 /// <param name="startRow">Starting row index</param>
 /// <param name="endRow">End row index</param>
 /// <param name="columnIndices">Array of column indices</param>
 ///
 public static void Set <T>(this T[][] destination, T value, int startRow, int endRow, int[] columnIndices)
 {
     T[][] source = Jagged.Create(destination.Rows(), destination.Columns(), value);
     Set(destination, source, startRow, endRow, columnIndices);
 }
Exemplo n.º 7
0
 /// <summary>
 ///   Sets a region of a matrix to the given values.
 /// </summary>
 ///
 /// <param name="destination">The matrix where elements will be set.</param>
 /// <param name="value">The value to which matrix elements will be set.</param>
 ///
 public static void Set <T>(this T[][] destination, T value)
 {
     Set(destination, Jagged.Create(destination.Rows(), destination.Columns(), value));
 }
Exemplo n.º 8
0
 /// <summary>
 ///   Sets a region of a matrix to the given values.
 /// </summary>
 ///
 /// <param name="destination">The matrix where elements will be set.</param>
 /// <param name="value">The matrix of values to which matrix elements will be set.</param>
 /// <param name="rowIndices">Array of row indices</param>
 /// <param name="startColumn">Start column index</param>
 /// <param name="endColumn">End column index</param>
 ///
 public static void Set <T>(this T[][] destination, T value, int[] rowIndices, int startColumn, int endColumn)
 {
     Set(destination, Jagged.Create(destination.Rows(), destination.Columns(), value), rowIndices, startColumn, endColumn);
 }
 /// <summary>
 ///   Transforms a vector into a matrix of given dimensions.
 /// </summary>
 ///
 public static T[][] Reshape <T>(T[] array, int rows, int cols, MatrixOrder order = MatrixOrder.Default)
 {
     return(Jagged.Reshape(array, rows, cols, Jagged.Create <T>(rows, cols), order));
 }
 /// <summary>
 ///   Creates a matrix of k-hot vectors, where all values at each row are
 ///   zero except for the indicated <paramref name="indices"/>, which are set to one.
 /// </summary>
 ///
 /// <typeparam name="T">The data type for the matrix.</typeparam>
 ///
 /// <param name="indices">The rows's dimension which will be marked as one.</param>
 /// <param name="columns">The size (length) of the vectors (columns of the matrix).</param>
 ///
 /// <returns>A matrix containing k-hot vectors where only elements at the indicated
 ///   <paramref name="indices"/> are set to one and the others are zero.</returns>
 ///
 public static T[][] KHot <T>(int[][] indices, int columns)
 {
     return(KHot <T>(indices, Jagged.Create <T>(indices.Length, columns)));
 }
 /// <summary>
 ///   Returns a matrix with a vector of values on its diagonal.
 /// </summary>
 ///
 public static T[][] Diagonal <T>(int rows, int cols, T[] values)
 {
     return(Diagonal(rows, cols, values, Jagged.Create <T>(rows, cols)));
 }
 /// <summary>
 ///   Return a square matrix with a vector of values on its diagonal.
 /// </summary>
 ///
 public static T[][] Diagonal <T>(T[] values)
 {
     return(Diagonal(values, Jagged.Create <T>(values.Length, values.Length)));
 }
 /// <summary>
 ///   Returns a square diagonal matrix of the given size.
 /// </summary>
 ///
 public static T[][] Diagonal <T>(int size, T value)
 {
     return(Diagonal(size, value, Jagged.Create <T>(size, size)));
 }
 /// <summary>
 ///   Creates a matrix of k-hot vectors, where all values at each row are
 ///   zero except for the ones in the positions where <paramref name="mask"/>
 ///   are true, which are set to one.
 /// </summary>
 ///
 /// <param name="mask">The boolean mask determining where ones will be placed.</param>
 /// <param name="columns">The size (length) of the vectors (columns of the matrix).</param>
 ///
 /// <returns>A matrix containing one-hot vectors where only a single position
 ///   is one and the others are zero.</returns>
 ///
 public static double[][] KHot(bool[][] mask, int columns)
 {
     return(KHot(mask, Jagged.Create <double>(mask.Length, columns)));
 }
 /// <summary>
 ///   Creates a matrix of k-hot vectors, where all values at each row are
 ///   zero except for the indicated <paramref name="indices"/>, which are set to one.
 /// </summary>
 ///
 /// <param name="indices">The rows's dimension which will be marked as one.</param>
 /// <param name="columns">The size (length) of the vectors (columns of the matrix).</param>
 ///
 /// <returns>A matrix containing k-hot vectors where only elements at the indicated
 ///   <paramref name="indices"/> are set to one and the others are zero.</returns>
 ///
 public static double[][] KHot(int[][] indices, int columns)
 {
     return(KHot(indices, Jagged.Create <double>(indices.Length, columns)));
 }
Exemplo n.º 16
0
        /// <summary>
        ///   Sets a region of a matrix to the given values.
        /// </summary>
        ///
        /// <param name="destination">The matrix where elements will be set.</param>
        /// <param name="value">The matrix of values to which matrix elements will be set.</param>
        /// <param name="rowIndices">Array of row indices</param>
        /// <param name="columnIndices">Array of column indices</param>
        ///
        public static void Set <T>(this T[][] destination, T value, int[] rowIndices, int[] columnIndices)
        {
            var values = Jagged.Create <T>(destination.Rows(), destination.Columns(), value);

            set(destination, rowIndices, columnIndices, values, rowIndices, columnIndices);
        }
 /// <summary>
 ///   Creates a matrix of one-hot vectors, where all values at each row are
 ///   zero except for the ones in the positions where <paramref name="mask"/>
 ///   are true, which are set to one.
 /// </summary>
 ///
 /// <typeparam name="T">The data type for the matrix.</typeparam>
 ///
 /// <param name="mask">The boolean mask determining where ones will be placed.</param>
 ///
 /// <returns>A matrix containing one-hot vectors where only a single position
 ///   is one and the others are zero.</returns>
 ///
 public static T[][] OneHot <T>(bool[] mask)
 {
     return(OneHot <T>(mask, Jagged.Create <T>(mask.Length, 2)));
 }