Skip to content

Commit c9dce78

Browse files
authored
Merge pull request #55 from rajithv/evals
evalf ruby wrapper
2 parents 511dd7c + 9e2be3f commit c9dce78

File tree

14 files changed

+177
-11
lines changed

14 files changed

+177
-11
lines changed

ext/symengine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
set(RUBY_WRAPPER_SRC
22
ruby_basic.c
33
ruby_symbol.c
4+
ruby_number.c
45
ruby_integer.c
56
ruby_real_double.c
67
ruby_real_mpfr.c
78
ruby_complex.c
89
ruby_complex_double.c
10+
ruby_complex_mpc.c
911
ruby_constant.c
1012
ruby_function.c
1113
ruby_ntheory.c

ext/symengine/ruby_complex_mpc.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "ruby_complex_mpc.h"
2+
3+
#ifdef HAVE_SYMENGINE_MPC
4+
VALUE ccomplex_mpc_real_part(VALUE self)
5+
{
6+
return function_onearg(complex_mpc_real_part, self);
7+
}
8+
9+
VALUE ccomplex_mpc_imaginary_part(VALUE self)
10+
{
11+
return function_onearg(complex_mpc_imaginary_part, self);
12+
}
13+
#endif // HAVE_SYMENGINE_MPC

ext/symengine/ruby_complex_mpc.h

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

ext/symengine/ruby_number.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "ruby_number.h"
2+
3+
int cnumber_comp(VALUE self, VALUE operand2)
4+
{
5+
basic cbasic_operand1;
6+
basic cbasic_operand2;
7+
basic_new_stack(cbasic_operand1);
8+
basic_new_stack(cbasic_operand2);
9+
10+
basic cbasic_sub;
11+
basic_new_stack(cbasic_sub);
12+
13+
sympify(self, cbasic_operand1);
14+
sympify(operand2, cbasic_operand2);
15+
16+
if (is_a_Number(cbasic_operand2) == 0) {
17+
rb_raise(rb_eTypeError, "Expected SymEngine::Number found %s ",
18+
rb_obj_classname(operand2));
19+
}
20+
21+
basic_sub(cbasic_sub, cbasic_operand1, cbasic_operand2);
22+
23+
int sign = basic_number_sign(cbasic_sub);
24+
25+
basic_free_stack(cbasic_operand1);
26+
basic_free_stack(cbasic_operand2);
27+
basic_free_stack(cbasic_sub);
28+
29+
return sign;
30+
}
31+
32+
VALUE cnumber_lt(VALUE self, VALUE operand2)
33+
{
34+
return cnumber_comp(self, operand2) == -1 ? Qtrue : Qfalse;
35+
}
36+
37+
VALUE cnumber_gt(VALUE self, VALUE operand2)
38+
{
39+
return cnumber_comp(self, operand2) == 1 ? Qtrue : Qfalse;
40+
}
41+
42+
VALUE cnumber_lt_e(VALUE self, VALUE operand2)
43+
{
44+
return cnumber_comp(self, operand2) == 1 ? Qfalse : Qtrue;
45+
}
46+
47+
VALUE cnumber_gt_e(VALUE self, VALUE operand2)
48+
{
49+
return cnumber_comp(self, operand2) == -1 ? Qfalse : Qtrue;
50+
}

ext/symengine/ruby_number.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef RUBY_NUMBER_H_
2+
#define RUBY_NUMBER_H_
3+
4+
#include <ruby.h>
5+
#include <symengine/cwrapper.h>
6+
7+
#include "symengine_utils.h"
8+
9+
int cnumber_comp(VALUE self, VALUE operand2);
10+
11+
VALUE cnumber_lt(VALUE self, VALUE operand2);
12+
13+
VALUE cnumber_gt(VALUE self, VALUE operand2);
14+
15+
VALUE cnumber_lt_e(VALUE self, VALUE operand2);
16+
17+
VALUE cnumber_gt_e(VALUE self, VALUE operand2);
18+
19+
#endif // RUBY_NUMBER_H_

ext/symengine/ruby_utils.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,18 @@ VALUE cutils_sympify(VALUE self, VALUE operand)
1414

1515
return result;
1616
}
17+
18+
VALUE cutils_evalf(VALUE self, VALUE operand, VALUE prec, VALUE real)
19+
{
20+
VALUE result;
21+
22+
basic_struct *cresult;
23+
cresult = basic_new_heap();
24+
25+
sympify(operand, cresult);
26+
basic_evalf(cresult, cresult, NUM2INT(prec), (real == Qtrue ? 1 : 0));
27+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, cbasic_free_heap,
28+
cresult);
29+
30+
return result;
31+
}

ext/symengine/ruby_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
// Returns the Ruby Value after going through sympify
77
VALUE cutils_sympify(VALUE self, VALUE operand);
8-
8+
VALUE cutils_evalf(VALUE self, VALUE operand, VALUE prec, VALUE real);
99
#endif // RUBY_UTILS_H_

ext/symengine/symengine.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#include "ruby_basic.h"
33
#include "ruby_symbol.h"
44
#include "ruby_integer.h"
5+
#include "ruby_number.h"
56
#include "ruby_real_double.h"
67
#include "ruby_constant.h"
78
#include "ruby_complex.h"
89
#include "ruby_complex_double.h"
10+
#include "ruby_complex_mpc.h"
911
#include "ruby_real_mpfr.h"
1012
#include "ruby_function.h"
1113
#include "ruby_ntheory.h"
@@ -58,52 +60,66 @@ void Init_symengine()
5860
rb_define_module_function(m_symengine, "convert", cutils_sympify, 1);
5961
rb_define_global_function("SymEngine", cutils_sympify, 1);
6062

63+
// evalf as a Module Level Function
64+
rb_define_module_function(m_symengine, "_evalf", cutils_evalf, 3);
65+
6166
// Symbol class
6267
c_symbol = rb_define_class_under(m_symengine, "Symbol", c_basic);
6368
rb_define_alloc_func(c_symbol, cbasic_alloc);
6469
rb_define_method(c_symbol, "initialize", csymbol_init, 1);
6570

71+
// Number class
72+
c_number = rb_define_class_under(m_symengine, "Number", c_basic);
73+
rb_define_alloc_func(c_number, cbasic_alloc);
74+
rb_define_method(c_number, ">", cnumber_gt, 1);
75+
rb_define_method(c_number, "<", cnumber_lt, 1);
76+
rb_define_method(c_number, ">=", cnumber_gt_e, 1);
77+
rb_define_method(c_number, "<=", cnumber_lt_e, 1);
78+
6679
// Integer class
67-
c_integer = rb_define_class_under(m_symengine, "Integer", c_basic);
80+
c_integer = rb_define_class_under(m_symengine, "Integer", c_number);
6881
rb_define_alloc_func(c_integer, cbasic_alloc);
6982
rb_define_method(c_integer, "initialize", cinteger_init, 1);
7083
rb_define_method(c_integer, "%", cntheory_mod, 1);
7184

7285
// RealDouble Class
73-
c_real_double = rb_define_class_under(m_symengine, "RealDouble", c_basic);
86+
c_real_double = rb_define_class_under(m_symengine, "RealDouble", c_number);
7487
rb_define_alloc_func(c_real_double, cbasic_alloc);
7588
rb_define_method(c_real_double, "to_f", crealdouble_to_float, 0);
7689

7790
// Rational class
78-
c_rational = rb_define_class_under(m_symengine, "Rational", c_basic);
91+
c_rational = rb_define_class_under(m_symengine, "Rational", c_number);
7992
rb_define_alloc_func(c_rational, cbasic_alloc);
8093

8194
// Complex class
82-
c_complex = rb_define_class_under(m_symengine, "Complex", c_basic);
95+
c_complex = rb_define_class_under(m_symengine, "Complex", c_number);
8396
rb_define_alloc_func(c_complex, cbasic_alloc);
8497
rb_define_method(c_complex, "real", ccomplex_real_part, 0);
8598
rb_define_method(c_complex, "imaginary", ccomplex_imaginary_part, 0);
8699

87100
// ComplexDouble class
88101
c_complex_double
89-
= rb_define_class_under(m_symengine, "ComplexDouble", c_basic);
102+
= rb_define_class_under(m_symengine, "ComplexDouble", c_number);
90103
rb_define_alloc_func(c_complex_double, cbasic_alloc);
91104
rb_define_method(c_complex_double, "real", ccomplex_double_real_part, 0);
92105
rb_define_method(c_complex_double, "imaginary",
93106
ccomplex_double_imaginary_part, 0);
94107

95108
#ifdef HAVE_SYMENGINE_MPFR
96109
// RealMPFR class
97-
c_real_mpfr = rb_define_class_under(m_symengine, "RealMPFR", c_basic);
110+
c_real_mpfr = rb_define_class_under(m_symengine, "RealMPFR", c_number);
98111
rb_define_alloc_func(c_real_mpfr, cbasic_alloc);
99112
rb_define_method(c_real_mpfr, "initialize", crealmpfr_init, 2);
100113
rb_define_method(c_real_mpfr, "to_f", crealmpfr_to_float, 0);
101114
#endif // HAVE_SYMENGINE_MPFR
102115

103116
#ifdef HAVE_SYMENGINE_MPC
104117
// ComplexMPC class
105-
c_complex_mpc = rb_define_class_under(m_symengine, "ComplexMPC", c_basic);
118+
c_complex_mpc = rb_define_class_under(m_symengine, "ComplexMPC", c_number);
106119
rb_define_alloc_func(c_complex_mpc, cbasic_alloc);
120+
rb_define_method(c_complex_mpc, "real", ccomplex_mpc_real_part, 0);
121+
rb_define_method(c_complex_mpc, "imaginary", ccomplex_mpc_imaginary_part,
122+
0);
107123
#endif // HAVE_SYMENGINE_MPC
108124

109125
// Constant class

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
// variable names for classes begin with c
1111
VALUE c_basic;
1212
VALUE c_symbol;
13+
VALUE c_number;
1314
VALUE c_integer;
1415
VALUE c_real_double;
1516
VALUE c_rational;

ext/symengine/symengine_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2)
9191
void get_symintfromval(VALUE operand2, basic_struct *cbasic_operand2)
9292
{
9393
if (TYPE(operand2) == T_FIXNUM) {
94-
int i = NUM2INT(operand2);
94+
long int i = NUM2LONG(operand2);
9595
integer_set_si(cbasic_operand2, i);
9696
} else if (TYPE(operand2) == T_BIGNUM) {
9797
VALUE Rb_Temp_String = rb_funcall(operand2, rb_intern("to_s"), 0, NULL);

0 commit comments

Comments
 (0)