|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | +using Numpy; |
| 7 | +using Numpy.Models; |
| 8 | +using Python.Runtime; |
| 9 | + |
| 10 | +namespace Numpy |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Manual type conversions |
| 14 | + /// </summary> |
| 15 | + public partial class NumPy |
| 16 | + { |
| 17 | + |
| 18 | + public NDarray array(NDarray @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) |
| 19 | + { |
| 20 | + var args = ToTuple(new object[] |
| 21 | + { |
| 22 | + @object, |
| 23 | + }); |
| 24 | + var kwargs = new PyDict(); |
| 25 | + if (dtype != null) kwargs["dtype"] = ToPython(dtype); |
| 26 | + if (copy != null) kwargs["copy"] = ToPython(copy); |
| 27 | + if (order != null) kwargs["order"] = ToPython(order); |
| 28 | + if (subok != null) kwargs["subok"] = ToPython(subok); |
| 29 | + if (ndmin != null) kwargs["ndmin"] = ToPython(ndmin); |
| 30 | + dynamic py = self.InvokeMethod("array", args, kwargs); |
| 31 | + return ToCsharp<NDarray>(py); |
| 32 | + } |
| 33 | + |
| 34 | + public NDarray<T> array<T>(T[] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) |
| 35 | + { |
| 36 | + var type = @object.GetDtype(); |
| 37 | + //if (dtype != null && !type.Equals( dtype)) |
| 38 | + // throw new NotImplementedException("Type of the array is different from specified dtype. Data conversion is not supported (yet)"); |
| 39 | + var ndarray = this.empty(new Shape(@object.Length), dtype: dtype??type, order:order); // todo: check out the other parameters |
| 40 | + long ptr = ndarray.PyObject.ctypes.data; |
| 41 | + switch ((object)@object) |
| 42 | + { |
| 43 | + case byte[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 44 | + case short[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 45 | + case int[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 46 | + case long[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 47 | + case float[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 48 | + case double[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 49 | + case bool[] a: |
| 50 | + var bytes = a.Select(x => (byte)(x ? 1 : 0)).ToArray(); |
| 51 | + Marshal.Copy(bytes, 0, new IntPtr(ptr), a.Length); |
| 52 | + break; |
| 53 | + } |
| 54 | + return new NDarray<T>(ndarray); |
| 55 | + } |
| 56 | + |
| 57 | + public NDarray<T> array<T>(T[,] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) |
| 58 | + { |
| 59 | + var d1_array = @object.Cast<T>().ToArray(); |
| 60 | + var type = d1_array.GetDtype(); |
| 61 | + //if (dtype != null && type != dtype) |
| 62 | + // throw new NotImplementedException("Type of the array is different from specified dtype. Data conversion is not supported (yet)"); |
| 63 | + var ndarray = this.empty(new Shape(@object.GetLength(0), @object.GetLength(1)), dtype: dtype??type, order: order); // todo: check out the other parameters |
| 64 | + long ptr = ndarray.PyObject.ctypes.data; |
| 65 | + switch ((object)d1_array) |
| 66 | + { |
| 67 | + case byte[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 68 | + case short[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 69 | + case int[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 70 | + case long[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 71 | + case float[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 72 | + case double[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 73 | + case bool[] a: |
| 74 | + var bytes = a.Select(x => (byte) (x ? 1 : 0)).ToArray(); |
| 75 | + Marshal.Copy(bytes, 0, new IntPtr(ptr), a.Length); |
| 76 | + break; |
| 77 | + } |
| 78 | + return new NDarray<T>(ndarray); |
| 79 | + } |
| 80 | + |
| 81 | + public NDarray<T> array<T>(T[,,] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) |
| 82 | + { |
| 83 | + var d1_array = @object.Cast<T>().ToArray(); |
| 84 | + var type = d1_array.GetDtype(); |
| 85 | + //if (dtype != null && type != dtype) |
| 86 | + // throw new NotImplementedException("Type of the array is different from specified dtype. Data conversion is not supported (yet)"); |
| 87 | + var ndarray = this.empty(new Shape(@object.GetLength(0), @object.GetLength(1), @object.GetLength(2)), dtype: dtype ?? type, order: order); // todo: check out the other parameters |
| 88 | + long ptr = ndarray.PyObject.ctypes.data; |
| 89 | + switch ((object)d1_array) |
| 90 | + { |
| 91 | + case byte[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 92 | + case short[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 93 | + case int[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 94 | + case long[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 95 | + case float[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 96 | + case double[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break; |
| 97 | + case bool[] a: |
| 98 | + var bytes = a.Select(x => (byte)(x ? 1 : 0)).ToArray(); |
| 99 | + Marshal.Copy(bytes, 0, new IntPtr(ptr), a.Length); |
| 100 | + break; |
| 101 | + } |
| 102 | + return new NDarray<T>(ndarray); |
| 103 | + } |
| 104 | + |
| 105 | + public NDarray array(string[] obj, int? itemsize = null, bool? copy = null, bool? unicode = null, string order = null) |
| 106 | + { |
| 107 | + var args = ToTuple(obj); |
| 108 | + var kwargs = new PyDict(); |
| 109 | + if (itemsize != null) kwargs["itemsize"] = ToPython(itemsize); |
| 110 | + if (copy != null) kwargs["copy"] = ToPython(copy); |
| 111 | + if (unicode != null) kwargs["unicode"] = ToPython(unicode); |
| 112 | + if (order != null) kwargs["order"] = ToPython(order); |
| 113 | + dynamic py = self.InvokeMethod("array", args, kwargs); |
| 114 | + return ToCsharp<NDarray>(py); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Create a 0d array from scalar |
| 119 | + /// </summary> |
| 120 | + /// <param name="scalar"></param> |
| 121 | + /// <param name="dtype"></param> |
| 122 | + /// <returns></returns> |
| 123 | + public NDarray asarray(ValueType scalar, Dtype dtype = null) |
| 124 | + { |
| 125 | + var __self__ = self; |
| 126 | + var pyargs = ToTuple(new object[] |
| 127 | + { |
| 128 | + scalar, |
| 129 | + }); |
| 130 | + var kwargs = new PyDict(); |
| 131 | + if (dtype != null) kwargs["dtype"] = ToPython(dtype); |
| 132 | + dynamic py = __self__.InvokeMethod("asarray", pyargs, kwargs); |
| 133 | + return ToCsharp<NDarray>(py); |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Convert an array of size 1 to its scalar equivalent. |
| 138 | + /// </summary> |
| 139 | + /// <returns> |
| 140 | + /// Scalar representation of a. The output data type is the same type |
| 141 | + /// returned by the input’s item method. |
| 142 | + /// </returns> |
| 143 | + public T asscalar<T>(NDarray a) => self.InvokeMethod("asscalar", a.PyObject).As<T>(); |
| 144 | + |
| 145 | + |
| 146 | + } |
| 147 | +} |
0 commit comments