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

Common evals method and its CWrappers #987

Merged
merged 17 commits into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from 16 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ symengine/tests/eval/test_eval_double
symengine/tests/eval/test_eval_arb
symengine/tests/eval/test_eval_mpfr
symengine/tests/eval/test_eval_mpc
symengine/tests/eval/test_evalf
symengine/tests/eval/test_lambda_double
symengine/tests/expression/test_expression
symengine/tests/basic/test_series_expansion_UP
Expand Down
3 changes: 2 additions & 1 deletion symengine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ set(SRC
mp_wrapper.cpp
sets.cpp
polynomial_multivariate.cpp
eval.cpp
fields.cpp
)

Expand Down Expand Up @@ -81,7 +82,7 @@ set(HEADERS
eval_mpfr.h eval_arb.h eval_mpc.h complex_double.h series_visitor.h
real_mpfr.h complex_mpc.h type_codes.inc lambda_double.h series.h series_piranha.h
basic-methods.inc series_flint.h series_generic.h sets.h derivative.h polys/upolybase.h
polys/uintpoly_flint.h polys/uintpoly_piranha.h
polys/uintpoly_flint.h polys/uintpoly_piranha.h eval.h
)

# Configure SymEngine using our CMake options:
Expand Down
22 changes: 21 additions & 1 deletion symengine/cwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <symengine/symbol.h>
#include <symengine/cwrapper.h>
#include <symengine/printer.h>
#include <symengine/eval.h>

#define xstr(s) str(s)
#define str(s) #s
Expand Down Expand Up @@ -237,8 +238,20 @@ mpfr_prec_t real_mpfr_get_prec(const basic s)
return ((rcp_static_cast<const RealMPFR>(s->m))->as_mpfr()).get_prec();
}

#endif // HAVE_SYMENGINE_MPFR
int real_mpfr_is_zero(const basic s)
{
SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
return (int)((rcp_static_cast<const RealMPFR>(s->m))->is_zero());
}

#endif // HAVE_SYMENGINE_MPFR
#ifdef HAVE_SYMENGINE_MPC
int complex_mpc_is_zero(const basic s)
{
SYMENGINE_ASSERT(is_a<ComplexMPC>(*(s->m)));
return (int)((rcp_static_cast<const ComplexMPC>(s->m))->is_zero());
}
#endif // HAVE_SYMENGINE_MPC
signed long integer_get_si(const basic s)
{
SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
Expand Down Expand Up @@ -720,6 +733,13 @@ void ntheory_binomial(basic s, const basic a, unsigned long b)
s->m = SymEngine::binomial(static_cast<const Integer &>(*(a->m)), b);
}

//! Wrapper for evalf
void basic_evalf(basic s, const basic b, unsigned long bits, int real)
{

s->m = SymEngine::evalf(*(b->m), bits, (bool)real);
}

//! Print stacktrace on segfault
void symengine_print_stack_on_segfault()
{
Expand Down
11 changes: 10 additions & 1 deletion symengine/cwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void integer_set_ui(basic s, unsigned long i);
void integer_set_mpz(basic s, const mpz_t i);
//! Assign to s, an integer that has base 10 representation c.
void integer_set_str(basic s, const char *c);
//! Assign to s, a real_double that has base 10 representation c.
//! Assign to s, a real_double that has value of d.
void real_double_set_d(basic s, double d);
//! Returns double value of s.
double real_double_get_d(const basic s);
Expand All @@ -149,8 +149,15 @@ void real_mpfr_set(basic s, mpfr_srcptr m);
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);
//! Returns 1 if s has value zero; 0 otherwise
int real_mpfr_is_zero(const basic s);
#endif // HAVE_SYMENGINE_MPFR

#ifdef HAVE_SYMENGINE_MPC
//! Returns 1 if s has value zero; 0 otherwise
int complex_mpc_is_zero(const basic s);
#endif // HAVE_SYMENGINE_MPC

//! Returns signed long value of s.
signed long integer_get_si(const basic s);
//! Returns unsigned long value of s.
Expand Down Expand Up @@ -394,6 +401,8 @@ void ntheory_fibonacci(basic s, unsigned long a);
void ntheory_lucas(basic s, unsigned long a);
//! Binomial Coefficient
void ntheory_binomial(basic s, const basic a, unsigned long b);
//! Evaluate b and assign the value to s
void basic_evalf(basic s, const basic b, unsigned long bits, int real);

//! Print stacktrace on segfault
void symengine_print_stack_on_segfault();
Expand Down
52 changes: 52 additions & 0 deletions symengine/eval.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <exception>
#include <symengine/eval.h>
#include <symengine/eval_double.h>
#include <symengine/real_double.h>
#include <symengine/complex_double.h>
#include <symengine/symengine_rcp.h>
#ifdef HAVE_SYMENGINE_MPFR
#include <mpfr.h>
#include <symengine/real_mpfr.h>
#include <symengine/eval_mpfr.h>
#endif // HAVE_SYMENGINE_MPFR

#ifdef HAVE_SYMENGINE_MPC
#include <mpc.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<mpc.h> is already there in <symengine/complex_mpc.h>. I am not sure if occurrences like these are removed.

#include <symengine/complex_mpc.h>
#include <symengine/eval_mpc.h>
#endif // HAVE_SYMENGINE_MPC

namespace SymEngine
{

RCP<const Number> evalf(const Basic &b, unsigned long bits, bool real)
{
if (bits <= 53 && real) { // double
double d = eval_double(b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return a RealDouble here

return real_double(d);
} else if (bits <= 53 && !real) { // complex double
std::complex<double> d = eval_complex_double(b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return a ComplexDouble here

return complex_double(d);
} else if (bits > 53 && real) { // mpfr
#ifdef HAVE_SYMENGINE_MPFR
mpfr_class mc = mpfr_class(bits);
mpfr_ptr result = mc.get_mpfr_t();
eval_mpfr(result, b, MPFR_RNDN);
return make_rcp<RealMPFR>(std::move(mc));
#else
throw std::invalid_argument(
"For multiple bit precision, MPFR is needed");
#endif // HAVE_SYMENGINE_MPFR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw an error here if HAVE_SYMENGINE_MPFR is not defined saying something like, https://github.com/symengine/symengine.py/blob/master/symengine/lib/symengine_wrapper.pyx#L2111

} else { // mpc
#ifdef HAVE_SYMENGINE_MPC
mpc_class mc = mpc_class(bits);
mpc_ptr result = mc.get_mpc_t();
eval_mpc(result, b, MPFR_RNDN);
return make_rcp<ComplexMPC>(std::move(mc));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

#else
throw std::invalid_argument(
"For multiple bit precision, MPC is needed");
#endif // HAVE_SYMENGINE_MPC
}
}
} // SymEngine
32 changes: 32 additions & 0 deletions symengine/eval.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* \file eval.h
*
**/
#ifndef SYMENGINE_EVAL_H
#define SYMENGINE_EVAL_H

#include <symengine/basic.h>
#include <symengine/dict.h>
#include <symengine/symengine_rcp.h>

#ifdef HAVE_SYMENGINE_MPFR
#include <mpfr.h>
#endif // HAVE_SYMENGINE_MPFR

#ifdef SYMENGINE_HAVE_MPC
#include <mpc.h>
#endif // HAVE_SYMENGINE_MPC

namespace SymEngine
{

/*
* Evaluates basic b, according to the number of significant bits, and the
* complex / real nature of the number
*/

RCP<const Number> evalf(const Basic &b, unsigned long bits, bool real);

} // SymEngine

#endif // SYMENGINE_EVAL_H
6 changes: 4 additions & 2 deletions symengine/fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ class GaloisFieldDict
// Returns whether polynomial is squarefield in `modulo_`
bool gf_is_sqf() const;

// Returns the square free decomposition of polynomial's monic representation in `modulo_`
// A vector of pair is returned where each element is a factor and each pair's first
// Returns the square free decomposition of polynomial's monic
// representation in `modulo_`
// A vector of pair is returned where each element is a factor and each
// pair's first
// raised to power of second gives the factor.
std::vector<std::pair<GaloisFieldDict, integer_class>> gf_sqf_list() const;

Expand Down
3 changes: 2 additions & 1 deletion symengine/polys/uintpoly_piranha.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace piranha
// overloading pow for pirahna::math::evaluate
namespace math
{

template <typename U>
struct pow_impl<SymEngine::integer_class, U,
SymEngine::enable_if_t<std::is_integral<U>::value>> {
Expand Down Expand Up @@ -224,4 +225,4 @@ inline bool divides_upoly(const UIntPolyPiranha &a, const UIntPolyPiranha &b,

#endif // HAVE_SYMENGINE_PIRANHA

#endif // SYMENGINE_UINTPOLY_PIRANHA_H
#endif // SYMENGINE_UINTPOLY_PIRANHA_H
Loading