Skip to content

Commit d0da640

Browse files
committed
Real part and Imaginary part wrappers
1 parent 22cff51 commit d0da640

File tree

7 files changed

+95
-4
lines changed

7 files changed

+95
-4
lines changed

ext/symengine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(RUBY_WRAPPER_SRC
22
ruby_basic.c
33
ruby_symbol.c
44
ruby_integer.c
5+
ruby_complex.c
56
ruby_constant.c
67
ruby_function.c
78
ruby_ntheory.c

ext/symengine/ruby_complex.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "ruby_complex.h"
2+
3+
VALUE ccomplex_real_part(VALUE self) {
4+
basic_struct *c_result = basic_new_heap();
5+
6+
basic cbasic_operand;
7+
basic_new_stack(cbasic_operand);
8+
9+
VALUE result;
10+
11+
sympify(self, cbasic_operand);
12+
complex_real_part(c_result, cbasic_operand);
13+
14+
result = Data_Wrap_Struct(Klass_of_Basic(c_result), NULL, cbasic_free_heap, c_result);
15+
16+
basic_free_stack(cbasic_operand);
17+
18+
return result;
19+
}
20+
21+
VALUE ccomplex_imaginary_part(VALUE self) {
22+
basic_struct *c_result = basic_new_heap();
23+
24+
basic cbasic_operand;
25+
basic_new_stack(cbasic_operand);
26+
27+
VALUE result;
28+
29+
sympify(self, cbasic_operand);
30+
complex_imaginary_part(c_result, cbasic_operand);
31+
32+
result = Data_Wrap_Struct(Klass_of_Basic(c_result), NULL, cbasic_free_heap, c_result);
33+
34+
basic_free_stack(cbasic_operand);
35+
36+
return result;
37+
}

ext/symengine/ruby_complex.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef RUBY_COMPLEX_H_
2+
#define RUBY_COMPLEX_H_
3+
4+
#include <ruby.h>
5+
#include <symengine/cwrapper.h>
6+
7+
#include "symengine.h"
8+
#include "symengine_utils.h"
9+
10+
VALUE ccomplex_real_part(VALUE self);
11+
VALUE ccomplex_imaginary_part(VALUE self);
12+
13+
#endif //RUBY_COMPLEX_H_

ext/symengine/symengine.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ruby_symbol.h"
44
#include "ruby_integer.h"
55
#include "ruby_constant.h"
6+
#include "ruby_complex.h"
67
#include "ruby_function.h"
78
#include "ruby_ntheory.h"
89
#include "ruby_utils.h"
@@ -67,6 +68,8 @@ void Init_symengine() {
6768
//Complex class
6869
c_complex = rb_define_class_under(m_symengine, "Complex", c_basic);
6970
rb_define_alloc_func(c_complex, cbasic_alloc);
71+
rb_define_method(c_complex, "real", ccomplex_real_part, 0);
72+
rb_define_method(c_complex, "imaginary", ccomplex_imaginary_part, 0);
7073

7174
//ComplexDouble class
7275
c_complex_double = rb_define_class_under(m_symengine, "ComplexDouble", c_basic);

lib/symengine/complex.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ def to_c
44
self.to_s.sub('I', 'i').sub('*','').gsub(' ','').to_c
55
end
66
end
7-
end
7+
end

spec/complex_spec.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,50 @@
1313
end
1414
context 'with a Ruby Integer as input' do
1515
it 'returns an instance of SymEngine::Integer class' do
16-
a = Complex(2, 0);
16+
a = Complex(2, 0)
1717
a = SymEngine::sympify(a)
1818
expect(a).to be_an_instance_of SymEngine::Integer
1919
expect(a.to_s).to eq('2')
2020
end
2121
end
22+
context 'Obtaining the integer real part' do
23+
it 'returns an instance of SymEngine::Integer class' do
24+
a = Complex(2, 7)
25+
a = SymEngine::S(a)
26+
a = a.real
27+
expect(a).to be_an_instance_of SymEngine::Integer
28+
expect(a.to_s).to eq('2');
29+
end
30+
end
31+
context 'Obtaining the integer imaginary part' do
32+
it 'returns an instance of SymEngine::Integer class' do
33+
a = Complex(2, 7)
34+
a = SymEngine::S(a)
35+
a = a.imaginary
36+
expect(a).to be_an_instance_of SymEngine::Integer
37+
expect(a.to_s).to eq('7');
38+
end
39+
end
40+
context 'Obtaining the rational real part ' do
41+
it 'returns an instance of SymEngine::Rational class' do
42+
a = Rational('5/4')
43+
a = Complex(a, 7)
44+
a = SymEngine::S(a)
45+
a = a.real
46+
expect(a).to be_an_instance_of SymEngine::Rational
47+
expect(a.to_s).to eq('5/4');
48+
end
49+
end
50+
context 'Obtaining the rational imaginary part' do
51+
it 'returns an instance of SymEngine::Integer class' do
52+
a = Rational('5/4')
53+
a = Complex(2, a)
54+
a = SymEngine::S(a)
55+
a = a.imaginary
56+
expect(a).to be_an_instance_of SymEngine::Rational
57+
expect(a.to_s).to eq('5/4');
58+
end
59+
end
2260
end
2361
end
2462
end

symengine_version.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
5e88e62b42c38200573eecf688d90b46beb38a77
2-
1+
382a917700e6c6116e025e64baff66d0668c64f2

0 commit comments

Comments
 (0)