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>
14using SymEngine::LLVMDoubleVisitor;
15using SymEngine::LLVMFloatVisitor;
16#ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
17using SymEngine::LLVMLongDoubleVisitor;
30using SymEngine::down_cast;
31using SymEngine::function_symbol;
34using SymEngine::integer_class;
38using SymEngine::rational_class;
43#ifdef HAVE_SYMENGINE_MPFR
44using SymEngine::mpfr_class;
47#ifdef HAVE_SYMENGINE_MPC
52using SymEngine::rcp_static_cast;
59#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
60using SymEngine::get_mpq_t;
61using SymEngine::get_mpz_t;
63using SymEngine::ccode;
66using SymEngine::jscode;
67using SymEngine::julia_str;
68using SymEngine::latex;
69using SymEngine::mathml;
70using SymEngine::mp_get_si;
71using SymEngine::mp_get_ui;
72using SymEngine::numeric_cast;
74using SymEngine::parse;
75using SymEngine::SymEngineException;
76using SymEngine::zeros;
82inline bool is_aligned(T *p,
size_t n =
alignof(T))
84 return 0 ==
reinterpret_cast<uintptr_t
>(p) % n;
95#define CWRAPPER_BEGIN try {
98 return SYMENGINE_NO_EXCEPTION; \
100 catch (SymEngineException & e) \
102 return e.error_code(); \
106 return SYMENGINE_RUNTIME_ERROR; \
110 SymEngine::RCP<const SymEngine::Basic> m;
118 "Size of 'basic' is not correct");
121 "Alignment of 'basic' is not correct");
123void basic_new_stack(basic s)
128void basic_free_stack(basic s)
143const char *symengine_version()
145 return SYMENGINE_VERSION;
148void basic_const_set(basic s,
const char *c)
153void basic_const_zero(basic s)
155 s->m = SymEngine::zero;
158void basic_const_one(basic s)
160 s->m = SymEngine::one;
163void basic_const_minus_one(basic s)
165 s->m = SymEngine::minus_one;
168void basic_const_I(basic s)
173void basic_const_pi(basic s)
175 s->m = SymEngine::pi;
178void basic_const_E(basic s)
183void basic_const_EulerGamma(basic s)
185 s->m = SymEngine::EulerGamma;
188void basic_const_Catalan(basic s)
190 s->m = SymEngine::Catalan;
193void basic_const_GoldenRatio(basic s)
195 s->m = SymEngine::GoldenRatio;
198void basic_const_infinity(basic s)
200 s->m = SymEngine::Inf;
203void basic_const_neginfinity(basic s)
205 s->m = SymEngine::NegInf;
208void basic_const_complex_infinity(basic s)
210 s->m = SymEngine::ComplexInf;
213void basic_const_nan(basic s)
215 s->m = SymEngine::Nan;
218TypeID basic_get_class_id(
const char *c)
221#define SYMENGINE_INCLUDE_ALL
222#define SYMENGINE_ENUM(type, Class) {xstr(Class), type},
223#include "symengine/type_codes.inc"
225#undef SYMENGINE_INCLUDE_ALL
226 {
"", SYMENGINE_TypeID_Count}};
231char *basic_get_class_from_id(TypeID
id)
234#define SYMENGINE_INCLUDE_ALL
235#define SYMENGINE_ENUM(type, Class) {type, xstr(Class)},
236#include "symengine/type_codes.inc"
238#undef SYMENGINE_INCLUDE_ALL
239 {SYMENGINE_TypeID_Count,
""}};
242 auto cc =
new char[name.
length() + 1];
247TypeID basic_get_type(
const basic s)
249 return static_cast<TypeID
>(s->m->get_type_code());
252CWRAPPER_OUTPUT_TYPE symbol_set(basic s,
const char *c)
259int number_is_zero(
const basic s)
261 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
262 return (
int)((down_cast<const Number &>(*(s->m))).is_zero());
265int number_is_negative(
const basic s)
267 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
268 return (
int)((down_cast<const Number &>(*(s->m))).is_negative());
271int number_is_positive(
const basic s)
273 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
274 return (
int)((down_cast<const Number &>(*(s->m))).is_positive());
277int number_is_complex(
const basic s)
279 SYMENGINE_ASSERT(is_a_Number(*(s->m)));
280 return (
int)((down_cast<const Number &>(*(s->m))).is_complex());
283CWRAPPER_OUTPUT_TYPE integer_set_si(basic s,
long i)
290CWRAPPER_OUTPUT_TYPE integer_set_ui(basic s,
unsigned long i)
297#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
298CWRAPPER_OUTPUT_TYPE integer_set_mpz(basic s,
const mpz_t i)
306CWRAPPER_OUTPUT_TYPE integer_set_str(basic s,
const char *c)
313CWRAPPER_OUTPUT_TYPE real_double_set_d(basic s,
double d)
316 s->m = SymEngine::real_double(d);
320double real_double_get_d(
const basic s)
322 SYMENGINE_ASSERT(is_a<RealDouble>(*(s->m)));
323 return (down_cast<const RealDouble &>(*(s->m))).as_double();
326#ifdef HAVE_SYMENGINE_MPFR
328CWRAPPER_OUTPUT_TYPE real_mpfr_set_d(basic s,
double d,
int prec)
331 mpfr_class mc = mpfr_class(prec);
332 mpfr_set_d(mc.get_mpfr_t(), d, MPFR_RNDN);
333 s->m = SymEngine::real_mpfr(
std::move(mc));
337CWRAPPER_OUTPUT_TYPE real_mpfr_set_str(basic s,
const char *c,
int prec)
340 s->m = SymEngine::real_mpfr(mpfr_class(c, prec, 10));
344double real_mpfr_get_d(
const basic s)
346 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
348 ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
352CWRAPPER_OUTPUT_TYPE real_mpfr_set(basic s, mpfr_srcptr m)
355 s->m = SymEngine::real_mpfr(mpfr_class(m));
359CWRAPPER_OUTPUT_TYPE real_mpfr_get(mpfr_ptr m,
const basic s)
362 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
363 mpfr_set(m, ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
368mpfr_prec_t real_mpfr_get_prec(
const basic s)
370 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
371 return ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_prec();
376CWRAPPER_OUTPUT_TYPE complex_base_real_part(basic s,
const basic com)
380 s->m = (down_cast<const ComplexBase &>(*(com->m))).real_part();
384CWRAPPER_OUTPUT_TYPE complex_base_imaginary_part(basic s,
const basic com)
388 s->m = (down_cast<const ComplexBase &>(*(com->m))).imaginary_part();
392signed long integer_get_si(
const basic s)
394 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
395 return mp_get_si((down_cast<const Integer &>(*(s->m))).as_integer_class());
398unsigned long integer_get_ui(
const basic s)
400 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
401 return mp_get_ui((down_cast<const Integer &>(*(s->m))).as_integer_class());
404#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
405CWRAPPER_OUTPUT_TYPE integer_get_mpz(mpz_t a,
const basic s)
408 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
410 a, get_mpz_t((down_cast<const Integer &>(*(s->m))).as_integer_class()));
415CWRAPPER_OUTPUT_TYPE rational_set_si(basic s,
long a,
long b)
422CWRAPPER_OUTPUT_TYPE rational_set_ui(basic s,
unsigned long a,
unsigned long b)
429CWRAPPER_OUTPUT_TYPE rational_set(basic s,
const basic a,
const basic b)
431 if (not is_a_Integer(a) or not is_a_Integer(b)) {
432 return SYMENGINE_RUNTIME_ERROR;
435 *(rcp_static_cast<const Integer>(a->m)),
436 *(rcp_static_cast<const Integer>(b->m)));
437 return SYMENGINE_NO_EXCEPTION;
440#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
441CWRAPPER_OUTPUT_TYPE rational_get_mpq(mpq_t a,
const basic s)
444 SYMENGINE_ASSERT(is_a<Rational>(*(s->m)));
445 mpq_set(a, get_mpq_t(
446 (down_cast<const Rational &>(*(s->m))).as_rational_class()));
450CWRAPPER_OUTPUT_TYPE rational_set_mpq(basic s,
const mpq_t i)
458CWRAPPER_OUTPUT_TYPE complex_set(basic s,
const basic re,
const basic im)
462 *(rcp_static_cast<const Number>(re->m)),
463 *(rcp_static_cast<const Number>(im->m)));
467CWRAPPER_OUTPUT_TYPE complex_set_rat(basic s,
const basic re,
const basic im)
471 *(rcp_static_cast<const Rational>(re->m)),
472 *(rcp_static_cast<const Rational>(im->m)));
476#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
477CWRAPPER_OUTPUT_TYPE complex_set_mpq(basic s,
const mpq_t re,
const mpq_t im)
485dcomplex complex_double_get(
const basic s)
487 SYMENGINE_ASSERT(is_a<ComplexDouble>(*(s->m)));
489 d.real = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
491 d.imag = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
496char *basic_dumps(
const basic s,
unsigned long *size)
499 *size = str.length();
500 auto cc =
new char[*size];
505CWRAPPER_OUTPUT_TYPE basic_loads(basic s,
const char *c,
unsigned long size)
513CWRAPPER_OUTPUT_TYPE basic_diff(basic s,
const basic expr, basic
const symbol)
515 if (not is_a_Symbol(symbol))
516 return SYMENGINE_RUNTIME_ERROR;
518 s->m = expr->m->diff(rcp_static_cast<const Symbol>(
symbol->m));
522CWRAPPER_OUTPUT_TYPE basic_assign(basic a,
const basic b)
529CWRAPPER_OUTPUT_TYPE basic_parse(basic b,
const char *str)
532 b->m = SymEngine::parse(str);
536CWRAPPER_OUTPUT_TYPE basic_parse2(basic b,
const char *str,
int convert_xor)
539 if (convert_xor > 0) {
540 b->m = SymEngine::parse(str);
542 b->m = SymEngine::parse(str,
false);
547CWRAPPER_OUTPUT_TYPE basic_add(basic s,
const basic a,
const basic b)
554CWRAPPER_OUTPUT_TYPE basic_sub(basic s,
const basic a,
const basic b)
561CWRAPPER_OUTPUT_TYPE basic_mul(basic s,
const basic a,
const basic b)
568CWRAPPER_OUTPUT_TYPE basic_pow(basic s,
const basic a,
const basic b)
571 s->m = SymEngine::pow(a->m, b->m);
575CWRAPPER_OUTPUT_TYPE basic_div(basic s,
const basic a,
const basic b)
582int basic_eq(
const basic a,
const basic b)
587int basic_neq(
const basic a,
const basic b)
592#define IMPLEMENT_ONE_ARG_FUNC(func) \
593 CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a) \
596 s->m = SymEngine::func(a->m); \
600IMPLEMENT_ONE_ARG_FUNC(expand)
601IMPLEMENT_ONE_ARG_FUNC(neg)
602IMPLEMENT_ONE_ARG_FUNC(abs)
603IMPLEMENT_ONE_ARG_FUNC(erf)
604IMPLEMENT_ONE_ARG_FUNC(erfc)
605IMPLEMENT_ONE_ARG_FUNC(sin)
606IMPLEMENT_ONE_ARG_FUNC(cos)
607IMPLEMENT_ONE_ARG_FUNC(tan)
608IMPLEMENT_ONE_ARG_FUNC(csc)
609IMPLEMENT_ONE_ARG_FUNC(sec)
610IMPLEMENT_ONE_ARG_FUNC(cot)
611IMPLEMENT_ONE_ARG_FUNC(asin)
612IMPLEMENT_ONE_ARG_FUNC(acos)
613IMPLEMENT_ONE_ARG_FUNC(asec)
614IMPLEMENT_ONE_ARG_FUNC(acsc)
615IMPLEMENT_ONE_ARG_FUNC(atan)
616IMPLEMENT_ONE_ARG_FUNC(acot)
617IMPLEMENT_ONE_ARG_FUNC(sinh)
618IMPLEMENT_ONE_ARG_FUNC(cosh)
619IMPLEMENT_ONE_ARG_FUNC(tanh)
620IMPLEMENT_ONE_ARG_FUNC(csch)
621IMPLEMENT_ONE_ARG_FUNC(sech)
622IMPLEMENT_ONE_ARG_FUNC(coth)
623IMPLEMENT_ONE_ARG_FUNC(asinh)
624IMPLEMENT_ONE_ARG_FUNC(acosh)
625IMPLEMENT_ONE_ARG_FUNC(asech)
626IMPLEMENT_ONE_ARG_FUNC(acsch)
627IMPLEMENT_ONE_ARG_FUNC(atanh)
628IMPLEMENT_ONE_ARG_FUNC(acoth)
629IMPLEMENT_ONE_ARG_FUNC(lambertw)
630IMPLEMENT_ONE_ARG_FUNC(zeta)
631IMPLEMENT_ONE_ARG_FUNC(dirichlet_eta)
632IMPLEMENT_ONE_ARG_FUNC(gamma)
633IMPLEMENT_ONE_ARG_FUNC(loggamma)
634IMPLEMENT_ONE_ARG_FUNC(sqrt)
635IMPLEMENT_ONE_ARG_FUNC(cbrt)
636IMPLEMENT_ONE_ARG_FUNC(exp)
637IMPLEMENT_ONE_ARG_FUNC(log)
638IMPLEMENT_ONE_ARG_FUNC(floor)
639IMPLEMENT_ONE_ARG_FUNC(ceiling)
641#define IMPLEMENT_TWO_ARG_FUNC(func) \
642 CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a, const basic b) \
645 s->m = SymEngine::func(a->m, b->m); \
649IMPLEMENT_TWO_ARG_FUNC(atan2)
650IMPLEMENT_TWO_ARG_FUNC(kronecker_delta)
651IMPLEMENT_TWO_ARG_FUNC(lowergamma)
652IMPLEMENT_TWO_ARG_FUNC(uppergamma)
653IMPLEMENT_TWO_ARG_FUNC(beta)
654IMPLEMENT_TWO_ARG_FUNC(polygamma)
656#define IMPLEMENT_STR_CONVERSION(name, func) \
657 char *basic_##name(const basic s) \
662 } catch (SymEngineException & e) { \
667 auto cc = new char[str.length() + 1]; \
668 std::strcpy(cc, str.c_str()); \
672IMPLEMENT_STR_CONVERSION(str, _str)
673IMPLEMENT_STR_CONVERSION(str_julia, julia_str)
674IMPLEMENT_STR_CONVERSION(str_mathml, mathml)
675IMPLEMENT_STR_CONVERSION(str_latex, latex)
676IMPLEMENT_STR_CONVERSION(str_ccode, ccode)
677IMPLEMENT_STR_CONVERSION(str_jscode, jscode)
679void basic_str_free(
char *s)
684void bool_set_true(basic s)
686 s->m = SymEngine::boolTrue;
689void bool_set_false(basic s)
691 s->m = SymEngine::boolFalse;
694CWRAPPER_OUTPUT_TYPE basic_set_interval(basic s,
const basic start,
695 const basic end,
int left_open,
698 SYMENGINE_ASSERT(is_a_Number(*(start->m)));
699 SYMENGINE_ASSERT(is_a_Number(*(
end->m)));
703 rcp_static_cast<const Number>(
end->m),
704 (
bool)left_open, (
bool)right_open);
708CWRAPPER_OUTPUT_TYPE basic_set_finiteset(basic s,
const CSetBasic *container)
715void basic_set_emptyset(basic s)
720void basic_set_universalset(basic s)
725void basic_set_complexes(basic s)
730void basic_set_reals(basic s)
735void basic_set_rationals(basic s)
740void basic_set_integers(basic s)
745CWRAPPER_OUTPUT_TYPE basic_set_union(basic s,
const basic a,
const basic b)
748 s->m = rcp_static_cast<const Set>(a->m)->set_union(
749 rcp_static_cast<const Set>(b->m));
753CWRAPPER_OUTPUT_TYPE basic_set_intersection(basic s,
const basic a,
757 s->m = rcp_static_cast<const Set>(a->m)->set_intersection(
758 rcp_static_cast<const Set>(b->m));
762CWRAPPER_OUTPUT_TYPE basic_set_complement(basic s,
const basic a,
const basic b)
765 s->m = rcp_static_cast<const Set>(a->m)->set_complement(
766 rcp_static_cast<const Set>(b->m));
770CWRAPPER_OUTPUT_TYPE basic_set_contains(basic s,
const basic a,
const basic b)
773 s->m = rcp_static_cast<const Set>(a->m)->contains(b->m);
777int basic_set_is_subset(
const basic a,
const basic b)
779 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
780 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
781 return rcp_static_cast<const Set>(a->m)->is_subset(
782 rcp_static_cast<const Set>(b->m));
785int basic_set_is_proper_subset(
const basic a,
const basic b)
787 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
788 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
789 return rcp_static_cast<const Set>(a->m)->is_proper_subset(
790 rcp_static_cast<const Set>(b->m));
793int basic_set_is_superset(
const basic a,
const basic b)
795 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
796 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
797 return rcp_static_cast<const Set>(a->m)->is_superset(
798 rcp_static_cast<const Set>(b->m));
801int basic_set_is_proper_superset(
const basic a,
const basic b)
803 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
804 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
805 return rcp_static_cast<const Set>(a->m)->is_proper_superset(
806 rcp_static_cast<const Set>(b->m));
809CWRAPPER_OUTPUT_TYPE basic_set_inf(basic s,
const basic a)
812 s->m = SymEngine::inf(*rcp_static_cast<const Set>(a->m));
816CWRAPPER_OUTPUT_TYPE basic_set_sup(basic s,
const basic a)
819 s->m = SymEngine::sup(*rcp_static_cast<const Set>(a->m));
823CWRAPPER_OUTPUT_TYPE basic_set_boundary(basic s,
const basic a)
826 s->m = SymEngine::boundary(*rcp_static_cast<const Set>(a->m));
830CWRAPPER_OUTPUT_TYPE basic_set_interior(basic s,
const basic a)
833 s->m = SymEngine::interior(*rcp_static_cast<const Set>(a->m));
837CWRAPPER_OUTPUT_TYPE basic_set_closure(basic s,
const basic a)
840 s->m = SymEngine::closure(*rcp_static_cast<const Set>(a->m));
844int symengine_have_component(
const char *c)
846#ifdef HAVE_SYMENGINE_MPFR
850#ifdef HAVE_SYMENGINE_MPC
854#ifdef HAVE_SYMENGINE_FLINT
858#ifdef HAVE_SYMENGINE_ARB
862#ifdef HAVE_SYMENGINE_ECM
866#ifdef HAVE_SYMENGINE_PRIMESIEVE
870#ifdef HAVE_SYMENGINE_PIRANHA
874#ifdef HAVE_SYMENGINE_BOOST
878#ifdef HAVE_SYMENGINE_PTHREAD
882#ifdef HAVE_SYMENGINE_LLVM
886#ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
893int is_a_Number(
const basic s)
895 return (
int)is_a_Number(*(s->m));
897int is_a_Integer(
const basic c)
899 return is_a<Integer>(*(c->m));
901int is_a_Rational(
const basic c)
903 return is_a<Rational>(*(c->m));
905int is_a_Symbol(
const basic c)
907 return is_a<Symbol>(*(c->m));
911 return is_a<Complex>(*(c->m));
913int is_a_RealDouble(
const basic c)
915 return is_a<RealDouble>(*(c->m));
917int is_a_ComplexDouble(
const basic c)
919 return is_a<ComplexDouble>(*(c->m));
921int is_a_RealMPFR(
const basic c)
923#ifdef HAVE_SYMENGINE_MPFR
924 return is_a<RealMPFR>(*(c->m));
929int is_a_ComplexMPC(
const basic c)
931#ifdef HAVE_SYMENGINE_MPC
932 return is_a<ComplexMPC>(*(c->m));
937int is_a_Set(
const basic c)
939 return SymEngine::is_a_Set(*(c->m));
953int vectorint_placement_new_check(
void *data,
size_t size)
958 if (not SymEngine::is_aligned(self))
963CVectorInt *vectorint_placement_new(
void *data)
965#if defined(WITH_SYMENGINE_ASSERT)
968 SYMENGINE_ASSERT(SymEngine::is_aligned(self));
974void vectorint_placement_free(
CVectorInt *self)
984void vectorint_push_back(
CVectorInt *self,
int value)
1010CWRAPPER_OUTPUT_TYPE vecbasic_push_back(
CVecBasic *self,
const basic value)
1019CWRAPPER_OUTPUT_TYPE vecbasic_get(
CVecBasic *self,
size_t n, basic result)
1023 SYMENGINE_ASSERT(n < self->m.size());
1024 result->m = self->m[n];
1029CWRAPPER_OUTPUT_TYPE vecbasic_set(
CVecBasic *self,
size_t n,
const basic s)
1032 SYMENGINE_ASSERT(n < self->m.size());
1037CWRAPPER_OUTPUT_TYPE vecbasic_erase(
CVecBasic *self,
size_t n)
1040 SYMENGINE_ASSERT(n < self->m.size());
1047 return self->m.
size();
1050CWRAPPER_OUTPUT_TYPE basic_max(basic s,
const CVecBasic *d)
1057CWRAPPER_OUTPUT_TYPE basic_min(basic s,
const CVecBasic *d)
1064CWRAPPER_OUTPUT_TYPE basic_add_vec(basic s,
const CVecBasic *d)
1071CWRAPPER_OUTPUT_TYPE basic_mul_vec(basic s,
const CVecBasic *d)
1098CDenseMatrix *dense_matrix_new_rows_cols(
unsigned rows,
unsigned cols)
1123void sparse_matrix_rows_cols(
CSparseMatrix *s,
unsigned long int rows,
1124 unsigned long int cols)
1127 numeric_cast<unsigned>(cols));
1140 auto cc =
new char[str.length() + 1];
1148 auto cc =
new char[str.length() + 1];
1153CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(
CDenseMatrix *mat,
unsigned r,
1157 mat->m.resize(r, c);
1161CWRAPPER_OUTPUT_TYPE dense_matrix_get_basic(basic s,
const CDenseMatrix *mat,
1162 unsigned long int r,
1163 unsigned long int c)
1166 s->m = mat->m.get(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c));
1170CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(
CDenseMatrix *mat,
1171 unsigned long int r,
1172 unsigned long int c, basic s)
1175 mat->m.set(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c), s->m);
1179CWRAPPER_OUTPUT_TYPE sparse_matrix_get_basic(basic s,
const CSparseMatrix *mat,
1180 unsigned long int r,
1181 unsigned long int c)
1184 s->m = mat->m.get(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c));
1188CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(
CSparseMatrix *mat,
1189 unsigned long int r,
1190 unsigned long int c, basic s)
1193 mat->m.set(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c), s->m);
1197CWRAPPER_OUTPUT_TYPE dense_matrix_det(basic s,
const CDenseMatrix *mat)
1200 s->m = mat->m.det();
1206 dense_matrix_rows_cols(s, mat->m.nrows(), mat->m.ncols());
1210CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(
CDenseMatrix *s,
1214 dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows());
1215 mat->m.transpose(s->m);
1220 unsigned long int r1,
unsigned long int c1,
1221 unsigned long int r2,
unsigned long int c2,
1222 unsigned long int r,
unsigned long int c)
1225 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r2 - r1 + 1),
1226 numeric_cast<unsigned>(c2 - c1 + 1));
1227 mat->m.submatrix(s->m, numeric_cast<unsigned>(r1),
1228 numeric_cast<unsigned>(c1), numeric_cast<unsigned>(r2),
1229 numeric_cast<unsigned>(c2), numeric_cast<unsigned>(r),
1230 numeric_cast<unsigned>(c));
1234CWRAPPER_OUTPUT_TYPE dense_matrix_row_join(
CDenseMatrix *A,
1238 A->m.row_join(B->m);
1242CWRAPPER_OUTPUT_TYPE dense_matrix_col_join(
CDenseMatrix *A,
1246 A->m.col_join(B->m);
1250CWRAPPER_OUTPUT_TYPE dense_matrix_row_del(
CDenseMatrix *A,
unsigned k)
1257CWRAPPER_OUTPUT_TYPE dense_matrix_col_del(
CDenseMatrix *A,
unsigned k)
1264unsigned long int dense_matrix_rows(
const CDenseMatrix *s)
1266 return s->m.nrows();
1269unsigned long int dense_matrix_cols(
const CDenseMatrix *s)
1271 return s->m.ncols();
1274CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(
CDenseMatrix *s,
1279 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1280 matA->m.add_matrix(matB->m, s->m);
1284CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(
CDenseMatrix *s,
1289 dense_matrix_rows_cols(s, matA->m.nrows(), matB->m.ncols());
1290 matA->m.mul_matrix(matB->m, s->m);
1294CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(
CDenseMatrix *s,
1299 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1300 matA->m.add_scalar(b->m, s->m);
1304CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(
CDenseMatrix *s,
1309 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1310 matA->m.mul_scalar(b->m, s->m);
1318 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1319 dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols());
1320 mat->m.LU(l->m, u->m);
1328 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1329 dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols());
1330 mat->m.LDL(l->m, d->m);
1334CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(
CDenseMatrix *lu,
1338 dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols());
1348 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1349 dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols());
1350 dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols());
1351 mat->m.FFLDU(l->m, d->m, u->m);
1355CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(
CDenseMatrix *x,
1360 dense_matrix_rows_cols(x, A->m.ncols(), 1);
1361 A->m.LU_solve(b->m, x->m);
1365CWRAPPER_OUTPUT_TYPE dense_matrix_ones(
CDenseMatrix *s,
unsigned long int r,
1366 unsigned long int c)
1369 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r),
1370 numeric_cast<unsigned>(c));
1375CWRAPPER_OUTPUT_TYPE dense_matrix_zeros(
CDenseMatrix *s,
unsigned long int r,
1376 unsigned long int c)
1379 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r),
1380 numeric_cast<unsigned>(c));
1388 unsigned int vec_size = numeric_cast<unsigned>(vecbasic_size(d));
1389 dense_matrix_rows_cols(
1390 s, numeric_cast<unsigned>(vec_size + (k >= 0 ? k : -k)),
1391 numeric_cast<unsigned>(vec_size + (k >= 0 ? k : -k)));
1392 diag(s->m, d->m, numeric_cast<int>(k));
1396CWRAPPER_OUTPUT_TYPE dense_matrix_eye(
CDenseMatrix *s,
unsigned long int N,
1397 unsigned long int M,
int k)
1400 dense_matrix_rows_cols(s, numeric_cast<unsigned>(N),
1401 numeric_cast<unsigned>(M));
1406CWRAPPER_OUTPUT_TYPE dense_matrix_diff(
CDenseMatrix *result,
1409 if (not is_a_Symbol(x))
1410 return SYMENGINE_RUNTIME_ERROR;
1412 diff(A->m, rcp_static_cast<const Symbol>(x->m), result->m);
1416CWRAPPER_OUTPUT_TYPE dense_matrix_jacobian(
CDenseMatrix *result,
1421 jacobian(A->m, x->m, result->m);
1427 return is_a<DenseMatrix>(c->m);
1432 return is_a<CSRMatrix>(c->m);
1437 return (lhs->m) == (rhs->m);
1442 return (lhs->m) == (rhs->m);
1457int setbasic_insert(
CSetBasic *self,
const basic value)
1459 return (self->m.
insert(value->m)).second ? 1 : 0;
1462void setbasic_get(
CSetBasic *self,
int n, basic result)
1464 result->m = *
std::next((self->m).begin(), n);
1467int setbasic_find(
CSetBasic *self, basic value)
1469 return self->m.
find(value->m) != (self->m).
end() ? 1 : 0;
1472int setbasic_erase(
CSetBasic *self,
const basic value)
1474 return (self->m.
erase(value->m)) ? 1 : 0;
1479 return self->m.
size();
1501 (self->m)[key->m] = mapped->m;
1504int mapbasicbasic_get(
CMapBasicBasic *self,
const basic key, basic mapped)
1506 auto it = self->m.
find(key->m);
1507 if (it != self->m.
end()) {
1508 mapped->m = it->second;
1516 return self->m.
size();
1521CWRAPPER_OUTPUT_TYPE basic_get_args(
const basic self,
CVecBasic *args)
1524 args->m = self->m->get_args();
1528CWRAPPER_OUTPUT_TYPE basic_free_symbols(
const basic self,
CSetBasic *symbols)
1531 symbols->m = SymEngine::free_symbols(*(self->m));
1535CWRAPPER_OUTPUT_TYPE basic_function_symbols(
CSetBasic *symbols,
1539 symbols->m = SymEngine::atoms<SymEngine::FunctionSymbol>(*(self->m));
1543size_t basic_hash(
const basic self)
1545 return static_cast<size_t>(self->m->hash());
1548CWRAPPER_OUTPUT_TYPE basic_subs(basic s,
const basic e,
1552 s->m = e->m->subs(mapbb->m);
1556CWRAPPER_OUTPUT_TYPE basic_subs2(basic s,
const basic e,
const basic a,
1560 s->m = e->m->subs({{a->m, b->m}});
1564CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s,
const char *c,
1568 s->m = function_symbol(c, arg->m);
1572char *function_symbol_get_name(
const basic b)
1574 SYMENGINE_ASSERT(is_a<FunctionSymbol>(*(b->m)));
1575 std::string str = down_cast<const FunctionSymbol &>(*(b->m)).get_name();
1576 auto cc =
new char[str.length() + 1];
1581CWRAPPER_OUTPUT_TYPE basic_coeff(basic c,
const basic b,
const basic x,
1585 c->m = SymEngine::coeff(*(b->m), *(x->m), *(n->m));
1595 vec_basic vb = sym->m;
1598 [](RCP<const Basic> b) { return is_a<const Symbol>(*b); }));
1599 vec_sym vs(vb.size());
1600 for (
unsigned i = 0; i < vb.size(); i++)
1601 vs[i] = rcp_static_cast<const Symbol>(vb[i]);
1602 sol->m = SymEngine::linsolve(sys->m, vs);
1606CWRAPPER_OUTPUT_TYPE basic_solve_poly(
CSetBasic *r,
const basic f,
1610 SYMENGINE_ASSERT(is_a<Symbol>(*(s->m)));
1612 = SymEngine::solve_poly(f->m, rcp_static_cast<const Symbol>(s->m));
1613 if (not is_a<FiniteSet>(*
set)) {
1614 return SYMENGINE_NOT_IMPLEMENTED;
1616 r->m = down_cast<const FiniteSet &>(*set).get_container();
1622char *ascii_art_str()
1625 auto cc =
new char[str.length() + 1];
1632CWRAPPER_OUTPUT_TYPE ntheory_gcd(basic s,
const basic a,
const basic b)
1635 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1636 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1638 down_cast<const Integer &>(*(b->m)));
1642CWRAPPER_OUTPUT_TYPE ntheory_lcm(basic s,
const basic a,
const basic b)
1645 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1646 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1648 down_cast<const Integer &>(*(b->m)));
1652CWRAPPER_OUTPUT_TYPE ntheory_gcd_ext(basic g, basic s, basic t,
const basic a,
1656 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1657 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1658 SymEngine::RCP<const Integer> g_, s_, t_;
1660 SymEngine::outArg(t_),
1661 down_cast<const Integer &>(*(a->m)),
1662 down_cast<const Integer &>(*(b->m)));
1669CWRAPPER_OUTPUT_TYPE ntheory_nextprime(basic s,
const basic a)
1672 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1677CWRAPPER_OUTPUT_TYPE ntheory_mod(basic s,
const basic n,
const basic d)
1680 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1681 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1683 down_cast<const Integer &>(*(d->m)));
1687CWRAPPER_OUTPUT_TYPE ntheory_quotient(basic s,
const basic n,
const basic d)
1690 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1691 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1693 down_cast<const Integer &>(*(d->m)));
1697CWRAPPER_OUTPUT_TYPE ntheory_quotient_mod(basic q, basic r,
const basic n,
1701 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1702 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1703 SymEngine::RCP<const Integer> q_, r_;
1705 down_cast<const Integer &>(*(n->m)),
1706 down_cast<const Integer &>(*(d->m)));
1712CWRAPPER_OUTPUT_TYPE ntheory_mod_f(basic s,
const basic n,
const basic d)
1715 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1716 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1718 down_cast<const Integer &>(*(d->m)));
1722CWRAPPER_OUTPUT_TYPE ntheory_quotient_f(basic s,
const basic n,
const basic d)
1725 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1726 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1728 down_cast<const Integer &>(*(d->m)));
1732CWRAPPER_OUTPUT_TYPE ntheory_quotient_mod_f(basic q, basic r,
const basic n,
1736 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1737 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1738 SymEngine::RCP<const Integer> q_, r_;
1740 down_cast<const Integer &>(*(n->m)),
1741 down_cast<const Integer &>(*(d->m)));
1747int ntheory_mod_inverse(basic b,
const basic a,
const basic m)
1750 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1751 SYMENGINE_ASSERT(is_a<Integer>(*(m->m)));
1752 SymEngine::RCP<const Integer> b_;
1754 down_cast<const Integer &>(*(a->m)),
1755 down_cast<const Integer &>(*(m->m)));
1760CWRAPPER_OUTPUT_TYPE ntheory_fibonacci(basic s,
unsigned long a)
1767CWRAPPER_OUTPUT_TYPE ntheory_fibonacci2(basic g, basic s,
unsigned long a)
1770 SymEngine::RCP<const Integer> g_, s_;
1777CWRAPPER_OUTPUT_TYPE ntheory_lucas(basic s,
unsigned long a)
1784CWRAPPER_OUTPUT_TYPE ntheory_lucas2(basic g, basic s,
unsigned long a)
1787 SymEngine::RCP<const Integer> g_, s_;
1794CWRAPPER_OUTPUT_TYPE ntheory_binomial(basic s,
const basic a,
unsigned long b)
1797 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1802CWRAPPER_OUTPUT_TYPE ntheory_factorial(basic s,
unsigned long n)
1810CWRAPPER_OUTPUT_TYPE basic_evalf(basic s,
const basic b,
unsigned long bits,
1815 s->m = SymEngine::evalf(*(b->m), bits, (SymEngine::EvalfDomain)real);
1819CWRAPPER_OUTPUT_TYPE basic_as_numer_denom(basic numer, basic denom,
1823 SymEngine::as_numer_denom(x->m, SymEngine::outArg(numer->m),
1824 SymEngine::outArg(denom->m));
1839 const CVecBasic *exprs,
int perform_cse)
1841 self->m.init(args->m, exprs->m, perform_cse);
1846 const double *
const inps)
1848 self->m.call(outs, inps);
1856#ifdef HAVE_SYMENGINE_LLVM
1858struct CLLVMDoubleVisitor {
1859 SymEngine::LLVMDoubleVisitor m;
1862CLLVMDoubleVisitor *llvm_double_visitor_new()
1864 return new CLLVMDoubleVisitor();
1867void llvm_double_visitor_init(CLLVMDoubleVisitor *self,
const CVecBasic *args,
1868 const CVecBasic *exprs,
int perform_cse,
1871 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1874void llvm_double_visitor_call(CLLVMDoubleVisitor *self,
double *
const outs,
1875 const double *
const inps)
1877 self->m.call(outs, inps);
1880void llvm_double_visitor_free(CLLVMDoubleVisitor *self)
1885struct CLLVMFloatVisitor {
1886 SymEngine::LLVMFloatVisitor m;
1889CLLVMFloatVisitor *llvm_float_visitor_new()
1891 return new CLLVMFloatVisitor();
1894void llvm_float_visitor_init(CLLVMFloatVisitor *self,
const CVecBasic *args,
1895 const CVecBasic *exprs,
int perform_cse,
1898 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1901void llvm_float_visitor_call(CLLVMFloatVisitor *self,
float *
const outs,
1902 const float *
const inps)
1904 self->m.call(outs, inps);
1907void llvm_float_visitor_free(CLLVMFloatVisitor *self)
1911#ifdef SYMENGINE_HAVE_LLVM_LONG_DOUBLE
1913struct CLLVMLongDoubleVisitor {
1914 SymEngine::LLVMLongDoubleVisitor m;
1917CLLVMLongDoubleVisitor *llvm_long_double_visitor_new()
1919 return new CLLVMLongDoubleVisitor();
1922void llvm_long_double_visitor_init(CLLVMLongDoubleVisitor *self,
1924 const CVecBasic *exprs,
int perform_cse,
1927 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1930void llvm_long_double_visitor_call(CLLVMLongDoubleVisitor *self,
1931 long double *
const outs,
1932 const long double *
const inps)
1934 self->m.call(outs, inps);
1937void llvm_long_double_visitor_free(CLLVMLongDoubleVisitor *self)
1944CWRAPPER_OUTPUT_TYPE basic_cse(
CVecBasic *replacement_syms,
1949 vec_pair replacements;
1950 SymEngine::cse(replacements, reduced_exprs->m, exprs->m);
1951 for (
auto &p : replacements) {
1953 replacement_exprs->m.
push_back(p.second);
1958void symengine_print_stack_on_segfault()
1960 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()
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)