Repository navigation
Expand file tree
/
Copy pathnp.array.cs
More file actions
204 lines (185 loc) · 10.1 KB
/
Copy pathnp.array.cs
File metadata and controls
204 lines (185 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using Numpy.Models;
using Python.Runtime;
namespace Numpy
{
public static partial class np
{
/// <summary>
/// Create an array.
///
/// <param name="data">
/// The array to initialize the ndarray with
/// </param>
/// <returns>
/// An array object satisfying the specified requirements.
/// </returns>
public static NDarray<T> array<T>(params T[] data) where T : struct
{
var __self__ = self;
return array(data, dtype:null);
}
public static NDarray array(NDarray @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
{
var __self__ = self;
var args = ToTuple(new object[]
{
@object,
});
var kwargs = new PyDict();
if (dtype != null) kwargs["dtype"] = ToPython(dtype);
if (copy != null) kwargs["copy"] = ToPython(copy);
if (order != null) kwargs["order"] = ToPython(order);
if (subok != null) kwargs["subok"] = ToPython(subok);
if (ndmin != null) kwargs["ndmin"] = ToPython(ndmin);
dynamic py = self.InvokeMethod("array", args, kwargs);
return ToCsharp<NDarray>(py);
}
public static NDarray<T> array<T>(T[] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) where T : struct
{
var __self__ = self;
var type = @object.GetDtype();
var ndarray = np.empty(new Shape(@object.Length), dtype: type, order: order);
if (@object.Length == 0)
return new NDarray<T>(ndarray);
long ptr = ndarray.PyObject.ctypes.data;
switch ((object)@object)
{
case char[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
case byte[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
case short[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
case int[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
case long[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
case float[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
case double[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
case bool[] a:
var bytes = a.Select(x => (byte)(x ? 1 : 0)).ToArray();
Marshal.Copy(bytes, 0, new IntPtr(ptr), a.Length);
break;
case Complex[] a:
var real = new double[@object.Length];
var imag = new double[@object.Length];
for (int i = 0; i < @object.Length; i++)
{
real[i] = a[i].Real;
imag[i] = a[i].Imaginary;
}
var ndreal = np.array(real);
var ndimag = np.array(imag);
ndarray.real = ndreal;
ndarray.imag = ndimag;
break;
}
if (dtype !=null || subok != null || ndmin != null)
return new NDarray<T>(np.array(ndarray, dtype:dtype, copy: false, subok: subok, ndmin: ndmin));
return new NDarray<T>(ndarray);
}
public static NDarray<T> array<T>(T[,] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) where T : struct
{
var __self__ = self;
var d1_array = @object.Cast<T>().ToArray();
var shape = new Shape(@object.GetLength(0), @object.GetLength(1));
var ndarray = array<T>(d1_array, dtype, copy, order, subok, ndmin);
return new NDarray<T>(ndarray.reshape(shape));
}
public static NDarray<T> array<T>(T[,,] data, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) where T : struct
{
var __self__ = self;
var d1_array = data.Cast<T>().ToArray();
var shape = new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2));
var ndarray = array<T>(d1_array, dtype, copy, order, subok, ndmin);
return new NDarray<T>(ndarray.reshape(shape));
}
public static NDarray<T> array<T>(T[,,,] data, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) where T : struct
{
var __self__ = self;
var d1_array = data.Cast<T>().ToArray();
var shape = new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3));
var ndarray = array<T>(d1_array, dtype, copy, order, subok, ndmin);
return new NDarray<T>(ndarray.reshape(shape));
}
public static NDarray<T> array<T>(T[,,,,] data, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) where T : struct
{
var __self__ = self;
var d1_array = data.Cast<T>().ToArray();
var shape = new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4));
var ndarray = array<T>(d1_array, dtype, copy, order, subok, ndmin);
return new NDarray<T>( ndarray.reshape(shape));
}
public static NDarray<T> array<T>(T[,,,,,] data, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null) where T : struct
{
var __self__ = self;
var d1_array = data.Cast<T>().ToArray();
var shape = new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3), data.GetLength(4), data.GetLength(5));
var ndarray = array<T>(d1_array, dtype, copy, order, subok, ndmin);
return new NDarray<T>(ndarray.reshape(shape));
}
public static NDarray array(string[] strings, int? itemsize = null, bool? copy = null, bool? unicode = null, string order = null)
{
var __self__ = self;
var args = new PyTuple(new PyObject[] { new PyList(strings.Select(s => new PyString(s) as PyObject).ToArray()) });
//var args = new PyList(new PyObject[0]);
//foreach (var s in strings)
// args.Append(new PyString(s));
var kwargs = new PyDict();
if (itemsize != null) kwargs["itemsize"] = ToPython(itemsize);
if (copy != null) kwargs["copy"] = ToPython(copy);
if (unicode != null) kwargs["unicode"] = ToPython(unicode);
if (order != null) kwargs["order"] = ToPython(order);
dynamic py = self.InvokeMethod("array", args, kwargs);
return ToCsharp<NDarray>(py);
}
public static NDarray array<T>(NDarray<T>[] arrays, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
=> array(arrays.OfType<NDarray>(), dtype, copy, order, subok, ndmin);
public static NDarray array<T>(IEnumerable<NDarray<T>> arrays, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
=> array(arrays.OfType<NDarray>(), dtype, copy, order, subok, ndmin);
public static NDarray array(List<NDarray> arrays, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
=> array((IEnumerable<NDarray>) arrays, dtype, copy, order, subok, ndmin);
public static NDarray array(NDarray[] arrays, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
=> array((IEnumerable<NDarray>)arrays, dtype, copy, order, subok, ndmin);
public static NDarray array(IEnumerable<NDarray> arrays, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
{
var __self__ = self;
var args = new PyTuple(new PyObject[]{ new PyList(arrays.Select(nd => nd.PyObject as PyObject).ToArray())});
var kwargs = new PyDict();
if (dtype != null) kwargs["dtype"] = ToPython(dtype);
if (copy != null) kwargs["copy"] = ToPython(copy);
if (order != null) kwargs["order"] = ToPython(order);
if (subok != true) kwargs["subok"] = ToPython(subok);
if (ndmin != null) kwargs["ndmin"] = ToPython(ndmin);
dynamic py = self.InvokeMethod("array", args, kwargs);
//dynamic py = dynamic_self.array(arrays, dtype, copy, order, subok, ndmin);
return ToCsharp<NDarray>(py);
}
public static NDarray asarray(ValueType scalar, Dtype dtype = null)
{
var __self__ = self;
var pyargs = ToTuple(new object[]
{
scalar,
});
var kwargs = new PyDict();
if (dtype != null) kwargs["dtype"] = ToPython(dtype);
dynamic py = __self__.InvokeMethod("asarray", pyargs, kwargs);
return ToCsharp<NDarray>(py);
}
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <returns>
/// Scalar representation of a. The output data type is the same type
/// returned by the input’s item method.
/// </returns>
public static T asscalar<T>(NDarray a)
{
if (typeof(T) == typeof(Complex))
return (T)(object) new Complex(a.real.asscalar<double>(), a.imag.asscalar<double>());
return self.InvokeMethod("asscalar", a.PyObject).As<T>();
}
}
}