From d56b892020cffd4f59f1de665a6baf6ab26942f0 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 12 May 2016 12:24:27 +0530 Subject: [PATCH 1/9] Added NTheory methods to Binding List --- ext/symengine/symengine.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index e04deb8..f906b15 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -5,6 +5,7 @@ #include "ruby_rational.h" #include "ruby_constant.h" #include "ruby_function.h" +#include "ruby_ntheory.h" #include "symengine.h" /////////////////// @@ -145,4 +146,14 @@ void Init_symengine() { rb_define_module_function(m_symengine, "zeta", cfunction_zeta, 1); rb_define_module_function(m_symengine, "gamma", cfunction_gamma, 1); + //NTheory Functions as Module Level Functions + rb_define_module_function(m_symengine, "gcd", cfunction_gcd, 2); + rb_define_module_function(m_symengine, "lcm", cfunction_lcm, 2); + rb_define_module_function(m_symengine, "nextprime", cfunction_nextprime, 1); + rb_define_module_function(m_symengine, "mod", cfunction_mod, 2); + rb_define_module_function(m_symengine, "quotient", cfunction_quotient, 2); + rb_define_module_function(m_symengine, "fibonacci", cfunction_fibonacci, 1); + rb_define_module_function(m_symengine, "lucas", cfunction_lucas, 1); + rb_define_module_function(m_symengine, "binomial", cfunction_binomial, 2); + } From 5ad2393528d3e44decff24acd161c35a048cfc95 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 12 May 2016 12:36:39 +0530 Subject: [PATCH 2/9] NTheory Wrappers --- ext/symengine/ruby_ntheory.c | 58 ++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_ntheory.h | 23 ++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 ext/symengine/ruby_ntheory.c create mode 100644 ext/symengine/ruby_ntheory.h diff --git a/ext/symengine/ruby_ntheory.c b/ext/symengine/ruby_ntheory.c new file mode 100644 index 0000000..e39bcd0 --- /dev/null +++ b/ext/symengine/ruby_ntheory.c @@ -0,0 +1,58 @@ +#include "ruby_ntheory.h" + +VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) { + basic_struct *cresult; + VALUE result; + + basic cbasic_operand1; + basic_new_stack(cbasic_operand1); + sympify(operand1, cbasic_operand1); + + cresult = basic_new_heap(); + cwfunc_ptr(cresult, cbasic_operand1); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); + basic_free_stack(cbasic_operand1); + + return result; +} + +VALUE cfunction_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2) { + basic_struct *cresult; + VALUE result; + + basic cbasic_operand1; + basic_new_stack(cbasic_operand1); + sympify(operand1, cbasic_operand1); + + basic cbasic_operand2; + basic_new_stack(cbasic_operand2); + sympify(operand2, cbasic_operand2); + + cresult = basic_new_heap(); + cwfunc_ptr(cresult, cbasic_operand1, cbasic_operand2); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); + basic_free_stack(cbasic_operand1); + basic_free_stack(cbasic_operand2); + + return result; +} + +VALUE cfunction_nextprime(VALUE self, VALUE operand1) { + return cfunction_onearg(nthoery_gcd, operand1); +} + +VALUE cfunction_gcd(VALUE self, VALUE operand1, VALUE operand2) { + return cfunction_twoarg(ntheory_gcd, operand1, operand2); +} + +VALUE cfunction_lcm(VALUE self, VALUE operand1, VALUE operand2) { + return cfunction_twoarg(ntheory_lcm, operand1, operand2); +} + +VALUE cfunction_mod(VALUE self, VALUE operand1, VALUE operand2) { + return cfunction_twoarg(ntheory_mod, operand1, operand2); +} + +VALUE cfunction_quotient(VALUE self, VALUE operand1, VALUE operand2) { + return cfunction_twoarg(ntheory_quotient, operand1, operand2); +} diff --git a/ext/symengine/ruby_ntheory.h b/ext/symengine/ruby_ntheory.h new file mode 100644 index 0000000..3c52150 --- /dev/null +++ b/ext/symengine/ruby_ntheory.h @@ -0,0 +1,23 @@ +#ifndef RUBY_NTHEORY_H_ +#define RUBY_NTHEORY_H_ + +#include +#include + +#include "ruby_basic.h" +#include "symengine.h" +#include "symengine_macros.h" + +VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); +VALUE cfunction_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2); + +VALUE cfunction_gcd(VALUE self, VALUE operand1, VALUE operand2); +VALUE cfunction_lcm(VALUE self, VALUE operand1, VALUE operand2); +VALUE cfunction_nextprime(VALUE self, VALUE operand1); +VALUE cfunction_mod(VALUE self, VALUE operand1, VALUE operand2); +VALUE cfunction_quotient(VALUE self, VALUE operand1, VALUE operand2); +//VALUE cfunction_fibonacci(VALUE self, VALUE operand1); +//VALUE cfunction_lucas(VALUE self, VALUE operand1); +//VALUE cfunction_binomial(VALUE self, VALUE operand1, VALUE operand2); + +#endif //RUBY_NTHEORY_H_ From 6cd53d11a3a52013806b5ad06d75ea40f20a84e2 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 12 May 2016 19:48:01 +0530 Subject: [PATCH 3/9] Next Prime, GCD, LCM, mod and quotient working --- ext/symengine/CMakeLists.txt | 1 + ext/symengine/ruby_ntheory.c | 24 ++++++++++++------------ ext/symengine/ruby_ntheory.h | 20 ++++++++++---------- ext/symengine/symengine.c | 16 ++++++++-------- symengine_version.txt | 3 ++- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/ext/symengine/CMakeLists.txt b/ext/symengine/CMakeLists.txt index 0fad727..76d0bb9 100644 --- a/ext/symengine/CMakeLists.txt +++ b/ext/symengine/CMakeLists.txt @@ -5,6 +5,7 @@ set(RUBY_WRAPPER_SRC ruby_rational.c ruby_constant.c ruby_function.c + ruby_ntheory.c symengine_macros.c symengine.c ) diff --git a/ext/symengine/ruby_ntheory.c b/ext/symengine/ruby_ntheory.c index e39bcd0..b54e36b 100644 --- a/ext/symengine/ruby_ntheory.c +++ b/ext/symengine/ruby_ntheory.c @@ -1,6 +1,6 @@ #include "ruby_ntheory.h" -VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) { +VALUE cntheory_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) { basic_struct *cresult; VALUE result; @@ -16,7 +16,7 @@ VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), V return result; } -VALUE cfunction_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2) { +VALUE cntheory_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2) { basic_struct *cresult; VALUE result; @@ -37,22 +37,22 @@ VALUE cfunction_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, co return result; } -VALUE cfunction_nextprime(VALUE self, VALUE operand1) { - return cfunction_onearg(nthoery_gcd, operand1); +VALUE cntheory_nextprime(VALUE self, VALUE operand1) { + return cntheory_onearg(ntheory_nextprime, operand1); } -VALUE cfunction_gcd(VALUE self, VALUE operand1, VALUE operand2) { - return cfunction_twoarg(ntheory_gcd, operand1, operand2); +VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2) { + return cntheory_twoarg(ntheory_gcd, operand1, operand2); } -VALUE cfunction_lcm(VALUE self, VALUE operand1, VALUE operand2) { - return cfunction_twoarg(ntheory_lcm, operand1, operand2); +VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2) { + return cntheory_twoarg(ntheory_lcm, operand1, operand2); } -VALUE cfunction_mod(VALUE self, VALUE operand1, VALUE operand2) { - return cfunction_twoarg(ntheory_mod, operand1, operand2); +VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2) { + return cntheory_twoarg(ntheory_mod, operand1, operand2); } -VALUE cfunction_quotient(VALUE self, VALUE operand1, VALUE operand2) { - return cfunction_twoarg(ntheory_quotient, operand1, operand2); +VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2) { + return cntheory_twoarg(ntheory_quotient, operand1, operand2); } diff --git a/ext/symengine/ruby_ntheory.h b/ext/symengine/ruby_ntheory.h index 3c52150..4f10108 100644 --- a/ext/symengine/ruby_ntheory.h +++ b/ext/symengine/ruby_ntheory.h @@ -8,16 +8,16 @@ #include "symengine.h" #include "symengine_macros.h" -VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); -VALUE cfunction_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2); +VALUE cntheory_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); +VALUE cntheory_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2); -VALUE cfunction_gcd(VALUE self, VALUE operand1, VALUE operand2); -VALUE cfunction_lcm(VALUE self, VALUE operand1, VALUE operand2); -VALUE cfunction_nextprime(VALUE self, VALUE operand1); -VALUE cfunction_mod(VALUE self, VALUE operand1, VALUE operand2); -VALUE cfunction_quotient(VALUE self, VALUE operand1, VALUE operand2); -//VALUE cfunction_fibonacci(VALUE self, VALUE operand1); -//VALUE cfunction_lucas(VALUE self, VALUE operand1); -//VALUE cfunction_binomial(VALUE self, VALUE operand1, VALUE operand2); +VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2); +VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2); +VALUE cntheory_nextprime(VALUE self, VALUE operand1); +VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2); +VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2); +//VALUE cntheory_fibonacci(VALUE self, VALUE operand1); +//VALUE cntheory_lucas(VALUE self, VALUE operand1); +//VALUE cntheory_binomial(VALUE self, VALUE operand1, VALUE operand2); #endif //RUBY_NTHEORY_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index f906b15..e00532e 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -147,13 +147,13 @@ void Init_symengine() { rb_define_module_function(m_symengine, "gamma", cfunction_gamma, 1); //NTheory Functions as Module Level Functions - rb_define_module_function(m_symengine, "gcd", cfunction_gcd, 2); - rb_define_module_function(m_symengine, "lcm", cfunction_lcm, 2); - rb_define_module_function(m_symengine, "nextprime", cfunction_nextprime, 1); - rb_define_module_function(m_symengine, "mod", cfunction_mod, 2); - rb_define_module_function(m_symengine, "quotient", cfunction_quotient, 2); - rb_define_module_function(m_symengine, "fibonacci", cfunction_fibonacci, 1); - rb_define_module_function(m_symengine, "lucas", cfunction_lucas, 1); - rb_define_module_function(m_symengine, "binomial", cfunction_binomial, 2); + rb_define_module_function(m_symengine, "gcd", cntheory_gcd, 2); + rb_define_module_function(m_symengine, "lcm", cntheory_lcm, 2); + rb_define_module_function(m_symengine, "nextprime", cntheory_nextprime, 1); + rb_define_module_function(m_symengine, "mod", cntheory_mod, 2); + rb_define_module_function(m_symengine, "quotient", cntheory_quotient, 2); + //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); } diff --git a/symengine_version.txt b/symengine_version.txt index f34b6ce..642392c 100644 --- a/symengine_version.txt +++ b/symengine_version.txt @@ -1 +1,2 @@ -46f54aeafbf2a93be0c8b1d492666f56f0ccbe73 +5e88e62b42c38200573eecf688d90b46beb38a77 + From d57e5f5509792986987e790a70c3ea0aba30bd7f Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 12 May 2016 20:21:20 +0530 Subject: [PATCH 4/9] Ntheory functions wrapped --- ext/symengine/ruby_ntheory.c | 47 ++++++++++++++++++++++++++++++++++++ ext/symengine/ruby_ntheory.h | 6 ++--- ext/symengine/symengine.c | 6 ++--- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/ext/symengine/ruby_ntheory.c b/ext/symengine/ruby_ntheory.c index b54e36b..c8dcec6 100644 --- a/ext/symengine/ruby_ntheory.c +++ b/ext/symengine/ruby_ntheory.c @@ -56,3 +56,50 @@ VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2) { VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2) { return cntheory_twoarg(ntheory_quotient, operand1, operand2); } + +VALUE cntheory_fibonacci(VALUE self, VALUE operand1){ + basic_struct *cresult; + VALUE result; + + unsigned long cbasic_operand1; + cbasic_operand1 = NUM2ULONG(operand1); + + cresult = basic_new_heap(); + ntheory_fibonacci(cresult, cbasic_operand1); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); + + return result; +} + +VALUE cntheory_lucas(VALUE self, VALUE operand1){ + basic_struct *cresult; + VALUE result; + + unsigned long cbasic_operand1; + cbasic_operand1 = NUM2ULONG(operand1); + + cresult = basic_new_heap(); + ntheory_lucas(cresult, cbasic_operand1); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); + + return result; +} + +VALUE cntheory_binomial(VALUE self, VALUE operand1, VALUE operand2){ + basic_struct *cresult; + VALUE result; + + basic cbasic_operand1; + basic_new_stack(cbasic_operand1); + sympify(operand1, cbasic_operand1); + + unsigned long cbasic_operand2; + cbasic_operand2 = NUM2ULONG(operand2); + + cresult = basic_new_heap(); + ntheory_binomial(cresult, cbasic_operand1, cbasic_operand2); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); + basic_free_stack(cbasic_operand1); + + return result; +} diff --git a/ext/symengine/ruby_ntheory.h b/ext/symengine/ruby_ntheory.h index 4f10108..2c26ecc 100644 --- a/ext/symengine/ruby_ntheory.h +++ b/ext/symengine/ruby_ntheory.h @@ -16,8 +16,8 @@ VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2); VALUE cntheory_nextprime(VALUE self, VALUE operand1); VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2); VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2); -//VALUE cntheory_fibonacci(VALUE self, VALUE operand1); -//VALUE cntheory_lucas(VALUE self, VALUE operand1); -//VALUE cntheory_binomial(VALUE self, VALUE operand1, VALUE operand2); +VALUE cntheory_fibonacci(VALUE self, VALUE operand1); +VALUE cntheory_lucas(VALUE self, VALUE operand1); +VALUE cntheory_binomial(VALUE self, VALUE operand1, VALUE operand2); #endif //RUBY_NTHEORY_H_ diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index e00532e..00edf27 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -152,8 +152,8 @@ void Init_symengine() { rb_define_module_function(m_symengine, "nextprime", cntheory_nextprime, 1); rb_define_module_function(m_symengine, "mod", cntheory_mod, 2); rb_define_module_function(m_symengine, "quotient", cntheory_quotient, 2); - //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); + 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); } From 94614ce0bd318bb02b33e495af9eb7204e21120a Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 13 May 2016 09:13:50 +0530 Subject: [PATCH 5/9] Restructuring macros->utils --- ext/symengine/CMakeLists.txt | 2 +- ext/symengine/ruby_basic.h | 2 +- ext/symengine/ruby_constant.h | 3 +- ext/symengine/ruby_function.c | 19 +------- ext/symengine/ruby_function.h | 3 +- ext/symengine/ruby_ntheory.c | 47 ++----------------- ext/symengine/ruby_ntheory.h | 3 +- .../{symengine_macros.c => symengine_utils.c} | 39 ++++++++++++++- .../{symengine_macros.h => symengine_utils.h} | 5 ++ 9 files changed, 54 insertions(+), 69 deletions(-) rename ext/symengine/{symengine_macros.c => symengine_utils.c} (72%) rename ext/symengine/{symengine_macros.h => symengine_utils.h} (65%) diff --git a/ext/symengine/CMakeLists.txt b/ext/symengine/CMakeLists.txt index 76d0bb9..b587bb6 100644 --- a/ext/symengine/CMakeLists.txt +++ b/ext/symengine/CMakeLists.txt @@ -6,7 +6,7 @@ set(RUBY_WRAPPER_SRC ruby_constant.c ruby_function.c ruby_ntheory.c - symengine_macros.c + symengine_utils.c symengine.c ) diff --git a/ext/symengine/ruby_basic.h b/ext/symengine/ruby_basic.h index 62d9bc2..792efaa 100644 --- a/ext/symengine/ruby_basic.h +++ b/ext/symengine/ruby_basic.h @@ -4,7 +4,7 @@ #include #include -#include "symengine_macros.h" +#include "symengine_utils.h" void cbasic_free(void *ptr); diff --git a/ext/symengine/ruby_constant.h b/ext/symengine/ruby_constant.h index d0f0454..bfaeabe 100644 --- a/ext/symengine/ruby_constant.h +++ b/ext/symengine/ruby_constant.h @@ -4,9 +4,8 @@ #include #include -#include "ruby_basic.h" #include "symengine.h" -#include "symengine_macros.h" +#include "symengine_utils.h" VALUE cconstant_const(void (*cwfunc_ptr)(basic_struct*)); diff --git a/ext/symengine/ruby_function.c b/ext/symengine/ruby_function.c index f4c8783..e8be33a 100644 --- a/ext/symengine/ruby_function.c +++ b/ext/symengine/ruby_function.c @@ -1,25 +1,8 @@ #include "ruby_function.h" - -VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) { - basic_struct *cresult; - VALUE result; - - basic cbasic_operand1; - basic_new_stack(cbasic_operand1); - sympify(operand1, cbasic_operand1); - - cresult = basic_new_heap(); - cwfunc_ptr(cresult, cbasic_operand1); - result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); - basic_free_stack(cbasic_operand1); - - return result; -} - #define IMPLEMENT_ONE_ARG_FUNC(func) \ VALUE cfunction_ ## func(VALUE self, VALUE operand1) { \ - return cfunction_onearg(basic_ ## func, operand1); \ + return function_onearg(basic_ ## func, operand1); \ } IMPLEMENT_ONE_ARG_FUNC(abs); diff --git a/ext/symengine/ruby_function.h b/ext/symengine/ruby_function.h index d40bd58..3ea1db0 100644 --- a/ext/symengine/ruby_function.h +++ b/ext/symengine/ruby_function.h @@ -4,9 +4,8 @@ #include #include -#include "ruby_basic.h" #include "symengine.h" -#include "symengine_macros.h" +#include "symengine_utils.h" VALUE cfunction_func(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); diff --git a/ext/symengine/ruby_ntheory.c b/ext/symengine/ruby_ntheory.c index c8dcec6..b120646 100644 --- a/ext/symengine/ruby_ntheory.c +++ b/ext/symengine/ruby_ntheory.c @@ -1,60 +1,23 @@ #include "ruby_ntheory.h" -VALUE cntheory_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) { - basic_struct *cresult; - VALUE result; - - basic cbasic_operand1; - basic_new_stack(cbasic_operand1); - sympify(operand1, cbasic_operand1); - - cresult = basic_new_heap(); - cwfunc_ptr(cresult, cbasic_operand1); - result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); - basic_free_stack(cbasic_operand1); - - return result; -} - -VALUE cntheory_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2) { - basic_struct *cresult; - VALUE result; - - basic cbasic_operand1; - basic_new_stack(cbasic_operand1); - sympify(operand1, cbasic_operand1); - - basic cbasic_operand2; - basic_new_stack(cbasic_operand2); - sympify(operand2, cbasic_operand2); - - cresult = basic_new_heap(); - cwfunc_ptr(cresult, cbasic_operand1, cbasic_operand2); - result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); - basic_free_stack(cbasic_operand1); - basic_free_stack(cbasic_operand2); - - return result; -} - VALUE cntheory_nextprime(VALUE self, VALUE operand1) { - return cntheory_onearg(ntheory_nextprime, operand1); + return function_onearg(ntheory_nextprime, operand1); } VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2) { - return cntheory_twoarg(ntheory_gcd, operand1, operand2); + return function_twoarg(ntheory_gcd, operand1, operand2); } VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2) { - return cntheory_twoarg(ntheory_lcm, operand1, operand2); + return function_twoarg(ntheory_lcm, operand1, operand2); } VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2) { - return cntheory_twoarg(ntheory_mod, operand1, operand2); + return function_twoarg(ntheory_mod, operand1, operand2); } VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2) { - return cntheory_twoarg(ntheory_quotient, operand1, operand2); + return function_twoarg(ntheory_quotient, operand1, operand2); } VALUE cntheory_fibonacci(VALUE self, VALUE operand1){ diff --git a/ext/symengine/ruby_ntheory.h b/ext/symengine/ruby_ntheory.h index 2c26ecc..33db76c 100644 --- a/ext/symengine/ruby_ntheory.h +++ b/ext/symengine/ruby_ntheory.h @@ -4,9 +4,8 @@ #include #include -#include "ruby_basic.h" #include "symengine.h" -#include "symengine_macros.h" +#include "symengine_utils.h" VALUE cntheory_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); VALUE cntheory_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2); diff --git a/ext/symengine/symengine_macros.c b/ext/symengine/symengine_utils.c similarity index 72% rename from ext/symengine/symengine_macros.c rename to ext/symengine/symengine_utils.c index 5c18744..8de55aa 100644 --- a/ext/symengine/symengine_macros.c +++ b/ext/symengine/symengine_utils.c @@ -1,4 +1,4 @@ -#include "symengine_macros.h" +#include "symengine_utils.h" #include "symengine.h" void sympify(VALUE operand2, basic_struct *cbasic_operand2) { @@ -111,3 +111,40 @@ VALUE Klass_of_Basic(const basic_struct *basic_ptr) { return c_basic; } } + +VALUE function_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) { + basic_struct *cresult; + VALUE result; + + basic cbasic_operand1; + basic_new_stack(cbasic_operand1); + sympify(operand1, cbasic_operand1); + + cresult = basic_new_heap(); + cwfunc_ptr(cresult, cbasic_operand1); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); + basic_free_stack(cbasic_operand1); + + return result; +} + +VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2) { + basic_struct *cresult; + VALUE result; + + basic cbasic_operand1; + basic_new_stack(cbasic_operand1); + sympify(operand1, cbasic_operand1); + + basic cbasic_operand2; + basic_new_stack(cbasic_operand2); + sympify(operand2, cbasic_operand2); + + cresult = basic_new_heap(); + cwfunc_ptr(cresult, cbasic_operand1, cbasic_operand2); + result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult); + basic_free_stack(cbasic_operand1); + basic_free_stack(cbasic_operand2); + + return result; +} diff --git a/ext/symengine/symengine_macros.h b/ext/symengine/symengine_utils.h similarity index 65% rename from ext/symengine/symengine_macros.h rename to ext/symengine/symengine_utils.h index 6de1562..79c9b08 100644 --- a/ext/symengine/symengine_macros.h +++ b/ext/symengine/symengine_utils.h @@ -2,12 +2,17 @@ #define SYMENGINE_MACROS_H_ #include "ruby.h" +#include "ruby_basic.h" #include "symengine/cwrapper.h" //Returns the pointer wrapped inside the Ruby VALUE void sympify(VALUE operand2, basic_struct *cbasic_operand2); //Returns the Ruby class of the corresponding basic_struct pointer VALUE Klass_of_Basic(const basic_struct *basic_ptr); +//Returns the result from the function pointed by cwfunc_ptr: for one argument functions +VALUE function_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); +//Returns the result from the function pointed by cwfunc_ptr: for two argument functions +VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2); //Obtains the value from Ruby Fixnum or Bignum to an already allocated basic_struct #define GET_SYMINTFROMVAL(num_value, this) { \ From 719470c0340ebacc70b527537f5fe73ec26bc4c2 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 13 May 2016 13:35:01 +0530 Subject: [PATCH 6/9] Tests Done --- spec/ntheory_spec.rb | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 spec/ntheory_spec.rb diff --git a/spec/ntheory_spec.rb b/spec/ntheory_spec.rb new file mode 100644 index 0000000..e948dc5 --- /dev/null +++ b/spec/ntheory_spec.rb @@ -0,0 +1,82 @@ +require 'spec_helper' + +describe SymEngine do + before :each do + end + + describe 'NTheory' do + + describe '#gcd' do + context 'GCD of 2 and 4' do + it 'returns 2' do + f = SymEngine::gcd(2, 4) + expect(f).to eql(2) + end + end + end + + describe '#lcm' do + context 'LCM of 2 and 4' do + it 'returns 4' do + f = SymEngine::lcm(2, 4) + expect(f).to eql(4) + end + end + end + + describe '#nextprime' do + context 'NextPrime after 4' do + it 'returns 5' do + f = SymEngine::nextprime(4) + expect(f).to eql(5) + end + end + end + + describe '#mod' do + context '5 mod 4' do + it 'returns 1' do + f = SymEngine::mod(5, 4) + expect(f).to eql(1) + end + end + end + + describe '#quotient' do + context 'quotient of 5 divided by 2' do + it 'returns 2' do + f = SymEngine::quotient(5, 2) + expect(f).to eql(2) + end + end + end + + describe '#fibonacci' do + context '5th Fibonacci Number' do + it 'returns 5' do + f = SymEngine::fibonacci(5) + expect(f).to eql(5) + end + end + end + + describe '#lucas' do + context '1st Lucas Number' do + it 'returns 1' do + f = SymEngine::fibonacci(1) + expect(f).to eql(1) + end + end + end + + describe '#binomial' do + context 'binomial (n=5, k=1)' do + it 'returns 5' do + f = SymEngine::binomial(5, 1) + expect(f).to eql(5) + end + end + end + + end +end From 515b7152dbdd5e506535a7a1e379473b89c1bc94 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 13 May 2016 20:15:16 +0530 Subject: [PATCH 7/9] Fixed incompatibility of mod operation --- ext/symengine/ruby_ntheory.c | 4 +++- ext/symengine/ruby_ntheory.h | 1 + ext/symengine/symengine_utils.c | 3 ++- ext/symengine/symengine_utils.h | 3 ++- spec/ntheory_spec.rb | 18 ++++++++++++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ext/symengine/ruby_ntheory.c b/ext/symengine/ruby_ntheory.c index b120646..a04b4f6 100644 --- a/ext/symengine/ruby_ntheory.c +++ b/ext/symengine/ruby_ntheory.c @@ -13,7 +13,9 @@ VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2) { } VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2) { - return function_twoarg(ntheory_mod, operand1, operand2); + VALUE ans = function_twoarg(ntheory_mod, operand1, operand2); + VALUE ans1 = cbasic_add(ans, operand2); + return function_twoarg(ntheory_mod, ans1, operand2); } VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2) { diff --git a/ext/symengine/ruby_ntheory.h b/ext/symengine/ruby_ntheory.h index 33db76c..f1eb122 100644 --- a/ext/symengine/ruby_ntheory.h +++ b/ext/symengine/ruby_ntheory.h @@ -5,6 +5,7 @@ #include #include "symengine.h" +#include "ruby_basic.h" #include "symengine_utils.h" VALUE cntheory_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); diff --git a/ext/symengine/symengine_utils.c b/ext/symengine/symengine_utils.c index 8de55aa..f914ac3 100644 --- a/ext/symengine/symengine_utils.c +++ b/ext/symengine/symengine_utils.c @@ -128,7 +128,8 @@ VALUE function_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VA return result; } -VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2) { +VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), + VALUE operand1, VALUE operand2) { basic_struct *cresult; VALUE result; diff --git a/ext/symengine/symengine_utils.h b/ext/symengine/symengine_utils.h index 79c9b08..572b839 100644 --- a/ext/symengine/symengine_utils.h +++ b/ext/symengine/symengine_utils.h @@ -12,7 +12,8 @@ VALUE Klass_of_Basic(const basic_struct *basic_ptr); //Returns the result from the function pointed by cwfunc_ptr: for one argument functions VALUE function_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); //Returns the result from the function pointed by cwfunc_ptr: for two argument functions -VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2); +VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), + VALUE operand1, VALUE operand2); //Obtains the value from Ruby Fixnum or Bignum to an already allocated basic_struct #define GET_SYMINTFROMVAL(num_value, this) { \ diff --git a/spec/ntheory_spec.rb b/spec/ntheory_spec.rb index e948dc5..ec958ef 100644 --- a/spec/ntheory_spec.rb +++ b/spec/ntheory_spec.rb @@ -40,6 +40,24 @@ expect(f).to eql(1) end end + context '-5 mod -4' do + it 'returns -1' do + f = SymEngine::mod(-5, -4) + expect(f).to eql(-1) + end + end + context '5 mod -4' do + it 'returns -3' do + f = SymEngine::mod(5, -4) + expect(f).to eql(-3) + end + end + context '-5 mod 4' do + it 'returns 3' do + f = SymEngine::mod(-5, 4) + expect(f).to eql(3) + end + end end describe '#quotient' do From 6b29a732ea997218c53dc71beb0fcd74f319f7d6 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 13 May 2016 23:07:46 +0530 Subject: [PATCH 8/9] changed mod -> % --- ext/symengine/ruby_function.h | 2 -- ext/symengine/ruby_ntheory.h | 3 --- ext/symengine/symengine.c | 2 +- spec/ntheory_spec.rb | 8 ++++---- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/ext/symengine/ruby_function.h b/ext/symengine/ruby_function.h index 3ea1db0..e55b63b 100644 --- a/ext/symengine/ruby_function.h +++ b/ext/symengine/ruby_function.h @@ -7,8 +7,6 @@ #include "symengine.h" #include "symengine_utils.h" -VALUE cfunction_func(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); - VALUE cfunction_abs(VALUE self, VALUE operand1); VALUE cfunction_sin(VALUE self, VALUE operand1); VALUE cfunction_cos(VALUE self, VALUE operand1); diff --git a/ext/symengine/ruby_ntheory.h b/ext/symengine/ruby_ntheory.h index f1eb122..2a0ce80 100644 --- a/ext/symengine/ruby_ntheory.h +++ b/ext/symengine/ruby_ntheory.h @@ -8,9 +8,6 @@ #include "ruby_basic.h" #include "symengine_utils.h" -VALUE cntheory_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1); -VALUE cntheory_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2); - VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2); VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2); VALUE cntheory_nextprime(VALUE self, VALUE operand1); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index 00edf27..e7f01f8 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -150,7 +150,7 @@ void Init_symengine() { rb_define_module_function(m_symengine, "gcd", cntheory_gcd, 2); rb_define_module_function(m_symengine, "lcm", cntheory_lcm, 2); rb_define_module_function(m_symengine, "nextprime", cntheory_nextprime, 1); - rb_define_module_function(m_symengine, "mod", cntheory_mod, 2); + rb_define_module_function(m_symengine, "%", cntheory_mod, 2); rb_define_module_function(m_symengine, "quotient", cntheory_quotient, 2); rb_define_module_function(m_symengine, "fibonacci", cntheory_fibonacci, 1); rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1); diff --git a/spec/ntheory_spec.rb b/spec/ntheory_spec.rb index ec958ef..9e6f6f4 100644 --- a/spec/ntheory_spec.rb +++ b/spec/ntheory_spec.rb @@ -36,25 +36,25 @@ describe '#mod' do context '5 mod 4' do it 'returns 1' do - f = SymEngine::mod(5, 4) + f = 5 % 4 expect(f).to eql(1) end end context '-5 mod -4' do it 'returns -1' do - f = SymEngine::mod(-5, -4) + f = -5 % -4 expect(f).to eql(-1) end end context '5 mod -4' do it 'returns -3' do - f = SymEngine::mod(5, -4) + f = 5 % -4 expect(f).to eql(-3) end end context '-5 mod 4' do it 'returns 3' do - f = SymEngine::mod(-5, 4) + f = -5 % 4 expect(f).to eql(3) end end From f29f4c4332525467694a1a3121048a8c3da6ed3c Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Tue, 17 May 2016 09:59:33 +0530 Subject: [PATCH 9/9] SymEngine::Integer to int implicit conversion --- ext/symengine/ruby_integer.c | 1 + ext/symengine/ruby_ntheory.c | 4 ++-- ext/symengine/ruby_ntheory.h | 2 +- ext/symengine/symengine.c | 2 +- lib/symengine.rb | 1 + lib/symengine/integer.rb | 7 +++++++ spec/ntheory_spec.rb | 36 +++++++++++++++++++++++++++++++----- 7 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 lib/symengine/integer.rb diff --git a/ext/symengine/ruby_integer.c b/ext/symengine/ruby_integer.c index 229a4c5..b61a34f 100644 --- a/ext/symengine/ruby_integer.c +++ b/ext/symengine/ruby_integer.c @@ -6,3 +6,4 @@ VALUE cinteger_init(VALUE self, VALUE num_value) { GET_SYMINTFROMVAL(num_value, this); return self; } + diff --git a/ext/symengine/ruby_ntheory.c b/ext/symengine/ruby_ntheory.c index a04b4f6..141ee70 100644 --- a/ext/symengine/ruby_ntheory.c +++ b/ext/symengine/ruby_ntheory.c @@ -12,8 +12,8 @@ VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2) { return function_twoarg(ntheory_lcm, operand1, operand2); } -VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2) { - VALUE ans = function_twoarg(ntheory_mod, operand1, operand2); +VALUE cntheory_mod(VALUE self, VALUE operand2) { + VALUE ans = function_twoarg(ntheory_mod, self, operand2); VALUE ans1 = cbasic_add(ans, operand2); return function_twoarg(ntheory_mod, ans1, operand2); } diff --git a/ext/symengine/ruby_ntheory.h b/ext/symengine/ruby_ntheory.h index 2a0ce80..a5508c0 100644 --- a/ext/symengine/ruby_ntheory.h +++ b/ext/symengine/ruby_ntheory.h @@ -11,7 +11,7 @@ VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2); VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2); VALUE cntheory_nextprime(VALUE self, VALUE operand1); -VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2); +VALUE cntheory_mod(VALUE self, VALUE operand2); VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2); VALUE cntheory_fibonacci(VALUE self, VALUE operand1); VALUE cntheory_lucas(VALUE self, VALUE operand1); diff --git a/ext/symengine/symengine.c b/ext/symengine/symengine.c index e7f01f8..2f9f22c 100644 --- a/ext/symengine/symengine.c +++ b/ext/symengine/symengine.c @@ -53,6 +53,7 @@ void Init_symengine() { c_integer = rb_define_class_under(m_symengine, "Integer", c_basic); rb_define_alloc_func(c_integer, cbasic_alloc); rb_define_method(c_integer, "initialize", cinteger_init, 1); + rb_define_method(c_integer, "%", cntheory_mod, 1); //Rational class c_rational = rb_define_class_under(m_symengine, "Rational", c_basic); @@ -150,7 +151,6 @@ void Init_symengine() { rb_define_module_function(m_symengine, "gcd", cntheory_gcd, 2); rb_define_module_function(m_symengine, "lcm", cntheory_lcm, 2); rb_define_module_function(m_symengine, "nextprime", cntheory_nextprime, 1); - rb_define_module_function(m_symengine, "%", cntheory_mod, 2); rb_define_module_function(m_symengine, "quotient", cntheory_quotient, 2); rb_define_module_function(m_symengine, "fibonacci", cntheory_fibonacci, 1); rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1); diff --git a/lib/symengine.rb b/lib/symengine.rb index b454130..31e5f3e 100644 --- a/lib/symengine.rb +++ b/lib/symengine.rb @@ -34,3 +34,4 @@ def symbols ary_or_string, *params require 'symengine/symengine' require 'symengine/iruby' require 'symengine/basic' +require 'symengine/integer' diff --git a/lib/symengine/integer.rb b/lib/symengine/integer.rb new file mode 100644 index 0000000..965eff6 --- /dev/null +++ b/lib/symengine/integer.rb @@ -0,0 +1,7 @@ +module SymEngine + class Integer + def to_int + self.to_s.to_i + end + end +end diff --git a/spec/ntheory_spec.rb b/spec/ntheory_spec.rb index 9e6f6f4..20ed4ae 100644 --- a/spec/ntheory_spec.rb +++ b/spec/ntheory_spec.rb @@ -1,10 +1,18 @@ require 'spec_helper' describe SymEngine do + before :each do end describe 'NTheory' do + before :each do + @i1 = SymEngine::Integer.new(1) + @i4 = SymEngine::Integer.new(4) + @i5 = SymEngine::Integer.new(5) + @im4 = SymEngine::Integer.new(-4) + @im5 = SymEngine::Integer.new(-5) + end describe '#gcd' do context 'GCD of 2 and 4' do @@ -36,25 +44,25 @@ describe '#mod' do context '5 mod 4' do it 'returns 1' do - f = 5 % 4 + f = @i5 % @i4 expect(f).to eql(1) end end context '-5 mod -4' do it 'returns -1' do - f = -5 % -4 + f = @im5 % @im4 expect(f).to eql(-1) end end context '5 mod -4' do it 'returns -3' do - f = 5 % -4 + f = @i5 % @im4 expect(f).to eql(-3) end end context '-5 mod 4' do it 'returns 3' do - f = -5 % 4 + f = @im5 % @i4 expect(f).to eql(3) end end @@ -76,12 +84,24 @@ expect(f).to eql(5) end end + context '5th Fibonacci Number' do + it 'returns 5' do + f = SymEngine::fibonacci(@i5) + expect(f).to eql(5) + end + end end describe '#lucas' do context '1st Lucas Number' do it 'returns 1' do - f = SymEngine::fibonacci(1) + f = SymEngine::lucas(1) + expect(f).to eql(1) + end + end + context '1st Lucas Number' do + it 'returns 1' do + f = SymEngine::lucas(@i1) expect(f).to eql(1) end end @@ -94,6 +114,12 @@ expect(f).to eql(5) end end + context 'binomial (n=5, k=1)' do + it 'returns 5' do + f = SymEngine::binomial(@i5, @i1) + expect(f).to eql(5) + end + end end end