[WIP] Wrappers for Real MPFR and Complex MPC #972

Merged
merged 9 commits into from Jun 8, 2016

Projects

None yet

2 participants

@rajithv
Contributor
rajithv commented Jun 1, 2016

I am having trouble testing the MPFR code so far, as the cmake sets WITH_MPFR to false. I tried to change the CMakeLists.txt to no avail. I have installed libmpfr4 and libmpfr-dev on system, in case that is needed. What am I missing here?

@rajithv rajithv MPFR init stuff
2da29b6
@isuruf
Member
isuruf commented Jun 2, 2016

Use cmake -DWITH_MPFR=yes

@rajithv
Contributor
rajithv commented Jun 2, 2016

@isuruf thanks.. it is working now

@rajithv rajithv Fixed some errors in mpfr
71b0aff
@isuruf isuruf and 1 other commented on an outdated diff Jun 3, 2016
symengine/cwrapper.cpp
@@ -200,6 +205,34 @@ double real_double_get_d(const basic s)
return (rcp_static_cast<const RealDouble>(s->m))->as_double();
}
+#ifdef HAVE_SYMENGINE_MPFR
+
+void real_mpfr_set_d(basic s, double d, int prec)
+{
+ mpfr_t mp;
@isuruf
isuruf Jun 3, 2016 Member

Use mpfr_class

@rajithv
rajithv Jun 3, 2016 Contributor

that would require using this constructor: mpfr_class(std::string s, mpfr_prec_t prec = 53, unsigned base = 10)

For the real_mpfr_set_str this would be better, but for real_mpfr_set_d it would need to be converted into string first. What do you think?

@isuruf
isuruf Jun 3, 2016 Member

You can use this constructor, mpfr_class(mpfr_prec_t prec = 53)

@rajithv
rajithv Jun 3, 2016 edited Contributor

That is to set precision. To set the value, we would anyway have to access the mpfr_t directly. Unless we go through the mpfr_class(std::string s, mpfr_prec_t prec = 53, unsigned base = 10) class. Isn't it so?

@isuruf
isuruf Jun 3, 2016 Member

You can access the mpfr_t directly. This way, mp is not copied unnecessarily.

@isuruf isuruf commented on an outdated diff Jun 3, 2016
symengine/cwrapper.cpp
@@ -200,6 +205,34 @@ double real_double_get_d(const basic s)
return (rcp_static_cast<const RealDouble>(s->m))->as_double();
}
+#ifdef HAVE_SYMENGINE_MPFR
+
+void real_mpfr_set_d(basic s, double d, int prec)
+{
+ mpfr_t mp;
+ mpfr_init2 (mp, prec);
+ mpfr_set_d (mp, d, MPFR_RNDN);
+ s->m = SymEngine::real_mpfr(mpfr_class(mp));
+ mpfr_clear(mp);
+}
+
+void real_mpfr_set_str(basic s, char *c, int prec)
+{
+ mpfr_t mp;
@isuruf
isuruf Jun 3, 2016 Member

Same here

@isuruf isuruf commented on an outdated diff Jun 3, 2016
symengine/cwrapper.cpp
@@ -410,6 +443,16 @@ int is_a_ComplexDouble(const basic c)
{
return is_a<ComplexDouble>(*(c->m));
}
+#ifdef HAVE_SYMENGINE_MPFR
+int is_a_RealMPFR(const basic c)
+{
+ return is_a<RealMPFR>(*(c->m));
@isuruf
isuruf Jun 3, 2016 Member

This should return false if HAVE_SYMENGINE_MPFR is not defined

@isuruf isuruf commented on an outdated diff Jun 3, 2016
symengine/cwrapper.cpp
@@ -410,6 +443,16 @@ int is_a_ComplexDouble(const basic c)
{
return is_a<ComplexDouble>(*(c->m));
}
+#ifdef HAVE_SYMENGINE_MPFR
+int is_a_RealMPFR(const basic c)
+{
+ return is_a<RealMPFR>(*(c->m));
+}
+int is_a_ComplexMPC(const basic c)
+{
+ return is_a<ComplexMPC>(*(c->m));
@isuruf
isuruf Jun 3, 2016 Member

This should return false if HAVE_SYMENGINE_MPC is not defined. (Note: MPC not MPFR)

@isuruf isuruf commented on the diff Jun 3, 2016
symengine/cwrapper.h
@@ -128,6 +132,12 @@ void integer_set_str(basic s, char *c);
void real_double_set_d(basic s, double d);
//! Returns double value of s.
double real_double_get_d(const basic s);
+//! Assign to s, a real mpfr that has value d with precision prec.
+void real_mpfr_set_d(basic s, double d, int prec);
+//! Assign to s, a real mpfr that has base 10 representation c with precision prec.
+void real_mpfr_set_str(basic s, char *c, int prec);
+//! Returns double value of s.
+double real_mpfr_get_d(const basic s);
@isuruf
isuruf Jun 3, 2016 edited Member

For completeness, you'll need,

void real_mpfr_set(basic s, mpfr_srcptr m)
void real_mpfr_get(mpfr_ptr m, const basic s)
mpfr_prec_t real_mpfr_get_prec(const basic s)
@rajithv rajithv Minor fixes
abba2a0
@isuruf isuruf commented on an outdated diff Jun 3, 2016
symengine/cwrapper.cpp
@@ -200,6 +205,29 @@ double real_double_get_d(const basic s)
return (rcp_static_cast<const RealDouble>(s->m))->as_double();
}
+#ifdef HAVE_SYMENGINE_MPFR
+
+void real_mpfr_set_d(basic s, double d, int prec)
+{
+ mpfr_class mc = mpfr_class(prec);
+ mpfr_ptr mp = mc.get_mpfr_t();
+ mpfr_set_d (mp, d, MPFR_RNDN);
+ s->m = SymEngine::real_mpfr(mpfr_class(mp));
@isuruf
isuruf Jun 3, 2016 Member

Use it like this,

mpfr_class mc = mpfr_class(prec);
mpfr_set_d (mc.get_mpfr_t(), d, MPFR_RNDN);
s->m = SymEngine::real_mpfr(std::move(mc));
@isuruf isuruf commented on an outdated diff Jun 3, 2016
symengine/cwrapper.cpp
@@ -410,6 +438,20 @@ int is_a_ComplexDouble(const basic c)
{
return is_a<ComplexDouble>(*(c->m));
}
+int is_a_RealMPFR(const basic c)
+{
+#ifdef HAVE_SYMENGINE_MPFR
+ return is_a<RealMPFR>(*(c->m));
+#endif // HAVE_SYMENGINE_MPFR
+ return false;
+}
+int is_a_ComplexMPC(const basic c)
+{
+#ifdef HAVE_SYMENGINE_MPC
+ return is_a<ComplexMPC>(*(c->m));
+#endif // HAVE_SYMENGINE_MPC
+ return false;
@isuruf
isuruf Jun 3, 2016 Member

Use an else here, otherwise the compiler spits out warnings about unreachable code

@isuruf isuruf commented on an outdated diff Jun 4, 2016
symengine/cwrapper.cpp
@@ -28,10 +30,13 @@ using SymEngine::Rational;
using SymEngine::Integer;
using SymEngine::integer_class;
using SymEngine::rational_class;
+using SymEngine::mpfr_class;
@isuruf
isuruf Jun 4, 2016 Member

This should be inside a #ifdef

@isuruf isuruf commented on an outdated diff Jun 4, 2016
symengine/cwrapper.cpp
using SymEngine::Number;
using SymEngine::Complex;
using SymEngine::ComplexDouble;
using SymEngine::RealDouble;
+using SymEngine::RealMPFR;
+using SymEngine::ComplexMPC;
@isuruf
isuruf Jun 4, 2016 Member

Same here

rajithv added some commits Jun 6, 2016
@rajithv rajithv Fixes in cwrapper
52ff18a
@rajithv rajithv Merge branch 'master' of https://github.com/symengine/symengine into …
…mpfr_mpc
db49b4f
@isuruf isuruf commented on an outdated diff Jun 6, 2016
symengine/cwrapper.cpp
+
+double real_mpfr_get_d(const basic s)
+{
+ SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
+ return mpfr_get_d ( ((rcp_static_cast<const RealMPFR>(s->m))->as_mpfr()).get_mpfr_t() , MPFR_RNDN);
+}
+
+void real_mpfr_set(basic s, mpfr_srcptr m)
+{
+ s->m = SymEngine::real_mpfr(mpfr_class(m));
+}
+
+void real_mpfr_get(mpfr_ptr m, const basic s)
+{
+ SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
+ m = ((rcp_static_cast<const RealMPFR>(s->m))->as_mpfr()).get_mpfr_t();
@isuruf
isuruf Jun 6, 2016 Member

Use mpfr_set here

rajithv added some commits Jun 6, 2016
@rajithv rajithv MPC wrappers
5408fb2
@rajithv rajithv Minor fix
a05bd15
@isuruf isuruf commented on an outdated diff Jun 7, 2016
symengine/cwrapper.h
+void real_mpfr_set_d(basic s, double d, int prec);
+//! Assign to s, a real mpfr that has base 10 representation c with precision prec.
+void real_mpfr_set_str(basic s, char *c, int prec);
+//! Returns double value of s.
+double real_mpfr_get_d(const basic s);
+//! Assign to s, a real mpfr that has value pointed by m.
+void real_mpfr_set(basic s, mpfr_srcptr m);
+//! Assign to m, the mpfr_t given in s.
+void real_mpfr_get(mpfr_ptr m, const basic s);
+//! Returns the precision of the mpfr_t given by s.
+mpfr_prec_t real_mpfr_get_prec(const basic s);
+#endif // HAVE_SYMENGINE_MPFR
+
+#ifdef HAVE_SYMENGINE_MPC
+//! Assign to s, the CompleMPC equivalent to real + i*imaginary.
+void complex_mpc_set_mpfr(basic s, const basic real, const basic imaginary);
@isuruf
isuruf Jun 7, 2016 Member

Is this implemented?

@rajithv rajithv minor fix
b7565d5
@isuruf
Member
isuruf commented Jun 8, 2016

Looks good. It will be helpful if you could have commit messages that make more sense in the future.

@isuruf isuruf merged commit 637931c into symengine:master Jun 8, 2016

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@isuruf isuruf referenced this pull request in symengine/symengine.rb Jun 8, 2016
Merged

Abs #50

@isuruf isuruf commented on the diff Jun 10, 2016
symengine/cwrapper.h
@@ -5,6 +5,10 @@
#include <stdlib.h>
#include <gmp.h>
+#ifdef HAVE_SYMENGINE_MPFR
@isuruf
isuruf Jun 10, 2016 Member

You should include symengine/symengine_config.h before this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment