Пример #1
0
        public static A ToA(PyObject array)
        {
            var info = new NumpyArrayInterface(array);

            PyObject arr = array;

            // If the array is not contiguous in memory, copy it first.
            // This overwrites the array (but obviously the contents stay the same).
            if (!info.IsCStyleContiguous)
            {
                arr = deepcopy.Invoke(array);
            }

            var result    = A.Create(info.DataType, info.Shape.Select(x => (int)x).ToArray());
            int nElements = info.NBytes / Marshal.SizeOf(info.DataType);

            if (result.Count != nElements)
            {
                throw new NumpyException("Element count mismatch");
            }

            if (result is A <byte> byteArray)
            {
                Marshal.Copy(info.Address, byteArray.Buffer, 0, result.Count);
            }
            else if (result is A <short> shortArray)
            {
                Marshal.Copy(info.Address, shortArray.Buffer, 0, result.Count);
            }
            else if (result is A <int> intArray)
            {
                Marshal.Copy(info.Address, intArray.Buffer, 0, result.Count);
            }
            else if (result is A <long> longArray)
            {
                Marshal.Copy(info.Address, longArray.Buffer, 0, result.Count);
            }
            else if (result is A <double> doubleArray)
            {
                Marshal.Copy(info.Address, doubleArray.Buffer, 0, result.Count);
            }
            else if (result is A <double> floatArray)
            {
                Marshal.Copy(info.Address, floatArray.Buffer, 0, result.Count);
            }
            else
            {
                byte[] data = new byte[info.NBytes];
                Marshal.Copy(info.Address, data, 0, info.NBytes);
                Buffer.BlockCopy(data, 0, result.Buffer, 0, info.NBytes);
            }

            return(result);
        }
Пример #2
0
            public static Array ToArray(PyObject array)
            {
                var info = new NumpyArrayInterface(array);
                // If the array is not contiguous in memory, copy it first.
                // This overwrites the array (but obviously the contents stay the same).
                PyObject arr = array;

                if (!info.IsCStyleContiguous)
                {
                    arr = deepcopy.Invoke(array);
                }

                byte[] data = new byte[info.NBytes];
                Marshal.Copy(info.Address, data, 0, info.NBytes);
                if (info.DataType == typeof(byte) && info.Shape.Length == 1)
                {
                    return(data);
                }
                var result = System.Array.CreateInstance(info.DataType, info.Shape);

                System.Buffer.BlockCopy(data, 0, result, 0, info.NBytes);
                return(result);
            }