Skip to content

Commit 149bc95

Browse files
committed
implemented 1)np.meshgrid 2) np.broadcast_array 3) _broadcast_to 4) _broadcast_shape
1 parent b70b4d9 commit 149bc95

5 files changed

Lines changed: 205 additions & 5 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NumSharp.Core.Casting
6+
{
7+
public class Broadcast
8+
{
9+
public Shape shape { get; set; }
10+
public int nd { get; set; }
11+
public int ndim { get; set; }
12+
public int size { get; set; }
13+
// public int index { get; set; }
14+
// public int numiters { get; set; }
15+
16+
public Broadcast(int nd, int ndim, Shape shape, int size)
17+
{
18+
this.nd = nd;
19+
this.ndim = ndim;
20+
this.size = size;
21+
this.shape = shape;
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NumSharp.Core.Casting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core
7+
{
8+
public static partial class np
9+
{/// <summary>
10+
///
11+
/// </summary>
12+
/// <param name="nd1"> 1-D arrays representing the coordinates of a grid </param>
13+
/// <param name="nd2"> 1-D arrays representing the coordinates of a grid</param>
14+
/// <returns></returns>
15+
public static Broadcast broadcast(NDArray nd1, NDArray nd2)
16+
{
17+
int nd1maxAxis = nd1.shape[0] > nd1.shape[1] ? nd1.shape[0] : nd1.shape[1];
18+
int nd2maxAxis = nd2.shape[0] > nd2.shape[1] ? nd2.shape[0] : nd2.shape[1];
19+
Shape shape = new Shape(nd1maxAxis, nd2maxAxis);
20+
int nd = 2;
21+
int ndim = 2;
22+
int size = nd1maxAxis * nd2maxAxis;
23+
24+
return new Broadcast(nd, ndim, shape, size);
25+
}
26+
}
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using NumSharp.Core.Casting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core
7+
{
8+
public static partial class np
9+
{
10+
public static NDArray[] broadcast_arrays(NDArray nd1, NDArray nd2, bool subok = false)
11+
{
12+
NDArray args1 = np.array(nd1);
13+
NDArray args2 = np.array(nd2);
14+
var shape = _broadcast_shape(args1, args2);
15+
16+
if (nd1.shape == shape && nd2.shape == shape)
17+
{
18+
return new NDArray[] { nd1, nd2 };
19+
}
20+
21+
return new NDArray[] { _broadcast_to(nd1, shape, subok, false) };
22+
}
23+
24+
private static Shape _broadcast_shape(NDArray nd1, NDArray nd2)
25+
{
26+
Broadcast b = np.broadcast(nd1, nd2);
27+
return b.shape;
28+
}
29+
30+
private static NDArray _broadcast_to(NDArray nd, Shape shape, bool subok, bool rdonly)
31+
{
32+
double[,] table = new double[shape.Dimensions[0], shape.Dimensions[1]];
33+
if (nd.shape[0] == 1)
34+
{// (1,2,3)
35+
for (int i = 0; i < shape.Dimensions[0]; i++)
36+
{
37+
for (int j = 0; j < shape.Dimensions[1]; j++)
38+
{
39+
table[i, j] = (double)nd.Storage.GetData(0, j);
40+
}
41+
}
42+
}
43+
else if (nd.shape[1] == 1)
44+
{
45+
for (int i = 0; i < shape.Dimensions[0]; i++)
46+
{
47+
for (int j = 0; j < shape.Dimensions[1]; j++)
48+
{
49+
table[i, j] = (double)nd.Storage.GetData(i, 0);
50+
}
51+
}
52+
}
53+
return new NDArray(table);
54+
}
55+
}
56+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NumSharp.Core
6+
{
7+
public static partial class np
8+
{
9+
public static NDArray asanyarray(string data)
10+
{
11+
var nd = new NDArray(typeof(string), new int[0]);
12+
nd.Storage.SetData(new string[] { data });
13+
return nd;
14+
}
15+
16+
public static NDArray asanyarray<T>(T data) where T : struct
17+
{
18+
var nd = new NDArray(typeof(T), new int[0]);
19+
nd.Storage.SetData(new T[] { data });
20+
return nd;
21+
}
22+
23+
public static NDArray asanyarray(string[] data, int ndim = 1)
24+
{
25+
var nd = new NDArray(typeof(string), data.Length);
26+
nd.Storage.SetData(data);
27+
return nd;
28+
}
29+
30+
public static NDArray asanyarray<T>(T[] data, int ndim = 1) where T : struct
31+
{
32+
var nd = new NDArray(typeof(T), data.Length);
33+
nd.Storage.SetData(data);
34+
return nd;
35+
}
36+
}
37+
}

src/NumSharp.Core/Creation/np.meshgrid.cs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5-
namespace NumSharp.Core.Creation
5+
namespace NumSharp.Core
66
{
77
static partial class np
88
{
@@ -14,12 +14,68 @@ static partial class np
1414
/// .. versionchanged:: 1.9
1515
/// 1-D and 0-D cases are allowed.
1616
/// </summary>
17-
/// <param name="x1"></param>
18-
/// <param name="x2"></param>
17+
/// <param name="x1"> 1-D arrays representing the coordinates of a grid</param>
18+
/// /// <param name="x2"> 1-D arrays representing the coordinates of a grid</param>
1919
/// <returns></returns>
20-
public static NDArray meshgrid(NDArray x1, NDArray x2)
20+
public static (NDArray, NDArray) meshgrid(NDArray x1, NDArray x2, Kwargs kwargs =null)
2121
{
22-
throw new NotFiniteNumberException();
22+
if (kwargs == null)
23+
{
24+
kwargs = new Kwargs();
25+
}
26+
int ndim = 2;
27+
var s0 = (1,1);
28+
var output = new NDArray[]{ np.asanyarray(x1).reshape(-1,1), np.asanyarray(x2).reshape(1,-1)};
29+
30+
if (kwargs.indexing == "xy" && ndim > 1)
31+
{
32+
// Switch first and second axis
33+
output[0].reshape(1,-1);
34+
output[1].reshape(-1, 1);
35+
}
36+
37+
if (!kwargs.sparse)
38+
{
39+
// Return the full N-D matrix(not only the 1 - D vector)
40+
output = np.broadcast_arrays(output[0], output[1], true);
41+
}
42+
43+
if (kwargs.copy)
44+
{
45+
46+
}
47+
48+
return (output[0], output[1]);
49+
}
50+
51+
}
52+
53+
public class Kwargs
54+
{
55+
public string indexing { get; set; }
56+
public bool sparse { get; set; }
57+
public bool copy { get; set; }
58+
/// <summary>
59+
/// Kwargs constructor
60+
/// </summary>
61+
/// <param name="indexing"> {'xy', 'ij'}, optional Cartesian('xy', default) or matrix('ij') indexing of output.</param>
62+
/// <param name="sparse">If True a sparse grid is returned in order to conserve memory. Default is False.</param>
63+
/// <param name="copy">If False, a view into the original arrays are returned in order to conserve memory.
64+
/// Default is True.Please note that sparse= False, copy= False`` will likely return non-contiguous arrays.
65+
/// Furthermore, more than one element of a broadcast array may refer to a single memory location.
66+
/// If you need to write to the arrays, make copies first.</param>
67+
public Kwargs(string indexing, bool sparse, bool copy)
68+
{
69+
this.indexing = indexing;
70+
this.sparse = sparse;
71+
this.copy = copy;
72+
}
73+
74+
public Kwargs()
75+
{
76+
this.indexing = "xy";
77+
this.sparse = false;
78+
this.copy = true;
2379
}
2480
}
2581
}

0 commit comments

Comments
 (0)