[4c33f81] | 1 | /* |
---|
| 2 | Copyright (C) 2018 Paul Brossier <piem@aubio.org> |
---|
| 3 | |
---|
| 4 | This file is part of aubio. |
---|
| 5 | |
---|
| 6 | aubio is free software: you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | (at your option) any later version. |
---|
| 10 | |
---|
| 11 | aubio is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #ifndef AUBIO_TENSOR_H |
---|
| 22 | #define AUBIO_TENSOR_H |
---|
| 23 | |
---|
[40520ea] | 24 | /** \file |
---|
| 25 | |
---|
| 26 | Tensor for real-valued data. |
---|
| 27 | |
---|
| 28 | */ |
---|
| 29 | |
---|
[ddd124a] | 30 | #ifdef __cplusplus |
---|
| 31 | extern "C" { |
---|
| 32 | #endif |
---|
| 33 | |
---|
| 34 | /** Maximum number of dimensions |
---|
| 35 | |
---|
| 36 | This is defined as a constant so that ::aubio_tensor_t allocated |
---|
| 37 | on the stack can be manipulated. |
---|
| 38 | |
---|
| 39 | */ |
---|
[40520ea] | 40 | #define AUBIO_TENSOR_MAXDIM 10 |
---|
| 41 | |
---|
| 42 | /** Tensor for real-valued data |
---|
| 43 | |
---|
| 44 | This object holds a tensor of real-valued data, ::smpl_t, with up to |
---|
| 45 | AUBIO_TENSOR_MAXDIM dimentsions. |
---|
| 46 | |
---|
[ddd124a] | 47 | Todo: |
---|
| 48 | - add array of strides |
---|
| 49 | - add accessors macros |
---|
| 50 | - consider removing data |
---|
| 51 | |
---|
[40520ea] | 52 | */ |
---|
| 53 | typedef struct |
---|
| 54 | { |
---|
| 55 | uint_t ndim; /**< number of dimensions */ |
---|
| 56 | uint_t shape[AUBIO_TENSOR_MAXDIM]; /**< dimensions array */ |
---|
| 57 | uint_t size; /**< total number of elements */ |
---|
| 58 | smpl_t *buffer; /**< buffer of values */ |
---|
| 59 | smpl_t **data; /**< pointer to rows, or NULL when subtensor */ |
---|
[4c33f81] | 60 | } aubio_tensor_t; |
---|
| 61 | |
---|
[f7838fc] | 62 | /** create a new tensor |
---|
| 63 | |
---|
| 64 | \param ndim number of dimensions |
---|
| 65 | \param shape array of dimensions |
---|
| 66 | |
---|
| 67 | \return new ::aubio_tensor_t |
---|
| 68 | |
---|
| 69 | */ |
---|
[5010e61] | 70 | aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape); |
---|
[4c33f81] | 71 | |
---|
[f7838fc] | 72 | /** destroy a tensor |
---|
| 73 | |
---|
| 74 | \param c tensor to destroy |
---|
| 75 | |
---|
| 76 | */ |
---|
[4c33f81] | 77 | void del_aubio_tensor(aubio_tensor_t *c); |
---|
| 78 | |
---|
[f7838fc] | 79 | /** view tensor as a vector |
---|
| 80 | |
---|
| 81 | \param c tensor to view as ::fvec_t |
---|
| 82 | \param o pointer to use to store view |
---|
| 83 | |
---|
| 84 | \return 0 on success, non-zero otherwise |
---|
| 85 | |
---|
| 86 | */ |
---|
[4c33f81] | 87 | uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o); |
---|
[f7838fc] | 88 | |
---|
| 89 | /** view vector as a tensor |
---|
| 90 | |
---|
| 91 | \param o ::fvec_t to view as a tensor |
---|
| 92 | \param c pointer to use to store view |
---|
| 93 | |
---|
| 94 | \return 0 on success, non-zero otherwise |
---|
| 95 | |
---|
| 96 | */ |
---|
[4c33f81] | 97 | uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c); |
---|
| 98 | |
---|
[f7838fc] | 99 | /** view tensor as a matrix |
---|
| 100 | |
---|
| 101 | \param c tensor to view as ::fmat_t |
---|
| 102 | \param o pointer to use to store view |
---|
| 103 | |
---|
| 104 | \return 0 on success, non-zero otherwise |
---|
| 105 | |
---|
| 106 | */ |
---|
[4c33f81] | 107 | uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o); |
---|
[f7838fc] | 108 | |
---|
| 109 | /** view matrix as a tensor |
---|
| 110 | |
---|
| 111 | \param o ::fmat_t to view as a tensor |
---|
| 112 | \param c pointer to use to store view |
---|
| 113 | |
---|
| 114 | \return 0 on success, non-zero otherwise |
---|
| 115 | |
---|
| 116 | */ |
---|
[4c33f81] | 117 | uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c); |
---|
| 118 | |
---|
[f7838fc] | 119 | /** view i-th row of tensor t as a tensor |
---|
| 120 | |
---|
| 121 | \param t tensor to get maximum from |
---|
| 122 | \param i index of row to retrieve |
---|
| 123 | \param st subtensor to fill in |
---|
| 124 | |
---|
| 125 | \return 0 on success, non-zero otherwise |
---|
| 126 | */ |
---|
[7e0b641] | 127 | uint_t aubio_tensor_get_subtensor(aubio_tensor_t *t, uint_t i, |
---|
| 128 | aubio_tensor_t *st); |
---|
| 129 | |
---|
[f7838fc] | 130 | /** find the maximum value of a tensor |
---|
| 131 | |
---|
| 132 | \param t tensor to get maximum from |
---|
| 133 | |
---|
| 134 | \return maximum value of all elements in tensor |
---|
| 135 | */ |
---|
[4c33f81] | 136 | smpl_t aubio_tensor_max(aubio_tensor_t *t); |
---|
| 137 | |
---|
[f7838fc] | 138 | /** check if sizes of 2 tensor match |
---|
| 139 | |
---|
| 140 | \param t first tensor to check size with |
---|
| 141 | \param s second tensor to check size with |
---|
| 142 | |
---|
| 143 | \return 1 if tensors have the same size, 0 otherwise |
---|
| 144 | */ |
---|
[e2010b3] | 145 | uint_t aubio_tensor_have_same_shape(aubio_tensor_t *t, aubio_tensor_t *s); |
---|
[1343aca] | 146 | |
---|
[f7838fc] | 147 | /** print the content of a tensor |
---|
| 148 | |
---|
| 149 | \param t tensor to print |
---|
| 150 | |
---|
| 151 | */ |
---|
[1343aca] | 152 | void aubio_tensor_print(aubio_tensor_t *t); |
---|
| 153 | |
---|
[f7838fc] | 154 | /** get a string representing the dimensions of this tensor |
---|
| 155 | |
---|
| 156 | \param t tensor to get shape string from |
---|
| 157 | |
---|
| 158 | \return string of characters containing the dimensions of t |
---|
| 159 | */ |
---|
[1343aca] | 160 | const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t); |
---|
[4c33f81] | 161 | |
---|
[cc74a29] | 162 | /** compute matrix multiplication A x B = C |
---|
| 163 | |
---|
| 164 | \param a left tensor |
---|
| 165 | \param b right tensor |
---|
| 166 | \param c output tensor |
---|
| 167 | |
---|
| 168 | */ |
---|
[6006760] | 169 | void aubio_tensor_matmul(aubio_tensor_t *a, aubio_tensor_t *b, |
---|
| 170 | aubio_tensor_t *c); |
---|
| 171 | |
---|
[cc74a29] | 172 | /** copy tensor |
---|
| 173 | |
---|
| 174 | \param s source tensor |
---|
| 175 | \param d destination tensor |
---|
| 176 | |
---|
| 177 | */ |
---|
| 178 | void aubio_tensor_copy(aubio_tensor_t *s, aubio_tensor_t *d); |
---|
| 179 | |
---|
[4c33f81] | 180 | #ifdef __cplusplus |
---|
| 181 | } |
---|
| 182 | #endif |
---|
| 183 | |
---|
| 184 | #endif /* AUBIO_TENSOR_H */ |
---|