Skip to content

Commit 8e536cf

Browse files
committed
Adding HAVE_MPFR/MPC as constants, adding HAVE_SYMENGINE_MPFR/MPC checks, and fixing formating
1 parent f092e0d commit 8e536cf

File tree

9 files changed

+104
-50
lines changed

9 files changed

+104
-50
lines changed

ext/symengine/ruby_constant.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,19 @@ VALUE cconstant_euler_gamma() {
2626
VALUE cconstant_i() {
2727
return cconstant_const(basic_const_I, c_complex);
2828
}
29+
30+
VALUE cconstant_have_mpfr() {
31+
#ifdef HAVE_SYMENGINE_MPFR
32+
return Qtrue;
33+
#else
34+
return Qfalse;
35+
#endif //HAVE_SYMENGINE_MPFR
36+
}
37+
38+
VALUE cconstant_have_mpc() {
39+
#ifdef HAVE_SYMENGINE_MPC
40+
return Qtrue;
41+
#else
42+
return Qfalse;
43+
#endif //HAVE_SYMENGINE_MPC
44+
}

ext/symengine/ruby_constant.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ VALUE cconstant_euler_gamma();
1717

1818
VALUE cconstant_i();
1919

20+
VALUE cconstant_have_mpfr();
21+
22+
VALUE cconstant_have_mpc();
23+
2024
#endif //RUBY_CONSTANTS_H_

ext/symengine/ruby_real_mpfr.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ruby_real_mpfr.h"
22

3+
#ifdef HAVE_SYMENGINE_MPFR
34
VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value)
45
{
56
basic_struct *cresult;
@@ -20,8 +21,8 @@ VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value)
2021
break;
2122
case T_DATA:
2223
c = rb_obj_classname(num_value);
23-
if(strcmp(c, "BigDecimal") == 0){
24-
c = RSTRING_PTR( rb_funcall(num_value, rb_intern("to_s"), 1, rb_str_new2("F")) );
24+
if (strcmp(c, "BigDecimal") == 0) {
25+
c = RSTRING_PTR(rb_funcall(num_value, rb_intern("to_s"), 1, rb_str_new2("F")));
2526
real_mpfr_set_str(cresult, c, prec);
2627
break;
2728
}
@@ -44,4 +45,5 @@ VALUE crealmpfr_to_float(VALUE self)
4445

4546
return rb_funcall(rb_funcall(self, rb_intern("to_s"), 0, NULL), rb_intern("to_f"), 0, NULL);
4647
}
48+
#endif //HAVE_SYMENGINE_MPFR
4749

ext/symengine/ruby_real_mpfr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "symengine.h"
88
#include "symengine_utils.h"
99

10+
#ifdef HAVE_SYMENGINE_MPFR
1011
VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value);
1112
VALUE crealmpfr_to_float(VALUE self);
13+
#endif //HAVE_SYMENGINE_MPFR
1214

1315
#endif //RUBY_REAL_MPFR_H_

ext/symengine/symengine.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,19 @@ void Init_symengine() {
8686
rb_define_method(c_complex_double, "real", ccomplex_double_real_part, 0);
8787
rb_define_method(c_complex_double, "imaginary", ccomplex_double_imaginary_part, 0);
8888

89+
#ifdef HAVE_SYMENGINE_MPFR
8990
//RealMPFR class
9091
c_real_mpfr = rb_define_class_under(m_symengine, "RealMPFR", c_basic);
9192
rb_define_alloc_func(c_real_mpfr, cbasic_alloc);
9293
rb_define_method(c_real_mpfr, "initialize", crealmpfr_init, 2);
9394
rb_define_method(c_real_mpfr, "to_f", crealmpfr_to_float, 0);
95+
#endif //HAVE_SYMENGINE_MPFR
9496

97+
#ifdef HAVE_SYMENGINE_MPC
9598
//ComplexMPC class
9699
c_complex_mpc = rb_define_class_under(m_symengine, "ComplexMPC", c_basic);
97100
rb_define_alloc_func(c_complex_mpc, cbasic_alloc);
101+
#endif //HAVE_SYMENGINE_MPC
98102

99103
//Constant class
100104
c_constant = rb_define_class_under(m_symengine, "Constant", c_basic);
@@ -104,6 +108,8 @@ void Init_symengine() {
104108
rb_define_const(m_symengine, "E", cconstant_e());
105109
rb_define_const(m_symengine, "EULER_GAMMA", cconstant_euler_gamma());
106110
rb_define_const(m_symengine, "I", cconstant_i());
111+
rb_define_const(m_symengine, "HAVE_MPFR", cconstant_have_mpfr());
112+
rb_define_const(m_symengine, "HAVE_MPC", cconstant_have_mpc());
107113

108114
//Add class
109115
c_add = rb_define_class_under(m_symengine, "Add", c_basic);

ext/symengine/symengine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ VALUE c_real_double;
1414
VALUE c_rational;
1515
VALUE c_complex;
1616
VALUE c_complex_double;
17+
#ifdef HAVE_SYMENGINE_MPFR
1718
VALUE c_real_mpfr;
19+
#endif //HAVE_SYMENGINE_MPFR
20+
#ifdef HAVE_SYMENGINE_MPC
1821
VALUE c_complex_mpc;
22+
#endif //HAVE_SYMENGINE_MPC
1923
VALUE c_constant;
2024
VALUE c_add;
2125
VALUE c_mul;

ext/symengine/symengine_utils.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
88
VALUE a, b;
99
double f;
1010
char *c;
11-
VALUE rb_cBigDecimal;
11+
rb_cBigDecimal = CLASS_OF(rb_eval_string("BigDecimal.new('0.0001')"));
1212

1313
switch(TYPE(operand2)) {
1414
case T_FIXNUM:
@@ -62,12 +62,13 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
6262

6363
case T_DATA:
6464
c = rb_obj_classname(operand2);
65-
rb_cBigDecimal = CLASS_OF(rb_eval_string("BigDecimal.new('0.0001')")) ;
65+
#ifdef HAVE_SYMENGINE_MPFR
6666
if(CLASS_OF(operand2) == rb_cBigDecimal){
6767
c = RSTRING_PTR( rb_funcall(operand2, rb_intern("to_s"), 1, rb_str_new2("F")) );
6868
real_mpfr_set_str(cbasic_operand2, c, 200);
6969
break;
7070
}
71+
#endif //HAVE_SYMENGINE_MPFR
7172

7273
Data_Get_Struct(operand2, basic_struct, temp);
7374
basic_assign(cbasic_operand2, temp);
@@ -96,16 +97,20 @@ VALUE Klass_of_Basic(const basic_struct *basic_ptr) {
9697
return c_integer;
9798
case SYMENGINE_REAL_DOUBLE:
9899
return c_real_double;
100+
#ifdef HAVE_SYMENGINE_MPFR
99101
case SYMENGINE_REAL_MPFR:
100102
return c_real_mpfr;
103+
#endif //HAVE_SYMENGINE_MPFR
101104
case SYMENGINE_RATIONAL:
102105
return c_rational;
103106
case SYMENGINE_COMPLEX:
104107
return c_complex;
105108
case SYMENGINE_COMPLEX_DOUBLE:
106109
return c_complex_double;
110+
#ifdef HAVE_SYMENGINE_MPC
107111
case SYMENGINE_COMPLEX_MPC:
108112
return c_complex_mpc;
113+
#endif //HAVE_SYMENGINE_MPFR
109114
case SYMENGINE_CONSTANT:
110115
return c_constant;
111116
case SYMENGINE_ADD:

ext/symengine/symengine_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "ruby_basic.h"
66
#include "symengine/cwrapper.h"
77

8+
VALUE rb_cBigDecimal;
9+
810
//Returns the pointer wrapped inside the Ruby VALUE
911
void sympify(VALUE operand2, basic_struct *cbasic_operand2);
1012
//Returns the pointer wrapped inside the Ruby Fixnum or Bignum

spec/real_mpfr_spec.rb

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,84 @@
11
require 'bigdecimal'
22

3-
describe SymEngine::RealMPFR do
4-
describe 'Convert BigDemcimal to RealMPFR' do
5-
shared_examples 'simple real check' do
6-
subject { SymEngine(value) }
3+
if SymEngine::HAVE_MPFR
74

8-
it { is_expected.to be_a SymEngine::RealMPFR }
9-
its(:to_f) { is_expected.to eq value.to_f }
10-
end
5+
describe SymEngine::RealMPFR do
6+
describe 'Convert BigDemcimal to RealMPFR' do
7+
shared_examples 'simple real check' do
8+
subject { SymEngine(value) }
119

12-
context 'positive BigDecimal' do
13-
let(:value) { BigDecimal("12.3") }
10+
it { is_expected.to be_a SymEngine::RealMPFR }
11+
its(:to_f) { is_expected.to eq value.to_f }
12+
end
1413

15-
it_behaves_like 'simple real check'
16-
end
14+
context 'positive BigDecimal' do
15+
let(:value) { BigDecimal("12.3") }
1716

18-
context 'negative BigDecimal' do
19-
let(:value) { BigDecimal("-12.3") }
17+
it_behaves_like 'simple real check'
18+
end
2019

21-
it_behaves_like 'simple real check'
20+
context 'negative BigDecimal' do
21+
let(:value) { BigDecimal("-12.3") }
22+
23+
it_behaves_like 'simple real check'
24+
end
2225
end
23-
end
2426

25-
describe 'Initialize RealMPFR' do
26-
shared_examples 'simple real check' do
27-
subject { SymEngine::RealMPFR.new(value, 200) }
27+
describe 'Initialize RealMPFR' do
28+
shared_examples 'simple real check' do
29+
subject { SymEngine::RealMPFR.new(value, 200) }
2830

29-
it { is_expected.to be_a SymEngine::RealMPFR }
30-
its(:to_f) { is_expected.to eq value.to_f }
31-
end
31+
it { is_expected.to be_a SymEngine::RealMPFR }
32+
its(:to_f) { is_expected.to eq value.to_f }
33+
end
3234

33-
context 'positive BigDecimal' do
34-
let(:value) { BigDecimal("12.3") }
35+
context 'positive BigDecimal' do
36+
let(:value) { BigDecimal("12.3") }
3537

36-
it_behaves_like 'simple real check'
37-
end
38+
it_behaves_like 'simple real check'
39+
end
3840

39-
context 'negative BigDecimal' do
40-
let(:value) { BigDecimal("-12.3") }
41+
context 'negative BigDecimal' do
42+
let(:value) { BigDecimal("-12.3") }
4143

42-
it_behaves_like 'simple real check'
43-
end
44+
it_behaves_like 'simple real check'
45+
end
4446

45-
context 'positive Float' do
46-
let(:value) { 12.3 }
47+
context 'positive Float' do
48+
let(:value) { 12.3 }
4749

48-
it_behaves_like 'simple real check'
49-
end
50+
it_behaves_like 'simple real check'
51+
end
5052

51-
context 'negative Float' do
52-
let(:value) { 12.3 }
53+
context 'negative Float' do
54+
let(:value) { 12.3 }
5355

54-
it_behaves_like 'simple real check'
55-
end
56+
it_behaves_like 'simple real check'
57+
end
5658

57-
context 'String representation of positive value' do
58-
let(:value) { "12.3" }
59+
context 'String representation of positive value' do
60+
let(:value) { "12.3" }
5961

60-
it_behaves_like 'simple real check'
61-
end
62+
it_behaves_like 'simple real check'
63+
end
6264

63-
context 'String representation of negative value' do
64-
let(:value) { "-12.3" }
65+
context 'String representation of negative value' do
66+
let(:value) { "-12.3" }
6567

66-
it_behaves_like 'simple real check'
68+
it_behaves_like 'simple real check'
69+
end
70+
end
71+
72+
if SymEngine::HAVE_MPC
73+
describe SymEngine::ComplexMPC do
74+
let(:i) { SymEngine::I }
75+
let(:real) { SymEngine(BigDecimal("34.5")) }
76+
let(:imag) { SymEngine(BigDecimal("65.8")) }
77+
78+
subject { real + i * imag }
79+
it { is_expected.to be_a SymEngine::ComplexMPC }
80+
end
6781
end
68-
end
69-
7082

83+
end
7184
end

0 commit comments

Comments
 (0)