Example #1
0
        public void FromMultiDimArray(Array dotNetArray)
        {
            if (dotNetArray.GetType().GetElementType().IsArray)
            {
                throw new Exception("Jagged arrays are not allowed here!");
            }

            int[] dims = new int[dotNetArray.Rank];

            for (int idx = 0; idx < dims.Length; idx++)
            {
                dims[idx] = dotNetArray.GetLength(idx);
            }

            Storage = new NDStorage();
            Storage.Allocate(dotNetArray.GetType().GetElementType(), new Shape(dims));

            Array internalStrg = Storage.GetData();

            var pufferShape = new Shape(dims);

            pufferShape.ChangeTensorLayout();

            int[]  idxDims  = null;
            object valueIdx = null;

            for (int idx = 0; idx < Storage.Shape.Size; idx++)
            {
                idxDims  = pufferShape.GetDimIndexOutShape(idx);
                valueIdx = dotNetArray.GetValue(pufferShape.GetDimIndexOutShape(idx));
                internalStrg.SetValue(valueIdx, Storage.Shape.GetIndexInShape(idxDims));
            }
        }
Example #2
0
        public Array ToMuliDimArray <T>()
        {
            Array dotNetArray = Arrays.Create(typeof(T), this.shape);

            var pufferShape = new Shape(shape);

            pufferShape.ChangeTensorLayout();

            int[]  indexes  = null;
            object idxValue = null;

            T[] array = Storage.GetData <T>();

            for (int idx = 0; idx < this.size; idx++)
            {
                indexes  = pufferShape.GetDimIndexOutShape(idx);
                idxValue = array[Storage.Shape.GetIndexInShape(slice, indexes)];
                dotNetArray.SetValue(idxValue, indexes);
            }

            return(dotNetArray);
        }
Example #3
0
        public Array GetData()
        {
            // since the view is a subset of the data we have to copy here
            int size = internal_shape.Size;
            var data = AllocateArray(size, _data.DType);

            // the algorithm is split into 1-D and N-D because for 1-D we need not go through coordinate transformation
            if (_slices.Length == 1)
            {
                for (var i = 0; i < size; i++)
                {
                    data.SetValue(GetValue(i), i);
                }
            }
            else
            {
                for (var i = 0; i < size; i++)
                {
                    data.SetValue(GetValue(internal_shape.GetDimIndexOutShape(i)), i);
                }
            }
            return(data);
        }
Example #4
0
        /// <summary>
        /// Return a copy of the array collapsed into one dimension.
        ///
        /// A 1-D array, containing the elements of the input, is returned.A copy is made only if needed.
        /// </summary>
        public NDArray flatten(string order = "C")
        {
            var nd = new NDArray(dtype, size);

            var s = new Shape(Storage.Shape.Dimensions);

            s.ChangeTensorLayout(order);

            switch (dtype.Name)
            {
            case "Int32":
                var values1 = Array as int[];
                var values2 = new int[size];
                for (int i = 0; i < size; i++)
                {
                    // Data<int>(s.GetDimIndexOutShape(i))
                    values2[i] = values1[Storage.Shape.GetIndexInShape(s.GetDimIndexOutShape(i))];
                }
                nd.Array = values2;
                break;
            }

            return(nd);
        }