From 13cbcb4353d05566f557b0dc40ea49065612e2eb Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sat, 23 Jul 2016 00:43:27 +0530 Subject: [PATCH 01/19] Init exception handling --- symengine/cwrapper.cpp | 14 ++++++++++++-- symengine/cwrapper.h | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index 0c0e91f730..d57622393d 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -399,9 +399,17 @@ void basic_pow(basic s, const basic a, const basic b) s->m = SymEngine::pow(a->m, b->m); } -void basic_div(basic s, const basic a, const basic b) +int basic_div(basic s, const basic a, const basic b) { - s->m = SymEngine::div(a->m, b->m); + try + { + s->m = SymEngine::div(a->m, b->m); + return 0; + } + catch ( ... ) + { + return -1; + } } int basic_eq(const basic a, const basic b) @@ -1036,4 +1044,6 @@ void symengine_print_stack_on_segfault() { SymEngine::print_stack_on_segfault(); } + + } diff --git a/symengine/cwrapper.h b/symengine/cwrapper.h index 7127acba45..886a1d0909 100644 --- a/symengine/cwrapper.h +++ b/symengine/cwrapper.h @@ -203,7 +203,7 @@ void basic_sub(basic s, const basic a, const basic b); //! Assigns s = a * b. void basic_mul(basic s, const basic a, const basic b); //! Assigns s = a / b. -void basic_div(basic s, const basic a, const basic b); +int basic_div(basic s, const basic a, const basic b); //! Assigns s = a ** b. void basic_pow(basic s, const basic a, const basic b); //! Assign to s, derivative of expr with respect to sym. Returns 0 if sym is not From efe0630bc670a93ba3898447cf63fdd46987f05b Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sun, 24 Jul 2016 12:32:03 +0530 Subject: [PATCH 02/19] Macros for exception handling --- symengine/cwrapper.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index d57622393d..d6a17a2528 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -58,6 +58,25 @@ inline bool is_aligned(T *p, size_t n = alignof(T)) extern "C" { + +#define CWRAPPER_BEGIN() \ + try \ + { + +#define CWRAPPER_END(error, code) \ + return 0; \ + } \ + catch ( error ) \ + { \ + return code; \ + } + +#define CWRAPPER_END_EXTRA(error, code) \ + catch ( error ) \ + { \ + return code; \ + } + struct CRCPBasic { SymEngine::RCP m; }; @@ -401,15 +420,9 @@ void basic_pow(basic s, const basic a, const basic b) int basic_div(basic s, const basic a, const basic b) { - try - { + CWRAPPER_BEGIN(); s->m = SymEngine::div(a->m, b->m); - return 0; - } - catch ( ... ) - { - return -1; - } + CWRAPPER_END(...,-1); } int basic_eq(const basic a, const basic b) From 27516e0798020d2f08f980616d11babd1a0e73ae Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sun, 24 Jul 2016 17:44:11 +0530 Subject: [PATCH 03/19] Basic runtime exception handling for most of the wrappers --- symengine/cwrapper.cpp | 270 ++++++++++++++++++++++++++++++----------- symengine/cwrapper.h | 194 ++++++++++++++--------------- 2 files changed, 299 insertions(+), 165 deletions(-) diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index d6a17a2528..176e8ab772 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -189,34 +189,46 @@ TypeID basic_get_type(const basic s) return static_cast(s->m->get_type_code()); } -void symbol_set(basic s, const char *c) +int symbol_set(basic s, const char *c) { + CWRAPPER_BEGIN(); s->m = SymEngine::symbol(std::string(c)); + CWRAPPER_END(...,-1); } -void integer_set_si(basic s, long i) +int integer_set_si(basic s, long i) { + CWRAPPER_BEGIN(); s->m = SymEngine::integer(integer_class(i)); + CWRAPPER_END(...,-1); } -void integer_set_ui(basic s, unsigned long i) +int integer_set_ui(basic s, unsigned long i) { + CWRAPPER_BEGIN(); s->m = SymEngine::integer(integer_class(i)); + CWRAPPER_END(...,-1); } -void integer_set_mpz(basic s, const mpz_t i) +int integer_set_mpz(basic s, const mpz_t i) { + CWRAPPER_BEGIN(); s->m = SymEngine::integer(integer_class(i)); + CWRAPPER_END(...,-1); } -void integer_set_str(basic s, const char *c) +int integer_set_str(basic s, const char *c) { + CWRAPPER_BEGIN(); s->m = SymEngine::integer(integer_class(c)); + CWRAPPER_END(...,-1); } -void real_double_set_d(basic s, double d) +int real_double_set_d(basic s, double d) { + CWRAPPER_BEGIN(); s->m = SymEngine::real_double(d); + CWRAPPER_END(...,-1); } double real_double_get_d(const basic s) @@ -227,16 +239,20 @@ double real_double_get_d(const basic s) #ifdef HAVE_SYMENGINE_MPFR -void real_mpfr_set_d(basic s, double d, int prec) +int real_mpfr_set_d(basic s, double d, int prec) { + CWRAPPER_BEGIN(); mpfr_class mc = mpfr_class(prec); mpfr_set_d(mc.get_mpfr_t(), d, MPFR_RNDN); s->m = SymEngine::real_mpfr(std::move(mc)); + CWRAPPER_END(...,-1); } -void real_mpfr_set_str(basic s, const char *c, int prec) +int real_mpfr_set_str(basic s, const char *c, int prec) { + CWRAPPER_BEGIN(); s->m = SymEngine::real_mpfr(mpfr_class(c, prec, 10)); + CWRAPPER_END(...,-1); } double real_mpfr_get_d(const basic s) @@ -247,17 +263,21 @@ double real_mpfr_get_d(const basic s) MPFR_RNDN); } -void real_mpfr_set(basic s, mpfr_srcptr m) +int real_mpfr_set(basic s, mpfr_srcptr m) { + CWRAPPER_BEGIN(); s->m = SymEngine::real_mpfr(mpfr_class(m)); + CWRAPPER_END(...,-1); } -void real_mpfr_get(mpfr_ptr m, const basic s) +int real_mpfr_get(mpfr_ptr m, const basic s) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(s->m))); mpfr_set(m, ((rcp_static_cast(s->m))->as_mpfr()).get_mpfr_t(), MPFR_RNDN); + CWRAPPER_END(...,-1); } mpfr_prec_t real_mpfr_get_prec(const basic s) @@ -280,16 +300,20 @@ int complex_mpc_is_zero(const basic s) return (int)((rcp_static_cast(s->m))->is_zero()); } -void complex_mpc_real_part(basic s, const basic com) +int complex_mpc_real_part(basic s, const basic com) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); + CWRAPPER_END(...,-1); } -void complex_mpc_imaginary_part(basic s, const basic com) +int complex_mpc_imaginary_part(basic s, const basic com) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); + CWRAPPER_END(...,-1); } #endif // HAVE_SYMENGINE_MPC @@ -305,20 +329,26 @@ unsigned long integer_get_ui(const basic s) return mp_get_ui((rcp_static_cast(s->m))->as_mpz()); } -void integer_get_mpz(mpz_t a, const basic s) +int integer_get_mpz(mpz_t a, const basic s) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(s->m))); mpz_set(a, get_mpz_t((rcp_static_cast(s->m))->as_mpz())); + CWRAPPER_END(...,-1); } -void rational_set_si(basic s, long a, long b) +int rational_set_si(basic s, long a, long b) { + CWRAPPER_BEGIN(); s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); + CWRAPPER_END(...,-1); } -void rational_set_ui(basic s, unsigned long a, unsigned long b) +int rational_set_ui(basic s, unsigned long a, unsigned long b) { + CWRAPPER_BEGIN(); s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); + CWRAPPER_END(...,-1); } int rational_set(basic s, const basic a, const basic b) @@ -332,52 +362,68 @@ int rational_set(basic s, const basic a, const basic b) return 1; } -void rational_set_mpq(basic s, const mpq_t i) +int rational_set_mpq(basic s, const mpq_t i) { + CWRAPPER_BEGIN(); s->m = SymEngine::Rational::from_mpq(rational_class(i)); + CWRAPPER_END(...,-1); } -void complex_set(basic s, const basic re, const basic im) +int complex_set(basic s, const basic re, const basic im) { + CWRAPPER_BEGIN(); s->m = SymEngine::Complex::from_two_nums( *(rcp_static_cast(re->m)), *(rcp_static_cast(im->m))); + CWRAPPER_END(...,-1); } -void complex_set_rat(basic s, const basic re, const basic im) +int complex_set_rat(basic s, const basic re, const basic im) { + CWRAPPER_BEGIN(); s->m = SymEngine::Complex::from_two_rats( *(rcp_static_cast(re->m)), *(rcp_static_cast(im->m))); + CWRAPPER_END(...,-1); } -void complex_set_mpq(basic s, const mpq_t re, const mpq_t im) +int complex_set_mpq(basic s, const mpq_t re, const mpq_t im) { + CWRAPPER_BEGIN(); s->m = SymEngine::Complex::from_mpq(rational_class(re), rational_class(im)); + CWRAPPER_END(...,-1); } -void complex_real_part(basic s, const basic com) +int complex_real_part(basic s, const basic com) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); + CWRAPPER_END(...,-1); } -void complex_imaginary_part(basic s, const basic com) +int complex_imaginary_part(basic s, const basic com) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); + CWRAPPER_END(...,-1); } -void complex_double_real_part(basic s, const basic com) +int complex_double_real_part(basic s, const basic com) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); + CWRAPPER_END(...,-1); } -void complex_double_imaginary_part(basic s, const basic com) +int complex_double_imaginary_part(basic s, const basic com) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); + CWRAPPER_END(...,-1); } int basic_diff(basic s, const basic expr, basic const symbol) @@ -388,40 +434,52 @@ int basic_diff(basic s, const basic expr, basic const symbol) return 1; } -void basic_assign(basic a, const basic b) +int basic_assign(basic a, const basic b) { + CWRAPPER_BEGIN(); a->m = b->m; + CWRAPPER_END(...,-1); } -void basic_parse(basic b, const char *str) +int basic_parse(basic b, const char *str) { + CWRAPPER_BEGIN(); b->m = SymEngine::parse(str); + CWRAPPER_END(...,-1); } -void basic_add(basic s, const basic a, const basic b) +int basic_add(basic s, const basic a, const basic b) { + CWRAPPER_BEGIN(); s->m = SymEngine::add(a->m, b->m); + CWRAPPER_END(...,-1); } -void basic_sub(basic s, const basic a, const basic b) +int basic_sub(basic s, const basic a, const basic b) { + CWRAPPER_BEGIN(); s->m = SymEngine::sub(a->m, b->m); + CWRAPPER_END(...,-1); } -void basic_mul(basic s, const basic a, const basic b) +int basic_mul(basic s, const basic a, const basic b) { + CWRAPPER_BEGIN(); s->m = SymEngine::mul(a->m, b->m); + CWRAPPER_END(...,-1); } -void basic_pow(basic s, const basic a, const basic b) +int basic_pow(basic s, const basic a, const basic b) { + CWRAPPER_BEGIN(); s->m = SymEngine::pow(a->m, b->m); + CWRAPPER_END(...,-1); } int basic_div(basic s, const basic a, const basic b) { CWRAPPER_BEGIN(); - s->m = SymEngine::div(a->m, b->m); + s->m = SymEngine::div(a->m, b->m); CWRAPPER_END(...,-1); } @@ -450,9 +508,11 @@ int basic_number_sign(const basic s) } #define IMPLEMENT_ONE_ARG_FUNC(func) \ - void basic_##func(basic s, const basic a) \ + int basic_##func(basic s, const basic a) \ { \ + CWRAPPER_BEGIN(); \ s->m = SymEngine::func(a->m); \ + CWRAPPER_END(...,-1); \ } IMPLEMENT_ONE_ARG_FUNC(expand); @@ -679,9 +739,11 @@ void sparse_matrix_rows_cols(CSparseMatrix *s, unsigned long int rows, s->m = SymEngine::CSRMatrix(rows, cols); } -void dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d) +int dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d) { + CWRAPPER_BEGIN(); s->m = d->m; + CWRAPPER_END(...,-1); } char *dense_matrix_str(const CDenseMatrix *s) @@ -700,56 +762,74 @@ char *sparse_matrix_str(const CSparseMatrix *s) return cc; } -void dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) +int dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) { + CWRAPPER_BEGIN(); mat->m.resize(r, c); + CWRAPPER_END(...,-1); } -void dense_matrix_get_basic(basic s, const CDenseMatrix *mat, +int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, unsigned long int r, unsigned long int c) { + CWRAPPER_BEGIN(); s->m = mat->m.get(r, c); + CWRAPPER_END(...,-1); } -void dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, +int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, unsigned long int c, basic s) { + CWRAPPER_BEGIN(); mat->m.set(r, c, s->m); + CWRAPPER_END(...,-1); } -void sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, +int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, unsigned long int r, unsigned long int c) { + CWRAPPER_BEGIN(); s->m = mat->m.get(r, c); + CWRAPPER_END(...,-1); } -void sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, +int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, unsigned long int c, basic s) { + CWRAPPER_BEGIN(); mat->m.set(r, c, s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_det(basic s, const CDenseMatrix *mat) +int dense_matrix_det(basic s, const CDenseMatrix *mat) { + CWRAPPER_BEGIN(); s->m = mat->m.det(); + CWRAPPER_END(...,-1); } -void dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat) +int dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, mat->m.nrows(), mat->m.ncols()); mat->m.inv(s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat) +int dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows()); mat->m.transpose(s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, +int dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, unsigned long int r1, unsigned long int c1, unsigned long int r2, unsigned long int c2, unsigned long int r, unsigned long int c) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, r2 - r1 + 1, c2 - c1 + 1); mat->m.submatrix(s->m, r1, c1, r2, c2, r, c); + CWRAPPER_END(...,-1); } unsigned long int dense_matrix_rows(const CDenseMatrix *s) @@ -762,96 +842,122 @@ unsigned long int dense_matrix_cols(const CDenseMatrix *s) return s->m.ncols(); } -void dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.add_matrix(matB->m, s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, matA->m.nrows(), matB->m.ncols()); matA->m.mul_matrix(matB->m, s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.add_scalar(b->m, s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.mul_scalar(b->m, s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) +int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols()); mat->m.LU(l->m, u->m); + CWRAPPER_END(...,-1); } -void dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) +int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols()); mat->m.LDL(l->m, d->m); + CWRAPPER_END(...,-1); } -void dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) +int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols()); mat->m.FFLU(lu->m); + CWRAPPER_END(...,-1); } -void dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, +int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, const CDenseMatrix *mat) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols()); mat->m.FFLDU(l->m, d->m, u->m); + CWRAPPER_END(...,-1); } -void dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, +int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, const CDenseMatrix *b) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(x, A->m.ncols(), 1); A->m.LU_solve(b->m, x->m); + CWRAPPER_END(...,-1); } -void dense_matrix_ones(CDenseMatrix *s, unsigned long int r, +int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, unsigned long int c) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, r, c); ones(s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, +int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, unsigned long int c) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, r, c); zeros(s->m); + CWRAPPER_END(...,-1); } -void dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) +int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) { + CWRAPPER_BEGIN(); int vec_size = vecbasic_size(d); dense_matrix_rows_cols(s, vec_size + (k >= 0 ? k : -k), vec_size + (k >= 0 ? k : -k)); diag(s->m, d->m, k); + CWRAPPER_END(...,-1); } -void dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, +int dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, int k) { + CWRAPPER_BEGIN(); dense_matrix_rows_cols(s, N, M); eye(s->m, k); + CWRAPPER_END(...,-1); } int is_a_DenseMatrix(const CDenseMatrix *c) @@ -949,14 +1055,18 @@ size_t mapbasicbasic_size(CMapBasicBasic *self) // ---------------------- -void basic_get_args(const basic self, CVecBasic *args) +int basic_get_args(const basic self, CVecBasic *args) { + CWRAPPER_BEGIN(); args->m = self->m->get_args(); + CWRAPPER_END(...,-1); } -void basic_free_symbols(const basic self, CSetBasic *symbols) +int basic_free_symbols(const basic self, CSetBasic *symbols) { + CWRAPPER_BEGIN(); symbols->m = SymEngine::free_symbols(*(self->m)); + CWRAPPER_END(...,-1); } size_t basic_hash(const basic self) @@ -964,19 +1074,25 @@ size_t basic_hash(const basic self) return self->m->hash(); } -void basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb) +int basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb) { + CWRAPPER_BEGIN(); s->m = e->m->subs(mapbb->m); + CWRAPPER_END(...,-1); } -void basic_subs2(basic s, const basic e, const basic a, const basic b) +int basic_subs2(basic s, const basic e, const basic a, const basic b) { + CWRAPPER_BEGIN(); s->m = e->m->subs({{a->m, b->m}}); + CWRAPPER_END(...,-1); } -void function_symbol_set(basic s, const char *c, const CVecBasic *arg) +int function_symbol_set(basic s, const char *c, const CVecBasic *arg) { + CWRAPPER_BEGIN(); s->m = function_symbol(c, arg->m); + CWRAPPER_END(...,-1); } // ---------------------- @@ -991,65 +1107,83 @@ char *ascii_art_str() // Cwrapper for ntheory -void ntheory_gcd(basic s, const basic a, const basic b) +int ntheory_gcd(basic s, const basic a, const basic b) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(a->m))); SYMENGINE_ASSERT(is_a(*(b->m))); s->m = SymEngine::gcd(static_cast(*(a->m)), static_cast(*(b->m))); + CWRAPPER_END(...,-1); } -void ntheory_lcm(basic s, const basic a, const basic b) +int ntheory_lcm(basic s, const basic a, const basic b) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(a->m))); SYMENGINE_ASSERT(is_a(*(b->m))); s->m = SymEngine::lcm(static_cast(*(a->m)), static_cast(*(b->m))); + CWRAPPER_END(...,-1); } -void ntheory_nextprime(basic s, const basic a) +int ntheory_nextprime(basic s, const basic a) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(a->m))); s->m = SymEngine::nextprime(static_cast(*(a->m))); + CWRAPPER_END(...,-1); } -void ntheory_mod(basic s, const basic n, const basic d) +int ntheory_mod(basic s, const basic n, const basic d) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(n->m))); SYMENGINE_ASSERT(is_a(*(d->m))); s->m = SymEngine::mod(static_cast(*(n->m)), static_cast(*(d->m))); + CWRAPPER_END(...,-1); } -void ntheory_quotient(basic s, const basic n, const basic d) +int ntheory_quotient(basic s, const basic n, const basic d) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(n->m))); SYMENGINE_ASSERT(is_a(*(d->m))); s->m = SymEngine::quotient(static_cast(*(n->m)), static_cast(*(d->m))); + CWRAPPER_END(...,-1); } -void ntheory_fibonacci(basic s, unsigned long a) +int ntheory_fibonacci(basic s, unsigned long a) { + CWRAPPER_BEGIN(); s->m = SymEngine::fibonacci(a); + CWRAPPER_END(...,-1); } -void ntheory_lucas(basic s, unsigned long a) +int ntheory_lucas(basic s, unsigned long a) { + CWRAPPER_BEGIN(); s->m = SymEngine::lucas(a); + CWRAPPER_END(...,-1); } -void ntheory_binomial(basic s, const basic a, unsigned long b) +int ntheory_binomial(basic s, const basic a, unsigned long b) { + CWRAPPER_BEGIN(); SYMENGINE_ASSERT(is_a(*(a->m))); s->m = SymEngine::binomial(static_cast(*(a->m)), b); + CWRAPPER_END(...,-1); } //! Wrapper for evalf -void basic_evalf(basic s, const basic b, unsigned long bits, int real) +int basic_evalf(basic s, const basic b, unsigned long bits, int real) { + CWRAPPER_BEGIN(); s->m = SymEngine::evalf(*(b->m), bits, (bool)real); + CWRAPPER_END(...,-1); } //! Print stacktrace on segfault diff --git a/symengine/cwrapper.h b/symengine/cwrapper.h index 886a1d0909..0dae401504 100644 --- a/symengine/cwrapper.h +++ b/symengine/cwrapper.h @@ -108,10 +108,10 @@ void basic_const_E(basic s); void basic_const_EulerGamma(basic s); //! Assign value of b to a. -void basic_assign(basic a, const basic b); +int basic_assign(basic a, const basic b); //! Parse str and assign value to b -void basic_parse(basic b, const char *str); +int basic_parse(basic b, const char *str); //! Returns the typeID of the basic struct TypeID basic_get_type(const basic s); @@ -123,33 +123,33 @@ char *basic_get_class_from_id(TypeID id); //! Assign to s, a symbol with string representation c. //! This function creates a new SymEngine::Symbol from a copy of //! the string in c, thus the caller is free to use c afterwards. -void symbol_set(basic s, const char *c); +int symbol_set(basic s, const char *c); //! Assign to s, a long. -void integer_set_si(basic s, long i); +int integer_set_si(basic s, long i); //! Assign to s, a ulong. -void integer_set_ui(basic s, unsigned long i); +int integer_set_ui(basic s, unsigned long i); //! Assign to s, a mpz_t. -void integer_set_mpz(basic s, const mpz_t i); +int 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); +int integer_set_str(basic s, const char *c); //! Assign to s, a real_double that has value of d. -void real_double_set_d(basic s, double d); +int real_double_set_d(basic s, double d); //! Returns double value of s. double real_double_get_d(const basic s); #ifdef HAVE_SYMENGINE_MPFR //! Assign to s, a real mpfr that has value d with precision prec. -void real_mpfr_set_d(basic s, double d, int prec); +int 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, const char *c, int prec); +int real_mpfr_set_str(basic s, const 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); +int 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); +int 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 @@ -160,9 +160,9 @@ int real_mpfr_is_zero(const basic s); //! Returns 1 if s has value zero; 0 otherwise int complex_mpc_is_zero(const basic s); //! Assign to s, the real part of com -void complex_mpc_real_part(basic s, const basic com); +int complex_mpc_real_part(basic s, const basic com); //! Assign to s, the imaginary part of com -void complex_mpc_imaginary_part(basic s, const basic com); +int complex_mpc_imaginary_part(basic s, const basic com); #endif // HAVE_SYMENGINE_MPC //! Returns signed long value of s. @@ -170,42 +170,42 @@ signed long integer_get_si(const basic s); //! Returns unsigned long value of s. unsigned long integer_get_ui(const basic s); //! Returns s as a mpz_t. -void integer_get_mpz(mpz_t a, const basic s); +int integer_get_mpz(mpz_t a, const basic s); //! Assign to s, a rational i/j. Returns 0 if either i or j is not an integer. int rational_set(basic s, const basic i, const basic j); //! Assign to s, a rational i/j, where i and j are signed longs. -void rational_set_si(basic s, long i, long j); +int rational_set_si(basic s, long i, long j); //! Assign to s, a rational i/j, where i and j are unsigned longs. -void rational_set_ui(basic s, unsigned long i, unsigned long j); +int rational_set_ui(basic s, unsigned long i, unsigned long j); //! Assign to s, a rational i, where is of type mpq_t. -void rational_set_mpq(basic s, const mpq_t i); +int rational_set_mpq(basic s, const mpq_t i); //! Assign to s, a complex re + i*im. -void complex_set(basic s, const basic re, const basic im); +int complex_set(basic s, const basic re, const basic im); //! Assign to s, a complex re + i*im, where re and im are rationals. -void complex_set_rat(basic s, const basic re, const basic im); +int complex_set_rat(basic s, const basic re, const basic im); //! Assign to s, a complex re + i*im, where re and im are of type mpq. -void complex_set_mpq(basic s, const mpq_t re, const mpq_t im); +int complex_set_mpq(basic s, const mpq_t re, const mpq_t im); //! Assign to s, a real where com is a complex -void complex_real_part(basic s, const basic com); +int complex_real_part(basic s, const basic com); //! Assign to s, an imaginary where com is a complex -void complex_imaginary_part(basic s, const basic com); +int complex_imaginary_part(basic s, const basic com); //! Assign to s, a real double where com is a complex double -void complex_double_real_part(basic s, const basic com); +int complex_double_real_part(basic s, const basic com); //! Assign to s, an imaginary double where com is a complex double -void complex_double_imaginary_part(basic s, const basic com); +int complex_double_imaginary_part(basic s, const basic com); //! Assigns s = a + b. -void basic_add(basic s, const basic a, const basic b); +int basic_add(basic s, const basic a, const basic b); //! Assigns s = a - b. -void basic_sub(basic s, const basic a, const basic b); +int basic_sub(basic s, const basic a, const basic b); //! Assigns s = a * b. -void basic_mul(basic s, const basic a, const basic b); +int basic_mul(basic s, const basic a, const basic b); //! Assigns s = a / b. int basic_div(basic s, const basic a, const basic b); //! Assigns s = a ** b. -void basic_pow(basic s, const basic a, const basic b); +int basic_pow(basic s, const basic a, const basic b); //! Assign to s, derivative of expr with respect to sym. Returns 0 if sym is not //! a symbol. int basic_diff(basic s, const basic expr, const basic sym); @@ -217,77 +217,77 @@ int basic_neq(const basic a, const basic b); int basic_number_sign(const basic s); //! Expands the expr a and assigns to s. -void basic_expand(basic s, const basic a); +int basic_expand(basic s, const basic a); //! Assigns s = -a. -void basic_neg(basic s, const basic a); +int basic_neg(basic s, const basic a); //! Assigns s = abs(a). -void basic_abs(basic s, const basic a); +int basic_abs(basic s, const basic a); //! Assigns s = sin(a). -void basic_sin(basic s, const basic a); +int basic_sin(basic s, const basic a); //! Assigns s = cos(a). -void basic_cos(basic s, const basic a); +int basic_cos(basic s, const basic a); //! Assigns s = tan(a). -void basic_tan(basic s, const basic a); +int basic_tan(basic s, const basic a); //! Assigns s = asin(a). -void basic_asin(basic s, const basic a); +int basic_asin(basic s, const basic a); //! Assigns s = acos(a). -void basic_acos(basic s, const basic a); +int basic_acos(basic s, const basic a); //! Assigns s = atan(a). -void basic_atan(basic s, const basic a); +int basic_atan(basic s, const basic a); //! Assigns s = csc(a). -void basic_csc(basic s, const basic a); +int basic_csc(basic s, const basic a); //! Assigns s = sec(a). -void basic_sec(basic s, const basic a); +int basic_sec(basic s, const basic a); //! Assigns s = cot(a). -void basic_cot(basic s, const basic a); +int basic_cot(basic s, const basic a); //! Assigns s = acsc(a). -void basic_acsc(basic s, const basic a); +int basic_acsc(basic s, const basic a); //! Assigns s = asec(a). -void basic_asec(basic s, const basic a); +int basic_asec(basic s, const basic a); //! Assigns s = acot(a). -void basic_acot(basic s, const basic a); +int basic_acot(basic s, const basic a); //! Assigns s = sinh(a). -void basic_sinh(basic s, const basic a); +int basic_sinh(basic s, const basic a); //! Assigns s = cosh(a). -void basic_cosh(basic s, const basic a); +int basic_cosh(basic s, const basic a); //! Assigns s = tanh(a). -void basic_tanh(basic s, const basic a); +int basic_tanh(basic s, const basic a); //! Assigns s = asinh(a). -void basic_asinh(basic s, const basic a); +int basic_asinh(basic s, const basic a); //! Assigns s = acosh(a). -void basic_acosh(basic s, const basic a); +int basic_acosh(basic s, const basic a); //! Assigns s = atanh(a). -void basic_atanh(basic s, const basic a); +int basic_atanh(basic s, const basic a); //! Assigns s = csch(a). -void basic_csch(basic s, const basic a); +int basic_csch(basic s, const basic a); //! Assigns s = sech(a). -void basic_sech(basic s, const basic a); +int basic_sech(basic s, const basic a); //! Assigns s = coth(a). -void basic_coth(basic s, const basic a); +int basic_coth(basic s, const basic a); //! Assigns s = acsch(a). -void basic_acsch(basic s, const basic a); +int basic_acsch(basic s, const basic a); //! Assigns s = asech(a). -void basic_asech(basic s, const basic a); +int basic_asech(basic s, const basic a); //! Assigns s = acoth(a). -void basic_acoth(basic s, const basic a); +int basic_acoth(basic s, const basic a); //! Assigns s = lambertw(a). -void basic_lambertw(basic s, const basic a); +int basic_lambertw(basic s, const basic a); //! Assigns s = zeta(a). -void basic_zeta(basic s, const basic a); +int basic_zeta(basic s, const basic a); //! Assigns s = dirichlet_eta(a). -void basic_dirichlet_eta(basic s, const basic a); +int basic_dirichlet_eta(basic s, const basic a); //! Assigns s = gamma(a). -void basic_gamma(basic s, const basic a); +int basic_gamma(basic s, const basic a); //! Returns a new char pointer to the string representation of s. char *basic_str(const basic s); @@ -361,33 +361,33 @@ CDenseMatrix *dense_matrix_new_rows_cols(unsigned r, unsigned c); void sparse_matrix_free(CSparseMatrix *self); //! Assign to s, a DenseMatrix with value d -void dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d); +int dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d); //! Return a string representation of s char *dense_matrix_str(const CDenseMatrix *s); //! Resize mat to rxc -void dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c); +int dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c); //! Assign to s, mat[r][c] -void dense_matrix_get_basic(basic s, const CDenseMatrix *mat, +int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, unsigned long int r, unsigned long int c); //! Assign s to mat[r][c] -void dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, +int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, unsigned long int c, basic s); //! Assign to s, mat[r][c] -void sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, +int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, unsigned long int r, unsigned long int c); //! Assign s to mat[r][c] -void sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, +int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, unsigned long int c, basic s); //! Assign to s, determinent of mat -void dense_matrix_det(basic s, const CDenseMatrix *mat); +int dense_matrix_det(basic s, const CDenseMatrix *mat); //! Assign to s, a DenseMatrix which is the inverse of mat -void dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat); +int dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat); //! Assign to s, a DenseMatrix which is the transpose of mat -void dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat); +int dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat); //! Assign to s, a SubMatrix of mat, starting with [r1, r2] until [r2, c2], with //! step sizes [r, c] -void dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, +int dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, unsigned long int r1, unsigned long int c1, unsigned long int r2, unsigned long int c2, unsigned long int r, unsigned long int c); @@ -396,41 +396,41 @@ unsigned long int dense_matrix_cols(const CDenseMatrix *s); //! Return the number of rows of s unsigned long int dense_matrix_rows(const CDenseMatrix *s); //! Assign to s, the addition of matA and matB -void dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB); //! Assign to s, the matrix multiplication of matA and matB -void dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB); //! Assign to s, the addition of scalar b to matrix matA -void dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b); //! Assign to s, the multiplication of scalar b to matrix matA -void dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b); //! Assign to l and u, LU factorization of mat -void dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat); +int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat); //! Assign to l and d, LDL factorization of mat -void dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, +int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat); //! Assign to lu, fraction free LU factorization of mat -void dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat); +int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat); //! Assign to l, d and u, FFLDU factorization of mat -void dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, +int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, const CDenseMatrix *mat); //! Assign to x, solution to A x = b -void dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, +int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, const CDenseMatrix *b); //! Assign to s, a matrix of ones of size rxc -void dense_matrix_ones(CDenseMatrix *s, unsigned long int r, +int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, unsigned long int c); //! Assign to s, a matrix of zeros of size rxc -void dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, +int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, unsigned long int c); //! Assign to s, a diagonal matrix with a diagonal at offset k, with elements in //! d -void dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k); +int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k); //! Assign to s, a matrix of size NxM, with diagonal of 1s at offset k -void dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, +int dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, int k); //! Assign to s, a CSRMatrix @@ -480,21 +480,21 @@ size_t mapbasicbasic_size(CMapBasicBasic *self); // ------------------------------------- //! Returns a CVecBasic of vec_basic given by get_args -void basic_get_args(const basic self, CVecBasic *args); +int basic_get_args(const basic self, CVecBasic *args); //! Returns a CSetBasic of set_basic given by free_symbols -void basic_free_symbols(const basic self, CSetBasic *symbols); +int basic_free_symbols(const basic self, CSetBasic *symbols); //! returns the hash of the Basic object size_t basic_hash(const basic self); //! substitutes all the keys with their mapped values //! in the given basic `e` and returns it through basic 's' -void basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb); +int basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb); //! substitutes a basic 'a' with another basic 'b', //! in the given basic 'e' and returns it through basic 's' -void basic_subs2(basic s, const basic e, const basic a, const basic b); +int basic_subs2(basic s, const basic e, const basic a, const basic b); //! Assigns to s a FunctionSymbol with name described by c, with dependent //! symbols arg -void function_symbol_set(basic s, const char *c, const CVecBasic *arg); +int function_symbol_set(basic s, const char *c, const CVecBasic *arg); //! Wrapper for ascii_art() @@ -504,23 +504,23 @@ char *ascii_art_str(); //! Wrapper for ntheory //! Greatest Common Divisor -void ntheory_gcd(basic s, const basic a, const basic b); +int ntheory_gcd(basic s, const basic a, const basic b); //! Least Common Multiple -void ntheory_lcm(basic s, const basic a, const basic b); +int ntheory_lcm(basic s, const basic a, const basic b); //! \return next prime after `a` -void ntheory_nextprime(basic s, const basic a); +int ntheory_nextprime(basic s, const basic a); //! modulo round toward zero -void ntheory_mod(basic s, const basic n, const basic d); +int ntheory_mod(basic s, const basic n, const basic d); //! \return quotient round toward zero when `n` is divided by `d` -void ntheory_quotient(basic s, const basic n, const basic d); +int ntheory_quotient(basic s, const basic n, const basic d); //! nth Fibonacci number // fibonacci(0) = 0 and fibonacci(1) = 1 -void ntheory_fibonacci(basic s, unsigned long a); +int ntheory_fibonacci(basic s, unsigned long a); //! Lucas number -void ntheory_lucas(basic s, unsigned long a); +int ntheory_lucas(basic s, unsigned long a); //! Binomial Coefficient -void ntheory_binomial(basic s, const basic a, unsigned long b); +int 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); +int basic_evalf(basic s, const basic b, unsigned long bits, int real); //! Print stacktrace on segfault void symengine_print_stack_on_segfault(); From 8a6cd35c4dfc5ea9caf87946896ba19bb65e1890 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Mon, 25 Jul 2016 23:46:41 +0530 Subject: [PATCH 04/19] SymEngine Exception classes --- symengine/CMakeLists.txt | 1 + symengine/cwrapper.cpp | 143 +++++++++++++++--------------- symengine/integer.cpp | 3 +- symengine/mul.cpp | 3 +- symengine/symengine_exception.cpp | 17 ++++ symengine/symengine_exception.h | 16 ++++ 6 files changed, 111 insertions(+), 72 deletions(-) create mode 100644 symengine/symengine_exception.cpp create mode 100644 symengine/symengine_exception.h diff --git a/symengine/CMakeLists.txt b/symengine/CMakeLists.txt index a210506f1e..612808c14d 100644 --- a/symengine/CMakeLists.txt +++ b/symengine/CMakeLists.txt @@ -84,6 +84,7 @@ set(HEADERS 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 eval.h flint_wrapper.h polys/basic_conversions.h + symengine_exception.h ) # Configure SymEngine using our CMake options: diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index 176e8ab772..00e0cd9d52 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #define xstr(s) str(s) #define str(s) #s @@ -45,6 +46,7 @@ using SymEngine::diag; using SymEngine::ones; using SymEngine::zeros; using SymEngine::parse; +using SymEngine::DivisionByZero; namespace SymEngine { @@ -59,7 +61,7 @@ inline bool is_aligned(T *p, size_t n = alignof(T)) extern "C" { -#define CWRAPPER_BEGIN() \ +#define CWRAPPER_BEGIN \ try \ { @@ -191,42 +193,42 @@ TypeID basic_get_type(const basic s) int symbol_set(basic s, const char *c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::symbol(std::string(c)); CWRAPPER_END(...,-1); } int integer_set_si(basic s, long i) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); CWRAPPER_END(...,-1); } int integer_set_ui(basic s, unsigned long i) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); CWRAPPER_END(...,-1); } int integer_set_mpz(basic s, const mpz_t i) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); CWRAPPER_END(...,-1); } int integer_set_str(basic s, const char *c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(c)); CWRAPPER_END(...,-1); } int real_double_set_d(basic s, double d) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::real_double(d); CWRAPPER_END(...,-1); } @@ -241,7 +243,7 @@ double real_double_get_d(const basic s) int real_mpfr_set_d(basic s, double d, int prec) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN mpfr_class mc = mpfr_class(prec); mpfr_set_d(mc.get_mpfr_t(), d, MPFR_RNDN); s->m = SymEngine::real_mpfr(std::move(mc)); @@ -250,7 +252,7 @@ int real_mpfr_set_d(basic s, double d, int prec) int real_mpfr_set_str(basic s, const char *c, int prec) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::real_mpfr(mpfr_class(c, prec, 10)); CWRAPPER_END(...,-1); } @@ -265,14 +267,14 @@ double real_mpfr_get_d(const basic s) int real_mpfr_set(basic s, mpfr_srcptr m) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::real_mpfr(mpfr_class(m)); CWRAPPER_END(...,-1); } int real_mpfr_get(mpfr_ptr m, const basic s) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(s->m))); mpfr_set(m, ((rcp_static_cast(s->m))->as_mpfr()).get_mpfr_t(), @@ -302,7 +304,7 @@ int complex_mpc_is_zero(const basic s) int complex_mpc_real_part(basic s, const basic com) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); CWRAPPER_END(...,-1); @@ -310,7 +312,7 @@ int complex_mpc_real_part(basic s, const basic com) int complex_mpc_imaginary_part(basic s, const basic com) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); CWRAPPER_END(...,-1); @@ -331,7 +333,7 @@ unsigned long integer_get_ui(const basic s) int integer_get_mpz(mpz_t a, const basic s) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(s->m))); mpz_set(a, get_mpz_t((rcp_static_cast(s->m))->as_mpz())); CWRAPPER_END(...,-1); @@ -339,14 +341,14 @@ int integer_get_mpz(mpz_t a, const basic s) int rational_set_si(basic s, long a, long b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); CWRAPPER_END(...,-1); } int rational_set_ui(basic s, unsigned long a, unsigned long b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); CWRAPPER_END(...,-1); } @@ -364,14 +366,14 @@ int rational_set(basic s, const basic a, const basic b) int rational_set_mpq(basic s, const mpq_t i) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(i)); CWRAPPER_END(...,-1); } int complex_set(basic s, const basic re, const basic im) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::Complex::from_two_nums( *(rcp_static_cast(re->m)), *(rcp_static_cast(im->m))); @@ -380,7 +382,7 @@ int complex_set(basic s, const basic re, const basic im) int complex_set_rat(basic s, const basic re, const basic im) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::Complex::from_two_rats( *(rcp_static_cast(re->m)), *(rcp_static_cast(im->m))); @@ -389,14 +391,14 @@ int complex_set_rat(basic s, const basic re, const basic im) int complex_set_mpq(basic s, const mpq_t re, const mpq_t im) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::Complex::from_mpq(rational_class(re), rational_class(im)); CWRAPPER_END(...,-1); } int complex_real_part(basic s, const basic com) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); CWRAPPER_END(...,-1); @@ -404,7 +406,7 @@ int complex_real_part(basic s, const basic com) int complex_imaginary_part(basic s, const basic com) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); CWRAPPER_END(...,-1); @@ -412,7 +414,7 @@ int complex_imaginary_part(basic s, const basic com) int complex_double_real_part(basic s, const basic com) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); CWRAPPER_END(...,-1); @@ -420,7 +422,7 @@ int complex_double_real_part(basic s, const basic com) int complex_double_imaginary_part(basic s, const basic com) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); CWRAPPER_END(...,-1); @@ -436,51 +438,52 @@ int basic_diff(basic s, const basic expr, basic const symbol) int basic_assign(basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN a->m = b->m; CWRAPPER_END(...,-1); } int basic_parse(basic b, const char *str) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN b->m = SymEngine::parse(str); CWRAPPER_END(...,-1); } int basic_add(basic s, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::add(a->m, b->m); CWRAPPER_END(...,-1); } int basic_sub(basic s, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::sub(a->m, b->m); CWRAPPER_END(...,-1); } int basic_mul(basic s, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::mul(a->m, b->m); CWRAPPER_END(...,-1); } int basic_pow(basic s, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::pow(a->m, b->m); CWRAPPER_END(...,-1); } int basic_div(basic s, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::div(a->m, b->m); - CWRAPPER_END(...,-1); + CWRAPPER_END(DivisionByZero,1) + CWRAPPER_END_EXTRA(...,-1) } int basic_eq(const basic a, const basic b) @@ -510,7 +513,7 @@ int basic_number_sign(const basic s) #define IMPLEMENT_ONE_ARG_FUNC(func) \ int basic_##func(basic s, const basic a) \ { \ - CWRAPPER_BEGIN(); \ + CWRAPPER_BEGIN \ s->m = SymEngine::func(a->m); \ CWRAPPER_END(...,-1); \ } @@ -741,7 +744,7 @@ void sparse_matrix_rows_cols(CSparseMatrix *s, unsigned long int rows, int dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = d->m; CWRAPPER_END(...,-1); } @@ -764,7 +767,7 @@ char *sparse_matrix_str(const CSparseMatrix *s) int dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN mat->m.resize(r, c); CWRAPPER_END(...,-1); } @@ -772,7 +775,7 @@ int dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, unsigned long int r, unsigned long int c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = mat->m.get(r, c); CWRAPPER_END(...,-1); } @@ -780,7 +783,7 @@ int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, unsigned long int c, basic s) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN mat->m.set(r, c, s->m); CWRAPPER_END(...,-1); } @@ -788,7 +791,7 @@ int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, unsigned long int r, unsigned long int c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = mat->m.get(r, c); CWRAPPER_END(...,-1); } @@ -796,27 +799,27 @@ int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, unsigned long int c, basic s) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN mat->m.set(r, c, s->m); CWRAPPER_END(...,-1); } int dense_matrix_det(basic s, const CDenseMatrix *mat) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = mat->m.det(); CWRAPPER_END(...,-1); } int dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, mat->m.nrows(), mat->m.ncols()); mat->m.inv(s->m); CWRAPPER_END(...,-1); } int dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows()); mat->m.transpose(s->m); CWRAPPER_END(...,-1); @@ -826,7 +829,7 @@ int dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, unsigned long int r2, unsigned long int c2, unsigned long int r, unsigned long int c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, r2 - r1 + 1, c2 - c1 + 1); mat->m.submatrix(s->m, r1, c1, r2, c2, r, c); CWRAPPER_END(...,-1); @@ -845,7 +848,7 @@ unsigned long int dense_matrix_cols(const CDenseMatrix *s) int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.add_matrix(matB->m, s->m); CWRAPPER_END(...,-1); @@ -854,7 +857,7 @@ int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matB->m.ncols()); matA->m.mul_matrix(matB->m, s->m); CWRAPPER_END(...,-1); @@ -863,7 +866,7 @@ int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.add_scalar(b->m, s->m); CWRAPPER_END(...,-1); @@ -872,7 +875,7 @@ int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.mul_scalar(b->m, s->m); CWRAPPER_END(...,-1); @@ -880,7 +883,7 @@ int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols()); mat->m.LU(l->m, u->m); @@ -889,7 +892,7 @@ int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols()); mat->m.LDL(l->m, d->m); @@ -898,7 +901,7 @@ int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols()); mat->m.FFLU(lu->m); CWRAPPER_END(...,-1); @@ -907,7 +910,7 @@ int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, const CDenseMatrix *mat) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols()); @@ -918,7 +921,7 @@ int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, const CDenseMatrix *b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(x, A->m.ncols(), 1); A->m.LU_solve(b->m, x->m); CWRAPPER_END(...,-1); @@ -927,7 +930,7 @@ int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, unsigned long int c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, r, c); ones(s->m); CWRAPPER_END(...,-1); @@ -936,14 +939,14 @@ int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, unsigned long int c) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, r, c); zeros(s->m); CWRAPPER_END(...,-1); } int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN int vec_size = vecbasic_size(d); dense_matrix_rows_cols(s, vec_size + (k >= 0 ? k : -k), vec_size + (k >= 0 ? k : -k)); @@ -954,7 +957,7 @@ int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) int dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, int k) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN dense_matrix_rows_cols(s, N, M); eye(s->m, k); CWRAPPER_END(...,-1); @@ -1057,14 +1060,14 @@ size_t mapbasicbasic_size(CMapBasicBasic *self) int basic_get_args(const basic self, CVecBasic *args) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN args->m = self->m->get_args(); CWRAPPER_END(...,-1); } int basic_free_symbols(const basic self, CSetBasic *symbols) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN symbols->m = SymEngine::free_symbols(*(self->m)); CWRAPPER_END(...,-1); } @@ -1076,21 +1079,21 @@ size_t basic_hash(const basic self) int basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = e->m->subs(mapbb->m); CWRAPPER_END(...,-1); } int basic_subs2(basic s, const basic e, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = e->m->subs({{a->m, b->m}}); CWRAPPER_END(...,-1); } int function_symbol_set(basic s, const char *c, const CVecBasic *arg) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = function_symbol(c, arg->m); CWRAPPER_END(...,-1); } @@ -1109,7 +1112,7 @@ char *ascii_art_str() int ntheory_gcd(basic s, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); SYMENGINE_ASSERT(is_a(*(b->m))); s->m = SymEngine::gcd(static_cast(*(a->m)), @@ -1119,7 +1122,7 @@ int ntheory_gcd(basic s, const basic a, const basic b) int ntheory_lcm(basic s, const basic a, const basic b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); SYMENGINE_ASSERT(is_a(*(b->m))); s->m = SymEngine::lcm(static_cast(*(a->m)), @@ -1129,7 +1132,7 @@ int ntheory_lcm(basic s, const basic a, const basic b) int ntheory_nextprime(basic s, const basic a) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); s->m = SymEngine::nextprime(static_cast(*(a->m))); CWRAPPER_END(...,-1); @@ -1137,7 +1140,7 @@ int ntheory_nextprime(basic s, const basic a) int ntheory_mod(basic s, const basic n, const basic d) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(n->m))); SYMENGINE_ASSERT(is_a(*(d->m))); s->m = SymEngine::mod(static_cast(*(n->m)), @@ -1147,7 +1150,7 @@ int ntheory_mod(basic s, const basic n, const basic d) int ntheory_quotient(basic s, const basic n, const basic d) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(n->m))); SYMENGINE_ASSERT(is_a(*(d->m))); s->m = SymEngine::quotient(static_cast(*(n->m)), @@ -1157,21 +1160,21 @@ int ntheory_quotient(basic s, const basic n, const basic d) int ntheory_fibonacci(basic s, unsigned long a) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::fibonacci(a); CWRAPPER_END(...,-1); } int ntheory_lucas(basic s, unsigned long a) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::lucas(a); CWRAPPER_END(...,-1); } int ntheory_binomial(basic s, const basic a, unsigned long b) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); s->m = SymEngine::binomial(static_cast(*(a->m)), b); CWRAPPER_END(...,-1); @@ -1181,7 +1184,7 @@ int ntheory_binomial(basic s, const basic a, unsigned long b) int basic_evalf(basic s, const basic b, unsigned long bits, int real) { - CWRAPPER_BEGIN(); + CWRAPPER_BEGIN s->m = SymEngine::evalf(*(b->m), bits, (bool)real); CWRAPPER_END(...,-1); } diff --git a/symengine/integer.cpp b/symengine/integer.cpp index 0793142660..bc5d1a42f6 100644 --- a/symengine/integer.cpp +++ b/symengine/integer.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace SymEngine { @@ -44,7 +45,7 @@ signed long int Integer::as_int() const RCP Integer::divint(const Integer &other) const { if (other.i == 0) - throw std::runtime_error("Rational: Division by zero."); + throw DivisionByZero(); rational_class q(this->i, other.i); // This is potentially slow, but has to be done, since q might not diff --git a/symengine/mul.cpp b/symengine/mul.cpp index 50d5e6d2e8..8cead552c4 100644 --- a/symengine/mul.cpp +++ b/symengine/mul.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace SymEngine { @@ -422,7 +423,7 @@ RCP mul(const vec_basic &a) RCP div(const RCP &a, const RCP &b) { if (is_a_Number(*b) and rcp_static_cast(b)->is_zero()) - throw std::runtime_error("div: Division by zero"); + throw DivisionByZero(); return mul(a, pow(b, minus_one)); } diff --git a/symengine/symengine_exception.cpp b/symengine/symengine_exception.cpp new file mode 100644 index 0000000000..8c5558474f --- /dev/null +++ b/symengine/symengine_exception.cpp @@ -0,0 +1,17 @@ +#include +#include + +using namespace std; + +namespace symengine +{ + +class DivisionByZero: public exception +{ + virtual const char* what() const throw() + { + return "Division By Zero"; + } +}; + +} diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h new file mode 100644 index 0000000000..7c26e0e330 --- /dev/null +++ b/symengine/symengine_exception.h @@ -0,0 +1,16 @@ +#ifndef SYMENGINE_EXCEPTION_H +#define SYMENGINE_EXCEPTION_H + +namespace SymEngine +{ + +class DivisionByZero: public std::exception +{ + virtual const char* what() const throw() + { + return "Division By Zero"; + } +}; + +} +#endif //SYMENGINE_EXCEPTION_H From 8a37dc96dd0183d0d9c832a7854c3a0d116af3e5 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Mon, 25 Jul 2016 23:59:09 +0530 Subject: [PATCH 05/19] Removing CWRAPPER_END_EXTRA --- symengine/cwrapper.cpp | 153 ++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 78 deletions(-) diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index 00e0cd9d52..16ee04a422 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -65,18 +65,16 @@ extern "C" { try \ { -#define CWRAPPER_END(error, code) \ +#define CWRAPPER_END \ return 0; \ } \ - catch ( error ) \ + catch ( DivisionByZero ) \ { \ - return code; \ - } - -#define CWRAPPER_END_EXTRA(error, code) \ - catch ( error ) \ - { \ - return code; \ + return 1; \ + } \ + catch ( ... ) \ + { \ + return -1; \ } struct CRCPBasic { @@ -195,42 +193,42 @@ int symbol_set(basic s, const char *c) { CWRAPPER_BEGIN s->m = SymEngine::symbol(std::string(c)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int integer_set_si(basic s, long i) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int integer_set_ui(basic s, unsigned long i) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int integer_set_mpz(basic s, const mpz_t i) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int integer_set_str(basic s, const char *c) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(c)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int real_double_set_d(basic s, double d) { CWRAPPER_BEGIN s->m = SymEngine::real_double(d); - CWRAPPER_END(...,-1); + CWRAPPER_END } double real_double_get_d(const basic s) @@ -247,14 +245,14 @@ int real_mpfr_set_d(basic s, double d, int prec) mpfr_class mc = mpfr_class(prec); mpfr_set_d(mc.get_mpfr_t(), d, MPFR_RNDN); s->m = SymEngine::real_mpfr(std::move(mc)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int real_mpfr_set_str(basic s, const char *c, int prec) { CWRAPPER_BEGIN s->m = SymEngine::real_mpfr(mpfr_class(c, prec, 10)); - CWRAPPER_END(...,-1); + CWRAPPER_END } double real_mpfr_get_d(const basic s) @@ -269,7 +267,7 @@ int real_mpfr_set(basic s, mpfr_srcptr m) { CWRAPPER_BEGIN s->m = SymEngine::real_mpfr(mpfr_class(m)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int real_mpfr_get(mpfr_ptr m, const basic s) @@ -279,7 +277,7 @@ int real_mpfr_get(mpfr_ptr m, const basic s) mpfr_set(m, ((rcp_static_cast(s->m))->as_mpfr()).get_mpfr_t(), MPFR_RNDN); - CWRAPPER_END(...,-1); + CWRAPPER_END } mpfr_prec_t real_mpfr_get_prec(const basic s) @@ -307,7 +305,7 @@ int complex_mpc_real_part(basic s, const basic com) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_mpc_imaginary_part(basic s, const basic com) @@ -315,7 +313,7 @@ int complex_mpc_imaginary_part(basic s, const basic com) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); - CWRAPPER_END(...,-1); + CWRAPPER_END } #endif // HAVE_SYMENGINE_MPC @@ -336,21 +334,21 @@ int integer_get_mpz(mpz_t a, const basic s) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(s->m))); mpz_set(a, get_mpz_t((rcp_static_cast(s->m))->as_mpz())); - CWRAPPER_END(...,-1); + CWRAPPER_END } int rational_set_si(basic s, long a, long b) { CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int rational_set_ui(basic s, unsigned long a, unsigned long b) { CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int rational_set(basic s, const basic a, const basic b) @@ -368,7 +366,7 @@ int rational_set_mpq(basic s, const mpq_t i) { CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(i)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_set(basic s, const basic re, const basic im) @@ -377,7 +375,7 @@ int complex_set(basic s, const basic re, const basic im) s->m = SymEngine::Complex::from_two_nums( *(rcp_static_cast(re->m)), *(rcp_static_cast(im->m))); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_set_rat(basic s, const basic re, const basic im) @@ -386,14 +384,14 @@ int complex_set_rat(basic s, const basic re, const basic im) s->m = SymEngine::Complex::from_two_rats( *(rcp_static_cast(re->m)), *(rcp_static_cast(im->m))); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_set_mpq(basic s, const mpq_t re, const mpq_t im) { CWRAPPER_BEGIN s->m = SymEngine::Complex::from_mpq(rational_class(re), rational_class(im)); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_real_part(basic s, const basic com) @@ -401,7 +399,7 @@ int complex_real_part(basic s, const basic com) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_imaginary_part(basic s, const basic com) @@ -409,7 +407,7 @@ int complex_imaginary_part(basic s, const basic com) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_double_real_part(basic s, const basic com) @@ -417,7 +415,7 @@ int complex_double_real_part(basic s, const basic com) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->real_part(); - CWRAPPER_END(...,-1); + CWRAPPER_END } int complex_double_imaginary_part(basic s, const basic com) @@ -425,7 +423,7 @@ int complex_double_imaginary_part(basic s, const basic com) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); s->m = (rcp_static_cast(com->m))->imaginary_part(); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_diff(basic s, const basic expr, basic const symbol) @@ -440,50 +438,49 @@ int basic_assign(basic a, const basic b) { CWRAPPER_BEGIN a->m = b->m; - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_parse(basic b, const char *str) { CWRAPPER_BEGIN b->m = SymEngine::parse(str); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_add(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::add(a->m, b->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_sub(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::sub(a->m, b->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_mul(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::mul(a->m, b->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_pow(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::pow(a->m, b->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_div(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::div(a->m, b->m); - CWRAPPER_END(DivisionByZero,1) - CWRAPPER_END_EXTRA(...,-1) + CWRAPPER_END } int basic_eq(const basic a, const basic b) @@ -515,7 +512,7 @@ int basic_number_sign(const basic s) { \ CWRAPPER_BEGIN \ s->m = SymEngine::func(a->m); \ - CWRAPPER_END(...,-1); \ + CWRAPPER_END \ } IMPLEMENT_ONE_ARG_FUNC(expand); @@ -746,7 +743,7 @@ int dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d) { CWRAPPER_BEGIN s->m = d->m; - CWRAPPER_END(...,-1); + CWRAPPER_END } char *dense_matrix_str(const CDenseMatrix *s) @@ -769,7 +766,7 @@ int dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) { CWRAPPER_BEGIN mat->m.resize(r, c); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, @@ -777,7 +774,7 @@ int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, { CWRAPPER_BEGIN s->m = mat->m.get(r, c); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, @@ -785,7 +782,7 @@ int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, { CWRAPPER_BEGIN mat->m.set(r, c, s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, @@ -793,7 +790,7 @@ int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, { CWRAPPER_BEGIN s->m = mat->m.get(r, c); - CWRAPPER_END(...,-1); + CWRAPPER_END } int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, @@ -801,28 +798,28 @@ int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, { CWRAPPER_BEGIN mat->m.set(r, c, s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_det(basic s, const CDenseMatrix *mat) { CWRAPPER_BEGIN s->m = mat->m.det(); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, mat->m.nrows(), mat->m.ncols()); mat->m.inv(s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows()); mat->m.transpose(s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, unsigned long int r1, unsigned long int c1, @@ -832,7 +829,7 @@ int dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, CWRAPPER_BEGIN dense_matrix_rows_cols(s, r2 - r1 + 1, c2 - c1 + 1); mat->m.submatrix(s->m, r1, c1, r2, c2, r, c); - CWRAPPER_END(...,-1); + CWRAPPER_END } unsigned long int dense_matrix_rows(const CDenseMatrix *s) @@ -851,7 +848,7 @@ int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.add_matrix(matB->m, s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, @@ -860,7 +857,7 @@ int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matB->m.ncols()); matA->m.mul_matrix(matB->m, s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, @@ -869,7 +866,7 @@ int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.add_scalar(b->m, s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, @@ -878,7 +875,7 @@ int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); matA->m.mul_scalar(b->m, s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) @@ -887,7 +884,7 @@ int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols()); mat->m.LU(l->m, u->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) @@ -896,7 +893,7 @@ int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols()); mat->m.LDL(l->m, d->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) @@ -904,7 +901,7 @@ int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) CWRAPPER_BEGIN dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols()); mat->m.FFLU(lu->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, @@ -915,7 +912,7 @@ int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols()); dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols()); mat->m.FFLDU(l->m, d->m, u->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, @@ -924,7 +921,7 @@ int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, CWRAPPER_BEGIN dense_matrix_rows_cols(x, A->m.ncols(), 1); A->m.LU_solve(b->m, x->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, @@ -933,7 +930,7 @@ int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, CWRAPPER_BEGIN dense_matrix_rows_cols(s, r, c); ones(s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, @@ -942,7 +939,7 @@ int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, CWRAPPER_BEGIN dense_matrix_rows_cols(s, r, c); zeros(s->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) { @@ -951,7 +948,7 @@ int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) dense_matrix_rows_cols(s, vec_size + (k >= 0 ? k : -k), vec_size + (k >= 0 ? k : -k)); diag(s->m, d->m, k); - CWRAPPER_END(...,-1); + CWRAPPER_END } int dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, @@ -960,7 +957,7 @@ int dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, CWRAPPER_BEGIN dense_matrix_rows_cols(s, N, M); eye(s->m, k); - CWRAPPER_END(...,-1); + CWRAPPER_END } int is_a_DenseMatrix(const CDenseMatrix *c) @@ -1062,14 +1059,14 @@ int basic_get_args(const basic self, CVecBasic *args) { CWRAPPER_BEGIN args->m = self->m->get_args(); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_free_symbols(const basic self, CSetBasic *symbols) { CWRAPPER_BEGIN symbols->m = SymEngine::free_symbols(*(self->m)); - CWRAPPER_END(...,-1); + CWRAPPER_END } size_t basic_hash(const basic self) @@ -1081,21 +1078,21 @@ int basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb) { CWRAPPER_BEGIN s->m = e->m->subs(mapbb->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } int basic_subs2(basic s, const basic e, const basic a, const basic b) { CWRAPPER_BEGIN s->m = e->m->subs({{a->m, b->m}}); - CWRAPPER_END(...,-1); + CWRAPPER_END } int function_symbol_set(basic s, const char *c, const CVecBasic *arg) { CWRAPPER_BEGIN s->m = function_symbol(c, arg->m); - CWRAPPER_END(...,-1); + CWRAPPER_END } // ---------------------- @@ -1117,7 +1114,7 @@ int ntheory_gcd(basic s, const basic a, const basic b) SYMENGINE_ASSERT(is_a(*(b->m))); s->m = SymEngine::gcd(static_cast(*(a->m)), static_cast(*(b->m))); - CWRAPPER_END(...,-1); + CWRAPPER_END } int ntheory_lcm(basic s, const basic a, const basic b) @@ -1127,7 +1124,7 @@ int ntheory_lcm(basic s, const basic a, const basic b) SYMENGINE_ASSERT(is_a(*(b->m))); s->m = SymEngine::lcm(static_cast(*(a->m)), static_cast(*(b->m))); - CWRAPPER_END(...,-1); + CWRAPPER_END } int ntheory_nextprime(basic s, const basic a) @@ -1135,7 +1132,7 @@ int ntheory_nextprime(basic s, const basic a) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); s->m = SymEngine::nextprime(static_cast(*(a->m))); - CWRAPPER_END(...,-1); + CWRAPPER_END } int ntheory_mod(basic s, const basic n, const basic d) @@ -1145,7 +1142,7 @@ int ntheory_mod(basic s, const basic n, const basic d) SYMENGINE_ASSERT(is_a(*(d->m))); s->m = SymEngine::mod(static_cast(*(n->m)), static_cast(*(d->m))); - CWRAPPER_END(...,-1); + CWRAPPER_END } int ntheory_quotient(basic s, const basic n, const basic d) @@ -1155,21 +1152,21 @@ int ntheory_quotient(basic s, const basic n, const basic d) SYMENGINE_ASSERT(is_a(*(d->m))); s->m = SymEngine::quotient(static_cast(*(n->m)), static_cast(*(d->m))); - CWRAPPER_END(...,-1); + CWRAPPER_END } int ntheory_fibonacci(basic s, unsigned long a) { CWRAPPER_BEGIN s->m = SymEngine::fibonacci(a); - CWRAPPER_END(...,-1); + CWRAPPER_END } int ntheory_lucas(basic s, unsigned long a) { CWRAPPER_BEGIN s->m = SymEngine::lucas(a); - CWRAPPER_END(...,-1); + CWRAPPER_END } int ntheory_binomial(basic s, const basic a, unsigned long b) @@ -1177,7 +1174,7 @@ int ntheory_binomial(basic s, const basic a, unsigned long b) CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); s->m = SymEngine::binomial(static_cast(*(a->m)), b); - CWRAPPER_END(...,-1); + CWRAPPER_END } //! Wrapper for evalf @@ -1186,7 +1183,7 @@ int basic_evalf(basic s, const basic b, unsigned long bits, int real) CWRAPPER_BEGIN s->m = SymEngine::evalf(*(b->m), bits, (bool)real); - CWRAPPER_END(...,-1); + CWRAPPER_END } //! Print stacktrace on segfault From fe0086cc971e787622894013e1d5b1893311faaa Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 28 Jul 2016 00:33:07 +0530 Subject: [PATCH 06/19] CWRAPPER_OUTPUT_TYPE implemented --- symengine/cwrapper.cpp | 158 +++++++++++------------ symengine/cwrapper.h | 200 +++++++++++++++--------------- symengine/symengine_exception.cpp | 17 --- 3 files changed, 180 insertions(+), 195 deletions(-) delete mode 100644 symengine/symengine_exception.cpp diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index 16ee04a422..75b3618e68 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -62,19 +62,19 @@ extern "C" { #define CWRAPPER_BEGIN \ - try \ + try \ { -#define CWRAPPER_END \ - return 0; \ - } \ - catch ( DivisionByZero ) \ - { \ - return 1; \ - } \ - catch ( ... ) \ - { \ - return -1; \ +#define CWRAPPER_END \ + return 0; \ + } \ + catch ( DivisionByZero ) \ + { \ + return 1; \ + } \ + catch ( ... ) \ + { \ + return -1; \ } struct CRCPBasic { @@ -189,42 +189,42 @@ TypeID basic_get_type(const basic s) return static_cast(s->m->get_type_code()); } -int symbol_set(basic s, const char *c) +CWRAPPER_OUTPUT_TYPE symbol_set(basic s, const char *c) { CWRAPPER_BEGIN s->m = SymEngine::symbol(std::string(c)); CWRAPPER_END } -int integer_set_si(basic s, long i) +CWRAPPER_OUTPUT_TYPE integer_set_si(basic s, long i) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); CWRAPPER_END } -int integer_set_ui(basic s, unsigned long i) +CWRAPPER_OUTPUT_TYPE integer_set_ui(basic s, unsigned long i) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); CWRAPPER_END } -int integer_set_mpz(basic s, const mpz_t i) +CWRAPPER_OUTPUT_TYPE integer_set_mpz(basic s, const mpz_t i) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(i)); CWRAPPER_END } -int integer_set_str(basic s, const char *c) +CWRAPPER_OUTPUT_TYPE integer_set_str(basic s, const char *c) { CWRAPPER_BEGIN s->m = SymEngine::integer(integer_class(c)); CWRAPPER_END } -int real_double_set_d(basic s, double d) +CWRAPPER_OUTPUT_TYPE real_double_set_d(basic s, double d) { CWRAPPER_BEGIN s->m = SymEngine::real_double(d); @@ -239,7 +239,7 @@ double real_double_get_d(const basic s) #ifdef HAVE_SYMENGINE_MPFR -int real_mpfr_set_d(basic s, double d, int prec) +CWRAPPER_OUTPUT_TYPE real_mpfr_set_d(basic s, double d, int prec) { CWRAPPER_BEGIN mpfr_class mc = mpfr_class(prec); @@ -248,7 +248,7 @@ int real_mpfr_set_d(basic s, double d, int prec) CWRAPPER_END } -int real_mpfr_set_str(basic s, const char *c, int prec) +CWRAPPER_OUTPUT_TYPE real_mpfr_set_str(basic s, const char *c, int prec) { CWRAPPER_BEGIN s->m = SymEngine::real_mpfr(mpfr_class(c, prec, 10)); @@ -263,14 +263,14 @@ double real_mpfr_get_d(const basic s) MPFR_RNDN); } -int real_mpfr_set(basic s, mpfr_srcptr m) +CWRAPPER_OUTPUT_TYPE real_mpfr_set(basic s, mpfr_srcptr m) { CWRAPPER_BEGIN s->m = SymEngine::real_mpfr(mpfr_class(m)); CWRAPPER_END } -int real_mpfr_get(mpfr_ptr m, const basic s) +CWRAPPER_OUTPUT_TYPE real_mpfr_get(mpfr_ptr m, const basic s) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(s->m))); @@ -300,7 +300,7 @@ int complex_mpc_is_zero(const basic s) return (int)((rcp_static_cast(s->m))->is_zero()); } -int complex_mpc_real_part(basic s, const basic com) +CWRAPPER_OUTPUT_TYPE complex_mpc_real_part(basic s, const basic com) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); @@ -308,7 +308,7 @@ int complex_mpc_real_part(basic s, const basic com) CWRAPPER_END } -int complex_mpc_imaginary_part(basic s, const basic com) +CWRAPPER_OUTPUT_TYPE complex_mpc_imaginary_part(basic s, const basic com) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); @@ -329,7 +329,7 @@ unsigned long integer_get_ui(const basic s) return mp_get_ui((rcp_static_cast(s->m))->as_mpz()); } -int integer_get_mpz(mpz_t a, const basic s) +CWRAPPER_OUTPUT_TYPE integer_get_mpz(mpz_t a, const basic s) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(s->m))); @@ -337,14 +337,14 @@ int integer_get_mpz(mpz_t a, const basic s) CWRAPPER_END } -int rational_set_si(basic s, long a, long b) +CWRAPPER_OUTPUT_TYPE rational_set_si(basic s, long a, long b) { CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); CWRAPPER_END } -int rational_set_ui(basic s, unsigned long a, unsigned long b) +CWRAPPER_OUTPUT_TYPE rational_set_ui(basic s, unsigned long a, unsigned long b) { CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(a, b)); @@ -362,14 +362,14 @@ int rational_set(basic s, const basic a, const basic b) return 1; } -int rational_set_mpq(basic s, const mpq_t i) +CWRAPPER_OUTPUT_TYPE rational_set_mpq(basic s, const mpq_t i) { CWRAPPER_BEGIN s->m = SymEngine::Rational::from_mpq(rational_class(i)); CWRAPPER_END } -int complex_set(basic s, const basic re, const basic im) +CWRAPPER_OUTPUT_TYPE complex_set(basic s, const basic re, const basic im) { CWRAPPER_BEGIN s->m = SymEngine::Complex::from_two_nums( @@ -378,7 +378,7 @@ int complex_set(basic s, const basic re, const basic im) CWRAPPER_END } -int complex_set_rat(basic s, const basic re, const basic im) +CWRAPPER_OUTPUT_TYPE complex_set_rat(basic s, const basic re, const basic im) { CWRAPPER_BEGIN s->m = SymEngine::Complex::from_two_rats( @@ -387,14 +387,14 @@ int complex_set_rat(basic s, const basic re, const basic im) CWRAPPER_END } -int complex_set_mpq(basic s, const mpq_t re, const mpq_t im) +CWRAPPER_OUTPUT_TYPE complex_set_mpq(basic s, const mpq_t re, const mpq_t im) { CWRAPPER_BEGIN s->m = SymEngine::Complex::from_mpq(rational_class(re), rational_class(im)); CWRAPPER_END } -int complex_real_part(basic s, const basic com) +CWRAPPER_OUTPUT_TYPE complex_real_part(basic s, const basic com) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); @@ -402,7 +402,7 @@ int complex_real_part(basic s, const basic com) CWRAPPER_END } -int complex_imaginary_part(basic s, const basic com) +CWRAPPER_OUTPUT_TYPE complex_imaginary_part(basic s, const basic com) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); @@ -410,7 +410,7 @@ int complex_imaginary_part(basic s, const basic com) CWRAPPER_END } -int complex_double_real_part(basic s, const basic com) +CWRAPPER_OUTPUT_TYPE complex_double_real_part(basic s, const basic com) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); @@ -418,7 +418,7 @@ int complex_double_real_part(basic s, const basic com) CWRAPPER_END } -int complex_double_imaginary_part(basic s, const basic com) +CWRAPPER_OUTPUT_TYPE complex_double_imaginary_part(basic s, const basic com) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(com->m))); @@ -434,49 +434,49 @@ int basic_diff(basic s, const basic expr, basic const symbol) return 1; } -int basic_assign(basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_assign(basic a, const basic b) { CWRAPPER_BEGIN a->m = b->m; CWRAPPER_END } -int basic_parse(basic b, const char *str) +CWRAPPER_OUTPUT_TYPE basic_parse(basic b, const char *str) { CWRAPPER_BEGIN b->m = SymEngine::parse(str); CWRAPPER_END } -int basic_add(basic s, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_add(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::add(a->m, b->m); CWRAPPER_END } -int basic_sub(basic s, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_sub(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::sub(a->m, b->m); CWRAPPER_END } -int basic_mul(basic s, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_mul(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::mul(a->m, b->m); CWRAPPER_END } -int basic_pow(basic s, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_pow(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::pow(a->m, b->m); CWRAPPER_END } -int basic_div(basic s, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_div(basic s, const basic a, const basic b) { CWRAPPER_BEGIN s->m = SymEngine::div(a->m, b->m); @@ -508,7 +508,7 @@ int basic_number_sign(const basic s) } #define IMPLEMENT_ONE_ARG_FUNC(func) \ - int basic_##func(basic s, const basic a) \ + CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a) \ { \ CWRAPPER_BEGIN \ s->m = SymEngine::func(a->m); \ @@ -739,7 +739,7 @@ void sparse_matrix_rows_cols(CSparseMatrix *s, unsigned long int rows, s->m = SymEngine::CSRMatrix(rows, cols); } -int dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d) +CWRAPPER_OUTPUT_TYPE dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d) { CWRAPPER_BEGIN s->m = d->m; @@ -762,14 +762,14 @@ char *sparse_matrix_str(const CSparseMatrix *s) return cc; } -int dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) +CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) { CWRAPPER_BEGIN mat->m.resize(r, c); CWRAPPER_END } -int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, +CWRAPPER_OUTPUT_TYPE dense_matrix_get_basic(basic s, const CDenseMatrix *mat, unsigned long int r, unsigned long int c) { CWRAPPER_BEGIN @@ -777,7 +777,7 @@ int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, CWRAPPER_END } -int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, +CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, unsigned long int c, basic s) { CWRAPPER_BEGIN @@ -785,7 +785,7 @@ int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, CWRAPPER_END } -int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, +CWRAPPER_OUTPUT_TYPE sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, unsigned long int r, unsigned long int c) { CWRAPPER_BEGIN @@ -793,7 +793,7 @@ int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, CWRAPPER_END } -int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, +CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, unsigned long int c, basic s) { CWRAPPER_BEGIN @@ -801,27 +801,27 @@ int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, CWRAPPER_END } -int dense_matrix_det(basic s, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_det(basic s, const CDenseMatrix *mat) { CWRAPPER_BEGIN s->m = mat->m.det(); CWRAPPER_END } -int dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, mat->m.nrows(), mat->m.ncols()); mat->m.inv(s->m); CWRAPPER_END } -int dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows()); mat->m.transpose(s->m); CWRAPPER_END } -int dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, +CWRAPPER_OUTPUT_TYPE dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, unsigned long int r1, unsigned long int c1, unsigned long int r2, unsigned long int c2, unsigned long int r, unsigned long int c) @@ -842,7 +842,7 @@ unsigned long int dense_matrix_cols(const CDenseMatrix *s) return s->m.ncols(); } -int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB) { CWRAPPER_BEGIN @@ -851,7 +851,7 @@ int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_END } -int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB) { CWRAPPER_BEGIN @@ -860,7 +860,7 @@ int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_END } -int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b) { CWRAPPER_BEGIN @@ -869,7 +869,7 @@ int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_END } -int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b) { CWRAPPER_BEGIN @@ -878,7 +878,7 @@ int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, CWRAPPER_END } -int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); @@ -887,7 +887,7 @@ int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) CWRAPPER_END } -int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); @@ -896,7 +896,7 @@ int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) CWRAPPER_END } -int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols()); @@ -904,7 +904,7 @@ int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) CWRAPPER_END } -int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, const CDenseMatrix *mat) { CWRAPPER_BEGIN @@ -915,7 +915,7 @@ int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, CWRAPPER_END } -int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, +CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, const CDenseMatrix *b) { CWRAPPER_BEGIN @@ -924,7 +924,7 @@ int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, CWRAPPER_END } -int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, +CWRAPPER_OUTPUT_TYPE dense_matrix_ones(CDenseMatrix *s, unsigned long int r, unsigned long int c) { CWRAPPER_BEGIN @@ -933,7 +933,7 @@ int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, CWRAPPER_END } -int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, +CWRAPPER_OUTPUT_TYPE dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, unsigned long int c) { CWRAPPER_BEGIN @@ -941,7 +941,7 @@ int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, zeros(s->m); CWRAPPER_END } -int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) +CWRAPPER_OUTPUT_TYPE dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) { CWRAPPER_BEGIN int vec_size = vecbasic_size(d); @@ -951,7 +951,7 @@ int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) CWRAPPER_END } -int dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, +CWRAPPER_OUTPUT_TYPE dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, int k) { CWRAPPER_BEGIN @@ -1055,14 +1055,14 @@ size_t mapbasicbasic_size(CMapBasicBasic *self) // ---------------------- -int basic_get_args(const basic self, CVecBasic *args) +CWRAPPER_OUTPUT_TYPE basic_get_args(const basic self, CVecBasic *args) { CWRAPPER_BEGIN args->m = self->m->get_args(); CWRAPPER_END } -int basic_free_symbols(const basic self, CSetBasic *symbols) +CWRAPPER_OUTPUT_TYPE basic_free_symbols(const basic self, CSetBasic *symbols) { CWRAPPER_BEGIN symbols->m = SymEngine::free_symbols(*(self->m)); @@ -1074,21 +1074,21 @@ size_t basic_hash(const basic self) return self->m->hash(); } -int basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb) +CWRAPPER_OUTPUT_TYPE basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb) { CWRAPPER_BEGIN s->m = e->m->subs(mapbb->m); CWRAPPER_END } -int basic_subs2(basic s, const basic e, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_subs2(basic s, const basic e, const basic a, const basic b) { CWRAPPER_BEGIN s->m = e->m->subs({{a->m, b->m}}); CWRAPPER_END } -int function_symbol_set(basic s, const char *c, const CVecBasic *arg) +CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s, const char *c, const CVecBasic *arg) { CWRAPPER_BEGIN s->m = function_symbol(c, arg->m); @@ -1107,7 +1107,7 @@ char *ascii_art_str() // Cwrapper for ntheory -int ntheory_gcd(basic s, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE ntheory_gcd(basic s, const basic a, const basic b) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); @@ -1117,7 +1117,7 @@ int ntheory_gcd(basic s, const basic a, const basic b) CWRAPPER_END } -int ntheory_lcm(basic s, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE ntheory_lcm(basic s, const basic a, const basic b) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); @@ -1127,7 +1127,7 @@ int ntheory_lcm(basic s, const basic a, const basic b) CWRAPPER_END } -int ntheory_nextprime(basic s, const basic a) +CWRAPPER_OUTPUT_TYPE ntheory_nextprime(basic s, const basic a) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); @@ -1135,7 +1135,7 @@ int ntheory_nextprime(basic s, const basic a) CWRAPPER_END } -int ntheory_mod(basic s, const basic n, const basic d) +CWRAPPER_OUTPUT_TYPE ntheory_mod(basic s, const basic n, const basic d) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(n->m))); @@ -1145,7 +1145,7 @@ int ntheory_mod(basic s, const basic n, const basic d) CWRAPPER_END } -int ntheory_quotient(basic s, const basic n, const basic d) +CWRAPPER_OUTPUT_TYPE ntheory_quotient(basic s, const basic n, const basic d) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(n->m))); @@ -1155,21 +1155,21 @@ int ntheory_quotient(basic s, const basic n, const basic d) CWRAPPER_END } -int ntheory_fibonacci(basic s, unsigned long a) +CWRAPPER_OUTPUT_TYPE ntheory_fibonacci(basic s, unsigned long a) { CWRAPPER_BEGIN s->m = SymEngine::fibonacci(a); CWRAPPER_END } -int ntheory_lucas(basic s, unsigned long a) +CWRAPPER_OUTPUT_TYPE ntheory_lucas(basic s, unsigned long a) { CWRAPPER_BEGIN s->m = SymEngine::lucas(a); CWRAPPER_END } -int ntheory_binomial(basic s, const basic a, unsigned long b) +CWRAPPER_OUTPUT_TYPE ntheory_binomial(basic s, const basic a, unsigned long b) { CWRAPPER_BEGIN SYMENGINE_ASSERT(is_a(*(a->m))); @@ -1178,7 +1178,7 @@ int ntheory_binomial(basic s, const basic a, unsigned long b) } //! Wrapper for evalf -int basic_evalf(basic s, const basic b, unsigned long bits, int real) +CWRAPPER_OUTPUT_TYPE basic_evalf(basic s, const basic b, unsigned long bits, int real) { CWRAPPER_BEGIN diff --git a/symengine/cwrapper.h b/symengine/cwrapper.h index 0dae401504..750008746e 100644 --- a/symengine/cwrapper.h +++ b/symengine/cwrapper.h @@ -25,6 +25,8 @@ extern "C" { abort(); \ } \ } + +typedef int CWRAPPER_OUTPUT_TYPE; typedef enum { #define SYMENGINE_INCLUDE_ALL @@ -108,10 +110,10 @@ void basic_const_E(basic s); void basic_const_EulerGamma(basic s); //! Assign value of b to a. -int basic_assign(basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_assign(basic a, const basic b); //! Parse str and assign value to b -int basic_parse(basic b, const char *str); +CWRAPPER_OUTPUT_TYPE basic_parse(basic b, const char *str); //! Returns the typeID of the basic struct TypeID basic_get_type(const basic s); @@ -123,33 +125,33 @@ char *basic_get_class_from_id(TypeID id); //! Assign to s, a symbol with string representation c. //! This function creates a new SymEngine::Symbol from a copy of //! the string in c, thus the caller is free to use c afterwards. -int symbol_set(basic s, const char *c); +CWRAPPER_OUTPUT_TYPE symbol_set(basic s, const char *c); //! Assign to s, a long. -int integer_set_si(basic s, long i); +CWRAPPER_OUTPUT_TYPE integer_set_si(basic s, long i); //! Assign to s, a ulong. -int integer_set_ui(basic s, unsigned long i); +CWRAPPER_OUTPUT_TYPE integer_set_ui(basic s, unsigned long i); //! Assign to s, a mpz_t. -int integer_set_mpz(basic s, const mpz_t i); +CWRAPPER_OUTPUT_TYPE integer_set_mpz(basic s, const mpz_t i); //! Assign to s, an integer that has base 10 representation c. -int integer_set_str(basic s, const char *c); +CWRAPPER_OUTPUT_TYPE integer_set_str(basic s, const char *c); //! Assign to s, a real_double that has value of d. -int real_double_set_d(basic s, double d); +CWRAPPER_OUTPUT_TYPE real_double_set_d(basic s, double d); //! Returns double value of s. double real_double_get_d(const basic s); #ifdef HAVE_SYMENGINE_MPFR //! Assign to s, a real mpfr that has value d with precision prec. -int real_mpfr_set_d(basic s, double d, int prec); +CWRAPPER_OUTPUT_TYPE 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. -int real_mpfr_set_str(basic s, const char *c, int prec); +CWRAPPER_OUTPUT_TYPE real_mpfr_set_str(basic s, const 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. -int real_mpfr_set(basic s, mpfr_srcptr m); +CWRAPPER_OUTPUT_TYPE real_mpfr_set(basic s, mpfr_srcptr m); //! Assign to m, the mpfr_t given in s. -int real_mpfr_get(mpfr_ptr m, const basic s); +CWRAPPER_OUTPUT_TYPE 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 @@ -160,9 +162,9 @@ int real_mpfr_is_zero(const basic s); //! Returns 1 if s has value zero; 0 otherwise int complex_mpc_is_zero(const basic s); //! Assign to s, the real part of com -int complex_mpc_real_part(basic s, const basic com); +CWRAPPER_OUTPUT_TYPE complex_mpc_real_part(basic s, const basic com); //! Assign to s, the imaginary part of com -int complex_mpc_imaginary_part(basic s, const basic com); +CWRAPPER_OUTPUT_TYPE complex_mpc_imaginary_part(basic s, const basic com); #endif // HAVE_SYMENGINE_MPC //! Returns signed long value of s. @@ -173,42 +175,42 @@ unsigned long integer_get_ui(const basic s); int integer_get_mpz(mpz_t a, const basic s); //! Assign to s, a rational i/j. Returns 0 if either i or j is not an integer. -int rational_set(basic s, const basic i, const basic j); +CWRAPPER_OUTPUT_TYPE rational_set(basic s, const basic i, const basic j); //! Assign to s, a rational i/j, where i and j are signed longs. -int rational_set_si(basic s, long i, long j); +CWRAPPER_OUTPUT_TYPE rational_set_si(basic s, long i, long j); //! Assign to s, a rational i/j, where i and j are unsigned longs. -int rational_set_ui(basic s, unsigned long i, unsigned long j); +CWRAPPER_OUTPUT_TYPE rational_set_ui(basic s, unsigned long i, unsigned long j); //! Assign to s, a rational i, where is of type mpq_t. -int rational_set_mpq(basic s, const mpq_t i); +CWRAPPER_OUTPUT_TYPE rational_set_mpq(basic s, const mpq_t i); //! Assign to s, a complex re + i*im. -int complex_set(basic s, const basic re, const basic im); +CWRAPPER_OUTPUT_TYPE complex_set(basic s, const basic re, const basic im); //! Assign to s, a complex re + i*im, where re and im are rationals. -int complex_set_rat(basic s, const basic re, const basic im); +CWRAPPER_OUTPUT_TYPE complex_set_rat(basic s, const basic re, const basic im); //! Assign to s, a complex re + i*im, where re and im are of type mpq. -int complex_set_mpq(basic s, const mpq_t re, const mpq_t im); +CWRAPPER_OUTPUT_TYPE complex_set_mpq(basic s, const mpq_t re, const mpq_t im); //! Assign to s, a real where com is a complex -int complex_real_part(basic s, const basic com); +CWRAPPER_OUTPUT_TYPE complex_real_part(basic s, const basic com); //! Assign to s, an imaginary where com is a complex -int complex_imaginary_part(basic s, const basic com); +CWRAPPER_OUTPUT_TYPE complex_imaginary_part(basic s, const basic com); //! Assign to s, a real double where com is a complex double -int complex_double_real_part(basic s, const basic com); +CWRAPPER_OUTPUT_TYPE complex_double_real_part(basic s, const basic com); //! Assign to s, an imaginary double where com is a complex double -int complex_double_imaginary_part(basic s, const basic com); +CWRAPPER_OUTPUT_TYPE complex_double_imaginary_part(basic s, const basic com); //! Assigns s = a + b. -int basic_add(basic s, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_add(basic s, const basic a, const basic b); //! Assigns s = a - b. -int basic_sub(basic s, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_sub(basic s, const basic a, const basic b); //! Assigns s = a * b. -int basic_mul(basic s, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_mul(basic s, const basic a, const basic b); //! Assigns s = a / b. -int basic_div(basic s, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_div(basic s, const basic a, const basic b); //! Assigns s = a ** b. -int basic_pow(basic s, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_pow(basic s, const basic a, const basic b); //! Assign to s, derivative of expr with respect to sym. Returns 0 if sym is not //! a symbol. -int basic_diff(basic s, const basic expr, const basic sym); +CWRAPPER_OUTPUT_TYPE basic_diff(basic s, const basic expr, const basic sym); //! Returns 1 if both basic are equal, 0 if not int basic_eq(const basic a, const basic b); //! Returns 1 if both basic are not equal, 0 if they are @@ -217,77 +219,77 @@ int basic_neq(const basic a, const basic b); int basic_number_sign(const basic s); //! Expands the expr a and assigns to s. -int basic_expand(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_expand(basic s, const basic a); //! Assigns s = -a. -int basic_neg(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_neg(basic s, const basic a); //! Assigns s = abs(a). -int basic_abs(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_abs(basic s, const basic a); //! Assigns s = sin(a). -int basic_sin(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_sin(basic s, const basic a); //! Assigns s = cos(a). -int basic_cos(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_cos(basic s, const basic a); //! Assigns s = tan(a). -int basic_tan(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_tan(basic s, const basic a); //! Assigns s = asin(a). -int basic_asin(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_asin(basic s, const basic a); //! Assigns s = acos(a). -int basic_acos(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_acos(basic s, const basic a); //! Assigns s = atan(a). -int basic_atan(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_atan(basic s, const basic a); //! Assigns s = csc(a). -int basic_csc(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_csc(basic s, const basic a); //! Assigns s = sec(a). -int basic_sec(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_sec(basic s, const basic a); //! Assigns s = cot(a). -int basic_cot(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_cot(basic s, const basic a); //! Assigns s = acsc(a). -int basic_acsc(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_acsc(basic s, const basic a); //! Assigns s = asec(a). -int basic_asec(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_asec(basic s, const basic a); //! Assigns s = acot(a). -int basic_acot(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_acot(basic s, const basic a); //! Assigns s = sinh(a). -int basic_sinh(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_sinh(basic s, const basic a); //! Assigns s = cosh(a). -int basic_cosh(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_cosh(basic s, const basic a); //! Assigns s = tanh(a). -int basic_tanh(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_tanh(basic s, const basic a); //! Assigns s = asinh(a). -int basic_asinh(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_asinh(basic s, const basic a); //! Assigns s = acosh(a). -int basic_acosh(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_acosh(basic s, const basic a); //! Assigns s = atanh(a). -int basic_atanh(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_atanh(basic s, const basic a); //! Assigns s = csch(a). -int basic_csch(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_csch(basic s, const basic a); //! Assigns s = sech(a). -int basic_sech(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_sech(basic s, const basic a); //! Assigns s = coth(a). -int basic_coth(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_coth(basic s, const basic a); //! Assigns s = acsch(a). -int basic_acsch(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_acsch(basic s, const basic a); //! Assigns s = asech(a). -int basic_asech(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_asech(basic s, const basic a); //! Assigns s = acoth(a). -int basic_acoth(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_acoth(basic s, const basic a); //! Assigns s = lambertw(a). -int basic_lambertw(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_lambertw(basic s, const basic a); //! Assigns s = zeta(a). -int basic_zeta(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_zeta(basic s, const basic a); //! Assigns s = dirichlet_eta(a). -int basic_dirichlet_eta(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_dirichlet_eta(basic s, const basic a); //! Assigns s = gamma(a). -int basic_gamma(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE basic_gamma(basic s, const basic a); //! Returns a new char pointer to the string representation of s. char *basic_str(const basic s); @@ -361,33 +363,33 @@ CDenseMatrix *dense_matrix_new_rows_cols(unsigned r, unsigned c); void sparse_matrix_free(CSparseMatrix *self); //! Assign to s, a DenseMatrix with value d -int dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d); +CWRAPPER_OUTPUT_TYPE dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d); //! Return a string representation of s char *dense_matrix_str(const CDenseMatrix *s); //! Resize mat to rxc -int dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c); +CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c); //! Assign to s, mat[r][c] -int dense_matrix_get_basic(basic s, const CDenseMatrix *mat, +CWRAPPER_OUTPUT_TYPE dense_matrix_get_basic(basic s, const CDenseMatrix *mat, unsigned long int r, unsigned long int c); //! Assign s to mat[r][c] -int dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, +CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, unsigned long int c, basic s); //! Assign to s, mat[r][c] -int sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, +CWRAPPER_OUTPUT_TYPE sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, unsigned long int r, unsigned long int c); //! Assign s to mat[r][c] -int sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, +CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, unsigned long int c, basic s); //! Assign to s, determinent of mat -int dense_matrix_det(basic s, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_det(basic s, const CDenseMatrix *mat); //! Assign to s, a DenseMatrix which is the inverse of mat -int dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat); //! Assign to s, a DenseMatrix which is the transpose of mat -int dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat); //! Assign to s, a SubMatrix of mat, starting with [r1, r2] until [r2, c2], with //! step sizes [r, c] -int dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, +CWRAPPER_OUTPUT_TYPE dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, unsigned long int r1, unsigned long int c1, unsigned long int r2, unsigned long int c2, unsigned long int r, unsigned long int c); @@ -396,41 +398,41 @@ unsigned long int dense_matrix_cols(const CDenseMatrix *s); //! Return the number of rows of s unsigned long int dense_matrix_rows(const CDenseMatrix *s); //! Assign to s, the addition of matA and matB -int dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB); //! Assign to s, the matrix multiplication of matA and matB -int dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, const CDenseMatrix *matB); //! Assign to s, the addition of scalar b to matrix matA -int dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b); //! Assign to s, the multiplication of scalar b to matrix matA -int dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, const basic b); //! Assign to l and u, LU factorization of mat -int dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat); //! Assign to l and d, LDL factorization of mat -int dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, +CWRAPPER_OUTPUT_TYPE dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat); //! Assign to lu, fraction free LU factorization of mat -int dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat); //! Assign to l, d and u, FFLDU factorization of mat -int dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, const CDenseMatrix *mat); //! Assign to x, solution to A x = b -int dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, +CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, const CDenseMatrix *b); //! Assign to s, a matrix of ones of size rxc -int dense_matrix_ones(CDenseMatrix *s, unsigned long int r, +CWRAPPER_OUTPUT_TYPE dense_matrix_ones(CDenseMatrix *s, unsigned long int r, unsigned long int c); //! Assign to s, a matrix of zeros of size rxc -int dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, +CWRAPPER_OUTPUT_TYPE dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, unsigned long int c); //! Assign to s, a diagonal matrix with a diagonal at offset k, with elements in //! d -int dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k); +CWRAPPER_OUTPUT_TYPE dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k); //! Assign to s, a matrix of size NxM, with diagonal of 1s at offset k -int dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, +CWRAPPER_OUTPUT_TYPE dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, int k); //! Assign to s, a CSRMatrix @@ -480,21 +482,21 @@ size_t mapbasicbasic_size(CMapBasicBasic *self); // ------------------------------------- //! Returns a CVecBasic of vec_basic given by get_args -int basic_get_args(const basic self, CVecBasic *args); +CWRAPPER_OUTPUT_TYPE basic_get_args(const basic self, CVecBasic *args); //! Returns a CSetBasic of set_basic given by free_symbols -int basic_free_symbols(const basic self, CSetBasic *symbols); +CWRAPPER_OUTPUT_TYPE basic_free_symbols(const basic self, CSetBasic *symbols); //! returns the hash of the Basic object size_t basic_hash(const basic self); //! substitutes all the keys with their mapped values //! in the given basic `e` and returns it through basic 's' -int basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb); +CWRAPPER_OUTPUT_TYPE basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb); //! substitutes a basic 'a' with another basic 'b', //! in the given basic 'e' and returns it through basic 's' -int basic_subs2(basic s, const basic e, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_subs2(basic s, const basic e, const basic a, const basic b); //! Assigns to s a FunctionSymbol with name described by c, with dependent //! symbols arg -int function_symbol_set(basic s, const char *c, const CVecBasic *arg); +CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s, const char *c, const CVecBasic *arg); //! Wrapper for ascii_art() @@ -504,23 +506,23 @@ char *ascii_art_str(); //! Wrapper for ntheory //! Greatest Common Divisor -int ntheory_gcd(basic s, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE ntheory_gcd(basic s, const basic a, const basic b); //! Least Common Multiple -int ntheory_lcm(basic s, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE ntheory_lcm(basic s, const basic a, const basic b); //! \return next prime after `a` -int ntheory_nextprime(basic s, const basic a); +CWRAPPER_OUTPUT_TYPE ntheory_nextprime(basic s, const basic a); //! modulo round toward zero -int ntheory_mod(basic s, const basic n, const basic d); +CWRAPPER_OUTPUT_TYPE ntheory_mod(basic s, const basic n, const basic d); //! \return quotient round toward zero when `n` is divided by `d` -int ntheory_quotient(basic s, const basic n, const basic d); +CWRAPPER_OUTPUT_TYPE ntheory_quotient(basic s, const basic n, const basic d); //! nth Fibonacci number // fibonacci(0) = 0 and fibonacci(1) = 1 -int ntheory_fibonacci(basic s, unsigned long a); +CWRAPPER_OUTPUT_TYPE ntheory_fibonacci(basic s, unsigned long a); //! Lucas number -int ntheory_lucas(basic s, unsigned long a); +CWRAPPER_OUTPUT_TYPE ntheory_lucas(basic s, unsigned long a); //! Binomial Coefficient -int ntheory_binomial(basic s, const basic a, unsigned long b); +CWRAPPER_OUTPUT_TYPE ntheory_binomial(basic s, const basic a, unsigned long b); //! Evaluate b and assign the value to s -int basic_evalf(basic s, const basic b, unsigned long bits, int real); +CWRAPPER_OUTPUT_TYPE basic_evalf(basic s, const basic b, unsigned long bits, int real); //! Print stacktrace on segfault void symengine_print_stack_on_segfault(); diff --git a/symengine/symengine_exception.cpp b/symengine/symengine_exception.cpp deleted file mode 100644 index 8c5558474f..0000000000 --- a/symengine/symengine_exception.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -using namespace std; - -namespace symengine -{ - -class DivisionByZero: public exception -{ - virtual const char* what() const throw() - { - return "Division By Zero"; - } -}; - -} From 18eb68b94504660b8cf8fddd7262c3bff63477ec Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 28 Jul 2016 16:55:04 +0530 Subject: [PATCH 07/19] All Division by Zero errors to be thrown as DivisioByZero, fixed tests to reflect the change --- symengine/complex.h | 7 +- symengine/cwrapper.cpp | 126 +++++++++++++---------- symengine/cwrapper.h | 96 ++++++++++------- symengine/integer.cpp | 2 +- symengine/rational.cpp | 5 +- symengine/rational.h | 7 +- symengine/series.h | 3 +- symengine/symengine_exception.h | 9 +- symengine/tests/basic/test_basic.cpp | 14 +-- symengine/tests/basic/test_functions.cpp | 12 ++- 10 files changed, 162 insertions(+), 119 deletions(-) diff --git a/symengine/complex.h b/symengine/complex.h index 4e26ed9e64..be7d2c201b 100644 --- a/symengine/complex.h +++ b/symengine/complex.h @@ -7,6 +7,7 @@ #define SYMENGINE_COMPLEX_H #include +#include namespace SymEngine { @@ -203,7 +204,7 @@ class Complex : public Number inline RCP divcomp(const Rational &other) const { if (other.is_zero()) { - throw std::runtime_error("Divide by zero."); + throw DivisionByZero(); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -214,7 +215,7 @@ class Complex : public Number inline RCP divcomp(const Integer &other) const { if (other.is_zero()) { - throw std::runtime_error("Divide by zero."); + throw DivisionByZero(); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -227,7 +228,7 @@ class Complex : public Number rational_class conjugate = this->real_ * this->real_ + this->imaginary_ * this->imaginary_; if (get_num(conjugate) == 0) { - throw std::runtime_error("Divide by zero."); + throw DivisionByZero(); } else { return from_mpq((this->real_ * other.i) / conjugate, (this->imaginary_ * (-other.i)) / conjugate); diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index 75b3618e68..15de2d606e 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -60,21 +60,18 @@ inline bool is_aligned(T *p, size_t n = alignof(T)) extern "C" { +#define CWRAPPER_BEGIN try { -#define CWRAPPER_BEGIN \ - try \ - { - -#define CWRAPPER_END \ - return 0; \ - } \ - catch ( DivisionByZero ) \ - { \ - return 1; \ - } \ - catch ( ... ) \ - { \ - return -1; \ +#define CWRAPPER_END \ + return 0; \ + } \ + catch (DivisionByZero) \ + { \ + return 1; \ + } \ + catch (...) \ + { \ + return -1; \ } struct CRCPBasic { @@ -508,11 +505,11 @@ int basic_number_sign(const basic s) } #define IMPLEMENT_ONE_ARG_FUNC(func) \ - CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a) \ + CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a) \ { \ - CWRAPPER_BEGIN \ + CWRAPPER_BEGIN \ s->m = SymEngine::func(a->m); \ - CWRAPPER_END \ + CWRAPPER_END \ } IMPLEMENT_ONE_ARG_FUNC(expand); @@ -762,7 +759,8 @@ char *sparse_matrix_str(const CSparseMatrix *s) return cc; } -CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c) +CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, + unsigned c) { CWRAPPER_BEGIN mat->m.resize(r, c); @@ -770,15 +768,17 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsig } CWRAPPER_OUTPUT_TYPE dense_matrix_get_basic(basic s, const CDenseMatrix *mat, - unsigned long int r, unsigned long int c) + unsigned long int r, + unsigned long int c) { CWRAPPER_BEGIN s->m = mat->m.get(r, c); CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, - unsigned long int c, basic s) +CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(CDenseMatrix *mat, + unsigned long int r, + unsigned long int c, basic s) { CWRAPPER_BEGIN mat->m.set(r, c, s->m); @@ -786,15 +786,17 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int } CWRAPPER_OUTPUT_TYPE sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, - unsigned long int r, unsigned long int c) + unsigned long int r, + unsigned long int c) { CWRAPPER_BEGIN s->m = mat->m.get(r, c); CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, - unsigned long int c, basic s) +CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(CSparseMatrix *mat, + unsigned long int r, + unsigned long int c, basic s) { CWRAPPER_BEGIN mat->m.set(r, c, s->m); @@ -814,17 +816,19 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat) mat->m.inv(s->m); CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(CDenseMatrix *s, + const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows()); mat->m.transpose(s->m); CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, - unsigned long int r1, unsigned long int c1, - unsigned long int r2, unsigned long int c2, - unsigned long int r, unsigned long int c) +CWRAPPER_OUTPUT_TYPE +dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, + unsigned long int r1, unsigned long int c1, + unsigned long int r2, unsigned long int c2, + unsigned long int r, unsigned long int c) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, r2 - r1 + 1, c2 - c1 + 1); @@ -842,8 +846,9 @@ unsigned long int dense_matrix_cols(const CDenseMatrix *s) return s->m.ncols(); } -CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, - const CDenseMatrix *matB) +CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(CDenseMatrix *s, + const CDenseMatrix *matA, + const CDenseMatrix *matB) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); @@ -851,8 +856,9 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, - const CDenseMatrix *matB) +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(CDenseMatrix *s, + const CDenseMatrix *matA, + const CDenseMatrix *matB) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matB->m.ncols()); @@ -860,8 +866,9 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, - const basic b) +CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(CDenseMatrix *s, + const CDenseMatrix *matA, + const basic b) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); @@ -869,8 +876,9 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, - const basic b) +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(CDenseMatrix *s, + const CDenseMatrix *matA, + const basic b) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols()); @@ -878,7 +886,8 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, + const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); @@ -887,7 +896,8 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDe CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, + const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); @@ -896,7 +906,8 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, const CD CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(CDenseMatrix *lu, + const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols()); @@ -904,8 +915,9 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, - const CDenseMatrix *mat) +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, + CDenseMatrix *u, + const CDenseMatrix *mat) { CWRAPPER_BEGIN dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols()); @@ -915,8 +927,9 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDense CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, - const CDenseMatrix *b) +CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(CDenseMatrix *x, + const CDenseMatrix *A, + const CDenseMatrix *b) { CWRAPPER_BEGIN dense_matrix_rows_cols(x, A->m.ncols(), 1); @@ -925,7 +938,7 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix * } CWRAPPER_OUTPUT_TYPE dense_matrix_ones(CDenseMatrix *s, unsigned long int r, - unsigned long int c) + unsigned long int c) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, r, c); @@ -934,14 +947,15 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_ones(CDenseMatrix *s, unsigned long int r, } CWRAPPER_OUTPUT_TYPE dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, - unsigned long int c) + unsigned long int c) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, r, c); zeros(s->m); CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k) +CWRAPPER_OUTPUT_TYPE dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, + long int k) { CWRAPPER_BEGIN int vec_size = vecbasic_size(d); @@ -951,8 +965,8 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, - int k) +CWRAPPER_OUTPUT_TYPE dense_matrix_eye(CDenseMatrix *s, unsigned long int N, + unsigned long int M, int k) { CWRAPPER_BEGIN dense_matrix_rows_cols(s, N, M); @@ -1074,21 +1088,24 @@ size_t basic_hash(const basic self) return self->m->hash(); } -CWRAPPER_OUTPUT_TYPE basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb) +CWRAPPER_OUTPUT_TYPE basic_subs(basic s, const basic e, + const CMapBasicBasic *mapbb) { CWRAPPER_BEGIN s->m = e->m->subs(mapbb->m); CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE basic_subs2(basic s, const basic e, const basic a, const basic b) +CWRAPPER_OUTPUT_TYPE basic_subs2(basic s, const basic e, const basic a, + const basic b) { CWRAPPER_BEGIN s->m = e->m->subs({{a->m, b->m}}); CWRAPPER_END } -CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s, const char *c, const CVecBasic *arg) +CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s, const char *c, + const CVecBasic *arg) { CWRAPPER_BEGIN s->m = function_symbol(c, arg->m); @@ -1178,7 +1195,8 @@ CWRAPPER_OUTPUT_TYPE ntheory_binomial(basic s, const basic a, unsigned long b) } //! Wrapper for evalf -CWRAPPER_OUTPUT_TYPE basic_evalf(basic s, const basic b, unsigned long bits, int real) +CWRAPPER_OUTPUT_TYPE basic_evalf(basic s, const basic b, unsigned long bits, + int real) { CWRAPPER_BEGIN @@ -1191,6 +1209,4 @@ void symengine_print_stack_on_segfault() { SymEngine::print_stack_on_segfault(); } - - } diff --git a/symengine/cwrapper.h b/symengine/cwrapper.h index 750008746e..d81d56e431 100644 --- a/symengine/cwrapper.h +++ b/symengine/cwrapper.h @@ -25,7 +25,7 @@ extern "C" { abort(); \ } \ } - + typedef int CWRAPPER_OUTPUT_TYPE; typedef enum { @@ -172,7 +172,7 @@ signed long integer_get_si(const basic s); //! Returns unsigned long value of s. unsigned long integer_get_ui(const basic s); //! Returns s as a mpz_t. -int integer_get_mpz(mpz_t a, const basic s); +CWRAPPER_OUTPUT_TYPE integer_get_mpz(mpz_t a, const basic s); //! Assign to s, a rational i/j. Returns 0 if either i or j is not an integer. CWRAPPER_OUTPUT_TYPE rational_set(basic s, const basic i, const basic j); @@ -368,72 +368,88 @@ CWRAPPER_OUTPUT_TYPE dense_matrix_set(CDenseMatrix *s, const CDenseMatrix *d); //! Return a string representation of s char *dense_matrix_str(const CDenseMatrix *s); //! Resize mat to rxc -CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, unsigned c); +CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(CDenseMatrix *mat, unsigned r, + unsigned c); //! Assign to s, mat[r][c] CWRAPPER_OUTPUT_TYPE dense_matrix_get_basic(basic s, const CDenseMatrix *mat, - unsigned long int r, unsigned long int c); + unsigned long int r, + unsigned long int c); //! Assign s to mat[r][c] -CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(CDenseMatrix *mat, unsigned long int r, - unsigned long int c, basic s); +CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(CDenseMatrix *mat, + unsigned long int r, + unsigned long int c, basic s); //! Assign to s, mat[r][c] CWRAPPER_OUTPUT_TYPE sparse_matrix_get_basic(basic s, const CSparseMatrix *mat, - unsigned long int r, unsigned long int c); + unsigned long int r, + unsigned long int c); //! Assign s to mat[r][c] -CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(CSparseMatrix *mat, unsigned long int r, - unsigned long int c, basic s); +CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(CSparseMatrix *mat, + unsigned long int r, + unsigned long int c, basic s); //! Assign to s, determinent of mat CWRAPPER_OUTPUT_TYPE dense_matrix_det(basic s, const CDenseMatrix *mat); //! Assign to s, a DenseMatrix which is the inverse of mat CWRAPPER_OUTPUT_TYPE dense_matrix_inv(CDenseMatrix *s, const CDenseMatrix *mat); //! Assign to s, a DenseMatrix which is the transpose of mat -CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(CDenseMatrix *s, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(CDenseMatrix *s, + const CDenseMatrix *mat); //! Assign to s, a SubMatrix of mat, starting with [r1, r2] until [r2, c2], with //! step sizes [r, c] -CWRAPPER_OUTPUT_TYPE dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, - unsigned long int r1, unsigned long int c1, - unsigned long int r2, unsigned long int c2, - unsigned long int r, unsigned long int c); +CWRAPPER_OUTPUT_TYPE +dense_matrix_submatrix(CDenseMatrix *s, const CDenseMatrix *mat, + unsigned long int r1, unsigned long int c1, + unsigned long int r2, unsigned long int c2, + unsigned long int r, unsigned long int c); //! Return the number of columns of s unsigned long int dense_matrix_cols(const CDenseMatrix *s); //! Return the number of rows of s unsigned long int dense_matrix_rows(const CDenseMatrix *s); //! Assign to s, the addition of matA and matB -CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(CDenseMatrix *s, const CDenseMatrix *matA, - const CDenseMatrix *matB); +CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(CDenseMatrix *s, + const CDenseMatrix *matA, + const CDenseMatrix *matB); //! Assign to s, the matrix multiplication of matA and matB -CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(CDenseMatrix *s, const CDenseMatrix *matA, - const CDenseMatrix *matB); +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(CDenseMatrix *s, + const CDenseMatrix *matA, + const CDenseMatrix *matB); //! Assign to s, the addition of scalar b to matrix matA -CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(CDenseMatrix *s, const CDenseMatrix *matA, - const basic b); +CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(CDenseMatrix *s, + const CDenseMatrix *matA, + const basic b); //! Assign to s, the multiplication of scalar b to matrix matA -CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(CDenseMatrix *s, const CDenseMatrix *matA, - const basic b); +CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(CDenseMatrix *s, + const CDenseMatrix *matA, + const basic b); //! Assign to l and u, LU factorization of mat -CWRAPPER_OUTPUT_TYPE dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_LU(CDenseMatrix *l, CDenseMatrix *u, + const CDenseMatrix *mat); //! Assign to l and d, LDL factorization of mat CWRAPPER_OUTPUT_TYPE dense_matrix_LDL(CDenseMatrix *l, CDenseMatrix *d, - const CDenseMatrix *mat); + const CDenseMatrix *mat); //! Assign to lu, fraction free LU factorization of mat -CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(CDenseMatrix *lu, const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(CDenseMatrix *lu, + const CDenseMatrix *mat); //! Assign to l, d and u, FFLDU factorization of mat -CWRAPPER_OUTPUT_TYPE dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, CDenseMatrix *u, - const CDenseMatrix *mat); +CWRAPPER_OUTPUT_TYPE dense_matrix_FFLDU(CDenseMatrix *l, CDenseMatrix *d, + CDenseMatrix *u, + const CDenseMatrix *mat); //! Assign to x, solution to A x = b -CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(CDenseMatrix *x, const CDenseMatrix *A, - const CDenseMatrix *b); +CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(CDenseMatrix *x, + const CDenseMatrix *A, + const CDenseMatrix *b); //! Assign to s, a matrix of ones of size rxc CWRAPPER_OUTPUT_TYPE dense_matrix_ones(CDenseMatrix *s, unsigned long int r, - unsigned long int c); + unsigned long int c); //! Assign to s, a matrix of zeros of size rxc CWRAPPER_OUTPUT_TYPE dense_matrix_zeros(CDenseMatrix *s, unsigned long int r, - unsigned long int c); + unsigned long int c); //! Assign to s, a diagonal matrix with a diagonal at offset k, with elements in //! d -CWRAPPER_OUTPUT_TYPE dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, long int k); +CWRAPPER_OUTPUT_TYPE dense_matrix_diag(CDenseMatrix *s, CVecBasic *d, + long int k); //! Assign to s, a matrix of size NxM, with diagonal of 1s at offset k -CWRAPPER_OUTPUT_TYPE dense_matrix_eye(CDenseMatrix *s, unsigned long int N, unsigned long int M, - int k); +CWRAPPER_OUTPUT_TYPE dense_matrix_eye(CDenseMatrix *s, unsigned long int N, + unsigned long int M, int k); //! Assign to s, a CSRMatrix void sparse_matrix_init(CSparseMatrix *s); @@ -489,14 +505,17 @@ CWRAPPER_OUTPUT_TYPE basic_free_symbols(const basic self, CSetBasic *symbols); size_t basic_hash(const basic self); //! substitutes all the keys with their mapped values //! in the given basic `e` and returns it through basic 's' -CWRAPPER_OUTPUT_TYPE basic_subs(basic s, const basic e, const CMapBasicBasic *mapbb); +CWRAPPER_OUTPUT_TYPE basic_subs(basic s, const basic e, + const CMapBasicBasic *mapbb); //! substitutes a basic 'a' with another basic 'b', //! in the given basic 'e' and returns it through basic 's' -CWRAPPER_OUTPUT_TYPE basic_subs2(basic s, const basic e, const basic a, const basic b); +CWRAPPER_OUTPUT_TYPE basic_subs2(basic s, const basic e, const basic a, + const basic b); //! Assigns to s a FunctionSymbol with name described by c, with dependent //! symbols arg -CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s, const char *c, const CVecBasic *arg); +CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s, const char *c, + const CVecBasic *arg); //! Wrapper for ascii_art() @@ -522,7 +541,8 @@ CWRAPPER_OUTPUT_TYPE ntheory_lucas(basic s, unsigned long a); //! Binomial Coefficient CWRAPPER_OUTPUT_TYPE ntheory_binomial(basic s, const basic a, unsigned long b); //! Evaluate b and assign the value to s -CWRAPPER_OUTPUT_TYPE basic_evalf(basic s, const basic b, unsigned long bits, int real); +CWRAPPER_OUTPUT_TYPE basic_evalf(basic s, const basic b, unsigned long bits, + int real); //! Print stacktrace on segfault void symengine_print_stack_on_segfault(); diff --git a/symengine/integer.cpp b/symengine/integer.cpp index bc5d1a42f6..dea619ad9a 100644 --- a/symengine/integer.cpp +++ b/symengine/integer.cpp @@ -59,7 +59,7 @@ RCP Integer::rdiv(const Number &other) const { if (is_a(other)) { if (this->i == 0) { - throw std::runtime_error("Rational: Division by zero."); + throw DivisionByZero(); } rational_class q((static_cast(other)).i, this->i); diff --git a/symengine/rational.cpp b/symengine/rational.cpp index d55f6f0906..df01beeaa0 100644 --- a/symengine/rational.cpp +++ b/symengine/rational.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace SymEngine { @@ -37,7 +38,7 @@ RCP Rational::from_mpq(rational_class i) RCP Rational::from_two_ints(const Integer &n, const Integer &d) { if (d.i == 0) - throw std::runtime_error("Rational: Division by zero."); + throw DivisionByZero(); rational_class q(n.i, d.i); // This is potentially slow, but has to be done, since 'n/d' might not be @@ -50,7 +51,7 @@ RCP Rational::from_two_ints(const Integer &n, const Integer &d) RCP Rational::from_two_ints(long n, long d) { if (d == 0) - throw std::runtime_error("Rational: Division by zero."); + throw DivisionByZero(); rational_class q(n, d); // This is potentially slow, but has to be done, since 'n/d' might not be diff --git a/symengine/rational.h b/symengine/rational.h index 2705a2a5fd..763d7de2f3 100644 --- a/symengine/rational.h +++ b/symengine/rational.h @@ -7,6 +7,7 @@ #define SYMENGINE_RATIONAL_H #include +#include namespace SymEngine { @@ -140,7 +141,7 @@ class Rational : public Number inline RCP divrat(const Rational &other) const { if (other.i == 0) { - throw std::runtime_error("Division by zero"); + throw DivisionByZero(); } else { return from_mpq(this->i / other.i); } @@ -151,7 +152,7 @@ class Rational : public Number inline RCP divrat(const Integer &other) const { if (other.i == 0) { - throw std::runtime_error("Division by zero"); + throw DivisionByZero(); } else { return from_mpq(this->i / other.i); } @@ -159,7 +160,7 @@ class Rational : public Number inline RCP rdivrat(const Integer &other) const { if (this->i == 0) { - throw std::runtime_error("Division by zero"); + throw DivisionByZero(); } else { return from_mpq(other.i / this->i); } diff --git a/symengine/series.h b/symengine/series.h index 8f17c3fd29..a6594cf34d 100644 --- a/symengine/series.h +++ b/symengine/series.h @@ -9,6 +9,7 @@ #include #include +#include namespace SymEngine { @@ -187,7 +188,7 @@ class SeriesBase : public SeriesCoeffInterface unsigned int prec) { if (s == 0) - throw std::runtime_error("Series::series_invert: division by zero"); + throw DivisionByZero(); if (s == 1) return Poly(1); const short ldeg = Series::ldegree(s); diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index 7c26e0e330..81c8b08dd8 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -4,13 +4,12 @@ namespace SymEngine { -class DivisionByZero: public std::exception +class DivisionByZero : public std::exception { - virtual const char* what() const throw() + virtual const char *what() const throw() { - return "Division By Zero"; + return "Division By Zero"; } }; - } -#endif //SYMENGINE_EXCEPTION_H +#endif // SYMENGINE_EXCEPTION_H diff --git a/symengine/tests/basic/test_basic.cpp b/symengine/tests/basic/test_basic.cpp index 710d9cac37..1093a25989 100644 --- a/symengine/tests/basic/test_basic.cpp +++ b/symengine/tests/basic/test_basic.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using SymEngine::Basic; using SymEngine::Add; @@ -40,6 +41,7 @@ using SymEngine::rational_class; using SymEngine::pi; using SymEngine::diff; using SymEngine::sdiff; +using SymEngine::DivisionByZero; using namespace SymEngine::literals; @@ -308,7 +310,7 @@ TEST_CASE("Integer: Basic", "[basic]") REQUIRE(eq(*k, *integer(-5))); REQUIRE(neq(*k, *integer(12))); - CHECK_THROWS_AS(divnum(i, zero), std::runtime_error); + CHECK_THROWS_AS(divnum(i, zero), DivisionByZero); } TEST_CASE("Rational: Basic", "[basic]") @@ -411,7 +413,7 @@ TEST_CASE("Rational: Basic", "[basic]") r1 = Rational::from_two_ints(*integer(2), *integer(3)); r2 = zero; - CHECK_THROWS_AS(divnum(r1, r2), std::runtime_error); + CHECK_THROWS_AS(divnum(r1, r2), DivisionByZero); r1 = Rational::from_two_ints(*integer(3), *integer(5)); REQUIRE(is_a(*r1)); @@ -458,7 +460,7 @@ TEST_CASE("Mul: Basic", "[basic]") r = div(x, x); REQUIRE(unified_eq(r->get_args(), {})); - CHECK_THROWS_AS(div(integer(1), zero), std::runtime_error); + CHECK_THROWS_AS(div(integer(1), zero), DivisionByZero); r = mul(mul(mul(x, y), mul(x, integer(2))), integer(3)); RCP mr = rcp_static_cast(r); @@ -788,13 +790,13 @@ TEST_CASE("Complex: Basic", "[basic]") REQUIRE(eq(*c->imaginary_part(), *r2)); // Explicit division by zero checks - CHECK_THROWS_AS(divnum(c1, integer(0)), std::runtime_error); + CHECK_THROWS_AS(divnum(c1, integer(0)), DivisionByZero); r3 = Rational::from_two_ints(*integer(0), *integer(1)); - CHECK_THROWS_AS(divnum(c1, r3), std::runtime_error); + CHECK_THROWS_AS(divnum(c1, r3), DivisionByZero); c2 = Complex::from_two_nums(*r3, *r3); - CHECK_THROWS_AS(divnum(c1, c2), std::runtime_error); + CHECK_THROWS_AS(divnum(c1, c2), DivisionByZero); } TEST_CASE("has_symbol: Basic", "[basic]") diff --git a/symengine/tests/basic/test_functions.cpp b/symengine/tests/basic/test_functions.cpp index 15058527b7..3e60b3107c 100644 --- a/symengine/tests/basic/test_functions.cpp +++ b/symengine/tests/basic/test_functions.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using SymEngine::Basic; using SymEngine::Add; @@ -93,6 +94,7 @@ using SymEngine::rcp_static_cast; using SymEngine::I; using SymEngine::integer_class; using SymEngine::get_mpz_t; +using SymEngine::DivisionByZero; using namespace SymEngine::literals; @@ -448,7 +450,7 @@ TEST_CASE("Tan: functions", "[functions]") r2 = cot(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), std::runtime_error); + CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), DivisionByZero); } TEST_CASE("Cot: functions", "[functions]") @@ -548,7 +550,7 @@ TEST_CASE("Cot: functions", "[functions]") r2 = tan(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(cot(mul(integer(7), pi)), std::runtime_error); + CHECK_THROWS_AS(cot(mul(integer(7), pi)), DivisionByZero); } TEST_CASE("Csc: functions", "[functions]") @@ -648,8 +650,8 @@ TEST_CASE("Csc: functions", "[functions]") r2 = sec(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(csc(mul(integer(7), pi)), std::runtime_error); - CHECK_THROWS_AS(csc(integer(0)), std::runtime_error); + CHECK_THROWS_AS(csc(mul(integer(7), pi)), DivisionByZero); + CHECK_THROWS_AS(csc(integer(0)), DivisionByZero); } TEST_CASE("Sec: functions", "[functions]") @@ -753,7 +755,7 @@ TEST_CASE("Sec: functions", "[functions]") r2 = csc(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), std::runtime_error); + CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), DivisionByZero); } TEST_CASE("TrigFunction: trig_to_sqrt", "[functions]") From cd9414ca81c9883acee468884c6bf45963f3cbee Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 29 Jul 2016 08:00:32 +0530 Subject: [PATCH 08/19] Added more common exceptions to symengine_exception.h --- symengine/symengine_exception.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index 81c8b08dd8..2969b60643 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -11,5 +11,30 @@ class DivisionByZero : public std::exception return "Division By Zero"; } }; + +class NotImplemented : public std::exception +{ + virtual const char *what() const throw() + { + return "Not Implemented"; + } +}; + +class ResultIsComplex : public std::exception +{ + virtual const char *what() const throw() + { + return "Result is Complex. Recompile with MPC support"; + } +}; + +class Undefined : public std::exception +{ + virtual const char *what() const throw() + { + return "Undefined"; + } +}; + } #endif // SYMENGINE_EXCEPTION_H From 6de56f09df291ac139f024d0c909a3f8ab5f2cbb Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 29 Jul 2016 14:08:33 +0530 Subject: [PATCH 09/19] Moved exceptions into one common class, added enums for error codes, and a message for customized errors --- symengine/complex.h | 6 ++-- symengine/cwrapper.cpp | 6 ++-- symengine/integer.cpp | 4 +-- symengine/mul.cpp | 2 +- symengine/rational.cpp | 4 +-- symengine/rational.h | 6 ++-- symengine/series.h | 3 +- symengine/symengine_exception.h | 38 ++++++++++-------------- symengine/tests/basic/test_basic.cpp | 14 ++++----- symengine/tests/basic/test_functions.cpp | 12 ++++---- 10 files changed, 45 insertions(+), 50 deletions(-) diff --git a/symengine/complex.h b/symengine/complex.h index be7d2c201b..2a41b54af5 100644 --- a/symengine/complex.h +++ b/symengine/complex.h @@ -204,7 +204,7 @@ class Complex : public Number inline RCP divcomp(const Rational &other) const { if (other.is_zero()) { - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -215,7 +215,7 @@ class Complex : public Number inline RCP divcomp(const Integer &other) const { if (other.is_zero()) { - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -228,7 +228,7 @@ class Complex : public Number rational_class conjugate = this->real_ * this->real_ + this->imaginary_ * this->imaginary_; if (get_num(conjugate) == 0) { - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); } else { return from_mpq((this->real_ * other.i) / conjugate, (this->imaginary_ * (-other.i)) / conjugate); diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index 15de2d606e..1b0011ad0d 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -46,7 +46,7 @@ using SymEngine::diag; using SymEngine::ones; using SymEngine::zeros; using SymEngine::parse; -using SymEngine::DivisionByZero; +using SymEngine::SymEngineException; namespace SymEngine { @@ -65,9 +65,9 @@ extern "C" { #define CWRAPPER_END \ return 0; \ } \ - catch (DivisionByZero) \ + catch (SymEngineException & e) \ { \ - return 1; \ + return e.error_code(); \ } \ catch (...) \ { \ diff --git a/symengine/integer.cpp b/symengine/integer.cpp index dea619ad9a..1f719eb10d 100644 --- a/symengine/integer.cpp +++ b/symengine/integer.cpp @@ -45,7 +45,7 @@ signed long int Integer::as_int() const RCP Integer::divint(const Integer &other) const { if (other.i == 0) - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); rational_class q(this->i, other.i); // This is potentially slow, but has to be done, since q might not @@ -59,7 +59,7 @@ RCP Integer::rdiv(const Number &other) const { if (is_a(other)) { if (this->i == 0) { - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); } rational_class q((static_cast(other)).i, this->i); diff --git a/symengine/mul.cpp b/symengine/mul.cpp index 8cead552c4..12b926f041 100644 --- a/symengine/mul.cpp +++ b/symengine/mul.cpp @@ -423,7 +423,7 @@ RCP mul(const vec_basic &a) RCP div(const RCP &a, const RCP &b) { if (is_a_Number(*b) and rcp_static_cast(b)->is_zero()) - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); return mul(a, pow(b, minus_one)); } diff --git a/symengine/rational.cpp b/symengine/rational.cpp index df01beeaa0..6bdbca2a95 100644 --- a/symengine/rational.cpp +++ b/symengine/rational.cpp @@ -38,7 +38,7 @@ RCP Rational::from_mpq(rational_class i) RCP Rational::from_two_ints(const Integer &n, const Integer &d) { if (d.i == 0) - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); rational_class q(n.i, d.i); // This is potentially slow, but has to be done, since 'n/d' might not be @@ -51,7 +51,7 @@ RCP Rational::from_two_ints(const Integer &n, const Integer &d) RCP Rational::from_two_ints(long n, long d) { if (d == 0) - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); rational_class q(n, d); // This is potentially slow, but has to be done, since 'n/d' might not be diff --git a/symengine/rational.h b/symengine/rational.h index 763d7de2f3..c80c3929de 100644 --- a/symengine/rational.h +++ b/symengine/rational.h @@ -141,7 +141,7 @@ class Rational : public Number inline RCP divrat(const Rational &other) const { if (other.i == 0) { - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); } else { return from_mpq(this->i / other.i); } @@ -152,7 +152,7 @@ class Rational : public Number inline RCP divrat(const Integer &other) const { if (other.i == 0) { - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); } else { return from_mpq(this->i / other.i); } @@ -160,7 +160,7 @@ class Rational : public Number inline RCP rdivrat(const Integer &other) const { if (this->i == 0) { - throw DivisionByZero(); + throw SymEngineException("Division By Zero", DIV_BY_ZERO); } else { return from_mpq(other.i / this->i); } diff --git a/symengine/series.h b/symengine/series.h index a6594cf34d..e724081ad3 100644 --- a/symengine/series.h +++ b/symengine/series.h @@ -188,7 +188,8 @@ class SeriesBase : public SeriesCoeffInterface unsigned int prec) { if (s == 0) - throw DivisionByZero(); + throw SymEngineException("Series::series_invert: Division By Zero", + DIV_BY_ZERO); if (s == 1) return Poly(1); const short ldeg = Series::ldegree(s); diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index 2969b60643..c90ec3e3cc 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -1,40 +1,34 @@ #ifndef SYMENGINE_EXCEPTION_H #define SYMENGINE_EXCEPTION_H +enum symengine_exceptions_t { + NO_EXCEPTION, + DIV_BY_ZERO, + NOT_IMPLEMENTED, + UNDEFINED +}; + namespace SymEngine { -class DivisionByZero : public std::exception +class SymEngineException : public std::exception { - virtual const char *what() const throw() - { - return "Division By Zero"; - } -}; + std::string m_msg; + symengine_exceptions_t ec; -class NotImplemented : public std::exception -{ - virtual const char *what() const throw() +public: + SymEngineException(const std::string &msg, symengine_exceptions_t error) + : m_msg(msg), ec(error) { - return "Not Implemented"; } -}; - -class ResultIsComplex : public std::exception -{ virtual const char *what() const throw() { - return "Result is Complex. Recompile with MPC support"; + return m_msg.c_str(); } -}; - -class Undefined : public std::exception -{ - virtual const char *what() const throw() + symengine_exceptions_t error_code() { - return "Undefined"; + return ec; } }; - } #endif // SYMENGINE_EXCEPTION_H diff --git a/symengine/tests/basic/test_basic.cpp b/symengine/tests/basic/test_basic.cpp index 1093a25989..56728230a7 100644 --- a/symengine/tests/basic/test_basic.cpp +++ b/symengine/tests/basic/test_basic.cpp @@ -41,7 +41,7 @@ using SymEngine::rational_class; using SymEngine::pi; using SymEngine::diff; using SymEngine::sdiff; -using SymEngine::DivisionByZero; +using SymEngine::SymEngineException; using namespace SymEngine::literals; @@ -310,7 +310,7 @@ TEST_CASE("Integer: Basic", "[basic]") REQUIRE(eq(*k, *integer(-5))); REQUIRE(neq(*k, *integer(12))); - CHECK_THROWS_AS(divnum(i, zero), DivisionByZero); + CHECK_THROWS_AS(divnum(i, zero), SymEngineException); } TEST_CASE("Rational: Basic", "[basic]") @@ -413,7 +413,7 @@ TEST_CASE("Rational: Basic", "[basic]") r1 = Rational::from_two_ints(*integer(2), *integer(3)); r2 = zero; - CHECK_THROWS_AS(divnum(r1, r2), DivisionByZero); + CHECK_THROWS_AS(divnum(r1, r2), SymEngineException); r1 = Rational::from_two_ints(*integer(3), *integer(5)); REQUIRE(is_a(*r1)); @@ -460,7 +460,7 @@ TEST_CASE("Mul: Basic", "[basic]") r = div(x, x); REQUIRE(unified_eq(r->get_args(), {})); - CHECK_THROWS_AS(div(integer(1), zero), DivisionByZero); + CHECK_THROWS_AS(div(integer(1), zero), SymEngineException); r = mul(mul(mul(x, y), mul(x, integer(2))), integer(3)); RCP mr = rcp_static_cast(r); @@ -790,13 +790,13 @@ TEST_CASE("Complex: Basic", "[basic]") REQUIRE(eq(*c->imaginary_part(), *r2)); // Explicit division by zero checks - CHECK_THROWS_AS(divnum(c1, integer(0)), DivisionByZero); + CHECK_THROWS_AS(divnum(c1, integer(0)), SymEngineException); r3 = Rational::from_two_ints(*integer(0), *integer(1)); - CHECK_THROWS_AS(divnum(c1, r3), DivisionByZero); + CHECK_THROWS_AS(divnum(c1, r3), SymEngineException); c2 = Complex::from_two_nums(*r3, *r3); - CHECK_THROWS_AS(divnum(c1, c2), DivisionByZero); + CHECK_THROWS_AS(divnum(c1, c2), SymEngineException); } TEST_CASE("has_symbol: Basic", "[basic]") diff --git a/symengine/tests/basic/test_functions.cpp b/symengine/tests/basic/test_functions.cpp index 3e60b3107c..c7a2a2f04f 100644 --- a/symengine/tests/basic/test_functions.cpp +++ b/symengine/tests/basic/test_functions.cpp @@ -94,7 +94,7 @@ using SymEngine::rcp_static_cast; using SymEngine::I; using SymEngine::integer_class; using SymEngine::get_mpz_t; -using SymEngine::DivisionByZero; +using SymEngine::SymEngineException; using namespace SymEngine::literals; @@ -450,7 +450,7 @@ TEST_CASE("Tan: functions", "[functions]") r2 = cot(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), DivisionByZero); + CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), SymEngineException); } TEST_CASE("Cot: functions", "[functions]") @@ -550,7 +550,7 @@ TEST_CASE("Cot: functions", "[functions]") r2 = tan(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(cot(mul(integer(7), pi)), DivisionByZero); + CHECK_THROWS_AS(cot(mul(integer(7), pi)), SymEngineException); } TEST_CASE("Csc: functions", "[functions]") @@ -650,8 +650,8 @@ TEST_CASE("Csc: functions", "[functions]") r2 = sec(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(csc(mul(integer(7), pi)), DivisionByZero); - CHECK_THROWS_AS(csc(integer(0)), DivisionByZero); + CHECK_THROWS_AS(csc(mul(integer(7), pi)), SymEngineException); + CHECK_THROWS_AS(csc(integer(0)), SymEngineException); } TEST_CASE("Sec: functions", "[functions]") @@ -755,7 +755,7 @@ TEST_CASE("Sec: functions", "[functions]") r2 = csc(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), DivisionByZero); + CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), SymEngineException); } TEST_CASE("TrigFunction: trig_to_sqrt", "[functions]") From 81ff094fcf14c6d9a139f764c4f13c13884ef6fa Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sat, 30 Jul 2016 11:51:01 +0530 Subject: [PATCH 10/19] Specialized exceptions, enum included in cwrappers --- symengine/complex.h | 6 ++--- symengine/cwrapper.cpp | 2 -- symengine/cwrapper.h | 2 ++ symengine/integer.cpp | 4 +-- symengine/mul.cpp | 2 +- symengine/rational.cpp | 4 +-- symengine/rational.h | 6 ++--- symengine/series.h | 3 +-- symengine/symengine_exception.h | 34 +++++++++++++++++++----- symengine/tests/basic/test_basic.cpp | 14 +++++----- symengine/tests/basic/test_functions.cpp | 12 ++++----- 11 files changed, 54 insertions(+), 35 deletions(-) diff --git a/symengine/complex.h b/symengine/complex.h index 2a41b54af5..1897ad5a30 100644 --- a/symengine/complex.h +++ b/symengine/complex.h @@ -204,7 +204,7 @@ class Complex : public Number inline RCP divcomp(const Rational &other) const { if (other.is_zero()) { - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -215,7 +215,7 @@ class Complex : public Number inline RCP divcomp(const Integer &other) const { if (other.is_zero()) { - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -228,7 +228,7 @@ class Complex : public Number rational_class conjugate = this->real_ * this->real_ + this->imaginary_ * this->imaginary_; if (get_num(conjugate) == 0) { - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); } else { return from_mpq((this->real_ * other.i) / conjugate, (this->imaginary_ * (-other.i)) / conjugate); diff --git a/symengine/cwrapper.cpp b/symengine/cwrapper.cpp index 1b0011ad0d..0fba61f57f 100644 --- a/symengine/cwrapper.cpp +++ b/symengine/cwrapper.cpp @@ -7,8 +7,6 @@ #include #include #include -#include - #define xstr(s) str(s) #define str(s) #s diff --git a/symengine/cwrapper.h b/symengine/cwrapper.h index d81d56e431..287f0ff247 100644 --- a/symengine/cwrapper.h +++ b/symengine/cwrapper.h @@ -28,6 +28,8 @@ extern "C" { typedef int CWRAPPER_OUTPUT_TYPE; +#include "symengine/symengine_exception.h" + typedef enum { #define SYMENGINE_INCLUDE_ALL #define SYMENGINE_ENUM(type, Class) SYMENGINE_##type, diff --git a/symengine/integer.cpp b/symengine/integer.cpp index 1f719eb10d..7180631ca7 100644 --- a/symengine/integer.cpp +++ b/symengine/integer.cpp @@ -45,7 +45,7 @@ signed long int Integer::as_int() const RCP Integer::divint(const Integer &other) const { if (other.i == 0) - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); rational_class q(this->i, other.i); // This is potentially slow, but has to be done, since q might not @@ -59,7 +59,7 @@ RCP Integer::rdiv(const Number &other) const { if (is_a(other)) { if (this->i == 0) { - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); } rational_class q((static_cast(other)).i, this->i); diff --git a/symengine/mul.cpp b/symengine/mul.cpp index 12b926f041..c3d24e62b0 100644 --- a/symengine/mul.cpp +++ b/symengine/mul.cpp @@ -423,7 +423,7 @@ RCP mul(const vec_basic &a) RCP div(const RCP &a, const RCP &b) { if (is_a_Number(*b) and rcp_static_cast(b)->is_zero()) - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); return mul(a, pow(b, minus_one)); } diff --git a/symengine/rational.cpp b/symengine/rational.cpp index 6bdbca2a95..ffd887cf75 100644 --- a/symengine/rational.cpp +++ b/symengine/rational.cpp @@ -38,7 +38,7 @@ RCP Rational::from_mpq(rational_class i) RCP Rational::from_two_ints(const Integer &n, const Integer &d) { if (d.i == 0) - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); rational_class q(n.i, d.i); // This is potentially slow, but has to be done, since 'n/d' might not be @@ -51,7 +51,7 @@ RCP Rational::from_two_ints(const Integer &n, const Integer &d) RCP Rational::from_two_ints(long n, long d) { if (d == 0) - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); rational_class q(n, d); // This is potentially slow, but has to be done, since 'n/d' might not be diff --git a/symengine/rational.h b/symengine/rational.h index c80c3929de..ab7b93e029 100644 --- a/symengine/rational.h +++ b/symengine/rational.h @@ -141,7 +141,7 @@ class Rational : public Number inline RCP divrat(const Rational &other) const { if (other.i == 0) { - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); } else { return from_mpq(this->i / other.i); } @@ -152,7 +152,7 @@ class Rational : public Number inline RCP divrat(const Integer &other) const { if (other.i == 0) { - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); } else { return from_mpq(this->i / other.i); } @@ -160,7 +160,7 @@ class Rational : public Number inline RCP rdivrat(const Integer &other) const { if (this->i == 0) { - throw SymEngineException("Division By Zero", DIV_BY_ZERO); + throw DivisionByZero("Division By Zero"); } else { return from_mpq(other.i / this->i); } diff --git a/symengine/series.h b/symengine/series.h index e724081ad3..e349b382f1 100644 --- a/symengine/series.h +++ b/symengine/series.h @@ -188,8 +188,7 @@ class SeriesBase : public SeriesCoeffInterface unsigned int prec) { if (s == 0) - throw SymEngineException("Series::series_invert: Division By Zero", - DIV_BY_ZERO); + throw DivisionByZero("Series::series_invert: Division By Zero"); if (s == 1) return Poly(1); const short ldeg = Series::ldegree(s); diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index c90ec3e3cc..ba5ed093f4 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -1,12 +1,14 @@ #ifndef SYMENGINE_EXCEPTION_H #define SYMENGINE_EXCEPTION_H -enum symengine_exceptions_t { - NO_EXCEPTION, - DIV_BY_ZERO, - NOT_IMPLEMENTED, - UNDEFINED -}; +typedef enum { + SYMENGINE_NO_EXCEPTION = 0, + SYMENGINE_DIV_BY_ZERO, + SYMENGINE_NOT_IMPLEMENTED, + SYMENGINE_UNDEFINED +} symengine_exceptions_t; + +#ifdef __cplusplus namespace SymEngine { @@ -21,7 +23,7 @@ class SymEngineException : public std::exception : m_msg(msg), ec(error) { } - virtual const char *what() const throw() + const char *what() const throw() { return m_msg.c_str(); } @@ -30,5 +32,23 @@ class SymEngineException : public std::exception return ec; } }; + +class DivisionByZero : public SymEngineException +{ +public: + DivisionByZero(const std::string &msg) + : SymEngineException(msg, SYMENGINE_DIV_BY_ZERO) + { + } +}; + +class NotImplemented : public SymEngineException +{ + NotImplemented(const std::string &msg) + : SymEngineException(msg, SYMENGINE_NOT_IMPLEMENTED) + { + } +}; } +#endif // __cplusplus #endif // SYMENGINE_EXCEPTION_H diff --git a/symengine/tests/basic/test_basic.cpp b/symengine/tests/basic/test_basic.cpp index 56728230a7..1093a25989 100644 --- a/symengine/tests/basic/test_basic.cpp +++ b/symengine/tests/basic/test_basic.cpp @@ -41,7 +41,7 @@ using SymEngine::rational_class; using SymEngine::pi; using SymEngine::diff; using SymEngine::sdiff; -using SymEngine::SymEngineException; +using SymEngine::DivisionByZero; using namespace SymEngine::literals; @@ -310,7 +310,7 @@ TEST_CASE("Integer: Basic", "[basic]") REQUIRE(eq(*k, *integer(-5))); REQUIRE(neq(*k, *integer(12))); - CHECK_THROWS_AS(divnum(i, zero), SymEngineException); + CHECK_THROWS_AS(divnum(i, zero), DivisionByZero); } TEST_CASE("Rational: Basic", "[basic]") @@ -413,7 +413,7 @@ TEST_CASE("Rational: Basic", "[basic]") r1 = Rational::from_two_ints(*integer(2), *integer(3)); r2 = zero; - CHECK_THROWS_AS(divnum(r1, r2), SymEngineException); + CHECK_THROWS_AS(divnum(r1, r2), DivisionByZero); r1 = Rational::from_two_ints(*integer(3), *integer(5)); REQUIRE(is_a(*r1)); @@ -460,7 +460,7 @@ TEST_CASE("Mul: Basic", "[basic]") r = div(x, x); REQUIRE(unified_eq(r->get_args(), {})); - CHECK_THROWS_AS(div(integer(1), zero), SymEngineException); + CHECK_THROWS_AS(div(integer(1), zero), DivisionByZero); r = mul(mul(mul(x, y), mul(x, integer(2))), integer(3)); RCP mr = rcp_static_cast(r); @@ -790,13 +790,13 @@ TEST_CASE("Complex: Basic", "[basic]") REQUIRE(eq(*c->imaginary_part(), *r2)); // Explicit division by zero checks - CHECK_THROWS_AS(divnum(c1, integer(0)), SymEngineException); + CHECK_THROWS_AS(divnum(c1, integer(0)), DivisionByZero); r3 = Rational::from_two_ints(*integer(0), *integer(1)); - CHECK_THROWS_AS(divnum(c1, r3), SymEngineException); + CHECK_THROWS_AS(divnum(c1, r3), DivisionByZero); c2 = Complex::from_two_nums(*r3, *r3); - CHECK_THROWS_AS(divnum(c1, c2), SymEngineException); + CHECK_THROWS_AS(divnum(c1, c2), DivisionByZero); } TEST_CASE("has_symbol: Basic", "[basic]") diff --git a/symengine/tests/basic/test_functions.cpp b/symengine/tests/basic/test_functions.cpp index c7a2a2f04f..3e60b3107c 100644 --- a/symengine/tests/basic/test_functions.cpp +++ b/symengine/tests/basic/test_functions.cpp @@ -94,7 +94,7 @@ using SymEngine::rcp_static_cast; using SymEngine::I; using SymEngine::integer_class; using SymEngine::get_mpz_t; -using SymEngine::SymEngineException; +using SymEngine::DivisionByZero; using namespace SymEngine::literals; @@ -450,7 +450,7 @@ TEST_CASE("Tan: functions", "[functions]") r2 = cot(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), SymEngineException); + CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), DivisionByZero); } TEST_CASE("Cot: functions", "[functions]") @@ -550,7 +550,7 @@ TEST_CASE("Cot: functions", "[functions]") r2 = tan(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(cot(mul(integer(7), pi)), SymEngineException); + CHECK_THROWS_AS(cot(mul(integer(7), pi)), DivisionByZero); } TEST_CASE("Csc: functions", "[functions]") @@ -650,8 +650,8 @@ TEST_CASE("Csc: functions", "[functions]") r2 = sec(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(csc(mul(integer(7), pi)), SymEngineException); - CHECK_THROWS_AS(csc(integer(0)), SymEngineException); + CHECK_THROWS_AS(csc(mul(integer(7), pi)), DivisionByZero); + CHECK_THROWS_AS(csc(integer(0)), DivisionByZero); } TEST_CASE("Sec: functions", "[functions]") @@ -755,7 +755,7 @@ TEST_CASE("Sec: functions", "[functions]") r2 = csc(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), SymEngineException); + CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), DivisionByZero); } TEST_CASE("TrigFunction: trig_to_sqrt", "[functions]") From 53d89c431061ec31960f35cda4281a475d438f11 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sun, 31 Jul 2016 00:30:08 +0530 Subject: [PATCH 11/19] ParseErrors for exception handling --- symengine/parser.cpp | 11 ++++++----- symengine/symengine_exception.h | 17 ++++++++++++++--- symengine/tests/basic/test_parser.cpp | 16 +++++++++------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/symengine/parser.cpp b/symengine/parser.cpp index fcf518b28e..3bc28601b1 100644 --- a/symengine/parser.cpp +++ b/symengine/parser.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace SymEngine { @@ -126,7 +127,7 @@ class ExpressionParser // a parse_string is called empty in scenarios like "x+" if (l == h) - throw std::runtime_error("Expected token!"); + throw ParseError("Expected token!"); for (unsigned int iter = l; iter < h; ++iter) { if (is_operator(iter)) { @@ -248,7 +249,7 @@ class ExpressionParser } else { if (num_dots > 1) { - throw std::runtime_error("Invalid symbol or number!"); + throw ParseError("Invalid symbol or number!"); } else if (num_dots == 1) { return real_double(std::atof(expr.c_str())); @@ -328,7 +329,7 @@ class ExpressionParser // this should never happen, every '(' should have a // matcihng ')' in the bracket stack if (operator_end[i] == (int)s_len) - throw std::runtime_error("Mismatching parantheses!"); + throw ParseError("Mismatching parantheses!"); right_bracket.pop(); op_stack.pop(); @@ -353,7 +354,7 @@ class ExpressionParser } if (last_char_was_op and operator_error(last_char, x)) - throw std::runtime_error("Operator inconsistency!"); + throw ParseError("Operator inconsistency!"); last_char_was_op = true; } else { @@ -364,7 +365,7 @@ class ExpressionParser } // extra right_brackets in the string if (right_bracket.top() != s_len) - throw std::runtime_error("Mismatching parantheses!"); + throw ParseError("Mismatching parantheses!"); // final answer is parse_string from [0, len) return parse_string(0, s_len); diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index ba5ed093f4..405e9906f5 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -3,9 +3,10 @@ typedef enum { SYMENGINE_NO_EXCEPTION = 0, - SYMENGINE_DIV_BY_ZERO, - SYMENGINE_NOT_IMPLEMENTED, - SYMENGINE_UNDEFINED + SYMENGINE_DIV_BY_ZERO = 1, + SYMENGINE_NOT_IMPLEMENTED = 2, + SYMENGINE_UNDEFINED = 3, + SYMENGINE_PARSE_ERROR = 4, } symengine_exceptions_t; #ifdef __cplusplus @@ -44,11 +45,21 @@ class DivisionByZero : public SymEngineException class NotImplemented : public SymEngineException { +public: NotImplemented(const std::string &msg) : SymEngineException(msg, SYMENGINE_NOT_IMPLEMENTED) { } }; + +class ParseError : public SymEngineException +{ +public: + ParseError(const std::string &msg) + : SymEngineException(msg, SYMENGINE_PARSE_ERROR) + { + } +}; } #endif // __cplusplus #endif // SYMENGINE_EXCEPTION_H diff --git a/symengine/tests/basic/test_parser.cpp b/symengine/tests/basic/test_parser.cpp index 7e2686aa24..9545a0a58e 100644 --- a/symengine/tests/basic/test_parser.cpp +++ b/symengine/tests/basic/test_parser.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using SymEngine::Basic; using SymEngine::Add; @@ -35,6 +36,7 @@ using SymEngine::loggamma; using SymEngine::gamma; using SymEngine::UIntPoly; using SymEngine::from_basic; +using SymEngine::ParseError; using namespace SymEngine::literals; @@ -331,23 +333,23 @@ TEST_CASE("Parsing: errors", "[parser]") std::string s; s = "x+y+"; - CHECK_THROWS_AS(parse(s), std::runtime_error); + CHECK_THROWS_AS(parse(s), ParseError); s = "x + (y))"; - CHECK_THROWS_AS(parse(s), std::runtime_error); + CHECK_THROWS_AS(parse(s), ParseError); s = "x + max((3, 2+1)"; - CHECK_THROWS_AS(parse(s), std::runtime_error); + CHECK_THROWS_AS(parse(s), ParseError); s = "2..33 + 2"; - CHECK_THROWS_AS(parse(s), std::runtime_error); + CHECK_THROWS_AS(parse(s), ParseError); s = "2 +- 3"; - CHECK_THROWS_AS(parse(s), std::runtime_error); + CHECK_THROWS_AS(parse(s), ParseError); s = "(2)(3)"; - CHECK_THROWS_AS(parse(s), std::runtime_error); + CHECK_THROWS_AS(parse(s), ParseError); s = "max(,3,2)"; - CHECK_THROWS_AS(parse(s), std::runtime_error); + CHECK_THROWS_AS(parse(s), ParseError); } From f2b14528f1f3a5f9de3cdf2a30e207ca11eefa6e Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Sun, 31 Jul 2016 15:42:39 +0530 Subject: [PATCH 12/19] Renaming exceptions --- symengine/complex.h | 6 +++--- symengine/integer.cpp | 4 ++-- symengine/mul.cpp | 2 +- symengine/rational.cpp | 4 ++-- symengine/rational.h | 6 +++--- symengine/series.h | 3 ++- symengine/symengine_exception.h | 8 ++++---- symengine/tests/basic/test_basic.cpp | 14 +++++++------- symengine/tests/basic/test_functions.cpp | 12 ++++++------ 9 files changed, 30 insertions(+), 29 deletions(-) diff --git a/symengine/complex.h b/symengine/complex.h index 1897ad5a30..c5ea8efced 100644 --- a/symengine/complex.h +++ b/symengine/complex.h @@ -204,7 +204,7 @@ class Complex : public Number inline RCP divcomp(const Rational &other) const { if (other.is_zero()) { - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -215,7 +215,7 @@ class Complex : public Number inline RCP divcomp(const Integer &other) const { if (other.is_zero()) { - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); } else { return from_mpq(this->real_ / other.i, this->imaginary_ / other.i); } @@ -228,7 +228,7 @@ class Complex : public Number rational_class conjugate = this->real_ * this->real_ + this->imaginary_ * this->imaginary_; if (get_num(conjugate) == 0) { - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); } else { return from_mpq((this->real_ * other.i) / conjugate, (this->imaginary_ * (-other.i)) / conjugate); diff --git a/symengine/integer.cpp b/symengine/integer.cpp index 7180631ca7..2169c30b44 100644 --- a/symengine/integer.cpp +++ b/symengine/integer.cpp @@ -45,7 +45,7 @@ signed long int Integer::as_int() const RCP Integer::divint(const Integer &other) const { if (other.i == 0) - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); rational_class q(this->i, other.i); // This is potentially slow, but has to be done, since q might not @@ -59,7 +59,7 @@ RCP Integer::rdiv(const Number &other) const { if (is_a(other)) { if (this->i == 0) { - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); } rational_class q((static_cast(other)).i, this->i); diff --git a/symengine/mul.cpp b/symengine/mul.cpp index c3d24e62b0..1f1c7a3fa1 100644 --- a/symengine/mul.cpp +++ b/symengine/mul.cpp @@ -423,7 +423,7 @@ RCP mul(const vec_basic &a) RCP div(const RCP &a, const RCP &b) { if (is_a_Number(*b) and rcp_static_cast(b)->is_zero()) - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); return mul(a, pow(b, minus_one)); } diff --git a/symengine/rational.cpp b/symengine/rational.cpp index ffd887cf75..abe42c4ec0 100644 --- a/symengine/rational.cpp +++ b/symengine/rational.cpp @@ -38,7 +38,7 @@ RCP Rational::from_mpq(rational_class i) RCP Rational::from_two_ints(const Integer &n, const Integer &d) { if (d.i == 0) - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); rational_class q(n.i, d.i); // This is potentially slow, but has to be done, since 'n/d' might not be @@ -51,7 +51,7 @@ RCP Rational::from_two_ints(const Integer &n, const Integer &d) RCP Rational::from_two_ints(long n, long d) { if (d == 0) - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); rational_class q(n, d); // This is potentially slow, but has to be done, since 'n/d' might not be diff --git a/symengine/rational.h b/symengine/rational.h index ab7b93e029..41a3b2e416 100644 --- a/symengine/rational.h +++ b/symengine/rational.h @@ -141,7 +141,7 @@ class Rational : public Number inline RCP divrat(const Rational &other) const { if (other.i == 0) { - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); } else { return from_mpq(this->i / other.i); } @@ -152,7 +152,7 @@ class Rational : public Number inline RCP divrat(const Integer &other) const { if (other.i == 0) { - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); } else { return from_mpq(this->i / other.i); } @@ -160,7 +160,7 @@ class Rational : public Number inline RCP rdivrat(const Integer &other) const { if (this->i == 0) { - throw DivisionByZero("Division By Zero"); + throw DivisionByZeroError("Division By Zero"); } else { return from_mpq(other.i / this->i); } diff --git a/symengine/series.h b/symengine/series.h index e349b382f1..0012fc066b 100644 --- a/symengine/series.h +++ b/symengine/series.h @@ -188,7 +188,8 @@ class SeriesBase : public SeriesCoeffInterface unsigned int prec) { if (s == 0) - throw DivisionByZero("Series::series_invert: Division By Zero"); + throw DivisionByZeroError( + "Series::series_invert: Division By Zero"); if (s == 1) return Poly(1); const short ldeg = Series::ldegree(s); diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index 405e9906f5..441a9b9897 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -34,19 +34,19 @@ class SymEngineException : public std::exception } }; -class DivisionByZero : public SymEngineException +class DivisionByZeroError : public SymEngineException { public: - DivisionByZero(const std::string &msg) + DivisionByZeroError(const std::string &msg) : SymEngineException(msg, SYMENGINE_DIV_BY_ZERO) { } }; -class NotImplemented : public SymEngineException +class NotImplementedError : public SymEngineException { public: - NotImplemented(const std::string &msg) + NotImplementedError(const std::string &msg) : SymEngineException(msg, SYMENGINE_NOT_IMPLEMENTED) { } diff --git a/symengine/tests/basic/test_basic.cpp b/symengine/tests/basic/test_basic.cpp index 1093a25989..47b1976d4e 100644 --- a/symengine/tests/basic/test_basic.cpp +++ b/symengine/tests/basic/test_basic.cpp @@ -41,7 +41,7 @@ using SymEngine::rational_class; using SymEngine::pi; using SymEngine::diff; using SymEngine::sdiff; -using SymEngine::DivisionByZero; +using SymEngine::DivisionByZeroError; using namespace SymEngine::literals; @@ -310,7 +310,7 @@ TEST_CASE("Integer: Basic", "[basic]") REQUIRE(eq(*k, *integer(-5))); REQUIRE(neq(*k, *integer(12))); - CHECK_THROWS_AS(divnum(i, zero), DivisionByZero); + CHECK_THROWS_AS(divnum(i, zero), DivisionByZeroError); } TEST_CASE("Rational: Basic", "[basic]") @@ -413,7 +413,7 @@ TEST_CASE("Rational: Basic", "[basic]") r1 = Rational::from_two_ints(*integer(2), *integer(3)); r2 = zero; - CHECK_THROWS_AS(divnum(r1, r2), DivisionByZero); + CHECK_THROWS_AS(divnum(r1, r2), DivisionByZeroError); r1 = Rational::from_two_ints(*integer(3), *integer(5)); REQUIRE(is_a(*r1)); @@ -460,7 +460,7 @@ TEST_CASE("Mul: Basic", "[basic]") r = div(x, x); REQUIRE(unified_eq(r->get_args(), {})); - CHECK_THROWS_AS(div(integer(1), zero), DivisionByZero); + CHECK_THROWS_AS(div(integer(1), zero), DivisionByZeroError); r = mul(mul(mul(x, y), mul(x, integer(2))), integer(3)); RCP mr = rcp_static_cast(r); @@ -790,13 +790,13 @@ TEST_CASE("Complex: Basic", "[basic]") REQUIRE(eq(*c->imaginary_part(), *r2)); // Explicit division by zero checks - CHECK_THROWS_AS(divnum(c1, integer(0)), DivisionByZero); + CHECK_THROWS_AS(divnum(c1, integer(0)), DivisionByZeroError); r3 = Rational::from_two_ints(*integer(0), *integer(1)); - CHECK_THROWS_AS(divnum(c1, r3), DivisionByZero); + CHECK_THROWS_AS(divnum(c1, r3), DivisionByZeroError); c2 = Complex::from_two_nums(*r3, *r3); - CHECK_THROWS_AS(divnum(c1, c2), DivisionByZero); + CHECK_THROWS_AS(divnum(c1, c2), DivisionByZeroError); } TEST_CASE("has_symbol: Basic", "[basic]") diff --git a/symengine/tests/basic/test_functions.cpp b/symengine/tests/basic/test_functions.cpp index 3e60b3107c..d23ff070a3 100644 --- a/symengine/tests/basic/test_functions.cpp +++ b/symengine/tests/basic/test_functions.cpp @@ -94,7 +94,7 @@ using SymEngine::rcp_static_cast; using SymEngine::I; using SymEngine::integer_class; using SymEngine::get_mpz_t; -using SymEngine::DivisionByZero; +using SymEngine::DivisionByZeroError; using namespace SymEngine::literals; @@ -450,7 +450,7 @@ TEST_CASE("Tan: functions", "[functions]") r2 = cot(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), DivisionByZero); + CHECK_THROWS_AS(tan(mul(integer(5), div(pi, i2))), DivisionByZeroError); } TEST_CASE("Cot: functions", "[functions]") @@ -550,7 +550,7 @@ TEST_CASE("Cot: functions", "[functions]") r2 = tan(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(cot(mul(integer(7), pi)), DivisionByZero); + CHECK_THROWS_AS(cot(mul(integer(7), pi)), DivisionByZeroError); } TEST_CASE("Csc: functions", "[functions]") @@ -650,8 +650,8 @@ TEST_CASE("Csc: functions", "[functions]") r2 = sec(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(csc(mul(integer(7), pi)), DivisionByZero); - CHECK_THROWS_AS(csc(integer(0)), DivisionByZero); + CHECK_THROWS_AS(csc(mul(integer(7), pi)), DivisionByZeroError); + CHECK_THROWS_AS(csc(integer(0)), DivisionByZeroError); } TEST_CASE("Sec: functions", "[functions]") @@ -755,7 +755,7 @@ TEST_CASE("Sec: functions", "[functions]") r2 = csc(y); REQUIRE(eq(*r1, *r2)); - CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), DivisionByZero); + CHECK_THROWS_AS(sec(mul(integer(7), div(pi, i2))), DivisionByZeroError); } TEST_CASE("TrigFunction: trig_to_sqrt", "[functions]") From 5bc99a6f0c0909e1ca171fb739640d282e1771b5 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Wed, 3 Aug 2016 23:40:27 +0530 Subject: [PATCH 13/19] Renaming error codes --- symengine/symengine_exception.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index 441a9b9897..9e2e14e4b6 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -2,11 +2,11 @@ #define SYMENGINE_EXCEPTION_H typedef enum { - SYMENGINE_NO_EXCEPTION = 0, - SYMENGINE_DIV_BY_ZERO = 1, - SYMENGINE_NOT_IMPLEMENTED = 2, - SYMENGINE_UNDEFINED = 3, - SYMENGINE_PARSE_ERROR = 4, + ESYMENGINE_NO_EXCEPTION = 0, + ESYMENGINE_DIV_BY_ZERO = 1, + ESYMENGINE_NOT_IMPLEMENTED = 2, + ESYMENGINE_UNDEFINED = 3, + ESYMENGINE_PARSE_ERROR = 4, } symengine_exceptions_t; #ifdef __cplusplus From e4bc43eb2cbb0baf5c5699a180b709c55a243c19 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Wed, 3 Aug 2016 23:58:43 +0530 Subject: [PATCH 14/19] NotImplementedError --- symengine/basic.h | 2 +- symengine/complex.h | 6 ++--- symengine/complex_double.h | 6 ++--- symengine/complex_mpc.h | 6 ++--- symengine/dense_matrix.cpp | 2 +- symengine/eval_arb.cpp | 46 ++++++++++++++++++------------------- symengine/eval_double.cpp | 4 ++-- symengine/eval_mpc.cpp | 2 +- symengine/eval_mpfr.cpp | 2 +- symengine/functions.cpp | 8 +++---- symengine/integer.cpp | 2 +- symengine/integer.h | 4 ++-- symengine/lambda_double.h | 2 +- symengine/rational.h | 6 ++--- symengine/real_double.h | 6 ++--- symengine/real_mpfr.h | 6 ++--- symengine/rings.cpp | 14 +++++------ symengine/sparse_matrix.cpp | 28 +++++++++++----------- 18 files changed, 76 insertions(+), 76 deletions(-) diff --git a/symengine/basic.h b/symengine/basic.h index a27bc236dd..437f17e16e 100644 --- a/symengine/basic.h +++ b/symengine/basic.h @@ -155,7 +155,7 @@ class Basic : public EnableRCPFromThis //! expands the special function in terms of exp function virtual RCP expand_as_exp() const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } //! Returns the list of arguments diff --git a/symengine/complex.h b/symengine/complex.h index c5ea8efced..fa526e66f2 100644 --- a/symengine/complex.h +++ b/symengine/complex.h @@ -273,7 +273,7 @@ class Complex : public Number } else if (is_a(other)) { return rsubcomp(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } }; //! Converts the param `other` appropriately and then calls `mulcomp` @@ -308,7 +308,7 @@ class Complex : public Number if (is_a(other)) { return rdivcomp(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } }; //! Converts the param `other` appropriately and then calls `powcomp` @@ -335,7 +335,7 @@ class Complex : public Number virtual RCP rpow(const Number &other) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; }; diff --git a/symengine/complex_double.h b/symengine/complex_double.h index f623dc4028..670e5e1ec9 100644 --- a/symengine/complex_double.h +++ b/symengine/complex_double.h @@ -242,7 +242,7 @@ class ComplexDouble : public Number } else if (is_a(other)) { return rsubcomp(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -413,7 +413,7 @@ class ComplexDouble : public Number } else if (is_a(other)) { return rdivcomp(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -530,7 +530,7 @@ class ComplexDouble : public Number } else if (is_a(other)) { return rpowcomp(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } }; diff --git a/symengine/complex_mpc.h b/symengine/complex_mpc.h index a01281eb0c..9cd8ac8d58 100644 --- a/symengine/complex_mpc.h +++ b/symengine/complex_mpc.h @@ -229,7 +229,7 @@ class ComplexMPC : public Number } else if (is_a(other)) { return rsub(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -316,7 +316,7 @@ class ComplexMPC : public Number } else if (is_a(other)) { return rdiv(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -373,7 +373,7 @@ class ComplexMPC : public Number } else if (is_a(other)) { return rpow(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } }; diff --git a/symengine/dense_matrix.cpp b/symengine/dense_matrix.cpp index 5b619fbb98..349989d7b4 100644 --- a/symengine/dense_matrix.cpp +++ b/symengine/dense_matrix.cpp @@ -50,7 +50,7 @@ void DenseMatrix::set(unsigned i, unsigned j, const RCP &e) unsigned DenseMatrix::rank() const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } RCP DenseMatrix::det() const diff --git a/symengine/eval_arb.cpp b/symengine/eval_arb.cpp index 3733c0194f..e17ef8e8c8 100644 --- a/symengine/eval_arb.cpp +++ b/symengine/eval_arb.cpp @@ -132,27 +132,27 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const UIntPoly &x) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void bvisit(const Complex &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void bvisit(const ComplexDouble &x) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void bvisit(const RealMPFR &x) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } #ifdef HAVE_SYMENGINE_MPC void bvisit(const ComplexMPC &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; #endif void bvisit(const Log &x) @@ -163,7 +163,7 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const Derivative &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void bvisit(const Cot &x) @@ -239,7 +239,7 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const LambertW &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void bvisit(const FunctionWrapper &x) @@ -255,7 +255,7 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const Csch &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const Cosh &x) @@ -266,7 +266,7 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const Sech &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const Tanh &x) @@ -323,43 +323,43 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const ASinh &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const ACsch &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const ACosh &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const ATanh &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const ACoth &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const ASech &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const KroneckerDelta &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const LeviCivita &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const Zeta &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const Dirichlet_eta &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const Gamma &x) { @@ -373,11 +373,11 @@ class EvalArbVisitor : public BaseVisitor } void bvisit(const LowerGamma &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const UpperGamma &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const Constant &x) @@ -396,12 +396,12 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const Abs &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const Basic &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const NumberWrapper &x) diff --git a/symengine/eval_double.cpp b/symengine/eval_double.cpp index 8ec4d1ca72..9538e444be 100644 --- a/symengine/eval_double.cpp +++ b/symengine/eval_double.cpp @@ -253,7 +253,7 @@ class EvalDoubleVisitor : public BaseVisitor void bvisit(const Basic &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; void bvisit(const NumberWrapper &x) @@ -389,7 +389,7 @@ std::vector init_eval_double() { std::vector table; table.assign(TypeID_Count, [](const Basic &x) -> double { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }); table[INTEGER] = [](const Basic &x) { double tmp = mp_get_d((static_cast(x)).i); diff --git a/symengine/eval_mpc.cpp b/symengine/eval_mpc.cpp index 1fc904188e..c85f7e4743 100644 --- a/symengine/eval_mpc.cpp +++ b/symengine/eval_mpc.cpp @@ -329,7 +329,7 @@ class EvalMPCVisitor : public BaseVisitor // Derivative, ATan2, Gamma void bvisit(const Basic &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; }; diff --git a/symengine/eval_mpfr.cpp b/symengine/eval_mpfr.cpp index 8ce5c8f67d..3705ef2b99 100644 --- a/symengine/eval_mpfr.cpp +++ b/symengine/eval_mpfr.cpp @@ -335,7 +335,7 @@ class EvalMPFRVisitor : public BaseVisitor // Derivative, Complex, ComplexDouble, ComplexMPC void bvisit(const Basic &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; }; diff --git a/symengine/functions.cpp b/symengine/functions.cpp index 0f6ce4bc5a..67267cf58f 100644 --- a/symengine/functions.cpp +++ b/symengine/functions.cpp @@ -1050,7 +1050,7 @@ RCP atan2(const RCP &num, const RCP &den) return zero; // else it is NAN, yet to be implemented else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } } else if (eq(*den, *zero)) { @@ -1062,7 +1062,7 @@ RCP atan2(const RCP &num, const RCP &den) return div(pi, im2); // else it is NAN, yet to be implemented else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } } @@ -1515,7 +1515,7 @@ RCP csch(const RCP &arg) { if (eq(*arg, *zero)) { // Answer is infinity. Yet to be implemented in SymEngine - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } if (is_a_Number(*arg)) { RCP _arg = rcp_static_cast(arg); @@ -1703,7 +1703,7 @@ RCP coth(const RCP &arg) { if (eq(*arg, *zero)) { // Answer is infinity. Yet to be implemented in SymEngine - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } if (is_a_Number(*arg)) { RCP _arg = rcp_static_cast(arg); diff --git a/symengine/integer.cpp b/symengine/integer.cpp index 2169c30b44..de2710f9c0 100644 --- a/symengine/integer.cpp +++ b/symengine/integer.cpp @@ -69,7 +69,7 @@ RCP Integer::rdiv(const Number &other) const return Rational::from_mpq(std::move(q)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } }; diff --git a/symengine/integer.h b/symengine/integer.h index b1e26dc825..218286565d 100644 --- a/symengine/integer.h +++ b/symengine/integer.h @@ -128,7 +128,7 @@ class Integer : public Number virtual RCP rsub(const Number &other) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; //! Slower Multiplication @@ -164,7 +164,7 @@ class Integer : public Number virtual RCP rpow(const Number &other) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; }; diff --git a/symengine/lambda_double.h b/symengine/lambda_double.h index 67c1e6d2dd..7825fdc8ad 100644 --- a/symengine/lambda_double.h +++ b/symengine/lambda_double.h @@ -303,7 +303,7 @@ class LambdaDoubleVisitor : public BaseVisitor> void bvisit(const Basic &) { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; }; diff --git a/symengine/rational.h b/symengine/rational.h index 41a3b2e416..0e02d7956d 100644 --- a/symengine/rational.h +++ b/symengine/rational.h @@ -226,7 +226,7 @@ class Rational : public Number if (is_a(other)) { return rsubrat(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } }; //! Converts the param `other` appropriately and then calls `mulrat` @@ -257,7 +257,7 @@ class Rational : public Number if (is_a(other)) { return rdivrat(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } }; //! Converts the param `other` appropriately and then calls `powrat` @@ -272,7 +272,7 @@ class Rational : public Number virtual RCP rpow(const Number &other) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); }; RCP get_num() const diff --git a/symengine/real_double.h b/symengine/real_double.h index f8514af075..616ae363c5 100644 --- a/symengine/real_double.h +++ b/symengine/real_double.h @@ -206,7 +206,7 @@ class RealDouble : public Number } else if (is_a(other)) { return rsubreal(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -344,7 +344,7 @@ class RealDouble : public Number } else if (is_a(other)) { return rdivreal(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -446,7 +446,7 @@ class RealDouble : public Number } else if (is_a(other)) { return rpowreal(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } }; diff --git a/symengine/real_mpfr.h b/symengine/real_mpfr.h index 3ca3ba0a84..a67cb2b886 100644 --- a/symengine/real_mpfr.h +++ b/symengine/real_mpfr.h @@ -216,7 +216,7 @@ class RealMPFR : public Number } else if (is_a(other)) { return rsubreal(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -294,7 +294,7 @@ class RealMPFR : public Number } else if (is_a(other)) { return rdivreal(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } @@ -345,7 +345,7 @@ class RealMPFR : public Number } else if (is_a(other)) { return rpowreal(static_cast(other)); } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } }; diff --git a/symengine/rings.cpp b/symengine/rings.cpp index d17fc7024d..68cb556e48 100644 --- a/symengine/rings.cpp +++ b/symengine/rings.cpp @@ -15,7 +15,7 @@ void expr2poly(const RCP &p, umap_basic_num &syms, umap_vec_mpz &P) integer_class coef; for (const auto &p : d) { if (not is_a(*p.second)) - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); coef = rcp_static_cast(p.second)->as_mpz(); exp.assign(n, 0); // Initialize to [0]*n if (is_a(*p.first)) { @@ -24,7 +24,7 @@ void expr2poly(const RCP &p, umap_basic_num &syms, umap_vec_mpz &P) for (const auto &q : term) { RCP sym = q.first; if (not is_a(*syms.at(sym))) - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); int i = rcp_static_cast(syms.at(sym)) ->as_int(); if (is_a(*q.second)) { @@ -42,25 +42,25 @@ void expr2poly(const RCP &p, umap_basic_num &syms, umap_vec_mpz &P) RCP exp_ = rcp_static_cast(p.first)->get_exp(); if (not is_a(*syms.at(sym))) - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); int i = rcp_static_cast(syms.at(sym))->as_int(); if (not is_a(*exp_)) - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); exp[i] = rcp_static_cast(exp_)->as_int(); } else if (is_a(*p.first)) { RCP sym = p.first; if (not is_a(*syms.at(sym))) - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); int i = rcp_static_cast(syms.at(sym))->as_int(); exp[i] = 1; } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } P[exp] = coef; } } else { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } } diff --git a/symengine/sparse_matrix.cpp b/symengine/sparse_matrix.cpp index 55fdf0bb55..90837c3df9 100644 --- a/symengine/sparse_matrix.cpp +++ b/symengine/sparse_matrix.cpp @@ -133,45 +133,45 @@ void CSRMatrix::set(unsigned i, unsigned j, const RCP &e) unsigned CSRMatrix::rank() const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } RCP CSRMatrix::det() const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void CSRMatrix::inv(MatrixBase &result) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void CSRMatrix::add_matrix(const MatrixBase &other, MatrixBase &result) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void CSRMatrix::mul_matrix(const MatrixBase &other, MatrixBase &result) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // Add a scalar void CSRMatrix::add_scalar(const RCP &k, MatrixBase &result) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // Multiply by a scalar void CSRMatrix::mul_scalar(const RCP &k, MatrixBase &result) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // Matrix transpose void CSRMatrix::transpose(MatrixBase &result) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // Extract out a submatrix @@ -180,37 +180,37 @@ void CSRMatrix::submatrix(MatrixBase &result, unsigned row_start, unsigned col_end, unsigned row_step, unsigned col_step) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // LU factorization void CSRMatrix::LU(MatrixBase &L, MatrixBase &U) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // LDL factorization void CSRMatrix::LDL(MatrixBase &L, MatrixBase &D) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // Solve Ax = b using LU factorization void CSRMatrix::LU_solve(const MatrixBase &b, MatrixBase &x) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // Fraction free LU factorization void CSRMatrix::FFLU(MatrixBase &LU) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } // Fraction free LDU factorization void CSRMatrix::FFLDU(MatrixBase &L, MatrixBase &D, MatrixBase &U) const { - throw std::runtime_error("Not implemented."); + throw NotImplementedError("Not Implemented"); } void CSRMatrix::csr_sum_duplicates(std::vector &p_, From d6d04ba3bcc19205e1bc9f944d5fb7088ed0c048 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 4 Aug 2016 10:10:34 +0530 Subject: [PATCH 15/19] NotImplementedError tests --- symengine/basic.h | 1 + symengine/complex_double.h | 1 + symengine/complex_mpc.h | 1 + symengine/dense_matrix.cpp | 1 + symengine/eval_arb.cpp | 1 + symengine/eval_double.cpp | 1 + symengine/eval_mpc.cpp | 1 + symengine/eval_mpfr.cpp | 1 + symengine/functions.cpp | 1 + symengine/integer.h | 1 + symengine/lambda_double.h | 1 + symengine/real_double.h | 1 + symengine/real_mpfr.h | 1 + symengine/rings.cpp | 1 + symengine/sparse_matrix.cpp | 1 + symengine/symengine_exception.h | 10 +++++----- symengine/tests/eval/test_eval_double.cpp | 12 +++++++----- symengine/tests/eval/test_lambda_double.cpp | 6 ++++-- 18 files changed, 31 insertions(+), 12 deletions(-) diff --git a/symengine/basic.h b/symengine/basic.h index 437f17e16e..dd1343f32d 100644 --- a/symengine/basic.h +++ b/symengine/basic.h @@ -23,6 +23,7 @@ #include #include +#include #ifdef WITH_SYMENGINE_THREAD_SAFE #include diff --git a/symengine/complex_double.h b/symengine/complex_double.h index 670e5e1ec9..a02d036983 100644 --- a/symengine/complex_double.h +++ b/symengine/complex_double.h @@ -7,6 +7,7 @@ #define SYMENGINE_COMPLEX_DOUBLE_H #include +#include namespace SymEngine { diff --git a/symengine/complex_mpc.h b/symengine/complex_mpc.h index 9cd8ac8d58..e9f595525e 100644 --- a/symengine/complex_mpc.h +++ b/symengine/complex_mpc.h @@ -7,6 +7,7 @@ #define SYMENGINE_REAL_MPC_H #include +#include #ifdef HAVE_SYMENGINE_MPC #include diff --git a/symengine/dense_matrix.cpp b/symengine/dense_matrix.cpp index 349989d7b4..130b1385a4 100644 --- a/symengine/dense_matrix.cpp +++ b/symengine/dense_matrix.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace SymEngine { diff --git a/symengine/eval_arb.cpp b/symengine/eval_arb.cpp index e17ef8e8c8..f166513444 100644 --- a/symengine/eval_arb.cpp +++ b/symengine/eval_arb.cpp @@ -1,5 +1,6 @@ #include #include +#include #ifdef HAVE_SYMENGINE_ARB diff --git a/symengine/eval_double.cpp b/symengine/eval_double.cpp index 9538e444be..f5bcc37f17 100644 --- a/symengine/eval_double.cpp +++ b/symengine/eval_double.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace SymEngine { diff --git a/symengine/eval_mpc.cpp b/symengine/eval_mpc.cpp index c85f7e4743..78ad551bba 100644 --- a/symengine/eval_mpc.cpp +++ b/symengine/eval_mpc.cpp @@ -1,5 +1,6 @@ #include #include +#include #ifdef HAVE_SYMENGINE_MPC diff --git a/symengine/eval_mpfr.cpp b/symengine/eval_mpfr.cpp index 3705ef2b99..c6a2db0f3a 100644 --- a/symengine/eval_mpfr.cpp +++ b/symengine/eval_mpfr.cpp @@ -1,5 +1,6 @@ #include #include +#include #ifdef HAVE_SYMENGINE_MPFR diff --git a/symengine/functions.cpp b/symengine/functions.cpp index 67267cf58f..9bfcd1faad 100644 --- a/symengine/functions.cpp +++ b/symengine/functions.cpp @@ -1,4 +1,5 @@ #include +#include namespace SymEngine { diff --git a/symengine/integer.h b/symengine/integer.h index 218286565d..03484177e8 100644 --- a/symengine/integer.h +++ b/symengine/integer.h @@ -8,6 +8,7 @@ #define SYMENGINE_INTEGER_H #include +#include namespace SymEngine { diff --git a/symengine/lambda_double.h b/symengine/lambda_double.h index 7825fdc8ad..92e576e56b 100644 --- a/symengine/lambda_double.h +++ b/symengine/lambda_double.h @@ -3,6 +3,7 @@ #include #include +#include namespace SymEngine { diff --git a/symengine/real_double.h b/symengine/real_double.h index 616ae363c5..5e947d52e8 100644 --- a/symengine/real_double.h +++ b/symengine/real_double.h @@ -7,6 +7,7 @@ #define SYMENGINE_REAL_DOUBLE_H #include +#include namespace SymEngine { diff --git a/symengine/real_mpfr.h b/symengine/real_mpfr.h index a67cb2b886..dc699687b6 100644 --- a/symengine/real_mpfr.h +++ b/symengine/real_mpfr.h @@ -7,6 +7,7 @@ #define SYMENGINE_REAL_MPFR_H #include +#include #ifdef HAVE_SYMENGINE_MPFR #include diff --git a/symengine/rings.cpp b/symengine/rings.cpp index 68cb556e48..624dbbf459 100644 --- a/symengine/rings.cpp +++ b/symengine/rings.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace SymEngine { diff --git a/symengine/sparse_matrix.cpp b/symengine/sparse_matrix.cpp index 90837c3df9..88ac40c8c5 100644 --- a/symengine/sparse_matrix.cpp +++ b/symengine/sparse_matrix.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace SymEngine { diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index 9e2e14e4b6..441a9b9897 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -2,11 +2,11 @@ #define SYMENGINE_EXCEPTION_H typedef enum { - ESYMENGINE_NO_EXCEPTION = 0, - ESYMENGINE_DIV_BY_ZERO = 1, - ESYMENGINE_NOT_IMPLEMENTED = 2, - ESYMENGINE_UNDEFINED = 3, - ESYMENGINE_PARSE_ERROR = 4, + SYMENGINE_NO_EXCEPTION = 0, + SYMENGINE_DIV_BY_ZERO = 1, + SYMENGINE_NOT_IMPLEMENTED = 2, + SYMENGINE_UNDEFINED = 3, + SYMENGINE_PARSE_ERROR = 4, } symengine_exceptions_t; #ifdef __cplusplus diff --git a/symengine/tests/eval/test_eval_double.cpp b/symengine/tests/eval/test_eval_double.cpp index fb9b05e935..1266de8e01 100644 --- a/symengine/tests/eval/test_eval_double.cpp +++ b/symengine/tests/eval/test_eval_double.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using SymEngine::Basic; using SymEngine::constant; @@ -51,6 +52,7 @@ using SymEngine::rational_class; using SymEngine::max; using SymEngine::min; using SymEngine::min; +using SymEngine::NotImplementedError; TEST_CASE("eval_double: eval_double", "[eval_double]") { @@ -126,17 +128,17 @@ TEST_CASE("eval_double: eval_double", "[eval_double]") // Symbol must raise an exception CHECK_THROWS_AS(eval_double(*symbol("x")), std::runtime_error); CHECK_THROWS_AS(eval_double_single_dispatch(*symbol("x")), - std::runtime_error); + NotImplementedError); // TODO: this is not implemented yet, so we check that it raises an // exception for now - CHECK_THROWS_AS(eval_double(*levi_civita({r1})), std::runtime_error); + CHECK_THROWS_AS(eval_double(*levi_civita({r1})), NotImplementedError); CHECK_THROWS_AS(eval_double_single_dispatch(*levi_civita({r1})), - std::runtime_error); + NotImplementedError); - CHECK_THROWS_AS(eval_double(*zeta(r1, r2)), std::runtime_error); + CHECK_THROWS_AS(eval_double(*zeta(r1, r2)), NotImplementedError); CHECK_THROWS_AS(eval_double_single_dispatch(*zeta(r1, r2)), - std::runtime_error); + NotImplementedError); CHECK_THROWS_AS(eval_double(*constant("dummy")), std::runtime_error); CHECK_THROWS_AS(eval_double_single_dispatch(*constant("dummy")), diff --git a/symengine/tests/eval/test_lambda_double.cpp b/symengine/tests/eval/test_lambda_double.cpp index bcdb9fc748..d7e626def8 100644 --- a/symengine/tests/eval/test_lambda_double.cpp +++ b/symengine/tests/eval/test_lambda_double.cpp @@ -1,6 +1,7 @@ #include "catch.hpp" #include +#include using SymEngine::Basic; using SymEngine::RCP; @@ -19,6 +20,7 @@ using SymEngine::E; using SymEngine::gamma; using SymEngine::loggamma; using SymEngine::min; +using SymEngine::NotImplementedError; TEST_CASE("Evaluate to double", "[lambda_double]") { @@ -54,7 +56,7 @@ TEST_CASE("Evaluate to double", "[lambda_double]") // Evaluating to double when there are complex doubles raise an exception CHECK_THROWS_AS( v.init({x}, *add(complex_double(std::complex(1, 2)), x)), - std::runtime_error); + NotImplementedError); // Undefined symbols raise an exception CHECK_THROWS_AS(v.init({x}, *r), std::runtime_error); @@ -120,4 +122,4 @@ TEST_CASE("Evaluate functions", "[lambda_gamma]") d = v.call({1.1}); REQUIRE(::fabs(d - 0.88020506957408169) < 1e-12); -} \ No newline at end of file +} From 8a2283d8913f9ffd7367fe99ff3f54407ba20868 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Thu, 4 Aug 2016 13:44:03 +0530 Subject: [PATCH 16/19] SymEngine Exceptions for Errors with specialized messages --- symengine/codegen.h | 3 +- symengine/complex.h | 2 +- symengine/complex_mpc.cpp | 3 +- symengine/fields.cpp | 3 +- symengine/fields.h | 8 ++-- symengine/functions.cpp | 19 ++++----- symengine/infinity.cpp | 5 ++- symengine/number.h | 10 ++--- symengine/pow.cpp | 6 ++- symengine/real_double.cpp | 2 +- symengine/real_mpfr.cpp | 2 +- symengine/series.h | 39 ++++++++++--------- symengine/series_flint.h | 5 ++- symengine/series_generic.cpp | 5 ++- symengine/series_piranha.cpp | 2 +- symengine/sets.cpp | 8 ++-- symengine/symengine_exception.h | 22 +++++++++-- symengine/tests/basic/test_functions.cpp | 3 +- symengine/tests/basic/test_infinity.cpp | 4 +- symengine/tests/basic/test_series_generic.cpp | 7 +++- symengine/tests/basic/test_sets.cpp | 10 +++-- symengine/visitor.cpp | 2 +- 22 files changed, 102 insertions(+), 68 deletions(-) diff --git a/symengine/codegen.h b/symengine/codegen.h index 2472998dc8..4a3a2d22e6 100644 --- a/symengine/codegen.h +++ b/symengine/codegen.h @@ -3,6 +3,7 @@ #include #include +#include namespace SymEngine { @@ -19,7 +20,7 @@ class CodePrinter : public BaseVisitor } void bvisit(const Complex &x) { - throw std::runtime_error("Not implemented"); + throw NotImplementedError("Not implemented"); } void bvisit(const Interval &x) { diff --git a/symengine/complex.h b/symengine/complex.h index fa526e66f2..71d73499ba 100644 --- a/symengine/complex.h +++ b/symengine/complex.h @@ -188,7 +188,7 @@ class Complex : public Number rational_class conjugate = other.real_ * other.real_ + other.imaginary_ * other.imaginary_; if (get_num(conjugate) == 0) { - throw std::runtime_error("Divide by zero."); + throw DivisionByZeroError("Divide by zero."); } else { return from_mpq((this->real_ * other.real_ + this->imaginary_ * other.imaginary_) diff --git a/symengine/complex_mpc.cpp b/symengine/complex_mpc.cpp index 7e5a92e0ef..bce12752a0 100644 --- a/symengine/complex_mpc.cpp +++ b/symengine/complex_mpc.cpp @@ -4,6 +4,7 @@ * **/ #include +#include #ifdef HAVE_SYMENGINE_MPC namespace SymEngine @@ -853,7 +854,7 @@ class EvaluateMPC : public Evaluate } virtual RCP gamma(Basic const &aConst) const { - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); } }; diff --git a/symengine/fields.cpp b/symengine/fields.cpp index bcdb0b1472..7d4a700543 100644 --- a/symengine/fields.cpp +++ b/symengine/fields.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace SymEngine { @@ -107,7 +108,7 @@ void GaloisFieldDict::gf_div(const GaloisFieldDict &o, if (modulo_ != o.modulo_) throw std::runtime_error("Error: field must be same."); if (o.dict_.empty()) - throw std::runtime_error("ZeroDivisionError"); + throw DivisionByZeroError("ZeroDivisionError"); std::vector dict_out; if (dict_.empty()) { *quo = GaloisFieldDict::from_vec(dict_out, modulo_); diff --git a/symengine/fields.h b/symengine/fields.h index c73062973b..af8f0dd416 100644 --- a/symengine/fields.h +++ b/symengine/fields.h @@ -424,7 +424,7 @@ class GaloisFieldDict GaloisFieldDict &operator/=(const integer_class &other) { if (other == integer_class(0)) { - throw std::runtime_error("ZeroDivisionError"); + throw DivisionByZeroError("ZeroDivisionError"); } if (dict_.empty()) return static_cast(*this); @@ -446,7 +446,7 @@ class GaloisFieldDict throw std::runtime_error("Error: field must be same."); auto dict_divisor = other.dict_; if (dict_divisor.empty()) { - throw std::runtime_error("ZeroDivisionError"); + throw DivisionByZeroError("ZeroDivisionError"); } if (dict_.empty()) return static_cast(*this); @@ -502,7 +502,7 @@ class GaloisFieldDict GaloisFieldDict &operator%=(const integer_class &other) { if (other == integer_class(0)) { - throw std::runtime_error("ZeroDivisionError"); + throw DivisionByZeroError("ZeroDivisionError"); } if (dict_.empty()) return static_cast(*this); @@ -516,7 +516,7 @@ class GaloisFieldDict throw std::runtime_error("Error: field must be same."); auto dict_divisor = other.dict_; if (dict_divisor.empty()) { - throw std::runtime_error("ZeroDivisionError"); + throw DivisionByZeroError("ZeroDivisionError"); } if (dict_.empty()) return static_cast(*this); diff --git a/symengine/functions.cpp b/symengine/functions.cpp index 9bfcd1faad..dbf5373623 100644 --- a/symengine/functions.cpp +++ b/symengine/functions.cpp @@ -2150,7 +2150,8 @@ RCP zeta(const RCP &s, const RCP &a) if (rcp_static_cast(s)->is_zero()) { return sub(div(one, i2), a); } else if (rcp_static_cast(s)->is_one()) { - throw std::runtime_error("Complex infinity is not yet implemented"); + throw NotImplementedError( + "Complex infinity is not yet implemented"); } else if (is_a(*s) and is_a(*a)) { auto s_ = static_cast(*s).as_int(); auto a_ = static_cast(*a).as_int(); @@ -2316,7 +2317,7 @@ RCP gamma(const RCP &arg) if (arg_->is_positive()) { return gamma_positive_int(arg); } else { - throw std::runtime_error("Complex Infinity not yet implemented"); + throw NotImplementedError("Complex Infinity not yet implemented"); } } else if (is_a(*arg)) { RCP arg_ = rcp_static_cast(arg); @@ -2477,7 +2478,7 @@ RCP loggamma(const RCP &arg) if (is_a(*arg)) { RCP arg_int = rcp_static_cast(arg); if (not arg_int->is_positive()) { - throw std::runtime_error("Infinity not yet implemented"); + throw NotImplementedError("Infinity not yet implemented"); } if (eq(*integer(1), *arg_int) or eq(*integer(2), *arg_int)) { return zero; @@ -2530,7 +2531,7 @@ RCP beta(const RCP &x, const RCP &y) { // Only special values are being evaluated if (eq(*add(x, y), *one)) { - throw std::runtime_error("Complex Infinity not yet implemented"); + throw NotImplementedError("Complex Infinity not yet implemented"); } if (is_a(*x)) { @@ -2543,7 +2544,7 @@ RCP beta(const RCP &x, const RCP &y) mul(gamma_positive_int(x), gamma_positive_int(y)), gamma_positive_int(add(x, y))); } else { - throw std::runtime_error( + throw NotImplementedError( "Complex Infinity not yet implemented"); } } else if (is_a(*y)) { @@ -2556,7 +2557,7 @@ RCP beta(const RCP &x, const RCP &y) } } } else { - throw std::runtime_error("Complex Infinity not yet implemented"); + throw NotImplementedError("Complex Infinity not yet implemented"); } } @@ -2573,7 +2574,7 @@ RCP beta(const RCP &x, const RCP &y) } } } else { - throw std::runtime_error("Complex Infinity not yet implemented"); + throw NotImplementedError("Complex Infinity not yet implemented"); } } @@ -2585,7 +2586,7 @@ RCP beta(const RCP &x, const RCP &y) return div(mul(gamma_multiple_2(x), gamma_positive_int(y)), gamma_multiple_2(add(x, y))); } else { - throw std::runtime_error( + throw NotImplementedError( "Complex Infinity not yet implemented"); } } @@ -2648,7 +2649,7 @@ RCP polygamma(const RCP &n_, // Only special values are being evaluated if (is_a_Number(*x_) and not(rcp_static_cast(x_))->is_positive()) { - throw std::runtime_error("Complex Infinity not yet implemented"); + throw NotImplementedError("Complex Infinity not yet implemented"); } if (is_a(*n_) and is_a(*x_)) { auto n = static_cast(*n_).as_int(); diff --git a/symengine/infinity.cpp b/symengine/infinity.cpp index a5bb8839e5..4ac5727340 100644 --- a/symengine/infinity.cpp +++ b/symengine/infinity.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace SymEngine { @@ -34,7 +35,7 @@ RCP Infty::from_int(const int val) bool Infty::is_canonical(const RCP &num) const { if (is_a(*num) || is_a(*num)) - throw std::runtime_error("Not implemented for all directions"); + throw NotImplementedError("Not implemented for all directions"); if (num->is_one() || num->is_zero() || num->is_minus_one()) return true; @@ -108,7 +109,7 @@ RCP Infty::add(const Number &other) const RCP Infty::mul(const Number &other) const { if (is_a(other)) - throw std::runtime_error("Multiplation with Complex not implemented"); + throw NotImplementedError("Multiplation with Complex not implemented"); if (is_a(other)) { const Infty &s = static_cast(other); diff --git a/symengine/number.h b/symengine/number.h index 862993b2f6..078a6ff708 100644 --- a/symengine/number.h +++ b/symengine/number.h @@ -41,7 +41,7 @@ class Number : public Basic //! Get `Evaluate` singleton to evaluate numerically virtual Evaluate &get_eval() const { - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); }; //! Addition @@ -65,11 +65,11 @@ class Number : public Basic virtual bool is_perfect_power(bool is_expected = false) const { - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); }; virtual bool nth_root(const Ptr> &, unsigned long n) const { - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); }; }; //! Add `self` and `other` @@ -136,11 +136,11 @@ class NumberWrapper : public Number IMPLEMENT_TYPEID(NUMBER_WRAPPER) virtual std::string __str__() const { - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); }; virtual RCP eval(long bits) const { - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); }; }; diff --git a/symengine/pow.cpp b/symengine/pow.cpp index 2570c3b422..7d6c1f779b 100644 --- a/symengine/pow.cpp +++ b/symengine/pow.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace SymEngine { @@ -294,7 +295,7 @@ RCP Log::create(const RCP &a) const RCP log(const RCP &arg) { if (eq(*arg, *zero)) { - throw std::runtime_error( + throw NotImplementedError( "log(0) is complex infinity. Yet to be implemented"); } if (eq(*arg, *one)) @@ -306,7 +307,8 @@ RCP log(const RCP &arg) if (not _arg->is_exact()) { return _arg->get_eval().log(*_arg); } else if (_arg->is_negative()) { - throw std::runtime_error("Imaginary Result. Yet to be implemented"); + throw NotImplementedError( + "Imaginary Result. Yet to be implemented"); } } if (is_a(*arg)) { diff --git a/symengine/real_double.cpp b/symengine/real_double.cpp index 17567f0125..b07125a096 100644 --- a/symengine/real_double.cpp +++ b/symengine/real_double.cpp @@ -236,7 +236,7 @@ class EvaluateComplexDouble : public EvaluateDouble virtual RCP gamma(const Basic &x) const override { SYMENGINE_ASSERT(is_a(x)) - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); } virtual RCP asin(const Basic &x) const override { diff --git a/symengine/real_mpfr.cpp b/symengine/real_mpfr.cpp index 6f25aef17e..5e18c81bcd 100644 --- a/symengine/real_mpfr.cpp +++ b/symengine/real_mpfr.cpp @@ -974,7 +974,7 @@ class EvaluateMPFR : public Evaluate mpfr_gamma(t.get_mpfr_t(), x_, MPFR_RNDN); return real_mpfr(std::move(t)); } else { - throw std::runtime_error("Not Implemented."); + throw NotImplementedError("Not Implemented."); } } }; diff --git a/symengine/series.h b/symengine/series.h index 0012fc066b..a69a48852e 100644 --- a/symengine/series.h +++ b/symengine/series.h @@ -90,7 +90,8 @@ class SeriesBase : public SeriesCoeffInterface const Series &o = static_cast(other); long deg = std::min(degree_, o.degree_); if (var_ != o.var_) { - throw std::runtime_error("Multivariate Series not implemented"); + throw NotImplementedError( + "Multivariate Series not implemented"); } return make_rcp(Poly(p_ + o.p_), var_, deg); } else if (other.get_type_code() < Series::type_code_id) { @@ -107,7 +108,8 @@ class SeriesBase : public SeriesCoeffInterface const Series &o = static_cast(other); long deg = std::min(degree_, o.degree_); if (var_ != o.var_) { - throw std::runtime_error("Multivariate Series not implemented"); + throw NotImplementedError( + "Multivariate Series not implemented"); } return make_rcp(Series::mul(p_, o.p_, deg), var_, deg); } else if (other.get_type_code() < Series::type_code_id) { @@ -126,7 +128,8 @@ class SeriesBase : public SeriesCoeffInterface const Series &o = static_cast(other); deg = std::min(deg, o.degree_); if (var_ != o.var_) { - throw std::runtime_error("Multivariate Series not implemented"); + throw NotImplementedError( + "Multivariate Series not implemented"); } p = o.p_; } else if (is_a(other)) { @@ -240,7 +243,7 @@ class SeriesBase : public SeriesCoeffInterface const short ldeg = Series::ldegree(s); if (ldeg % n != 0) { - throw std::runtime_error("Puiseux series not implemented."); + throw NotImplementedError("Puiseux series not implemented."); } Poly ss = s; if (ldeg != 0) { @@ -546,7 +549,7 @@ class SeriesBase : public SeriesCoeffInterface unsigned int prec) { if (Series::find_cf(s, var, 0) != 0) - throw std::logic_error("lambertw(const) not Implemented"); + throw NotImplementedError("lambertw(const) not Implemented"); Poly p1(0); @@ -649,55 +652,55 @@ class SeriesBase : public SeriesCoeffInterface static inline Coeff sin(const Coeff &c) { - throw std::runtime_error("sin(const) not implemented"); + throw NotImplementedError("sin(const) not implemented"); } static inline Coeff cos(const Coeff &c) { - throw std::runtime_error("cos(const) not implemented"); + throw NotImplementedError("cos(const) not implemented"); } static inline Coeff tan(const Coeff &c) { - throw std::runtime_error("tan(const) not implemented"); + throw NotImplementedError("tan(const) not implemented"); } static inline Coeff asin(const Coeff &c) { - throw std::runtime_error("asin(const) not implemented"); + throw NotImplementedError("asin(const) not implemented"); } static inline Coeff acos(const Coeff &c) { - throw std::runtime_error("acos(const) not implemented"); + throw NotImplementedError("acos(const) not implemented"); } static inline Coeff atan(const Coeff &c) { - throw std::runtime_error("atan(const) not implemented"); + throw NotImplementedError("atan(const) not implemented"); } static inline Coeff sinh(const Coeff &c) { - throw std::runtime_error("sinh(const) not implemented"); + throw NotImplementedError("sinh(const) not implemented"); } static inline Coeff cosh(const Coeff &c) { - throw std::runtime_error("cosh(const) not implemented"); + throw NotImplementedError("cosh(const) not implemented"); } static inline Coeff tanh(const Coeff &c) { - throw std::runtime_error("tanh(const) not implemented"); + throw NotImplementedError("tanh(const) not implemented"); } static inline Coeff asinh(const Coeff &c) { - throw std::runtime_error("asinh(const) not implemented"); + throw NotImplementedError("asinh(const) not implemented"); } static inline Coeff atanh(const Coeff &c) { - throw std::runtime_error("atanh(const) not implemented"); + throw NotImplementedError("atanh(const) not implemented"); } static inline Coeff exp(const Coeff &c) { - throw std::runtime_error("exp(const) not implemented"); + throw NotImplementedError("exp(const) not implemented"); } static inline Coeff log(const Coeff &c) { - throw std::runtime_error("log(const) not implemented"); + throw NotImplementedError("log(const) not implemented"); } }; diff --git a/symengine/series_flint.h b/symengine/series_flint.h index adc0aa178d..738cb996c6 100644 --- a/symengine/series_flint.h +++ b/symengine/series_flint.h @@ -3,6 +3,7 @@ #include #include +#include #ifdef HAVE_SYMENGINE_FLINT #include @@ -122,7 +123,7 @@ class URatPSeriesFlint static inline fqp_t series_acos(const fqp_t &s, const fqp_t &var, unsigned int prec) { - throw std::runtime_error("acos() not implemented"); + throw NotImplementedError("acos() not implemented"); } static inline fqp_t series_sinh(const fqp_t &s, const fqp_t &var, unsigned int prec) @@ -175,7 +176,7 @@ class URatPSeriesFlint const short ldeg = ldegree(s); if (ldeg % n != 0) { - throw std::runtime_error("Puiseux series not implemented."); + throw NotImplementedError("Puiseux series not implemented."); } fqp_t ss = s; if (ldeg != 0) { diff --git a/symengine/series_generic.cpp b/symengine/series_generic.cpp index f3289cd5bf..567ea5c9f2 100644 --- a/symengine/series_generic.cpp +++ b/symengine/series_generic.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using SymEngine::RCP; using SymEngine::make_rcp; @@ -102,7 +103,7 @@ UExprDict UnivariateSeries::pow(const UExprDict &base, int exp, unsigned prec) } if (exp == 0) { if (base == 0 or base.get_dict().size() == 0) { - throw std::runtime_error("Error: 0**0 is undefined."); + throw UndefinedError("Error: 0**0 is undefined."); } else { return UExprDict(1); } @@ -159,7 +160,7 @@ UExprDict UnivariateSeries::integrate(const UExprDict &s, const UExprDict &var) dict.insert(std::pair(it.first + 1, it.second / (it.first + 1))); } else { - throw std::runtime_error("Not Implemented"); + throw NotImplementedError("Not Implemented"); } } diff --git a/symengine/series_piranha.cpp b/symengine/series_piranha.cpp index 6915641ebe..ceb69f0d50 100644 --- a/symengine/series_piranha.cpp +++ b/symengine/series_piranha.cpp @@ -65,7 +65,7 @@ piranha::rational URatPSeriesPiranha::convert(const Rational &x) piranha::rational URatPSeriesPiranha::convert(const Basic &x) { - throw std::runtime_error("Not Implemented"); + throw std::NotImplementedError("Not Implemented"); } RCP URatPSeriesPiranha::as_basic() const diff --git a/symengine/sets.cpp b/symengine/sets.cpp index c8a11a4f90..ec2a9b2230 100644 --- a/symengine/sets.cpp +++ b/symengine/sets.cpp @@ -18,7 +18,7 @@ bool Interval::is_canonical(const RCP &s, bool right_open) { if (is_a(*s) or is_a(*e)) - throw std::runtime_error("Complex set not implemented"); + throw NotImplementedError("Complex set not implemented"); if (eq(*e, *s)) { return false; } else if (eq(*min({s, e}), *e)) { @@ -94,7 +94,7 @@ RCP Interval::close() const bool Interval::contains(const RCP &a) const { if (not is_a_Number(*a)) - throw std::runtime_error("Not implemented"); + throw NotImplementedError("Not implemented"); if ((eq(*start_, *a) and left_open_) or (eq(*end_, *a) and right_open_)) return false; if (eq(*start_, *a) or eq(*end_, *a)) @@ -167,7 +167,7 @@ RCP Interval::set_union(const RCP &o) const and ((eq(*end_end, *this->end_) and this->right_open_) or (eq(*end_end, *other.end_) and other.right_open_))) or (eq(*end_end, *m) and not eq(*end_end, *start_start))) { - throw std::runtime_error("not implemented"); + throw NotImplementedError("not implemented"); } else { if (eq(*min({this->start_, other.start_}), *this->start_)) start = this->start_; @@ -393,7 +393,7 @@ RCP FiniteSet::set_union(const RCP &o) const } } if (not container.empty()) { - throw std::runtime_error("not implemented"); + throw NotImplementedError("not implemented"); } else { if (left == other.left_open_ and right == other.right_open_) return o; diff --git a/symengine/symengine_exception.h b/symengine/symengine_exception.h index 441a9b9897..20c9f786cc 100644 --- a/symengine/symengine_exception.h +++ b/symengine/symengine_exception.h @@ -3,10 +3,11 @@ typedef enum { SYMENGINE_NO_EXCEPTION = 0, - SYMENGINE_DIV_BY_ZERO = 1, - SYMENGINE_NOT_IMPLEMENTED = 2, - SYMENGINE_UNDEFINED = 3, - SYMENGINE_PARSE_ERROR = 4, + SYMENGINE_RUNTIME_ERROR = 1, + SYMENGINE_DIV_BY_ZERO = 2, + SYMENGINE_NOT_IMPLEMENTED = 3, + SYMENGINE_UNDEFINED = 4, + SYMENGINE_PARSE_ERROR = 5, } symengine_exceptions_t; #ifdef __cplusplus @@ -24,6 +25,10 @@ class SymEngineException : public std::exception : m_msg(msg), ec(error) { } + SymEngineException(const std::string &msg) + : SymEngineException(msg, SYMENGINE_RUNTIME_ERROR) + { + } const char *what() const throw() { return m_msg.c_str(); @@ -52,6 +57,15 @@ class NotImplementedError : public SymEngineException } }; +class UndefinedError : public SymEngineException +{ +public: + UndefinedError(const std::string &msg) + : SymEngineException(msg, SYMENGINE_UNDEFINED) + { + } +}; + class ParseError : public SymEngineException { public: diff --git a/symengine/tests/basic/test_functions.cpp b/symengine/tests/basic/test_functions.cpp index d23ff070a3..7e6b605259 100644 --- a/symengine/tests/basic/test_functions.cpp +++ b/symengine/tests/basic/test_functions.cpp @@ -95,6 +95,7 @@ using SymEngine::I; using SymEngine::integer_class; using SymEngine::get_mpz_t; using SymEngine::DivisionByZeroError; +using SymEngine::NotImplementedError; using namespace SymEngine::literals; @@ -2214,7 +2215,7 @@ TEST_CASE("Zeta: functions", "[functions]") r1 = zeta(x, i2); REQUIRE(r1->__str__() == "zeta(x, 2)"); - CHECK_THROWS_AS(zeta(one, i2), std::runtime_error); + CHECK_THROWS_AS(zeta(one, i2), NotImplementedError); } TEST_CASE("Levi Civita: functions", "[functions]") diff --git a/symengine/tests/basic/test_infinity.cpp b/symengine/tests/basic/test_infinity.cpp index 9d0a9394a3..fd8ccb5d41 100644 --- a/symengine/tests/basic/test_infinity.cpp +++ b/symengine/tests/basic/test_infinity.cpp @@ -6,6 +6,7 @@ #include #include #include +#include using SymEngine::Basic; using SymEngine::Number; @@ -28,6 +29,7 @@ using SymEngine::ComplexInf; using SymEngine::Symbol; using SymEngine::symbol; using SymEngine::Complex; +using SymEngine::NotImplementedError; TEST_CASE("Constructors for Infinity", "[Infinity]") { @@ -192,5 +194,5 @@ TEST_CASE("Multiplication with Infinity", "[Infinity]") CHECK_THROWS_AS(c->mul(*zero), std::runtime_error); RCP cx = Complex::from_two_nums(*integer(1), *integer(1)); - CHECK_THROWS_AS(c->mul(*cx), std::runtime_error); + CHECK_THROWS_AS(c->mul(*cx), NotImplementedError); } diff --git a/symengine/tests/basic/test_series_generic.cpp b/symengine/tests/basic/test_series_generic.cpp index 5d0efcab45..e0572265f2 100644 --- a/symengine/tests/basic/test_series_generic.cpp +++ b/symengine/tests/basic/test_series_generic.cpp @@ -3,6 +3,7 @@ #include #include +#include using SymEngine::UExprDict; using SymEngine::UnivariateSeries; @@ -30,6 +31,8 @@ using SymEngine::Number; using SymEngine::umap_int_basic; using SymEngine::pi; using SymEngine::I; +using SymEngine::UndefinedError; +using SymEngine::NotImplementedError; using namespace SymEngine::literals; @@ -159,7 +162,7 @@ TEST_CASE("Exponentiation of UExprDict with precision", "[UnivariateSeries]") REQUIRE(e == c); REQUIRE(f == d); REQUIRE(g == one); - REQUIRE_THROWS_AS(UnivariateSeries::pow(zero, 0, 1), std::runtime_error); + REQUIRE_THROWS_AS(UnivariateSeries::pow(zero, 0, 1), UndefinedError); } TEST_CASE("Differentiation of UnivariateSeries", "[UnivariateSeries]") @@ -178,7 +181,7 @@ TEST_CASE("Integration of UnivariateSeries", "[UnivariateSeries]") UExprDict c({{1, 1}, {2, 1}, {3, 1}}); REQUIRE_THROWS_AS( UnivariateSeries::integrate(a, UnivariateSeries::var("x")), - std::runtime_error); + NotImplementedError); REQUIRE(UnivariateSeries::integrate(b, UnivariateSeries::var("x")) == c); } diff --git a/symengine/tests/basic/test_sets.cpp b/symengine/tests/basic/test_sets.cpp index 41109afc40..0e287a0dff 100644 --- a/symengine/tests/basic/test_sets.cpp +++ b/symengine/tests/basic/test_sets.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using SymEngine::Basic; using SymEngine::Integer; @@ -29,6 +30,7 @@ using SymEngine::symbol; using SymEngine::boolean; using SymEngine::Inf; using SymEngine::NegInf; +using SymEngine::NotImplementedError; TEST_CASE("Interval : Basic", "[basic]") { @@ -69,10 +71,10 @@ TEST_CASE("Interval : Basic", "[basic]") r3 = interval(im5, i2, false, false); // [-5, 2] r4 = interval(integer(3), i20, false, false); REQUIRE(r3->compare(*r4) == -1); - CHECK_THROWS_AS(r3->set_union(r4), std::runtime_error); - CHECK_THROWS_AS(r4->set_union(r3), std::runtime_error); + CHECK_THROWS_AS(r3->set_union(r4), NotImplementedError); + CHECK_THROWS_AS(r4->set_union(r3), NotImplementedError); r3 = interval(zero, i2, true, true); // (0, 2) - CHECK_THROWS_AS(r3->contains(sqrt(i2)), std::runtime_error); + CHECK_THROWS_AS(r3->contains(sqrt(i2)), NotImplementedError); r3 = interval(im5, i2, false, false); // [-5, 2] REQUIRE(r3->is_subset(r2)); @@ -136,7 +138,7 @@ TEST_CASE("Interval : Basic", "[basic]") REQUIRE(eq(*r5->get_args()[2], *boolean(r5->left_open_))); REQUIRE(eq(*r5->get_args()[3], *boolean(r5->right_open_))); RCP c1 = Complex::from_two_nums(*i2, *i20); - CHECK_THROWS_AS(interval(c1, one), std::runtime_error); + CHECK_THROWS_AS(interval(c1, one), NotImplementedError); CHECK_THROWS_AS(r5->diff(symbol("x")), std::runtime_error); } diff --git a/symengine/visitor.cpp b/symengine/visitor.cpp index fa0b54281b..2fb90adfb6 100644 --- a/symengine/visitor.cpp +++ b/symengine/visitor.cpp @@ -61,7 +61,7 @@ bool has_symbol(const Basic &b, const Symbol &x) RCP coeff(const Basic &b, const Basic &x, const Basic &n) { if (!is_a(x)) { - throw std::runtime_error("Not implemented for non Symbols."); + throw NotImplementedError("Not implemented for non Symbols."); } CoeffVisitor v(ptrFromRef(static_cast(x)), ptrFromRef(n)); return v.apply(b); From 25ef8fc1f3d6c1798ab4c336df1a1cbba5db7138 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 5 Aug 2016 00:18:01 +0530 Subject: [PATCH 17/19] All runtime_errors converted to SymEngineExceptions --- symengine/codegen.h | 22 +++---- symengine/complex.cpp | 2 +- symengine/dense_matrix.cpp | 6 +- symengine/derivative.cpp | 4 +- symengine/eval_arb.cpp | 4 +- symengine/eval_double.cpp | 6 +- symengine/eval_mpc.cpp | 4 +- symengine/eval_mpfr.cpp | 2 +- symengine/fields.cpp | 14 ++--- symengine/fields.h | 12 ++-- symengine/functions.cpp | 8 +-- symengine/infinity.cpp | 8 +-- symengine/integer.cpp | 6 +- symengine/integer.h | 2 +- symengine/lambda_double.h | 4 +- symengine/ntheory.cpp | 28 ++++----- symengine/parser.cpp | 2 +- symengine/polys/basic_conversions.h | 6 +- symengine/polys/uintpoly.cpp | 2 +- symengine/polys/uintpoly_flint.h | 6 +- symengine/polys/uintpoly_piranha.h | 12 ++-- symengine/polys/upolybase.h | 8 +-- symengine/polys/uratpoly.cpp | 2 +- symengine/pow.cpp | 8 +-- symengine/rational.cpp | 6 +- symengine/rational.h | 2 +- symengine/real_mpfr.cpp | 60 +++++++++---------- symengine/rings.cpp | 2 +- symengine/series.cpp | 2 +- symengine/series.h | 6 +- symengine/series_flint.cpp | 2 +- symengine/series_piranha.cpp | 4 +- symengine/series_visitor.h | 10 ++-- symengine/sparse_matrix.cpp | 4 +- symengine/subs.h | 2 +- symengine/symengine_rcp.h | 2 +- symengine/tests/basic/test_arit.cpp | 4 +- symengine/tests/basic/test_functions.cpp | 2 +- symengine/tests/basic/test_infinity.cpp | 15 ++--- symengine/tests/basic/test_number.cpp | 6 +- symengine/tests/basic/test_rational.cpp | 4 +- .../tests/basic/test_series_expansion_UP.cpp | 2 +- .../basic/test_series_expansion_URatF.cpp | 12 ++-- .../basic/test_series_expansion_URatP.cpp | 8 +-- symengine/tests/basic/test_sets.cpp | 7 ++- symengine/tests/eval/test_eval_arb.cpp | 2 + symengine/tests/eval/test_eval_double.cpp | 7 ++- symengine/tests/eval/test_eval_mpc.cpp | 2 + symengine/tests/eval/test_eval_mpfr.cpp | 2 + symengine/tests/eval/test_evalf.cpp | 2 + symengine/tests/eval/test_lambda_double.cpp | 5 +- symengine/tests/logic/test_logic.cpp | 6 +- symengine/tests/matrix/test_matrix.cpp | 10 ++-- .../polynomial/test_basic_conversions.cpp | 26 ++++---- symengine/tests/polynomial/test_uexprpoly.cpp | 8 ++- symengine/tests/polynomial/test_uintpoly.cpp | 10 ++-- .../tests/polynomial/test_uintpoly_flint.cpp | 8 ++- .../polynomial/test_uintpoly_piranha.cpp | 8 ++- symengine/tests/polynomial/test_uratpoly.cpp | 10 ++-- .../tests/polynomial/test_uratpoly_flint.cpp | 10 ++-- .../polynomial/test_uratpoly_piranha.cpp | 10 ++-- 61 files changed, 251 insertions(+), 215 deletions(-) diff --git a/symengine/codegen.h b/symengine/codegen.h index 4a3a2d22e6..d23df9cbbe 100644 --- a/symengine/codegen.h +++ b/symengine/codegen.h @@ -16,7 +16,7 @@ class CodePrinter : public BaseVisitor using StrPrinter::bvisit; void bvisit(const Basic &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void bvisit(const Complex &x) { @@ -62,7 +62,7 @@ class CodePrinter : public BaseVisitor for (size_t i = 0;; ++i) { if (i == vec.size() - 1) { if (neq(*vec[i].second, *boolTrue)) { - throw std::runtime_error( + throw SymEngineException( "Code generation requires a (Expr, True) at the end"); } s << "(\n " << apply(vec[i].first) << "\n"; @@ -90,15 +90,15 @@ class CodePrinter : public BaseVisitor } void bvisit(const EmptySet &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void bvisit(const FiniteSet &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void bvisit(const UniversalSet &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void _print_pow(std::ostringstream &o, const RCP &a, const RCP &b) @@ -127,28 +127,28 @@ class CodePrinter : public BaseVisitor else if (x.is_positive_infinity()) s << "HUGE_VAL"; else - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); str_ = s.str(); } void bvisit(const UnivariateSeries &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void bvisit(const FunctionSymbol &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void bvisit(const Derivative &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void bvisit(const Subs &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } void bvisit(const GaloisField &x) { - throw std::runtime_error("Not supported"); + throw SymEngineException("Not supported"); } }; diff --git a/symengine/complex.cpp b/symengine/complex.cpp index d5d4a6a738..6e9b45c9a4 100644 --- a/symengine/complex.cpp +++ b/symengine/complex.cpp @@ -108,7 +108,7 @@ RCP Complex::from_two_nums(const Number &re, const Number &im) rational_class im_mpq = static_cast(im).i; return Complex::from_mpq(re_mpq, im_mpq); } else { - throw std::runtime_error( + throw SymEngineException( "Invalid Format: Expected Integer or Rational"); } } diff --git a/symengine/dense_matrix.cpp b/symengine/dense_matrix.cpp index 130b1385a4..cb5e779cbc 100644 --- a/symengine/dense_matrix.cpp +++ b/symengine/dense_matrix.cpp @@ -205,7 +205,7 @@ void jacobian(const DenseMatrix &A, const DenseMatrix &x, DenseMatrix &result) } } if (error) { - throw std::runtime_error( + throw SymEngineException( "'x' must contain Symbols only. " "Use sjacobian for SymPy style differentiation"); } @@ -828,7 +828,7 @@ void LDL_solve(const DenseMatrix &A, const DenseMatrix &b, DenseMatrix &x) DenseMatrix x_ = DenseMatrix(b.nrows(), b.ncols()); if (not is_symmetric_dense(A)) - throw std::runtime_error("Matrix must be symmetric"); + throw SymEngineException("Matrix must be symmetric"); LDL(A, L, D); forward_substitution(L, b, x); @@ -940,7 +940,7 @@ void pivoted_LU(const DenseMatrix &A, DenseMatrix &LU, permutelist &pl) pivot = i; } if (pivot == -1) - throw std::runtime_error("Matrix is rank deficient"); + throw SymEngineException("Matrix is rank deficient"); if (pivot - j != 0) { // row must be swapped row_exchange_dense(LU, pivot, j); pl.push_back({pivot, j}); diff --git a/symengine/derivative.cpp b/symengine/derivative.cpp index 89ade1df17..bbd28193f0 100644 --- a/symengine/derivative.cpp +++ b/symengine/derivative.cpp @@ -589,13 +589,13 @@ class DiffImplementation static RCP diff(const Set &self, const RCP &x) { - throw std::runtime_error("Derivative doesn't exist."); + throw SymEngineException("Derivative doesn't exist."); } static RCP diff(const Boolean &self, const RCP &x) { - throw std::runtime_error("Derivative doesn't exist."); + throw SymEngineException("Derivative doesn't exist."); } static RCP diff(const Piecewise &self, diff --git a/symengine/eval_arb.cpp b/symengine/eval_arb.cpp index f166513444..3dd66bdf35 100644 --- a/symengine/eval_arb.cpp +++ b/symengine/eval_arb.cpp @@ -128,7 +128,7 @@ class EvalArbVisitor : public BaseVisitor void bvisit(const Symbol &x) { - throw std::runtime_error("Symbol cannot be evaluated as an arb type."); + throw SymEngineException("Symbol cannot be evaluated as an arb type."); } void bvisit(const UIntPoly &x) @@ -390,7 +390,7 @@ class EvalArbVisitor : public BaseVisitor } else if (x.__eq__(*EulerGamma)) { arb_const_euler(result_, prec_); } else { - throw std::runtime_error("Constant " + x.get_name() + throw SymEngineException("Constant " + x.get_name() + " is not implemented."); } } diff --git a/symengine/eval_double.cpp b/symengine/eval_double.cpp index f5bcc37f17..b56b901205 100644 --- a/symengine/eval_double.cpp +++ b/symengine/eval_double.cpp @@ -96,7 +96,7 @@ class EvalDoubleVisitor : public BaseVisitor void bvisit(const Symbol &) { - throw std::runtime_error("Symbol cannot be evaluated."); + throw SymEngineException("Symbol cannot be evaluated."); }; void bvisit(const Log &x) @@ -241,7 +241,7 @@ class EvalDoubleVisitor : public BaseVisitor result_ = 0.5772156649015328606065; // use until polygamma or // digamma is implemented } else { - throw std::runtime_error("Constant " + x.get_name() + throw SymEngineException("Constant " + x.get_name() + " is not implemented."); } }; @@ -586,7 +586,7 @@ std::vector init_eval_double() return 0.5772156649015328606065; // use until polygamma or digamma // is implemented } else { - throw std::runtime_error( + throw SymEngineException( "Constant " + static_cast(x).get_name() + " is not implemented."); } diff --git a/symengine/eval_mpc.cpp b/symengine/eval_mpc.cpp index 78ad551bba..efab3912c9 100644 --- a/symengine/eval_mpc.cpp +++ b/symengine/eval_mpc.cpp @@ -294,14 +294,14 @@ class EvalMPCVisitor : public BaseVisitor mpc_set_fr(result_, t, rnd_); mpfr_clear(t); } else { - throw std::runtime_error("Constant " + x.get_name() + throw SymEngineException("Constant " + x.get_name() + " is not implemented."); } } void bvisit(const Gamma &x) { - throw std::runtime_error("Not implemented"); + throw SymEngineException("Not implemented"); } void bvisit(const Abs &x) diff --git a/symengine/eval_mpfr.cpp b/symengine/eval_mpfr.cpp index c6a2db0f3a..4a5bbac223 100644 --- a/symengine/eval_mpfr.cpp +++ b/symengine/eval_mpfr.cpp @@ -279,7 +279,7 @@ class EvalMPFRVisitor : public BaseVisitor } else if (x.__eq__(*EulerGamma)) { mpfr_const_euler(result_, rnd_); } else { - throw std::runtime_error("Constant " + x.get_name() + throw SymEngineException("Constant " + x.get_name() + " is not implemented."); } } diff --git a/symengine/fields.cpp b/symengine/fields.cpp index 7d4a700543..d0248ecf78 100644 --- a/symengine/fields.cpp +++ b/symengine/fields.cpp @@ -106,7 +106,7 @@ void GaloisFieldDict::gf_div(const GaloisFieldDict &o, const Ptr &rem) const { if (modulo_ != o.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (o.dict_.empty()) throw DivisionByZeroError("ZeroDivisionError"); std::vector dict_out; @@ -235,7 +235,7 @@ void GaloisFieldDict::gf_monic(integer_class &res, GaloisFieldDict GaloisFieldDict::gf_gcd(const GaloisFieldDict &o) const { if (modulo_ != o.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); GaloisFieldDict f = static_cast(*this); GaloisFieldDict g = o; GaloisFieldDict temp_out; @@ -251,7 +251,7 @@ GaloisFieldDict GaloisFieldDict::gf_gcd(const GaloisFieldDict &o) const GaloisFieldDict GaloisFieldDict::gf_lcm(const GaloisFieldDict &o) const { if (modulo_ != o.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (dict_.empty()) return static_cast(*this); if (o.dict_.empty()) @@ -357,9 +357,9 @@ GaloisFieldDict GaloisFieldDict::gf_compose_mod(const GaloisFieldDict &g, const GaloisFieldDict &h) const { if (g.modulo_ != h.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (g.modulo_ != modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (g.dict_.size() == 0) return g; GaloisFieldDict out @@ -380,7 +380,7 @@ GaloisFieldDict GaloisFieldDict::gf_pow_mod(const GaloisFieldDict &f, const integer_class &n) const { if (modulo_ != f.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (n == 0_z) return GaloisFieldDict::from_vec({1_z}, modulo_); GaloisFieldDict in = f; @@ -437,7 +437,7 @@ GaloisFieldDict::gf_frobenius_map(const GaloisFieldDict &g, const std::vector &b) const { if (modulo_ != g.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); unsigned m = g.degree(); GaloisFieldDict temp_out(*this), out; if (this->degree() >= m) { diff --git a/symengine/fields.h b/symengine/fields.h index af8f0dd416..be55f2e134 100644 --- a/symengine/fields.h +++ b/symengine/fields.h @@ -200,7 +200,7 @@ class GaloisFieldDict GaloisFieldDict &operator+=(const GaloisFieldDict &other) { if (modulo_ != other.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (other.dict_.size() == 0) return static_cast(*this); if (this->dict_.size() == 0) { @@ -285,7 +285,7 @@ class GaloisFieldDict GaloisFieldDict &operator-=(const GaloisFieldDict &other) { if (modulo_ != other.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (other.dict_.size() == 0) return static_cast(*this); if (this->dict_.size() == 0) { @@ -332,7 +332,7 @@ class GaloisFieldDict { // TODO if (a.modulo_ != b.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (a.get_dict().empty()) return a; if (b.get_dict().empty()) @@ -385,7 +385,7 @@ class GaloisFieldDict GaloisFieldDict &operator*=(const GaloisFieldDict &other) { if (modulo_ != other.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); if (dict_.empty()) return static_cast(*this); @@ -443,7 +443,7 @@ class GaloisFieldDict GaloisFieldDict &operator/=(const GaloisFieldDict &other) { if (modulo_ != other.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); auto dict_divisor = other.dict_; if (dict_divisor.empty()) { throw DivisionByZeroError("ZeroDivisionError"); @@ -513,7 +513,7 @@ class GaloisFieldDict GaloisFieldDict &operator%=(const GaloisFieldDict &other) { if (modulo_ != other.modulo_) - throw std::runtime_error("Error: field must be same."); + throw SymEngineException("Error: field must be same."); auto dict_divisor = other.dict_; if (dict_divisor.empty()) { throw DivisionByZeroError("ZeroDivisionError"); diff --git a/symengine/functions.cpp b/symengine/functions.cpp index dbf5373623..ededd9c87f 100644 --- a/symengine/functions.cpp +++ b/symengine/functions.cpp @@ -2794,7 +2794,7 @@ RCP max(const vec_basic &arg) for (const auto &p : arg) { if (is_a(*p)) - throw std::runtime_error("Complex can't be passed to max!"); + throw SymEngineException("Complex can't be passed to max!"); if (is_a_Number(*p)) { if (not number_set) { @@ -2856,7 +2856,7 @@ RCP max(const vec_basic &arg) } else if (final_args.size() == 1) { return final_args[0]; } else { - throw std::runtime_error("Empty vec_basic passed to max!"); + throw SymEngineException("Empty vec_basic passed to max!"); } } @@ -2897,7 +2897,7 @@ RCP min(const vec_basic &arg) for (const auto &p : arg) { if (is_a(*p)) - throw std::runtime_error("Complex can't be passed to min!"); + throw SymEngineException("Complex can't be passed to min!"); if (is_a_Number(*p)) { if (not number_set) { @@ -2959,7 +2959,7 @@ RCP min(const vec_basic &arg) } else if (final_args.size() == 1) { return final_args[0]; } else { - throw std::runtime_error("Empty vec_basic passed to min!"); + throw SymEngineException("Empty vec_basic passed to min!"); } } diff --git a/symengine/infinity.cpp b/symengine/infinity.cpp index 4ac5727340..cc2a4c8555 100644 --- a/symengine/infinity.cpp +++ b/symengine/infinity.cpp @@ -91,15 +91,15 @@ RCP Infty::add(const Number &other) const if (not eq(*s.get_direction(), *_direction)) { if (is_unsigned_infinity() or s.is_unsigned_infinity()) - throw std::runtime_error("Indeterminate Expression: " + throw SymEngineException("Indeterminate Expression: " "`unsigned_Infty +- Infty` " "encountered"); else - throw std::runtime_error("Indeterminate Expression: `Infty +- " + throw SymEngineException("Indeterminate Expression: `Infty +- " "Infty` encountered. Directions don't " "match"); } else if (is_unsigned_infinity()) { - throw std::runtime_error("Indeterminate Expression: " + throw SymEngineException("Indeterminate Expression: " "`unsigned_Infty +- unsigned Infty` " "encountered"); } else @@ -121,7 +121,7 @@ RCP Infty::mul(const Number &other) const else if (other.is_negative()) return make_rcp(this->_direction->mul(*minus_one)); else - throw std::runtime_error( + throw SymEngineException( "Indeterminate Expression: `0 * Infty` encountered"); } } diff --git a/symengine/integer.cpp b/symengine/integer.cpp index de2710f9c0..d453956e85 100644 --- a/symengine/integer.cpp +++ b/symengine/integer.cpp @@ -37,7 +37,7 @@ signed long int Integer::as_int() const // "as_int()" and we leave it to the user to do any possible further integer // conversions. if (not(mp_fits_slong_p(this->i))) { - throw std::runtime_error("as_int: Integer larger than int"); + throw SymEngineException("as_int: Integer larger than int"); } return mp_get_si(this->i); } @@ -81,7 +81,7 @@ RCP Integer::pow_negint(const Integer &other) const mp_abs(static_cast(*tmp).i)); return Rational::from_mpq(std::move(q)); } else { - throw std::runtime_error("powint returned non-integer"); + throw SymEngineException("powint returned non-integer"); } } @@ -99,7 +99,7 @@ int i_nth_root(const Ptr> &r, const Integer &a, unsigned long int n) { if (n == 0) - throw std::runtime_error("i_nth_root: Can not find Zeroth root"); + throw SymEngineException("i_nth_root: Can not find Zeroth root"); int ret_val; integer_class t; diff --git a/symengine/integer.h b/symengine/integer.h index 03484177e8..ad1710d375 100644 --- a/symengine/integer.h +++ b/symengine/integer.h @@ -91,7 +91,7 @@ class Integer : public Number { if (not(mp_fits_ulong_p(other.i))) { if (other.i > 0u) - throw std::runtime_error( + throw SymEngineException( "powint: 'exp' does not fit unsigned long."); else return pow_negint(other); diff --git a/symengine/lambda_double.h b/symengine/lambda_double.h index 92e576e56b..92fe6866de 100644 --- a/symengine/lambda_double.h +++ b/symengine/lambda_double.h @@ -136,7 +136,7 @@ class LambdaDoubleVisitor : public BaseVisitor> return; } } - throw std::runtime_error("Symbol not in the symbols vector."); + throw SymEngineException("Symbol not in the symbols vector."); }; void bvisit(const Log &x) @@ -291,7 +291,7 @@ class LambdaDoubleVisitor : public BaseVisitor> } else if (eq(x, *E)) { result_ = [=](const std::vector &x) { return std::exp(1); }; } else { - throw std::runtime_error("Constant " + x.get_name() + throw SymEngineException("Constant " + x.get_name() + " is not implemented."); } }; diff --git a/symengine/ntheory.cpp b/symengine/ntheory.cpp index 425e986d91..18dc7538c3 100644 --- a/symengine/ntheory.cpp +++ b/symengine/ntheory.cpp @@ -176,7 +176,7 @@ int _factor_trial_division_sieve(integer_class &factor, const integer_class &N) integer_class sqrtN = mp_sqrt(N); unsigned long limit = mp_get_ui(sqrtN); if (limit > std::numeric_limits::max()) - throw std::runtime_error("N too large to factor"); + throw SymEngineException("N too large to factor"); Sieve::iterator pi(limit); unsigned p; while ((p = pi.next_prime()) <= limit) { @@ -191,7 +191,7 @@ int _factor_trial_division_sieve(integer_class &factor, const integer_class &N) int _factor_lehman_method(integer_class &rop, const integer_class &n) { if (n < 21) - throw std::runtime_error("Require n >= 21 to use lehman method"); + throw SymEngineException("Require n >= 21 to use lehman method"); int ret_val = 0; integer_class u_bound; @@ -259,7 +259,7 @@ int _factor_pollard_pm1_method(integer_class &rop, const integer_class &n, const integer_class &c, unsigned B) { if (n < 4 or B < 3) - throw std::runtime_error( + throw SymEngineException( "Require n > 3 and B > 2 to use Pollard's p-1 method"); integer_class m, _c; @@ -316,7 +316,7 @@ int _factor_pollard_rho_method(integer_class &rop, const integer_class &n, unsigned steps = 10000) { if (n < 5) - throw std::runtime_error("Require n > 4 to use pollard's-rho method"); + throw SymEngineException("Require n > 4 to use pollard's-rho method"); integer_class u, v, g, m; u = s; @@ -404,7 +404,7 @@ int factor(const Ptr> &f, const Integer &n, double B1) ret_val = ecm_factor(get_mpz_t(_f), get_mpz_t(_n), B1, nullptr); mp_demote(_f); if (not ret_val) - throw std::runtime_error( + throw SymEngineException( "ECM failed to factor the given number"); } } @@ -441,7 +441,7 @@ void prime_factors(std::vector> &prime_list, auto limit = mp_get_ui(sqrtN); if (not mp_fits_ulong_p(sqrtN) or limit > std::numeric_limits::max()) - throw std::runtime_error("N too large to factor"); + throw SymEngineException("N too large to factor"); Sieve::iterator pi(limit); unsigned p; @@ -471,7 +471,7 @@ void prime_factor_multiplicities(map_integer_uint &primes_mul, const Integer &n) auto limit = mp_get_ui(sqrtN); if (not mp_fits_ulong_p(sqrtN) or limit > std::numeric_limits::max()) - throw std::runtime_error("N too large to factor"); + throw SymEngineException("N too large to factor"); Sieve::iterator pi(limit); unsigned p; @@ -662,9 +662,9 @@ bool crt(const Ptr> &R, const std::vector> &mod) { if (mod.size() > rem.size()) - throw std::runtime_error("Too few remainders"); + throw SymEngineException("Too few remainders"); if (mod.size() == 0) - throw std::runtime_error("Moduli vector cannot be empty"); + throw SymEngineException("Moduli vector cannot be empty"); integer_class m, r, g, s, t; m = mod[0]->as_mpz(); @@ -693,9 +693,9 @@ void _crt_cartesian(std::vector> &R, const std::vector> &mod) { if (mod.size() > rem.size()) - throw std::runtime_error("Too few remainders"); + throw SymEngineException("Too few remainders"); if (mod.size() == 0) - throw std::runtime_error("Moduli vector cannot be empty"); + throw SymEngineException("Moduli vector cannot be empty"); integer_class m, _m, r, s, t; m = mod[0]->as_mpz(); R = rem[0]; @@ -1621,7 +1621,7 @@ vec_integer_class quadratic_residues(const Integer &a) */ if (a.as_mpz() < 1) { - throw std::runtime_error("quadratic_residues: Input must be > 0"); + throw SymEngineException("quadratic_residues: Input must be > 0"); } vec_integer_class residue; @@ -1645,7 +1645,7 @@ bool is_quad_residue(const Integer &a, const Integer &p) integer_class p2 = p.as_mpz(); if (p2 == 0) - throw std::runtime_error( + throw SymEngineException( "is_quad_residue: Second parameter must be non-zero"); if (p2 < 0) p2 = -p2; @@ -1713,7 +1713,7 @@ i.e a % mod in set([i**n % mod for i in range(mod)]). int mobius(const Integer &a) { if (a.as_int() <= 0) { - throw std::runtime_error("mobius: Integer <= 0"); + throw SymEngineException("mobius: Integer <= 0"); } map_integer_uint prime_mul; bool is_square_free = true; diff --git a/symengine/parser.cpp b/symengine/parser.cpp index 93b436059f..92a29544b3 100644 --- a/symengine/parser.cpp +++ b/symengine/parser.cpp @@ -412,7 +412,7 @@ class ExpressionParser if (last_char_was_op and operator_error(last_char, x)) throw ParseError("Operator inconsistency!"); if (last_char_was_op and operator_error(last_char, x)) { - throw std::runtime_error("Operator inconsistency!"); + throw SymEngineException("Operator inconsistency!"); } last_char_was_op = true; diff --git a/symengine/polys/basic_conversions.h b/symengine/polys/basic_conversions.h index 6e5724937e..3f10f4d3bb 100644 --- a/symengine/polys/basic_conversions.h +++ b/symengine/polys/basic_conversions.h @@ -156,7 +156,7 @@ class BasicToUIntPoly : public BasicToUPolyBase> void bvisit(const Rational &x) { - throw std::runtime_error("Non-integer found"); + throw SymEngineException("Non-integer found"); } void dict_set(unsigned int pow, const Basic &x) @@ -165,7 +165,7 @@ class BasicToUIntPoly : public BasicToUPolyBase> this->dict = Poly::container_from_dict( this->gen, {{pow, static_cast(x).i}}); else - throw std::runtime_error("Non-integer found"); + throw SymEngineException("Non-integer found"); } }; @@ -223,7 +223,7 @@ RCP from_basic(const RCP &basic, bool ex) umap_basic_num tmp = _find_gens_poly(exp); if (tmp.size() != 1) - throw std::runtime_error("Did not find exactly 1 generator"); + throw SymEngineException("Did not find exactly 1 generator"); RCP gen = pow(tmp.begin()->first, tmp.begin()->second); return P::from_container( diff --git a/symengine/polys/uintpoly.cpp b/symengine/polys/uintpoly.cpp index aa9ab3aafb..cd9bee0ded 100644 --- a/symengine/polys/uintpoly.cpp +++ b/symengine/polys/uintpoly.cpp @@ -27,7 +27,7 @@ bool divides_upoly(const UIntPoly &a, const UIntPoly &b, const Ptr> &out) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); auto a_poly = a.get_poly(); auto b_poly = b.get_poly(); diff --git a/symengine/polys/uintpoly_flint.h b/symengine/polys/uintpoly_flint.h index 33f9c06d91..2c22076af8 100644 --- a/symengine/polys/uintpoly_flint.h +++ b/symengine/polys/uintpoly_flint.h @@ -144,7 +144,7 @@ enable_if_t::value gcd_upoly(const T &a, const T &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); return make_rcp(a.get_var(), a.get_poly().gcd(b.get_poly())); } @@ -155,7 +155,7 @@ enable_if_t::value lcm_upoly(const T &a, const T &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); return make_rcp(a.get_var(), a.get_poly().lcm(b.get_poly())); } @@ -179,7 +179,7 @@ enable_if_t::value divides_upoly(const T &a, const T &b, const Ptr> &res) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); typename T::container_type q, r; diff --git a/symengine/polys/uintpoly_piranha.h b/symengine/polys/uintpoly_piranha.h index bfc3c94c4a..43f8491b23 100644 --- a/symengine/polys/uintpoly_piranha.h +++ b/symengine/polys/uintpoly_piranha.h @@ -280,7 +280,7 @@ inline RCP gcd_upoly(const UIntPolyPiranha &a, const UIntPolyPiranha &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); pintpoly gcdx(std::get<0>(pintpoly::gcd(a.get_poly(), b.get_poly()))); // following the convention, that leading coefficient should be positive @@ -293,7 +293,7 @@ inline RCP gcd_upoly(const URatPolyPiranha &a, const URatPolyPiranha &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); pratpoly gcdx(std::get<0>(pratpoly::gcd(a.get_poly(), b.get_poly()))); // following the convention, that polynomial should be monic @@ -305,7 +305,7 @@ inline RCP lcm_upoly(const UIntPolyPiranha &a, const UIntPolyPiranha &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); pintpoly lcmx(std::get<0>(pintpoly::gcd(a.get_poly(), b.get_poly()))); lcmx = (a.get_poly() * b.get_poly()) / lcmx; @@ -318,7 +318,7 @@ inline RCP lcm_upoly(const URatPolyPiranha &a, const URatPolyPiranha &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); pratpoly lcmx(std::get<0>(pratpoly::gcd(a.get_poly(), b.get_poly()))); lcmx = (a.get_poly() * b.get_poly()) / lcmx; @@ -344,7 +344,7 @@ inline bool divides_upoly(const UIntPolyPiranha &a, const UIntPolyPiranha &b, const Ptr> &res) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); try { pintpoly z; @@ -360,7 +360,7 @@ inline bool divides_upoly(const URatPolyPiranha &a, const URatPolyPiranha &b, const Ptr> &res) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); try { pratpoly z; diff --git a/symengine/polys/upolybase.h b/symengine/polys/upolybase.h index e984c20a1b..ed62ff2c37 100644 --- a/symengine/polys/upolybase.h +++ b/symengine/polys/upolybase.h @@ -640,7 +640,7 @@ template RCP add_upoly(const Poly &a, const Poly &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); auto dict = a.get_poly(); dict += b.get_poly(); @@ -659,7 +659,7 @@ template RCP sub_upoly(const Poly &a, const Poly &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); auto dict = a.get_poly(); dict -= b.get_poly(); @@ -670,7 +670,7 @@ template RCP mul_upoly(const Poly &a, const Poly &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); auto dict = a.get_poly(); dict *= b.get_poly(); @@ -688,7 +688,7 @@ template RCP quo_upoly(const Poly &a, const Poly &b) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); auto dict = a.get_poly(); dict /= b.get_poly(); diff --git a/symengine/polys/uratpoly.cpp b/symengine/polys/uratpoly.cpp index a5ce412fb5..a78dac5d67 100644 --- a/symengine/polys/uratpoly.cpp +++ b/symengine/polys/uratpoly.cpp @@ -28,7 +28,7 @@ bool divides_upoly(const URatPoly &a, const URatPoly &b, const Ptr> &out) { if (!(a.get_var()->__eq__(*b.get_var()))) - throw std::runtime_error("Error: variables must agree."); + throw SymEngineException("Error: variables must agree."); auto a_poly = a.get_poly(); auto b_poly = b.get_poly(); diff --git a/symengine/pow.cpp b/symengine/pow.cpp index 7d6c1f779b..cfb7de5119 100644 --- a/symengine/pow.cpp +++ b/symengine/pow.cpp @@ -166,9 +166,9 @@ void multinomial_coefficients(int m, int n, map_vec_int &r) int j, tj, start, k; long long int v; if (m < 2) - throw std::runtime_error("multinomial_coefficients: m >= 2 must hold."); + throw SymEngineException("multinomial_coefficients: m >= 2 must hold."); if (n < 0) - throw std::runtime_error("multinomial_coefficients: n >= 0 must hold."); + throw SymEngineException("multinomial_coefficients: n >= 0 must hold."); t.assign(m, 0); t[0] = n; r[t] = 1; @@ -211,9 +211,9 @@ void multinomial_coefficients_mpz(int m, int n, map_vec_mpz &r) int j, tj, start, k; integer_class v; if (m < 2) - throw std::runtime_error("multinomial_coefficients: m >= 2 must hold."); + throw SymEngineException("multinomial_coefficients: m >= 2 must hold."); if (n < 0) - throw std::runtime_error("multinomial_coefficients: n >= 0 must hold."); + throw SymEngineException("multinomial_coefficients: n >= 0 must hold."); t.assign(m, 0); t[0] = n; r[t] = 1; diff --git a/symengine/rational.cpp b/symengine/rational.cpp index abe42c4ec0..ea3b1600c9 100644 --- a/symengine/rational.cpp +++ b/symengine/rational.cpp @@ -92,7 +92,7 @@ int Rational::compare(const Basic &o) const const Integer &s = static_cast(o); return i < s.i ? -1 : 1; } - throw std::runtime_error("unhandled comparison of Rational"); + throw SymEngineException("unhandled comparison of Rational"); } void get_num_den(const Rational &rat, const Ptr> &num, @@ -127,7 +127,7 @@ bool Rational::nth_root(const Ptr> &the_rat, unsigned long n) const { if (n == 0) - throw std::runtime_error("i_nth_root: Can not find Zeroth root"); + throw SymEngineException("i_nth_root: Can not find Zeroth root"); rational_class r; int ret = mp_root(SymEngine::get_num(r), SymEngine::get_num(i), n); @@ -150,7 +150,7 @@ RCP Rational::powrat(const Rational &other) const RCP Rational::rpowrat(const Integer &other) const { if (not(mp_fits_ulong_p(SymEngine::get_den(i)))) - throw std::runtime_error("powrat: den of 'exp' does not fit ulong."); + throw SymEngineException("powrat: den of 'exp' does not fit ulong."); unsigned long exp = mp_get_ui(SymEngine::get_den(i)); RCP res; if (other.is_negative()) { diff --git a/symengine/rational.h b/symengine/rational.h index 0e02d7956d..a095eb002e 100644 --- a/symengine/rational.h +++ b/symengine/rational.h @@ -175,7 +175,7 @@ class Rational : public Number if (neg) exp_ = -exp_; if (not mp_fits_ulong_p(exp_)) - throw std::runtime_error("powrat: 'exp' does not fit ulong."); + throw SymEngineException("powrat: 'exp' does not fit ulong."); unsigned long exp = mp_get_ui(exp_); rational_class val; mp_pow_ui(SymEngine::get_num(val), SymEngine::get_num(i), exp); diff --git a/symengine/real_mpfr.cpp b/symengine/real_mpfr.cpp index 5e18c81bcd..c2fbb3c460 100644 --- a/symengine/real_mpfr.cpp +++ b/symengine/real_mpfr.cpp @@ -77,7 +77,7 @@ RCP RealMPFR::addreal(const Complex &other) const mpc_add_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -102,7 +102,7 @@ RCP RealMPFR::addreal(const ComplexDouble &other) const mpc_add_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -148,7 +148,7 @@ RCP RealMPFR::subreal(const Complex &other) const mpc_sub_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -173,7 +173,7 @@ RCP RealMPFR::subreal(const ComplexDouble &other) const mpc_sub_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -220,7 +220,7 @@ RCP RealMPFR::rsubreal(const Complex &other) const mpc_fr_sub(t.get_mpc_t(), this->i.get_mpfr_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -245,7 +245,7 @@ RCP RealMPFR::rsubreal(const ComplexDouble &other) const mpc_fr_sub(t.get_mpc_t(), this->i.get_mpfr_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -281,7 +281,7 @@ RCP RealMPFR::mulreal(const Complex &other) const mpc_mul_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -306,7 +306,7 @@ RCP RealMPFR::mulreal(const ComplexDouble &other) const mpc_mul_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -352,7 +352,7 @@ RCP RealMPFR::divreal(const Complex &other) const mpc_div_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -377,7 +377,7 @@ RCP RealMPFR::divreal(const ComplexDouble &other) const mpc_div_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -425,7 +425,7 @@ RCP RealMPFR::rdivreal(const Complex &other) const mpc_fr_div(t.get_mpc_t(), this->i.get_mpfr_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -450,7 +450,7 @@ RCP RealMPFR::rdivreal(const ComplexDouble &other) const mpc_fr_div(t.get_mpc_t(), this->i.get_mpfr_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -477,7 +477,7 @@ RCP RealMPFR::powreal(const Rational &other) const mpc_pow(t.get_mpc_t(), s.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -499,7 +499,7 @@ RCP RealMPFR::powreal(const Complex &other) const mpc_pow_fr(t.get_mpc_t(), t.get_mpc_t(), this->i.get_mpfr_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -515,7 +515,7 @@ RCP RealMPFR::powreal(const RealDouble &other) const mpc_pow_d(t.get_mpc_t(), t.get_mpc_t(), other.i, MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -537,7 +537,7 @@ RCP RealMPFR::powreal(const ComplexDouble &other) const mpc_pow(t.get_mpc_t(), s.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -554,7 +554,7 @@ RCP RealMPFR::powreal(const RealMPFR &other) const MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -576,7 +576,7 @@ RCP RealMPFR::rpowreal(const Integer &other) const mpc_pow(t.get_mpc_t(), t.get_mpc_t(), s.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -599,7 +599,7 @@ RCP RealMPFR::rpowreal(const Rational &other) const mpc_pow(t.get_mpc_t(), t.get_mpc_t(), s.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -622,7 +622,7 @@ RCP RealMPFR::rpowreal(const Complex &other) const mpc_pow(t.get_mpc_t(), s.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -639,7 +639,7 @@ RCP RealMPFR::rpowreal(const RealDouble &other) const mpc_pow(t.get_mpc_t(), t.get_mpc_t(), s.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -661,7 +661,7 @@ RCP RealMPFR::rpowreal(const ComplexDouble &other) const mpc_pow(t.get_mpc_t(), t.get_mpc_t(), s.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error("Result is complex. Recompile with MPC support."); + throw SymEngineException("Result is complex. Recompile with MPC support."); #endif } @@ -731,7 +731,7 @@ class EvaluateMPFR : public Evaluate mpc_asin(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -750,7 +750,7 @@ class EvaluateMPFR : public Evaluate mpc_acos(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -788,7 +788,7 @@ class EvaluateMPFR : public Evaluate mpc_acos(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -809,7 +809,7 @@ class EvaluateMPFR : public Evaluate mpc_asin(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -893,7 +893,7 @@ class EvaluateMPFR : public Evaluate mpc_acosh(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -912,7 +912,7 @@ class EvaluateMPFR : public Evaluate mpc_atanh(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -933,7 +933,7 @@ class EvaluateMPFR : public Evaluate mpc_atanh(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } @@ -952,7 +952,7 @@ class EvaluateMPFR : public Evaluate mpc_log(t.get_mpc_t(), t.get_mpc_t(), MPFR_RNDN); return complex_mpc(std::move(t)); #else - throw std::runtime_error( + throw SymEngineException( "Result is complex. Recompile with MPC support."); #endif } diff --git a/symengine/rings.cpp b/symengine/rings.cpp index 624dbbf459..aa6325e380 100644 --- a/symengine/rings.cpp +++ b/symengine/rings.cpp @@ -32,7 +32,7 @@ void expr2poly(const RCP &p, umap_basic_num &syms, umap_vec_mpz &P) exp[i] = rcp_static_cast(q.second) ->as_int(); } else { - throw std::runtime_error( + throw SymEngineException( "Cannot convert symbolic exponents to sparse " "polynomials with integer exponents."); } diff --git a/symengine/series.cpp b/symengine/series.cpp index ea01036483..93643cc21d 100644 --- a/symengine/series.cpp +++ b/symengine/series.cpp @@ -78,7 +78,7 @@ RCP series_invfunc(const RCP &ex, var->get_name(), prec); #else - throw std::runtime_error("Series reversion is supported only with Piranha"); + throw SymEngineException("Series reversion is supported only with Piranha"); #endif } diff --git a/symengine/series.h b/symengine/series.h index a69a48852e..1e7b4f110d 100644 --- a/symengine/series.h +++ b/symengine/series.h @@ -163,7 +163,7 @@ class SeriesBase : public SeriesCoeffInterface Series::var(var_), degree_); return make_rcp(p, var_, degree_); } else { - throw std::runtime_error("Unknown type"); + throw SymEngineException("Unknown type"); } } @@ -217,10 +217,10 @@ class SeriesBase : public SeriesCoeffInterface { const Coeff co = Series::find_cf(s, var, 0); if (co != 0) - throw std::runtime_error("reversion of series with constant term"); + throw SymEngineException("reversion of series with constant term"); const Coeff a = Series::find_cf(s, var, 1); if (a == 0) - throw std::runtime_error( + throw SymEngineException( "reversion of series with zero term of degree one"); Poly r(var); r /= a; diff --git a/symengine/series_flint.cpp b/symengine/series_flint.cpp index 15ce3d3af6..34880712ef 100644 --- a/symengine/series_flint.cpp +++ b/symengine/series_flint.cpp @@ -121,7 +121,7 @@ fqp_t URatPSeriesFlint::convert(const Rational &x) fqp_t URatPSeriesFlint::convert(const Basic &x) { - throw std::runtime_error("SeriesFlint::convert not Implemented"); + throw SymEngineException("SeriesFlint::convert not Implemented"); } fqp_t URatPSeriesFlint::pow(const fqp_t &s, int n, unsigned prec) diff --git a/symengine/series_piranha.cpp b/symengine/series_piranha.cpp index ceb69f0d50..f0573c5139 100644 --- a/symengine/series_piranha.cpp +++ b/symengine/series_piranha.cpp @@ -65,7 +65,7 @@ piranha::rational URatPSeriesPiranha::convert(const Rational &x) piranha::rational URatPSeriesPiranha::convert(const Basic &x) { - throw std::NotImplementedError("Not Implemented"); + throw NotImplementedError("Not Implemented"); } RCP URatPSeriesPiranha::as_basic() const @@ -164,7 +164,7 @@ piranha::rational URatPSeriesPiranha::root(piranha::rational &c, unsigned n) cl_root = static_cast(*cout).i; } if (not res) - throw std::runtime_error("constant term is not an nth power"); + throw SymEngineException("constant term is not an nth power"); return convert(cl_root); } diff --git a/symengine/series_visitor.h b/symengine/series_visitor.h index fccbc1ba99..3bfc51a3c7 100644 --- a/symengine/series_visitor.h +++ b/symengine/series_visitor.h @@ -55,7 +55,7 @@ class SeriesVisitor : public BaseVisitor> if (is_a(*exp)) { const Integer &ii = (static_cast(*exp)); if (not mp_fits_slong_p(ii.i)) - throw std::runtime_error("series power exponent size"); + throw SymEngineException("series power exponent size"); const int sh = mp_get_si(ii.i); base->accept(*this); if (sh == 1) { @@ -76,7 +76,7 @@ class SeriesVisitor : public BaseVisitor> const integer_class &expnumz = get_num(rat.i); const integer_class &expdenz = get_den(rat.i); if (not mp_fits_slong_p(expnumz) or not mp_fits_slong_p(expdenz)) - throw std::runtime_error("series rational power exponent size"); + throw SymEngineException("series rational power exponent size"); const int num = mp_get_si(expnumz); const int den = mp_get_si(expdenz); base->accept(*this); @@ -147,10 +147,10 @@ class SeriesVisitor : public BaseVisitor> void bvisit(const Series &x) { if (x.get_var() != varname) { - throw std::runtime_error("Multivariate Series not implemented"); + throw SymEngineException("Multivariate Series not implemented"); } if (x.get_degree() < prec) { - throw std::runtime_error("Series with lesser prec found"); + throw SymEngineException("Series with lesser prec found"); } p = x.get_poly(); } @@ -283,7 +283,7 @@ class SeriesVisitor : public BaseVisitor> if (!has_symbol(x, *symbol(varname))) { p = Series::convert(x); } else { - throw std::runtime_error("Not Implemented"); + throw SymEngineException("Not Implemented"); } } }; diff --git a/symengine/sparse_matrix.cpp b/symengine/sparse_matrix.cpp index 88ac40c8c5..5c28bdb92b 100644 --- a/symengine/sparse_matrix.cpp +++ b/symengine/sparse_matrix.cpp @@ -497,7 +497,7 @@ void csr_scale_rows(CSRMatrix &A, const DenseMatrix &X) for (unsigned i = 0; i < A.row_; i++) { if (eq(*(X.get(i, 0)), *zero)) - throw std::runtime_error("Scaling factor can't be zero"); + throw SymEngineException("Scaling factor can't be zero"); for (unsigned jj = A.p_[i]; jj < A.p_[i + 1]; jj++) A.x_[jj] = mul(A.x_[jj], X.get(i, 0)); } @@ -514,7 +514,7 @@ void csr_scale_columns(CSRMatrix &A, const DenseMatrix &X) for (i = 0; i < A.col_; i++) { if (eq(*(X.get(i, 0)), *zero)) - throw std::runtime_error("Scaling factor can't be zero"); + throw SymEngineException("Scaling factor can't be zero"); } for (i = 0; i < nnz; i++) diff --git a/symengine/subs.h b/symengine/subs.h index e0f1461ffb..c1bf82e9cc 100644 --- a/symengine/subs.h +++ b/symengine/subs.h @@ -187,7 +187,7 @@ class SubsVisitor : public BaseVisitor for (auto &p : x.get_symbols()) { auto t2 = p->subs(n); if (not is_a(*t2)) { - throw std::runtime_error("Error, expected a Symbol."); + throw SymEngineException("Error, expected a Symbol."); } t = t->diff(rcp_static_cast(t2)); } diff --git a/symengine/symengine_rcp.h b/symengine/symengine_rcp.h index e2984411b4..f27adad977 100644 --- a/symengine/symengine_rcp.h +++ b/symengine/symengine_rcp.h @@ -238,7 +238,7 @@ inline RCP rcp_dynamic_cast(const RCP &p1) return RCP(p); } } - throw std::runtime_error("rcp_dynamic_cast: cannot convert."); + throw SymEngineException("rcp_dynamic_cast: cannot convert."); } template diff --git a/symengine/tests/basic/test_arit.cpp b/symengine/tests/basic/test_arit.cpp index 9106b6e50f..305c35f4db 100644 --- a/symengine/tests/basic/test_arit.cpp +++ b/symengine/tests/basic/test_arit.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using SymEngine::Basic; using SymEngine::Add; @@ -43,6 +44,7 @@ using SymEngine::complex_double; using SymEngine::rational_class; using SymEngine::is_a; using SymEngine::set_basic; +using SymEngine::SymEngineException; TEST_CASE("Add: arit", "[arit]") { @@ -313,7 +315,7 @@ TEST_CASE("Mul: arit", "[arit]") REQUIRE(s.size() == 2); CHECK_THROWS_AS(Complex::from_two_nums(*one, *real_double(1.0)); - , std::runtime_error); + , SymEngineException); r1 = mul({}); REQUIRE(eq(*r1, *one)); diff --git a/symengine/tests/basic/test_functions.cpp b/symengine/tests/basic/test_functions.cpp index 7e6b605259..404688017b 100644 --- a/symengine/tests/basic/test_functions.cpp +++ b/symengine/tests/basic/test_functions.cpp @@ -2743,7 +2743,7 @@ TEST_CASE("MPFR and MPC: functions", "[functions]") REQUIRE(mpfr_cmp_z(a.get_mpfr_t(), get_mpz_t(q)) < 0); #else mpfr_set_si(a.get_mpfr_t(), 2, MPFR_RNDN); - CHECK_THROWS_AS(asin(real_mpfr(a)), std::runtime_error); + CHECK_THROWS_AS(asin(real_mpfr(a)), SymEngineException); #endif // HAVE_SYMENGINE_MPC #endif // HAVE_SYMENGINE_MPFR } diff --git a/symengine/tests/basic/test_infinity.cpp b/symengine/tests/basic/test_infinity.cpp index fd8ccb5d41..85072c7021 100644 --- a/symengine/tests/basic/test_infinity.cpp +++ b/symengine/tests/basic/test_infinity.cpp @@ -30,6 +30,7 @@ using SymEngine::Symbol; using SymEngine::symbol; using SymEngine::Complex; using SymEngine::NotImplementedError; +using SymEngine::SymEngineException; TEST_CASE("Constructors for Infinity", "[Infinity]") { @@ -62,7 +63,7 @@ TEST_CASE("Constructors for Infinity", "[Infinity]") REQUIRE(b->__str__() == "-oo"); // RCP cx = Complex::from_two_nums(*integer(1), *integer(1)); - // CHECK_THROWS_AS(Infty::from_direction(cx), std::runtime_error); + // CHECK_THROWS_AS(Infty::from_direction(cx), SymEngineException); } TEST_CASE("Hash Size for Infinity", "[Infinity]") @@ -155,9 +156,9 @@ TEST_CASE("Adding to Infinity", "[Infinity]") REQUIRE(n1->__str__() == "-oo"); n1 = c->add(*minus_one); REQUIRE(n1->__str__() == "zoo"); - CHECK_THROWS_AS(c->add(*c), std::runtime_error); - CHECK_THROWS_AS(c->add(*a), std::runtime_error); - CHECK_THROWS_AS(b->add(*a), std::runtime_error); + CHECK_THROWS_AS(c->add(*c), SymEngineException); + CHECK_THROWS_AS(c->add(*a), SymEngineException); + CHECK_THROWS_AS(b->add(*a), SymEngineException); } TEST_CASE("Subtracting from Infinity", "[Infinity]") @@ -189,9 +190,9 @@ TEST_CASE("Multiplication with Infinity", "[Infinity]") n2 = c->mul(*c); REQUIRE(n2->__str__() == "zoo"); - CHECK_THROWS_AS(a->mul(*zero), std::runtime_error); - CHECK_THROWS_AS(b->mul(*zero), std::runtime_error); - CHECK_THROWS_AS(c->mul(*zero), std::runtime_error); + CHECK_THROWS_AS(a->mul(*zero), SymEngineException); + CHECK_THROWS_AS(b->mul(*zero), SymEngineException); + CHECK_THROWS_AS(c->mul(*zero), SymEngineException); RCP cx = Complex::from_two_nums(*integer(1), *integer(1)); CHECK_THROWS_AS(c->mul(*cx), NotImplementedError); diff --git a/symengine/tests/basic/test_number.cpp b/symengine/tests/basic/test_number.cpp index a4e9f665a9..1f5c67640c 100644 --- a/symengine/tests/basic/test_number.cpp +++ b/symengine/tests/basic/test_number.cpp @@ -69,9 +69,9 @@ TEST_CASE("RealMPFR: arithmetic", "[number]") REQUIRE(is_a(*pownum(r5, half))); REQUIRE(is_a(*pownum(integer(-2), r5))); #else - CHECK_THROWS_AS(addnum(r1, c1), std::runtime_error); - CHECK_THROWS_AS(pownum(r5, half), std::runtime_error); - CHECK_THROWS_AS(pownum(integer(-2), r1), std::runtime_error); + CHECK_THROWS_AS(addnum(r1, c1), SymEngineException); + CHECK_THROWS_AS(pownum(r5, half), SymEngineException); + CHECK_THROWS_AS(pownum(integer(-2), r1), SymEngineException); #endif // HAVE_SYMENGINE_MPC #endif // HAVE_SYMENGINE_MPFR } diff --git a/symengine/tests/basic/test_rational.cpp b/symengine/tests/basic/test_rational.cpp index 71a832cc5e..7a1878c288 100644 --- a/symengine/tests/basic/test_rational.cpp +++ b/symengine/tests/basic/test_rational.cpp @@ -1,6 +1,7 @@ #include "catch.hpp" #include +#include using SymEngine::print_stack_on_segfault; using SymEngine::RCP; @@ -10,6 +11,7 @@ using SymEngine::Rational; using SymEngine::rational; using SymEngine::Number; using SymEngine::is_a; +using SymEngine::SymEngineException; TEST_CASE("Rational", "[rational]") { @@ -71,5 +73,5 @@ TEST_CASE("Rational is_power, nth root", "[rational is_power, nth root]") REQUIRE(res->__eq__(*qm1_3)); REQUIRE(q9_25->nth_root(outArg(res), 2)); REQUIRE(res->__eq__(*q3_5)); - CHECK_THROWS_AS(q9_25->nth_root(outArg(res), 0), std::runtime_error); + CHECK_THROWS_AS(q9_25->nth_root(outArg(res), 0), SymEngineException); } diff --git a/symengine/tests/basic/test_series_expansion_UP.cpp b/symengine/tests/basic/test_series_expansion_UP.cpp index ac8c74ce5f..0385a3b983 100644 --- a/symengine/tests/basic/test_series_expansion_UP.cpp +++ b/symengine/tests/basic/test_series_expansion_UP.cpp @@ -284,6 +284,6 @@ TEST_CASE("Check error when expansion called without Piranha ", RCP x = symbol("x"); auto ex1 = lambertw(x); REQUIRE_THROWS_AS(UPSeriesPiranha::series(ex1, "x", 10), - std::runtime_error); + SymEngineException); } #endif diff --git a/symengine/tests/basic/test_series_expansion_URatF.cpp b/symengine/tests/basic/test_series_expansion_URatF.cpp index fb6830a284..ffbc376ad5 100644 --- a/symengine/tests/basic/test_series_expansion_URatF.cpp +++ b/symengine/tests/basic/test_series_expansion_URatF.cpp @@ -219,11 +219,11 @@ TEST_CASE("Expression series expansion: atan, tan, asin, cot, sec, csc", // auto ex8 = cot(sin(x)); // auto ex10 = csc(x); // REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex7, "x", 10), - // std::runtime_error); + // SymEngineException); // REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex8, "x", 10), - // std::runtime_error); + // SymEngineException); // REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex10, "x", 10), - // std::runtime_error); + // SymEngineException); } TEST_CASE("Expression series expansion: sinh, cosh, tanh, asinh, atanh", @@ -261,8 +261,8 @@ TEST_CASE("Expression series expansion: lambertw ", "[Expansion of lambertw]") auto ex1 = lambertw(x); auto ex2 = lambertw(sin(x)); - REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex1, "x", 10), std::runtime_error); - REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex2, "x", 10), std::runtime_error); + REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex1, "x", 10), SymEngineException); + REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex2, "x", 10), SymEngineException); } #endif @@ -273,6 +273,6 @@ TEST_CASE("Check error when expansion called without Flint ", RCP x = symbol("x"); auto ex1 = lambertw(x); REQUIRE_THROWS_AS(URatPSeriesFlint::series(ex1, "x", 10), - std::runtime_error); + SymEngineException); } #endif diff --git a/symengine/tests/basic/test_series_expansion_URatP.cpp b/symengine/tests/basic/test_series_expansion_URatP.cpp index f60b0fb270..9aa98d7843 100644 --- a/symengine/tests/basic/test_series_expansion_URatP.cpp +++ b/symengine/tests/basic/test_series_expansion_URatP.cpp @@ -255,11 +255,11 @@ TEST_CASE("Expansion of sin ", "[Symbolic series expansion]") RCP x = symbol("x"); REQUIRE_THROWS_AS( URatPSeriesPiranha::series(sin(add(x, integer(1))), "x", 10), - std::runtime_error); + SymEngineException); REQUIRE_THROWS_AS( URatPSeriesPiranha::series( mul(sin(add(x, integer(1))), cos(add(x, integer(2)))), "x", 10), - std::runtime_error); + SymEngineException); } TEST_CASE("Expansion of log ", "[Symbolic series expansion]") @@ -267,7 +267,7 @@ TEST_CASE("Expansion of log ", "[Symbolic series expansion]") RCP x = symbol("x"); REQUIRE_THROWS_AS( URatPSeriesPiranha::series(log(add(x, integer(2))), "x", 10), - std::runtime_error); + SymEngineException); } #else @@ -277,6 +277,6 @@ TEST_CASE("Check error when expansion called without Piranha ", RCP x = symbol("x"); auto ex1 = lambertw(x); REQUIRE_THROWS_AS(URatPSeriesPiranha::series(ex1, "x", 10), - std::runtime_error); + SymEngineException); } #endif diff --git a/symengine/tests/basic/test_sets.cpp b/symengine/tests/basic/test_sets.cpp index 0e287a0dff..57a14e5f61 100644 --- a/symengine/tests/basic/test_sets.cpp +++ b/symengine/tests/basic/test_sets.cpp @@ -31,6 +31,7 @@ using SymEngine::boolean; using SymEngine::Inf; using SymEngine::NegInf; using SymEngine::NotImplementedError; +using SymEngine::SymEngineException; TEST_CASE("Interval : Basic", "[basic]") { @@ -139,7 +140,7 @@ TEST_CASE("Interval : Basic", "[basic]") REQUIRE(eq(*r5->get_args()[3], *boolean(r5->right_open_))); RCP c1 = Complex::from_two_nums(*i2, *i20); CHECK_THROWS_AS(interval(c1, one), NotImplementedError); - CHECK_THROWS_AS(r5->diff(symbol("x")), std::runtime_error); + CHECK_THROWS_AS(r5->diff(symbol("x")), SymEngineException); } TEST_CASE("EmptySet : Basic", "[basic]") @@ -163,7 +164,7 @@ TEST_CASE("EmptySet : Basic", "[basic]") REQUIRE(not r1->__eq__(*r2)); REQUIRE(r1->compare(*emptyset()) == 0); REQUIRE(not r1->contains(zero)); - CHECK_THROWS_AS(r1->diff(symbol("x")), std::runtime_error); + CHECK_THROWS_AS(r1->diff(symbol("x")), SymEngineException); } TEST_CASE("UniversalSet : Basic", "[basic]") @@ -196,7 +197,7 @@ TEST_CASE("UniversalSet : Basic", "[basic]") REQUIRE(r1->__hash__() == universalset()->__hash__()); REQUIRE(not r1->__eq__(*r2)); REQUIRE(r1->compare(*universalset()) == 0); - CHECK_THROWS_AS(r1->diff(symbol("x")), std::runtime_error); + CHECK_THROWS_AS(r1->diff(symbol("x")), SymEngineException); } TEST_CASE("FiniteSet : Basic", "[basic]") diff --git a/symengine/tests/eval/test_eval_arb.cpp b/symengine/tests/eval/test_eval_arb.cpp index b78c2e0877..d117720419 100644 --- a/symengine/tests/eval/test_eval_arb.cpp +++ b/symengine/tests/eval/test_eval_arb.cpp @@ -8,7 +8,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::RCP; using SymEngine::Basic; using SymEngine::integer; diff --git a/symengine/tests/eval/test_eval_double.cpp b/symengine/tests/eval/test_eval_double.cpp index 1266de8e01..a68815d55f 100644 --- a/symengine/tests/eval/test_eval_double.cpp +++ b/symengine/tests/eval/test_eval_double.cpp @@ -53,6 +53,7 @@ using SymEngine::max; using SymEngine::min; using SymEngine::min; using SymEngine::NotImplementedError; +using SymEngine::SymEngineException; TEST_CASE("eval_double: eval_double", "[eval_double]") { @@ -126,7 +127,7 @@ TEST_CASE("eval_double: eval_double", "[eval_double]") } // Symbol must raise an exception - CHECK_THROWS_AS(eval_double(*symbol("x")), std::runtime_error); + CHECK_THROWS_AS(eval_double(*symbol("x")), SymEngineException); CHECK_THROWS_AS(eval_double_single_dispatch(*symbol("x")), NotImplementedError); @@ -140,9 +141,9 @@ TEST_CASE("eval_double: eval_double", "[eval_double]") CHECK_THROWS_AS(eval_double_single_dispatch(*zeta(r1, r2)), NotImplementedError); - CHECK_THROWS_AS(eval_double(*constant("dummy")), std::runtime_error); + CHECK_THROWS_AS(eval_double(*constant("dummy")), SymEngineException); CHECK_THROWS_AS(eval_double_single_dispatch(*constant("dummy")), - std::runtime_error); + SymEngineException); // ... we don't test the rest of functions that are not implemented. } diff --git a/symengine/tests/eval/test_eval_mpc.cpp b/symengine/tests/eval/test_eval_mpc.cpp index 4522a05277..3bbb66eae2 100644 --- a/symengine/tests/eval/test_eval_mpc.cpp +++ b/symengine/tests/eval/test_eval_mpc.cpp @@ -3,7 +3,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::RCP; using SymEngine::Basic; using SymEngine::integer; diff --git a/symengine/tests/eval/test_eval_mpfr.cpp b/symengine/tests/eval/test_eval_mpfr.cpp index 3c7e79a97d..0f0700de9f 100644 --- a/symengine/tests/eval/test_eval_mpfr.cpp +++ b/symengine/tests/eval/test_eval_mpfr.cpp @@ -4,7 +4,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::RCP; using SymEngine::Basic; using SymEngine::integer; diff --git a/symengine/tests/eval/test_evalf.cpp b/symengine/tests/eval/test_evalf.cpp index cc129123d6..4dec6148fd 100644 --- a/symengine/tests/eval/test_evalf.cpp +++ b/symengine/tests/eval/test_evalf.cpp @@ -25,7 +25,9 @@ #ifdef HAVE_SYMENGINE_MPC #include #endif // HAVE_SYMENGINE_MPC +#include +using SymEngine::SymEngineException; using SymEngine::Basic; using SymEngine::constant; using SymEngine::Complex; diff --git a/symengine/tests/eval/test_lambda_double.cpp b/symengine/tests/eval/test_lambda_double.cpp index d7e626def8..fcb9d6007d 100644 --- a/symengine/tests/eval/test_lambda_double.cpp +++ b/symengine/tests/eval/test_lambda_double.cpp @@ -21,6 +21,7 @@ using SymEngine::gamma; using SymEngine::loggamma; using SymEngine::min; using SymEngine::NotImplementedError; +using SymEngine::SymEngineException; TEST_CASE("Evaluate to double", "[lambda_double]") { @@ -59,7 +60,7 @@ TEST_CASE("Evaluate to double", "[lambda_double]") NotImplementedError); // Undefined symbols raise an exception - CHECK_THROWS_AS(v.init({x}, *r), std::runtime_error); + CHECK_THROWS_AS(v.init({x}, *r), SymEngineException); } TEST_CASE("Evaluate to std::complex", "[lambda_complex_double]") @@ -88,7 +89,7 @@ TEST_CASE("Evaluate to std::complex", "[lambda_complex_double]") REQUIRE(::fabs(d.imag() - 0.0) < 1e-12); // Undefined symbols raise an exception - CHECK_THROWS_AS(v.init({x}, *r), std::runtime_error); + CHECK_THROWS_AS(v.init({x}, *r), SymEngineException); } TEST_CASE("Evaluate functions", "[lambda_gamma]") diff --git a/symengine/tests/logic/test_logic.cpp b/symengine/tests/logic/test_logic.cpp index 2b0ec4238a..566d8e385d 100644 --- a/symengine/tests/logic/test_logic.cpp +++ b/symengine/tests/logic/test_logic.cpp @@ -3,7 +3,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::Basic; using SymEngine::Integer; using SymEngine::integer; @@ -35,7 +37,7 @@ TEST_CASE("BooleanAtom : Basic", "[basic]") REQUIRE(unified_eq(v, u)); auto x = symbol("x"); - CHECK_THROWS_AS(boolTrue->diff(x), std::runtime_error); + CHECK_THROWS_AS(boolTrue->diff(x), SymEngineException); REQUIRE(not eq(*boolTrue, *boolFalse)); REQUIRE(eq(*boolFalse, *boolean(false))); @@ -62,7 +64,7 @@ TEST_CASE("Contains", "[logic]") vec_basic u = {x, int1}; REQUIRE(unified_eq(v, u)); - CHECK_THROWS_AS(p->diff(x), std::runtime_error); + CHECK_THROWS_AS(p->diff(x), SymEngineException); } TEST_CASE("Piecewise", "[logic]") diff --git a/symengine/tests/matrix/test_matrix.cpp b/symengine/tests/matrix/test_matrix.cpp index 92054f3449..6e7a3cbb43 100644 --- a/symengine/tests/matrix/test_matrix.cpp +++ b/symengine/tests/matrix/test_matrix.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using SymEngine::print_stack_on_segfault; using SymEngine::RCP; @@ -23,6 +24,7 @@ using SymEngine::diag; using SymEngine::vec_basic; using SymEngine::function_symbol; using SymEngine::permutelist; +using SymEngine::SymEngineException; TEST_CASE("test_get_set(): matrices", "[matrices]") { @@ -859,7 +861,7 @@ TEST_CASE("test_pivoted_LU(): matrices", "[matrices]") A = DenseMatrix(3, 3, {integer(0), integer(0), integer(0), integer(0), integer(0), integer(0), integer(8), integer(3), integer(1)}); - CHECK_THROWS_AS(pivoted_LU(A, L, U, pl), std::runtime_error); + CHECK_THROWS_AS(pivoted_LU(A, L, U, pl), SymEngineException); } TEST_CASE("test_fraction_free_LDU(): matrices", "[matrices]") @@ -1358,7 +1360,7 @@ TEST_CASE("test_csr_scale_rows(): matrices", "[matrices]") integer(15), integer(18)})); X = DenseMatrix(3, 1, {integer(1), integer(0), integer(-1)}); - CHECK_THROWS_AS(csr_scale_columns(A, X), std::runtime_error); + CHECK_THROWS_AS(csr_scale_columns(A, X), SymEngineException); } TEST_CASE("test_csr_scale_columns(): matrices", "[matrices]") @@ -1375,7 +1377,7 @@ TEST_CASE("test_csr_scale_columns(): matrices", "[matrices]") integer(-5), integer(18)})); X = DenseMatrix(3, 1, {integer(0), integer(1), integer(-1)}); - CHECK_THROWS_AS(csr_scale_columns(A, X), std::runtime_error); + CHECK_THROWS_AS(csr_scale_columns(A, X), SymEngineException); } TEST_CASE("test_csr_binop_csr_canonical(): matrices", "[matrices]") @@ -1493,7 +1495,7 @@ TEST_CASE("Test Jacobian", "[matrices]") integer(1), integer(0), integer(0)})); X = DenseMatrix(4, 1, {f, y, z, t}); - CHECK_THROWS_AS(jacobian(A, X, J), std::runtime_error); + CHECK_THROWS_AS(jacobian(A, X, J), SymEngineException); A = DenseMatrix( 4, 1, {add(x, z), mul(y, z), add(mul(z, x), add(y, t)), add(x, y)}); diff --git a/symengine/tests/polynomial/test_basic_conversions.cpp b/symengine/tests/polynomial/test_basic_conversions.cpp index 503d754fc0..85edf11098 100644 --- a/symengine/tests/polynomial/test_basic_conversions.cpp +++ b/symengine/tests/polynomial/test_basic_conversions.cpp @@ -5,7 +5,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::symbol; using SymEngine::Symbol; using SymEngine::Integer; @@ -247,55 +249,55 @@ TEST_CASE("basic_to_poly UInt", "[b2poly]") // x + y basic = add(x, y); gen = x; - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // x + 1/2 basic = add(x, hf); gen = x; - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // x/2 + 1 basic = add(div(x, i2), one); gen = x; - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // x + 1/x basic = add(x, div(one, x)); gen = x; - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // xy + 1 basic = add(mul(x, y), one); gen = x; - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // x**(1/2) + 1 basic = add(pow(x, hf), one); gen = x; - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // 3**x + 2**x basic = add(pow(i3, x), pow(i2, x)); gen = twopx; - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // 2**(2**(2x + 1)) + 2**(2**x) basic = add(pow(i2, twopx), pow(i2, pow(i2, add(mul(i2, x), one)))); gen = pow(i2, twopx); - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // 9**(x + (1/3)) basic = pow(i9, add(div(one, i3), x)); gen = pow(i9, x); - CHECK_THROWS_AS(from_basic(basic, gen), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic, gen), SymEngineException); // x + y basic = add(x, y); - CHECK_THROWS_AS(from_basic(basic), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic), SymEngineException); // x + 1/x basic = add(x, div(one, x)); - CHECK_THROWS_AS(from_basic(basic), std::runtime_error); + CHECK_THROWS_AS(from_basic(basic), SymEngineException); } TEST_CASE("basic_to_poly UExpr", "[b2poly]") @@ -463,4 +465,4 @@ TEST_CASE("basic_to_poly UIntFlint", "[b2poly]") poly2 = UIntPolyFlint::from_vec(gen, {{0_z, 0_z, 1_z, 0_z, 0_z, 4_z}}); REQUIRE(eq(*poly1, *poly2)); } -#endif \ No newline at end of file +#endif diff --git a/symengine/tests/polynomial/test_uexprpoly.cpp b/symengine/tests/polynomial/test_uexprpoly.cpp index cce56329d5..ad18724f9c 100644 --- a/symengine/tests/polynomial/test_uexprpoly.cpp +++ b/symengine/tests/polynomial/test_uexprpoly.cpp @@ -2,6 +2,7 @@ #include #include +#include using SymEngine::Expression; using SymEngine::UExprPoly; @@ -19,6 +20,7 @@ using SymEngine::one; using SymEngine::zero; using SymEngine::integer; using SymEngine::add; +using SymEngine::SymEngineException; using namespace SymEngine::literals; @@ -76,7 +78,7 @@ TEST_CASE("Adding two UExprPoly", "[UExprPoly]") REQUIRE(add_upoly(*d, a)->__str__() == "a*x**2 + 2*x + 3"); d = uexpr_poly(y, {{0, 2}, {1, 4}}); - CHECK_THROWS_AS(add_upoly(a, *d), std::runtime_error); + CHECK_THROWS_AS(add_upoly(a, *d), SymEngineException); } TEST_CASE("Negative of a UExprPoly", "[UExprPoly]") @@ -112,7 +114,7 @@ TEST_CASE("Subtracting two UExprPoly", "[UExprPoly]") REQUIRE(sub_upoly(*d, a)->__str__() == "-x**2 - 2*x + 1"); d = uexpr_poly(y, {{0, 2}, {1, 4}}); - CHECK_THROWS_AS(sub_upoly(a, *d), std::runtime_error); + CHECK_THROWS_AS(sub_upoly(a, *d), SymEngineException); } TEST_CASE("Multiplication of two UExprPoly", "[UExprPoly]") @@ -139,7 +141,7 @@ TEST_CASE("Multiplication of two UExprPoly", "[UExprPoly]") REQUIRE(mul_upoly(*f, *a)->__str__() == "2*a*x**2 + 2*b*x + 2"); f = uexpr_poly(y, {{0, 2}, {1, 4}}); - CHECK_THROWS_AS(mul_upoly(*a, *f), std::runtime_error); + CHECK_THROWS_AS(mul_upoly(*a, *f), SymEngineException); f = uexpr_poly(x, map_int_Expr{}); REQUIRE(mul_upoly(*a, *f)->__str__() == "0"); diff --git a/symengine/tests/polynomial/test_uintpoly.cpp b/symengine/tests/polynomial/test_uintpoly.cpp index 7f587e8278..1f4f8cd569 100644 --- a/symengine/tests/polynomial/test_uintpoly.cpp +++ b/symengine/tests/polynomial/test_uintpoly.cpp @@ -5,7 +5,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::UIntPoly; using SymEngine::Symbol; using SymEngine::symbol; @@ -71,7 +73,7 @@ TEST_CASE("Adding two UIntPoly", "[UIntPoly]") RCP g = UIntPoly::from_dict(y, {{0, 2_z}, {1, 3_z}, {2, 4_z}}); - CHECK_THROWS_AS(add_upoly(*a, *g), std::runtime_error); + CHECK_THROWS_AS(add_upoly(*a, *g), SymEngineException); } TEST_CASE("Negative of a UIntPoly", "[UIntPoly]") @@ -105,7 +107,7 @@ TEST_CASE("Subtracting two UIntPoly", "[UIntPoly]") REQUIRE(d->__str__() == "-x**2 - 2*x + 1"); d = sub_upoly(*a, *c); REQUIRE(d->__str__() == "x**2 + 2*x - 1"); - CHECK_THROWS_AS(sub_upoly(*a, *f), std::runtime_error); + CHECK_THROWS_AS(sub_upoly(*a, *f), SymEngineException); } TEST_CASE("Multiplication of two UIntPoly", "[UIntPoly]") @@ -145,7 +147,7 @@ TEST_CASE("Multiplication of two UIntPoly", "[UIntPoly]") REQUIRE(mul_upoly(*c, *a)->__str__() == "-x**2 - 2*x - 1"); c = UIntPoly::from_dict(y, {{0, -1_z}}); - CHECK_THROWS_AS(mul_upoly(*a, *c), std::runtime_error); + CHECK_THROWS_AS(mul_upoly(*a, *c), SymEngineException); } TEST_CASE("Comparing two UIntPoly", "[UIntPoly]") @@ -259,4 +261,4 @@ TEST_CASE("UIntPoly divides", "[UIntPoly]") REQUIRE(divides_upoly(*b, *c, outArg(res))); REQUIRE(res->__str__() == "2*x + 2"); REQUIRE(!divides_upoly(*b, *a, outArg(res))); -} \ No newline at end of file +} diff --git a/symengine/tests/polynomial/test_uintpoly_flint.cpp b/symengine/tests/polynomial/test_uintpoly_flint.cpp index e80cb6df24..17fc6323b7 100644 --- a/symengine/tests/polynomial/test_uintpoly_flint.cpp +++ b/symengine/tests/polynomial/test_uintpoly_flint.cpp @@ -5,7 +5,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::UIntPolyFlint; using SymEngine::Symbol; using SymEngine::add; @@ -76,7 +78,7 @@ TEST_CASE("Adding two UIntPolyFlint", "[UIntPolyFlint]") RCP g = UIntPolyFlint::from_dict(y, {{0, 2_z}, {1, 3_z}, {2, 4_z}}); - CHECK_THROWS_AS(add_upoly(*a, *g), std::runtime_error); + CHECK_THROWS_AS(add_upoly(*a, *g), SymEngineException); } TEST_CASE("Negative of a UIntPolyFlint", "[UIntPolyFlint]") @@ -110,7 +112,7 @@ TEST_CASE("Subtracting two UIntPolyFlint", "[UIntPolyFlint]") REQUIRE(d->__str__() == "-x**2 - 2*x + 1"); d = sub_upoly(*a, *c); REQUIRE(d->__str__() == "x**2 + 2*x - 1"); - CHECK_THROWS_AS(sub_upoly(*a, *f), std::runtime_error); + CHECK_THROWS_AS(sub_upoly(*a, *f), SymEngineException); } TEST_CASE("Multiplication of two UIntPolyFlint", "[UIntPolyFlint]") @@ -150,7 +152,7 @@ TEST_CASE("Multiplication of two UIntPolyFlint", "[UIntPolyFlint]") REQUIRE(mul_upoly(*c, *a)->__str__() == "-x**2 - 2*x - 1"); c = UIntPolyFlint::from_dict(y, {{0, -1_z}}); - CHECK_THROWS_AS(mul_upoly(*a, *c), std::runtime_error); + CHECK_THROWS_AS(mul_upoly(*a, *c), SymEngineException); } TEST_CASE("Evaluation of UIntPolyFlint", "[UIntPolyFlint]") diff --git a/symengine/tests/polynomial/test_uintpoly_piranha.cpp b/symengine/tests/polynomial/test_uintpoly_piranha.cpp index 24ec1e06b8..b23f20e15b 100644 --- a/symengine/tests/polynomial/test_uintpoly_piranha.cpp +++ b/symengine/tests/polynomial/test_uintpoly_piranha.cpp @@ -7,7 +7,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::UIntPolyPiranha; using SymEngine::Symbol; using SymEngine::add; @@ -74,7 +76,7 @@ TEST_CASE("Adding two UIntPolyPiranha", "[UIntPolyPiranha]") RCP g = UIntPolyPiranha::from_dict(y, {{0, 2_z}, {1, 3_z}, {2, 4_z}}); - CHECK_THROWS_AS(add_upoly(*a, *g), std::runtime_error); + CHECK_THROWS_AS(add_upoly(*a, *g), SymEngineException); } TEST_CASE("Negative of a UIntPolyPiranha", "[UIntPolyPiranha]") @@ -108,7 +110,7 @@ TEST_CASE("Subtracting two UIntPolyPiranha", "[UIntPolyPiranha]") REQUIRE(d->__str__() == "-x**2 - 2*x + 1"); d = sub_upoly(*a, *c); REQUIRE(d->__str__() == "x**2 + 2*x - 1"); - CHECK_THROWS_AS(sub_upoly(*a, *f), std::runtime_error); + CHECK_THROWS_AS(sub_upoly(*a, *f), SymEngineException); } TEST_CASE("Multiplication of two UIntPolyPiranha", "[UIntPolyPiranha]") @@ -148,7 +150,7 @@ TEST_CASE("Multiplication of two UIntPolyPiranha", "[UIntPolyPiranha]") REQUIRE(mul_upoly(*c, *a)->__str__() == "-x**2 - 2*x - 1"); c = UIntPolyPiranha::from_dict(y, {{0, -1_z}}); - CHECK_THROWS_AS(mul_upoly(*a, *c), std::runtime_error); + CHECK_THROWS_AS(mul_upoly(*a, *c), SymEngineException); } TEST_CASE("Evaluation of UIntPolyPiranha", "[UIntPolyPiranha]") diff --git a/symengine/tests/polynomial/test_uratpoly.cpp b/symengine/tests/polynomial/test_uratpoly.cpp index ada97521ea..9b249f7cf0 100644 --- a/symengine/tests/polynomial/test_uratpoly.cpp +++ b/symengine/tests/polynomial/test_uratpoly.cpp @@ -3,7 +3,9 @@ #include #include +#include +using SymEngine::SymEngineException; using SymEngine::URatPoly; using SymEngine::Symbol; using SymEngine::symbol; @@ -62,7 +64,7 @@ TEST_CASE("Adding two URatPoly", "[URatPoly]") RCP g = URatPoly::from_dict( y, {{0, 2_q}, {1, rc(-3_z, 2_z)}, {2, rc(1_z, 4_z)}}); - CHECK_THROWS_AS(add_upoly(*a, *g), std::runtime_error); + CHECK_THROWS_AS(add_upoly(*a, *g), SymEngineException); } TEST_CASE("Negative of a URatPoly", "[URatPoly]") @@ -94,7 +96,7 @@ TEST_CASE("Subtracting two URatPoly", "[URatPoly]") REQUIRE(d->__str__() == "-3/4*x**2 - 13/6*x + 3/2"); d = sub_upoly(*a, *c); REQUIRE(d->__str__() == "3/4*x**2 + 13/6*x - 3/2"); - CHECK_THROWS_AS(sub_upoly(*a, *f), std::runtime_error); + CHECK_THROWS_AS(sub_upoly(*a, *f), SymEngineException); } TEST_CASE("Multiplication of two URatPoly", "[URatPoly]") @@ -128,7 +130,7 @@ TEST_CASE("Multiplication of two URatPoly", "[URatPoly]") REQUIRE(mul_upoly(*c, *a)->__str__() == "-x**2 - 2/3*x - 1/2"); c = URatPoly::from_dict(y, {{0, rc(-1_z)}}); - CHECK_THROWS_AS(mul_upoly(*a, *c), std::runtime_error); + CHECK_THROWS_AS(mul_upoly(*a, *c), SymEngineException); } TEST_CASE("Comparing two URatPoly", "[URatPoly]") @@ -234,4 +236,4 @@ TEST_CASE("URatPoly divides", "[URatPoly]") REQUIRE(divides_upoly(*b, *a, outArg(res))); REQUIRE(res->__str__() == "1/4*x + 1/4"); REQUIRE(!divides_upoly(*a, *b, outArg(res))); -} \ No newline at end of file +} diff --git a/symengine/tests/polynomial/test_uratpoly_flint.cpp b/symengine/tests/polynomial/test_uratpoly_flint.cpp index 4dae49527d..5eeb369d57 100644 --- a/symengine/tests/polynomial/test_uratpoly_flint.cpp +++ b/symengine/tests/polynomial/test_uratpoly_flint.cpp @@ -3,7 +3,9 @@ #include #include +#include +using SymEngine::SymEngineException; using SymEngine::URatPolyFlint; using SymEngine::Symbol; using SymEngine::symbol; @@ -62,7 +64,7 @@ TEST_CASE("Adding two URatPolyFlint", "[URatPolyFlint]") RCP g = URatPolyFlint::from_dict( y, {{0, 2_q}, {1, rc(-3_z, 2_z)}, {2, rc(1_z, 4_z)}}); - CHECK_THROWS_AS(add_upoly(*a, *g), std::runtime_error); + CHECK_THROWS_AS(add_upoly(*a, *g), SymEngineException); } TEST_CASE("Negative of a URatPolyFlint", "[URatPolyFlint]") @@ -94,7 +96,7 @@ TEST_CASE("Subtracting two URatPolyFlint", "[URatPolyFlint]") REQUIRE(d->__str__() == "-3/4*x**2 - 13/6*x + 3/2"); d = sub_upoly(*a, *c); REQUIRE(d->__str__() == "3/4*x**2 + 13/6*x - 3/2"); - CHECK_THROWS_AS(sub_upoly(*a, *f), std::runtime_error); + CHECK_THROWS_AS(sub_upoly(*a, *f), SymEngineException); } TEST_CASE("Multiplication of two URatPolyFlint", "[URatPolyFlint]") @@ -128,7 +130,7 @@ TEST_CASE("Multiplication of two URatPolyFlint", "[URatPolyFlint]") REQUIRE(mul_upoly(*c, *a)->__str__() == "-x**2 - 2/3*x - 1/2"); c = URatPolyFlint::from_dict(y, {{0, rc(-1_z)}}); - CHECK_THROWS_AS(mul_upoly(*a, *c), std::runtime_error); + CHECK_THROWS_AS(mul_upoly(*a, *c), SymEngineException); } TEST_CASE("Comparing two URatPolyFlint", "[URatPolyFlint]") @@ -277,4 +279,4 @@ TEST_CASE("URatPolyFlint lcm", "[URatPolyFlint]") REQUIRE(ab->__str__() == "x**2"); REQUIRE(bc->__str__() == "x**2 + x"); REQUIRE(ac->__str__() == "x**3 + x**2"); -} \ No newline at end of file +} diff --git a/symengine/tests/polynomial/test_uratpoly_piranha.cpp b/symengine/tests/polynomial/test_uratpoly_piranha.cpp index e5922343c0..da617002e9 100644 --- a/symengine/tests/polynomial/test_uratpoly_piranha.cpp +++ b/symengine/tests/polynomial/test_uratpoly_piranha.cpp @@ -3,7 +3,9 @@ #include #include +#include +using SymEngine::SymEngineException; using SymEngine::URatPolyPiranha; using SymEngine::Symbol; using SymEngine::symbol; @@ -63,7 +65,7 @@ TEST_CASE("Adding two URatPolyPiranha", "[URatPolyPiranha]") RCP g = URatPolyPiranha::from_dict( y, {{0, 2_q}, {1, rc(-3_z, 2_z)}, {2, rc(1_z, 4_z)}}); - CHECK_THROWS_AS(add_upoly(*a, *g), std::runtime_error); + CHECK_THROWS_AS(add_upoly(*a, *g), SymEngineException); } TEST_CASE("Negative of a URatPolyPiranha", "[URatPolyPiranha]") @@ -95,7 +97,7 @@ TEST_CASE("Subtracting two URatPolyPiranha", "[URatPolyPiranha]") REQUIRE(d->__str__() == "-3/4*x**2 - 13/6*x + 3/2"); d = sub_upoly(*a, *c); REQUIRE(d->__str__() == "3/4*x**2 + 13/6*x - 3/2"); - CHECK_THROWS_AS(sub_upoly(*a, *f), std::runtime_error); + CHECK_THROWS_AS(sub_upoly(*a, *f), SymEngineException); } TEST_CASE("Multiplication of two URatPolyPiranha", "[URatPolyPiranha]") @@ -129,7 +131,7 @@ TEST_CASE("Multiplication of two URatPolyPiranha", "[URatPolyPiranha]") REQUIRE(mul_upoly(*c, *a)->__str__() == "-x**2 - 2/3*x - 1/2"); c = URatPolyPiranha::from_dict(y, {{0, rc(-1_z)}}); - CHECK_THROWS_AS(mul_upoly(*a, *c), std::runtime_error); + CHECK_THROWS_AS(mul_upoly(*a, *c), SymEngineException); } TEST_CASE("Comparing two URatPolyPiranha", "[URatPolyPiranha]") @@ -274,4 +276,4 @@ TEST_CASE("URatPolyPiranha divides", "[URatPolyPiranha]") REQUIRE(divides_upoly(*b, *a, outArg(res))); REQUIRE(res->__str__() == "1/4*x + 1/4"); REQUIRE(!divides_upoly(*a, *b, outArg(res))); -} \ No newline at end of file +} From e9ac6c2f410421a7ea4be9d56269e1af5a1aaf13 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 5 Aug 2016 10:49:53 +0530 Subject: [PATCH 18/19] Fixing errors --- symengine/tests/basic/test_functions.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/symengine/tests/basic/test_functions.cpp b/symengine/tests/basic/test_functions.cpp index 404688017b..9adfca5ca6 100644 --- a/symengine/tests/basic/test_functions.cpp +++ b/symengine/tests/basic/test_functions.cpp @@ -96,6 +96,7 @@ using SymEngine::integer_class; using SymEngine::get_mpz_t; using SymEngine::DivisionByZeroError; using SymEngine::NotImplementedError; +using SymEngine::SymEngineException; using namespace SymEngine::literals; From c0a0b73a1acd406861deae7f09bf8a82c30142e1 Mon Sep 17 00:00:00 2001 From: Rajith Vidanaarachchi Date: Fri, 12 Aug 2016 12:19:15 +0530 Subject: [PATCH 19/19] Fixing errors in tests --- symengine/tests/basic/test_as_numer_denom.cpp | 2 ++ symengine/tests/basic/test_fields.cpp | 2 ++ symengine/tests/basic/test_integer.cpp | 2 ++ symengine/tests/basic/test_number.cpp | 2 ++ symengine/tests/basic/test_poly.cpp | 2 ++ symengine/tests/basic/test_series_expansion_UP.cpp | 2 ++ symengine/tests/basic/test_series_expansion_URatF.cpp | 2 ++ symengine/tests/basic/test_series_expansion_URatP.cpp | 2 ++ symengine/tests/basic/test_series_generic.cpp | 1 + 9 files changed, 17 insertions(+) diff --git a/symengine/tests/basic/test_as_numer_denom.cpp b/symengine/tests/basic/test_as_numer_denom.cpp index b012f2f141..c19c5ef8db 100644 --- a/symengine/tests/basic/test_as_numer_denom.cpp +++ b/symengine/tests/basic/test_as_numer_denom.cpp @@ -2,7 +2,9 @@ #include #include +#include +using SymEngine::SymEngineException; using SymEngine::Basic; using SymEngine::Add; using SymEngine::Mul; diff --git a/symengine/tests/basic/test_fields.cpp b/symengine/tests/basic/test_fields.cpp index ab05f52830..e6ceef1185 100644 --- a/symengine/tests/basic/test_fields.cpp +++ b/symengine/tests/basic/test_fields.cpp @@ -6,7 +6,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::RCP; using SymEngine::Symbol; using SymEngine::symbol; diff --git a/symengine/tests/basic/test_integer.cpp b/symengine/tests/basic/test_integer.cpp index 76f2234c6b..a97d4b8992 100644 --- a/symengine/tests/basic/test_integer.cpp +++ b/symengine/tests/basic/test_integer.cpp @@ -1,7 +1,9 @@ #include "catch.hpp" #include +#include +using SymEngine::SymEngineException; using SymEngine::print_stack_on_segfault; using SymEngine::RCP; using SymEngine::Integer; diff --git a/symengine/tests/basic/test_number.cpp b/symengine/tests/basic/test_number.cpp index 1f5c67640c..5782c24b8e 100644 --- a/symengine/tests/basic/test_number.cpp +++ b/symengine/tests/basic/test_number.cpp @@ -3,7 +3,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::print_stack_on_segfault; using SymEngine::RCP; using SymEngine::Integer; diff --git a/symengine/tests/basic/test_poly.cpp b/symengine/tests/basic/test_poly.cpp index 1f5059663f..b66093cadd 100644 --- a/symengine/tests/basic/test_poly.cpp +++ b/symengine/tests/basic/test_poly.cpp @@ -5,7 +5,9 @@ #include #include #include +#include +using SymEngine::SymEngineException; using SymEngine::Basic; using SymEngine::Add; using SymEngine::Mul; diff --git a/symengine/tests/basic/test_series_expansion_UP.cpp b/symengine/tests/basic/test_series_expansion_UP.cpp index 0385a3b983..2c471126bd 100644 --- a/symengine/tests/basic/test_series_expansion_UP.cpp +++ b/symengine/tests/basic/test_series_expansion_UP.cpp @@ -1,7 +1,9 @@ #include #include "catch.hpp" #include +#include +using SymEngine::SymEngineException; using SymEngine::Basic; using SymEngine::Integer; using SymEngine::integer; diff --git a/symengine/tests/basic/test_series_expansion_URatF.cpp b/symengine/tests/basic/test_series_expansion_URatF.cpp index ffbc376ad5..34bdada9bb 100644 --- a/symengine/tests/basic/test_series_expansion_URatF.cpp +++ b/symengine/tests/basic/test_series_expansion_URatF.cpp @@ -1,7 +1,9 @@ #include #include "catch.hpp" #include +#include +using SymEngine::SymEngineException; using SymEngine::Basic; using SymEngine::Integer; using SymEngine::integer; diff --git a/symengine/tests/basic/test_series_expansion_URatP.cpp b/symengine/tests/basic/test_series_expansion_URatP.cpp index 9aa98d7843..f4e5b44693 100644 --- a/symengine/tests/basic/test_series_expansion_URatP.cpp +++ b/symengine/tests/basic/test_series_expansion_URatP.cpp @@ -1,7 +1,9 @@ #include #include "catch.hpp" #include +#include +using SymEngine::SymEngineException; using SymEngine::Basic; using SymEngine::Integer; using SymEngine::integer; diff --git a/symengine/tests/basic/test_series_generic.cpp b/symengine/tests/basic/test_series_generic.cpp index e0572265f2..2b47fae292 100644 --- a/symengine/tests/basic/test_series_generic.cpp +++ b/symengine/tests/basic/test_series_generic.cpp @@ -33,6 +33,7 @@ using SymEngine::pi; using SymEngine::I; using SymEngine::UndefinedError; using SymEngine::NotImplementedError; +using SymEngine::SymEngineException; using namespace SymEngine::literals;