Exemplo n.º 1
0
        /// <summary>
        /// Converts this array to a jagged array, while bringing indexing to zero-based.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="array">The array.</param>
        /// <returns>jagged array</returns>
        public static TValue[][] ToJaggedArray <TValue>([NotNull] this TValue[,] array)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            var aMin = array.GetLowerBound(0);
            var aMax = array.GetUpperBound(0);
            var rows = aMax - aMin + 1;

            var bMin = array.GetLowerBound(1);
            var bMax = array.GetUpperBound(1);
            var cols = bMax - bMin + 1;

            var result = new TValue[rows][];

            for (int row = 0; row < rows; row++)
            {
                result[row] = new TValue[cols];
                for (int col = 0; col < cols; col++)
                {
                    result[row][col] = array[row + aMin, col + bMin];
                }
            }
            return(result);
        }
        /// <summary>
        ///     Sorts the columns of a matrix by sorting keys.
        /// </summary>
        /// <param name="keys">The key value for each column.</param>
        /// <param name="values">The matrix to be sorted.</param>
        /// <param name="comparer">The comparer to use.</param>
        public static TValue[,] Sort <TKey, TValue>(TKey[] keys, TValue[,] values, IComparer <TKey> comparer)
        {
            var indices = Vector.Range(keys.Length);

            Array.Sort(keys.Copy(), indices, comparer);
            return(values.Get(0, values.Rows(), indices));
        }
 private void Init()
 {
     if (arr == null)
     {
         arr = new TValue[EnumHelper <Color> .EnumCount, EnumHelper <Square> .EnumCount];
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Construct a dense matrix from a 2d array.
        /// </summary>
        /// <param name="matrix"></param>
        public DenseMatrix(TValue[,] matrix)
        {
            int rows    = matrix.GetLength(0);
            int columns = matrix.GetLength(1);

            data = new TValue[rows, columns];
            Array.Copy(matrix, data, matrix.Length);
        }
Exemplo n.º 5
0
        public DenseSparse2DMatrix(AMatrix <TValue> src)
        {
            RowsCount = src.rows;
            ColsCount = src.cols;
            data      = new TValue[RowsCount, ColsCount];

            setData(src);
        }
Exemplo n.º 6
0
 public RectGridMap(int height, int width, TValue iv)
 {
     a = new TValue[height, width];
     for (int i = 0; i < height; ++i)
     {
         for (int j = 0; j < width; ++j)
         {
             a[i, j] = iv;
         }
     }
 }
Exemplo n.º 7
0
 public RectGridMap(int height, int width, Func <TValue> getIV)
 {
     a = new TValue[height, width];
     for (int i = 0; i < height; ++i)
     {
         for (int j = 0; j < width; ++j)
         {
             a[i, j] = getIV();
         }
     }
 }
Exemplo n.º 8
0
        /// <summary>
        ///   Sorts the columns of a matrix by sorting keys.
        /// </summary>
        /// <param name="keys">The key value for each column.</param>
        /// <param name="values">The matrix to be sorted.</param>
        /// <param name="comparer">The comparer to use.</param>
        public static TValue[,] Sort <TKey, TValue>(TKey[] keys, TValue[,] values, IComparer <TKey> comparer)
        {
            int[] indices = new int[keys.Length];
            for (int i = 0; i < keys.Length; i++)
            {
                indices[i] = i;
            }

            Array.Sort <TKey, int>(keys, indices, comparer);

            return(values.Submatrix(0, values.GetLength(0) - 1, indices));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the DenseMatrix class that wraps a .NET 2-D array
        /// </summary>
        /// <param name="valueArray">The 2-D .NET array to wrap.</param>
        /// <param name="rowKeySequence">A sequence of row keys. The items will become the RowKeys of the Matrix.</param>
        /// <param name="colKeySequence">A sequence of colKeys. The items will come the ColKeys of the Matrix.</param>
        /// <param name="missingValue">The special value that represents missing.</param>
        public DenseMatrix(ref TValue[,] valueArray, IEnumerable <TRowKey> rowKeySequence, IEnumerable <TColKey> colKeySequence, TValue missingValue)
        {
            _rowKeys = new ReadOnlyCollection <TRowKey>(rowKeySequence.ToList());
            _colKeys = new ReadOnlyCollection <TColKey>(colKeySequence.ToList());
            Helper.CheckCondition(valueArray.GetLength(0) == _rowKeys.Count, Properties.Resource.ExpectedRowKeysCountToEqualValueArrayCount, _rowKeys.Count, valueArray.GetLength(0));
            Helper.CheckCondition(valueArray.GetLength(1) == _colKeys.Count, Properties.Resource.ExpectedColumnKeysCountToEqualValueArrayCount, _colKeys.Count, valueArray.GetLength(1));

            //!!!Matrix - these lines appear in many places
            _indexOfRowKey = RowKeys.Select((key, index) => new { key, index }).ToDictionary(pair => pair.key, pair => pair.index);
            _indexOfColKey = ColKeys.Select((key, index) => new { key, index }).ToDictionary(pair => pair.key, pair => pair.index);

            ValueArray    = valueArray;
            _missingValue = missingValue;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the DenseMatrix class that wraps a .NET 2-D array
        /// </summary>
        /// <param name="valueArray">The 2-D .NET array to wrap.</param>
        /// <param name="rowKeySequence">A sequence of row keys. The items will become the RowKeys of the Matrix.</param>
        /// <param name="colKeySequence">A sequence of colKeys. The items will come the ColKeys of the Matrix.</param>
        /// <param name="missingValue">The special value that represents missing.</param>
        public DenseMatrix(ref TValue[,] valueArray, IEnumerable <TRowKey> rowKeySequence, IEnumerable <TColKey> colKeySequence, TValue missingValue)
        {
            _rowKeys = new ReadOnlyCollection <TRowKey>(rowKeySequence.ToList());
            _colKeys = new ReadOnlyCollection <TColKey>(colKeySequence.ToList());
            Helper.CheckCondition(valueArray.GetLength(0) == _rowKeys.Count, "Expect the # of rows in the input array to match the # of items in varList");
            Helper.CheckCondition(valueArray.GetLength(1) == _colKeys.Count, "Expect the # of cols in the input array to match the # of items in cidList");

            //!!!Matrix - these lines appear in many places
            _indexOfRowKey = RowKeys.Select((key, index) => new { key, index }).ToDictionary(pair => pair.key, pair => pair.index);
            _indexOfColKey = ColKeys.Select((key, index) => new { key, index }).ToDictionary(pair => pair.key, pair => pair.index);

            ValueArray    = valueArray;
            _missingValue = missingValue;
        }
Exemplo n.º 11
0
        public static TValue[,] MirrorHorizontal <TValue>(TValue[,] matrix)
        {
            int rows = matrix.GetLength(0);
            int cols = matrix.GetLength(1);

            TValue[,] mirroredMatrix = new TValue[rows, cols];
            for (int rowIndex = 0; rowIndex < rows; ++rowIndex)
            {
                for (int colIndex = 0; colIndex < cols; ++colIndex)
                {
                    mirroredMatrix[rows - 1 - rowIndex, colIndex] = matrix[rowIndex, colIndex];
                }
            }
            return(mirroredMatrix);
        }
Exemplo n.º 12
0
        public static TValue[,] RotateLeft <TValue>(TValue[,] matrix)
        {
            int rows = matrix.GetLength(0);
            int cols = matrix.GetLength(1);

            TValue[,] rotatedMatrix = new TValue[rows, cols];
            for (int rowIndex = 0; rowIndex < rows; ++rowIndex)
            {
                for (int colIndex = 0; colIndex < cols; ++colIndex)
                {
                    rotatedMatrix[rowIndex, colIndex] = matrix[cols - colIndex - 1, rowIndex];
                }
            }
            return(rotatedMatrix);
        }
Exemplo n.º 13
0
        //
        // </ Collection >
        //

        //
        // < Array >
        //

        public static void ToJsonArrayFromJobjArray <TValue>(IVOSJsonArray jarr, TValue[,] arr)
            where TValue : IVOSJsonSirializable
        {
            IVOSJsonArray  rw   = null;
            IVOSJsonObject jobj = null;

            for (int i = 0; i < arr.GetLength(0); ++i)
            {
                rw = jarr.AddArrayItem();
                for (int j = 0; j < arr.GetLength(1); ++j)
                {
                    jobj = rw.AddObjectItem();
                    arr[i, j].SerializeToJson(jobj);
                }
            }
        }
Exemplo n.º 14
0
        //---------------------------------------------------------------------

        public InputRaster(string path,
                           TValue[,]               data,
                           ConvertToUShort <TValue> convertToUShort)
            : base(path)
        {
            if (!(typeof(TValue) == typeof(byte) || typeof(TValue) == typeof(ushort)))
            {
                throw new System.ApplicationException("Type parameter TValue is not byte or ushort");
            }
            this.data       = data;
            this.Dimensions = new Dimensions(data.GetLength(0),
                                             data.GetLength(1));

            //  Initialize current pixel location so that RowMajor.Next returns
            //  location (1,1).
            this.currentPixelLoc = new Location(1, 0);

            this.pixel           = new Pixel();
            this.convertToUShort = convertToUShort;
        }
Exemplo n.º 15
0
        public static void FromJsonArrayToJobjArray <TValue>(IVOSJsonArray jarr, TValue[,] arr)
            where TValue : IVOSJsonSirializable, new()
        {
            TValue         val  = new TValue();
            IVOSJsonArray  rw   = null;
            IVOSJsonObject jobj = null;

            for (int i = 0; i < jarr.count; ++i)
            {
                rw = jarr.GetArrayItem(i);
                for (int j = 0; j < rw.count; ++j)
                {
                    jobj = rw.GetObjectItem(j);

                    val = new TValue();
                    val.DeserializeFromJson(jobj);

                    arr[i, j] = val;
                }
            }
        }
Exemplo n.º 16
0
 protected GenericRankArrayOutConstructorBase(out TValue[,] value)
 {
     value = new TValue[0, 0];
 }
Exemplo n.º 17
0
 /// <summary>
 ///   Sorts the columns of a matrix by sorting keys.
 /// </summary>
 ///
 /// <param name="keys">The key value for each column.</param>
 /// <param name="values">The matrix to be sorted.</param>
 /// <param name="comparer">The comparer to use.</param>
 ///
 public static TValue[,] Sort <TKey, TValue>(TKey[] keys, TValue[,] values, IComparer <TKey> comparer)
 {
     int[] indices = Accord.Math.Vector.Range(keys.Length);
     Array.Sort <TKey, int>(keys, indices, comparer);
     return(values.Get(0, values.Rows(), indices));
 }
Exemplo n.º 18
0
 /// <summary>
 /// Initializes a new instance of the DenseMatrix class that wraps a .NET 2-D array
 /// </summary>
 /// <param name="valueArray">The 2-D .NET array to wrap.</param>
 /// <param name="rowKeySequence">A sequence of row keys. The items will become the RowKeys of the Matrix.</param>
 /// <param name="colKeySequence">A sequence of colKeys. The items will come the ColKeys of the Matrix.</param>
 /// <param name="missingValue">The special value that represents missing.</param>
 public DenseMatrix(TValue[,] valueArray, IEnumerable <TRowKey> rowKeySequence, IEnumerable <TColKey> colKeySequence, TValue missingValue)
     : this(ref valueArray, rowKeySequence, colKeySequence, missingValue)
 {
 }
Exemplo n.º 19
0
 protected GenericRankArrayRefConstructorBase(ref TValue[,] value)
 {
     value = new TValue[0, 0];
 }
Exemplo n.º 20
0
 public virtual void Method <TValue>(out TValue[,] value)
 {
     value = new TValue[0, 0];
 }
Exemplo n.º 21
0
 public Indexable2DArray(TValue[,] array)
 {
     _array = array;
 }
Exemplo n.º 22
0
 public GenericRankArrayConstructor(TValue[,] value)
 {
 }
Exemplo n.º 23
0
 public virtual void Method <TValue>(TValue[,] value)
 {
 }
Exemplo n.º 24
0
 public RectGridMap(TValue[,] a)
 {
     this.a = a;
 }
Exemplo n.º 25
0
 /// <summary>
 /// Constructor - creates an empty sparse array instance.
 /// </summary>
 public DenseSparse2DMatrix(int rowsCount, int colsCount)
 {
     RowsCount = rowsCount;
     ColsCount = colsCount;
     data      = new TValue[rowsCount, colsCount];
 }
Exemplo n.º 26
0
 public abstract void Method <TValue>(TValue[,] value);
Exemplo n.º 27
0
 public GenericRankArrayRefConstructor(ref TValue[,] value)
 {
     value = new TValue[0, 0];
 }
Exemplo n.º 28
0
 public static TValue GetValueOrDefault <TValue>(this TValue[,] array, int i, int j)
 {
     return((i < array.GetLength(0) && i >= 0 && j < array.GetLength(1) && j >= 0) ? array[i, j] : default(TValue));
 }
Exemplo n.º 29
0
 public GenericRankArrayOutConstructor(out TValue[,] value)
 {
     value = new TValue[0, 0];
 }
Exemplo n.º 30
0
 protected GenericRankArrayConstructorBase(TValue[,] value)
 {
 }