-
Notifications
You must be signed in to change notification settings - Fork 15
Complex class #39
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
Complex class #39
Changes from all commits
f9b0661
b4af3b0
316a9c1
88b302f
dbd072d
d1498f4
62d67ed
505618a
dd79ff0
0f4694a
6633cb4
cb28383
fa1fbe5
7def8b6
9ddc2c6
22cff51
d0da640
038aace
a4b43d4
a219d58
7953c83
417eb97
1246cb2
81ade4c
e9162e0
7ddd68e
e96d934
52e1870
5802cf7
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #include "ruby_complex.h" | ||
|
|
||
| VALUE ccomplex_real_part(VALUE self) { | ||
| return function_onearg(complex_real_part, self); | ||
| } | ||
|
|
||
| VALUE ccomplex_imaginary_part(VALUE self) { | ||
| return function_onearg(complex_imaginary_part, self); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #ifndef RUBY_COMPLEX_H_ | ||
| #define RUBY_COMPLEX_H_ | ||
|
|
||
| #include <ruby.h> | ||
| #include <symengine/cwrapper.h> | ||
|
|
||
| #include "symengine.h" | ||
| #include "symengine_utils.h" | ||
|
|
||
| VALUE ccomplex_real_part(VALUE self); | ||
| VALUE ccomplex_imaginary_part(VALUE self); | ||
|
|
||
| #endif //RUBY_COMPLEX_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,28 @@ | ||
| #include "ruby_constant.h" | ||
|
|
||
| VALUE cconstant_const(void (*cwfunc_ptr)(basic_struct*)) { | ||
| VALUE cconstant_const(void (*cwfunc_ptr)(basic_struct*), VALUE klass) { | ||
| basic_struct *cresult; | ||
| VALUE result; | ||
|
|
||
| cresult = basic_new_heap(); | ||
| cwfunc_ptr(cresult); | ||
|
|
||
| result = Data_Wrap_Struct(c_constant, NULL, cbasic_free_heap, cresult); | ||
| result = Data_Wrap_Struct(klass, NULL, cbasic_free_heap, cresult); | ||
| return result; | ||
| } | ||
|
|
||
| VALUE cconstant_pi() { | ||
| return cconstant_const(basic_const_pi); | ||
| return cconstant_const(basic_const_pi, c_constant); | ||
| } | ||
|
|
||
| VALUE cconstant_e() { | ||
| return cconstant_const(basic_const_E); | ||
| return cconstant_const(basic_const_E, c_constant); | ||
| } | ||
|
|
||
| VALUE cconstant_euler_gamma() { | ||
| return cconstant_const(basic_const_EulerGamma); | ||
| return cconstant_const(basic_const_EulerGamma, c_constant); | ||
| } | ||
|
|
||
| VALUE cconstant_i() { | ||
| return cconstant_const(basic_const_I, c_complex); | ||
| } |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #include "ruby_utils.h" | ||
|
|
||
| VALUE cutils_sympify(VALUE self, VALUE operand) { | ||
|
|
||
| VALUE result; | ||
|
|
||
| basic_struct *cbasic_operand; | ||
| cbasic_operand = basic_new_heap(); | ||
|
|
||
| sympify(operand, cbasic_operand); | ||
| result = Data_Wrap_Struct(Klass_of_Basic(cbasic_operand), NULL , cbasic_free_heap, cbasic_operand); | ||
|
|
||
| return result; | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #ifndef RUBY_UTILS_H_ | ||
| #define RUBY_UTILS_H_ | ||
|
|
||
| #include "symengine_utils.h" | ||
|
|
||
| //Returns the Ruby Value after going through sympify | ||
| VALUE cutils_sympify(VALUE self, VALUE operand); | ||
|
|
||
| #endif //RUBY_UTILS_H_ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module SymEngine | ||
| class Complex | ||
| def to_c | ||
| to_s.tr('I', 'i').delete(' *').to_c | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| describe SymEngine::Complex do | ||
| context 'Convert to SymEngine types' do | ||
| context 'with a Complex' do | ||
| subject { SymEngine(Complex(2, 3)) } | ||
|
|
||
| it { is_expected.to be_a SymEngine::Complex } | ||
| its(:to_s) { is_expected.to eq '2 + 3*I' } | ||
| end | ||
|
|
||
| context 'with an integer' do | ||
| subject { SymEngine(Complex(2, 0)) } | ||
|
|
||
| it { is_expected.to be_a SymEngine::Integer } | ||
| its(:to_s) { is_expected.to eq '2' } | ||
| end | ||
| end | ||
|
|
||
| context 'real_part and imaginary_part' do | ||
| let(:a) { SymEngine(Complex(Rational('2/7'), Rational('3/8'))) } | ||
| let(:b) { SymEngine(Complex(2, 3)) } | ||
|
|
||
| context 'real_part' do | ||
| context 'using SymEngine Rationals' do | ||
| subject { a.real } | ||
| it { is_expected.to be_a SymEngine::Rational } | ||
| its(:to_s) { is_expected.to eq '2/7' } | ||
| end | ||
|
|
||
| context 'using SymEngine Integers' do | ||
| subject { b.real } | ||
| it { is_expected.to be_a SymEngine::Integer } | ||
| its(:to_s) { is_expected.to eq '2' } | ||
| end | ||
| end | ||
|
|
||
| context 'imaginary_part' do | ||
| context 'using SymEngine Rationals' do | ||
| subject { a.imaginary } | ||
| it { is_expected.to be_a SymEngine::Rational } | ||
| its(:to_s) { is_expected.to eq '3/8' } | ||
| end | ||
|
|
||
| context 'using SymEngine Integers' do | ||
| subject { b.imaginary } | ||
| it { is_expected.to be_a SymEngine::Integer } | ||
| its(:to_s) { is_expected.to eq '3' } | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
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. The tests for the other member functions of
Contributor
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. These are real_part() and imaginary_part() right? Or are there any other member functions which should be checked? like addition etc. |
||
|
|
||
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.
We can have this in
symengine_utils.hright? No need of a new fileThere 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 was thinking to keep the functions for C in symengine_utils.h, while having the functions exposed in Ruby in ruby_utils.h (as per the convention of other .h files)...
What do you think?
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.
Okay