| 175 | using arrayfire::sparseConvertStorage; |
| 176 | |
| 177 | af_err af_create_sparse_array(af_array *out, const dim_t nRows, |
| 178 | const dim_t nCols, const af_array values, |
| 179 | const af_array rowIdx, const af_array colIdx, |
| 180 | const af_storage stype) { |
| 181 | try { |
| 182 | // Checks: |
| 183 | // rowIdx and colIdx arrays are of s32 type |
| 184 | // values is of floating point type |
| 185 | // if COO, rowIdx, colIdx and values should have same dims |
| 186 | // if CRS, colIdx and values should have same dims, rowIdx.dims = nRows |
| 187 | // if CRC, rowIdx and values should have same dims, colIdx.dims = nCols |
| 188 | // stype is within acceptable range |
| 189 | // type is floating type |
| 190 | |
| 191 | if (!(stype == AF_STORAGE_CSR || stype == AF_STORAGE_CSC || |
| 192 | stype == AF_STORAGE_COO)) { |
| 193 | AF_ERROR("Storage type is out of range/unsupported", AF_ERR_ARG); |
| 194 | } |
| 195 | |
| 196 | const ArrayInfo &vInfo = getInfo(values); |
| 197 | const ArrayInfo &rInfo = getInfo(rowIdx); |
| 198 | const ArrayInfo &cInfo = getInfo(colIdx); |
| 199 | |
| 200 | TYPE_ASSERT(vInfo.isFloating()); |
| 201 | DIM_ASSERT(3, vInfo.isLinear()); |
| 202 | ARG_ASSERT(4, rInfo.getType() == s32); |
| 203 | DIM_ASSERT(4, rInfo.isLinear()); |
| 204 | ARG_ASSERT(5, cInfo.getType() == s32); |
| 205 | DIM_ASSERT(5, cInfo.isLinear()); |
| 206 | |
| 207 | const dim_t nNZ = vInfo.elements(); |
| 208 | if (stype == AF_STORAGE_COO) { |
| 209 | DIM_ASSERT(4, rInfo.elements() == nNZ); |
| 210 | DIM_ASSERT(5, cInfo.elements() == nNZ); |
| 211 | } else if (stype == AF_STORAGE_CSR) { |
| 212 | DIM_ASSERT(4, (dim_t)rInfo.elements() == nRows + 1); |
| 213 | DIM_ASSERT(5, cInfo.elements() == nNZ); |
| 214 | } else if (stype == AF_STORAGE_CSC) { |
| 215 | DIM_ASSERT(4, rInfo.elements() == nNZ); |
| 216 | DIM_ASSERT(5, (dim_t)cInfo.elements() == nCols + 1); |
| 217 | } |
| 218 | |
| 219 | af_array output = nullptr; |
| 220 | |
| 221 | dim4 dims(nRows, nCols); |
| 222 | |
| 223 | switch (vInfo.getType()) { |
| 224 | case f32: |
| 225 | output = createSparseArrayFromData<float>(dims, values, rowIdx, |
| 226 | colIdx, stype); |
| 227 | break; |
| 228 | case f64: |
| 229 | output = createSparseArrayFromData<double>(dims, values, rowIdx, |
| 230 | colIdx, stype); |
| 231 | break; |
| 232 | case c32: |
| 233 | output = createSparseArrayFromData<cfloat>(dims, values, rowIdx, |
| 234 | colIdx, stype); |