From 53b85a5e6748bc38db155f88630d6b8120a58eae Mon Sep 17 00:00:00 2001 From: uditgulati Date: Mon, 27 May 2019 23:05:12 +0530 Subject: [PATCH 1/2] Fix minor typo --- ext/sparse.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ext/sparse.c b/ext/sparse.c index 3079ace..1c79255 100644 --- a/ext/sparse.c +++ b/ext/sparse.c @@ -1,7 +1,6 @@ //allocates memory to the given sparse matrix object -VALUE nm_sparse_alloc(VALUE klass) -{ +VALUE nm_sparse_alloc(VALUE klass){ csr_nmatrix* mat = ALLOC(csr_nmatrix); return Data_Wrap_Struct(klass, NULL, nm_free, mat); @@ -388,7 +387,7 @@ VALUE nm_sparse_to_array(VALUE self){ size_t count = input->count; VALUE* array = ALLOC_N(VALUE, input->count); - switch (input->dtype) { + switch (input->sptype) { case coo: { double* elements = ALLOC_N(double, count); From 67ebc85f5d4ec9cce910bc206b5a98e2269ddf41 Mon Sep 17 00:00:00 2001 From: uditgulati Date: Mon, 27 May 2019 23:25:49 +0530 Subject: [PATCH 2/2] Add nm_bool to dia_sparse_nmatrix_init and add sptype switch to nm_sparse_to_nmatrix --- ext/sparse.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 83 insertions(+), 9 deletions(-) diff --git a/ext/sparse.c b/ext/sparse.c index 1c79255..1914567 100644 --- a/ext/sparse.c +++ b/ext/sparse.c @@ -305,6 +305,15 @@ VALUE dia_sparse_nmatrix_init(int argc, VALUE* argv){ } switch(mat->dtype){ + case nm_bool: + { + bool* elements = ALLOC_N(bool, (size_t)RARRAY_LEN(argv[1])); + for (size_t index = 0; index < (size_t)RARRAY_LEN(argv[1]); index++) { + elements[index] = (bool)NUM2DBL(RARRAY_AREF(argv[1], index)); + } + mat->diag->elements = elements; + break; + } case nm_int: { int* elements = ALLOC_N(int, (size_t)RARRAY_LEN(argv[1])); @@ -464,10 +473,65 @@ VALUE nm_sparse_to_nmatrix(VALUE self){ result->shape[1] = input->shape[1]; result->count = input->count; + void* elements; + switch (input->dtype) { + case nm_bool: + { + bool* temp_elements = ALLOC_N(bool, result->count); + elements = temp_elements; + } + case nm_int: + { + int* temp_elements = ALLOC_N(int, result->count); + elements = temp_elements; + } + case nm_float32: + { + float* temp_elements = ALLOC_N(float, result->count); + elements = temp_elements; + } case nm_float64: { - double* elements = ALLOC_N(double, result->count); + double* temp_elements = ALLOC_N(double, result->count); + elements = temp_elements; + } + case nm_complex32: + { + float complex* temp_elements = ALLOC_N(float complex, result->count); + elements = temp_elements; + } + case nm_complex64: + { + float complex* temp_elements = ALLOC_N(float complex, result->count); + elements = temp_elements; + } + } + switch (input->sptype) { + case coo: + { + get_dense_from_coo(input->coo->elements, + input->shape[0], + input->shape[1], + input->coo->ia, + input->coo->ja, + elements); + result->elements = elements; + break; + } + case csc: + { + get_dense_from_csc(input->csc->elements, + input->shape[0], + input->shape[1], + input->csc->ia, + input->csc->ja, + elements); + result->elements = elements; + break; + } + case csr: + { get_dense_from_csr(input->csr->elements, input->shape[0], input->shape[1], @@ -477,15 +541,25 @@ VALUE nm_sparse_to_nmatrix(VALUE self){ result->elements = elements; break; } + case dia: + { + get_dense_from_dia(input->diag->elements, + input->shape[0], + input->shape[1], + input->diag->offset, + elements); + result->elements = elements; + break; + } } return Data_Wrap_Struct(NMatrix, NULL, nm_free, result); } //extracts elements from coo type sparse matrix //called by nm_sparse_to_nmatrix to get elements list -void get_dense_from_coo(const double* data, const size_t rows, +void get_dense_from_coo(const void* data, const size_t rows, const size_t cols, const size_t* ia, - const size_t* ja, double* elements){ + const size_t* ja, void* elements){ for(size_t i = 0; i < rows*cols; ++i){ elements[i] = 0; } size_t index = 0; @@ -500,9 +574,9 @@ void get_dense_from_coo(const double* data, const size_t rows, //extracts elements from csc type sparse matrix //called by nm_sparse_to_nmatrix to get elements list -void get_dense_from_csc(const double* data, const size_t rows, +void get_dense_from_csc(const void* data, const size_t rows, const size_t cols, const size_t* ia, - const size_t* ja, double* elements){ + const size_t* ja, void* elements){ for(size_t i = 0; i < rows*cols; ++i){ elements[i] = 0; } size_t index = 0; @@ -517,9 +591,9 @@ void get_dense_from_csc(const double* data, const size_t rows, //extracts elements from csr type sparse matrix //called by nm_sparse_to_nmatrix to get elements list -void get_dense_from_csr(const double* data, const size_t rows, +void get_dense_from_csr(const void* data, const size_t rows, const size_t cols, const size_t* ia, - const size_t* ja, double* elements){ + const size_t* ja, void* elements){ for(size_t i = 0; i < rows*cols; ++i){ elements[i] = 0; } size_t index = 0; @@ -534,9 +608,9 @@ void get_dense_from_csr(const double* data, const size_t rows, //extracts elements from dia type sparse matrix //called by nm_sparse_to_nmatrix to get elements list -void get_dense_from_dia(const double* data, const size_t rows, +void get_dense_from_dia(const void* data, const size_t rows, const size_t cols, const size_t* offset, - double* elements){ + void* elements){ for(size_t i = 0; i < rows*cols; ++i){ elements[i] = 0; } size_t index = 0;