Skip to content
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

Add ability to assign ruby complex types to GSL::Matrix::Complex #38

Merged
merged 2 commits into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ext/gsl_native/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ gsl_complex rb_gsl_obj_to_gsl_complex(VALUE obj, gsl_complex *z)
if (!NIL_P(vre)) GSL_SET_REAL(z, NUM2DBL(vre));
if (!NIL_P(vim)) GSL_SET_IMAG(z, NUM2DBL(vim));
break;
case T_COMPLEX:
vre = rb_funcall(obj, rb_intern("real"), 0);
vim = rb_funcall(obj, rb_intern("imag"), 0);
*z = gsl_complex_rect(NUM2DBL(vre), NUM2DBL(vim));
break;
case T_FLOAT:
case T_FIXNUM:
case T_BIGNUM:
Expand All @@ -63,14 +68,19 @@ gsl_complex rb_gsl_obj_to_gsl_complex(VALUE obj, gsl_complex *z)
static VALUE rb_gsl_complex_new(int argc, VALUE *argv, VALUE klass)
{
gsl_complex *c = NULL;
VALUE obj;
VALUE obj, vre, vim;
obj = Data_Make_Struct(klass, gsl_complex, 0, free, c);
switch (argc) {
case 1:
switch (TYPE(argv[0])) {
case T_ARRAY:
*c = ary2complex(argv[0]);
break;
case T_COMPLEX:
vre = rb_funcall(argv[0], rb_intern("real"), 0);
vim = rb_funcall(argv[0], rb_intern("imag"), 0);
*c = gsl_complex_rect(NUM2DBL(vre), NUM2DBL(vim));
break;
case T_FLOAT:
case T_FIXNUM:
case T_BIGNUM:
Expand Down
13 changes: 12 additions & 1 deletion test/gsl/complex_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,16 @@ def test_complex
'gsl_complex_polar imag part at (r=%g,t=%g)' % [r, t]
}
end


# Test if it is possible to create a GSL::Complex from ::Complex
def test_rb_complex_creation
rb_comp = Complex(rand, rand)

z = GSL::Complex.alloc(rb_comp)

assert_rel z.real, rb_comp.real, GSL::DBL_EPSILON,
"gsl_complex real part. Re(#{rb_comp}) = #{z.real}"
assert_rel z.imag, rb_comp.imag, GSL::DBL_EPSILON,
"gsl_complex imag part. Im(#{rb_comp}) = #{z.imag}"
end
end