Пример #1
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);
        }
Пример #2
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;
        }
Пример #3
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;
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
                }
            }
        }
Пример #7
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;
        }
Пример #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));
        }
Пример #9
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));
 }