Skip to content

Commit 2d1035d

Browse files
committed
added Numpy.Bare which doesn't distribute Python and Numpy wheel
1 parent 7922051 commit 2d1035d

80 files changed

Lines changed: 60100 additions & 399 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Numpy.NET.sln

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Numpy.UnitTest", "test\Nump
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{25D84E98-E107-45C9-A0EC-0B25E24DA607}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MatmulExample", "src\Examples\MatmulExample\MatmulExample.csproj", "{A92C1287-BF2B-4FDC-8BBA-A17F410CD41F}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MatmulExample", "src\Examples\MatmulExample\MatmulExample.csproj", "{A92C1287-BF2B-4FDC-8BBA-A17F410CD41F}"
13+
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Numpy.Bare", "src\Numpy.Bare\Numpy.Bare.csproj", "{DA501219-D114-4ED1-9B34-C39CC0480D04}"
15+
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Numpy.Bare.UnitTest", "test\Numpy.UnitTest\Numpy.Bare.UnitTest.csproj", "{3E69A902-0C8F-4FF4-BA27-EAB9D2E08FEA}"
1317
EndProject
1418
Global
1519
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -29,6 +33,14 @@ Global
2933
{A92C1287-BF2B-4FDC-8BBA-A17F410CD41F}.Debug|Any CPU.Build.0 = Debug|Any CPU
3034
{A92C1287-BF2B-4FDC-8BBA-A17F410CD41F}.Release|Any CPU.ActiveCfg = Release|Any CPU
3135
{A92C1287-BF2B-4FDC-8BBA-A17F410CD41F}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{DA501219-D114-4ED1-9B34-C39CC0480D04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{DA501219-D114-4ED1-9B34-C39CC0480D04}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{DA501219-D114-4ED1-9B34-C39CC0480D04}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{DA501219-D114-4ED1-9B34-C39CC0480D04}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{3E69A902-0C8F-4FF4-BA27-EAB9D2E08FEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{3E69A902-0C8F-4FF4-BA27-EAB9D2E08FEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{3E69A902-0C8F-4FF4-BA27-EAB9D2E08FEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{3E69A902-0C8F-4FF4-BA27-EAB9D2E08FEA}.Release|Any CPU.Build.0 = Release|Any CPU
3244
EndGlobalSection
3345
GlobalSection(SolutionProperties) = preSolution
3446
HideSolutionNode = FALSE
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Threading.Tasks;
34
using Numpy;
5+
using Python.Runtime;
46

57
namespace MatmulExample
68
{
79
class Program
810
{
911
static void Main(string[] args)
1012
{
11-
Console.WriteLine("Efficient matrix multiplication with NumPy (only GPU backed libraries are faster):");
12-
// before starting the measurement, let us call numpy once to get the setup checks done.
13-
np.arange(1);
13+
Console.WriteLine("Efficient matrix multiplication with NumPy:");
14+
// before starting the measurement, let us call numpy once to get the setup checks done.
15+
var stopwatch = Stopwatch.StartNew();
16+
np.arange(1);
1417

15-
var stopwatch = Stopwatch.StartNew();
16-
var a1 = np.arange(60000).reshape(300, 200);
17-
var a2 = np.arange(80000).reshape(200, 400);
18+
var a1 = np.arange(60000).reshape(300, 200);
19+
var a2 = np.arange(80000).reshape(200, 400);
1820

19-
var result = np.matmul(a1, a2);
20-
stopwatch.Stop();
21+
var result = np.matmul(a1, a2);
22+
stopwatch.Stop();
2123

22-
Console.WriteLine($"execution time with NumPy: {stopwatch.Elapsed.TotalMilliseconds}ms\n");
24+
Console.WriteLine($"execution time with NumPy: {stopwatch.Elapsed.TotalMilliseconds}ms\n");
25+
Console.WriteLine("Result:\n" + result.repr);
2326

24-
Console.WriteLine("Result:\n"+result.repr);
25-
Console.ReadKey();
27+
28+
Console.WriteLine("executing on bg thread");
29+
30+
var a = np.arange(1000);
31+
var b = np.arange(1000);
32+
33+
// https://github.com/pythonnet/pythonnet/issues/109
34+
PythonEngine.BeginAllowThreads();
35+
36+
Task.Run(() =>
37+
{
38+
using (Py.GIL())
39+
{
40+
np.matmul(a, b);
41+
Console.WriteLine("matmul on bg thread is done");
42+
}
43+
}).Wait();
44+
Console.WriteLine("Press key");
45+
46+
Console.ReadKey();
2647
}
2748
}
2849
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
/// <summary>
19+
/// Matrix or vector norm.
20+
///
21+
/// This function is able to return one of eight different matrix norms,
22+
/// or one of an infinite number of vector norms (described below), depending
23+
/// on the value of the ord parameter.
24+
///
25+
/// Notes
26+
///
27+
/// For values of ord &lt;= 0, the result is, strictly speaking, not a
28+
/// mathematical ‘norm’, but it may still be useful for various numerical
29+
/// purposes.
30+
///
31+
/// The following norms can be calculated:
32+
///
33+
/// The Frobenius norm is given by [1]:
34+
///
35+
/// The nuclear norm is the sum of the singular values.
36+
///
37+
/// References
38+
/// </summary>
39+
/// <param name="x">
40+
/// Input array. If axis is None, x must be 1-D or 2-D.
41+
/// </param>
42+
/// <param name="ord">
43+
/// Order of the norm (see table under Notes). inf means numpy’s
44+
/// inf object.
45+
/// </param>
46+
/// <param name="axis">
47+
/// If axis is an integer, it specifies the axis of x along which to
48+
/// compute the vector norms. If axis is a 2-tuple, it specifies the
49+
/// axes that hold 2-D matrices, and the matrix norms of these matrices
50+
/// are computed. If axis is None then either a vector norm (when x
51+
/// is 1-D) or a matrix norm (when x is 2-D) is returned.
52+
/// </param>
53+
/// <param name="keepdims">
54+
/// If this is set to True, the axes which are normed over are left in the
55+
/// result as dimensions with size one. With this option the result will
56+
/// broadcast correctly against the original x.
57+
/// </param>
58+
/// <returns>
59+
/// Norm of the matrix or vector(s).
60+
/// </returns>
61+
public NDarray norm(NDarray x, int? ord = null, int[] axis = null, bool? keepdims = null)
62+
{
63+
var pyargs = ToTuple(new object[] { x, });
64+
var kwargs = new PyDict();
65+
if (ord != null) kwargs["ord"] = ToPython(ord);
66+
if (axis != null) kwargs["axis"] = ToPython(axis);
67+
if (keepdims != null) kwargs["keepdims"] = ToPython(keepdims);
68+
var linalg = self.GetAttr("linalg");
69+
dynamic py = linalg.InvokeMethod("norm", pyargs, kwargs);
70+
return ToCsharp <NDarray> (py);
71+
}
72+
73+
public float norm(NDarray x, int? ord = null)
74+
{
75+
var pyargs = ToTuple(new object[] { x, });
76+
var kwargs = new PyDict();
77+
if (ord != null) kwargs["ord"] = ToPython(ord);
78+
var linalg = self.GetAttr("linalg");
79+
dynamic py = linalg.InvokeMethod("norm", pyargs, kwargs);
80+
81+
return ToCsharp<float>(py);
82+
}
83+
84+
public float norm(NDarray x, string ord)
85+
{
86+
var pyargs = ToTuple(new object[] { x, });
87+
var kwargs = new PyDict();
88+
if (ord != null) kwargs["ord"] = ToPython(ord);
89+
var linalg = self.GetAttr("linalg");
90+
dynamic py = linalg.InvokeMethod("norm", pyargs, kwargs);
91+
return ToCsharp<float>(py);
92+
}
93+
94+
public float norm(NDarray x, Constants ord)
95+
{
96+
if (ord!=Constants.inf && ord!=Constants.neg_inf)
97+
throw new ArgumentException("ord must be either inf or neg_inf");
98+
99+
var pyargs = ToTuple(new object[] { x, });
100+
var kwargs = new PyDict();
101+
if (ord != null) kwargs["ord"] = ord==Constants.inf ? self.inf : -(self.inf);
102+
var linalg = self.GetAttr("linalg");
103+
dynamic py = linalg.InvokeMethod("norm", pyargs, kwargs);
104+
return ToCsharp<float>(py);
105+
}
106+
107+
}
108+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 static partial class np
16+
{
17+
/// <summary>
18+
/// Gives a new shape to an array without changing its data.
19+
///
20+
/// Notes
21+
///
22+
/// It is not always possible to change the shape of an array without
23+
/// copying the data. If you want an error to be raised when the data is copied,
24+
/// you should assign the new shape to the shape attribute of the array:
25+
///
26+
/// The order keyword gives the index ordering both for fetching the values
27+
/// from a, and then placing the values into the output array.
28+
/// For example, let’s say you have an array:
29+
///
30+
/// You can think of reshaping as first raveling the array (using the given
31+
/// index order), then inserting the elements from the raveled array into the
32+
/// new array using the same kind of index ordering as was used for the
33+
/// raveling.
34+
/// </summary>
35+
/// <param name="a">The array to reshape</param>
36+
/// <param name="newshape">
37+
/// The new shape should be compatible with the original shape. If
38+
/// an integer, then the result will be a 1-D array of that length.
39+
/// One shape dimension can be -1. In this case, the value is
40+
/// inferred from the length of the array and remaining dimensions.
41+
/// </param>
42+
/// <returns>
43+
/// This will be a new view object if possible; otherwise, it will
44+
/// be a copy. Note there is no guarantee of the memory layout (C- or
45+
/// Fortran- contiguous) of the returned array.
46+
/// </returns>
47+
public static NDarray reshape(NDarray a, params int[] newshape)
48+
{
49+
return NumPy.Instance.reshape(a, new Shape(newshape));
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)