Skip to content

Commit b65fbdd

Browse files
committed
added np.meshgrid
1 parent c827514 commit b65fbdd

8 files changed

Lines changed: 193 additions & 764 deletions

File tree

‎src/CodeMinion.ApiGenerator/NumPy/ApiGenerator.cs‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,12 @@ private void PostProcess(Function decl)
672672
if (decl.Returns.Count==0)
673673
decl.Returns.Add(new Argument(){Type = "NDarray"});
674674
break;
675-
case "meshgrid":
676675
case "mat":
677676
case "bmat":
678677
case "block":
679678
case "interp":
680679
case "einsum_path":
681680
case "cond":
682-
case "ogrid":
683681
case "get_state":
684682
case "set_state":
685683
case "genfromtxt":
@@ -840,6 +838,20 @@ private void PostProcess(Function decl)
840838
case "column_stack":
841839
decl.ManualOverride = true;
842840
break;
841+
case "meshgrid":
842+
decl.ReturnType = "NDarray[]";
843+
decl.Arguments[0].Type = "NDarray[]";
844+
decl.Arguments[0].Name = "xi";
845+
decl.Arguments.RemoveAt(1);
846+
decl.Arguments[1].DefaultValue = "\"xy\"";
847+
break;
848+
case "mgrid":
849+
decl.ReturnType = "NDarray[]";
850+
break;
851+
case "ogrid":
852+
decl.ReturnType = "NDarray[]";
853+
decl.Arguments.Clear();
854+
break;
843855
}
844856
}
845857

@@ -874,7 +886,7 @@ private IEnumerable<Function> InferOverloads(Function decl)
874886
switch (decl.Name)
875887
{
876888
case "norm":
877-
case "asscalar":
889+
case "asscalar":
878890
case "normal":
879891
yield break;
880892
case "all":
@@ -1096,6 +1108,16 @@ private IEnumerable<Function> InferOverloads(Function decl)
10961108
else
10971109
yield return decl;
10981110
yield break;
1111+
case "meshgrid":
1112+
yield return decl;
1113+
yield return decl.Clone(f =>
1114+
{
1115+
f.Arguments[0].Type = "params NDarray[]";
1116+
f.Arguments.RemoveAt(1);
1117+
f.Arguments.RemoveAt(1);
1118+
f.Arguments.RemoveAt(1);
1119+
});
1120+
yield break;
10991121
}
11001122

11011123
// without args we don't need to consider possible overloads

‎src/Numpy.Bare.Dotnet/Numpy.Bare.Dotnet.csproj‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<Compile Include="..\Numpy\Manual\np.linspace.cs">
7676
<Link>Manual\np.linspace.cs</Link>
7777
</Compile>
78+
<Compile Include="..\Numpy\Manual\np.meshgrid.cs">
79+
<Link>Manual\np.meshgrid.cs</Link>
80+
</Compile>
7881
<Compile Include="..\Numpy\Manual\np.random.cs">
7982
<Link>Manual\np.random.cs</Link>
8083
</Compile>

‎src/Numpy.Bare/Numpy.Bare.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<RepositoryUrl>https://github.com/SciSharp/Numpy.NET</RepositoryUrl>
1515
<PackageTags>Data science, Machine Learning, AI, Scientific Computing, NumPy, Linear Algebra, FFT, SVD, Matrix, Python</PackageTags>
1616
<PackageLicenseUrl>https://github.com/SciSharp/Numpy.NET/blob/master/LICENSE</PackageLicenseUrl>
17-
<Version>3.7.1.19</Version>
17+
<Version>3.7.1.20</Version>
1818
<PackageIconUrl>https://github.com/SciSharp/Numpy.NET/blob/master/doc/img/numpy.net.icon128.png?raw=true</PackageIconUrl>
1919
<AssemblyVersion>3.7.1.4</AssemblyVersion>
2020
</PropertyGroup>
@@ -39,6 +39,7 @@
3939
<Compile Include="..\Numpy\Manual\np.delete.cs" Link="Manual\np.delete.cs"></Compile>
4040
<Compile Include="..\Numpy\Manual\np.linalg.norm.cs" Link="Manual\np.linalg.norm.cs"></Compile>
4141
<Compile Include="..\Numpy\Manual\np.linspace.cs" Link="Manual\np.linspace.cs"></Compile>
42+
<Compile Include="..\Numpy\Manual\np.meshgrid.cs" Link="Manual\np.meshgrid.cs"></Compile>
4243
<Compile Include="..\Numpy\Manual\np.random.cs" Link="Manual\np.random.cs"></Compile>
4344
<Compile Include="..\Numpy\Manual\np.resize.cs" Link="Manual\np.resize.cs"></Compile>
4445
<Compile Include="..\Numpy\Models\Axis.cs" Link="Models\Axis.cs"></Compile>

‎src/Numpy/Manual/np.meshgrid.cs‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
18+
/// <summary>
19+
/// Return coordinate matrices from coordinate vectors.<br></br>
20+
///
21+
/// Make N-D coordinate arrays for vectorized evaluations of
22+
/// N-D scalar/vector fields over N-D grids, given
23+
/// one-dimensional coordinate arrays x1, x2,…, xn.<br></br>
24+
///
25+
/// Notes
26+
///
27+
/// This function supports both indexing conventions through the indexing
28+
/// keyword argument.<br></br>
29+
/// Giving the string ‘ij’ returns a meshgrid with
30+
/// matrix indexing, while ‘xy’ returns a meshgrid with Cartesian indexing.<br></br>
31+
///
32+
/// In the 2-D case with inputs of length M and N, the outputs are of shape
33+
/// (N, M) for ‘xy’ indexing and (M, N) for ‘ij’ indexing.<br></br>
34+
/// In the 3-D case
35+
/// with inputs of length M, N and P, outputs are of shape (N, M, P) for
36+
/// ‘xy’ indexing and (M, N, P) for ‘ij’ indexing.<br></br>
37+
/// The difference is
38+
/// illustrated by the following code snippet:
39+
///
40+
/// In the 1-D and 0-D case, the indexing and sparse keywords have no effect.
41+
/// </summary>
42+
/// <param name="xi">
43+
/// 1-D arrays representing the coordinates of a grid.
44+
/// </param>
45+
/// <param name="indexing">
46+
/// Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output.<br></br>
47+
///
48+
/// See Notes for more details.
49+
/// </param>
50+
/// <param name="sparse">
51+
/// If True a sparse grid is returned in order to conserve memory.<br></br>
52+
///
53+
/// Default is False.
54+
/// </param>
55+
/// <param name="copy">
56+
/// If False, a view into the original arrays are returned in order to
57+
/// conserve memory.<br></br>
58+
/// Default is True.<br></br>
59+
/// Please note that
60+
/// sparse=False, copy=False will likely return non-contiguous
61+
/// arrays.<br></br>
62+
/// Furthermore, more than one element of a broadcast array
63+
/// may refer to a single memory location.<br></br>
64+
/// If you need to write to the
65+
/// arrays, make copies first.
66+
/// </param>
67+
/// <returns>
68+
/// For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) ,
69+
/// return (N1, N2, N3,...Nn) shaped arrays if indexing=’ij’
70+
/// or (N2, N1, N3,...Nn) shaped arrays if indexing=’xy’
71+
/// with the elements of xi repeated to fill the matrix along
72+
/// the first dimension for x1, the second for x2 and so on.
73+
/// </returns>
74+
public static NDarray[] meshgrid(NDarray[] xi, string indexing = "xy", bool? sparse = null, bool? copy = null)
75+
{
76+
var __self__ = self;
77+
var pyargs = ToTuple(xi);
78+
var kwargs = new PyDict();
79+
if (indexing != "xy") kwargs["indexing"] = ToPython(indexing);
80+
if (sparse != null) kwargs["sparse"] = ToPython(sparse);
81+
if (copy != null) kwargs["copy"] = ToPython(copy);
82+
dynamic py = __self__.InvokeMethod("meshgrid", pyargs, kwargs);
83+
return ToCsharp<NDarray[]>(py);
84+
}
85+
86+
/// <summary>
87+
/// Return coordinate matrices from coordinate vectors.<br></br>
88+
///
89+
/// Make N-D coordinate arrays for vectorized evaluations of
90+
/// N-D scalar/vector fields over N-D grids, given
91+
/// one-dimensional coordinate arrays x1, x2,…, xn.<br></br>
92+
///
93+
/// Notes
94+
///
95+
/// This function supports both indexing conventions through the indexing
96+
/// keyword argument.<br></br>
97+
/// Giving the string ‘ij’ returns a meshgrid with
98+
/// matrix indexing, while ‘xy’ returns a meshgrid with Cartesian indexing.<br></br>
99+
///
100+
/// In the 2-D case with inputs of length M and N, the outputs are of shape
101+
/// (N, M) for ‘xy’ indexing and (M, N) for ‘ij’ indexing.<br></br>
102+
/// In the 3-D case
103+
/// with inputs of length M, N and P, outputs are of shape (N, M, P) for
104+
/// ‘xy’ indexing and (M, N, P) for ‘ij’ indexing.<br></br>
105+
/// The difference is
106+
/// illustrated by the following code snippet:
107+
///
108+
/// In the 1-D and 0-D case, the indexing and sparse keywords have no effect.
109+
/// </summary>
110+
/// <param name="xi">
111+
/// 1-D arrays representing the coordinates of a grid.
112+
/// </param>
113+
/// <returns>
114+
/// For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) ,
115+
/// return (N1, N2, N3,...Nn) shaped arrays if indexing=’ij’
116+
/// or (N2, N1, N3,...Nn) shaped arrays if indexing=’xy’
117+
/// with the elements of xi repeated to fill the matrix along
118+
/// the first dimension for x1, the second for x2 and so on.
119+
/// </returns>
120+
public static NDarray[] meshgrid(params NDarray[] xi)
121+
{
122+
var __self__ = self;
123+
var pyargs = ToTuple(xi);
124+
var kwargs = new PyDict();
125+
dynamic py = __self__.InvokeMethod("meshgrid", pyargs, kwargs);
126+
return ToCsharp<NDarray[]>(py);
127+
}
128+
129+
}
130+
}

‎src/Numpy/Models/NDarray.CastOperators.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ public static implicit operator NDarray(Array array)
2020
case long[] a: return np.array(a);
2121
case float[] a: return np.array(a);
2222
case double[] a: return np.array(a);
23+
case string[] a: return np.array(a);
2324
case byte[,] a: return np.array(a);
2425
case bool[,] a: return np.array(a);
2526
case short[,] a: return np.array(a);
2627
case int[,] a: return np.array(a);
2728
case long[,] a: return np.array(a);
2829
case float[,] a: return np.array(a);
2930
case double[,] a: return np.array(a);
31+
case string[,] a: return np.array(a);
3032
case byte[,,] a: return np.array(a);
3133
case bool[,,] a: return np.array(a);
3234
case short[,,] a: return np.array(a);
3335
case int[,,] a: return np.array(a);
3436
case long[,,] a: return np.array(a);
3537
case float[,,] a: return np.array(a);
3638
case double[,,] a: return np.array(a);
39+
case string[,,] a: return np.array(a);
3740
}
3841
throw new InvalidOperationException($"Unable to cast {array.GetType()} to NDarray");
3942
}

‎src/Numpy/Numpy.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<RepositoryUrl>https://github.com/SciSharp/Numpy.NET</RepositoryUrl>
1515
<PackageTags>Data science, Machine Learning, ML, AI, Scientific Computing, NumPy, Linear Algebra, FFT, SVD, Matrix, Python</PackageTags>
1616
<PackageLicenseUrl>https://github.com/SciSharp/Numpy.NET/blob/master/LICENSE</PackageLicenseUrl>
17-
<Version>3.7.1.19</Version>
17+
<Version>3.7.1.20</Version>
1818
<PackageIconUrl>https://github.com/SciSharp/Numpy.NET/blob/master/doc/img/numpy.net.icon128.png?raw=true</PackageIconUrl>
1919
</PropertyGroup>
2020

0 commit comments

Comments
 (0)