Skip to content

Commit 5ad2393

Browse files
committed
NTheory Wrappers
1 parent d56b892 commit 5ad2393

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

ext/symengine/ruby_ntheory.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "ruby_ntheory.h"
2+
3+
VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1) {
4+
basic_struct *cresult;
5+
VALUE result;
6+
7+
basic cbasic_operand1;
8+
basic_new_stack(cbasic_operand1);
9+
sympify(operand1, cbasic_operand1);
10+
11+
cresult = basic_new_heap();
12+
cwfunc_ptr(cresult, cbasic_operand1);
13+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
14+
basic_free_stack(cbasic_operand1);
15+
16+
return result;
17+
}
18+
19+
VALUE cfunction_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2) {
20+
basic_struct *cresult;
21+
VALUE result;
22+
23+
basic cbasic_operand1;
24+
basic_new_stack(cbasic_operand1);
25+
sympify(operand1, cbasic_operand1);
26+
27+
basic cbasic_operand2;
28+
basic_new_stack(cbasic_operand2);
29+
sympify(operand2, cbasic_operand2);
30+
31+
cresult = basic_new_heap();
32+
cwfunc_ptr(cresult, cbasic_operand1, cbasic_operand2);
33+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL , cbasic_free_heap, cresult);
34+
basic_free_stack(cbasic_operand1);
35+
basic_free_stack(cbasic_operand2);
36+
37+
return result;
38+
}
39+
40+
VALUE cfunction_nextprime(VALUE self, VALUE operand1) {
41+
return cfunction_onearg(nthoery_gcd, operand1);
42+
}
43+
44+
VALUE cfunction_gcd(VALUE self, VALUE operand1, VALUE operand2) {
45+
return cfunction_twoarg(ntheory_gcd, operand1, operand2);
46+
}
47+
48+
VALUE cfunction_lcm(VALUE self, VALUE operand1, VALUE operand2) {
49+
return cfunction_twoarg(ntheory_lcm, operand1, operand2);
50+
}
51+
52+
VALUE cfunction_mod(VALUE self, VALUE operand1, VALUE operand2) {
53+
return cfunction_twoarg(ntheory_mod, operand1, operand2);
54+
}
55+
56+
VALUE cfunction_quotient(VALUE self, VALUE operand1, VALUE operand2) {
57+
return cfunction_twoarg(ntheory_quotient, operand1, operand2);
58+
}

ext/symengine/ruby_ntheory.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef RUBY_NTHEORY_H_
2+
#define RUBY_NTHEORY_H_
3+
4+
#include <ruby.h>
5+
#include <symengine/cwrapper.h>
6+
7+
#include "ruby_basic.h"
8+
#include "symengine.h"
9+
#include "symengine_macros.h"
10+
11+
VALUE cfunction_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VALUE operand1);
12+
VALUE cfunction_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*), VALUE operand1, VALUE operand2);
13+
14+
VALUE cfunction_gcd(VALUE self, VALUE operand1, VALUE operand2);
15+
VALUE cfunction_lcm(VALUE self, VALUE operand1, VALUE operand2);
16+
VALUE cfunction_nextprime(VALUE self, VALUE operand1);
17+
VALUE cfunction_mod(VALUE self, VALUE operand1, VALUE operand2);
18+
VALUE cfunction_quotient(VALUE self, VALUE operand1, VALUE operand2);
19+
//VALUE cfunction_fibonacci(VALUE self, VALUE operand1);
20+
//VALUE cfunction_lucas(VALUE self, VALUE operand1);
21+
//VALUE cfunction_binomial(VALUE self, VALUE operand1, VALUE operand2);
22+
23+
#endif //RUBY_NTHEORY_H_

0 commit comments

Comments
 (0)