Skip to content

Commit f29f4c4

Browse files
committed
SymEngine::Integer to int implicit conversion
1 parent 6b29a73 commit f29f4c4

File tree

7 files changed

+44
-9
lines changed

7 files changed

+44
-9
lines changed

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2) {
1212
return function_twoarg(ntheory_lcm, operand1, operand2);
1313
}
1414

15-
VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2) {
16-
VALUE ans = function_twoarg(ntheory_mod, operand1, operand2);
15+
VALUE cntheory_mod(VALUE self, VALUE operand2) {
16+
VALUE ans = function_twoarg(ntheory_mod, self, operand2);
1717
VALUE ans1 = cbasic_add(ans, operand2);
1818
return function_twoarg(ntheory_mod, ans1, operand2);
1919
}

ext/symengine/ruby_ntheory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
VALUE cntheory_gcd(VALUE self, VALUE operand1, VALUE operand2);
1212
VALUE cntheory_lcm(VALUE self, VALUE operand1, VALUE operand2);
1313
VALUE cntheory_nextprime(VALUE self, VALUE operand1);
14-
VALUE cntheory_mod(VALUE self, VALUE operand1, VALUE operand2);
14+
VALUE cntheory_mod(VALUE self, VALUE operand2);
1515
VALUE cntheory_quotient(VALUE self, VALUE operand1, VALUE operand2);
1616
VALUE cntheory_fibonacci(VALUE self, VALUE operand1);
1717
VALUE cntheory_lucas(VALUE self, VALUE operand1);

ext/symengine/symengine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void Init_symengine() {
5353
c_integer = rb_define_class_under(m_symengine, "Integer", c_basic);
5454
rb_define_alloc_func(c_integer, cbasic_alloc);
5555
rb_define_method(c_integer, "initialize", cinteger_init, 1);
56+
rb_define_method(c_integer, "%", cntheory_mod, 1);
5657

5758
//Rational class
5859
c_rational = rb_define_class_under(m_symengine, "Rational", c_basic);
@@ -150,7 +151,6 @@ void Init_symengine() {
150151
rb_define_module_function(m_symengine, "gcd", cntheory_gcd, 2);
151152
rb_define_module_function(m_symengine, "lcm", cntheory_lcm, 2);
152153
rb_define_module_function(m_symengine, "nextprime", cntheory_nextprime, 1);
153-
rb_define_module_function(m_symengine, "%", cntheory_mod, 2);
154154
rb_define_module_function(m_symengine, "quotient", cntheory_quotient, 2);
155155
rb_define_module_function(m_symengine, "fibonacci", cntheory_fibonacci, 1);
156156
rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1);

lib/symengine.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ def symbols ary_or_string, *params
3434
require 'symengine/symengine'
3535
require 'symengine/iruby'
3636
require 'symengine/basic'
37+
require 'symengine/integer'

lib/symengine/integer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module SymEngine
2+
class Integer
3+
def to_int
4+
self.to_s.to_i
5+
end
6+
end
7+
end

spec/ntheory_spec.rb

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
require 'spec_helper'
22

33
describe SymEngine do
4+
45
before :each do
56
end
67

78
describe 'NTheory' do
9+
before :each do
10+
@i1 = SymEngine::Integer.new(1)
11+
@i4 = SymEngine::Integer.new(4)
12+
@i5 = SymEngine::Integer.new(5)
13+
@im4 = SymEngine::Integer.new(-4)
14+
@im5 = SymEngine::Integer.new(-5)
15+
end
816

917
describe '#gcd' do
1018
context 'GCD of 2 and 4' do
@@ -36,25 +44,25 @@
3644
describe '#mod' do
3745
context '5 mod 4' do
3846
it 'returns 1' do
39-
f = 5 % 4
47+
f = @i5 % @i4
4048
expect(f).to eql(1)
4149
end
4250
end
4351
context '-5 mod -4' do
4452
it 'returns -1' do
45-
f = -5 % -4
53+
f = @im5 % @im4
4654
expect(f).to eql(-1)
4755
end
4856
end
4957
context '5 mod -4' do
5058
it 'returns -3' do
51-
f = 5 % -4
59+
f = @i5 % @im4
5260
expect(f).to eql(-3)
5361
end
5462
end
5563
context '-5 mod 4' do
5664
it 'returns 3' do
57-
f = -5 % 4
65+
f = @im5 % @i4
5866
expect(f).to eql(3)
5967
end
6068
end
@@ -76,12 +84,24 @@
7684
expect(f).to eql(5)
7785
end
7886
end
87+
context '5th Fibonacci Number' do
88+
it 'returns 5' do
89+
f = SymEngine::fibonacci(@i5)
90+
expect(f).to eql(5)
91+
end
92+
end
7993
end
8094

8195
describe '#lucas' do
8296
context '1st Lucas Number' do
8397
it 'returns 1' do
84-
f = SymEngine::fibonacci(1)
98+
f = SymEngine::lucas(1)
99+
expect(f).to eql(1)
100+
end
101+
end
102+
context '1st Lucas Number' do
103+
it 'returns 1' do
104+
f = SymEngine::lucas(@i1)
85105
expect(f).to eql(1)
86106
end
87107
end
@@ -94,6 +114,12 @@
94114
expect(f).to eql(5)
95115
end
96116
end
117+
context 'binomial (n=5, k=1)' do
118+
it 'returns 5' do
119+
f = SymEngine::binomial(@i5, @i1)
120+
expect(f).to eql(5)
121+
end
122+
end
97123
end
98124

99125
end

0 commit comments

Comments
 (0)