5#include <symengine/cwrapper.h>
6#include <symengine/printers.h>
7#include <symengine/matrix.h>
9#include <symengine/parser.h>
10#include <symengine/lambda_double.h>
12#ifdef HAVE_SYMENGINE_LLVM
13#include <symengine/llvm_double.h>
16#ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
30using SymEngine::down_cast;
31using SymEngine::function_symbol;
33using SymEngine::has_symbol;
35using SymEngine::integer_class;
39using SymEngine::rational_class;
44#ifdef HAVE_SYMENGINE_MPFR
48#ifdef HAVE_SYMENGINE_MPC
53using SymEngine::rcp_static_cast;
60#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
61using SymEngine::get_mpq_t;
62using SymEngine::get_mpz_t;
64using SymEngine::ccode;
67using SymEngine::jscode;
68using SymEngine::julia_str;
69using SymEngine::latex;
70using SymEngine::mathml;
71using SymEngine::mp_get_si;
72using SymEngine::mp_get_ui;
73using SymEngine::numeric_cast;
75using SymEngine::parse;
76using SymEngine::SymEngineException;
77using SymEngine::zeros;
83inline bool is_aligned(T *p,
size_t n =
alignof(T))
85 return 0 ==
reinterpret_cast<uintptr_t
>(p) % n;
96#define CWRAPPER_BEGIN try {
99 return SYMENGINE_NO_EXCEPTION; \
101 catch (SymEngineException & e) \
103 return e.error_code(); \
107 return SYMENGINE_RUNTIME_ERROR; \
111 SymEngine::RCP<const SymEngine::Basic> m;
119 "Size of 'basic' is not correct");
122 "Alignment of 'basic' is not correct");
124void basic_new_stack(basic s)
129void basic_free_stack(basic s)
144const char *symengine_version()
146 return SYMENGINE_VERSION;
149void basic_const_set(basic s,
const char *c)
154void basic_const_zero(basic s)
156 s->m = SymEngine::zero;
159void basic_const_one(basic s)
161 s->m = SymEngine::one;
164void basic_const_minus_one(basic s)
166 s->m = SymEngine::minus_one;
169void basic_const_I(basic s)
174void basic_const_pi(basic s)
176 s->m = SymEngine::pi;
179void basic_const_E(basic s)
184void basic_const_EulerGamma(basic s)
186 s->m = SymEngine::EulerGamma;
189void basic_const_Catalan(basic s)
191 s->m = SymEngine::Catalan;
194void basic_const_GoldenRatio(basic s)
196 s->m = SymEngine::GoldenRatio;
199void basic_const_infinity(basic s)
201 s->m = SymEngine::Inf;
204void basic_const_neginfinity(basic s)
206 s->m = SymEngine::NegInf;
209void basic_const_complex_infinity(basic s)
211 s->m = SymEngine::ComplexInf;
214void basic_const_nan(basic s)
216 s->m = SymEngine::Nan;
219TypeID basic_get_class_id(
const char *c)
222#define SYMENGINE_INCLUDE_ALL
223#define SYMENGINE_ENUM(type, Class) {xstr(Class), type},
224#include "symengine/type_codes.inc"
226#undef SYMENGINE_INCLUDE_ALL
227 {
"", SYMENGINE_TypeID_Count}};
232char *basic_get_class_from_id(TypeID
id)
235#define SYMENGINE_INCLUDE_ALL
236#define SYMENGINE_ENUM(type, Class) {type, xstr(Class)},
237#include "symengine/type_codes.inc"
239#undef SYMENGINE_INCLUDE_ALL
240 {SYMENGINE_TypeID_Count,
""}};
243 auto cc =
new char[name.
length() + 1];
248TypeID basic_get_type(
const basic s)
250 return static_cast<TypeID
>(s->m->get_type_code());
253CWRAPPER_OUTPUT_TYPE symbol_set(basic s,
const char *c)
260int number_is_zero(
const basic s)
262 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
263 return (
int)((down_cast<const Number &>(*(s->m))).is_zero());
266int number_is_negative(
const basic s)
268 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
269 return (
int)((down_cast<const Number &>(*(s->m))).is_negative());
272int number_is_positive(
const basic s)
274 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
275 return (
int)((down_cast<const Number &>(*(s->m))).is_positive());
278int number_is_complex(
const basic s)
280 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
281 return (
int)((down_cast<const Number &>(*(s->m))).is_complex());
284int basic_has_symbol(
const basic e,
const basic s)
286 return (
int)(has_symbol(*(e->m), *(s->m)));
289CWRAPPER_OUTPUT_TYPE integer_set_si(basic s,
long i)
296CWRAPPER_OUTPUT_TYPE integer_set_ui(basic s,
unsigned long i)
303#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
304CWRAPPER_OUTPUT_TYPE integer_set_mpz(basic s,
const mpz_t i)
312CWRAPPER_OUTPUT_TYPE integer_set_str(basic s,
const char *c)
319CWRAPPER_OUTPUT_TYPE real_double_set_d(basic s,
double d)
322 s->m = SymEngine::real_double(d);
326double real_double_get_d(
const basic s)
328 SYMENGINE_ASSERT(is_a<RealDouble>(*(s->m)));
329 return (down_cast<const RealDouble &>(*(s->m))).as_double();
332#ifdef HAVE_SYMENGINE_MPFR
334CWRAPPER_OUTPUT_TYPE real_mpfr_set_d(basic s,
double d,
int prec)
337 mpfr_class mc = mpfr_class(prec);
338 mpfr_set_d(mc.get_mpfr_t(), d, MPFR_RNDN);
343CWRAPPER_OUTPUT_TYPE real_mpfr_set_str(basic s,
const char *c,
int prec)
350double real_mpfr_get_d(
const basic s)
352 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
354 ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
358CWRAPPER_OUTPUT_TYPE real_mpfr_set(basic s, mpfr_srcptr m)
365CWRAPPER_OUTPUT_TYPE real_mpfr_get(mpfr_ptr m,
const basic s)
368 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
369 mpfr_set(m, ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
374mpfr_prec_t real_mpfr_get_prec(
const basic s)
376 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
377 return ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_prec();
382CWRAPPER_OUTPUT_TYPE complex_base_real_part(basic s,
const basic com)
386 s->m = (down_cast<const ComplexBase &>(*(com->m))).real_part();
390CWRAPPER_OUTPUT_TYPE complex_base_imaginary_part(basic s,
const basic com)
394 s->m = (down_cast<const ComplexBase &>(*(com->m))).imaginary_part();
398signed long integer_get_si(
const basic s)
400 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
401 return mp_get_si((down_cast<const Integer &>(*(s->m))).as_integer_class());
404unsigned long integer_get_ui(
const basic s)
406 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
407 return mp_get_ui((down_cast<const Integer &>(*(s->m))).as_integer_class());
410#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
411CWRAPPER_OUTPUT_TYPE integer_get_mpz(mpz_t a,
const basic s)
414 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
416 a, get_mpz_t((down_cast<const Integer &>(*(s->m))).as_integer_class()));
421CWRAPPER_OUTPUT_TYPE rational_set_si(basic s,
long a,
long b)
428CWRAPPER_OUTPUT_TYPE rational_set_ui(basic s,
unsigned long a,
unsigned long b)
435CWRAPPER_OUTPUT_TYPE rational_set(basic s,
const basic a,
const basic b)
437 if (not is_a_Integer(a) or not is_a_Integer(b)) {
438 return SYMENGINE_RUNTIME_ERROR;
441 *(rcp_static_cast<const Integer>(a->m)),
442 *(rcp_static_cast<const Integer>(b->m)));
443 return SYMENGINE_NO_EXCEPTION;
446#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
447CWRAPPER_OUTPUT_TYPE rational_get_mpq(mpq_t a,
const basic s)
450 SYMENGINE_ASSERT(is_a<Rational>(*(s->m)));
451 mpq_set(a, get_mpq_t(
452 (down_cast<const Rational &>(*(s->m))).as_rational_class()));
456CWRAPPER_OUTPUT_TYPE rational_set_mpq(basic s,
const mpq_t i)
464CWRAPPER_OUTPUT_TYPE complex_set(basic s,
const basic re,
const basic im)
468 *(rcp_static_cast<const Number>(re->m)),
469 *(rcp_static_cast<const Number>(im->m)));
473CWRAPPER_OUTPUT_TYPE complex_set_rat(basic s,
const basic re,
const basic im)
477 *(rcp_static_cast<const Rational>(re->m)),
478 *(rcp_static_cast<const Rational>(im->m)));
482#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
483CWRAPPER_OUTPUT_TYPE complex_set_mpq(basic s,
const mpq_t re,
const mpq_t im)
491dcomplex complex_double_get(
const basic s)
493 SYMENGINE_ASSERT(is_a<ComplexDouble>(*(s->m)));
495 d.real = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
497 d.imag = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
502char *basic_dumps(
const basic s,
unsigned long *size)
505 *size = str.length();
506 auto cc =
new char[*size];
511CWRAPPER_OUTPUT_TYPE basic_loads(basic s,
const char *c,
unsigned long size)
519CWRAPPER_OUTPUT_TYPE basic_diff(basic s,
const basic expr, basic
const symbol)
521 if (not is_a_Symbol(symbol))
522 return SYMENGINE_RUNTIME_ERROR;
524 s->m = expr->m->diff(rcp_static_cast<const Symbol>(
symbol->m));
528CWRAPPER_OUTPUT_TYPE basic_assign(basic a,
const basic b)
535CWRAPPER_OUTPUT_TYPE basic_parse(basic b,
const char *str)
538 b->m = SymEngine::parse(str);
542CWRAPPER_OUTPUT_TYPE basic_parse2(basic b,
const char *str,
int convert_xor)
545 if (convert_xor > 0) {
546 b->m = SymEngine::parse(str);
548 b->m = SymEngine::parse(str,
false);
553CWRAPPER_OUTPUT_TYPE basic_add(basic s,
const basic a,
const basic b)
560CWRAPPER_OUTPUT_TYPE basic_sub(basic s,
const basic a,
const basic b)
567CWRAPPER_OUTPUT_TYPE basic_mul(basic s,
const basic a,
const basic b)
574CWRAPPER_OUTPUT_TYPE basic_pow(basic s,
const basic a,
const basic b)
577 s->m = SymEngine::pow(a->m, b->m);
581CWRAPPER_OUTPUT_TYPE basic_div(basic s,
const basic a,
const basic b)
588int basic_eq(
const basic a,
const basic b)
593int basic_neq(
const basic a,
const basic b)
598#define IMPLEMENT_ONE_ARG_FUNC(func) \
599 CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a) \
602 s->m = SymEngine::func(a->m); \
606IMPLEMENT_ONE_ARG_FUNC(expand)
607IMPLEMENT_ONE_ARG_FUNC(neg)
608IMPLEMENT_ONE_ARG_FUNC(abs)
609IMPLEMENT_ONE_ARG_FUNC(erf)
610IMPLEMENT_ONE_ARG_FUNC(erfc)
611IMPLEMENT_ONE_ARG_FUNC(sin)
612IMPLEMENT_ONE_ARG_FUNC(cos)
613IMPLEMENT_ONE_ARG_FUNC(tan)
614IMPLEMENT_ONE_ARG_FUNC(csc)
615IMPLEMENT_ONE_ARG_FUNC(sec)
616IMPLEMENT_ONE_ARG_FUNC(cot)
617IMPLEMENT_ONE_ARG_FUNC(asin)
618IMPLEMENT_ONE_ARG_FUNC(acos)
619IMPLEMENT_ONE_ARG_FUNC(asec)
620IMPLEMENT_ONE_ARG_FUNC(acsc)
621IMPLEMENT_ONE_ARG_FUNC(atan)
622IMPLEMENT_ONE_ARG_FUNC(acot)
623IMPLEMENT_ONE_ARG_FUNC(sinh)
624IMPLEMENT_ONE_ARG_FUNC(cosh)
625IMPLEMENT_ONE_ARG_FUNC(tanh)
626IMPLEMENT_ONE_ARG_FUNC(csch)
627IMPLEMENT_ONE_ARG_FUNC(sech)
628IMPLEMENT_ONE_ARG_FUNC(coth)
629IMPLEMENT_ONE_ARG_FUNC(asinh)
630IMPLEMENT_ONE_ARG_FUNC(acosh)
631IMPLEMENT_ONE_ARG_FUNC(asech)
632IMPLEMENT_ONE_ARG_FUNC(acsch)
633IMPLEMENT_ONE_ARG_FUNC(atanh)
634IMPLEMENT_ONE_ARG_FUNC(acoth)
635IMPLEMENT_ONE_ARG_FUNC(lambertw)
636IMPLEMENT_ONE_ARG_FUNC(zeta)
637IMPLEMENT_ONE_ARG_FUNC(dirichlet_eta)
638IMPLEMENT_ONE_ARG_FUNC(gamma)
639IMPLEMENT_ONE_ARG_FUNC(loggamma)
640IMPLEMENT_ONE_ARG_FUNC(sqrt)
641IMPLEMENT_ONE_ARG_FUNC(cbrt)
642IMPLEMENT_ONE_ARG_FUNC(exp)
643IMPLEMENT_ONE_ARG_FUNC(log)
644IMPLEMENT_ONE_ARG_FUNC(floor)
645IMPLEMENT_ONE_ARG_FUNC(ceiling)
647#define IMPLEMENT_TWO_ARG_FUNC(func) \
648 CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a, const basic b) \
651 s->m = SymEngine::func(a->m, b->m); \
655IMPLEMENT_TWO_ARG_FUNC(atan2)
656IMPLEMENT_TWO_ARG_FUNC(kronecker_delta)
657IMPLEMENT_TWO_ARG_FUNC(lowergamma)
658IMPLEMENT_TWO_ARG_FUNC(uppergamma)
659IMPLEMENT_TWO_ARG_FUNC(beta)
660IMPLEMENT_TWO_ARG_FUNC(polygamma)
662#define IMPLEMENT_STR_CONVERSION(name, func) \
663 char *basic_##name(const basic s) \
668 } catch (SymEngineException & e) { \
673 auto cc = new char[str.length() + 1]; \
674 std::strcpy(cc, str.c_str()); \
678IMPLEMENT_STR_CONVERSION(str, _str)
679IMPLEMENT_STR_CONVERSION(str_julia, julia_str)
680IMPLEMENT_STR_CONVERSION(str_mathml, mathml)
681IMPLEMENT_STR_CONVERSION(str_latex, latex)
682IMPLEMENT_STR_CONVERSION(str_ccode, ccode)
683IMPLEMENT_STR_CONVERSION(str_jscode, jscode)
685void basic_str_free(
char *s)
690void bool_set_true(basic s)
692 s->m = SymEngine::boolTrue;
695void bool_set_false(basic s)
697 s->m = SymEngine::boolFalse;
700CWRAPPER_OUTPUT_TYPE basic_set_interval(basic s,
const basic start,
701 const basic end,
int left_open,
704 SYMENGINE_ASSERT(is_a_Number(*(start->m)));
705 SYMENGINE_ASSERT(is_a_Number(*(
end->m)));
709 rcp_static_cast<const Number>(
end->m),
710 (
bool)left_open, (
bool)right_open);
714CWRAPPER_OUTPUT_TYPE basic_set_finiteset(basic s,
const CSetBasic *container)
721void basic_set_emptyset(basic s)
726void basic_set_universalset(basic s)
731void basic_set_complexes(basic s)
736void basic_set_reals(basic s)
741void basic_set_rationals(basic s)
746void basic_set_integers(basic s)
751CWRAPPER_OUTPUT_TYPE basic_set_union(basic s,
const basic a,
const basic b)
754 s->m = rcp_static_cast<const Set>(a->m)->set_union(
755 rcp_static_cast<const Set>(b->m));
759CWRAPPER_OUTPUT_TYPE basic_set_intersection(basic s,
const basic a,
763 s->m = rcp_static_cast<const Set>(a->m)->set_intersection(
764 rcp_static_cast<const Set>(b->m));
768CWRAPPER_OUTPUT_TYPE basic_set_complement(basic s,
const basic a,
const basic b)
771 s->m = rcp_static_cast<const Set>(a->m)->set_complement(
772 rcp_static_cast<const Set>(b->m));
776CWRAPPER_OUTPUT_TYPE basic_set_contains(basic s,
const basic a,
const basic b)
779 s->m = rcp_static_cast<const Set>(a->m)->contains(b->m);
783int basic_set_is_subset(
const basic a,
const basic b)
785 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
786 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
787 return rcp_static_cast<const Set>(a->m)->is_subset(
788 rcp_static_cast<const Set>(b->m));
791int basic_set_is_proper_subset(
const basic a,
const basic b)
793 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
794 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
795 return rcp_static_cast<const Set>(a->m)->is_proper_subset(
796 rcp_static_cast<const Set>(b->m));
799int basic_set_is_superset(
const basic a,
const basic b)
801 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
802 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
803 return rcp_static_cast<const Set>(a->m)->is_superset(
804 rcp_static_cast<const Set>(b->m));
807int basic_set_is_proper_superset(
const basic a,
const basic b)
809 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
810 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
811 return rcp_static_cast<const Set>(a->m)->is_proper_superset(
812 rcp_static_cast<const Set>(b->m));
815CWRAPPER_OUTPUT_TYPE basic_set_inf(basic s,
const basic a)
818 s->m = SymEngine::inf(*rcp_static_cast<const Set>(a->m));
822CWRAPPER_OUTPUT_TYPE basic_set_sup(basic s,
const basic a)
825 s->m = SymEngine::sup(*rcp_static_cast<const Set>(a->m));
829CWRAPPER_OUTPUT_TYPE basic_set_boundary(basic s,
const basic a)
832 s->m = SymEngine::boundary(*rcp_static_cast<const Set>(a->m));
836CWRAPPER_OUTPUT_TYPE basic_set_interior(basic s,
const basic a)
839 s->m = SymEngine::interior(*rcp_static_cast<const Set>(a->m));
843CWRAPPER_OUTPUT_TYPE basic_set_closure(basic s,
const basic a)
846 s->m = SymEngine::closure(*rcp_static_cast<const Set>(a->m));
850int symengine_have_component(
const char *c)
852#ifdef HAVE_SYMENGINE_MPFR
856#ifdef HAVE_SYMENGINE_MPC
860#ifdef HAVE_SYMENGINE_FLINT
864#ifdef HAVE_SYMENGINE_ARB
868#ifdef HAVE_SYMENGINE_ECM
872#ifdef HAVE_SYMENGINE_PRIMESIEVE
876#ifdef HAVE_SYMENGINE_PIRANHA
880#ifdef HAVE_SYMENGINE_BOOST
884#ifdef HAVE_SYMENGINE_PTHREAD
888#ifdef HAVE_SYMENGINE_LLVM
892#ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
899int is_a_Number(
const basic s)
901 return (
int)is_a_Number(*(s->m));
903int is_a_Integer(
const basic c)
905 return is_a<Integer>(*(c->m));
907int is_a_Rational(
const basic c)
909 return is_a<Rational>(*(c->m));
911int is_a_Symbol(
const basic c)
913 return is_a<Symbol>(*(c->m));
917 return is_a<Complex>(*(c->m));
919int is_a_RealDouble(
const basic c)
921 return is_a<RealDouble>(*(c->m));
923int is_a_ComplexDouble(
const basic c)
925 return is_a<ComplexDouble>(*(c->m));
927int is_a_RealMPFR(
const basic c)
929#ifdef HAVE_SYMENGINE_MPFR
930 return is_a<RealMPFR>(*(c->m));
935int is_a_ComplexMPC(
const basic c)
937#ifdef HAVE_SYMENGINE_MPC
938 return is_a<ComplexMPC>(*(c->m));
943int is_a_Set(
const basic c)
945 return SymEngine::is_a_Set(*(c->m));
959int vectorint_placement_new_check(
void *data,
size_t size)
964 if (not SymEngine::is_aligned(self))
969CVectorInt *vectorint_placement_new(
void *data)
971#if defined(WITH_SYMENGINE_ASSERT)
974 SYMENGINE_ASSERT(SymEngine::is_aligned(self));
980void vectorint_placement_free(
CVectorInt *self)
990void vectorint_push_back(
CVectorInt *self,
int value)
1016CWRAPPER_OUTPUT_TYPE vecbasic_push_back(
CVecBasic *self,
const basic value)
1025CWRAPPER_OUTPUT_TYPE vecbasic_get(
CVecBasic *self,
size_t n, basic result)
1029 SYMENGINE_ASSERT(n < self->m.size());
1030 result->m = self->m[n];
1035CWRAPPER_OUTPUT_TYPE vecbasic_set(
CVecBasic *self,
size_t n,
const basic s)
1038 SYMENGINE_ASSERT(n < self->m.size());
1043CWRAPPER_OUTPUT_TYPE vecbasic_erase(
CVecBasic *self,
size_t n)
1046 SYMENGINE_ASSERT(n < self->m.size());
1053 return self->m.
size();
1056CWRAPPER_OUTPUT_TYPE basic_max(basic s,
const CVecBasic *d)
1063CWRAPPER_OUTPUT_TYPE basic_min(basic s,
const CVecBasic *d)
1070CWRAPPER_OUTPUT_TYPE basic_add_vec(basic s,
const CVecBasic *d)
1077CWRAPPER_OUTPUT_TYPE basic_mul_vec(basic s,
const CVecBasic *d)
1104CDenseMatrix *dense_matrix_new_rows_cols(
unsigned rows,
unsigned cols)
1129void sparse_matrix_rows_cols(
CSparseMatrix *s,
unsigned long int rows,
1130 unsigned long int cols)
1133 numeric_cast<unsigned>(cols));
1146 auto cc =
new char[str.length() + 1];
1154 auto cc =
new char[str.length() + 1];
1159CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(
CDenseMatrix *mat,
unsigned r,
1163 mat->m.resize(r, c);
1167CWRAPPER_OUTPUT_TYPE dense_matrix_get_basic(basic s,
const CDenseMatrix *mat,
1168 unsigned long int r,
1169 unsigned long int c)
1172 s->m = mat->m.get(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c));
1176CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(
CDenseMatrix *mat,
1177 unsigned long int r,
1178 unsigned long int c, basic s)
1181 mat->m.set(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c), s->m);
1185CWRAPPER_OUTPUT_TYPE sparse_matrix_get_basic(basic s,
const CSparseMatrix *mat,
1186 unsigned long int r,
1187 unsigned long int c)
1190 s->m = mat->m.get(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c));
1194CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(
CSparseMatrix *mat,
1195 unsigned long int r,
1196 unsigned long int c, basic s)
1199 mat->m.set(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c), s->m);
1203CWRAPPER_OUTPUT_TYPE dense_matrix_det(basic s,
const CDenseMatrix *mat)
1206 s->m = mat->m.det();
1212 dense_matrix_rows_cols(s, mat->m.nrows(), mat->m.ncols());
1216CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(
CDenseMatrix *s,
1220 dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows());
1221 mat->m.transpose(s->m);
1226 unsigned long int r1,
unsigned long int c1,
1227 unsigned long int r2,
unsigned long int c2,
1228 unsigned long int r,
unsigned long int c)
1231 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r2 - r1 + 1),
1232 numeric_cast<unsigned>(c2 - c1 + 1));
1233 mat->m.submatrix(s->m, numeric_cast<unsigned>(r1),
1234 numeric_cast<unsigned>(c1), numeric_cast<unsigned>(r2),
1235 numeric_cast<unsigned>(c2), numeric_cast<unsigned>(r),
1236 numeric_cast<unsigned>(c));
1240CWRAPPER_OUTPUT_TYPE dense_matrix_row_join(
CDenseMatrix *A,
1244 A->m.row_join(B->m);
1248CWRAPPER_OUTPUT_TYPE dense_matrix_col_join(
CDenseMatrix *A,
1252 A->m.col_join(B->m);
1256CWRAPPER_OUTPUT_TYPE dense_matrix_row_del(
CDenseMatrix *A,
unsigned k)
1263CWRAPPER_OUTPUT_TYPE dense_matrix_col_del(
CDenseMatrix *A,
unsigned k)
1270unsigned long int dense_matrix_rows(
const CDenseMatrix *s)
1272 return s->m.nrows();
1275unsigned long int dense_matrix_cols(
const CDenseMatrix *s)
1277 return s->m.ncols();
1280CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(
CDenseMatrix *s,
1285 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1286 matA->m.add_matrix(matB->m, s->m);
1290CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(
CDenseMatrix *s,
1295 dense_matrix_rows_cols(s, matA->m.nrows(), matB->m.ncols());
1296 matA->m.mul_matrix(matB->m, s->m);
1300CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(
CDenseMatrix *s,
1305 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1306 matA->m.add_scalar(b->m, s->m);
1310CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(
CDenseMatrix *s,
1315 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1316 matA->m.mul_scalar(b->m, s->m);
1324 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1325 dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols());
1326 mat->m.LU(l->m, u->m);
1334 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1335 dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols());
1336 mat->m.LDL(l->m, d->m);
1340CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(
CDenseMatrix *lu,
1344 dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols());
1354 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1355 dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols());
1356 dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols());
1357 mat->m.FFLDU(l->m, d->m, u->m);
1361CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(
CDenseMatrix *x,
1366 dense_matrix_rows_cols(x, A->m.ncols(), 1);
1367 A->m.LU_solve(b->m, x->m);
1371CWRAPPER_OUTPUT_TYPE dense_matrix_ones(
CDenseMatrix *s,
unsigned long int r,
1372 unsigned long int c)
1375 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r),
1376 numeric_cast<unsigned>(c));
1381CWRAPPER_OUTPUT_TYPE dense_matrix_zeros(
CDenseMatrix *s,
unsigned long int r,
1382 unsigned long int c)
1385 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r),
1386 numeric_cast<unsigned>(c));
1394 unsigned int vec_size = numeric_cast<unsigned>(vecbasic_size(d));
1395 dense_matrix_rows_cols(
1396 s, numeric_cast<unsigned>(vec_size + (k >= 0 ? k : -k)),
1397 numeric_cast<unsigned>(vec_size + (k >= 0 ? k : -k)));
1398 diag(s->m, d->m, numeric_cast<int>(k));
1402CWRAPPER_OUTPUT_TYPE dense_matrix_eye(
CDenseMatrix *s,
unsigned long int N,
1403 unsigned long int M,
int k)
1406 dense_matrix_rows_cols(s, numeric_cast<unsigned>(N),
1407 numeric_cast<unsigned>(M));
1412CWRAPPER_OUTPUT_TYPE dense_matrix_diff(
CDenseMatrix *result,
1415 if (not is_a_Symbol(x))
1416 return SYMENGINE_RUNTIME_ERROR;
1418 diff(A->m, rcp_static_cast<const Symbol>(x->m), result->m);
1422CWRAPPER_OUTPUT_TYPE dense_matrix_jacobian(
CDenseMatrix *result,
1427 jacobian(A->m, x->m, result->m);
1433 return is_a<DenseMatrix>(c->m);
1438 return is_a<CSRMatrix>(c->m);
1443 return (lhs->m) == (rhs->m);
1448 return (lhs->m) == (rhs->m);
1463int setbasic_insert(
CSetBasic *self,
const basic value)
1465 return (self->m.
insert(value->m)).second ? 1 : 0;
1468void setbasic_get(
CSetBasic *self,
int n, basic result)
1470 result->m = *
std::next((self->m).begin(), n);
1473int setbasic_find(
CSetBasic *self, basic value)
1475 return self->m.
find(value->m) != (self->m).
end() ? 1 : 0;
1478int setbasic_erase(
CSetBasic *self,
const basic value)
1480 return (self->m.
erase(value->m)) ? 1 : 0;
1485 return self->m.
size();
1507 (self->m)[key->m] = mapped->m;
1510int mapbasicbasic_get(
CMapBasicBasic *self,
const basic key, basic mapped)
1512 auto it = self->m.
find(key->m);
1513 if (it != self->m.
end()) {
1514 mapped->m = it->second;
1522 return self->m.
size();
1527CWRAPPER_OUTPUT_TYPE basic_get_args(
const basic self,
CVecBasic *args)
1530 args->m = self->m->get_args();
1534CWRAPPER_OUTPUT_TYPE basic_free_symbols(
const basic self,
CSetBasic *symbols)
1537 symbols->m = SymEngine::free_symbols(*(self->m));
1541CWRAPPER_OUTPUT_TYPE basic_function_symbols(
CSetBasic *symbols,
1549size_t basic_hash(
const basic self)
1551 return static_cast<size_t>(self->m->hash());
1554CWRAPPER_OUTPUT_TYPE basic_subs(basic s,
const basic e,
1558 s->m = e->m->subs(mapbb->m);
1562CWRAPPER_OUTPUT_TYPE basic_subs2(basic s,
const basic e,
const basic a,
1566 s->m = e->m->subs({{a->m, b->m}});
1570CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s,
const char *c,
1574 s->m = function_symbol(c, arg->m);
1578char *function_symbol_get_name(
const basic b)
1580 SYMENGINE_ASSERT(is_a<FunctionSymbol>(*(b->m)));
1581 std::string str = down_cast<const FunctionSymbol &>(*(b->m)).get_name();
1582 auto cc =
new char[str.length() + 1];
1587CWRAPPER_OUTPUT_TYPE basic_coeff(basic c,
const basic b,
const basic x,
1591 c->m = SymEngine::coeff(*(b->m), *(x->m), *(n->m));
1601 vec_basic vb = sym->m;
1604 [](RCP<const Basic> b) { return is_a<const Symbol>(*b); }));
1605 vec_sym vs(vb.size());
1606 for (
unsigned i = 0; i < vb.size(); i++)
1607 vs[i] = rcp_static_cast<const Symbol>(vb[i]);
1608 sol->m = SymEngine::linsolve(sys->m, vs);
1612CWRAPPER_OUTPUT_TYPE basic_solve_poly(
CSetBasic *r,
const basic f,
1616 SYMENGINE_ASSERT(is_a<Symbol>(*(s->m)));
1618 = SymEngine::solve_poly(f->m, rcp_static_cast<const Symbol>(s->m));
1619 if (not is_a<FiniteSet>(*
set)) {
1620 return SYMENGINE_NOT_IMPLEMENTED;
1622 r->m = down_cast<const FiniteSet &>(*set).get_container();
1628char *ascii_art_str()
1631 auto cc =
new char[str.length() + 1];
1638CWRAPPER_OUTPUT_TYPE ntheory_gcd(basic s,
const basic a,
const basic b)
1641 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1642 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1644 down_cast<const Integer &>(*(b->m)));
1648CWRAPPER_OUTPUT_TYPE ntheory_lcm(basic s,
const basic a,
const basic b)
1651 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1652 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1654 down_cast<const Integer &>(*(b->m)));
1658CWRAPPER_OUTPUT_TYPE ntheory_gcd_ext(basic g, basic s, basic t,
const basic a,
1662 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1663 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1666 SymEngine::outArg(t_),
1667 down_cast<const Integer &>(*(a->m)),
1668 down_cast<const Integer &>(*(b->m)));
1675CWRAPPER_OUTPUT_TYPE ntheory_nextprime(basic s,
const basic a)
1678 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1683CWRAPPER_OUTPUT_TYPE ntheory_mod(basic s,
const basic n,
const basic d)
1686 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1687 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1689 down_cast<const Integer &>(*(d->m)));
1693CWRAPPER_OUTPUT_TYPE ntheory_quotient(basic s,
const basic n,
const basic d)
1696 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1697 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1699 down_cast<const Integer &>(*(d->m)));
1703CWRAPPER_OUTPUT_TYPE ntheory_quotient_mod(basic q, basic r,
const basic n,
1707 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1708 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1711 down_cast<const Integer &>(*(n->m)),
1712 down_cast<const Integer &>(*(d->m)));
1718CWRAPPER_OUTPUT_TYPE ntheory_mod_f(basic s,
const basic n,
const basic d)
1721 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1722 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1724 down_cast<const Integer &>(*(d->m)));
1728CWRAPPER_OUTPUT_TYPE ntheory_quotient_f(basic s,
const basic n,
const basic d)
1731 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1732 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1734 down_cast<const Integer &>(*(d->m)));
1738CWRAPPER_OUTPUT_TYPE ntheory_quotient_mod_f(basic q, basic r,
const basic n,
1742 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1743 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1746 down_cast<const Integer &>(*(n->m)),
1747 down_cast<const Integer &>(*(d->m)));
1753int ntheory_mod_inverse(basic b,
const basic a,
const basic m)
1756 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1757 SYMENGINE_ASSERT(is_a<Integer>(*(m->m)));
1760 down_cast<const Integer &>(*(a->m)),
1761 down_cast<const Integer &>(*(m->m)));
1766CWRAPPER_OUTPUT_TYPE ntheory_fibonacci(basic s,
unsigned long a)
1773CWRAPPER_OUTPUT_TYPE ntheory_fibonacci2(basic g, basic s,
unsigned long a)
1783CWRAPPER_OUTPUT_TYPE ntheory_lucas(basic s,
unsigned long a)
1790CWRAPPER_OUTPUT_TYPE ntheory_lucas2(basic g, basic s,
unsigned long a)
1800CWRAPPER_OUTPUT_TYPE ntheory_binomial(basic s,
const basic a,
unsigned long b)
1803 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1808CWRAPPER_OUTPUT_TYPE ntheory_factorial(basic s,
unsigned long n)
1816CWRAPPER_OUTPUT_TYPE basic_evalf(basic s,
const basic b,
unsigned long bits,
1821 s->m = SymEngine::evalf(*(b->m), bits, (SymEngine::EvalfDomain)real);
1825CWRAPPER_OUTPUT_TYPE basic_as_numer_denom(basic numer, basic denom,
1829 SymEngine::as_numer_denom(x->m, SymEngine::outArg(numer->m),
1830 SymEngine::outArg(denom->m));
1845 const CVecBasic *exprs,
int perform_cse)
1847 self->m.init(args->m, exprs->m, perform_cse);
1852 const double *
const inps)
1854 self->m.call(outs, inps);
1862#ifdef HAVE_SYMENGINE_LLVM
1864struct CLLVMDoubleVisitor {
1868CLLVMDoubleVisitor *llvm_double_visitor_new()
1870 return new CLLVMDoubleVisitor();
1873void llvm_double_visitor_init(CLLVMDoubleVisitor *self,
const CVecBasic *args,
1874 const CVecBasic *exprs,
int perform_cse,
1877 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1880void llvm_double_visitor_call(CLLVMDoubleVisitor *self,
double *
const outs,
1881 const double *
const inps)
1883 self->m.call(outs, inps);
1886void llvm_double_visitor_free(CLLVMDoubleVisitor *self)
1891struct CLLVMFloatVisitor {
1895CLLVMFloatVisitor *llvm_float_visitor_new()
1897 return new CLLVMFloatVisitor();
1900void llvm_float_visitor_init(CLLVMFloatVisitor *self,
const CVecBasic *args,
1901 const CVecBasic *exprs,
int perform_cse,
1904 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1907void llvm_float_visitor_call(CLLVMFloatVisitor *self,
float *
const outs,
1908 const float *
const inps)
1910 self->m.call(outs, inps);
1913void llvm_float_visitor_free(CLLVMFloatVisitor *self)
1917#ifdef SYMENGINE_HAVE_LLVM_LONG_DOUBLE
1919struct CLLVMLongDoubleVisitor {
1923CLLVMLongDoubleVisitor *llvm_long_double_visitor_new()
1925 return new CLLVMLongDoubleVisitor();
1928void llvm_long_double_visitor_init(CLLVMLongDoubleVisitor *self,
1930 const CVecBasic *exprs,
int perform_cse,
1933 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1936void llvm_long_double_visitor_call(CLLVMLongDoubleVisitor *self,
1937 long double *
const outs,
1938 const long double *
const inps)
1940 self->m.call(outs, inps);
1943void llvm_long_double_visitor_free(CLLVMLongDoubleVisitor *self)
1950CWRAPPER_OUTPUT_TYPE basic_cse(
CVecBasic *replacement_syms,
1955 vec_pair replacements;
1956 SymEngine::cse(replacements, reduced_exprs->m, exprs->m);
1957 for (
auto &p : replacements) {
1959 replacement_exprs->m.
push_back(p.second);
1964void symengine_print_stack_on_segfault()
1966 SymEngine::print_stack_on_segfault();
The lowest unit of symbolic representation.
std::string __str__() const
static RCP< const Basic > loads(const std::string &)
Creates an instance of a serialized string.
ComplexBase Class for deriving all complex classes.
Complex Double Class to hold std::complex<double> values.
static RCP< const Number > from_mpq(const rational_class re, const rational_class im)
static RCP< const Number > from_two_rats(const Rational &re, const Rational &im)
static RCP< const Number > from_two_nums(const Number &re, const Number &im)
static RCP< const Number > from_mpq(const rational_class &i)
static RCP< const Number > from_two_ints(const Integer &n, const Integer &d)
RealDouble Class to hold double values.
Main namespace for SymEngine package.
RCP< const Integer > nextprime(const Integer &a)
RCP< const Reals > reals()
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
RCP< const EmptySet > emptyset()
RCP< const Set > finiteset(const set_basic &container)
void quotient_mod_f(const Ptr< RCP< const Integer > > &q, const Ptr< RCP< const Integer > > &r, const Integer &n, const Integer &d)
RCP< const Integer > gcd(const Integer &a, const Integer &b)
Greatest Common Divisor.
RCP< const Basic > max(const vec_basic &arg)
Canonicalize Max:
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
RCP< const Integer > mod(const Integer &n, const Integer &d)
modulo round toward zero
void lucas2(const Ptr< RCP< const Integer > > &g, const Ptr< RCP< const Integer > > &s, unsigned long n)
Lucas number n and n-1.
RCP< const Integer > lucas(unsigned long n)
Lucas number.
RCP< const Integer > quotient_f(const Integer &n, const Integer &d)
RCP< const Integers > integers()
void hash_combine(hash_t &seed, const T &v)
RCP< const Integer > fibonacci(unsigned long n)
Fibonacci number.
RCP< const Basic > sub(const RCP< const Basic > &a, const RCP< const Basic > &b)
Substracts b from a.
void gcd_ext(const Ptr< RCP< const Integer > > &g, const Ptr< RCP< const Integer > > &s, const Ptr< RCP< const Integer > > &t, const Integer &a, const Integer &b)
Extended GCD.
RCP< const Integer > mod_f(const Integer &n, const Integer &d)
modulo round toward -inf
RCP< const Integer > quotient(const Integer &n, const Integer &d)
bool is_a(const Basic &b)
Templatised version to check is_a type.
RCP< const Integer > lcm(const Integer &a, const Integer &b)
Least Common Multiple.
RCP< const Integer > binomial(const Integer &n, unsigned long k)
Binomial Coefficient.
RCP< const Rationals > rationals()
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
RCP< const Symbol > symbol(const std::string &name)
inline version to return Symbol
void fibonacci2(const Ptr< RCP< const Integer > > &g, const Ptr< RCP< const Integer > > &s, unsigned long n)
Fibonacci n and n-1.
RCP< const Complexes > complexes()
RCP< const Integer > factorial(unsigned long n)
Factorial.
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
void quotient_mod(const Ptr< RCP< const Integer > > &q, const Ptr< RCP< const Integer > > &r, const Integer &n, const Integer &d)
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b
RCP< const Basic > min(const vec_basic &arg)
Canonicalize Min:
bool is_a_Complex(const Basic &b)
std::enable_if< std::is_integral< T >::value, RCP< constInteger > >::type integer(T i)
RCP< const Constant > constant(const std::string &name)
inline version to return Constant
int mod_inverse(const Ptr< RCP< const Integer > > &b, const Integer &a, const Integer &m)
inverse modulo
RCP< const Set > interval(const RCP< const Number > &start, const RCP< const Number > &end, const bool left_open=false, const bool right_open=false)