fixed test scripts

This commit is contained in:
Zoltán Vörös 2020-10-17 21:26:59 +02:00
parent e2fd36ed7a
commit ca61961d22
13 changed files with 143 additions and 84 deletions

View file

@ -47,7 +47,6 @@ bool linalg_invert_matrix(mp_float_t *data, size_t N) {
// initially, this is the unit matrix: the contents of this matrix is what
// will be returned after all the transformations
mp_float_t *unit = m_new(mp_float_t, N*N);
mp_float_t elem = 1.0;
// initialise the unit matrix
memset(unit, 0, sizeof(mp_float_t)*N*N);
@ -188,6 +187,9 @@ static mp_obj_t linalg_det(mp_obj_t oin) {
array += ndarray->strides[ULAB_MAX_DIMS - 2];
}
// re-wind the pointer
tmp -= N*N;
mp_float_t c;
mp_float_t det_sign = 1.0;
@ -449,7 +451,6 @@ static mp_obj_t linalg_inv(mp_obj_t o_in) {
ndarray_obj_t *ndarray = linalg_object_is_square(o_in);
uint8_t *array = (uint8_t *)ndarray->array;
size_t N = ndarray->shape[ULAB_MAX_DIMS - 1];
ndarray_obj_t *inverted = ndarray_new_dense_ndarray(2, ndarray_shape_vector(0, 0, N, N), NDARRAY_FLOAT);
mp_float_t *iarray = (mp_float_t *)inverted->array;
@ -461,9 +462,10 @@ static mp_obj_t linalg_inv(mp_obj_t o_in) {
array -= ndarray->strides[ULAB_MAX_DIMS - 1] * N;
array += ndarray->strides[ULAB_MAX_DIMS - 2];
}
// re-wind the pointer
iarray -= N*N;
if(!linalg_invert_matrix(iarray, N)) {
// TODO: I am not sure this is needed here. Otherwise,
// how should we free up the unused RAM of inverted?
mp_raise_ValueError(translate("input matrix is singular"));
}
return MP_OBJ_FROM_PTR(inverted);

View file

@ -461,13 +461,13 @@ void ndarray_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t ki
#if ULAB_MAX_DIMS > 1
array += self->strides[ULAB_MAX_DIMS-2];
k++;
ndarray_print_bracket(print, k, self->shape[ULAB_MAX_DIMS-2], ",\n\t");
ndarray_print_bracket(print, k, self->shape[ULAB_MAX_DIMS-2], ",\n ");
} while(k < self->shape[ULAB_MAX_DIMS-2]);
#endif
#if ULAB_MAX_DIMS > 2
ndarray_print_bracket(print, 0, self->shape[ULAB_MAX_DIMS-2], "]");
j++;
ndarray_print_bracket(print, j, self->shape[ULAB_MAX_DIMS-3], ",\n\n\t");
ndarray_print_bracket(print, j, self->shape[ULAB_MAX_DIMS-3], ",\n\n ");
array -= self->strides[ULAB_MAX_DIMS-2] * self->shape[ULAB_MAX_DIMS-2];
array += self->strides[ULAB_MAX_DIMS-3];
} while(j < self->shape[ULAB_MAX_DIMS-3]);
@ -477,7 +477,7 @@ void ndarray_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t ki
array -= self->strides[ULAB_MAX_DIMS-3] * self->shape[ULAB_MAX_DIMS-3];
array += self->strides[ULAB_MAX_DIMS-4];
i++;
ndarray_print_bracket(print, i, self->shape[ULAB_MAX_DIMS-4], ",\n\n\t");
ndarray_print_bracket(print, i, self->shape[ULAB_MAX_DIMS-4], ",\n\n ");
} while(i < self->shape[ULAB_MAX_DIMS-4]);
#endif

View file

@ -206,20 +206,83 @@ static mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t
mp_raise_ValueError(translate("attempt to get (arg)min/(arg)max of empty sequence"));
}
if(axis == mp_const_none) {
// work with the flattened array
uint8_t *array = (uint8_t *)ndarray->array;
mp_float_t best_value = ndarray_get_float_value(array, ndarray->dtype);
mp_float_t value;
size_t index = 0, best_index = 0;
#if ULAB_MAX_DIMS > 3
size_t i = 0;
do {
#endif
#if ULAB_MAX_DIMS > 2
size_t j = 0;
do {
#endif
#if ULAB_MAX_DIMS > 1
size_t k = 0;
do {
#endif
size_t l = 0;
do {
value = ndarray_get_float_value(array, ndarray->dtype);
if((optype == NUMERICAL_ARGMAX) || (optype == NUMERICAL_MAX)) {
if(best_value < value) {
best_value = value;
best_index = index;
}
} else {
if(best_value > value) {
best_value = value;
best_index = index;
}
}
array += ndarray->strides[ULAB_MAX_DIMS - 1];
l++;
index++;
} while(l < ndarray->shape[ULAB_MAX_DIMS - 1]);
#if ULAB_MAX_DIMS > 1
array -= ndarray->strides[ULAB_MAX_DIMS - 1] * ndarray->shape[ULAB_MAX_DIMS-1];
array += ndarray->strides[ULAB_MAX_DIMS - 2];
k++;
} while(k < ndarray->shape[ULAB_MAX_DIMS - 2]);
#endif
#if ULAB_MAX_DIMS > 2
array -= ndarray->strides[ULAB_MAX_DIMS - 2] * ndarray->shape[ULAB_MAX_DIMS-2];
array += ndarray->strides[ULAB_MAX_DIMS - 3];
j++;
} while(j < ndarray->shape[ULAB_MAX_DIMS - 3]);
#endif
#if ULAB_MAX_DIMS > 3
sarray -= ndarray->strides[ULAB_MAX_DIMS - 3] * ndarray->shape[ULAB_MAX_DIMS-3];
sarray += ndarray->strides[ULAB_MAX_DIMS - 4];
i++;
} while(i < ndarray->shape[ULAB_MAX_DIMS - 4]);
#endif
if((optype == NUMERICAL_ARGMIN) || (optype == NUMERICAL_ARGMAX)) {
return mp_obj_new_int(best_index);
} else {
if(ndarray->dtype == NDARRAY_FLOAT) {
return mp_obj_new_float(best_value);
} else {
return MP_OBJ_NEW_SMALL_INT((uint32_t)best_value);
}
}
} else {
int8_t ax = mp_obj_get_int(axis);
if(ax < 0) ax += ndarray->ndim;
if((ax < 0) || (ax > ndarray->ndim - 1)) {
mp_raise_ValueError(translate("axis is out of bounds"));
}
uint8_t *array = (uint8_t *)ndarray->array;
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
memset(strides, 0, sizeof(uint32_t)*ULAB_MAX_DIMS);
if(axis == mp_const_none) {
// pass for now
} else {
int8_t ax = mp_obj_get_int(axis);
if(ax < 0) ax += ndarray->ndim;
if((ax < 0) || (ax > ndarray->ndim - 1)) {
mp_raise_ValueError(translate("index out of range"));
}
numerical_reduce_axes(ndarray, ax, shape, strides);
uint8_t index = ULAB_MAX_DIMS - ndarray->ndim + ax;

View file

@ -27,7 +27,7 @@
// i.e., functions can be called at the top level,
// without having to import the sub-modules (linalg and fft are exceptions,
// since those must be imported even in numpy)
#define ULAB_NUMPY_COMPATIBILITY (0)
#define ULAB_NUMPY_COMPATIBILITY (1)
// The maximum number of dimensions the firmware should be able to support
// Possible values lie between 1, and 4, inclusive

View file

@ -297,10 +297,7 @@ mp_obj_t create_eye(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
} else {
m = mp_obj_get_int(args[1].u_rom_obj);
}
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
shape[ULAB_MAX_DIMS - 2] = m;
shape[ULAB_MAX_DIMS - 1] = n;
ndarray_obj_t *ndarray = ndarray_new_dense_ndarray(2, shape, dtype);
ndarray_obj_t *ndarray = ndarray_new_dense_ndarray(2, ndarray_shape_vector(0, 0, m, n), dtype);
mp_obj_t one = mp_obj_new_int(1);
size_t i = 0;
if((args[2].u_int >= 0)) {

View file

@ -1,4 +1,4 @@
import ulab
import ulab as np
# Adapted from https://docs.python.org/3.8/library/itertools.html#itertools.permutations
def permutations(iterable, r=None):
@ -28,26 +28,26 @@ def permutations(iterable, r=None):
# Combinations expected to throw
try:
print(ulab.numerical.argmin([]))
print(np.argmin([]))
except ValueError:
print("ValueError")
try:
print(ulab.numerical.argmax([]))
print(np.argmax([]))
except ValueError:
print("ValueError")
# Combinations expected to succeed
print(ulab.numerical.argmin([1]))
print(ulab.numerical.argmax([1]))
print(ulab.numerical.argmin(ulab.array([1])))
print(ulab.numerical.argmax(ulab.array([1])))
print(np.argmin([1]))
print(np.argmax([1]))
print(np.argmin(np.array([1])))
print(np.argmax(np.array([1])))
print()
print("max tests")
for p in permutations((100,200,300)):
m1 = ulab.numerical.argmax(p)
m2 = ulab.numerical.argmax(ulab.array(p))
m1 = np.argmax(p)
m2 = np.argmax(np.array(p))
print(p, m1, m2)
if m1 != m2 or p[m1] != max(p):
print("FAIL", p, m1, m2, max(p))
@ -55,8 +55,8 @@ for p in permutations((100,200,300)):
print()
print("min tests")
for p in permutations((100,200,300)):
m1 = ulab.numerical.argmin(p)
m2 = ulab.numerical.argmin(ulab.array(p))
m1 = np.argmin(p)
m2 = np.argmin(np.array(p))
print(p, m1, m2)
if m1 != m2 or p[m1] != min(p):
print("FAIL", p, m1, m2, min(p))

View file

@ -1,14 +1,13 @@
import ulab
from ulab import compare
import ulab as np
a = ulab.array([1, 2, 3, 4, 5], dtype=ulab.uint8)
b = ulab.array([5, 4, 3, 2, 1], dtype=ulab.float)
print(compare.minimum(a, b))
print(compare.maximum(a, b))
print(compare.maximum(1, 5.5))
a = np.array([1, 2, 3, 4, 5], dtype=np.uint8)
b = np.array([5, 4, 3, 2, 1], dtype=np.float)
print(np.minimum(a, b))
print(np.maximum(a, b))
print(np.maximum(1, 5.5))
a = ulab.array(range(9), dtype=ulab.uint8)
print(compare.clip(a, 3, 7))
a = np.array(range(9), dtype=np.uint8)
print(np.clip(a, 3, 7))
b = 3 * ulab.ones(len(a), dtype=ulab.float)
print(compare.clip(a, b, 7))
b = 3 * np.ones(len(a), dtype=np.float)
print(np.clip(a, b, 7))

View file

@ -6,11 +6,11 @@ array([3.0, 3.0, 3.0], dtype=float)
array([-1.0, -1.0, -1.0], dtype=float)
array([2.0, 2.0, 2.0], dtype=float)
array([0.5, 0.5, 0.5], dtype=float)
[False, False, False]
[True, True, True]
[False, False, False]
[True, True, True]
[False, False, False]
[False, False, False]
[True, True, True]
[True, True, True]
array([False, False, False], dtype=bool)
array([True, True, True], dtype=bool)
array([False, False, False], dtype=bool)
array([True, True, True], dtype=bool)
array([False, False, False], dtype=bool)
array([False, False, False], dtype=bool)
array([True, True, True], dtype=bool)
array([True, True, True], dtype=bool)

View file

@ -1,27 +1,25 @@
import ulab
from ulab import poly
from ulab import vector
import ulab as np
# polynom evaluation
x = ulab.linspace(0, 10, num=9)
x = np.linspace(0, 10, num=9)
p = [1, 2, 3]
y = poly.polyval(p, x)
y = np.polyval(p, x)
print(y)
# linear fit
x = ulab.linspace(-5, 5, num=11)
y = x + vector.sin(x)
p = poly.polyfit(x, y, 1)
x = np.linspace(-5, 5, num=11)
y = x + np.sin(x)
p = np.polyfit(x, y, 1)
print(p)
# quadratic fit
x = ulab.linspace(-5, 5, num=11)
y = x*x + vector.sin(x)*3.0
p = poly.polyfit(x, y, 2)
x = np.linspace(-5, 5, num=11)
y = x*x + np.sin(x)*3.0
p = np.polyfit(x, y, 2)
print(p)
# cubic fit
x = ulab.linspace(-5, 5, num=11)
y = x*x*x + vector.sin(x)*10.0
p = poly.polyfit(x, y, 3)
x = np.linspace(-5, 5, num=11)
y = x*x*x + np.sin(x)*10.0
p = np.polyfit(x, y, 3)
print(p)