Пример #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), 1);

            Array internalStrg = Storage.GetData();

            var pufferShape = new Shape(dims);

            pufferShape.ChangeTensorLayout(2);

            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));
            }
        }
Пример #2
0
        public void FromJaggedArray(Array dotNetArray)
        {
            if (!dotNetArray.GetType().GetElementType().IsArray)
            {
                throw new Exception("Multi dim arrays are not allowed here!");
            }

            List <int> dimList = new List <int>();

            dimList.Add(dotNetArray.Length);

            object currentArr = dotNetArray;

            while (currentArr.GetType().GetElementType().IsArray)
            {
                Array child = (Array)((Array)currentArr).GetValue(0);
                dimList.Add(child.Length);
                currentArr = child;
            }

            Type elementType = currentArr.GetType().GetElementType();

            int[] dims = dimList.ToArray();

            Shape shape = new Shape(dims);

            shape.ChangeTensorLayout(1);

            NDArray nd = new NDArray(elementType, shape);

            Array ndStrg = nd.Storage.GetData();

            for (int idx = 0; idx < shape.Size; idx++)
            {
                int[] indexes = shape.GetDimIndexOutShape(idx);

                Array puffer = (Array)dotNetArray.GetValue(indexes[0]);

                for (int jdx = 1; jdx < indexes.Length - 1; jdx++)
                {
                    puffer = (Array)puffer.GetValue(indexes[jdx]);
                }

                ndStrg.SetValue(puffer.GetValue(indexes[indexes.Length - 1]), nd.Storage.Shape.GetIndexInShape(indexes));
            }

            this.Storage = nd.Storage;
        }
        public Array ToMuliDimArray <T>()
        {
            Array dotNetArray = Array.CreateInstance(typeof(T), this.shape);

            var pufferShape = new Shape(shape);

            pufferShape.ChangeTensorLayout(2);

            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(indexes)];
                dotNetArray.SetValue(idxValue, indexes);
            }

            return(dotNetArray);
        }
Пример #4
0
        protected void _ChangeColumnToRowLayout()
        {
            if (_Shape.NDim == 1)
            {
            }
            else if (_Shape.NDim == 2)
            {
                var puffer = Array.CreateInstance(_values.GetType().GetElementType(), _values.Length);

                var pufferShape = new Shape(_Shape.Dimensions);
                pufferShape.ChangeTensorLayout(1);

                for (int idx = 0; idx < _values.Length; idx++)
                {
                    puffer.SetValue(_values.GetValue(idx), pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
                }

                _values = puffer;
            }
            else
            {
                var puffer = Array.CreateInstance(_values.GetType().GetElementType(), _values.Length);

                var pufferShape = new Shape(_Shape.Dimensions);
                pufferShape.ChangeTensorLayout(1);

                for (int idx = 0; idx < _values.Length; idx++)
                {
                    puffer.SetValue(_values.GetValue(idx), pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
                }

                _values = puffer;
            }
            _TensorLayout = 1;
            Shape.ChangeTensorLayout(1);
        }