Skip to content

Commit f3d3821

Browse files
committed
Merge pull request #46 from rajithv/double2
Real Double Class
2 parents 6d9b596 + 681ee2e commit f3d3821

15 files changed

+209
-17
lines changed

ext/symengine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ set(RUBY_WRAPPER_SRC
22
ruby_basic.c
33
ruby_symbol.c
44
ruby_integer.c
5+
ruby_real_double.c
56
ruby_complex.c
7+
ruby_complex_double.c
68
ruby_constant.c
79
ruby_function.c
810
ruby_ntheory.c
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "ruby_complex_double.h"
2+
3+
VALUE ccomplex_double_real_part(VALUE self) {
4+
return function_onearg(complex_double_real_part, self);
5+
}
6+
7+
VALUE ccomplex_double_imaginary_part(VALUE self) {
8+
return function_onearg(complex_double_imaginary_part, self);
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef RUBY_COMPLEX_DOUBLE_H_
2+
#define RUBY_COMPLEX_DOUBLE_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_double_real_part(VALUE self);
11+
VALUE ccomplex_double_imaginary_part(VALUE self);
12+
13+
#endif //RUBY_COMPLEX_DOUBLE_H_

ext/symengine/ruby_integer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
VALUE cinteger_init(VALUE self, VALUE num_value) {
44
basic_struct *this;
55
Data_Get_Struct(self, basic_struct, this);
6-
GET_SYMINTFROMVAL(num_value, this);
6+
get_symintfromval(num_value, this);
77
return self;
88
}
99

ext/symengine/ruby_real_double.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "ruby_real_double.h"
2+
3+
4+
VALUE crealdouble_to_float(VALUE self)
5+
{
6+
VALUE result;
7+
8+
basic cbasic_operand1;
9+
basic_new_stack(cbasic_operand1);
10+
sympify(self, cbasic_operand1);
11+
12+
result = rb_float_new(real_double_get_d(cbasic_operand1));
13+
basic_free_stack(cbasic_operand1);
14+
15+
return result;
16+
}

ext/symengine/ruby_real_double.h

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

ext/symengine/symengine.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#include "ruby_basic.h"
33
#include "ruby_symbol.h"
44
#include "ruby_integer.h"
5+
#include "ruby_real_double.h"
56
#include "ruby_constant.h"
67
#include "ruby_complex.h"
8+
#include "ruby_complex_double.h"
79
#include "ruby_function.h"
810
#include "ruby_ntheory.h"
911
#include "ruby_utils.h"
@@ -62,6 +64,11 @@ void Init_symengine() {
6264
rb_define_method(c_integer, "initialize", cinteger_init, 1);
6365
rb_define_method(c_integer, "%", cntheory_mod, 1);
6466

67+
//RealDouble Class
68+
c_real_double = rb_define_class_under(m_symengine, "RealDouble", c_basic);
69+
rb_define_alloc_func(c_real_double, cbasic_alloc);
70+
rb_define_method(c_real_double, "to_f", crealdouble_to_float, 0);
71+
6572
//Rational class
6673
c_rational = rb_define_class_under(m_symengine, "Rational", c_basic);
6774
rb_define_alloc_func(c_rational, cbasic_alloc);
@@ -75,6 +82,8 @@ void Init_symengine() {
7582
//ComplexDouble class
7683
c_complex_double = rb_define_class_under(m_symengine, "ComplexDouble", c_basic);
7784
rb_define_alloc_func(c_complex_double, cbasic_alloc);
85+
rb_define_method(c_complex_double, "real", ccomplex_double_real_part, 0);
86+
rb_define_method(c_complex_double, "imaginary", ccomplex_double_imaginary_part, 0);
7887

7988
//Constant class
8089
c_constant = rb_define_class_under(m_symengine, "Constant", c_basic);

ext/symengine/symengine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ VALUE m_symengine;
1010
VALUE c_basic;
1111
VALUE c_symbol;
1212
VALUE c_integer;
13+
VALUE c_real_double;
1314
VALUE c_rational;
1415
VALUE c_complex;
1516
VALUE c_complex_double;

ext/symengine/symengine_utils.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
66
basic_struct *temp;
77
VALUE new_operand2, num, den;
88
VALUE real, imag;
9+
double f;
910

1011
switch(TYPE(operand2)) {
1112
case T_FIXNUM:
1213
case T_BIGNUM:
13-
GET_SYMINTFROMVAL(operand2, cbasic_operand2);
14+
get_symintfromval(operand2, cbasic_operand2);
15+
break;
16+
17+
case T_FLOAT:
18+
f = RFLOAT_VALUE(operand2);
19+
real_double_set_d(cbasic_operand2, f);
1420
break;
1521

1622
case T_RATIONAL:
@@ -21,8 +27,8 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
2127
basic_new_stack(num_basic);
2228
basic_new_stack(den_basic);
2329

24-
GET_SYMINTFROMVAL(num, num_basic);
25-
GET_SYMINTFROMVAL(den, den_basic);
30+
get_symintfromval(num, num_basic);
31+
get_symintfromval(den, den_basic);
2632

2733
rational_set(cbasic_operand2, num_basic, den_basic);
2834

@@ -60,12 +66,27 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
6066
}
6167
}
6268

69+
void get_symintfromval(VALUE operand2, basic_struct *cbasic_operand2)
70+
{
71+
if ( TYPE(operand2) == T_FIXNUM ){
72+
int i = NUM2INT( operand2 );
73+
integer_set_si(cbasic_operand2, i);
74+
} else if ( TYPE(operand2) == T_BIGNUM ){
75+
VALUE Rb_Temp_String = rb_funcall(operand2, rb_intern("to_s"), 0, NULL);
76+
integer_set_str(cbasic_operand2, StringValueCStr(Rb_Temp_String));
77+
} else {
78+
rb_raise(rb_eTypeError, "Invalid Type: Fixnum or Bignum required");
79+
}
80+
}
81+
6382
VALUE Klass_of_Basic(const basic_struct *basic_ptr) {
6483
switch(basic_get_type(basic_ptr)) {
6584
case SYMENGINE_SYMBOL:
6685
return c_symbol;
6786
case SYMENGINE_INTEGER:
6887
return c_integer;
88+
case SYMENGINE_REAL_DOUBLE:
89+
return c_real_double;
6990
case SYMENGINE_RATIONAL:
7091
return c_rational;
7192
case SYMENGINE_COMPLEX:

ext/symengine/symengine_utils.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
#ifndef SYMENGINE_MACROS_H_
2-
#define SYMENGINE_MACROS_H_
1+
#ifndef SYMENGINE_UTILS_H_
2+
#define SYMENGINE_UTILS_H_
33

44
#include "ruby.h"
55
#include "ruby_basic.h"
66
#include "symengine/cwrapper.h"
77

88
//Returns the pointer wrapped inside the Ruby VALUE
99
void sympify(VALUE operand2, basic_struct *cbasic_operand2);
10+
//Returns the pointer wrapped inside the Ruby Fixnum or Bignum
11+
void get_symintfromval(VALUE operand2, basic_struct *cbasic_operand2);
1012
//Returns the Ruby class of the corresponding basic_struct pointer
1113
VALUE Klass_of_Basic(const basic_struct *basic_ptr);
1214
//Returns the result from the function pointed by cwfunc_ptr: for one argument functions
@@ -15,13 +17,4 @@ VALUE function_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VA
1517
VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*),
1618
VALUE operand1, VALUE operand2);
1719

18-
//Obtains the value from Ruby Fixnum or Bignum to an already allocated basic_struct
19-
#define GET_SYMINTFROMVAL(num_value, this) { \
20-
if ( ! RB_TYPE_P(num_value, T_FIXNUM) && ! RB_TYPE_P(num_value, T_BIGNUM) ) { \
21-
rb_raise(rb_eTypeError, "Invalid Type: Fixnum or Bignum required"); \
22-
} \
23-
VALUE Rb_Temp_String = rb_funcall(num_value, rb_intern("to_s"), 0, NULL); \
24-
integer_set_str(this, StringValueCStr(Rb_Temp_String)); \
25-
}
26-
27-
#endif //SYMENGINE_MACROS_H_
20+
#endif //SYMENGINE_UTILS_H_

0 commit comments

Comments
 (0)