@@ -61,98 +61,6 @@ void ndarray_set_complex_value(void *p, size_t index, mp_obj_t value) {
61
61
}
62
62
}
63
63
64
- #ifdef CIRCUITPY
65
- void ndarray_set_value (char typecode , void * p , size_t index , mp_obj_t val_in ) {
66
- switch (typecode ) {
67
- case NDARRAY_INT8 :
68
- ((signed char * )p )[index ] = mp_obj_get_int (val_in );
69
- break ;
70
- case NDARRAY_UINT8 :
71
- ((unsigned char * )p )[index ] = mp_obj_get_int (val_in );
72
- break ;
73
- case NDARRAY_INT16 :
74
- ((short * )p )[index ] = mp_obj_get_int (val_in );
75
- break ;
76
- case NDARRAY_UINT16 :
77
- ((unsigned short * )p )[index ] = mp_obj_get_int (val_in );
78
- break ;
79
- case NDARRAY_FLOAT :
80
- ((mp_float_t * )p )[index ] = mp_obj_get_float (val_in );
81
- break ;
82
- #if ULAB_SUPPORTS_COMPLEX
83
- case NDARRAY_COMPLEX :
84
- ndarray_set_complex_value (p , index , val_in );
85
- break ;
86
- #endif
87
- }
88
- }
89
- #endif
90
-
91
- #if defined(MICROPY_VERSION_MAJOR ) && MICROPY_VERSION_MAJOR == 1 && MICROPY_VERSION_MINOR == 11
92
-
93
- void mp_obj_slice_indices (mp_obj_t self_in , mp_int_t length , mp_bound_slice_t * result ) {
94
- mp_obj_slice_t * self = MP_OBJ_TO_PTR (self_in );
95
- mp_int_t start , stop , step ;
96
-
97
- if (self -> step == mp_const_none ) {
98
- step = 1 ;
99
- } else {
100
- step = mp_obj_get_int (self -> step );
101
- if (step == 0 ) {
102
- mp_raise_ValueError (translate ("slice step can't be zero" ));
103
- }
104
- }
105
-
106
- if (step > 0 ) {
107
- // Positive step
108
- if (self -> start == mp_const_none ) {
109
- start = 0 ;
110
- } else {
111
- start = mp_obj_get_int (self -> start );
112
- if (start < 0 ) {
113
- start += length ;
114
- }
115
- start = MIN (length , MAX (start , 0 ));
116
- }
117
-
118
- if (self -> stop == mp_const_none ) {
119
- stop = length ;
120
- } else {
121
- stop = mp_obj_get_int (self -> stop );
122
- if (stop < 0 ) {
123
- stop += length ;
124
- }
125
- stop = MIN (length , MAX (stop , 0 ));
126
- }
127
- } else {
128
- // Negative step
129
- if (self -> start == mp_const_none ) {
130
- start = length - 1 ;
131
- } else {
132
- start = mp_obj_get_int (self -> start );
133
- if (start < 0 ) {
134
- start += length ;
135
- }
136
- start = MIN (length - 1 , MAX (start , -1 ));
137
- }
138
-
139
- if (self -> stop == mp_const_none ) {
140
- stop = -1 ;
141
- } else {
142
- stop = mp_obj_get_int (self -> stop );
143
- if (stop < 0 ) {
144
- stop += length ;
145
- }
146
- stop = MIN (length - 1 , MAX (stop , -1 ));
147
- }
148
- }
149
-
150
- result -> start = start ;
151
- result -> stop = stop ;
152
- result -> step = step ;
153
- }
154
- #endif /* MICROPY_VERSION v1.11 */
155
-
156
64
void ndarray_fill_array_iterable (mp_float_t * array , mp_obj_t iterable ) {
157
65
mp_obj_iter_buf_t x_buf ;
158
66
mp_obj_t x_item , x_iterable = mp_getiter (iterable , & x_buf );
@@ -291,7 +199,7 @@ mp_obj_t ndarray_dtype_make_new(const mp_obj_type_t *type, size_t n_args, size_t
291
199
if ((_dtype != NDARRAY_BOOL ) && (_dtype != NDARRAY_UINT8 )
292
200
&& (_dtype != NDARRAY_INT8 ) && (_dtype != NDARRAY_UINT16 )
293
201
&& (_dtype != NDARRAY_INT16 ) && (_dtype != NDARRAY_FLOAT )) {
294
- mp_raise_TypeError (translate ("data type not understood" ));
202
+ mp_raise_TypeError (MP_ERROR_TEXT ("data type not understood" ));
295
203
}
296
204
} else {
297
205
GET_STR_DATA_LEN (_args [0 ].u_obj , _dtype_ , len );
@@ -312,7 +220,7 @@ mp_obj_t ndarray_dtype_make_new(const mp_obj_type_t *type, size_t n_args, size_t
312
220
}
313
221
#endif
314
222
else {
315
- mp_raise_TypeError (translate ("data type not understood" ));
223
+ mp_raise_TypeError (MP_ERROR_TEXT ("data type not understood" ));
316
224
}
317
225
}
318
226
dtype -> dtype = _dtype ;
@@ -344,7 +252,7 @@ mp_obj_t ndarray_dtype(mp_obj_t self_in) {
344
252
&& (* _dtype != NDARRAY_COMPLEX )
345
253
#endif
346
254
)) {
347
- mp_raise_TypeError (translate ("data type not understood" ));
255
+ mp_raise_TypeError (MP_ERROR_TEXT ("data type not understood" ));
348
256
}
349
257
dtype = * _dtype ;
350
258
}
@@ -596,7 +504,7 @@ bool ndarray_is_dense(ndarray_obj_t *ndarray) {
596
504
static size_t multiply_size (size_t a , size_t b ) {
597
505
size_t result ;
598
506
if (__builtin_mul_overflow (a , b , & result )) {
599
- mp_raise_ValueError (translate ("array is too big" ));
507
+ mp_raise_ValueError (MP_ERROR_TEXT ("array is too big" ));
600
508
}
601
509
return result ;
602
510
}
@@ -623,7 +531,7 @@ ndarray_obj_t *ndarray_new_ndarray(uint8_t ndim, size_t *shape, int32_t *strides
623
531
}
624
532
625
533
if (SIZE_MAX / ndarray -> itemsize <= ndarray -> len ) {
626
- mp_raise_ValueError (translate ("ndarray length overflows" ));
534
+ mp_raise_ValueError (MP_ERROR_TEXT ("ndarray length overflows" ));
627
535
}
628
536
629
537
// if the length is 0, still allocate a single item, so that contractions can be handled
@@ -782,7 +690,7 @@ ndarray_obj_t *ndarray_copy_view_convert_type(ndarray_obj_t *source, uint8_t dty
782
690
#if ULAB_SUPPORTS_COMPLEX
783
691
if (source -> dtype == NDARRAY_COMPLEX ) {
784
692
if (dtype != NDARRAY_COMPLEX ) {
785
- mp_raise_TypeError (translate ("cannot convert complex type" ));
693
+ mp_raise_TypeError (MP_ERROR_TEXT ("cannot convert complex type" ));
786
694
} else {
787
695
memcpy (array , sarray , complex_size );
788
696
}
@@ -948,7 +856,7 @@ ndarray_obj_t *ndarray_from_iterable(mp_obj_t obj, uint8_t dtype) {
948
856
break ;
949
857
}
950
858
if (ndim == ULAB_MAX_DIMS ) {
951
- mp_raise_ValueError (translate ("too many dimensions" ));
859
+ mp_raise_ValueError (MP_ERROR_TEXT ("too many dimensions" ));
952
860
}
953
861
shape [ndim ] = MP_OBJ_SMALL_INT_VALUE (mp_obj_len_maybe (item ));
954
862
if (shape [ndim ] == 0 ) {
@@ -1138,13 +1046,13 @@ static mp_bound_slice_t generate_slice(mp_int_t n, mp_obj_t index) {
1138
1046
_index += n ;
1139
1047
}
1140
1048
if ((_index >= n ) || (_index < 0 )) {
1141
- mp_raise_msg (& mp_type_IndexError , translate ("index is out of bounds" ));
1049
+ mp_raise_msg (& mp_type_IndexError , MP_ERROR_TEXT ("index is out of bounds" ));
1142
1050
}
1143
1051
slice .start = _index ;
1144
1052
slice .stop = _index + 1 ;
1145
1053
slice .step = 1 ;
1146
1054
} else {
1147
- mp_raise_msg (& mp_type_IndexError , translate ("indices must be integers, slices, or Boolean lists" ));
1055
+ mp_raise_msg (& mp_type_IndexError , MP_ERROR_TEXT ("indices must be integers, slices, or Boolean lists" ));
1148
1056
}
1149
1057
return slice ;
1150
1058
}
@@ -1170,7 +1078,7 @@ static ndarray_obj_t *ndarray_view_from_slices(ndarray_obj_t *ndarray, mp_obj_tu
1170
1078
k += ndarray -> shape [ULAB_MAX_DIMS - ndarray -> ndim + i ];
1171
1079
}
1172
1080
if ((k >= (int32_t )ndarray -> shape [ULAB_MAX_DIMS - ndarray -> ndim + i ]) || (k < 0 )) {
1173
- mp_raise_msg (& mp_type_IndexError , translate ("index is out of bounds" ));
1081
+ mp_raise_msg (& mp_type_IndexError , MP_ERROR_TEXT ("index is out of bounds" ));
1174
1082
}
1175
1083
offset += ndarray -> strides [ULAB_MAX_DIMS - ndarray -> ndim + i ] * k ;
1176
1084
// ... and then we have to shift the shapes to the right
@@ -1197,7 +1105,7 @@ void ndarray_assign_view(ndarray_obj_t *view, ndarray_obj_t *values) {
1197
1105
int32_t * lstrides = m_new0 (int32_t , ULAB_MAX_DIMS );
1198
1106
int32_t * rstrides = m_new0 (int32_t , ULAB_MAX_DIMS );
1199
1107
if (!ndarray_can_broadcast (view , values , & ndim , shape , lstrides , rstrides )) {
1200
- mp_raise_ValueError (translate ("operands could not be broadcast together" ));
1108
+ mp_raise_ValueError (MP_ERROR_TEXT ("operands could not be broadcast together" ));
1201
1109
} else {
1202
1110
1203
1111
ndarray_obj_t * ndarray = ndarray_copy_view_convert_type (values , view -> dtype );
@@ -1263,7 +1171,7 @@ void ndarray_assign_view(ndarray_obj_t *view, ndarray_obj_t *values) {
1263
1171
static mp_obj_t ndarray_from_boolean_index (ndarray_obj_t * ndarray , ndarray_obj_t * index ) {
1264
1172
// returns a 1D array, indexed by a Boolean array
1265
1173
if (ndarray -> len != index -> len ) {
1266
- mp_raise_ValueError (translate ("array and index length must be equal" ));
1174
+ mp_raise_ValueError (MP_ERROR_TEXT ("array and index length must be equal" ));
1267
1175
}
1268
1176
uint8_t * iarray = (uint8_t * )index -> array ;
1269
1177
// first we have to find out how many trues there are
@@ -1315,7 +1223,7 @@ static mp_obj_t ndarray_assign_from_boolean_index(ndarray_obj_t *ndarray, ndarra
1315
1223
#if ULAB_SUPPORTS_COMPLEX
1316
1224
if (values -> dtype == NDARRAY_COMPLEX ) {
1317
1225
if (ndarray -> dtype != NDARRAY_COMPLEX ) {
1318
- mp_raise_TypeError (translate ("cannot convert complex to dtype" ));
1226
+ mp_raise_TypeError (MP_ERROR_TEXT ("cannot convert complex to dtype" ));
1319
1227
} else {
1320
1228
uint8_t * array = (uint8_t * )ndarray -> array ;
1321
1229
for (size_t i = 0 ; i < ndarray -> len ; i ++ ) {
@@ -1406,7 +1314,7 @@ static mp_obj_t ndarray_get_slice(ndarray_obj_t *ndarray, mp_obj_t index, ndarra
1406
1314
if (mp_obj_is_type (index , & ulab_ndarray_type )) {
1407
1315
ndarray_obj_t * nindex = MP_OBJ_TO_PTR (index );
1408
1316
if ((nindex -> ndim > 1 ) || (nindex -> boolean == false)) {
1409
- mp_raise_NotImplementedError (translate ("operation is implemented for 1D Boolean arrays only" ));
1317
+ mp_raise_NotImplementedError (MP_ERROR_TEXT ("operation is implemented for 1D Boolean arrays only" ));
1410
1318
}
1411
1319
if (values == NULL ) { // return value(s)
1412
1320
return ndarray_from_boolean_index (ndarray , nindex );
@@ -1419,7 +1327,7 @@ static mp_obj_t ndarray_get_slice(ndarray_obj_t *ndarray, mp_obj_t index, ndarra
1419
1327
if (mp_obj_is_type (index , & mp_type_tuple )) {
1420
1328
tuple = MP_OBJ_TO_PTR (index );
1421
1329
if (tuple -> len > ndarray -> ndim ) {
1422
- mp_raise_msg (& mp_type_IndexError , translate ("too many indices" ));
1330
+ mp_raise_msg (& mp_type_IndexError , MP_ERROR_TEXT ("too many indices" ));
1423
1331
}
1424
1332
} else {
1425
1333
mp_obj_t * items = m_new (mp_obj_t , 1 );
@@ -1443,7 +1351,7 @@ static mp_obj_t ndarray_get_slice(ndarray_obj_t *ndarray, mp_obj_t index, ndarra
1443
1351
1444
1352
mp_obj_t ndarray_subscr (mp_obj_t self_in , mp_obj_t index , mp_obj_t value ) {
1445
1353
if (value == MP_OBJ_NULL ) {
1446
- mp_raise_ValueError (translate ("cannot delete array elements" ));
1354
+ mp_raise_ValueError (MP_ERROR_TEXT ("cannot delete array elements" ));
1447
1355
}
1448
1356
ndarray_obj_t * self = MP_OBJ_TO_PTR (self_in );
1449
1357
@@ -1521,7 +1429,7 @@ mp_obj_t ndarray_flatten(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a
1521
1429
ndarray_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
1522
1430
GET_STR_DATA_LEN (args [0 ].u_obj , order , len );
1523
1431
if ((len != 1 ) || ((memcmp (order , "C" , 1 ) != 0 ) && (memcmp (order , "F" , 1 ) != 0 ))) {
1524
- mp_raise_ValueError (translate ("flattening order must be either 'C', or 'F'" ));
1432
+ mp_raise_ValueError (MP_ERROR_TEXT ("flattening order must be either 'C', or 'F'" ));
1525
1433
}
1526
1434
1527
1435
uint8_t * sarray = (uint8_t * )self -> array ;
@@ -1659,7 +1567,7 @@ mp_obj_t ndarray_tobytes(mp_obj_t self_in) {
1659
1567
// Piping into a bytearray makes sense for dense arrays only,
1660
1568
// so bail out, if that is not the case
1661
1569
if (!ndarray_is_dense (self )) {
1662
- mp_raise_ValueError (translate ("tobytes can be invoked for dense arrays only" ));
1570
+ mp_raise_ValueError (MP_ERROR_TEXT ("tobytes can be invoked for dense arrays only" ));
1663
1571
}
1664
1572
return mp_obj_new_bytearray_by_ref (self -> itemsize * self -> len , self -> array );
1665
1573
}
@@ -1799,7 +1707,7 @@ mp_obj_t ndarray_binary_op(mp_binary_op_t _op, mp_obj_t lobj, mp_obj_t robj) {
1799
1707
broadcastable = ndarray_can_broadcast (lhs , rhs , & ndim , shape , lstrides , rstrides );
1800
1708
}
1801
1709
if (!broadcastable ) {
1802
- mp_raise_ValueError (translate ("operands could not be broadcast together" ));
1710
+ mp_raise_ValueError (MP_ERROR_TEXT ("operands could not be broadcast together" ));
1803
1711
m_del (size_t , shape , ULAB_MAX_DIMS );
1804
1712
m_del (int32_t , lstrides , ULAB_MAX_DIMS );
1805
1713
m_del (int32_t , rstrides , ULAB_MAX_DIMS );
@@ -2015,7 +1923,7 @@ mp_obj_t ndarray_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
2015
1923
#else
2016
1924
if (self -> dtype == NDARRAY_FLOAT ) {
2017
1925
#endif
2018
- mp_raise_ValueError (translate ("operation is not supported for given type" ));
1926
+ mp_raise_ValueError (MP_ERROR_TEXT ("operation is not supported for given type" ));
2019
1927
}
2020
1928
// we can invert the content byte by byte, no need to distinguish between different dtypes
2021
1929
ndarray = ndarray_copy_view (self ); // from this point, this is a dense copy
@@ -2104,7 +2012,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(ndarray_transpose_obj, ndarray_transpose);
2104
2012
mp_obj_t ndarray_reshape_core (mp_obj_t oin , mp_obj_t _shape , bool inplace ) {
2105
2013
ndarray_obj_t * source = MP_OBJ_TO_PTR (oin );
2106
2014
if (!mp_obj_is_type (_shape , & mp_type_tuple ) && !mp_obj_is_int (_shape )) {
2107
- mp_raise_TypeError (translate ("shape must be integer or tuple of integers" ));
2015
+ mp_raise_TypeError (MP_ERROR_TEXT ("shape must be integer or tuple of integers" ));
2108
2016
}
2109
2017
2110
2018
mp_obj_tuple_t * shape ;
@@ -2118,7 +2026,7 @@ mp_obj_t ndarray_reshape_core(mp_obj_t oin, mp_obj_t _shape, bool inplace) {
2118
2026
}
2119
2027
2120
2028
if (shape -> len > ULAB_MAX_DIMS ) {
2121
- mp_raise_ValueError (translate ("maximum number of dimensions is " MP_STRINGIFY (ULAB_MAX_DIMS )));
2029
+ mp_raise_ValueError (MP_ERROR_TEXT ("maximum number of dimensions is " MP_STRINGIFY (ULAB_MAX_DIMS )));
2122
2030
}
2123
2031
2124
2032
size_t new_length = 1 ;
@@ -2138,14 +2046,14 @@ mp_obj_t ndarray_reshape_core(mp_obj_t oin, mp_obj_t _shape, bool inplace) {
2138
2046
}
2139
2047
2140
2048
if (unknown_dim > 1 ) {
2141
- mp_raise_ValueError (translate ("can only specify one unknown dimension" ));
2049
+ mp_raise_ValueError (MP_ERROR_TEXT ("can only specify one unknown dimension" ));
2142
2050
} else if (unknown_dim == 1 ) {
2143
2051
new_shape [unknown_index ] = source -> len / new_length ;
2144
2052
new_length = source -> len ;
2145
2053
}
2146
2054
2147
2055
if (source -> len != new_length ) {
2148
- mp_raise_ValueError (translate ("cannot reshape array" ));
2056
+ mp_raise_ValueError (MP_ERROR_TEXT ("cannot reshape array" ));
2149
2057
}
2150
2058
2151
2059
ndarray_obj_t * ndarray ;
@@ -2162,7 +2070,7 @@ mp_obj_t ndarray_reshape_core(mp_obj_t oin, mp_obj_t _shape, bool inplace) {
2162
2070
}
2163
2071
} else {
2164
2072
if (inplace ) {
2165
- mp_raise_ValueError (translate ("cannot assign new shape" ));
2073
+ mp_raise_ValueError (MP_ERROR_TEXT ("cannot assign new shape" ));
2166
2074
}
2167
2075
if (mp_obj_is_type (_shape , & mp_type_tuple )) {
2168
2076
ndarray = ndarray_new_ndarray_from_tuple (shape , source -> dtype );
@@ -2185,7 +2093,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(ndarray_reshape_obj, ndarray_reshape);
2185
2093
#if ULAB_NUMPY_HAS_NDINFO
2186
2094
mp_obj_t ndarray_info (mp_obj_t obj_in ) {
2187
2095
if (!mp_obj_is_type (obj_in , & ulab_ndarray_type )) {
2188
- mp_raise_TypeError (translate ("function is defined for ndarrays only" ));
2096
+ mp_raise_TypeError (MP_ERROR_TEXT ("function is defined for ndarrays only" ));
2189
2097
}
2190
2098
ndarray_obj_t * ndarray = MP_OBJ_TO_PTR (obj_in );
2191
2099
mp_printf (MP_PYTHON_PRINTER , "class: ndarray\n" );
0 commit comments