Skip to content

Commit be15717

Browse files
committed
Merge pull request #14 from rajithv/master
Add Constant Wrappers
2 parents b186216 + 8d03724 commit be15717

File tree

9 files changed

+130
-2
lines changed

9 files changed

+130
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ install:
3838
- source bin/install_travis.sh
3939

4040
# Build C++ library
41-
- source bin/test_travis.sh
41+
- bin/test_travis.sh
4242

4343
# Setup travis for Ruby wrappers
4444
- cd $RUBY_SOURCE_DIR

ext/symengine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(RUBY_WRAPPER_SRC
33
ruby_symbol.c
44
ruby_integer.c
55
ruby_rational.c
6+
ruby_constant.c
67
symengine_macros.c
78
symengine.c
89
)

ext/symengine/ruby_constant.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "ruby_constant.h"
2+
3+
VALUE cconstant_const(void (*cwfunc_ptr)(basic_struct*)) {
4+
basic_struct *cresult;
5+
VALUE result;
6+
7+
cresult = basic_new_heap();
8+
cwfunc_ptr(cresult);
9+
10+
result = Data_Wrap_Struct(c_constant, NULL, cbasic_free_heap, cresult);
11+
return result;
12+
}
13+
14+
VALUE cconstant_pi() {
15+
return cconstant_const(basic_const_pi);
16+
}
17+
18+
VALUE cconstant_e() {
19+
return cconstant_const(basic_const_E);
20+
}
21+
22+
VALUE cconstant_euler_gamma() {
23+
return cconstant_const(basic_const_EulerGamma);
24+
}

ext/symengine/ruby_constant.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef RUBY_CONSTANTS_H_
2+
#define RUBY_CONSTANTS_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 cconstant_const(void (*cwfunc_ptr)(basic_struct*));
12+
13+
VALUE cconstant_pi();
14+
15+
VALUE cconstant_e();
16+
17+
VALUE cconstant_euler_gamma();
18+
19+
#endif //RUBY_CONSTANTS_H_

ext/symengine/symengine.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "ruby_symbol.h"
33
#include "ruby_integer.h"
44
#include "ruby_rational.h"
5+
#include "ruby_constant.h"
56
#include "symengine.h"
67

78
///////////////////
@@ -46,4 +47,12 @@ void Init_symengine() {
4647
c_rational = rb_define_class_under(m_symengine, "Rational", c_basic);
4748
rb_define_alloc_func(c_rational, cbasic_alloc);
4849
rb_define_method(c_rational, "initialize", crational_init, 1);
50+
51+
//Constant class
52+
c_constant = rb_define_class_under(m_symengine, "Constant", c_basic);
53+
54+
//Constants as Module Level Constants
55+
rb_define_const(m_symengine, "PI", cconstant_pi());
56+
rb_define_const(m_symengine, "E", cconstant_e());
57+
rb_define_const(m_symengine, "EULER_GAMMA", cconstant_euler_gamma());
4958
}

ext/symengine/symengine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ VALUE c_basic;
1111
VALUE c_symbol;
1212
VALUE c_integer;
1313
VALUE c_rational;
14+
VALUE c_constant;
1415

1516
#endif //SYMENGINE_H_

ext/symengine/symengine_macros.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ VALUE Klass_of_Basic(const basic_struct *basic_ptr) {
4343
return c_integer;
4444
case SYMENGINE_RATIONAL:
4545
return c_rational;
46+
case SYMENGINE_CONSTANT:
47+
return c_constant;
4648
default:
4749
return c_basic;
4850
}

spec/constant_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require 'spec_helper'
2+
3+
describe SymEngine do
4+
before :each do
5+
end
6+
7+
describe 'Constant' do
8+
before :each do
9+
@a = SymEngine::PI
10+
@b = SymEngine::E
11+
@c = SymEngine::EULER_GAMMA
12+
@d = SymEngine::Integer.new(1);
13+
@e = SymEngine::Integer.new(0);
14+
@x = SymEngine::Symbol.new("x");
15+
end
16+
17+
describe '#pi' do
18+
context 'with summation with one' do
19+
it 'returns an initialised Basic object that is equalent to 1 + pi' do
20+
f = @a + @d
21+
expect(@a).to be_an_instance_of SymEngine::Constant
22+
expect(f).to be_an_instance_of SymEngine::Basic
23+
expect(f.to_s).to eql('1 + pi')
24+
end
25+
end
26+
27+
context 'with plus one and minus one' do
28+
it 'returns a Constant' do
29+
expect(1 + @a - 1).to be_an_instance_of SymEngine::Constant
30+
end
31+
end
32+
33+
end
34+
35+
describe '#EulerGamma' do
36+
context 'with powered to zero' do
37+
it 'returns an initialised Basic object that is equalent 1' do
38+
f = @c ** @e
39+
expect(@c).to be_an_instance_of SymEngine::Constant
40+
expect(f).to be_an_instance_of SymEngine::Integer
41+
expect(f.to_s).to eql('1')
42+
end
43+
end
44+
45+
context 'with plus one and minus one' do
46+
it 'returns a Constant' do
47+
expect(1 + @c - 1).to be_an_instance_of SymEngine::Constant
48+
end
49+
end
50+
51+
end
52+
53+
describe '#E' do
54+
context 'with summation with x' do
55+
it 'returns an initialised Basic object that is equalent to x + E' do
56+
f = @b + @x
57+
expect(@b).to be_an_instance_of SymEngine::Constant
58+
expect(f).to be_an_instance_of SymEngine::Basic
59+
expect(f.to_s).to eql('x + E')
60+
end
61+
end
62+
63+
context 'with plus one and minus one' do
64+
it 'returns a Constant' do
65+
expect(1 + @b - 1).to be_an_instance_of SymEngine::Constant
66+
end
67+
end
68+
69+
end
70+
71+
end
72+
end

symengine_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3f0c0947ae09bac9545334a6903d9d72c7043a52
1+
f6f1c89bde0d12ba718a9aeddcdf630f36893756

0 commit comments

Comments
 (0)