Exemplo n.º 1
0
        public static void ArraySet <[AnyPrimitive] TScalar>(
            MFullArray <TScalar> array, int index, TScalar value)
        {
            Contract.Requires(array != null);

            if (index < 1)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (index > array.Count)
            {
                // Must resize, only works if the array is empty, a scalar or a vector
                // - Empty, scalar or row vector: grows in columns
                // - Column vector: grows in rows
                // - Otherwise: throws
                if (array.IsHigherDimensional)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                else if (array.RowCount <= 1)
                {
                    array.Resize(new MArrayShape(1, index));
                }
                else if (array.ColumnCount == 1)
                {
                    array.Resize(new MArrayShape(index, 1));
                }
                else
                {
                    throw new ArgumentOutOfRangeException("index");
                }
            }

            array[index - 1] = value;
        }
Exemplo n.º 2
0
        public static void ArraySet <[AnyPrimitive] TScalar>(
            MFullArray <TScalar> array, int rowIndex, int columnIndex, TScalar value)
        {
            // Performance critical method!
            Contract.Requires(array != null);

            if (rowIndex < 1)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }
            if (columnIndex < 1)
            {
                throw new ArgumentOutOfRangeException("columnIndex");
            }

            int rowCount = array.shape.RowCount;

            if (rowIndex > array.RowCount || columnIndex > array.ColumnCount)
            {
                // Array needs resizing
                // TODO: zeros(2, 2, 2) (3, 3) = 5 should work and produce a 3x2x2 matrix
                if (array.IsHigherDimensional)
                {
                    throw new ArgumentOutOfRangeException();
                }
                if (rowIndex > rowCount)
                {
                    rowCount = rowIndex;
                }
                int columnCount = Math.Max(array.shape.ColumnCount, columnIndex);
                array.Resize(new MArrayShape(rowCount, columnCount));
            }

            array.elements[(columnIndex - 1) * rowCount + (rowIndex - 1)] = value;
        }
Exemplo n.º 3
0
        public static void ArraySet <[AnyPrimitive] TScalar>(
            MFullArray <TScalar> array, double rowIndex, double columnIndex, TScalar value)
        {
            // Performance critical method!
            Contract.Requires(array != null);

            int rowIndexInt = ToInt(rowIndex);

            if (rowIndexInt < 1)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }

            int rowCount = array.shape.RowCount;

            int columnIndexInt = ToInt(columnIndex);

            if (rowIndexInt > rowCount || columnIndexInt > array.shape.ColumnCount)
            {
                if (array.shape.IsHigherDimensional)
                {
                    throw new ArgumentOutOfRangeException();
                }
                if (rowIndexInt > rowCount)
                {
                    rowCount = rowIndexInt;
                }
                int columnCount = Math.Max(array.shape.ColumnCount, columnIndexInt);
                array.Resize(new MArrayShape(rowCount, columnCount));
            }

            array.elements[(columnIndexInt - 1) * rowCount + (rowIndexInt - 1)] = value;
        }