Skip to content

Commit 2751553

Browse files
committed
Fix boolean operators & | and ^ between NDarrays (#129)
1 parent c6391ca commit 2751553

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Numpy/Models/NDarray.Operators.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ public NDarray pow(ValueType obj)
289289
return new NDarray(a.self.InvokeMethod("__and__", obj.ToPython()));
290290
}
291291

292+
/// <summary>
293+
/// Return self&value.
294+
/// </summary>
295+
public static NDarray operator &(NDarray a, NDarray b)
296+
{
297+
return new NDarray(a.self.InvokeMethod("__and__", b.self));
298+
}
299+
292300
/// <summary>
293301
/// Return self|value.
294302
/// </summary>
@@ -297,6 +305,14 @@ public NDarray pow(ValueType obj)
297305
return new NDarray(a.self.InvokeMethod("__or__", obj.ToPython()));
298306
}
299307

308+
/// <summary>
309+
/// Return self|value.
310+
/// </summary>
311+
public static NDarray operator |(NDarray a, NDarray b)
312+
{
313+
return new NDarray(a.self.InvokeMethod("__or__", b.self));
314+
}
315+
300316
/// <summary>
301317
/// Return self^value.
302318
/// </summary>
@@ -305,6 +321,14 @@ public NDarray pow(ValueType obj)
305321
return new NDarray(a.self.InvokeMethod("__xor__", obj.ToPython()));
306322
}
307323

324+
/// <summary>
325+
/// Return self^value.
326+
/// </summary>
327+
public static NDarray operator ^(NDarray a, NDarray b)
328+
{
329+
return new NDarray(a.self.InvokeMethod("__xor__", b.self));
330+
}
331+
308332
//------------------------------
309333
// Arithmetic, in-place:
310334
//------------------------------

test/Numpy.UnitTest/NumpyTest.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,13 +1103,30 @@ public void IssueByPandath()
11031103
//>>> ys
11041104
//array([3, 4, 3, 4], dtype = int64)
11051105

1106-
var grid = np.array(new[,] {{ 1, 2, 3, 0, 0, 4 }, { 5, 6, 7, 0, 0, 8 } });
1106+
var grid = np.array(new[,] { { 1, 2, 3, 0, 0, 4 }, { 5, 6, 7, 0, 0, 8 } });
11071107
var result = np.where(grid.equals(0));
11081108
Console.WriteLine(result[0].repr);
11091109
Console.WriteLine(result[1].repr);
11101110
Assert.AreEqual("array([0, 0, 1, 1], dtype=int64)", result[0].repr);
11111111
Assert.AreEqual("array([3, 4, 3, 4], dtype=int64)", result[1].repr);
11121112
}
1113+
1114+
[TestMethod]
1115+
public void IssueByXiaozhu1988()
1116+
{
1117+
//>>> data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120])
1118+
//>>> (data >= 50) & (data < 100)
1119+
//array([False, False, False, False, True, True, True, True, True,
1120+
// False, False, False])
1121+
1122+
var data = np.array(new[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 });
1123+
Console.WriteLine(((data >= 50) & (data < 100)).repr);
1124+
Assert.AreEqual("array([False, False, False, False, True, True, True, True, True,\n False, False, False])", ((data >= 50) & (data < 100)).repr);
1125+
Console.WriteLine(((data >= 50) | (data < 100)).repr);
1126+
Assert.AreEqual("array([ True, True, True, True, True, True, True, True, True,\n True, True, True])", ((data >= 50) | (data < 100)).repr);
1127+
Console.WriteLine(((data >= 50) ^ (data < 100)).repr);
1128+
Assert.AreEqual("array([ True, True, True, True, False, False, False, False, False,\n True, True, True])", ((data >= 50) ^ (data < 100)).repr);
1129+
}
11131130
}
11141131

11151132

0 commit comments

Comments
 (0)