forked from SciSharp/Numpy.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumPy.array.cs
More file actions
150 lines (140 loc) · 7.08 KB
/
NumPy.array.cs
File metadata and controls
150 lines (140 loc) · 7.08 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Numpy;
using Numpy.Models;
using Python.Runtime;
namespace Numpy
{
/// <summary>
/// Manual type conversions
/// </summary>
public partial class NumPy
{
public NDarray array(NDarray @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
{
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 NDarray<T> array<T>(T[] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
{
var type = @object.GetDtype();
var ndarray = this.empty(new Shape(@object.Length), dtype: dtype??type, order:order); // todo: check out the other parameters
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;
}
return new NDarray<T>(ndarray);
}
public NDarray<T> array<T>(T[,] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
{
var d1_array = @object.Cast<T>().ToArray();
var type = d1_array.GetDtype();
var ndarray = this.empty(new Shape(@object.GetLength(0), @object.GetLength(1)), dtype: dtype??type, order: order); // todo: check out the other parameters
if (@object.Length == 0)
return new NDarray<T>(ndarray);
long ptr = ndarray.PyObject.ctypes.data;
switch ((object)d1_array)
{
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;
}
return new NDarray<T>(ndarray);
}
public NDarray<T> array<T>(T[,,] @object, Dtype dtype = null, bool? copy = null, string order = null, bool? subok = null, int? ndmin = null)
{
var d1_array = @object.Cast<T>().ToArray();
var type = d1_array.GetDtype();
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
if (@object.Length == 0)
return new NDarray<T>(ndarray);
long ptr = ndarray.PyObject.ctypes.data;
switch ((object)d1_array)
{
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;
}
return new NDarray<T>(ndarray);
}
public NDarray array(string[] obj, int? itemsize = null, bool? copy = null, bool? unicode = null, string order = null)
{
var args = ToTuple(obj);
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);
}
/// <summary>
/// Create a 0d array from scalar
/// </summary>
/// <param name="scalar"></param>
/// <param name="dtype"></param>
/// <returns></returns>
public 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 T asscalar<T>(NDarray a) => self.InvokeMethod("asscalar", a.PyObject).As<T>();
}
}