Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add imag function to carray
  • Loading branch information
v923z committed Apr 20, 2021
commit 0e022bc50a43c515a9bb66406fea91e24d85a059
48 changes: 48 additions & 0 deletions code/numpy/carray/carray.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,57 @@
* Copyright (c) 2021 Zoltán Vörös
*/

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "py/obj.h"
#include "py/objint.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "py/misc.h"

#include "../../ulab.h"
#include "../../ndarray.h"

#if ULAB_SUPPORTS_COMPLEX

STATIC mp_obj_t carray_real(mp_obj_t _source) {
if(MP_OBJ_IS_TYPE(_source, &ulab_ndarray_type)) {
ndarray_obj_t *source = MP_OBJ_TO_PTR(_source);
if(source->dtype != NDARRAY_COMPLEX) {
ndarray_obj_t *target = ndarray_new_dense_ndarray(source->ndim, source->shape, source->dtype);
ndarray_copy_array(source, target, 0);
return MP_OBJ_FROM_PTR(target);
} else { // the input is most definitely a complex array
ndarray_obj_t *target = ndarray_new_dense_ndarray(source->ndim, source->shape, NDARRAY_FLOAT);
ndarray_copy_array(source, target, 0);
return MP_OBJ_FROM_PTR(target);
}
} else {
mp_raise_NotImplementedError(translate("function is implemented for ndarrays only"));
}
return mp_const_none;
}

MP_DEFINE_CONST_FUN_OBJ_1(carray_real_obj, carray_real);

STATIC mp_obj_t carray_imag(mp_obj_t _source) {
if(MP_OBJ_IS_TYPE(_source, &ulab_ndarray_type)) {
ndarray_obj_t *source = MP_OBJ_TO_PTR(_source);
if(source->dtype != NDARRAY_COMPLEX) { // if not complex, then the imaginary part is zero
ndarray_obj_t *target = ndarray_new_dense_ndarray(source->ndim, source->shape, source->dtype);
return MP_OBJ_FROM_PTR(target);
} else { // the input is most definitely a complex array
ndarray_obj_t *target = ndarray_new_dense_ndarray(source->ndim, source->shape, NDARRAY_FLOAT);
ndarray_copy_array(source, target, source->itemsize / 2);
return MP_OBJ_FROM_PTR(target);
}
} else {
mp_raise_NotImplementedError(translate("function is implemented for ndarrays only"));
}
return mp_const_none;
}

MP_DEFINE_CONST_FUN_OBJ_1(carray_imag_obj, carray_imag);

#endif
4 changes: 2 additions & 2 deletions code/numpy/carray/carray.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef _CARRAY_
#define _CARRAY_

mp_obj_t carray_real(mp_obj_t);
mp_obj_t carray_imag(mp_obj_t);
MP_DECLARE_CONST_FUN_OBJ_1(carray_real_obj);
MP_DECLARE_CONST_FUN_OBJ_1(carray_imag_obj);

#endif
1 change: 1 addition & 0 deletions code/numpy/numpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
#endif
#if ULAB_SUPPORTS_COMPLEX
{ MP_OBJ_NEW_QSTR(MP_QSTR_real), (mp_obj_t)&carray_real_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_imag), (mp_obj_t)&carray_imag_obj },
#endif

};
Expand Down