From 7aab5b3184f0ad8f47ba05df949fb54f680e460d Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Tue, 21 Jun 2016 22:53:14 +0530 Subject: [PATCH 01/28] Classes for Matrices --- ext/symengine/symengine.c | 7 +++++++ ext/symengine/symengine.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 98908f7..e79b00b 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -224,6 +224,13 @@ void Init_symengine() rb_define_module_function(m_symengine, "fibonacci", cntheory_fibonacci, 1); rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1); rb_define_module_function(m_symengine, "binomial", cntheory_binomial, 2); + + // MatrixBase Class + c_matrix_base = rb_define_class_under(m_symengine, "MatrixBase", rb_cObject); + + // DenseMatrix and SparseMatrix Classes + c_dense_matrix = rb_define_class_under(m_symengine, "DenseMatrix", c_matrix_base); + c_sparse_matrix = rb_define_class_under(m_symengine, "SparseMatrix", c_matrix_base); symengine_print_stack_on_segfault(); } diff --git a/ext/symengine/symengine.h b/ext/symengine/symengine.h index 9284f61..ef4cf72 100644 --- a/ext/symengine/symengine.h +++ b/ext/symengine/symengine.h @@ -59,5 +59,8 @@ VALUE c_atanh; VALUE c_acsch; VALUE c_asech; VALUE c_acoth; +VALUE c_matrix_base; +VALUE c_dense_matrix; +VALUE c_sparse_matrix; #endif // SYMENGINE_H_ From 11badd1562c8812190ba422ce3261a97db584ea5 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Tue, 21 Jun 2016 23:10:52 +0530 Subject: [PATCH 02/28] Alloc and Free methods for MatrixBase --- ext/symengine/CMakeLists.txt | 1 + ext/symengine/ruby_matrix.c | 13 +++++++++++++ ext/symengine/ruby_matrix.h | 10 ++++++++++ ext/symengine/symengine.c | 1 + 4 files changed, 25 insertions(+) create mode 100644 ext/symengine/ruby_matrix.c create mode 100644 ext/symengine/ruby_matrix.h diff --git a/ext/symengine/CMakeLists.txt b/ext/symengine/CMakeLists.txt index 5c85b3a..212c8f9 100644 --- a/ext/symengine/CMakeLists.txt +++ b/ext/symengine/CMakeLists.txt @@ -10,6 +10,7 @@ set(RUBY_WRAPPER_SRC ruby_function.c ruby_ntheory.c ruby_utils.c + ruby_matrix.c symengine_utils.c symengine.c ) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c new file mode 100644 index 0000000..de6e03d --- /dev/null +++ b/ext/symengine/ruby_matrix.c @@ -0,0 +1,13 @@ +#include "ruby_matrix.h" + +void cmatrix_free(void *ptr) +{ + CMatrixBase *mat_ptr = ptr; + matrix_base_free(mat_ptr); +} + +VALUE cmatrix_alloc(VALUE klass) +{ + CMatrixBase *mat_ptr = matrix_base_new(); + return Data_Wrap_Struct(klass, NULL, cmatrix_free, mat_ptr); +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h new file mode 100644 index 0000000..c1eb507 --- /dev/null +++ b/ext/symengine/ruby_matrix.h @@ -0,0 +1,10 @@ +#ifndef RUBY_MATRIX_H_ +#define RUBY_MATRIX_H_ + +#include +#include + +void cmatrix_free(void *ptr); +VALUE cmatrix_alloc(VALUE klass); + +#endif // RUBY_MATRIX_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index e79b00b..53d9149 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -10,6 +10,7 @@ #include "ruby_function.h" #include "ruby_ntheory.h" #include "ruby_utils.h" +#include "ruby_matrix.h" #include "symengine_utils.h" #include "symengine.h" From 7f918e05facabc5cdbe25afa257b512fcba0fe6d Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Wed, 22 Jun 2016 00:35:25 +0530 Subject: [PATCH 03/28] DenseMatrix alloc and free funcs --- ext/symengine/ruby_matrix.c | 23 +++++++++++++++++------ ext/symengine/ruby_matrix.h | 5 +++-- ext/symengine/symengine.c | 3 +++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index de6e03d..d9949ba 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -1,13 +1,24 @@ #include "ruby_matrix.h" -void cmatrix_free(void *ptr) +void cmatrix_dense_free(void *ptr) { - CMatrixBase *mat_ptr = ptr; - matrix_base_free(mat_ptr); + CDenseMatrix *mat_ptr = ptr; + dense_matrix_free(mat_ptr); } -VALUE cmatrix_alloc(VALUE klass) +VALUE cmatrix_dense_alloc(VALUE klass) { - CMatrixBase *mat_ptr = matrix_base_new(); - return Data_Wrap_Struct(klass, NULL, cmatrix_free, mat_ptr); + CDenseMatrix *mat_ptr = dense_matrix_new(); + return Data_Wrap_Struct(klass, NULL, cmatrix_dense_free, mat_ptr); +} + + +VALUE cmatrix_dense_init(VALUE self) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + dense_matrix(this); + + return self; } diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index c1eb507..5f4de8d 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -4,7 +4,8 @@ #include #include -void cmatrix_free(void *ptr); -VALUE cmatrix_alloc(VALUE klass); +void cmatrix_dense_free(void *ptr); +VALUE cmatrix_dense_alloc(VALUE klass); +VALUE cmatrix_dense_init(VALUE self); #endif // RUBY_MATRIX_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 53d9149..4258388 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -232,6 +232,9 @@ void Init_symengine() // DenseMatrix and SparseMatrix Classes c_dense_matrix = rb_define_class_under(m_symengine, "DenseMatrix", c_matrix_base); c_sparse_matrix = rb_define_class_under(m_symengine, "SparseMatrix", c_matrix_base); + + // DenseMatrix Methods + rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, 0); symengine_print_stack_on_segfault(); } From 8de5d9f706ad85df2d51b0deff3b5abd1eba458f Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 23 Jun 2016 01:31:42 +0530 Subject: [PATCH 04/28] DenseMatrix() and SymEngine::DenseMatrix(no_rows, no_cols) working --- ext/symengine/ruby_matrix.c | 42 +++++++++++++++++++++++++++++++++++-- ext/symengine/ruby_matrix.h | 2 +- ext/symengine/symengine.c | 3 ++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index d9949ba..4726854 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -13,12 +13,50 @@ VALUE cmatrix_dense_alloc(VALUE klass) } -VALUE cmatrix_dense_init(VALUE self) +VALUE cmatrix_dense_init(VALUE self, VALUE args) { + int argc = RARRAY_LEN(args); CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - dense_matrix(this); + if (argc == 0) { + // SymEngine::DenseMatrix() + dense_matrix(this); + } else if (argc == 1) { + // SymEngine::DenseMatrix(SymEngine::DenseMatrix) OR + // SymEngine::DenseMatrix(NMatrix) OR + // SymEngine::DenseMatrix(Array) + VALUE operand = rb_ary_shift(args); + char *s = rb_obj_classname(operand); + printf("class name: %s\n", s); + if(strcmp(s, "SymEngine::DenseMatrix") == 0) { + // SymEngine::DenseMatrix(SymEngine::DenseMatrix) + + } else if(strcmp(s, "NMatrix") == 0) { + // SymEngine::DenseMatrix(SymEngine::DenseMatrix) + + } else if(strcmp(s, "Array") == 0) { + // SymEngine::DenseMatrix(SymEngine::DenseMatrix) + + } else { + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); + } + + } else if (argc == 2) { + // SymEngine::DenseMatrix(no_rows, no_cols) + VALUE val1 = rb_ary_shift(args); + VALUE val2 = rb_ary_shift(args); + if((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) + && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM) ) { + unsigned long int rows = NUM2ULONG(val1); + unsigned long int cols = NUM2ULONG(val2); + dense_matrix_rows_cols(this, rows, cols); + } else { + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); + } + } else { + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); + } return self; } diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 5f4de8d..9c9a187 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -6,6 +6,6 @@ void cmatrix_dense_free(void *ptr); VALUE cmatrix_dense_alloc(VALUE klass); -VALUE cmatrix_dense_init(VALUE self); +VALUE cmatrix_dense_init(VALUE self, VALUE args); #endif // RUBY_MATRIX_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 4258388..835b8b9 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -234,7 +234,8 @@ void Init_symengine() c_sparse_matrix = rb_define_class_under(m_symengine, "SparseMatrix", c_matrix_base); // DenseMatrix Methods - rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, 0); + rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc); + rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, -2); symengine_print_stack_on_segfault(); } From 0b897b92575361186785e2bab22a41d2deeaf19e Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 23 Jun 2016 02:06:50 +0530 Subject: [PATCH 05/28] DenseMatrix(Array) and DenseMatrix(SymEngine::DenseMatrix) working --- ext/symengine/ruby_matrix.c | 53 ++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 4726854..1c42d11 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -20,42 +20,89 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) Data_Get_Struct(self, CDenseMatrix, this); if (argc == 0) { + // SymEngine::DenseMatrix() dense_matrix(this); + } else if (argc == 1) { // SymEngine::DenseMatrix(SymEngine::DenseMatrix) OR // SymEngine::DenseMatrix(NMatrix) OR // SymEngine::DenseMatrix(Array) + VALUE operand = rb_ary_shift(args); char *s = rb_obj_classname(operand); printf("class name: %s\n", s); + if(strcmp(s, "SymEngine::DenseMatrix") == 0) { - // SymEngine::DenseMatrix(SymEngine::DenseMatrix) - } else if(strcmp(s, "NMatrix") == 0) { // SymEngine::DenseMatrix(SymEngine::DenseMatrix) + CDenseMatrix *temp; + Data_Get_Struct(operand, CDenseMatrix, temp); + dense_matrix_set(this, temp); + + } else if(strcmp(s, "NMatrix") == 0) { + // SymEngine::DenseMatrix(NMatrix) + rb_raise(rb_eTypeError, "TODO"); + } else if(strcmp(s, "Array") == 0) { - // SymEngine::DenseMatrix(SymEngine::DenseMatrix) + // SymEngine::DenseMatrix(Array) + int counter = 0; + + int rows = RARRAY_LEN(operand); + int cols = -1; + CVecBasic *cargs = vecbasic_new(); + basic x; + basic_new_stack(x); + int i; + for (i = 0; i < rows; i++) { + int j; + VALUE row = rb_ary_shift(operand); + if ( cols == -1 ) { + cols = RARRAY_LEN(row); + // Checking all rows for same col length + } else if (cols != RARRAY_LEN(row)) { + rb_raise(rb_eTypeError, "2D Array's rows contain different column counts"); + } + + for(j = 0; j < cols; j++) { + sympify(rb_ary_shift(row), x); + vecbasic_push_back(cargs, x); + counter++; + } + } + dense_matrix_set_vec(this, rows, cols, cargs); + + basic_free_stack(x); + vecbasic_free(cargs); + } else { rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); } } else if (argc == 2) { + // SymEngine::DenseMatrix(no_rows, no_cols) VALUE val1 = rb_ary_shift(args); VALUE val2 = rb_ary_shift(args); + if((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM) ) { + unsigned long int rows = NUM2ULONG(val1); unsigned long int cols = NUM2ULONG(val2); dense_matrix_rows_cols(this, rows, cols); + } else { + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); + } } else { + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); + } return self; From 8f50218ec0bbe94df6b574675447f6d78105f110 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 23 Jun 2016 11:40:51 +0530 Subject: [PATCH 06/28] inspect and to_s for MatrixBase --- ext/symengine/ruby_matrix.c | 16 +++++++++++++++- ext/symengine/ruby_matrix.h | 1 + ext/symengine/symengine.c | 1 + lib/symengine.rb | 1 + lib/symengine/matrix_base.rb | 7 +++++++ 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 lib/symengine/matrix_base.rb diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 1c42d11..9560de7 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -12,6 +12,21 @@ VALUE cmatrix_dense_alloc(VALUE klass) return Data_Wrap_Struct(klass, NULL, cmatrix_dense_free, mat_ptr); } +VALUE cmatrix_to_str(VALUE self) +{ + CDenseMatrix *this; + char *str_ptr; + VALUE result; + + Data_Get_Struct(self, CDenseMatrix, this); + + str_ptr = matrix_str(this); + result = rb_str_new_cstr(str_ptr); + basic_str_free(str_ptr); + + return result; +} + VALUE cmatrix_dense_init(VALUE self, VALUE args) { @@ -31,7 +46,6 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) VALUE operand = rb_ary_shift(args); char *s = rb_obj_classname(operand); - printf("class name: %s\n", s); if(strcmp(s, "SymEngine::DenseMatrix") == 0) { diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 9c9a187..6f40afc 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -7,5 +7,6 @@ void cmatrix_dense_free(void *ptr); VALUE cmatrix_dense_alloc(VALUE klass); VALUE cmatrix_dense_init(VALUE self, VALUE args); +VALUE cmatrix_to_str(VALUE self); #endif // RUBY_MATRIX_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 835b8b9..f81eb07 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -236,6 +236,7 @@ void Init_symengine() // DenseMatrix Methods rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc); rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, -2); + rb_define_method(c_dense_matrix, "to_s", cmatrix_to_str, 0); symengine_print_stack_on_segfault(); } diff --git a/lib/symengine.rb b/lib/symengine.rb index e4d3477..d2c246c 100644 --- a/lib/symengine.rb +++ b/lib/symengine.rb @@ -40,3 +40,4 @@ def Function(n) require 'symengine/complex' require 'symengine/complex_double' require 'symengine/undef_function' +require 'symengine/matrix_base' diff --git a/lib/symengine/matrix_base.rb b/lib/symengine/matrix_base.rb new file mode 100644 index 0000000..7e751b9 --- /dev/null +++ b/lib/symengine/matrix_base.rb @@ -0,0 +1,7 @@ +module SymEngine + class MatrixBase + def inspect + "#<#{self.class}(#{to_s})>" + end + end +end From b71912647f64f52ecb990f9de9e36b733c6b101a Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 23 Jun 2016 22:52:00 +0530 Subject: [PATCH 07/28] Changed dense_matrix to dense_matrix_init --- ext/symengine/ruby_matrix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 9560de7..c681c14 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -37,7 +37,7 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) if (argc == 0) { // SymEngine::DenseMatrix() - dense_matrix(this); + dense_matrix_init(this); } else if (argc == 1) { // SymEngine::DenseMatrix(SymEngine::DenseMatrix) OR From d7c8b29629894086431434d25da82a45d2334acb Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 24 Jun 2016 00:25:04 +0530 Subject: [PATCH 08/28] SparseMatrix ruby wrappers --- ext/symengine/ruby_matrix.c | 69 ++++++++++++++++++++++++++++++++++-- ext/symengine/ruby_matrix.h | 7 +++- ext/symengine/symengine.c | 8 ++++- lib/symengine/matrix_base.rb | 2 +- 4 files changed, 81 insertions(+), 5 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index c681c14..fd866a7 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -6,13 +6,25 @@ void cmatrix_dense_free(void *ptr) dense_matrix_free(mat_ptr); } +void cmatrix_sparse_free(void *ptr) +{ + CSparseMatrix *mat_ptr = ptr; + sparse_matrix_free(mat_ptr); +} + VALUE cmatrix_dense_alloc(VALUE klass) { CDenseMatrix *mat_ptr = dense_matrix_new(); return Data_Wrap_Struct(klass, NULL, cmatrix_dense_free, mat_ptr); } -VALUE cmatrix_to_str(VALUE self) +VALUE cmatrix_sparse_alloc(VALUE klass) +{ + CSparseMatrix *mat_ptr = sparse_matrix_new(); + return Data_Wrap_Struct(klass, NULL, cmatrix_sparse_free, mat_ptr); +} + +VALUE cmatrix_dense_to_str(VALUE self) { CDenseMatrix *this; char *str_ptr; @@ -20,7 +32,22 @@ VALUE cmatrix_to_str(VALUE self) Data_Get_Struct(self, CDenseMatrix, this); - str_ptr = matrix_str(this); + str_ptr = dense_matrix_str(this); + result = rb_str_new_cstr(str_ptr); + basic_str_free(str_ptr); + + return result; +} + +VALUE cmatrix_sparse_to_str(VALUE self) +{ + CSparseMatrix *this; + char *str_ptr; + VALUE result; + + Data_Get_Struct(self, CSparseMatrix, this); + + str_ptr = sparse_matrix_str(this); result = rb_str_new_cstr(str_ptr); basic_str_free(str_ptr); @@ -121,3 +148,41 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) return self; } + +VALUE cmatrix_sparse_init(VALUE self, VALUE args) +{ + int argc = RARRAY_LEN(args); + CSparseMatrix *this; + Data_Get_Struct(self, CSparseMatrix, this); + + if (argc == 0) { + + // SymEngine::SparseMatrix() + sparse_matrix_init(this); + + } else if (argc == 2) { + + // SymEngine::SparseMatrix(no_rows, no_cols) + VALUE val1 = rb_ary_shift(args); + VALUE val2 = rb_ary_shift(args); + + if((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) + && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM) ) { + + unsigned long int rows = NUM2ULONG(val1); + unsigned long int cols = NUM2ULONG(val2); + sparse_matrix_rows_cols(this, rows, cols); + + } else { + + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, or two Numerics expected."); + + } + } else { + + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, or two Numerics expected."); + + } + + return self; +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 6f40afc..07dface 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -7,6 +7,11 @@ void cmatrix_dense_free(void *ptr); VALUE cmatrix_dense_alloc(VALUE klass); VALUE cmatrix_dense_init(VALUE self, VALUE args); -VALUE cmatrix_to_str(VALUE self); +VALUE cmatrix_dense_to_str(VALUE self); + +void cmatrix_sparse_free(void *ptr); +VALUE cmatrix_sparse_alloc(VALUE klass); +VALUE cmatrix_sparse_init(VALUE self, VALUE args); +VALUE cmatrix_sparse_to_str(VALUE self); #endif // RUBY_MATRIX_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index f81eb07..0cf5029 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -236,7 +236,13 @@ void Init_symengine() // DenseMatrix Methods rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc); rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, -2); - rb_define_method(c_dense_matrix, "to_s", cmatrix_to_str, 0); + rb_define_method(c_dense_matrix, "to_s", cmatrix_dense_to_str, 0); + + // SparseMatrix Methods + rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc); + rb_define_method(c_sparse_matrix, "initialize", cmatrix_sparse_init, -2); + rb_define_method(c_sparse_matrix, "to_s", cmatrix_sparse_to_str, 0); + symengine_print_stack_on_segfault(); } diff --git a/lib/symengine/matrix_base.rb b/lib/symengine/matrix_base.rb index 7e751b9..4b871a3 100644 --- a/lib/symengine/matrix_base.rb +++ b/lib/symengine/matrix_base.rb @@ -1,7 +1,7 @@ module SymEngine class MatrixBase def inspect - "#<#{self.class}(#{to_s})>" + "#<#{self.class}()>" end end end From fc9db03e47506114b93c924d20bab956dcba787e Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 24 Jun 2016 01:59:12 +0530 Subject: [PATCH 09/28] Getter and setter for DenseMatrix --- ext/symengine/ruby_matrix.c | 43 +++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_matrix.h | 6 ++++++ ext/symengine/symengine.c | 2 ++ 3 files changed, 51 insertions(+) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index fd866a7..8dd551b 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -186,3 +186,46 @@ VALUE cmatrix_sparse_init(VALUE self, VALUE args) return self; } + + +VALUE cmatrix_dense_get(VALUE self, VALUE r, VALUE c) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + basic_struct *cresult; + VALUE result; + + unsigned long cbasic_r; + cbasic_r = NUM2ULONG(r); + unsigned long cbasic_c; + cbasic_c = NUM2ULONG(c); + + cresult = basic_new_heap(); + + dense_matrix_get_basic(cresult, this, cbasic_r, cbasic_c); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, basic_free_heap, + cresult); + return result; +} + +VALUE cmatrix_dense_set(VALUE self, VALUE r, VALUE c, VALUE operand) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + basic cbasic_operand; + basic_new_stack(cbasic_operand); + sympify(operand, cbasic_operand); + + unsigned long cbasic_r; + cbasic_r = NUM2ULONG(r); + unsigned long cbasic_c; + cbasic_c = NUM2ULONG(c); + + dense_matrix_set_basic(this, cbasic_r, cbasic_c, cbasic_operand); + + basic_free_stack(cbasic_operand); + + return self; +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 07dface..14944f4 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -4,10 +4,16 @@ #include #include +#include "symengine.h" +#include "ruby_basic.h" +#include "symengine_utils.h" + void cmatrix_dense_free(void *ptr); VALUE cmatrix_dense_alloc(VALUE klass); VALUE cmatrix_dense_init(VALUE self, VALUE args); VALUE cmatrix_dense_to_str(VALUE self); +VALUE cmatrix_dense_get(VALUE self, VALUE r, VALUE c); +VALUE cmatrix_dense_set(VALUE self, VALUE r, VALUE c, VALUE operand); void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 0cf5029..17eed02 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -237,6 +237,8 @@ void Init_symengine() rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc); rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, -2); rb_define_method(c_dense_matrix, "to_s", cmatrix_dense_to_str, 0); + rb_define_method(c_dense_matrix, "get", cmatrix_dense_get, 2); + rb_define_method(c_dense_matrix, "set", cmatrix_dense_set, 3); // SparseMatrix Methods rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc); From c72348e753d1fc4381d21612d50ce38fb12263d3 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 24 Jun 2016 02:49:49 +0530 Subject: [PATCH 10/28] det, inv, transpose and submatrix --- ext/symengine/ruby_matrix.c | 79 +++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_matrix.h | 4 ++ ext/symengine/symengine.c | 4 ++ 3 files changed, 87 insertions(+) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 8dd551b..e486b51 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -229,3 +229,82 @@ VALUE cmatrix_dense_set(VALUE self, VALUE r, VALUE c, VALUE operand) return self; } + + +VALUE cmatrix_dense_det(VALUE self) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + basic_struct *cresult; + VALUE result; + + cresult = basic_new_heap(); + + dense_matrix_det(cresult, this); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, basic_free_heap, + cresult); + return result; +} +VALUE cmatrix_dense_inv(VALUE self) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult; + VALUE result; + + cresult = dense_matrix_new(); + + dense_matrix_inv(cresult, this); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + return result; +} + +VALUE cmatrix_dense_transpose(VALUE self) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult; + VALUE result; + + cresult = dense_matrix_new(); + + dense_matrix_transpose(cresult, this); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + return result; +} + +VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2, VALUE r_, VALUE c_) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult; + VALUE result; + + unsigned long cbasic_r1; + cbasic_r1 = NUM2ULONG(r1); + unsigned long cbasic_c1; + cbasic_c1 = NUM2ULONG(c1); + + unsigned long cbasic_r2; + cbasic_r2 = NUM2ULONG(r2); + unsigned long cbasic_c2; + cbasic_c2 = NUM2ULONG(c2); + + unsigned long cbasic_r; + cbasic_r = NUM2ULONG(r_); + unsigned long cbasic_c; + cbasic_c = NUM2ULONG(c_); + + cresult = dense_matrix_new(); + + dense_matrix_submatrix(cresult, this, cbasic_r1, cbasic_c1, cbasic_r2, cbasic_c2, cbasic_r, cbasic_c); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + return result; +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 14944f4..90dfd41 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -14,6 +14,10 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args); VALUE cmatrix_dense_to_str(VALUE self); VALUE cmatrix_dense_get(VALUE self, VALUE r, VALUE c); VALUE cmatrix_dense_set(VALUE self, VALUE r, VALUE c, VALUE operand); +VALUE cmatrix_dense_det(VALUE self); +VALUE cmatrix_dense_inv(VALUE self); +VALUE cmatrix_dense_transpose(VALUE self); +VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2, VALUE r_, VALUE c_); void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 17eed02..6081750 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -239,6 +239,10 @@ void Init_symengine() rb_define_method(c_dense_matrix, "to_s", cmatrix_dense_to_str, 0); rb_define_method(c_dense_matrix, "get", cmatrix_dense_get, 2); rb_define_method(c_dense_matrix, "set", cmatrix_dense_set, 3); + rb_define_method(c_dense_matrix, "det", cmatrix_dense_det, 0); + rb_define_method(c_dense_matrix, "inv", cmatrix_dense_inv, 0); + rb_define_method(c_dense_matrix, "transpose", cmatrix_dense_transpose, 0); + rb_define_method(c_dense_matrix, "submatrix", cmatrix_dense_submatrix, 6); // SparseMatrix Methods rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc); From ad72687f87a13bb754a8bbaf2c84da771a397257 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sun, 26 Jun 2016 22:06:52 +0530 Subject: [PATCH 11/28] + operator for DenceMatrix --- ext/symengine/ruby_matrix.c | 52 +++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_matrix.h | 4 +++ ext/symengine/symengine.c | 5 ++++ 3 files changed, 61 insertions(+) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index e486b51..8c8977e 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -308,3 +308,55 @@ VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2 cresult); return result; } + +VALUE cmatrix_dense_rows(VALUE self) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + return ULONG2NUM( dense_matrix_rows(this)); +} + + +VALUE cmatrix_dense_cols(VALUE self) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + return ULONG2NUM( dense_matrix_cols(this)); +} + +VALUE cmatrix_dense_add(VALUE self, VALUE operand) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult; + VALUE result; + + cresult = dense_matrix_new(); + + char *s = rb_obj_classname(operand); + + if(strcmp(s, "SymEngine::DenseMatrix") == 0) { + // Matrix Addition + CDenseMatrix *coperand; + Data_Get_Struct(operand, CDenseMatrix, coperand); + + dense_matrix_add_matrix(cresult, this, coperand); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + + dense_matrix_free(coperand); + } else { + // Scalar Addition + basic_struct *coperand = basic_new_heap(); + sympify(operand, coperand); + + dense_matrix_add_scalar(cresult, this, coperand); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + basic_free_heap(coperand); + } + + return result; +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 90dfd41..f8f4e86 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -18,6 +18,10 @@ VALUE cmatrix_dense_det(VALUE self); VALUE cmatrix_dense_inv(VALUE self); VALUE cmatrix_dense_transpose(VALUE self); VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2, VALUE r_, VALUE c_); +VALUE cmatrix_dense_rows(VALUE self); +VALUE cmatrix_dense_cols(VALUE self); +VALUE cmatrix_dense_add(VALUE self, VALUE operand); +VALUE cmatrix_dense_mul(VALUE self, VALUE operand); void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 6081750..161d84c 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -243,6 +243,11 @@ void Init_symengine() rb_define_method(c_dense_matrix, "inv", cmatrix_dense_inv, 0); rb_define_method(c_dense_matrix, "transpose", cmatrix_dense_transpose, 0); rb_define_method(c_dense_matrix, "submatrix", cmatrix_dense_submatrix, 6); + rb_define_method(c_dense_matrix, "rows", cmatrix_dense_rows, 0); + rb_define_method(c_dense_matrix, "cols", cmatrix_dense_cols, 0); + rb_define_method(c_dense_matrix, "+", cmatrix_dense_add, 1); + //rb_define_method(c_dense_matrix, "*", cmatrix_dense_mul, 1); + // SparseMatrix Methods rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc); From 423259bfd1134001fbda6e86f06e0dc081ff7ee6 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sun, 26 Jun 2016 22:33:58 +0530 Subject: [PATCH 12/28] * operator for DenseMatrix --- ext/symengine/ruby_matrix.c | 37 +++++++++++++++++++++++++++++++++++++ ext/symengine/symengine.c | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 8c8977e..c7bcf3e 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -360,3 +360,40 @@ VALUE cmatrix_dense_add(VALUE self, VALUE operand) return result; } + +VALUE cmatrix_dense_mul(VALUE self, VALUE operand) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult; + VALUE result; + + cresult = dense_matrix_new(); + + char *s = rb_obj_classname(operand); + + if(strcmp(s, "SymEngine::DenseMatrix") == 0) { + // Matrix Multiplication + CDenseMatrix *coperand; + Data_Get_Struct(operand, CDenseMatrix, coperand); + + dense_matrix_mul_matrix(cresult, this, coperand); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + + dense_matrix_free(coperand); + } else { + // Scalar Multiplication + basic_struct *coperand = basic_new_heap(); + sympify(operand, coperand); + + dense_matrix_mul_scalar(cresult, this, coperand); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + basic_free_heap(coperand); + } + + return result; +} diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 161d84c..d981533 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -246,7 +246,7 @@ void Init_symengine() rb_define_method(c_dense_matrix, "rows", cmatrix_dense_rows, 0); rb_define_method(c_dense_matrix, "cols", cmatrix_dense_cols, 0); rb_define_method(c_dense_matrix, "+", cmatrix_dense_add, 1); - //rb_define_method(c_dense_matrix, "*", cmatrix_dense_mul, 1); + rb_define_method(c_dense_matrix, "*", cmatrix_dense_mul, 1); // SparseMatrix Methods From 4c40373b0f4cea76cff7791694e3d6e2e79363a3 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Mon, 27 Jun 2016 15:02:07 +0530 Subject: [PATCH 13/28] LU for DenseMatrix --- ext/symengine/ruby_matrix.c | 50 +++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_matrix.h | 5 ++++ ext/symengine/symengine.c | 1 + 3 files changed, 56 insertions(+) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index c7bcf3e..b7feb5d 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -397,3 +397,53 @@ VALUE cmatrix_dense_mul(VALUE self, VALUE operand) return result; } + + +VALUE cmatrix_dense_LU(VALUE self) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult_l; + VALUE result_l; + cresult_l = dense_matrix_new(); + + CDenseMatrix *cresult_u; + VALUE result_u; + cresult_u = dense_matrix_new(); + + dense_matrix_LU(cresult_l, cresult_u, this); + + result_l = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_l); + result_u = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_u); + + VALUE result; + result = rb_ary_new(); + rb_ary_push(result, result_l); + rb_ary_push(result, result_u); + + return result; +} + +/*VALUE cmatrix_dense_LDL(VALUE self) +{ + +} + +VALUE cmatrix_dense_LU_solve(VALUE self, VALUE b) +{ + +} + +VALUE cmatrix_dense_FFLU(VALUE self) +{ + +} + + +VALUE cmatrix_dense_FFLDU(VALUE self) +{ + +}*/ diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index f8f4e86..7ae6f67 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -22,6 +22,11 @@ VALUE cmatrix_dense_rows(VALUE self); VALUE cmatrix_dense_cols(VALUE self); VALUE cmatrix_dense_add(VALUE self, VALUE operand); VALUE cmatrix_dense_mul(VALUE self, VALUE operand); +VALUE cmatrix_dense_LU(VALUE self); +/*VALUE cmatrix_dense_LDL(VALUE self); +VALUE cmatrix_dense_LU_solve(VALUE self, VALUE b); +VALUE cmatrix_dense_FFLU(VALUE self); +VALUE cmatrix_dense_FFLDU(VALUE self);*/ void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index d981533..4299f65 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -247,6 +247,7 @@ void Init_symengine() rb_define_method(c_dense_matrix, "cols", cmatrix_dense_cols, 0); rb_define_method(c_dense_matrix, "+", cmatrix_dense_add, 1); rb_define_method(c_dense_matrix, "*", cmatrix_dense_mul, 1); + rb_define_method(c_dense_matrix, "LU", cmatrix_dense_LU, 0); // SparseMatrix Methods From e00ff7f80b3495441b56676d70694dc00b8fff5b Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Mon, 27 Jun 2016 23:39:59 +0530 Subject: [PATCH 14/28] LDL, LU_solve, FFLU, FFLDU working --- ext/symengine/ruby_matrix.c | 83 ++++++++++++++++++++++++++++++++++++- ext/symengine/ruby_matrix.h | 4 +- ext/symengine/symengine.c | 4 ++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index b7feb5d..4493052 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -427,23 +427,102 @@ VALUE cmatrix_dense_LU(VALUE self) return result; } -/*VALUE cmatrix_dense_LDL(VALUE self) +VALUE cmatrix_dense_LDL(VALUE self) { + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult_l; + VALUE result_l; + cresult_l = dense_matrix_new(); + + CDenseMatrix *cresult_d; + VALUE result_d; + cresult_d = dense_matrix_new(); + + dense_matrix_LDL(cresult_l, cresult_d, this); + + result_l = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_l); + result_d = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_d); + + VALUE result; + result = rb_ary_new(); + rb_ary_push(result, result_l); + rb_ary_push(result, result_d); + return result; } VALUE cmatrix_dense_LU_solve(VALUE self, VALUE b) { + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult; + VALUE result; + cresult = dense_matrix_new(); + + CDenseMatrix *coperand; + Data_Get_Struct(b, CDenseMatrix, coperand); + dense_matrix_LU_solve(cresult, this, coperand); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + return result; } VALUE cmatrix_dense_FFLU(VALUE self) { + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult_lu; + VALUE result_lu; + cresult_lu = dense_matrix_new(); + + dense_matrix_FFLU(cresult_lu, this); + + result_lu = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_lu); + return result_lu; } VALUE cmatrix_dense_FFLDU(VALUE self) { + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult_l; + VALUE result_l; + cresult_l = dense_matrix_new(); + + CDenseMatrix *cresult_d; + VALUE result_d; + cresult_d = dense_matrix_new(); + + CDenseMatrix *cresult_u; + VALUE result_u; + cresult_u = dense_matrix_new(); + + dense_matrix_LDL(cresult_l, cresult_d, this); + + result_l = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_l); + result_d = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_d); + result_u = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult_u); -}*/ + VALUE result; + result = rb_ary_new(); + rb_ary_push(result, result_l); + rb_ary_push(result, result_d); + rb_ary_push(result, result_u); + + return result; +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 7ae6f67..adf82eb 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -23,10 +23,10 @@ VALUE cmatrix_dense_cols(VALUE self); VALUE cmatrix_dense_add(VALUE self, VALUE operand); VALUE cmatrix_dense_mul(VALUE self, VALUE operand); VALUE cmatrix_dense_LU(VALUE self); -/*VALUE cmatrix_dense_LDL(VALUE self); +VALUE cmatrix_dense_LDL(VALUE self); VALUE cmatrix_dense_LU_solve(VALUE self, VALUE b); VALUE cmatrix_dense_FFLU(VALUE self); -VALUE cmatrix_dense_FFLDU(VALUE self);*/ +VALUE cmatrix_dense_FFLDU(VALUE self); void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 4299f65..82f92e0 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -248,6 +248,10 @@ void Init_symengine() rb_define_method(c_dense_matrix, "+", cmatrix_dense_add, 1); rb_define_method(c_dense_matrix, "*", cmatrix_dense_mul, 1); rb_define_method(c_dense_matrix, "LU", cmatrix_dense_LU, 0); + rb_define_method(c_dense_matrix, "LDL", cmatrix_dense_LDL, 0); + rb_define_method(c_dense_matrix, "LU_solve", cmatrix_dense_LU_solve, 1); + rb_define_method(c_dense_matrix, "FFLU", cmatrix_dense_FFLU, 0); + rb_define_method(c_dense_matrix, "FFLDU", cmatrix_dense_FFLDU, 0); // SparseMatrix Methods From 6d3136955fa4b46ee58363acfcb842ce1e474164 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Tue, 28 Jun 2016 14:05:13 +0530 Subject: [PATCH 15/28] convert() can convert 2D arrays into DenseMatrices --- ext/symengine/ruby_matrix.c | 6 +++++ ext/symengine/ruby_utils.c | 45 ++++++++++++++++++++++++++++++++- ext/symengine/ruby_utils.h | 2 ++ ext/symengine/symengine_utils.c | 2 +- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 4493052..3d0f30b 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -100,6 +100,12 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) for (i = 0; i < rows; i++) { int j; VALUE row = rb_ary_shift(operand); + + s = rb_obj_classname(row); + if(strcmp(s, "Array") != 0){ + rb_raise(rb_eTypeError, "Not a 2D Array"); + } + if ( cols == -1 ) { cols = RARRAY_LEN(row); // Checking all rows for same col length diff --git a/ext/symengine/ruby_utils.c b/ext/symengine/ruby_utils.c index 24ec50d..3da0d19 100644 --- a/ext/symengine/ruby_utils.c +++ b/ext/symengine/ruby_utils.c @@ -4,6 +4,49 @@ VALUE cutils_sympify(VALUE self, VALUE operand) { VALUE result; + char *s = rb_obj_classname(operand); + + if(strcmp(s, "Array") == 0) { + + CDenseMatrix *mat_result = dense_matrix_new(); + int counter = 0; + + int rows = RARRAY_LEN(operand); + int cols = -1; + CVecBasic *cargs = vecbasic_new(); + basic x; + basic_new_stack(x); + int i; + + for (i = 0; i < rows; i++) { + int j; + VALUE row = rb_ary_shift(operand); + s = rb_obj_classname(row); + if(strcmp(s, "Array") != 0){ + rb_raise(rb_eTypeError, "2D Array is required"); + } + + if ( cols == -1 ) { + cols = RARRAY_LEN(row); + // Checking all rows for same col length + } else if (cols != RARRAY_LEN(row)) { + rb_raise(rb_eTypeError, "2D Array's rows contain different column counts"); + } + + for(j = 0; j < cols; j++) { + sympify(rb_ary_shift(row), x); + vecbasic_push_back(cargs, x); + counter++; + } + } + + dense_matrix_set_vec(mat_result, rows, cols, cargs); + + basic_free_stack(x); + vecbasic_free(cargs); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, mat_result); + } else { basic_struct *cbasic_operand; cbasic_operand = basic_new_heap(); @@ -11,6 +54,6 @@ VALUE cutils_sympify(VALUE self, VALUE operand) sympify(operand, cbasic_operand); result = Data_Wrap_Struct(Klass_of_Basic(cbasic_operand), NULL, cbasic_free_heap, cbasic_operand); - + } return result; } diff --git a/ext/symengine/ruby_utils.h b/ext/symengine/ruby_utils.h index de004ff..00d926a 100644 --- a/ext/symengine/ruby_utils.h +++ b/ext/symengine/ruby_utils.h @@ -1,6 +1,8 @@ #ifndef RUBY_UTILS_H_ #define RUBY_UTILS_H_ + +#include "symengine.h" #include "symengine_utils.h" // Returns the Ruby Value after going through sympify diff --git a/ext/symengine/symengine_utils.c b/ext/symengine/symengine_utils.c index f17489a..6b7b80c 100644 --- a/ext/symengine/symengine_utils.c +++ b/ext/symengine/symengine_utils.c @@ -83,7 +83,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) { VALUE ret = check_sympify(operand2, cbasic_operand2); if (ret == Qfalse) { - rb_raise(rb_eTypeError, "%s can't be coerced into SymEngine::Basic", + rb_raise(rb_eTypeError, "%s can't be coerced into a SymEngine type.", rb_obj_classname(operand2)); } } From fb787a1a5559599c7e3be03f2275f0283eda2ce9 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Wed, 29 Jun 2016 00:10:05 +0530 Subject: [PATCH 16/28] numpy-like functions ones and zeros --- ext/symengine/ruby_matrix.c | 45 +++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_matrix.h | 4 ++++ ext/symengine/symengine.c | 6 +++++ 3 files changed, 55 insertions(+) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 3d0f30b..2554e75 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -532,3 +532,48 @@ VALUE cmatrix_dense_FFLDU(VALUE self) return result; } + + +VALUE cmatrix_dense_ones(VALUE self, VALUE r, VALUE c) +{ + unsigned long int r_ = NUM2ULONG(r); + unsigned long int c_ = NUM2ULONG(c); + + CDenseMatrix *cresult; + cresult = dense_matrix_new(); + VALUE result; + + dense_matrix_ones(cresult, r_, c_); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + + return result; +} + +VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c) +{ + unsigned long int r_ = NUM2ULONG(r); + unsigned long int c_ = NUM2ULONG(c); + + CDenseMatrix *cresult; + cresult = dense_matrix_new(); + VALUE result; + + dense_matrix_zeros(cresult, r_, c_); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + + return result; +} +/* +VALUE cmatrix_dense_diag(VALUE self, VALUE args) +{ + +} + +VALUE cmatrix_dense_eye(VALUE self, VALUE args) +{ + +}*/ diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index adf82eb..097a8b1 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -27,6 +27,10 @@ VALUE cmatrix_dense_LDL(VALUE self); VALUE cmatrix_dense_LU_solve(VALUE self, VALUE b); VALUE cmatrix_dense_FFLU(VALUE self); VALUE cmatrix_dense_FFLDU(VALUE self); +VALUE cmatrix_dense_ones(VALUE self, VALUE r, VALUE c); +VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c); +/*VALUE cmatrix_dense_diag(VALUE self, VALUE args); +VALUE cmatrix_dense_eye(VALUE self, VALUE args);*/ void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 82f92e0..3636342 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -253,6 +253,12 @@ void Init_symengine() rb_define_method(c_dense_matrix, "FFLU", cmatrix_dense_FFLU, 0); rb_define_method(c_dense_matrix, "FFLDU", cmatrix_dense_FFLDU, 0); + // Numpy-like methods for DenseMatrix as module level methods + rb_define_module_function(m_symengine, "ones", cmatrix_dense_ones, 2); + rb_define_module_function(m_symengine, "zeros", cmatrix_dense_zeros, 2); + /*rb_define_module_function(m_symengine, "diag", cmatrix_dense_diag, -2); + rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2);*/ + // SparseMatrix Methods rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc); From 42d3e9526d0187e5011f505a37fd0c03dd0af6ac Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Wed, 29 Jun 2016 23:27:22 +0530 Subject: [PATCH 17/28] diag method of numpy-like functions --- ext/symengine/ruby_matrix.c | 46 +++++++++++++++++++++++++++++++++++-- ext/symengine/ruby_matrix.h | 2 +- ext/symengine/symengine.c | 4 ++-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 2554e75..e66f8bb 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -567,12 +567,54 @@ VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c) return result; } -/* + VALUE cmatrix_dense_diag(VALUE self, VALUE args) { + int argc = RARRAY_LEN(args); + + if (argc != 1 && argc != 2){ + rb_raise(rb_eTypeError, "Wrong number of arguments"); + } + + CDenseMatrix *cresult; + cresult = dense_matrix_new(); + VALUE result; + + VALUE operand = rb_ary_shift(args); + char *s = rb_obj_classname(operand); + + CVecBasic *cargs; + + if(strcmp(s, "Array") == 0) { + cargs = vecbasic_new(); + basic x; + basic_new_stack(x); + int cols = RARRAY_LEN(operand); + int j; + for(j = 0; j < cols; j++) { + sympify(rb_ary_shift(operand), x); + vecbasic_push_back(cargs, x); + } + basic_free_stack(x); + } else { + rb_raise(rb_eTypeError, "Invalid Argument type"); + } + + long int k = 0; + + if (argc == 2) { // With offset + k = NUM2LONG(rb_ary_shift(args)); + } + + dense_matrix_diag(cresult, cargs, k); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + vecbasic_free(cargs); + return result; } - +/* VALUE cmatrix_dense_eye(VALUE self, VALUE args) { diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 097a8b1..7fa3d75 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -29,7 +29,7 @@ VALUE cmatrix_dense_FFLU(VALUE self); VALUE cmatrix_dense_FFLDU(VALUE self); VALUE cmatrix_dense_ones(VALUE self, VALUE r, VALUE c); VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c); -/*VALUE cmatrix_dense_diag(VALUE self, VALUE args); +VALUE cmatrix_dense_diag(VALUE self, VALUE args);/* VALUE cmatrix_dense_eye(VALUE self, VALUE args);*/ void cmatrix_sparse_free(void *ptr); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 3636342..2385056 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -256,8 +256,8 @@ void Init_symengine() // Numpy-like methods for DenseMatrix as module level methods rb_define_module_function(m_symengine, "ones", cmatrix_dense_ones, 2); rb_define_module_function(m_symengine, "zeros", cmatrix_dense_zeros, 2); - /*rb_define_module_function(m_symengine, "diag", cmatrix_dense_diag, -2); - rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2);*/ + rb_define_module_function(m_symengine, "diag", cmatrix_dense_diag, -2); + /*rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2);*/ // SparseMatrix Methods From c07f5c714294cd7fffc121235272bbe3c9fb2a86 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sat, 2 Jul 2016 13:39:35 +0530 Subject: [PATCH 18/28] All numpy-like functions wrapped for DenseMatrix --- ext/symengine/ruby_matrix.c | 30 ++++++++++++++++++++++++++++-- ext/symengine/ruby_matrix.h | 4 ++-- ext/symengine/symengine.c | 2 +- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index e66f8bb..c7a4a32 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -614,8 +614,34 @@ VALUE cmatrix_dense_diag(VALUE self, VALUE args) return result; } -/* + VALUE cmatrix_dense_eye(VALUE self, VALUE args) { + int argc = RARRAY_LEN(args); + + if (argc != 1 && argc != 3){ + rb_raise(rb_eTypeError, "Wrong number of arguments"); + } + + CDenseMatrix *cresult; + cresult = dense_matrix_new(); + VALUE result; -}*/ + VALUE operand = rb_ary_shift(args); + unsigned long int N = NUM2ULONG(operand); + unsigned long int M = N; + int k = 0; + + if(argc == 3) { + operand = rb_ary_shift(args); + M = NUM2ULONG(operand); + operand = rb_ary_shift(args); + k = NUM2INT(operand); + } + + dense_matrix_eye(cresult, N, M, k); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + cresult); + return result; +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 7fa3d75..4a7f6cd 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -29,8 +29,8 @@ VALUE cmatrix_dense_FFLU(VALUE self); VALUE cmatrix_dense_FFLDU(VALUE self); VALUE cmatrix_dense_ones(VALUE self, VALUE r, VALUE c); VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c); -VALUE cmatrix_dense_diag(VALUE self, VALUE args);/* -VALUE cmatrix_dense_eye(VALUE self, VALUE args);*/ +VALUE cmatrix_dense_diag(VALUE self, VALUE args); +VALUE cmatrix_dense_eye(VALUE self, VALUE args); void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 2385056..ee4631c 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -257,7 +257,7 @@ void Init_symengine() rb_define_module_function(m_symengine, "ones", cmatrix_dense_ones, 2); rb_define_module_function(m_symengine, "zeros", cmatrix_dense_zeros, 2); rb_define_module_function(m_symengine, "diag", cmatrix_dense_diag, -2); - /*rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2);*/ + rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2); // SparseMatrix Methods From 7f4c24d242a7549742ea54cbd239de90577a15f6 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sun, 3 Jul 2016 16:23:32 +0530 Subject: [PATCH 19/28] SparseMatrix get and set methods --- ext/symengine/ruby_matrix.c | 42 +++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_matrix.h | 2 ++ ext/symengine/symengine.c | 2 ++ 3 files changed, 46 insertions(+) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index c7a4a32..82c3557 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -645,3 +645,45 @@ VALUE cmatrix_dense_eye(VALUE self, VALUE args) cresult); return result; } + +VALUE cmatrix_sparse_get(VALUE self, VALUE r, VALUE c) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CSparseMatrix, this); + + basic_struct *cresult; + VALUE result; + + unsigned long cbasic_r; + cbasic_r = NUM2ULONG(r); + unsigned long cbasic_c; + cbasic_c = NUM2ULONG(c); + + cresult = basic_new_heap(); + + sparse_matrix_get_basic(cresult, this, cbasic_r, cbasic_c); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, basic_free_heap, + cresult); + return result; +} + +VALUE cmatrix_sparse_set(VALUE self, VALUE r, VALUE c, VALUE operand) +{ + CSparseMatrix *this; + Data_Get_Struct(self, CSparseMatrix, this); + + basic cbasic_operand; + basic_new_stack(cbasic_operand); + sympify(operand, cbasic_operand); + + unsigned long cbasic_r; + cbasic_r = NUM2ULONG(r); + unsigned long cbasic_c; + cbasic_c = NUM2ULONG(c); + + sparse_matrix_set_basic(this, cbasic_r, cbasic_c, cbasic_operand); + + basic_free_stack(cbasic_operand); + + return self; +} diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 4a7f6cd..44f0eb6 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -36,5 +36,7 @@ void cmatrix_sparse_free(void *ptr); VALUE cmatrix_sparse_alloc(VALUE klass); VALUE cmatrix_sparse_init(VALUE self, VALUE args); VALUE cmatrix_sparse_to_str(VALUE self); +VALUE cmatrix_sparse_get(VALUE self, VALUE r, VALUE c); +VALUE cmatrix_sparse_set(VALUE self, VALUE r, VALUE c, VALUE operand); #endif // RUBY_MATRIX_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index ee4631c..e23b6ba 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -264,6 +264,8 @@ void Init_symengine() rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc); rb_define_method(c_sparse_matrix, "initialize", cmatrix_sparse_init, -2); rb_define_method(c_sparse_matrix, "to_s", cmatrix_sparse_to_str, 0); + rb_define_method(c_sparse_matrix, "get", cmatrix_sparse_get, 2); + rb_define_method(c_sparse_matrix, "set", cmatrix_sparse_set, 3); symengine_print_stack_on_segfault(); From 5c739d1a0895899f1944ba1170d262dcb5c26673 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Mon, 4 Jul 2016 11:22:16 +0530 Subject: [PATCH 20/28] equal and not equal operators for DenseMatrix --- ext/symengine/ruby_matrix.c | 29 +++++++++++++++++++++++++++++ ext/symengine/ruby_matrix.h | 2 ++ ext/symengine/symengine.c | 3 +++ 3 files changed, 34 insertions(+) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 82c3557..ba1d7e5 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -404,6 +404,35 @@ VALUE cmatrix_dense_mul(VALUE self, VALUE operand) return result; } +VALUE cmatrix_dense_eq(VALUE self, VALUE operand) +{ + CDenseMatrix *this; + Data_Get_Struct(self, CDenseMatrix, this); + + CDenseMatrix *cresult; + VALUE result; + + cresult = dense_matrix_new(); + + char *s = rb_obj_classname(operand); + + if(strcmp(s, "SymEngine::DenseMatrix") == 0) { + CDenseMatrix *coperand; + Data_Get_Struct(operand, CDenseMatrix, coperand); + + result = dense_matrix_eq(this, coperand) == 1 ? Qtrue : Qfalse; + } else { + result = Qfalse; + } + return result; +} + +VALUE cmatrix_dense_neq(VALUE self, VALUE operand) +{ + VALUE result = cmatrix_dense_eq(self, operand); + rb_p(result); + return result == Qtrue ? Qfalse : Qtrue; +} VALUE cmatrix_dense_LU(VALUE self) { diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 44f0eb6..760574f 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -22,6 +22,8 @@ VALUE cmatrix_dense_rows(VALUE self); VALUE cmatrix_dense_cols(VALUE self); VALUE cmatrix_dense_add(VALUE self, VALUE operand); VALUE cmatrix_dense_mul(VALUE self, VALUE operand); +VALUE cmatrix_dense_eq(VALUE self, VALUE operand); +VALUE cmatrix_dense_neq(VALUE self, VALUE operand); VALUE cmatrix_dense_LU(VALUE self); VALUE cmatrix_dense_LDL(VALUE self); VALUE cmatrix_dense_LU_solve(VALUE self, VALUE b); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index e23b6ba..dc6fc39 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -247,6 +247,9 @@ void Init_symengine() rb_define_method(c_dense_matrix, "cols", cmatrix_dense_cols, 0); rb_define_method(c_dense_matrix, "+", cmatrix_dense_add, 1); rb_define_method(c_dense_matrix, "*", cmatrix_dense_mul, 1); + rb_define_method(c_dense_matrix, "==", cmatrix_dense_eq, 1); + rb_define_method(c_dense_matrix, "eql?", cmatrix_dense_eq, 1); + rb_define_method(c_dense_matrix, "!=", cmatrix_dense_neq, 1); rb_define_method(c_dense_matrix, "LU", cmatrix_dense_LU, 0); rb_define_method(c_dense_matrix, "LDL", cmatrix_dense_LDL, 0); rb_define_method(c_dense_matrix, "LU_solve", cmatrix_dense_LU_solve, 1); From ff06faaf5efe3c675e21c765fa02022b1643f640 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 7 Jul 2016 00:10:21 +0530 Subject: [PATCH 21/28] Changed code due to changes in Cwrapper, started spec --- ext/symengine/ruby_matrix.c | 4 ++-- ext/symengine/ruby_utils.c | 4 ++-- spec/matrix_spec.rb | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 spec/matrix_spec.rb diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index ba1d7e5..dd1f92e 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -119,7 +119,7 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) counter++; } } - dense_matrix_set_vec(this, rows, cols, cargs); + this = dense_matrix_new_vec(rows, cols, cargs); basic_free_stack(x); vecbasic_free(cargs); @@ -544,7 +544,7 @@ VALUE cmatrix_dense_FFLDU(VALUE self) VALUE result_u; cresult_u = dense_matrix_new(); - dense_matrix_LDL(cresult_l, cresult_d, this); + dense_matrix_FFLDU(cresult_l, cresult_d, cresult_u, this); result_l = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_l); diff --git a/ext/symengine/ruby_utils.c b/ext/symengine/ruby_utils.c index 3da0d19..973dfaf 100644 --- a/ext/symengine/ruby_utils.c +++ b/ext/symengine/ruby_utils.c @@ -8,7 +8,7 @@ VALUE cutils_sympify(VALUE self, VALUE operand) if(strcmp(s, "Array") == 0) { - CDenseMatrix *mat_result = dense_matrix_new(); + CDenseMatrix *mat_result; int counter = 0; int rows = RARRAY_LEN(operand); @@ -40,7 +40,7 @@ VALUE cutils_sympify(VALUE self, VALUE operand) } } - dense_matrix_set_vec(mat_result, rows, cols, cargs); + mat_result = dense_matrix_new_vec(rows, cols, cargs); basic_free_stack(x); vecbasic_free(cargs); diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb new file mode 100644 index 0000000..245b4f8 --- /dev/null +++ b/spec/matrix_spec.rb @@ -0,0 +1,23 @@ +describe SymEngine::DenseMatrix do + describe 'convert' do + subject { SymEngine([[1, 2],[3, 4]]) } + + it { is_expected.to be_a SymEngine::DenseMatrix } + its(:to_s) { is_expected.to eq "[1, 2]\n[3, 4]\n" } + end + + describe 'dense_matrix functions' do + subject { SymEngine([[4, 3],[3, 2]]) } + + its(:inv) { is_expected.to be_a SymEngine::DenseMatrix } + its(:inv) { is_expected.to eq SymEngine([[-2, 3],[3, -4]]) } + + its(:FFLU) { is_expected.to be_a SymEngine::DenseMatrix } + its(:FFLU) { is_expected.to eq SymEngine([[4, 3],[3, -1]]) } + + #its(:LU) { is_expected.to eq [SymEngine([[1, 0],[3/4, 1]]), SymEngine([[4, 3],[0, -1/4]])] } + + + end + +end From 6d6521f65c78fc47fb007349d9c0dfce6c5c8f4c Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 7 Jul 2016 10:00:59 +0530 Subject: [PATCH 22/28] matrix spec update --- ext/symengine/ruby_matrix.c | 3 - notebooks/beginner.ipynb | 325 ++++++++++++++++++++++++++++++++---- spec/matrix_spec.rb | 41 ++++- 3 files changed, 335 insertions(+), 34 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index dd1f92e..9f34d46 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -352,7 +352,6 @@ VALUE cmatrix_dense_add(VALUE self, VALUE operand) result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); - dense_matrix_free(coperand); } else { // Scalar Addition basic_struct *coperand = basic_new_heap(); @@ -388,8 +387,6 @@ VALUE cmatrix_dense_mul(VALUE self, VALUE operand) result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); - - dense_matrix_free(coperand); } else { // Scalar Multiplication basic_struct *coperand = basic_new_heap(); diff --git a/notebooks/beginner.ipynb b/notebooks/beginner.ipynb index eb00175..2791492 100644 --- a/notebooks/beginner.ipynb +++ b/notebooks/beginner.ipynb @@ -62,35 +62,6 @@ "SymEngine.ascii_art" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "or create a variable" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "#" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "basic = SymEngine::Basic.new" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -614,11 +585,305 @@ "source": [ "k.expand.to_s" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SymEngine::RealDouble\n", + "\n", + "SymEngine::RealDouble can be constructed by converting any ruby Float into SymEngine" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "d = SymEngine(1.2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SymEngine::ComplexDouble\n", + "SymEngine::ComplexDouble can be constructed by converting any ruby Complex into SymEngine" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "c = SymEngine(Complex(2.3, 3.2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SymEngine::RealMPFR\n", + "SymEngine::RealMPFR can be constructed either by converting any Ruby BigDecimal into SymEngine, or using the constructor to express any real number with a given number of bits of precision" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "require 'bigdecimal'\n", + "r1 = SymEngine(BigDecimal(\"12.3\"))\n", + "r2 = SymEngine::RealMPFR.new(12.3, 200)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SymEngine::ComplexMPC\n", + "SymEngine::RealMPC can be constructed by arithmatic operations of any SymEngine::RealMPFR objects, as shown below." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "i = SymEngine:I\n", + "c1 = r1 + i * r2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SymEngine::Constant - SymEngine Constants\n", + "\n", + "SymEngine offers the following constants" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "i = SymEngine::I\n", + "e = SymEngine::E\n", + "eg = SymEngine::EULER_GAMMA\n", + "pi = SymEngine::PI\n", + "\n", + "i.inspect + e.inspect + eg.inspect + pi.inspect" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SymEngine::TrigFunction\n", + "\n", + "sin, cos, tan, cosec, sec, cot, asin, acos, atan, acosec, asec, acot are available as shown below:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "i1 = SymEngine::sin(pi)\n", + "i2 = SymEngine::cos(0.2)\n", + "i3 = SymEngine::tan(pi/4)\n", + "i4 = SymEngine::csc(pi/2)\n", + "i5 = SymEngine::sec(0.2)\n", + "i6 = SymEngine::cot(pi/4)\n", + "\n", + "print \"sin(pi): \", i1,\"\\ncos(0.2): \", i2, \"\\ntan(pi/4): \", i3, \"\\ncsc(pi/2): \", i4, \"\\nsec(0.2): \", i5,\"\\ncot(pi/4): \", i6, \"\\n\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "i1 = SymEngine::asin(1)\n", + "i2 = SymEngine::acos(0)\n", + "i3 = SymEngine::atan(5)\n", + "i4 = SymEngine::acsc(1)\n", + "i5 = SymEngine::asec(0.2)\n", + "i6 = SymEngine::acot(0.5)\n", + "\n", + "print \"i1: \", i1,\"\\ni2: \", i2, \"\\ni3: \", i3, \"\\ni4: \", i4, \"\\ni5: \", i5,\"\\ni6: \", i6, \"\\n\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SymEngine::HyperbolicFunction\n", + "sinh, cosh, tanh, cosech, sech, coth, asinh, acosh, atanh, acosech, asech, acoth are available as shown below:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "i1 = SymEngine::sinh(pi)\n", + "i2 = SymEngine::cosh(0.2)\n", + "i3 = SymEngine::tanh(pi/4)\n", + "i4 = SymEngine::csch(pi/2)\n", + "i5 = SymEngine::sech(0.2)\n", + "i6 = SymEngine::coth(pi/4)\n", + "\n", + "print \"sinh(pi): \", i1,\"\\ncosh(0.2): \", i2, \"\\ntanh(pi/4): \", i3, \"\\ncsch(pi/2): \", i4, \"\\nsech(0.2): \", i5,\"\\ncoth(pi/4): \", i6, \"\\n\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "i1 = SymEngine::asinh(1)\n", + "i2 = SymEngine::acosh(0)\n", + "i3 = SymEngine::atanh(5)\n", + "i4 = SymEngine::acsch(1)\n", + "i5 = SymEngine::asech(0.2)\n", + "i6 = SymEngine::acoth(0.5)\n", + "\n", + "print \"i1: \", i1,\"\\ni2: \", i2, \"\\ni3: \", i3, \"\\ni4: \", i4, \"\\ni5: \", i5,\"\\ni6: \", i6, \"\\n\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## NTheory Functions\n", + "\n", + "Several Number Theory functions are available in SymEngine.\n", + "\n", + "GCD and LCM" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "gcd = SymEngine::gcd(45, 40)\n", + "lcm = SymEngine::lcm(45, 40)\n", + "\n", + "print \"for 45 and 40,\\ngcd is: \", gcd, \"\\nlcm is: \",lcm, \"\\n\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next Prime" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "np = SymEngine::nextprime(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Quotient" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "q = SymEngine::quotient(5, 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lucas and Fibonacci series" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "l = SymEngine::lucas(3)\n", + "f = SymEngine::fibonacci(3)\n", + "\n", + "p l, f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Binomials" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "b = SymEngine::binomial(5, 2)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Ruby 2.2.0", + "display_name": "Ruby 2.3.0", "language": "ruby", "name": "ruby" }, diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 245b4f8..2ac37f6 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -15,9 +15,48 @@ its(:FFLU) { is_expected.to be_a SymEngine::DenseMatrix } its(:FFLU) { is_expected.to eq SymEngine([[4, 3],[3, -1]]) } - #its(:LU) { is_expected.to eq [SymEngine([[1, 0],[3/4, 1]]), SymEngine([[4, 3],[0, -1/4]])] } + its(:LU) { is_expected.to eq [SymEngine([[1, 0],[SymEngine(3)/SymEngine(4), 1]]), + SymEngine([[4, 3],[0, SymEngine(-1)/SymEngine(4)]])] } + + its(:LDL) { is_expected.to eq [SymEngine([[1, 0],[SymEngine(3)/SymEngine(4), 1]]), + SymEngine([[4, 0],[0, SymEngine(-1)/SymEngine(4)]])] } + + its(:FFLU) { is_expected.to eq SymEngine([[4, 3],[3, -1]]) } + + its(:FFLDU) { is_expected.to eq [SymEngine([[4, 0],[3, 1]]), SymEngine([[4, 0],[0, 4]]), + SymEngine([[4, 3],[0, -1]])] } + + its(:det) { is_expected.to eq (-1)} + + end + + describe 'more dense_matrix functions' do + + let(:mat1) { SymEngine([[1, 2],[3, 4]]) } + let(:mat2) { SymEngine([[4, 3],[2, 1]]) } + let(:a) { SymEngine(4) } + let(:matA) { SymEngine([[3, 1, 2],[5, 7, 5], [1, 2, 3]]) } + let(:b) { SymEngine([[4],[4],[4]]) } + it do + expect(mat1 + mat2).to eq(SymEngine([[5, 5],[5, 5]])) + expect(mat1 + a).to eq(SymEngine([[5, 6],[7, 8]])) + expect(mat1 * mat2).to eq(SymEngine([[8, 5],[20, 13]])) + expect(mat1 * a).to eq(SymEngine([[4, 8],[12, 16]])) + + expect(mat1.transpose).to eq(SymEngine([[1, 3],[2, 4]])) + + expect(mat1.submatrix(0, 0, 1, 0, 1, 1)).to eq(SymEngine([[1],[3]])) + + + expect(matA.LU_solve(b)).to eq(SymEngine([[SymEngine(12)/SymEngine(29)], + [SymEngine(-32)/SymEngine(29)], + [SymEngine(56)/SymEngine(29)] + ])) + expect(mat1.set(0, 0, a)).to eq(SymEngine([[4, 2],[3, 4]])) + expect(mat1.get(1, 0)).to eq(SymEngine(3)) + end end end From 6a7fa2b9cabce4d25053e1c4c597e87971c35d67 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 7 Jul 2016 14:06:29 +0530 Subject: [PATCH 23/28] changed inspect for DenseMatrix --- lib/symengine.rb | 1 + lib/symengine/dense_matrix.rb | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 lib/symengine/dense_matrix.rb diff --git a/lib/symengine.rb b/lib/symengine.rb index d2c246c..9803272 100644 --- a/lib/symengine.rb +++ b/lib/symengine.rb @@ -41,3 +41,4 @@ def Function(n) require 'symengine/complex_double' require 'symengine/undef_function' require 'symengine/matrix_base' +require 'symengine/dense_matrix' diff --git a/lib/symengine/dense_matrix.rb b/lib/symengine/dense_matrix.rb new file mode 100644 index 0000000..63abf7c --- /dev/null +++ b/lib/symengine/dense_matrix.rb @@ -0,0 +1,7 @@ +module SymEngine + class MatrixBase + def inspect + "#<#{self.class}(#{rows}x#{cols})>" + end + end +end From e447e974083f38e8f6a1cd1c696cf5fc4217de63 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 7 Jul 2016 16:17:57 +0530 Subject: [PATCH 24/28] Updated matrix spec, better grouping for tests and row, column tests --- lib/symengine/dense_matrix.rb | 2 +- spec/matrix_spec.rb | 48 +++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lib/symengine/dense_matrix.rb b/lib/symengine/dense_matrix.rb index 63abf7c..e836df4 100644 --- a/lib/symengine/dense_matrix.rb +++ b/lib/symengine/dense_matrix.rb @@ -1,5 +1,5 @@ module SymEngine - class MatrixBase + class DenseMatrix def inspect "#<#{self.class}(#{rows}x#{cols})>" end diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 2ac37f6..441bace 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -6,7 +6,7 @@ its(:to_s) { is_expected.to eq "[1, 2]\n[3, 4]\n" } end - describe 'dense_matrix functions' do + describe 'dense_matrix inverse, FFLU, LU, LDL, FFLDU, det' do subject { SymEngine([[4, 3],[3, 2]]) } its(:inv) { is_expected.to be_a SymEngine::DenseMatrix } @@ -20,8 +20,6 @@ its(:LDL) { is_expected.to eq [SymEngine([[1, 0],[SymEngine(3)/SymEngine(4), 1]]), SymEngine([[4, 0],[0, SymEngine(-1)/SymEngine(4)]])] } - - its(:FFLU) { is_expected.to eq SymEngine([[4, 3],[3, -1]]) } its(:FFLDU) { is_expected.to eq [SymEngine([[4, 0],[3, 1]]), SymEngine([[4, 0],[0, 4]]), SymEngine([[4, 3],[0, -1]])] } @@ -30,33 +28,51 @@ end - describe 'more dense_matrix functions' do - - let(:mat1) { SymEngine([[1, 2],[3, 4]]) } - let(:mat2) { SymEngine([[4, 3],[2, 1]]) } - let(:a) { SymEngine(4) } - let(:matA) { SymEngine([[3, 1, 2],[5, 7, 5], [1, 2, 3]]) } - let(:b) { SymEngine([[4],[4],[4]]) } + + let(:mat1) { SymEngine([[1, 2],[3, 4]]) } + let(:mat2) { SymEngine([[4, 3],[2, 1]]) } + let(:a) { SymEngine(4) } + let(:matA) { SymEngine([[3, 1, 2],[5, 7, 5], [1, 2, 3]]) } + let(:b) { SymEngine([[4],[4],[4]]) } - it do + describe 'dense_matrix addition, multiplication' do + it 'adds and multiplies' do expect(mat1 + mat2).to eq(SymEngine([[5, 5],[5, 5]])) expect(mat1 + a).to eq(SymEngine([[5, 6],[7, 8]])) expect(mat1 * mat2).to eq(SymEngine([[8, 5],[20, 13]])) expect(mat1 * a).to eq(SymEngine([[4, 8],[12, 16]])) - + end + end + + describe 'dense_matrix transpose and submatrix' do + it 'performs transpose and submatrix' do expect(mat1.transpose).to eq(SymEngine([[1, 3],[2, 4]])) - expect(mat1.submatrix(0, 0, 1, 0, 1, 1)).to eq(SymEngine([[1],[3]])) - - + end + end + + describe 'LU_solve' do + it 'solves Ax = b' do expect(matA.LU_solve(b)).to eq(SymEngine([[SymEngine(12)/SymEngine(29)], [SymEngine(-32)/SymEngine(29)], [SymEngine(56)/SymEngine(29)] - ])) + ])) + end + end + + describe 'getter and setter' do + it 'gets and sets elements' do expect(mat1.set(0, 0, a)).to eq(SymEngine([[4, 2],[3, 4]])) expect(mat1.get(1, 0)).to eq(SymEngine(3)) end end + describe 'rows and cols' do + it 'returns row and column count' do + expect(matA.rows). to eq (3) + expect(matA.cols). to eq (3) + end + end + end From 61cee1a1ee5b86bac1b9fb971e033ce7030b5134 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 8 Jul 2016 11:27:25 +0530 Subject: [PATCH 25/28] Removing unwanted files, and updating symengine version --- ext/symengine/symengine_utils.c | 2 +- notebooks/beginner.ipynb | 325 +++----------------------------- symengine_version.txt | 2 +- 3 files changed, 32 insertions(+), 297 deletions(-) diff --git a/ext/symengine/symengine_utils.c b/ext/symengine/symengine_utils.c index 6b7b80c..f17489a 100644 --- a/ext/symengine/symengine_utils.c +++ b/ext/symengine/symengine_utils.c @@ -83,7 +83,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) { VALUE ret = check_sympify(operand2, cbasic_operand2); if (ret == Qfalse) { - rb_raise(rb_eTypeError, "%s can't be coerced into a SymEngine type.", + rb_raise(rb_eTypeError, "%s can't be coerced into SymEngine::Basic", rb_obj_classname(operand2)); } } diff --git a/notebooks/beginner.ipynb b/notebooks/beginner.ipynb index 2791492..eb00175 100644 --- a/notebooks/beginner.ipynb +++ b/notebooks/beginner.ipynb @@ -62,6 +62,35 @@ "SymEngine.ascii_art" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "or create a variable" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "#" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "basic = SymEngine::Basic.new" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -585,305 +614,11 @@ "source": [ "k.expand.to_s" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SymEngine::RealDouble\n", - "\n", - "SymEngine::RealDouble can be constructed by converting any ruby Float into SymEngine" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d = SymEngine(1.2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SymEngine::ComplexDouble\n", - "SymEngine::ComplexDouble can be constructed by converting any ruby Complex into SymEngine" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "c = SymEngine(Complex(2.3, 3.2))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SymEngine::RealMPFR\n", - "SymEngine::RealMPFR can be constructed either by converting any Ruby BigDecimal into SymEngine, or using the constructor to express any real number with a given number of bits of precision" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "require 'bigdecimal'\n", - "r1 = SymEngine(BigDecimal(\"12.3\"))\n", - "r2 = SymEngine::RealMPFR.new(12.3, 200)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SymEngine::ComplexMPC\n", - "SymEngine::RealMPC can be constructed by arithmatic operations of any SymEngine::RealMPFR objects, as shown below." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "i = SymEngine:I\n", - "c1 = r1 + i * r2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SymEngine::Constant - SymEngine Constants\n", - "\n", - "SymEngine offers the following constants" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "i = SymEngine::I\n", - "e = SymEngine::E\n", - "eg = SymEngine::EULER_GAMMA\n", - "pi = SymEngine::PI\n", - "\n", - "i.inspect + e.inspect + eg.inspect + pi.inspect" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SymEngine::TrigFunction\n", - "\n", - "sin, cos, tan, cosec, sec, cot, asin, acos, atan, acosec, asec, acot are available as shown below:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "i1 = SymEngine::sin(pi)\n", - "i2 = SymEngine::cos(0.2)\n", - "i3 = SymEngine::tan(pi/4)\n", - "i4 = SymEngine::csc(pi/2)\n", - "i5 = SymEngine::sec(0.2)\n", - "i6 = SymEngine::cot(pi/4)\n", - "\n", - "print \"sin(pi): \", i1,\"\\ncos(0.2): \", i2, \"\\ntan(pi/4): \", i3, \"\\ncsc(pi/2): \", i4, \"\\nsec(0.2): \", i5,\"\\ncot(pi/4): \", i6, \"\\n\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "i1 = SymEngine::asin(1)\n", - "i2 = SymEngine::acos(0)\n", - "i3 = SymEngine::atan(5)\n", - "i4 = SymEngine::acsc(1)\n", - "i5 = SymEngine::asec(0.2)\n", - "i6 = SymEngine::acot(0.5)\n", - "\n", - "print \"i1: \", i1,\"\\ni2: \", i2, \"\\ni3: \", i3, \"\\ni4: \", i4, \"\\ni5: \", i5,\"\\ni6: \", i6, \"\\n\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## SymEngine::HyperbolicFunction\n", - "sinh, cosh, tanh, cosech, sech, coth, asinh, acosh, atanh, acosech, asech, acoth are available as shown below:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "i1 = SymEngine::sinh(pi)\n", - "i2 = SymEngine::cosh(0.2)\n", - "i3 = SymEngine::tanh(pi/4)\n", - "i4 = SymEngine::csch(pi/2)\n", - "i5 = SymEngine::sech(0.2)\n", - "i6 = SymEngine::coth(pi/4)\n", - "\n", - "print \"sinh(pi): \", i1,\"\\ncosh(0.2): \", i2, \"\\ntanh(pi/4): \", i3, \"\\ncsch(pi/2): \", i4, \"\\nsech(0.2): \", i5,\"\\ncoth(pi/4): \", i6, \"\\n\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "i1 = SymEngine::asinh(1)\n", - "i2 = SymEngine::acosh(0)\n", - "i3 = SymEngine::atanh(5)\n", - "i4 = SymEngine::acsch(1)\n", - "i5 = SymEngine::asech(0.2)\n", - "i6 = SymEngine::acoth(0.5)\n", - "\n", - "print \"i1: \", i1,\"\\ni2: \", i2, \"\\ni3: \", i3, \"\\ni4: \", i4, \"\\ni5: \", i5,\"\\ni6: \", i6, \"\\n\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## NTheory Functions\n", - "\n", - "Several Number Theory functions are available in SymEngine.\n", - "\n", - "GCD and LCM" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "gcd = SymEngine::gcd(45, 40)\n", - "lcm = SymEngine::lcm(45, 40)\n", - "\n", - "print \"for 45 and 40,\\ngcd is: \", gcd, \"\\nlcm is: \",lcm, \"\\n\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Next Prime" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "np = SymEngine::nextprime(5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Quotient" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "q = SymEngine::quotient(5, 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Lucas and Fibonacci series" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "l = SymEngine::lucas(3)\n", - "f = SymEngine::fibonacci(3)\n", - "\n", - "p l, f" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Binomials" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "b = SymEngine::binomial(5, 2)" - ] } ], "metadata": { "kernelspec": { - "display_name": "Ruby 2.3.0", + "display_name": "Ruby 2.2.0", "language": "ruby", "name": "ruby" }, diff --git a/symengine_version.txt b/symengine_version.txt index bc0c91d..1c34458 100644 --- a/symengine_version.txt +++ b/symengine_version.txt @@ -1 +1 @@ -0c17ca9d88f25881c6d0ca382c26b2eb392b9b48 +fd7d2be06d935e2625a56fe0fd637925de2420ff From aaa2aa8b78e29b7849876088c2db05faa390229d Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 8 Jul 2016 13:26:14 +0530 Subject: [PATCH 26/28] Formatting and fixing warnings --- ext/symengine/ruby_matrix.c | 348 ++++++++++++++++++------------------ ext/symengine/ruby_matrix.h | 3 +- ext/symengine/ruby_utils.c | 28 +-- ext/symengine/ruby_utils.h | 1 - ext/symengine/symengine.c | 21 +-- 5 files changed, 199 insertions(+), 202 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 9f34d46..2dd9f2f 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -54,43 +54,42 @@ VALUE cmatrix_sparse_to_str(VALUE self) return result; } - VALUE cmatrix_dense_init(VALUE self, VALUE args) { int argc = RARRAY_LEN(args); CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + if (argc == 0) { - - // SymEngine::DenseMatrix() + + // SymEngine::DenseMatrix() dense_matrix_init(this); - + } else if (argc == 1) { - // SymEngine::DenseMatrix(SymEngine::DenseMatrix) OR - // SymEngine::DenseMatrix(NMatrix) OR - // SymEngine::DenseMatrix(Array) - + // SymEngine::DenseMatrix(SymEngine::DenseMatrix) OR + // SymEngine::DenseMatrix(NMatrix) OR + // SymEngine::DenseMatrix(Array) + VALUE operand = rb_ary_shift(args); char *s = rb_obj_classname(operand); - - if(strcmp(s, "SymEngine::DenseMatrix") == 0) { - - // SymEngine::DenseMatrix(SymEngine::DenseMatrix) + + if (strcmp(s, "SymEngine::DenseMatrix") == 0) { + + // SymEngine::DenseMatrix(SymEngine::DenseMatrix) CDenseMatrix *temp; Data_Get_Struct(operand, CDenseMatrix, temp); dense_matrix_set(this, temp); - - } else if(strcmp(s, "NMatrix") == 0) { - - // SymEngine::DenseMatrix(NMatrix) + + } else if (strcmp(s, "NMatrix") == 0) { + + // SymEngine::DenseMatrix(NMatrix) rb_raise(rb_eTypeError, "TODO"); - - } else if(strcmp(s, "Array") == 0) { - - // SymEngine::DenseMatrix(Array) + + } else if (strcmp(s, "Array") == 0) { + + // SymEngine::DenseMatrix(Array) int counter = 0; - + int rows = RARRAY_LEN(operand); int cols = -1; CVecBasic *cargs = vecbasic_new(); @@ -102,56 +101,61 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) VALUE row = rb_ary_shift(operand); s = rb_obj_classname(row); - if(strcmp(s, "Array") != 0){ + if (strcmp(s, "Array") != 0) { rb_raise(rb_eTypeError, "Not a 2D Array"); } - if ( cols == -1 ) { - cols = RARRAY_LEN(row); - // Checking all rows for same col length + if (cols == -1) { + cols = RARRAY_LEN(row); + // Checking all rows for same col length } else if (cols != RARRAY_LEN(row)) { - rb_raise(rb_eTypeError, "2D Array's rows contain different column counts"); + rb_raise(rb_eTypeError, + "2D Array's rows contain different column counts"); } - - for(j = 0; j < cols; j++) { + + for (j = 0; j < cols; j++) { sympify(rb_ary_shift(row), x); vecbasic_push_back(cargs, x); counter++; } } this = dense_matrix_new_vec(rows, cols, cargs); - + basic_free_stack(x); vecbasic_free(cargs); - + } else { - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); - } - + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single " + "SymEngine::DenseMatrix, a single NMatrix, " + "a single Array or two Numerics expected."); + } + } else if (argc == 2) { - - // SymEngine::DenseMatrix(no_rows, no_cols) + + // SymEngine::DenseMatrix(no_rows, no_cols) VALUE val1 = rb_ary_shift(args); VALUE val2 = rb_ary_shift(args); - - if((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) - && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM) ) { - + + if ((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) + && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM)) { + unsigned long int rows = NUM2ULONG(val1); unsigned long int cols = NUM2ULONG(val2); dense_matrix_rows_cols(this, rows, cols); - + } else { - - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); - + + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single " + "SymEngine::DenseMatrix, a single NMatrix, " + "a single Array or two Numerics expected."); } } else { - - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); - + + rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single " + "SymEngine::DenseMatrix, a single NMatrix, a " + "single Array or two Numerics expected."); } - + return self; } @@ -160,45 +164,45 @@ VALUE cmatrix_sparse_init(VALUE self, VALUE args) int argc = RARRAY_LEN(args); CSparseMatrix *this; Data_Get_Struct(self, CSparseMatrix, this); - + if (argc == 0) { - - // SymEngine::SparseMatrix() + + // SymEngine::SparseMatrix() sparse_matrix_init(this); - + } else if (argc == 2) { - - // SymEngine::SparseMatrix(no_rows, no_cols) + + // SymEngine::SparseMatrix(no_rows, no_cols) VALUE val1 = rb_ary_shift(args); VALUE val2 = rb_ary_shift(args); - - if((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) - && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM) ) { - + + if ((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) + && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM)) { + unsigned long int rows = NUM2ULONG(val1); unsigned long int cols = NUM2ULONG(val2); sparse_matrix_rows_cols(this, rows, cols); - + } else { - - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, or two Numerics expected."); - + + rb_raise( + rb_eTypeError, + "Invalid Arguments. No Arguments, or two Numerics expected."); } } else { - - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, or two Numerics expected."); - + + rb_raise(rb_eTypeError, + "Invalid Arguments. No Arguments, or two Numerics expected."); } - + return self; } - VALUE cmatrix_dense_get(VALUE self, VALUE r, VALUE c) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + basic_struct *cresult; VALUE result; @@ -208,7 +212,7 @@ VALUE cmatrix_dense_get(VALUE self, VALUE r, VALUE c) cbasic_c = NUM2ULONG(c); cresult = basic_new_heap(); - + dense_matrix_get_basic(cresult, this, cbasic_r, cbasic_c); result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, basic_free_heap, cresult); @@ -219,34 +223,33 @@ VALUE cmatrix_dense_set(VALUE self, VALUE r, VALUE c, VALUE operand) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + basic cbasic_operand; basic_new_stack(cbasic_operand); sympify(operand, cbasic_operand); - + unsigned long cbasic_r; cbasic_r = NUM2ULONG(r); unsigned long cbasic_c; cbasic_c = NUM2ULONG(c); - + dense_matrix_set_basic(this, cbasic_r, cbasic_c, cbasic_operand); - + basic_free_stack(cbasic_operand); - + return self; } - VALUE cmatrix_dense_det(VALUE self) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + basic_struct *cresult; VALUE result; cresult = basic_new_heap(); - + dense_matrix_det(cresult, this); result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, basic_free_heap, cresult); @@ -256,15 +259,14 @@ VALUE cmatrix_dense_inv(VALUE self) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + CDenseMatrix *cresult; VALUE result; cresult = dense_matrix_new(); - + dense_matrix_inv(cresult, this); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); return result; } @@ -272,46 +274,46 @@ VALUE cmatrix_dense_transpose(VALUE self) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + CDenseMatrix *cresult; VALUE result; cresult = dense_matrix_new(); - + dense_matrix_transpose(cresult, this); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); return result; } -VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2, VALUE r_, VALUE c_) +VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, + VALUE c2, VALUE r_, VALUE c_) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + CDenseMatrix *cresult; VALUE result; - + unsigned long cbasic_r1; cbasic_r1 = NUM2ULONG(r1); unsigned long cbasic_c1; cbasic_c1 = NUM2ULONG(c1); - + unsigned long cbasic_r2; cbasic_r2 = NUM2ULONG(r2); unsigned long cbasic_c2; cbasic_c2 = NUM2ULONG(c2); - + unsigned long cbasic_r; cbasic_r = NUM2ULONG(r_); unsigned long cbasic_c; cbasic_c = NUM2ULONG(c_); cresult = dense_matrix_new(); - - dense_matrix_submatrix(cresult, this, cbasic_r1, cbasic_c1, cbasic_r2, cbasic_c2, cbasic_r, cbasic_c); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + + dense_matrix_submatrix(cresult, this, cbasic_r1, cbasic_c1, cbasic_r2, + cbasic_c2, cbasic_r, cbasic_c); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); return result; } @@ -319,50 +321,49 @@ VALUE cmatrix_dense_rows(VALUE self) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - return ULONG2NUM( dense_matrix_rows(this)); + return ULONG2NUM(dense_matrix_rows(this)); } - VALUE cmatrix_dense_cols(VALUE self) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - return ULONG2NUM( dense_matrix_cols(this)); + return ULONG2NUM(dense_matrix_cols(this)); } VALUE cmatrix_dense_add(VALUE self, VALUE operand) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + CDenseMatrix *cresult; VALUE result; cresult = dense_matrix_new(); - + char *s = rb_obj_classname(operand); - - if(strcmp(s, "SymEngine::DenseMatrix") == 0) { - // Matrix Addition + + if (strcmp(s, "SymEngine::DenseMatrix") == 0) { + // Matrix Addition CDenseMatrix *coperand; Data_Get_Struct(operand, CDenseMatrix, coperand); - + dense_matrix_add_matrix(cresult, this, coperand); - + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); - + cresult); + } else { - // Scalar Addition + // Scalar Addition basic_struct *coperand = basic_new_heap(); sympify(operand, coperand); - + dense_matrix_add_scalar(cresult, this, coperand); result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + cresult); basic_free_heap(coperand); } - + return result; } @@ -370,34 +371,34 @@ VALUE cmatrix_dense_mul(VALUE self, VALUE operand) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + CDenseMatrix *cresult; VALUE result; cresult = dense_matrix_new(); - + char *s = rb_obj_classname(operand); - - if(strcmp(s, "SymEngine::DenseMatrix") == 0) { - // Matrix Multiplication + + if (strcmp(s, "SymEngine::DenseMatrix") == 0) { + // Matrix Multiplication CDenseMatrix *coperand; Data_Get_Struct(operand, CDenseMatrix, coperand); - + dense_matrix_mul_matrix(cresult, this, coperand); - + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + cresult); } else { - // Scalar Multiplication + // Scalar Multiplication basic_struct *coperand = basic_new_heap(); sympify(operand, coperand); - + dense_matrix_mul_scalar(cresult, this, coperand); result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + cresult); basic_free_heap(coperand); } - + return result; } @@ -405,18 +406,18 @@ VALUE cmatrix_dense_eq(VALUE self, VALUE operand) { CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - + CDenseMatrix *cresult; VALUE result; cresult = dense_matrix_new(); - + char *s = rb_obj_classname(operand); - - if(strcmp(s, "SymEngine::DenseMatrix") == 0) { + + if (strcmp(s, "SymEngine::DenseMatrix") == 0) { CDenseMatrix *coperand; Data_Get_Struct(operand, CDenseMatrix, coperand); - + result = dense_matrix_eq(this, coperand) == 1 ? Qtrue : Qfalse; } else { result = Qfalse; @@ -428,7 +429,7 @@ VALUE cmatrix_dense_neq(VALUE self, VALUE operand) { VALUE result = cmatrix_dense_eq(self, operand); rb_p(result); - return result == Qtrue ? Qfalse : Qtrue; + return result == Qtrue ? Qfalse : Qtrue; } VALUE cmatrix_dense_LU(VALUE self) @@ -446,10 +447,10 @@ VALUE cmatrix_dense_LU(VALUE self) dense_matrix_LU(cresult_l, cresult_u, this); - result_l = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_l); - result_u = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_u); + result_l + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_l); + result_u + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_u); VALUE result; result = rb_ary_new(); @@ -474,10 +475,10 @@ VALUE cmatrix_dense_LDL(VALUE self) dense_matrix_LDL(cresult_l, cresult_d, this); - result_l = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_l); - result_d = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_d); + result_l + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_l); + result_d + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_d); VALUE result; result = rb_ary_new(); @@ -501,8 +502,7 @@ VALUE cmatrix_dense_LU_solve(VALUE self, VALUE b) dense_matrix_LU_solve(cresult, this, coperand); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); return result; } @@ -517,13 +517,12 @@ VALUE cmatrix_dense_FFLU(VALUE self) dense_matrix_FFLU(cresult_lu, this); - result_lu = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_lu); + result_lu + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_lu); return result_lu; } - VALUE cmatrix_dense_FFLDU(VALUE self) { CDenseMatrix *this; @@ -543,12 +542,12 @@ VALUE cmatrix_dense_FFLDU(VALUE self) dense_matrix_FFLDU(cresult_l, cresult_d, cresult_u, this); - result_l = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_l); - result_d = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_d); - result_u = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult_u); + result_l + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_l); + result_d + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_d); + result_u + = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult_u); VALUE result; result = rb_ary_new(); @@ -559,7 +558,6 @@ VALUE cmatrix_dense_FFLDU(VALUE self) return result; } - VALUE cmatrix_dense_ones(VALUE self, VALUE r, VALUE c) { unsigned long int r_ = NUM2ULONG(r); @@ -571,8 +569,7 @@ VALUE cmatrix_dense_ones(VALUE self, VALUE r, VALUE c) dense_matrix_ones(cresult, r_, c_); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); return result; } @@ -588,8 +585,7 @@ VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c) dense_matrix_zeros(cresult, r_, c_); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); return result; } @@ -597,27 +593,27 @@ VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c) VALUE cmatrix_dense_diag(VALUE self, VALUE args) { int argc = RARRAY_LEN(args); - - if (argc != 1 && argc != 2){ + + if (argc != 1 && argc != 2) { rb_raise(rb_eTypeError, "Wrong number of arguments"); } - + CDenseMatrix *cresult; cresult = dense_matrix_new(); VALUE result; - + VALUE operand = rb_ary_shift(args); char *s = rb_obj_classname(operand); - + CVecBasic *cargs; - - if(strcmp(s, "Array") == 0) { + + if (strcmp(s, "Array") == 0) { cargs = vecbasic_new(); basic x; basic_new_stack(x); int cols = RARRAY_LEN(operand); int j; - for(j = 0; j < cols; j++) { + for (j = 0; j < cols; j++) { sympify(rb_ary_shift(operand), x); vecbasic_push_back(cargs, x); } @@ -625,17 +621,16 @@ VALUE cmatrix_dense_diag(VALUE self, VALUE args) } else { rb_raise(rb_eTypeError, "Invalid Argument type"); } - + long int k = 0; - + if (argc == 2) { // With offset k = NUM2LONG(rb_ary_shift(args)); } - + dense_matrix_diag(cresult, cargs, k); - - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); vecbasic_free(cargs); return result; @@ -645,7 +640,7 @@ VALUE cmatrix_dense_eye(VALUE self, VALUE args) { int argc = RARRAY_LEN(args); - if (argc != 1 && argc != 3){ + if (argc != 1 && argc != 3) { rb_raise(rb_eTypeError, "Wrong number of arguments"); } @@ -658,7 +653,7 @@ VALUE cmatrix_dense_eye(VALUE self, VALUE args) unsigned long int M = N; int k = 0; - if(argc == 3) { + if (argc == 3) { operand = rb_ary_shift(args); M = NUM2ULONG(operand); operand = rb_ary_shift(args); @@ -667,8 +662,7 @@ VALUE cmatrix_dense_eye(VALUE self, VALUE args) dense_matrix_eye(cresult, N, M, k); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, - cresult); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, cresult); return result; } @@ -676,7 +670,7 @@ VALUE cmatrix_sparse_get(VALUE self, VALUE r, VALUE c) { CDenseMatrix *this; Data_Get_Struct(self, CSparseMatrix, this); - + basic_struct *cresult; VALUE result; @@ -686,7 +680,7 @@ VALUE cmatrix_sparse_get(VALUE self, VALUE r, VALUE c) cbasic_c = NUM2ULONG(c); cresult = basic_new_heap(); - + sparse_matrix_get_basic(cresult, this, cbasic_r, cbasic_c); result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, basic_free_heap, cresult); @@ -697,19 +691,19 @@ VALUE cmatrix_sparse_set(VALUE self, VALUE r, VALUE c, VALUE operand) { CSparseMatrix *this; Data_Get_Struct(self, CSparseMatrix, this); - + basic cbasic_operand; basic_new_stack(cbasic_operand); sympify(operand, cbasic_operand); - + unsigned long cbasic_r; cbasic_r = NUM2ULONG(r); unsigned long cbasic_c; cbasic_c = NUM2ULONG(c); - + sparse_matrix_set_basic(this, cbasic_r, cbasic_c, cbasic_operand); - + basic_free_stack(cbasic_operand); - + return self; } diff --git a/ext/symengine/ruby_matrix.h b/ext/symengine/ruby_matrix.h index 760574f..12e777e 100644 --- a/ext/symengine/ruby_matrix.h +++ b/ext/symengine/ruby_matrix.h @@ -17,7 +17,8 @@ VALUE cmatrix_dense_set(VALUE self, VALUE r, VALUE c, VALUE operand); VALUE cmatrix_dense_det(VALUE self); VALUE cmatrix_dense_inv(VALUE self); VALUE cmatrix_dense_transpose(VALUE self); -VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2, VALUE r_, VALUE c_); +VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, + VALUE c2, VALUE r_, VALUE c_); VALUE cmatrix_dense_rows(VALUE self); VALUE cmatrix_dense_cols(VALUE self); VALUE cmatrix_dense_add(VALUE self, VALUE operand); diff --git a/ext/symengine/ruby_utils.c b/ext/symengine/ruby_utils.c index 973dfaf..e809f38 100644 --- a/ext/symengine/ruby_utils.c +++ b/ext/symengine/ruby_utils.c @@ -4,9 +4,9 @@ VALUE cutils_sympify(VALUE self, VALUE operand) { VALUE result; - char *s = rb_obj_classname(operand); + const char *s = rb_obj_classname(operand); - if(strcmp(s, "Array") == 0) { + if (strcmp(s, "Array") == 0) { CDenseMatrix *mat_result; int counter = 0; @@ -22,18 +22,19 @@ VALUE cutils_sympify(VALUE self, VALUE operand) int j; VALUE row = rb_ary_shift(operand); s = rb_obj_classname(row); - if(strcmp(s, "Array") != 0){ + if (strcmp(s, "Array") != 0) { rb_raise(rb_eTypeError, "2D Array is required"); } - if ( cols == -1 ) { + if (cols == -1) { cols = RARRAY_LEN(row); - // Checking all rows for same col length + // Checking all rows for same col length } else if (cols != RARRAY_LEN(row)) { - rb_raise(rb_eTypeError, "2D Array's rows contain different column counts"); + rb_raise(rb_eTypeError, + "2D Array's rows contain different column counts"); } - for(j = 0; j < cols; j++) { + for (j = 0; j < cols; j++) { sympify(rb_ary_shift(row), x); vecbasic_push_back(cargs, x); counter++; @@ -45,15 +46,16 @@ VALUE cutils_sympify(VALUE self, VALUE operand) basic_free_stack(x); vecbasic_free(cargs); - result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, mat_result); + result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, + mat_result); } else { - basic_struct *cbasic_operand; - cbasic_operand = basic_new_heap(); + basic_struct *cbasic_operand; + cbasic_operand = basic_new_heap(); - sympify(operand, cbasic_operand); - result = Data_Wrap_Struct(Klass_of_Basic(cbasic_operand), NULL, - cbasic_free_heap, cbasic_operand); + sympify(operand, cbasic_operand); + result = Data_Wrap_Struct(Klass_of_Basic(cbasic_operand), NULL, + cbasic_free_heap, cbasic_operand); } return result; } diff --git a/ext/symengine/ruby_utils.h b/ext/symengine/ruby_utils.h index 00d926a..036544f 100644 --- a/ext/symengine/ruby_utils.h +++ b/ext/symengine/ruby_utils.h @@ -1,7 +1,6 @@ #ifndef RUBY_UTILS_H_ #define RUBY_UTILS_H_ - #include "symengine.h" #include "symengine_utils.h" diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index dc6fc39..85f2925 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -225,14 +225,17 @@ void Init_symengine() rb_define_module_function(m_symengine, "fibonacci", cntheory_fibonacci, 1); rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1); rb_define_module_function(m_symengine, "binomial", cntheory_binomial, 2); - + // MatrixBase Class - c_matrix_base = rb_define_class_under(m_symengine, "MatrixBase", rb_cObject); - + c_matrix_base + = rb_define_class_under(m_symengine, "MatrixBase", rb_cObject); + // DenseMatrix and SparseMatrix Classes - c_dense_matrix = rb_define_class_under(m_symengine, "DenseMatrix", c_matrix_base); - c_sparse_matrix = rb_define_class_under(m_symengine, "SparseMatrix", c_matrix_base); - + c_dense_matrix + = rb_define_class_under(m_symengine, "DenseMatrix", c_matrix_base); + c_sparse_matrix + = rb_define_class_under(m_symengine, "SparseMatrix", c_matrix_base); + // DenseMatrix Methods rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc); rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, -2); @@ -255,21 +258,19 @@ void Init_symengine() rb_define_method(c_dense_matrix, "LU_solve", cmatrix_dense_LU_solve, 1); rb_define_method(c_dense_matrix, "FFLU", cmatrix_dense_FFLU, 0); rb_define_method(c_dense_matrix, "FFLDU", cmatrix_dense_FFLDU, 0); - + // Numpy-like methods for DenseMatrix as module level methods rb_define_module_function(m_symengine, "ones", cmatrix_dense_ones, 2); rb_define_module_function(m_symengine, "zeros", cmatrix_dense_zeros, 2); rb_define_module_function(m_symengine, "diag", cmatrix_dense_diag, -2); rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2); - - + // SparseMatrix Methods rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc); rb_define_method(c_sparse_matrix, "initialize", cmatrix_sparse_init, -2); rb_define_method(c_sparse_matrix, "to_s", cmatrix_sparse_to_str, 0); rb_define_method(c_sparse_matrix, "get", cmatrix_sparse_get, 2); rb_define_method(c_sparse_matrix, "set", cmatrix_sparse_set, 3); - symengine_print_stack_on_segfault(); } From 662680d50b392181b0b1615dd73607d45bd9b47c Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Mon, 11 Jul 2016 13:16:10 +0530 Subject: [PATCH 27/28] Improvement to specs --- spec/matrix_spec.rb | 58 ++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 441bace..494bb16 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -1,78 +1,66 @@ describe SymEngine::DenseMatrix do describe 'convert' do subject { SymEngine([[1, 2],[3, 4]]) } - + it { is_expected.to be_a SymEngine::DenseMatrix } its(:to_s) { is_expected.to eq "[1, 2]\n[3, 4]\n" } end - - describe 'dense_matrix inverse, FFLU, LU, LDL, FFLDU, det' do + + describe 'methods without arguments' do subject { SymEngine([[4, 3],[3, 2]]) } + its(:rows) { is_expected.to eq (2) } + its(:cols) { is_expected.to eq (2) } + its(:inv) { is_expected.to be_a SymEngine::DenseMatrix } its(:inv) { is_expected.to eq SymEngine([[-2, 3],[3, -4]]) } - + its(:FFLU) { is_expected.to be_a SymEngine::DenseMatrix } its(:FFLU) { is_expected.to eq SymEngine([[4, 3],[3, -1]]) } - + its(:LU) { is_expected.to eq [SymEngine([[1, 0],[SymEngine(3)/SymEngine(4), 1]]), SymEngine([[4, 3],[0, SymEngine(-1)/SymEngine(4)]])] } - + its(:LDL) { is_expected.to eq [SymEngine([[1, 0],[SymEngine(3)/SymEngine(4), 1]]), SymEngine([[4, 0],[0, SymEngine(-1)/SymEngine(4)]])] } - + its(:FFLDU) { is_expected.to eq [SymEngine([[4, 0],[3, 1]]), SymEngine([[4, 0],[0, 4]]), SymEngine([[4, 3],[0, -1]])] } - + its(:det) { is_expected.to eq (-1)} - + end - - - let(:mat1) { SymEngine([[1, 2],[3, 4]]) } - let(:mat2) { SymEngine([[4, 3],[2, 1]]) } - let(:a) { SymEngine(4) } - let(:matA) { SymEngine([[3, 1, 2],[5, 7, 5], [1, 2, 3]]) } - let(:b) { SymEngine([[4],[4],[4]]) } - - describe 'dense_matrix addition, multiplication' do - + + describe 'methods with arguments' do + + let(:mat1) { SymEngine([[1, 2],[3, 4]]) } + let(:mat2) { SymEngine([[4, 3],[2, 1]]) } + let(:a) { SymEngine(4) } + let(:matA) { SymEngine([[3, 1, 2],[5, 7, 5], [1, 2, 3]]) } + let(:b) { SymEngine([[4],[4],[4]]) } + it 'adds and multiplies' do expect(mat1 + mat2).to eq(SymEngine([[5, 5],[5, 5]])) expect(mat1 + a).to eq(SymEngine([[5, 6],[7, 8]])) expect(mat1 * mat2).to eq(SymEngine([[8, 5],[20, 13]])) expect(mat1 * a).to eq(SymEngine([[4, 8],[12, 16]])) end - end - - describe 'dense_matrix transpose and submatrix' do + it 'performs transpose and submatrix' do expect(mat1.transpose).to eq(SymEngine([[1, 3],[2, 4]])) expect(mat1.submatrix(0, 0, 1, 0, 1, 1)).to eq(SymEngine([[1],[3]])) end - end - - describe 'LU_solve' do + it 'solves Ax = b' do expect(matA.LU_solve(b)).to eq(SymEngine([[SymEngine(12)/SymEngine(29)], [SymEngine(-32)/SymEngine(29)], [SymEngine(56)/SymEngine(29)] ])) end - end - describe 'getter and setter' do it 'gets and sets elements' do expect(mat1.set(0, 0, a)).to eq(SymEngine([[4, 2],[3, 4]])) expect(mat1.get(1, 0)).to eq(SymEngine(3)) end end - - describe 'rows and cols' do - it 'returns row and column count' do - expect(matA.rows). to eq (3) - expect(matA.cols). to eq (3) - end - end - end From 4820121c51a28ea4cdb3f478e55a88cb6ee040fe Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Wed, 13 Jul 2016 10:50:30 +0530 Subject: [PATCH 28/28] Restructuring matrix constructors --- ext/symengine/ruby_matrix.c | 39 +++++++------------------------------ 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/ext/symengine/ruby_matrix.c b/ext/symengine/ruby_matrix.c index 2dd9f2f..7987fe9 100644 --- a/ext/symengine/ruby_matrix.c +++ b/ext/symengine/ruby_matrix.c @@ -60,12 +60,7 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) CDenseMatrix *this; Data_Get_Struct(self, CDenseMatrix, this); - if (argc == 0) { - - // SymEngine::DenseMatrix() - dense_matrix_init(this); - - } else if (argc == 1) { + if (argc == 1) { // SymEngine::DenseMatrix(SymEngine::DenseMatrix) OR // SymEngine::DenseMatrix(NMatrix) OR // SymEngine::DenseMatrix(Array) @@ -125,35 +120,15 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args) vecbasic_free(cargs); } else { - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single " - "SymEngine::DenseMatrix, a single NMatrix, " - "a single Array or two Numerics expected."); + rb_raise(rb_eTypeError, "Invalid Arguments. A single argument" + "NMatrix, " + "or a single Array expected."); } - } else if (argc == 2) { - - // SymEngine::DenseMatrix(no_rows, no_cols) - VALUE val1 = rb_ary_shift(args); - VALUE val2 = rb_ary_shift(args); - - if ((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) - && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM)) { - - unsigned long int rows = NUM2ULONG(val1); - unsigned long int cols = NUM2ULONG(val2); - dense_matrix_rows_cols(this, rows, cols); - - } else { - - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single " - "SymEngine::DenseMatrix, a single NMatrix, " - "a single Array or two Numerics expected."); - } } else { - - rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single " - "SymEngine::DenseMatrix, a single NMatrix, a " - "single Array or two Numerics expected."); + rb_raise(rb_eTypeError, "Invalid Arguments. A single argument" + "NMatrix, " + "or a single Array expected."); } return self;