예제 #1
0
        /// <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 == null)
            {
                result = Jagged.Create <T>(size, size);
            }

            if (!symmetric)
            {
                for (int i = 0; i < size; i++)
                {
                    result[i] = generator.Generate(size);
                }
            }
            else
            {
                for (int 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);
        }
예제 #2
0
        public static T[][] Diagonal <T>(T[][][] blocks)
        {
            int rows = 0;
            int cols = 0;

            for (int 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 (int i = 0; i < blocks.Length; i++)
            {
                for (int r = 0; r < blocks[i].Length; r++)
                {
                    for (int 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);
        }
        /// <summary>
        ///   Creates a matrix with uniformly distributed random data.
        /// </summary>
        ///
        public static int[][] Random(int size, int min, int max, bool symmetric = false, int[][] result = null)
        {
            if (result == null)
            {
                result = Jagged.Create <int>(size, size);
            }

            var random = Accord.Math.Random.Generator.Random;

            if (symmetric)
            {
                for (int i = 0; i < size; i++)
                {
                    for (int j = i; j < size; j++)
                    {
                        result[i][j] = result[j][i] = (int)random.Next((int)min, (int)max);
                    }
                }
            }
            else
            {
                for (int i = 0; i < size; i++)
                {
                    for (int j = i; j < size; j++)
                    {
                        result[i][j] = (int)random.Next((int)min, (int)max);
                    }
                }
            }
            return(result);
        }
예제 #4
0
        public static Array Create(Type elementType, params int[] shape)
        {
            int s = shape[0];

            if (shape.Length == 1)
            {
                return(Array.CreateInstance(elementType, s));
            }
            else
            {
                int[] rest = shape.Get(1, 0);

                if (s == 0)
                {
                    Array dummy     = Array.CreateInstance(elementType, rest);
                    Array container = Array.CreateInstance(dummy.GetType(), 0);
                    return(container);
                }
                else
                {
                    Array first     = Jagged.Create(elementType, rest);
                    Array container = Array.CreateInstance(first.GetType(), s);

                    container.SetValue(first, 0);
                    for (int i = 1; i < container.Length; i++)
                    {
                        container.SetValue(Create(elementType, rest), i);
                    }

                    return(container);
                }
            }
        }
        /// <summary>
        ///   Creates a matrix with uniformly distributed random data.
        /// </summary>
        ///
        public static decimal[][] Random(int size, decimal min, decimal max, bool symmetric = false, decimal[][] result = null)
        {
            if (result == null)
            {
                result = Jagged.Create <decimal>(size, size);
            }

            var random = Accord.Math.Random.Generator.Random;

            if (symmetric)
            {
                for (int i = 0; i < size; i++)
                {
                    for (int j = i; j < size; j++)
                    {
                        result[i][j] = result[j][i] = (decimal)random.NextDouble() * (max - min) + min;
                    }
                }
            }
            else
            {
                for (int i = 0; i < size; i++)
                {
                    for (int j = i; j < size; j++)
                    {
                        result[i][j] = (decimal)random.NextDouble() * (max - min) + min;
                    }
                }
            }
            return(result);
        }
예제 #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="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)
        {
            var values        = Jagged.Create <T>(destination.Rows(), destination.Columns(), value);
            var rowIndices    = Accord.Math.Vector.Range(startRow, end(endRow, destination.Rows()));
            var columnIndices = Accord.Math.Vector.Range(startCol, end(endCol, destination.Columns()));

            set(destination, rowIndices, columnIndices, values, rowIndices, columnIndices);
        }
예제 #7
0
        public static Array CreateAs(Array matrix, Type type)
        {
            int[] outputShape = Matrix.GetShape(matrix, type);

#if !NETSTANDARD1_4
            // multidimensional or jagged -> jagged
            return(Jagged.Create(elementType: type.GetInnerMostType(), shape: outputShape, value: type.GetDefaultValue()));
#else
            return(Jagged.Create(elementType: type.GetInnerMostType(), shape: outputShape, value: 0));
#endif
        }
예제 #8
0
        /// <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 == null)
            {
                result = Jagged.Create <T>(rows, cols);
            }

            for (int i = 0; i < rows; i++)
            {
                result[i] = generator.Generate(cols);
            }
            return(result);
        }
        /// <summary>
        ///   Creates a matrix with uniformly distributed random data.
        /// </summary>
        ///
        public static int[][] Random(int rows, int columns, int min, int max, int[][] result = null)
        {
            if (result == null)
            {
                result = Jagged.Create <int>(rows, columns);
            }

            var random = Accord.Math.Random.Generator.Random;

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    result[i][j] = (int)random.Next((int)min, (int)max);
                }
            }
            return(result);
        }
        /// <summary>
        ///   Creates a matrix with uniformly distributed random data.
        /// </summary>
        ///
        public static decimal[][] Random(int rows, int columns, decimal min, decimal max, decimal[][] result = null)
        {
            if (result == null)
            {
                result = Jagged.Create <decimal>(rows, columns);
            }

            var random = Accord.Math.Random.Generator.Random;

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    result[i][j] = (decimal)random.NextDouble() * (max - min) + min;
                }
            }
            return(result);
        }
예제 #11
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);
 }
예제 #12
0
 public static T[][] Diagonal <T>(int rows, int cols, T[] values)
 {
     return(Diagonal(rows, cols, values, Jagged.Create <T>(rows, cols)));
 }
예제 #13
0
 public static T[][] Diagonal <T>(T[] values)
 {
     return(Diagonal(values, Jagged.Create <T>(values.Length, values.Length)));
 }
예제 #14
0
 public static T[][] Diagonal <T>(int size, T value)
 {
     return(Diagonal(size, value, Jagged.Create <T>(size, size)));
 }
예제 #15
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));
 }
예제 #16
0
 public static double[][] KHot(bool[][] mask, int columns)
 {
     return(KHot(mask, Jagged.Create <double>(mask.Length, columns)));
 }
예제 #17
0
 public static double[][] KHot(int[][] indices, int columns)
 {
     return(KHot(indices, Jagged.Create <double>(indices.Length, columns)));
 }
예제 #18
0
 public static T[][] KHot <T>(int[][] indices, int columns)
 {
     return(KHot <T>(indices, Jagged.Create <T>(indices.Length, columns)));
 }
예제 #19
0
 public static T[][] OneHot <T>(bool[] mask)
 {
     return(OneHot <T>(mask, Jagged.Create <T>(mask.Length, 2)));
 }
예제 #20
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);
        }
예제 #21
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);
 }
예제 #22
0
 /// <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));
 }