-
Notifications
You must be signed in to change notification settings - Fork 15
Ruby Wrappers for NTheory #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d56b892
5ad2393
6cd53d1
d57e5f5
94614ce
719470c
515b715
6b29a73
f29f4c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ VALUE cinteger_init(VALUE self, VALUE num_value) { | |
| GET_SYMINTFROMVAL(num_value, this); | ||
| return self; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #include "ruby_ntheory.h" | ||
|
|
||
| VALUE cntheory_nextprime(VALUE self, VALUE operand1) { | ||
| return function_onearg(ntheory_nextprime, operand1); | ||
| } | ||
|
|
||
| VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2) { | ||
| return function_twoarg(ntheory_gcd, operand1, operand2); | ||
| } | ||
|
|
||
| VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2) { | ||
| return function_twoarg(ntheory_lcm, 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); | ||
| } | ||
|
|
||
| VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2) { | ||
| return function_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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now, it gives an error Can I use |
||
|
|
||
| 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #ifndef RUBY_NTHEORY_H_ | ||
| #define RUBY_NTHEORY_H_ | ||
|
|
||
| #include <ruby.h> | ||
| #include <symengine/cwrapper.h> | ||
|
|
||
| #include "symengine.h" | ||
| #include "ruby_basic.h" | ||
| #include "symengine_utils.h" | ||
|
|
||
| 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 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_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module SymEngine | ||
| class Integer | ||
| def to_int | ||
| self.to_s.to_i | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abinashmeher999 am I using this correctly? Pls check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is correct. If you get unsure about return types, refer here: http://rxr.whitequark.org/mri/ident
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then all you need to do is to add an
implicit conversion of SymEngine::Integer into IntegerUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured our half way on how to do this:
so I added a new method to the 'Integer' class
rb_define_method(c_integer, "to_int", cinteger_to_int, 0);From which I should return a VALUE pointer pointing to a Ruby
Bignumor aFixnum. This can be done by converting a C int by usingINT2NUMmacro.The step I'm stuck at is on extracting a C int from the SymEngine Integer object. @abinashmeher999 can you point me to somewhere I can read more about the Ruby Wrapper's SymEngine Integer's structure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajithv, the Integer object uses GMP underneath. So you can extract a gmp int easily using the
as_mpz()method. If you want an integer, you can callas_int(), but it will raise an exception if the integer doesn't fit intosigned long int.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can convert a SymEngine::Integer to a Ruby string first and then call
to_imethodThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. This can be done. So instead of using the Ruby C API for the above, you can define
#to_intin ruby itself here. Make a new fileinteger.rband then proceed.