From: Paul Brossier Date: Wed, 2 Jan 2019 21:58:45 +0000 (+0100) Subject: [tensor] add print helpers X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=49e7171ccef996308b880d2469e5b9d52647f646;p=aubio.git [tensor] add print helpers --- diff --git a/src/ai/tensor.c b/src/ai/tensor.c index b52418f0..58f629cd 100644 --- a/src/ai/tensor.c +++ b/src/ai/tensor.c @@ -2,6 +2,13 @@ #include "fmat.h" #include "tensor.h" +#define STRN_LENGTH 40 +#if !HAVE_AUBIO_DOUBLE +#define AUBIO_SMPL_TFMT "% 9.4f" +#else +#define AUBIO_SMPL_TFMT "% 9.4lf" +#endif /* HAVE_AUBIO_DOUBLE */ + aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape) { aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t); @@ -150,3 +157,29 @@ const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t) { "%s)", shape_str_previous); return shape_str; } + +static void aubio_tensor_print_subtensor(aubio_tensor_t *t, uint_t depth) +{ + uint_t i; + AUBIO_MSG("["); + for (i = 0; i < t->shape[0]; i ++) { + AUBIO_MSG("%*s", i == 0 ? 0 : depth + 1, i == 0 ? "" : " "); + if (t->ndim == 1) { + AUBIO_MSG(AUBIO_SMPL_TFMT, t->buffer[i]); + } else { + aubio_tensor_t st; + aubio_tensor_get_subtensor(t, i, &st); + aubio_tensor_print_subtensor(&st, depth + 1); // recursive call + } + AUBIO_MSG("%s%s", (i < t->shape[0] - 1) ? "," : "", + t->ndim == 1 ? " " : ((i < t->shape[0] - 1) ? "\n" : "")); + } + AUBIO_MSG("]"); +} + +void aubio_tensor_print(aubio_tensor_t *t) +{ + AUBIO_MSG("tensor of shape %s\n", aubio_tensor_get_shape_string(t)); + aubio_tensor_print_subtensor(t, 0); + AUBIO_MSG("\n"); +}