Skip to content

Commit 7ea8a63

Browse files
committed
Merge pull request #38 from rajithv/ntheory
Ruby Wrappers for NTheory
2 parents 7c5f114 + f29f4c4 commit 7ea8a63

File tree

15 files changed

+289
-28
lines changed

15 files changed

+289
-28
lines changed

ext/symengine/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ set(RUBY_WRAPPER_SRC
55
ruby_rational.c
66
ruby_constant.c
77
ruby_function.c
8-
symengine_macros.c
8+
ruby_ntheory.c
9+
symengine_utils.c
910
symengine.c
1011
)
1112

ext/symengine/ruby_basic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ruby.h>
55
#include <symengine/cwrapper.h>
66

7-
#include "symengine_macros.h"
7+
#include "symengine_utils.h"
88

99
void cbasic_free(void *ptr);
1010

ext/symengine/ruby_constant.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
#include <ruby.h>
55
#include <symengine/cwrapper.h>
66

7-
#include "ruby_basic.h"
87
#include "symengine.h"
9-
#include "symengine_macros.h"
8+
#include "symengine_utils.h"
109

1110
VALUE cconstant_const(void (*cwfunc_ptr)(basic_struct*));
1211

ext/symengine/ruby_function.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
#include "ruby_function.h"
22

3-
4-
VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) {
5-
basic_struct *cresult;
6-
VALUE result;
7-
8-
basic cbasic_operand1;
9-
basic_new_stack(cbasic_operand1);
10-
sympify(operand1, cbasic_operand1);
11-
12-
cresult = basic_new_heap();
13-
cwfunc_ptr(cresult, cbasic_operand1);
14-
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
15-
basic_free_stack(cbasic_operand1);
16-
17-
return result;
18-
}
19-
203
#define IMPLEMENT_ONE_ARG_FUNC(func) \
214
VALUE cfunction_ ## func(VALUE self, VALUE operand1) { \
22-
return cfunction_onearg(basic_ ## func, operand1); \
5+
return function_onearg(basic_ ## func, operand1); \
236
}
247

258
IMPLEMENT_ONE_ARG_FUNC(abs);

ext/symengine/ruby_function.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
#include <ruby.h>
55
#include <symengine/cwrapper.h>
66

7-
#include "ruby_basic.h"
87
#include "symengine.h"
9-
#include "symengine_macros.h"
10-
11-
VALUE cfunction_func(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1);
8+
#include "symengine_utils.h"
129

1310
VALUE cfunction_abs(VALUE self, VALUE operand1);
1411
VALUE cfunction_sin(VALUE self, VALUE operand1);

ext/symengine/ruby_integer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ VALUE cinteger_init(VALUE self, VALUE num_value) {
66
GET_SYMINTFROMVAL(num_value, this);
77
return self;
88
}
9+

ext/symengine/ruby_ntheory.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "ruby_ntheory.h"
2+
3+
VALUE cntheory_nextprime(VALUE self, VALUE operand1) {
4+
return function_onearg(ntheory_nextprime, operand1);
5+
}
6+
7+
VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2) {
8+
return function_twoarg(ntheory_gcd, operand1, operand2);
9+
}
10+
11+
VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2) {
12+
return function_twoarg(ntheory_lcm, operand1, operand2);
13+
}
14+
15+
VALUE cntheory_mod(VALUE self, VALUE operand2) {
16+
VALUE ans = function_twoarg(ntheory_mod, self, operand2);
17+
VALUE ans1 = cbasic_add(ans, operand2);
18+
return function_twoarg(ntheory_mod, ans1, operand2);
19+
}
20+
21+
VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2) {
22+
return function_twoarg(ntheory_quotient, operand1, operand2);
23+
}
24+
25+
VALUE cntheory_fibonacci(VALUE self, VALUE operand1){
26+
basic_struct *cresult;
27+
VALUE result;
28+
29+
unsigned long cbasic_operand1;
30+
cbasic_operand1 = NUM2ULONG(operand1);
31+
32+
cresult = basic_new_heap();
33+
ntheory_fibonacci(cresult, cbasic_operand1);
34+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
35+
36+
return result;
37+
}
38+
39+
VALUE cntheory_lucas(VALUE self, VALUE operand1){
40+
basic_struct *cresult;
41+
VALUE result;
42+
43+
unsigned long cbasic_operand1;
44+
cbasic_operand1 = NUM2ULONG(operand1);
45+
46+
cresult = basic_new_heap();
47+
ntheory_lucas(cresult, cbasic_operand1);
48+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
49+
50+
return result;
51+
}
52+
53+
VALUE cntheory_binomial(VALUE self, VALUE operand1, VALUE operand2){
54+
basic_struct *cresult;
55+
VALUE result;
56+
57+
basic cbasic_operand1;
58+
basic_new_stack(cbasic_operand1);
59+
sympify(operand1, cbasic_operand1);
60+
61+
unsigned long cbasic_operand2;
62+
cbasic_operand2 = NUM2ULONG(operand2);
63+
64+
cresult = basic_new_heap();
65+
ntheory_binomial(cresult, cbasic_operand1, cbasic_operand2);
66+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
67+
basic_free_stack(cbasic_operand1);
68+
69+
return result;
70+
}

ext/symengine/ruby_ntheory.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef RUBY_NTHEORY_H_
2+
#define RUBY_NTHEORY_H_
3+
4+
#include <ruby.h>
5+
#include <symengine/cwrapper.h>
6+
7+
#include "symengine.h"
8+
#include "ruby_basic.h"
9+
#include "symengine_utils.h"
10+
11+
VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2);
12+
VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2);
13+
VALUE cntheory_nextprime(VALUE self, VALUE operand1);
14+
VALUE cntheory_mod(VALUE self, VALUE operand2);
15+
VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2);
16+
VALUE cntheory_fibonacci(VALUE self, VALUE operand1);
17+
VALUE cntheory_lucas(VALUE self, VALUE operand1);
18+
VALUE cntheory_binomial(VALUE self, VALUE operand1, VALUE operand2);
19+
20+
#endif //RUBY_NTHEORY_H_

ext/symengine/symengine.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "ruby_rational.h"
66
#include "ruby_constant.h"
77
#include "ruby_function.h"
8+
#include "ruby_ntheory.h"
89
#include "symengine.h"
910

1011
///////////////////
@@ -52,6 +53,7 @@ void Init_symengine() {
5253
c_integer = rb_define_class_under(m_symengine, "Integer", c_basic);
5354
rb_define_alloc_func(c_integer, cbasic_alloc);
5455
rb_define_method(c_integer, "initialize", cinteger_init, 1);
56+
rb_define_method(c_integer, "%", cntheory_mod, 1);
5557

5658
//Rational class
5759
c_rational = rb_define_class_under(m_symengine, "Rational", c_basic);
@@ -145,4 +147,13 @@ void Init_symengine() {
145147
rb_define_module_function(m_symengine, "zeta", cfunction_zeta, 1);
146148
rb_define_module_function(m_symengine, "gamma", cfunction_gamma, 1);
147149

150+
//NTheory Functions as Module Level Functions
151+
rb_define_module_function(m_symengine, "gcd", cntheory_gcd, 2);
152+
rb_define_module_function(m_symengine, "lcm", cntheory_lcm, 2);
153+
rb_define_module_function(m_symengine, "nextprime", cntheory_nextprime, 1);
154+
rb_define_module_function(m_symengine, "quotient", cntheory_quotient, 2);
155+
rb_define_module_function(m_symengine, "fibonacci", cntheory_fibonacci, 1);
156+
rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1);
157+
rb_define_module_function(m_symengine, "binomial", cntheory_binomial, 2);
158+
148159
}

ext/symengine/symengine_macros.c renamed to ext/symengine/symengine_utils.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "symengine_macros.h"
1+
#include "symengine_utils.h"
22
#include "symengine.h"
33

44
void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
@@ -111,3 +111,41 @@ VALUE Klass_of_Basic(const basic_struct *basic_ptr) {
111111
return c_basic;
112112
}
113113
}
114+
115+
VALUE function_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) {
116+
basic_struct *cresult;
117+
VALUE result;
118+
119+
basic cbasic_operand1;
120+
basic_new_stack(cbasic_operand1);
121+
sympify(operand1, cbasic_operand1);
122+
123+
cresult = basic_new_heap();
124+
cwfunc_ptr(cresult, cbasic_operand1);
125+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
126+
basic_free_stack(cbasic_operand1);
127+
128+
return result;
129+
}
130+
131+
VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*),
132+
VALUE operand1, VALUE operand2) {
133+
basic_struct *cresult;
134+
VALUE result;
135+
136+
basic cbasic_operand1;
137+
basic_new_stack(cbasic_operand1);
138+
sympify(operand1, cbasic_operand1);
139+
140+
basic cbasic_operand2;
141+
basic_new_stack(cbasic_operand2);
142+
sympify(operand2, cbasic_operand2);
143+
144+
cresult = basic_new_heap();
145+
cwfunc_ptr(cresult, cbasic_operand1, cbasic_operand2);
146+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
147+
basic_free_stack(cbasic_operand1);
148+
basic_free_stack(cbasic_operand2);
149+
150+
return result;
151+
}

0 commit comments

Comments
 (0)