Compare commits

...

3 commits

Author SHA1 Message Date
Zoltán Vörös
fe177f3df4
Merge branch 'master' into logical 2023-07-20 21:30:49 +02:00
Zoltán Vörös
9ba0350403 fix Boolean case for logic operators 2023-02-13 22:34:13 +01:00
Zoltán Vörös
6779a6a9ec add logical operators 2023-02-10 18:50:14 +01:00
13 changed files with 351 additions and 11 deletions

View file

@ -6,8 +6,8 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2019-2022 Zoltán Vörös
* 2020 Jeff Epler for Adafruit Industries
* Copyright (c) 2019-2023 Zoltán Vörös
* 2020-2023 Jeff Epler for Adafruit Industries
* 2020 Taku Fukada
*/
@ -964,6 +964,11 @@ ndarray_obj_t *ndarray_from_iterable(mp_obj_t obj, uint8_t dtype) {
shape[ULAB_MAX_DIMS - i - 1] = shape[ndim - 1 - i];
}
// reset the shapes on the left hand side
for(uint8_t i = 0; i < ULAB_MAX_DIMS - 1 - ndim; i++) {
shape[i] = 0;
}
ndarray_obj_t *ndarray = ndarray_new_dense_ndarray(ndim, shape, dtype);
item = obj;
for(uint8_t i = 0; i < ndim - 1; i++) {
@ -1762,7 +1767,6 @@ ndarray_obj_t *ndarray_from_mp_obj(mp_obj_t obj, uint8_t other_type) {
#if NDARRAY_HAS_BINARY_OPS || NDARRAY_HAS_INPLACE_OPS
mp_obj_t ndarray_binary_op(mp_binary_op_t _op, mp_obj_t lobj, mp_obj_t robj) {
// TODO: implement in-place operators
// if the ndarray stands on the right hand side of the expression, simply swap the operands
ndarray_obj_t *lhs, *rhs;
mp_binary_op_t op = _op;
@ -1949,6 +1953,12 @@ mp_obj_t ndarray_binary_op(mp_binary_op_t _op, mp_obj_t lobj, mp_obj_t robj) {
return ndarray_binary_power(lhs, rhs, ndim, shape, lstrides, rstrides);
break;
#endif
#if NDARRAY_HAS_BINARY_OP_OR | NDARRAY_HAS_BINARY_OP_XOR | NDARRAY_HAS_BINARY_OP_AND
case MP_BINARY_OP_OR:
case MP_BINARY_OP_XOR:
case MP_BINARY_OP_AND:
return ndarray_binary_logical(lhs, rhs, ndim, shape, lstrides, rstrides, op);
#if NDARRAY_HAS_BINARY_OP_FLOOR_DIVIDE
case MP_BINARY_OP_FLOOR_DIVIDE:
COMPLEX_DTYPE_NOT_IMPLEMENTED(lhs->dtype);

View file

@ -181,8 +181,8 @@ mp_obj_t ndarray_binary_add(ndarray_obj_t *lhs, ndarray_obj_t *rhs,
if(lhs->dtype == NDARRAY_UINT8) {
if(rhs->dtype == NDARRAY_UINT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint8_t, uint8_t, larray, lstrides, rarray, rstrides, +);
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT8);
BINARY_LOOP(results, uint8_t, uint8_t, uint8_t, larray, lstrides, rarray, rstrides, +);
} else if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int8_t, larray, lstrides, rarray, rstrides, +);
@ -264,8 +264,8 @@ mp_obj_t ndarray_binary_multiply(ndarray_obj_t *lhs, ndarray_obj_t *rhs,
if(lhs->dtype == NDARRAY_UINT8) {
if(rhs->dtype == NDARRAY_UINT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint8_t, uint8_t, larray, lstrides, rarray, rstrides, *);
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT8);
BINARY_LOOP(results, uint8_t, uint8_t, uint8_t, larray, lstrides, rarray, rstrides, *);
} else if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int8_t, larray, lstrides, rarray, rstrides, *);
@ -857,6 +857,194 @@ mp_obj_t ndarray_binary_power(ndarray_obj_t *lhs, ndarray_obj_t *rhs,
}
#endif /* NDARRAY_HAS_BINARY_OP_POWER */
#if NDARRAY_HAS_BINARY_OP_OR | NDARRAY_HAS_BINARY_OP_XOR | NDARRAY_HAS_BINARY_OP_AND
mp_obj_t ndarray_binary_logical(ndarray_obj_t *lhs, ndarray_obj_t *rhs,
uint8_t ndim, size_t *shape, int32_t *lstrides, int32_t *rstrides, mp_binary_op_t op) {
#if ULAB_SUPPORTS_COMPLEX
if((lhs->dtype == NDARRAY_COMPLEX) || (rhs->dtype == NDARRAY_COMPLEX) || (lhs->dtype == NDARRAY_FLOAT) || (rhs->dtype == NDARRAY_FLOAT)) {
mp_raise_TypeError(translate("operation not supported for the input types"));
}
#else
if((lhs->dtype == NDARRAY_FLOAT) || (rhs->dtype == NDARRAY_FLOAT)) {
mp_raise_TypeError(translate("operation not supported for the input types"));
}
#endif
// bail out, if both inputs are of 16-bit types, but differ in sign;
// numpy promotes the result to int32
if(((lhs->dtype == NDARRAY_INT16) && (rhs->dtype == NDARRAY_UINT16)) ||
((lhs->dtype == NDARRAY_UINT16) && (rhs->dtype == NDARRAY_INT16))) {
mp_raise_TypeError(translate("dtype of int32 is not supported"));
}
ndarray_obj_t *results = NULL;
uint8_t *larray = (uint8_t *)lhs->array;
uint8_t *rarray = (uint8_t *)rhs->array;
switch(op) {
case MP_BINARY_OP_XOR:
if(lhs->dtype == NDARRAY_UINT8) {
if(rhs->dtype == NDARRAY_UINT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT8);
if(lhs->boolean & rhs->boolean) {
results->boolean = 1;
}
BINARY_LOOP(results, uint8_t, uint8_t, uint8_t, larray, lstrides, rarray, rstrides, ^);
} else if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int8_t, larray, lstrides, rarray, rstrides, ^);
} else if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint8_t, uint16_t, larray, lstrides, rarray, rstrides, ^);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int16_t, larray, lstrides, rarray, rstrides, ^);
}
} else if(lhs->dtype == NDARRAY_INT8) {
if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT8);
BINARY_LOOP(results, int8_t, int8_t, int8_t, larray, lstrides, rarray, rstrides, ^);
} else if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int8_t, uint16_t, larray, lstrides, rarray, rstrides, ^);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int8_t, int16_t, larray, lstrides, rarray, rstrides, ^);
} else {
return ndarray_binary_op(MP_BINARY_OP_XOR, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
} else if(lhs->dtype == NDARRAY_UINT16) {
if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint16_t, uint16_t, larray, lstrides, rarray, rstrides, ^);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_FLOAT);
BINARY_LOOP(results, mp_float_t, uint16_t, int16_t, larray, lstrides, rarray, rstrides, ^);
} else {
return ndarray_binary_op(MP_BINARY_OP_XOR, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
} else if(lhs->dtype == NDARRAY_INT16) {
if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int16_t, int16_t, larray, lstrides, rarray, rstrides, ^);
} else {
return ndarray_binary_op(MP_BINARY_OP_XOR, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
}
break;
case MP_BINARY_OP_OR:
if(lhs->dtype == NDARRAY_UINT8) {
if(rhs->dtype == NDARRAY_UINT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT8);
if(lhs->boolean & rhs->boolean) {
results->boolean = 1;
}
BINARY_LOOP(results, uint8_t, uint8_t, uint8_t, larray, lstrides, rarray, rstrides, |);
} else if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int8_t, larray, lstrides, rarray, rstrides, |);
} else if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint8_t, uint16_t, larray, lstrides, rarray, rstrides, |);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int16_t, larray, lstrides, rarray, rstrides, |);
}
} else if(lhs->dtype == NDARRAY_INT8) {
if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT8);
BINARY_LOOP(results, int8_t, int8_t, int8_t, larray, lstrides, rarray, rstrides, |);
} else if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int8_t, uint16_t, larray, lstrides, rarray, rstrides, |);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int8_t, int16_t, larray, lstrides, rarray, rstrides, |);
} else {
return ndarray_binary_op(MP_BINARY_OP_OR, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
} else if(lhs->dtype == NDARRAY_UINT16) {
if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint16_t, uint16_t, larray, lstrides, rarray, rstrides, |);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_FLOAT);
BINARY_LOOP(results, mp_float_t, uint16_t, int16_t, larray, lstrides, rarray, rstrides, |);
} else {
return ndarray_binary_op(MP_BINARY_OP_OR, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
} else if(lhs->dtype == NDARRAY_INT16) {
if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int16_t, int16_t, larray, lstrides, rarray, rstrides, |);
} else {
return ndarray_binary_op(MP_BINARY_OP_OR, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
}
break;
case MP_BINARY_OP_AND:
if(lhs->dtype == NDARRAY_UINT8) {
if(rhs->dtype == NDARRAY_UINT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT8);
if(lhs->boolean & rhs->boolean) {
results->boolean = 1;
}
BINARY_LOOP(results, uint8_t, uint8_t, uint8_t, larray, lstrides, rarray, rstrides, &);
} else if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int8_t, larray, lstrides, rarray, rstrides, &);
} else if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint8_t, uint16_t, larray, lstrides, rarray, rstrides, &);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, uint8_t, int16_t, larray, lstrides, rarray, rstrides, &);
}
} else if(lhs->dtype == NDARRAY_INT8) {
if(rhs->dtype == NDARRAY_INT8) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT8);
BINARY_LOOP(results, int8_t, int8_t, int8_t, larray, lstrides, rarray, rstrides, &);
} else if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int8_t, uint16_t, larray, lstrides, rarray, rstrides, &);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int8_t, int16_t, larray, lstrides, rarray, rstrides, &);
} else {
return ndarray_binary_op(MP_BINARY_OP_AND, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
} else if(lhs->dtype == NDARRAY_UINT16) {
if(rhs->dtype == NDARRAY_UINT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_UINT16);
BINARY_LOOP(results, uint16_t, uint16_t, uint16_t, larray, lstrides, rarray, rstrides, &);
} else if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_FLOAT);
BINARY_LOOP(results, mp_float_t, uint16_t, int16_t, larray, lstrides, rarray, rstrides, &);
} else {
return ndarray_binary_op(MP_BINARY_OP_AND, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
} else if(lhs->dtype == NDARRAY_INT16) {
if(rhs->dtype == NDARRAY_INT16) {
results = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_INT16);
BINARY_LOOP(results, int16_t, int16_t, int16_t, larray, lstrides, rarray, rstrides, &);
} else {
return ndarray_binary_op(MP_BINARY_OP_AND, MP_OBJ_FROM_PTR(rhs), MP_OBJ_FROM_PTR(lhs));
}
}
break;
default:
return MP_OBJ_NULL; // op not supported
break;
}
return MP_OBJ_FROM_PTR(results);
}
#endif /* NDARRAY_HAS_BINARY_OP_OR | NDARRAY_HAS_BINARY_OP_XOR | NDARRAY_HAS_BINARY_OP_AND */
#if NDARRAY_HAS_INPLACE_ADD || NDARRAY_HAS_INPLACE_MULTIPLY || NDARRAY_HAS_INPLACE_SUBTRACT
mp_obj_t ndarray_inplace_ams(ndarray_obj_t *lhs, ndarray_obj_t *rhs, int32_t *rstrides, uint8_t optype) {

View file

@ -17,6 +17,7 @@ mp_obj_t ndarray_binary_more(ndarray_obj_t *, ndarray_obj_t *, uint8_t , size_t
mp_obj_t ndarray_binary_power(ndarray_obj_t *, ndarray_obj_t *, uint8_t , size_t *, int32_t *, int32_t *);
mp_obj_t ndarray_binary_subtract(ndarray_obj_t *, ndarray_obj_t *, uint8_t , size_t *, int32_t *, int32_t *);
mp_obj_t ndarray_binary_true_divide(ndarray_obj_t *, ndarray_obj_t *, uint8_t , size_t *, int32_t *, int32_t *);
mp_obj_t ndarray_binary_logical(ndarray_obj_t *, ndarray_obj_t *, uint8_t , size_t *, int32_t *, int32_t *, mp_binary_op_t );
mp_obj_t ndarray_binary_floor_divide(ndarray_obj_t *, ndarray_obj_t *, uint8_t , size_t *, int32_t *, int32_t *);
mp_obj_t ndarray_inplace_ams(ndarray_obj_t *, ndarray_obj_t *, int32_t *, uint8_t );

View file

@ -6,8 +6,8 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2019-2021 Zoltán Vörös
* 2020 Jeff Epler for Adafruit Industries
* Copyright (c) 2019-2023 Zoltán Vörös
* 2020-2023 Jeff Epler for Adafruit Industries
*/
#include <math.h>
@ -33,7 +33,7 @@
#include "user/user.h"
#include "utils/utils.h"
#define ULAB_VERSION 6.3.5
#define ULAB_VERSION 6.4.0
#define xstr(s) str(s)
#define str(s) #s

View file

@ -97,6 +97,10 @@
#define NDARRAY_HAS_BINARY_OP_ADD (1)
#endif
#ifndef NDARRAY_HAS_BINARY_OP_AND
#define NDARRAY_HAS_BINARY_OP_AND (1)
#endif
#ifndef NDARRAY_HAS_BINARY_OP_EQUAL
#define NDARRAY_HAS_BINARY_OP_EQUAL (1)
#endif
@ -129,6 +133,10 @@
#define NDARRAY_HAS_BINARY_OP_NOT_EQUAL (1)
#endif
#ifndef NDARRAY_HAS_BINARY_OP_OR
#define NDARRAY_HAS_BINARY_OP_OR (1)
#endif
#ifndef NDARRAY_HAS_BINARY_OP_POWER
#define NDARRAY_HAS_BINARY_OP_POWER (1)
#endif
@ -141,6 +149,10 @@
#define NDARRAY_HAS_BINARY_OP_TRUE_DIVIDE (1)
#endif
#ifndef NDARRAY_HAS_BINARY_OP_XOR
#define NDARRAY_HAS_BINARY_OP_XOR (1)
#endif
#ifndef NDARRAY_HAS_INPLACE_OPS
#define NDARRAY_HAS_INPLACE_OPS (1)
#endif

View file

@ -1,3 +1,9 @@
Thu, 20 Jul 2023
version 6.4.0
add logical operators and, or, xor
Sat, 1 Jul 2023
version 6.3.5

21
tests/2d/numpy/and.py Normal file
View file

@ -0,0 +1,21 @@
try:
from ulab import numpy as np
except ImportError:
import numpy as np
dtypes = (np.uint8, np.int8, np.uint16, np.int16)
for dtype_a in dtypes:
a = np.array(range(5), dtype=dtype_a)
for dtype_b in dtypes:
b = np.array(range(250, 255), dtype=dtype_b)
try:
print('a & b: ', a & b)
except Exception as e:
print(e)
b = np.array([False, True, False, True, False], dtype=np.bool)
try:
print('a & b (bool): ', a & b)
except Exception as e:
print(e)

20
tests/2d/numpy/and.py.exp Normal file
View file

@ -0,0 +1,20 @@
a & b: array([0, 1, 0, 1, 4], dtype=uint8)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b: array([0, 1, 0, 1, 4], dtype=uint16)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b (bool): array([0, 1, 0, 1, 0], dtype=uint8)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b: array([0, 1, 0, 1, 4], dtype=int8)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b (bool): array([0, 1, 0, 1, 0], dtype=int16)
a & b: array([0, 1, 0, 1, 4], dtype=uint16)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b: array([0, 1, 0, 1, 4], dtype=uint16)
dtype of int32 is not supported
a & b (bool): array([0, 1, 0, 1, 0], dtype=uint16)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b: array([0, 1, 0, 1, 4], dtype=int16)
dtype of int32 is not supported
a & b: array([0, 1, 0, 1, 4], dtype=int16)
a & b (bool): array([0, 1, 0, 1, 0], dtype=int16)

View file

@ -94,7 +94,7 @@ array([1.0, 2.0, 3.0], dtype=float64)
array([1.0, 32.0, 729.0], dtype=float64)
array([1.0, 32.0, 729.0], dtype=float64)
array([1.0, 32.0, 729.0], dtype=float64)
array([5, 7, 9], dtype=uint16)
array([5, 7, 9], dtype=uint8)
array([5, 7, 9], dtype=int16)
array([5, 7, 9], dtype=int8)
array([5, 7, 9], dtype=uint16)

21
tests/2d/numpy/or.py Normal file
View file

@ -0,0 +1,21 @@
try:
from ulab import numpy as np
except ImportError:
import numpy as np
dtypes = (np.uint8, np.int8, np.uint16, np.int16)
for dtype_a in dtypes:
a = np.array(range(5), dtype=dtype_a)
for dtype_b in dtypes:
b = np.array(range(250, 255), dtype=dtype_b)
try:
print('a | b: ', a | b)
except Exception as e:
print(e)
b = np.array([False, True, False, True, False], dtype=np.bool)
try:
print('a | b (bool): ', a | b)
except Exception as e:
print(e)

20
tests/2d/numpy/or.py.exp Normal file
View file

@ -0,0 +1,20 @@
a | b: array([250, 251, 254, 255, 254], dtype=uint8)
a | b: array([-6, -5, -2, -1, -2], dtype=int16)
a | b: array([250, 251, 254, 255, 254], dtype=uint16)
a | b: array([250, 251, 254, 255, 254], dtype=int16)
a | b (bool): array([0, 1, 2, 3, 4], dtype=uint8)
a | b: array([250, 251, 254, 255, 254], dtype=int16)
a | b: array([-6, -5, -2, -1, -2], dtype=int8)
a | b: array([250, 251, 254, 255, 254], dtype=int16)
a | b: array([250, 251, 254, 255, 254], dtype=int16)
a | b (bool): array([0, 1, 2, 3, 4], dtype=int16)
a | b: array([250, 251, 254, 255, 254], dtype=uint16)
a | b: array([-6, -5, -2, -1, -2], dtype=int16)
a | b: array([250, 251, 254, 255, 254], dtype=uint16)
dtype of int32 is not supported
a | b (bool): array([0, 1, 2, 3, 4], dtype=uint16)
a | b: array([250, 251, 254, 255, 254], dtype=int16)
a | b: array([-6, -5, -2, -1, -2], dtype=int16)
dtype of int32 is not supported
a | b: array([250, 251, 254, 255, 254], dtype=int16)
a | b (bool): array([0, 1, 2, 3, 4], dtype=int16)

21
tests/2d/numpy/xor.py Normal file
View file

@ -0,0 +1,21 @@
try:
from ulab import numpy as np
except ImportError:
import numpy as np
dtypes = (np.uint8, np.int8, np.uint16, np.int16)
for dtype_a in dtypes:
a = np.array(range(5), dtype=dtype_a)
for dtype_b in dtypes:
b = np.array(range(250, 255), dtype=dtype_b)
try:
print('a ^ b: ', a ^ b)
except Exception as e:
print(e)
b = np.array([False, True, False, True, False], dtype=np.bool)
try:
print('a ^ b (bool): ', a ^ b)
except Exception as e:
print(e)

20
tests/2d/numpy/xor.py.exp Normal file
View file

@ -0,0 +1,20 @@
a ^ b: array([250, 250, 254, 254, 250], dtype=uint8)
a ^ b: array([-6, -6, -2, -2, -6], dtype=int16)
a ^ b: array([250, 250, 254, 254, 250], dtype=uint16)
a ^ b: array([250, 250, 254, 254, 250], dtype=int16)
a ^ b (bool): array([0, 0, 2, 2, 4], dtype=uint8)
a ^ b: array([250, 250, 254, 254, 250], dtype=int16)
a ^ b: array([-6, -6, -2, -2, -6], dtype=int8)
a ^ b: array([250, 250, 254, 254, 250], dtype=int16)
a ^ b: array([250, 250, 254, 254, 250], dtype=int16)
a ^ b (bool): array([0, 0, 2, 2, 4], dtype=int16)
a ^ b: array([250, 250, 254, 254, 250], dtype=uint16)
a ^ b: array([-6, -6, -2, -2, -6], dtype=int16)
a ^ b: array([250, 250, 254, 254, 250], dtype=uint16)
dtype of int32 is not supported
a ^ b (bool): array([0, 0, 2, 2, 4], dtype=uint16)
a ^ b: array([250, 250, 254, 254, 250], dtype=int16)
a ^ b: array([-6, -6, -2, -2, -6], dtype=int16)
dtype of int32 is not supported
a ^ b: array([250, 250, 254, 254, 250], dtype=int16)
a ^ b (bool): array([0, 0, 2, 2, 4], dtype=int16)