-
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
Conversation
ext/symengine/ruby_complex.c
Outdated
| imag = rb_funcall(comp_value, rb_intern("imaginary"), 0, NULL); | ||
|
|
||
| crational_init(real_basic, real); | ||
| crational_init(imag_basic, imag); |
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.
crational_init takes a VALUE and extracts the c pointer from that in this line. You are passing a basic_struct * which is being cast to VALUE/unintptr_t/void *.
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 assume you want to extract the numerator and denominator info from ruby Rational objects so that you can pass it to complex_set. You can use sympify to do that.
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 I realised that this is wrong and am looking to solve the issue with sympify. This would solve the issue with Rational as well.
|
The sympify function has been exposed, but there's a small error. I am guessing it's a result of mismanaging memory, but cannot seem to find out what's causing the error. Probably something small. I've been trying for a long time now, without success. Basically, this is what happens now: and Any insights into solving this issue? |
ext/symengine/ruby_utils.c
Outdated
|
|
||
| VALUE result; | ||
| basic cbasic_operand; | ||
| basic_new_stack(cbasic_operand); |
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.
basic_new_stack -> basic_new_heap
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.
it did not work.. actually now the error comes earlier
2.1.8 :002 > a = Rational('1/2')
=> (1/2)
2.1.8 :003 > a1 = SymEngine::sympify(a)
terminate called after throwing an instance of 'std::logic_error'
what(): /home/rajith/Development/symengine/symengine/utilities/teuchos/Teuchos_RCPNode.hpp:95:
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.
@isuruf thanks. It's working now.
ext/symengine/symengine_utils.c
Outdated
| sympify(real, real_basic); | ||
| sympify(imag, imag_basic); | ||
|
|
||
| complex_set(cbasic_operand2, real_basic, imag_basic); |
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.
complex_set requires integers or rationals, but Ruby complex can have any real number as real and imaginary parts.
Best way is to do cbasic_operand2 = real_basic + basic_const_I * imag_basic
|
Shall we do the Complex Doubles and Complex MPCs in another PR? I am looking into complex doubles now, and it will need wrapping real values into Ruby as well. So I will add more tests for the If there is anything else that needs to be done here, let me know. |
ext/symengine/symengine_utils.c
Outdated
|
|
||
|
|
||
| basic_struct *const_I = basic_new_heap(); | ||
| basic_struct *imag_part = basic_new_heap(); |
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 use cbasic_operand2 instead of the new variables.
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.
+1
|
We will also need a |
spec/complex_spec.rb
Outdated
| it 'returns an instance of SymEngine::Integer class' do | ||
| a = Complex(2, 7) | ||
| a = SymEngine::S(a) | ||
| a = a.real |
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.
It's bad practice to re-assign variables in side a single unit test. r = a.real would be better.
|
One thing that I notice is that you're following 4 space tabs everywhere. Is that a convention for symengine in general? In Ruby we generally use a 2 space tab. |
|
I have been following the same convention as per the existing repository. But generally in the repo the Ruby files have a 2 space tab. And C files have a 4 space tab. Maybe @abinashmeher999 and @isuruf can give more detailed answers on these. |
…o doubles Conflicts: spec/constant_spec.rb spec/rational_spec.rb
Conflicts: symengine_version.txt
spec/constant_spec.rb
Outdated
| describe '#i' do | ||
| subject(:i) { SymEngine::I } | ||
|
|
||
| it { is_expected.to be_a SymEngine::Constant } |
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.
Is this passing? I is not a constant, it's a Complex
|
@abinashmeher999 @isuruf please review |
lib/symengine/complex.rb
Outdated
| module SymEngine | ||
| class Complex | ||
| def to_c | ||
| self.to_s.sub('I', 'i').sub('*','').gsub(' ','').to_c |
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.
- unnecessary self;
tris better thansubin this case:tr('I', 'i').tr('* ', '')
ext/symengine/ruby_complex.c
Outdated
|
|
||
| basic_free_stack(cbasic_operand); | ||
|
|
||
| return result; |
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.
Use function_onearg instead of the above implementation.
|
+1 to merge, @zverok, any other comments? |
lib/symengine/complex.rb
Outdated
| module SymEngine | ||
| class Complex | ||
| def to_c | ||
| to_s.tr('I', 'i').tr('*','').tr(' ','').to_c |
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.
This is pretty minor, but you can use one tr('* ', '') instead of two, when replacing chars with nothing.
Oh! Last moment thought: just String#delete would be even better :)
|
Only one last (and pretty minor). Otherwise everything looks good to me 👍 |
|
Thanks for the insight @zverok |
@rajithv Please open up a WIP PR from now on whenever you start working on something. That way if you run into a problem, all the conversations can happen there.