Skip to content

Commit

Permalink
Bitwise (#628)
Browse files Browse the repository at this point in the history
* add bitwise operators

* add build to requirements

* [EDIT] - Tweaked test data and saved test results.

* Tweaked test values for and / or

* [EDIT] - Setting print options to be verbose for test comparisons.

* [EDIT] - Removed call to set_printoptions and added output from ulab instead of numpy. Of note - there seems to be a discrepancy between the numpy and ulab output for one of the left_shift cases.

* [EDIT] - Added newline at end of file for diffing purposes.

* [EDIT] - Added print options back in as output seemed truncated.

---------

Co-authored-by: Zoltán Vörös <zvoros@gmail.com>
Co-authored-by: JamieDouugh <jamie.davies@douugh.com>
  • Loading branch information
3 people committed Jun 21, 2023
1 parent ef248b6 commit 2cde128
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 80 deletions.
6 changes: 6 additions & 0 deletions docs/ulab-change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ version 6.1.0

add sinc function

Fri, 12 May 2023

version 6.0.13

add bitwise operators

Sun, 7 May 2023

version 6.0.12
Expand Down
8 changes: 5 additions & 3 deletions tests/2d/numpy/bitwise_and.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@


dtypes = (np.uint8, np.int8, np.uint16, np.int16)
test_values1 = (0, 1, 0, 1, 2, 5)
test_values2 = (0, 1, 1, 0, 2, 7)


for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
x1 = np.array(test_values1, dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

x2 = np.array(test_values2, dtype=dtype2)
print(np.bitwise_and(x1, x2))
32 changes: 16 additions & 16 deletions tests/2d/numpy/bitwise_and.py.exp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
array([0, 0, 2, 2, 0], dtype=uint8)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int8)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=uint8)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=uint16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=int8)
array([0, 1, 0, 0, 2, 5], dtype=uint16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=uint16)
array([0, 1, 0, 0, 2, 5], dtype=uint16)
array([0, 1, 0, 0, 2, 5], dtype=uint16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
array([0, 1, 0, 0, 2, 5], dtype=int16)
8 changes: 5 additions & 3 deletions tests/2d/numpy/bitwise_or.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@


dtypes = (np.uint8, np.int8, np.uint16, np.int16)
test_values1 = (0, 1, 0, 1, 2, 5)
test_values2 = (0, 1, 1, 0, 2, 7)


for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
x1 = np.array(test_values1, dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

x2 = np.array(test_values2, dtype=dtype2)
print(np.bitwise_or(x1, x2))
32 changes: 16 additions & 16 deletions tests/2d/numpy/bitwise_or.py.exp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
array([5, 5, 3, 3, 5], dtype=uint8)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int8)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=uint8)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=uint16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=int8)
array([0, 1, 1, 1, 2, 7], dtype=uint16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=uint16)
array([0, 1, 1, 1, 2, 7], dtype=uint16)
array([0, 1, 1, 1, 2, 7], dtype=uint16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
array([0, 1, 1, 1, 2, 7], dtype=int16)
17 changes: 12 additions & 5 deletions tests/2d/numpy/left_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
from ulab import numpy as np
except:
import numpy as np
np.set_printoptions(threshold=100)


shift_values = (
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
)
dtypes = (np.uint8, np.int8, np.uint16, np.int16)

for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

print(np.left_shift(x1, x2))
for shift_value in shift_values:
for dtype1 in dtypes:
x1 = np.array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(shift_value, dtype=dtype2)
print(np.left_shift(x1, x2))
64 changes: 48 additions & 16 deletions tests/2d/numpy/left_shift.py.exp
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
array([0, 16, 16, 12, 8], dtype=uint8)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int8)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int8)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint8)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint8)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int8)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int8)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=uint16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 2, 4, 8, 16, 32, 64, 6, 10, 14, 22, 26], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint8)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, -128, 12, 20, 28, 44, 52], dtype=int8)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16)
array([0, 4, 8, 16, 32, 64, -128, 12, 20, 28, 44, 52], dtype=int8)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=uint16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
array([0, 4, 8, 16, 32, 64, 128, 12, 20, 28, 44, 52], dtype=int16)
17 changes: 12 additions & 5 deletions tests/2d/numpy/right_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
from ulab import numpy as np
except:
import numpy as np
np.set_printoptions(threshold=100)


shift_values = (
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
)
dtypes = (np.uint8, np.int8, np.uint16, np.int16)

for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

print(np.right_shift(x1, x2))
for shift_value in shift_values:
for dtype1 in dtypes:
x1 = np.array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(shift_value, dtype=dtype2)
print(np.right_shift(x1, x2))
64 changes: 48 additions & 16 deletions tests/2d/numpy/right_shift.py.exp
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
array([0, 0, 0, 0, 2], dtype=uint8)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int8)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int8)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint8)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int8)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=uint16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 1, 2, 4, 8, 16, 32, 3, 5, 7, 11, 13], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint8)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int8)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int8)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=uint16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 1, 2, 4, 8, 16, 1, 2, 3, 5, 6], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint8)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int8)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int8)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=uint16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)
array([0, 0, 0, 1, 2, 4, 8, 0, 1, 1, 2, 3], dtype=int16)

0 comments on commit 2cde128

Please sign in to comment.