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>
14 using SymEngine::LLVMDoubleVisitor;
15 using SymEngine::LLVMFloatVisitor;
16 #ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
17 using SymEngine::LLVMLongDoubleVisitor;
21 #define xstr(s) str(s)
31 using SymEngine::down_cast;
32 using SymEngine::function_symbol;
34 using SymEngine::has_symbol;
36 using SymEngine::integer_class;
41 using SymEngine::rational_class;
45 using SymEngine::zero;
46 #ifdef HAVE_SYMENGINE_MPFR
47 using SymEngine::mpfr_class;
50 #ifdef HAVE_SYMENGINE_MPC
55 using SymEngine::rcp_static_cast;
62 #if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
63 using SymEngine::get_mpq_t;
64 using SymEngine::get_mpz_t;
66 using SymEngine::ccode;
67 using SymEngine::diag;
69 using SymEngine::jscode;
70 using SymEngine::julia_str;
71 using SymEngine::latex;
72 using SymEngine::mathml;
73 using SymEngine::mp_get_si;
74 using SymEngine::mp_get_ui;
75 using SymEngine::numeric_cast;
76 using SymEngine::ones;
77 using SymEngine::parse;
78 using SymEngine::SymEngineException;
79 using SymEngine::zeros;
85 inline bool is_aligned(T *p,
size_t n =
alignof(T))
87 return 0 ==
reinterpret_cast<uintptr_t
>(p) % n;
98 #define CWRAPPER_BEGIN try {
100 #define CWRAPPER_END \
101 return SYMENGINE_NO_EXCEPTION; \
103 catch (SymEngineException & e) \
105 return e.error_code(); \
109 return SYMENGINE_RUNTIME_ERROR; \
113 SymEngine::RCP<const SymEngine::Basic> m;
121 "Size of 'basic' is not correct");
124 "Alignment of 'basic' is not correct");
126 void basic_new_stack(basic s)
131 void basic_free_stack(basic s)
146 const char *symengine_version()
148 return SYMENGINE_VERSION;
151 void basic_const_set(basic s,
const char *c)
156 void basic_const_zero(basic s)
158 s->m = SymEngine::zero;
161 void basic_const_one(basic s)
163 s->m = SymEngine::one;
166 void basic_const_minus_one(basic s)
168 s->m = SymEngine::minus_one;
171 void basic_const_I(basic s)
176 void basic_const_pi(basic s)
178 s->m = SymEngine::pi;
181 void basic_const_E(basic s)
186 void basic_const_EulerGamma(basic s)
188 s->m = SymEngine::EulerGamma;
191 void basic_const_Catalan(basic s)
193 s->m = SymEngine::Catalan;
196 void basic_const_GoldenRatio(basic s)
198 s->m = SymEngine::GoldenRatio;
201 void basic_const_infinity(basic s)
203 s->m = SymEngine::Inf;
206 void basic_const_neginfinity(basic s)
208 s->m = SymEngine::NegInf;
211 void basic_const_complex_infinity(basic s)
213 s->m = SymEngine::ComplexInf;
216 void basic_const_nan(basic s)
218 s->m = SymEngine::Nan;
221 TypeID basic_get_class_id(
const char *c)
224 #define SYMENGINE_INCLUDE_ALL
225 #define SYMENGINE_ENUM(type, Class) {xstr(Class), type},
226 #include "symengine/type_codes.inc"
227 #undef SYMENGINE_ENUM
228 #undef SYMENGINE_INCLUDE_ALL
229 {
"", SYMENGINE_TypeID_Count}};
234 char *basic_get_class_from_id(TypeID
id)
237 #define SYMENGINE_INCLUDE_ALL
238 #define SYMENGINE_ENUM(type, Class) {type, xstr(Class)},
239 #include "symengine/type_codes.inc"
240 #undef SYMENGINE_ENUM
241 #undef SYMENGINE_INCLUDE_ALL
242 {SYMENGINE_TypeID_Count,
""}};
245 auto cc =
new char[name.
length() + 1];
250 TypeID basic_get_type(
const basic s)
252 return static_cast<TypeID
>(s->m->get_type_code());
255 CWRAPPER_OUTPUT_TYPE symbol_set(basic s,
const char *c)
262 int number_is_zero(
const basic s)
265 return (
int)((down_cast<const Number &>(*(s->m))).
is_zero());
268 int number_is_negative(
const basic s)
271 return (
int)((down_cast<const Number &>(*(s->m))).is_negative());
274 int number_is_positive(
const basic s)
277 return (
int)((down_cast<const Number &>(*(s->m))).is_positive());
280 int number_is_complex(
const basic s)
283 return (
int)((down_cast<const Number &>(*(s->m))).is_complex());
286 int basic_has_symbol(
const basic e,
const basic s)
288 return (
int)(has_symbol(*(e->m), *(s->m)));
291 CWRAPPER_OUTPUT_TYPE integer_set_si(basic s,
long i)
298 CWRAPPER_OUTPUT_TYPE integer_set_ui(basic s,
unsigned long i)
305 #if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
306 CWRAPPER_OUTPUT_TYPE integer_set_mpz(basic s,
const mpz_t i)
314 CWRAPPER_OUTPUT_TYPE integer_set_str(basic s,
const char *c)
321 CWRAPPER_OUTPUT_TYPE real_double_set_d(basic s,
double d)
324 s->m = SymEngine::real_double(d);
328 double real_double_get_d(
const basic s)
330 SYMENGINE_ASSERT(is_a<RealDouble>(*(s->m)));
331 return (down_cast<const RealDouble &>(*(s->m))).as_double();
334 #ifdef HAVE_SYMENGINE_MPFR
336 CWRAPPER_OUTPUT_TYPE real_mpfr_set_d(basic s,
double d,
int prec)
339 mpfr_class mc = mpfr_class(prec);
340 mpfr_set_d(mc.get_mpfr_t(), d, MPFR_RNDN);
341 s->m = SymEngine::real_mpfr(
std::move(mc));
345 CWRAPPER_OUTPUT_TYPE real_mpfr_set_str(basic s,
const char *c,
int prec)
348 s->m = SymEngine::real_mpfr(mpfr_class(c, prec, 10));
352 double real_mpfr_get_d(
const basic s)
354 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
356 ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
360 CWRAPPER_OUTPUT_TYPE real_mpfr_set(basic s, mpfr_srcptr m)
363 s->m = SymEngine::real_mpfr(mpfr_class(m));
367 CWRAPPER_OUTPUT_TYPE real_mpfr_get(mpfr_ptr m,
const basic s)
370 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
371 mpfr_set(m, ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
376 mpfr_prec_t real_mpfr_get_prec(
const basic s)
378 SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
379 return ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_prec();
384 CWRAPPER_OUTPUT_TYPE complex_base_real_part(basic s,
const basic com)
388 s->m = (down_cast<const ComplexBase &>(*(com->m))).real_part();
392 CWRAPPER_OUTPUT_TYPE complex_base_imaginary_part(basic s,
const basic com)
396 s->m = (down_cast<const ComplexBase &>(*(com->m))).imaginary_part();
400 signed long integer_get_si(
const basic s)
402 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
403 return mp_get_si((down_cast<const Integer &>(*(s->m))).as_integer_class());
406 unsigned long integer_get_ui(
const basic s)
408 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
409 return mp_get_ui((down_cast<const Integer &>(*(s->m))).as_integer_class());
412 #if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
413 CWRAPPER_OUTPUT_TYPE integer_get_mpz(mpz_t a,
const basic s)
416 SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
418 a, get_mpz_t((down_cast<const Integer &>(*(s->m))).as_integer_class()));
423 CWRAPPER_OUTPUT_TYPE rational_set_si(basic s,
long a,
long b)
430 CWRAPPER_OUTPUT_TYPE rational_set_ui(basic s,
unsigned long a,
unsigned long b)
437 CWRAPPER_OUTPUT_TYPE rational_set(basic s,
const basic a,
const basic b)
439 if (not is_a_Integer(a) or not is_a_Integer(b)) {
440 return SYMENGINE_RUNTIME_ERROR;
443 *(rcp_static_cast<const Integer>(a->m)),
444 *(rcp_static_cast<const Integer>(b->m)));
445 return SYMENGINE_NO_EXCEPTION;
448 #if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
449 CWRAPPER_OUTPUT_TYPE rational_get_mpq(mpq_t a,
const basic s)
452 SYMENGINE_ASSERT(is_a<Rational>(*(s->m)));
453 mpq_set(a, get_mpq_t(
454 (down_cast<const Rational &>(*(s->m))).as_rational_class()));
458 CWRAPPER_OUTPUT_TYPE rational_set_mpq(basic s,
const mpq_t i)
466 CWRAPPER_OUTPUT_TYPE complex_set(basic s,
const basic re,
const basic im)
470 *(rcp_static_cast<const Number>(re->m)),
471 *(rcp_static_cast<const Number>(im->m)));
475 CWRAPPER_OUTPUT_TYPE complex_set_rat(basic s,
const basic re,
const basic im)
479 *(rcp_static_cast<const Rational>(re->m)),
480 *(rcp_static_cast<const Rational>(im->m)));
484 #if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
485 CWRAPPER_OUTPUT_TYPE complex_set_mpq(basic s,
const mpq_t re,
const mpq_t im)
493 dcomplex complex_double_get(
const basic s)
495 SYMENGINE_ASSERT(is_a<ComplexDouble>(*(s->m)));
497 d.real = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
499 d.imag = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
504 char *basic_dumps(
const basic s,
unsigned long *size)
507 *size = str.length();
508 auto cc =
new char[*size];
513 CWRAPPER_OUTPUT_TYPE basic_loads(basic s,
const char *c,
unsigned long size)
517 s->m = Basic::loads(data);
521 CWRAPPER_OUTPUT_TYPE basic_diff(basic s,
const basic expr, basic
const symbol)
523 if (not is_a_Symbol(
symbol))
524 return SYMENGINE_RUNTIME_ERROR;
526 s->m = expr->m->diff(rcp_static_cast<const Symbol>(
symbol->m));
530 CWRAPPER_OUTPUT_TYPE basic_assign(basic a,
const basic b)
537 CWRAPPER_OUTPUT_TYPE basic_parse(basic b,
const char *str)
540 b->m = SymEngine::parse(str);
544 CWRAPPER_OUTPUT_TYPE basic_parse2(basic b,
const char *str,
int convert_xor)
547 if (convert_xor > 0) {
548 b->m = SymEngine::parse(str);
550 b->m = SymEngine::parse(str,
false);
555 CWRAPPER_OUTPUT_TYPE basic_add(basic s,
const basic a,
const basic b)
562 CWRAPPER_OUTPUT_TYPE basic_sub(basic s,
const basic a,
const basic b)
569 CWRAPPER_OUTPUT_TYPE basic_mul(basic s,
const basic a,
const basic b)
576 CWRAPPER_OUTPUT_TYPE basic_pow(basic s,
const basic a,
const basic b)
579 s->m = SymEngine::pow(a->m, b->m);
583 CWRAPPER_OUTPUT_TYPE basic_div(basic s,
const basic a,
const basic b)
590 int basic_eq(
const basic a,
const basic b)
595 int basic_neq(
const basic a,
const basic b)
600 #define IMPLEMENT_ONE_ARG_FUNC(func) \
601 CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a) \
604 s->m = SymEngine::func(a->m); \
608 IMPLEMENT_ONE_ARG_FUNC(
expand)
609 IMPLEMENT_ONE_ARG_FUNC(neg)
610 IMPLEMENT_ONE_ARG_FUNC(abs)
611 IMPLEMENT_ONE_ARG_FUNC(erf)
612 IMPLEMENT_ONE_ARG_FUNC(erfc)
613 IMPLEMENT_ONE_ARG_FUNC(sin)
614 IMPLEMENT_ONE_ARG_FUNC(cos)
615 IMPLEMENT_ONE_ARG_FUNC(tan)
616 IMPLEMENT_ONE_ARG_FUNC(csc)
617 IMPLEMENT_ONE_ARG_FUNC(sec)
618 IMPLEMENT_ONE_ARG_FUNC(cot)
619 IMPLEMENT_ONE_ARG_FUNC(asin)
620 IMPLEMENT_ONE_ARG_FUNC(acos)
621 IMPLEMENT_ONE_ARG_FUNC(asec)
622 IMPLEMENT_ONE_ARG_FUNC(acsc)
623 IMPLEMENT_ONE_ARG_FUNC(atan)
624 IMPLEMENT_ONE_ARG_FUNC(acot)
625 IMPLEMENT_ONE_ARG_FUNC(sinh)
626 IMPLEMENT_ONE_ARG_FUNC(cosh)
627 IMPLEMENT_ONE_ARG_FUNC(tanh)
628 IMPLEMENT_ONE_ARG_FUNC(csch)
629 IMPLEMENT_ONE_ARG_FUNC(sech)
630 IMPLEMENT_ONE_ARG_FUNC(coth)
631 IMPLEMENT_ONE_ARG_FUNC(asinh)
632 IMPLEMENT_ONE_ARG_FUNC(acosh)
633 IMPLEMENT_ONE_ARG_FUNC(asech)
634 IMPLEMENT_ONE_ARG_FUNC(acsch)
635 IMPLEMENT_ONE_ARG_FUNC(atanh)
636 IMPLEMENT_ONE_ARG_FUNC(acoth)
637 IMPLEMENT_ONE_ARG_FUNC(lambertw)
638 IMPLEMENT_ONE_ARG_FUNC(zeta)
639 IMPLEMENT_ONE_ARG_FUNC(dirichlet_eta)
640 IMPLEMENT_ONE_ARG_FUNC(gamma)
641 IMPLEMENT_ONE_ARG_FUNC(loggamma)
642 IMPLEMENT_ONE_ARG_FUNC(
sqrt)
643 IMPLEMENT_ONE_ARG_FUNC(
cbrt)
644 IMPLEMENT_ONE_ARG_FUNC(exp)
645 IMPLEMENT_ONE_ARG_FUNC(log)
646 IMPLEMENT_ONE_ARG_FUNC(floor)
647 IMPLEMENT_ONE_ARG_FUNC(ceiling)
648 IMPLEMENT_ONE_ARG_FUNC(sign)
650 #define IMPLEMENT_TWO_ARG_FUNC(func) \
651 CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a, const basic b) \
654 s->m = SymEngine::func(a->m, b->m); \
658 IMPLEMENT_TWO_ARG_FUNC(atan2)
659 IMPLEMENT_TWO_ARG_FUNC(kronecker_delta)
660 IMPLEMENT_TWO_ARG_FUNC(lowergamma)
661 IMPLEMENT_TWO_ARG_FUNC(uppergamma)
662 IMPLEMENT_TWO_ARG_FUNC(beta)
663 IMPLEMENT_TWO_ARG_FUNC(polygamma)
665 #define IMPLEMENT_STR_CONVERSION(name, func) \
666 char *basic_##name(const basic s) \
671 } catch (SymEngineException & e) { \
676 auto cc = new char[str.length() + 1]; \
677 std::strcpy(cc, str.c_str()); \
681 IMPLEMENT_STR_CONVERSION(str, _str)
682 IMPLEMENT_STR_CONVERSION(str_julia, julia_str)
683 IMPLEMENT_STR_CONVERSION(str_mathml, mathml)
684 IMPLEMENT_STR_CONVERSION(str_latex, latex)
685 IMPLEMENT_STR_CONVERSION(str_ccode, ccode)
686 IMPLEMENT_STR_CONVERSION(str_jscode, jscode)
688 void basic_str_free(
char *s)
693 void bool_set_true(basic s)
695 s->m = SymEngine::boolTrue;
698 void bool_set_false(basic s)
700 s->m = SymEngine::boolFalse;
703 CWRAPPER_OUTPUT_TYPE basic_set_interval(basic s,
const basic start,
704 const basic end,
int left_open,
712 rcp_static_cast<const Number>(
end->m),
713 (
bool)left_open, (
bool)right_open);
717 CWRAPPER_OUTPUT_TYPE basic_set_finiteset(basic s,
const CSetBasic *container)
724 void basic_set_emptyset(basic s)
729 void basic_set_universalset(basic s)
734 void basic_set_complexes(basic s)
739 void basic_set_reals(basic s)
744 void basic_set_rationals(basic s)
749 void basic_set_integers(basic s)
754 CWRAPPER_OUTPUT_TYPE basic_set_union(basic s,
const basic a,
const basic b)
757 s->m = rcp_static_cast<const Set>(a->m)->set_union(
758 rcp_static_cast<const Set>(b->m));
762 CWRAPPER_OUTPUT_TYPE basic_set_intersection(basic s,
const basic a,
766 s->m = rcp_static_cast<const Set>(a->m)->set_intersection(
767 rcp_static_cast<const Set>(b->m));
771 CWRAPPER_OUTPUT_TYPE basic_set_complement(basic s,
const basic a,
const basic b)
774 s->m = rcp_static_cast<const Set>(a->m)->set_complement(
775 rcp_static_cast<const Set>(b->m));
779 CWRAPPER_OUTPUT_TYPE basic_set_contains(basic s,
const basic a,
const basic b)
782 s->m = rcp_static_cast<const Set>(a->m)->contains(b->m);
786 int basic_set_is_subset(
const basic a,
const basic b)
788 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
789 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
790 return rcp_static_cast<const Set>(a->m)->is_subset(
791 rcp_static_cast<const Set>(b->m));
794 int basic_set_is_proper_subset(
const basic a,
const basic b)
796 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
797 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
798 return rcp_static_cast<const Set>(a->m)->is_proper_subset(
799 rcp_static_cast<const Set>(b->m));
802 int basic_set_is_superset(
const basic a,
const basic b)
804 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
805 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
806 return rcp_static_cast<const Set>(a->m)->is_superset(
807 rcp_static_cast<const Set>(b->m));
810 int basic_set_is_proper_superset(
const basic a,
const basic b)
812 SYMENGINE_ASSERT(is_a_Set(*(a->m)));
813 SYMENGINE_ASSERT(is_a_Set(*(b->m)));
814 return rcp_static_cast<const Set>(a->m)->is_proper_superset(
815 rcp_static_cast<const Set>(b->m));
818 CWRAPPER_OUTPUT_TYPE basic_set_inf(basic s,
const basic a)
821 s->m = SymEngine::inf(*rcp_static_cast<const Set>(a->m));
825 CWRAPPER_OUTPUT_TYPE basic_set_sup(basic s,
const basic a)
828 s->m = SymEngine::sup(*rcp_static_cast<const Set>(a->m));
832 CWRAPPER_OUTPUT_TYPE basic_set_boundary(basic s,
const basic a)
835 s->m = SymEngine::boundary(*rcp_static_cast<const Set>(a->m));
839 CWRAPPER_OUTPUT_TYPE basic_set_interior(basic s,
const basic a)
842 s->m = SymEngine::interior(*rcp_static_cast<const Set>(a->m));
846 CWRAPPER_OUTPUT_TYPE basic_set_closure(basic s,
const basic a)
849 s->m = SymEngine::closure(*rcp_static_cast<const Set>(a->m));
853 int symengine_have_component(
const char *c)
855 #ifdef HAVE_SYMENGINE_MPFR
859 #ifdef HAVE_SYMENGINE_MPC
863 #ifdef HAVE_SYMENGINE_FLINT
867 #ifdef HAVE_SYMENGINE_ARB
871 #ifdef HAVE_SYMENGINE_ECM
875 #ifdef HAVE_SYMENGINE_PRIMESIEVE
879 #ifdef HAVE_SYMENGINE_PIRANHA
883 #ifdef HAVE_SYMENGINE_BOOST
887 #ifdef HAVE_SYMENGINE_PTHREAD
891 #ifdef HAVE_SYMENGINE_LLVM
895 #ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
906 int is_a_Integer(
const basic c)
908 return is_a<Integer>(*(c->m));
910 int is_a_Rational(
const basic c)
912 return is_a<Rational>(*(c->m));
914 int is_a_Symbol(
const basic c)
916 return is_a<Symbol>(*(c->m));
920 return is_a<Complex>(*(c->m));
922 int is_a_RealDouble(
const basic c)
924 return is_a<RealDouble>(*(c->m));
926 int is_a_ComplexDouble(
const basic c)
928 return is_a<ComplexDouble>(*(c->m));
930 int is_a_RealMPFR(
const basic c)
932 #ifdef HAVE_SYMENGINE_MPFR
933 return is_a<RealMPFR>(*(c->m));
938 int is_a_ComplexMPC(
const basic c)
940 #ifdef HAVE_SYMENGINE_MPC
941 return is_a<ComplexMPC>(*(c->m));
946 int is_a_Set(
const basic c)
948 return SymEngine::is_a_Set(*(c->m));
962 int vectorint_placement_new_check(
void *data,
size_t size)
967 if (not SymEngine::is_aligned(
self))
972 CVectorInt *vectorint_placement_new(
void *data)
974 #if defined(WITH_SYMENGINE_ASSERT)
977 SYMENGINE_ASSERT(SymEngine::is_aligned(
self));
983 void vectorint_placement_free(
CVectorInt *
self)
985 self->m.~vector<
int>();
993 void vectorint_push_back(
CVectorInt *
self,
int value)
995 self->m.push_back(value);
1019 CWRAPPER_OUTPUT_TYPE vecbasic_push_back(
CVecBasic *
self,
const basic value)
1023 self->m.push_back(value->m);
1028 CWRAPPER_OUTPUT_TYPE vecbasic_get(
CVecBasic *
self,
size_t n, basic result)
1032 SYMENGINE_ASSERT(n < self->m.size());
1033 result->m =
self->m[n];
1038 CWRAPPER_OUTPUT_TYPE vecbasic_set(
CVecBasic *
self,
size_t n,
const basic s)
1041 SYMENGINE_ASSERT(n < self->m.size());
1046 CWRAPPER_OUTPUT_TYPE vecbasic_erase(
CVecBasic *
self,
size_t n)
1049 SYMENGINE_ASSERT(n < self->m.size());
1050 self->m.erase(self->m.begin() + n);
1056 return self->m.size();
1059 CWRAPPER_OUTPUT_TYPE basic_max(basic s,
const CVecBasic *d)
1066 CWRAPPER_OUTPUT_TYPE basic_min(basic s,
const CVecBasic *d)
1073 CWRAPPER_OUTPUT_TYPE basic_add_vec(basic s,
const CVecBasic *d)
1080 CWRAPPER_OUTPUT_TYPE basic_mul_vec(basic s,
const CVecBasic *d)
1107 CDenseMatrix *dense_matrix_new_rows_cols(
unsigned rows,
unsigned cols)
1132 void sparse_matrix_rows_cols(
CSparseMatrix *s,
unsigned long int rows,
1133 unsigned long int cols)
1136 numeric_cast<unsigned>(cols));
1149 auto cc =
new char[str.length() + 1];
1157 auto cc =
new char[str.length() + 1];
1162 CWRAPPER_OUTPUT_TYPE dense_matrix_rows_cols(
CDenseMatrix *mat,
unsigned r,
1166 mat->m.resize(r, c);
1170 CWRAPPER_OUTPUT_TYPE dense_matrix_get_basic(basic s,
const CDenseMatrix *mat,
1171 unsigned long int r,
1172 unsigned long int c)
1175 s->m = mat->m.get(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c));
1179 CWRAPPER_OUTPUT_TYPE dense_matrix_set_basic(
CDenseMatrix *mat,
1180 unsigned long int r,
1181 unsigned long int c, basic s)
1184 mat->m.set(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c), s->m);
1188 CWRAPPER_OUTPUT_TYPE sparse_matrix_get_basic(basic s,
const CSparseMatrix *mat,
1189 unsigned long int r,
1190 unsigned long int c)
1193 s->m = mat->m.get(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c));
1197 CWRAPPER_OUTPUT_TYPE sparse_matrix_set_basic(
CSparseMatrix *mat,
1198 unsigned long int r,
1199 unsigned long int c, basic s)
1202 mat->m.set(numeric_cast<unsigned>(r), numeric_cast<unsigned>(c), s->m);
1206 CWRAPPER_OUTPUT_TYPE dense_matrix_det(basic s,
const CDenseMatrix *mat)
1209 s->m = mat->m.det();
1215 dense_matrix_rows_cols(s, mat->m.nrows(), mat->m.ncols());
1219 CWRAPPER_OUTPUT_TYPE dense_matrix_transpose(
CDenseMatrix *s,
1223 dense_matrix_rows_cols(s, mat->m.ncols(), mat->m.nrows());
1224 mat->m.transpose(s->m);
1227 CWRAPPER_OUTPUT_TYPE
1229 unsigned long int r1,
unsigned long int c1,
1230 unsigned long int r2,
unsigned long int c2,
1231 unsigned long int r,
unsigned long int c)
1234 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r2 - r1 + 1),
1235 numeric_cast<unsigned>(c2 - c1 + 1));
1236 mat->m.submatrix(s->m, numeric_cast<unsigned>(r1),
1237 numeric_cast<unsigned>(c1), numeric_cast<unsigned>(r2),
1238 numeric_cast<unsigned>(c2), numeric_cast<unsigned>(r),
1239 numeric_cast<unsigned>(c));
1243 CWRAPPER_OUTPUT_TYPE dense_matrix_row_join(
CDenseMatrix *A,
1247 A->m.row_join(B->m);
1251 CWRAPPER_OUTPUT_TYPE dense_matrix_col_join(
CDenseMatrix *A,
1255 A->m.col_join(B->m);
1259 CWRAPPER_OUTPUT_TYPE dense_matrix_row_del(
CDenseMatrix *A,
unsigned k)
1266 CWRAPPER_OUTPUT_TYPE dense_matrix_col_del(
CDenseMatrix *A,
unsigned k)
1273 unsigned long int dense_matrix_rows(
const CDenseMatrix *s)
1275 return s->m.nrows();
1278 unsigned long int dense_matrix_cols(
const CDenseMatrix *s)
1280 return s->m.ncols();
1283 CWRAPPER_OUTPUT_TYPE dense_matrix_add_matrix(
CDenseMatrix *s,
1288 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1289 matA->m.add_matrix(matB->m, s->m);
1293 CWRAPPER_OUTPUT_TYPE dense_matrix_mul_matrix(
CDenseMatrix *s,
1298 dense_matrix_rows_cols(s, matA->m.nrows(), matB->m.ncols());
1299 matA->m.mul_matrix(matB->m, s->m);
1303 CWRAPPER_OUTPUT_TYPE dense_matrix_add_scalar(
CDenseMatrix *s,
1308 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1309 matA->m.add_scalar(b->m, s->m);
1313 CWRAPPER_OUTPUT_TYPE dense_matrix_mul_scalar(
CDenseMatrix *s,
1318 dense_matrix_rows_cols(s, matA->m.nrows(), matA->m.ncols());
1319 matA->m.mul_scalar(b->m, s->m);
1327 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1328 dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols());
1329 mat->m.LU(l->m, u->m);
1337 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1338 dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols());
1339 mat->m.LDL(l->m, d->m);
1343 CWRAPPER_OUTPUT_TYPE dense_matrix_FFLU(
CDenseMatrix *lu,
1347 dense_matrix_rows_cols(lu, mat->m.nrows(), mat->m.ncols());
1357 dense_matrix_rows_cols(l, mat->m.nrows(), mat->m.ncols());
1358 dense_matrix_rows_cols(d, mat->m.nrows(), mat->m.ncols());
1359 dense_matrix_rows_cols(u, mat->m.nrows(), mat->m.ncols());
1360 mat->m.FFLDU(l->m, d->m, u->m);
1364 CWRAPPER_OUTPUT_TYPE dense_matrix_LU_solve(
CDenseMatrix *x,
1369 dense_matrix_rows_cols(x, A->m.ncols(), 1);
1370 A->m.LU_solve(b->m, x->m);
1374 CWRAPPER_OUTPUT_TYPE dense_matrix_ones(
CDenseMatrix *s,
unsigned long int r,
1375 unsigned long int c)
1378 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r),
1379 numeric_cast<unsigned>(c));
1384 CWRAPPER_OUTPUT_TYPE dense_matrix_zeros(
CDenseMatrix *s,
unsigned long int r,
1385 unsigned long int c)
1388 dense_matrix_rows_cols(s, numeric_cast<unsigned>(r),
1389 numeric_cast<unsigned>(c));
1397 unsigned int vec_size = numeric_cast<unsigned>(vecbasic_size(d));
1398 dense_matrix_rows_cols(
1399 s, numeric_cast<unsigned>(vec_size + (k >= 0 ? k : -k)),
1400 numeric_cast<unsigned>(vec_size + (k >= 0 ? k : -k)));
1401 diag(s->m, d->m, numeric_cast<int>(k));
1405 CWRAPPER_OUTPUT_TYPE dense_matrix_eye(
CDenseMatrix *s,
unsigned long int N,
1406 unsigned long int M,
int k)
1409 dense_matrix_rows_cols(s, numeric_cast<unsigned>(N),
1410 numeric_cast<unsigned>(M));
1415 CWRAPPER_OUTPUT_TYPE dense_matrix_diff(
CDenseMatrix *result,
1418 if (not is_a_Symbol(x))
1419 return SYMENGINE_RUNTIME_ERROR;
1421 diff(A->m, rcp_static_cast<const Symbol>(x->m), result->m);
1425 CWRAPPER_OUTPUT_TYPE dense_matrix_jacobian(
CDenseMatrix *result,
1430 jacobian(A->m, x->m, result->m);
1436 return is_a<DenseMatrix>(c->m);
1441 return is_a<CSRMatrix>(c->m);
1446 return (lhs->m) == (rhs->m);
1451 return (lhs->m) == (rhs->m);
1466 int setbasic_insert(
CSetBasic *
self,
const basic value)
1468 return (self->m.insert(value->m)).second ? 1 : 0;
1471 void setbasic_get(
CSetBasic *
self,
int n, basic result)
1473 result->m = *
std::next((self->m).begin(), n);
1476 int setbasic_find(
CSetBasic *
self, basic value)
1478 return self->m.find(value->m) != (
self->m).
end() ? 1 : 0;
1481 int setbasic_erase(
CSetBasic *
self,
const basic value)
1483 return (self->m.erase(value->m)) ? 1 : 0;
1488 return self->m.size();
1510 (
self->m)[key->m] = mapped->m;
1513 int mapbasicbasic_get(
CMapBasicBasic *
self,
const basic key, basic mapped)
1515 auto it =
self->m.find(key->m);
1516 if (it != self->m.end()) {
1517 mapped->m = it->second;
1525 return self->m.size();
1530 CWRAPPER_OUTPUT_TYPE basic_get_args(
const basic
self,
CVecBasic *args)
1533 args->m =
self->m->get_args();
1537 CWRAPPER_OUTPUT_TYPE basic_free_symbols(
const basic
self,
CSetBasic *symbols)
1540 symbols->m = SymEngine::free_symbols(*(self->m));
1544 CWRAPPER_OUTPUT_TYPE basic_function_symbols(
CSetBasic *symbols,
1548 symbols->m = SymEngine::atoms<SymEngine::FunctionSymbol>(*(self->m));
1552 size_t basic_hash(
const basic
self)
1554 return static_cast<size_t>(
self->m->hash());
1557 CWRAPPER_OUTPUT_TYPE basic_subs(basic s,
const basic e,
1561 s->m = e->m->subs(mapbb->m);
1565 CWRAPPER_OUTPUT_TYPE basic_subs2(basic s,
const basic e,
const basic a,
1569 s->m = e->m->subs({{a->m, b->m}});
1573 CWRAPPER_OUTPUT_TYPE function_symbol_set(basic s,
const char *c,
1577 s->m = function_symbol(c, arg->m);
1581 char *function_symbol_get_name(
const basic b)
1583 SYMENGINE_ASSERT(is_a<FunctionSymbol>(*(b->m)));
1584 std::string str = down_cast<const FunctionSymbol &>(*(b->m)).get_name();
1585 auto cc =
new char[str.length() + 1];
1590 CWRAPPER_OUTPUT_TYPE basic_coeff(basic c,
const basic b,
const basic x,
1594 c->m = SymEngine::coeff(*(b->m), *(x->m), *(n->m));
1604 vec_basic vb = sym->m;
1607 [](RCP<const Basic> b) { return is_a<const Symbol>(*b); }));
1608 vec_sym vs(vb.size());
1609 for (
unsigned i = 0; i < vb.size(); i++)
1610 vs[i] = rcp_static_cast<const Symbol>(vb[i]);
1611 sol->m = SymEngine::linsolve(sys->m, vs);
1615 CWRAPPER_OUTPUT_TYPE basic_solve_poly(
CSetBasic *r,
const basic f,
1619 SYMENGINE_ASSERT(is_a<Symbol>(*(s->m)));
1621 = SymEngine::solve_poly(f->m, rcp_static_cast<const Symbol>(s->m));
1622 if (not is_a<FiniteSet>(*
set)) {
1623 return SYMENGINE_NOT_IMPLEMENTED;
1625 r->m = down_cast<const FiniteSet &>(*set).get_container();
1631 char *ascii_art_str()
1634 auto cc =
new char[str.length() + 1];
1641 CWRAPPER_OUTPUT_TYPE ntheory_gcd(basic s,
const basic a,
const basic b)
1644 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1645 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1647 down_cast<const Integer &>(*(b->m)));
1651 CWRAPPER_OUTPUT_TYPE ntheory_lcm(basic s,
const basic a,
const basic b)
1654 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1655 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1657 down_cast<const Integer &>(*(b->m)));
1661 CWRAPPER_OUTPUT_TYPE ntheory_gcd_ext(basic g, basic s, basic t,
const basic a,
1665 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1666 SYMENGINE_ASSERT(is_a<Integer>(*(b->m)));
1667 SymEngine::RCP<const Integer> g_, s_, t_;
1669 SymEngine::outArg(t_),
1670 down_cast<const Integer &>(*(a->m)),
1671 down_cast<const Integer &>(*(b->m)));
1678 CWRAPPER_OUTPUT_TYPE ntheory_nextprime(basic s,
const basic a)
1681 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1686 CWRAPPER_OUTPUT_TYPE ntheory_mod(basic s,
const basic n,
const basic d)
1689 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1690 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1692 down_cast<const Integer &>(*(d->m)));
1696 CWRAPPER_OUTPUT_TYPE ntheory_quotient(basic s,
const basic n,
const basic d)
1699 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1700 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1702 down_cast<const Integer &>(*(d->m)));
1706 CWRAPPER_OUTPUT_TYPE ntheory_quotient_mod(basic q, basic r,
const basic n,
1710 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1711 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1712 SymEngine::RCP<const Integer> q_, r_;
1714 down_cast<const Integer &>(*(n->m)),
1715 down_cast<const Integer &>(*(d->m)));
1721 CWRAPPER_OUTPUT_TYPE ntheory_mod_f(basic s,
const basic n,
const basic d)
1724 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1725 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1727 down_cast<const Integer &>(*(d->m)));
1731 CWRAPPER_OUTPUT_TYPE ntheory_quotient_f(basic s,
const basic n,
const basic d)
1734 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1735 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1737 down_cast<const Integer &>(*(d->m)));
1741 CWRAPPER_OUTPUT_TYPE ntheory_quotient_mod_f(basic q, basic r,
const basic n,
1745 SYMENGINE_ASSERT(is_a<Integer>(*(n->m)));
1746 SYMENGINE_ASSERT(is_a<Integer>(*(d->m)));
1747 SymEngine::RCP<const Integer> q_, r_;
1749 down_cast<const Integer &>(*(n->m)),
1750 down_cast<const Integer &>(*(d->m)));
1756 int ntheory_mod_inverse(basic b,
const basic a,
const basic m)
1759 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1760 SYMENGINE_ASSERT(is_a<Integer>(*(m->m)));
1761 SymEngine::RCP<const Integer> b_;
1763 down_cast<const Integer &>(*(a->m)),
1764 down_cast<const Integer &>(*(m->m)));
1769 CWRAPPER_OUTPUT_TYPE ntheory_fibonacci(basic s,
unsigned long a)
1776 CWRAPPER_OUTPUT_TYPE ntheory_fibonacci2(basic g, basic s,
unsigned long a)
1779 SymEngine::RCP<const Integer> g_, s_;
1786 CWRAPPER_OUTPUT_TYPE ntheory_lucas(basic s,
unsigned long a)
1793 CWRAPPER_OUTPUT_TYPE ntheory_lucas2(basic g, basic s,
unsigned long a)
1796 SymEngine::RCP<const Integer> g_, s_;
1803 CWRAPPER_OUTPUT_TYPE ntheory_binomial(basic s,
const basic a,
unsigned long b)
1806 SYMENGINE_ASSERT(is_a<Integer>(*(a->m)));
1811 CWRAPPER_OUTPUT_TYPE ntheory_factorial(basic s,
unsigned long n)
1819 CWRAPPER_OUTPUT_TYPE basic_evalf(basic s,
const basic b,
unsigned long bits,
1824 s->m = SymEngine::evalf(*(b->m), bits, (SymEngine::EvalfDomain)real);
1828 CWRAPPER_OUTPUT_TYPE basic_as_numer_denom(basic numer, basic denom,
1832 SymEngine::as_numer_denom(x->m, SymEngine::outArg(numer->m),
1833 SymEngine::outArg(denom->m));
1837 CWRAPPER_OUTPUT_TYPE basic_add_as_two_terms(basic term1, basic term2,
1841 SYMENGINE_ASSERT(is_a<Add>(*(s->m)));
1842 rcp_static_cast<const Add>(s->m)->as_two_terms(SymEngine::outArg(term1->m),
1843 SymEngine::outArg(term2->m));
1847 CWRAPPER_OUTPUT_TYPE basic_mul_as_two_terms(basic term1, basic term2,
1851 SYMENGINE_ASSERT(is_a<Mul>(*(s->m)));
1852 rcp_static_cast<const Mul>(s->m)->as_two_terms(SymEngine::outArg(term1->m),
1853 SymEngine::outArg(term2->m));
1868 const CVecBasic *exprs,
int perform_cse)
1870 self->m.init(args->m, exprs->m, perform_cse);
1875 const double *
const inps)
1877 self->m.call(outs, inps);
1885 #ifdef HAVE_SYMENGINE_LLVM
1887 struct CLLVMDoubleVisitor {
1888 SymEngine::LLVMDoubleVisitor m;
1891 CLLVMDoubleVisitor *llvm_double_visitor_new()
1893 return new CLLVMDoubleVisitor();
1896 void llvm_double_visitor_init(CLLVMDoubleVisitor *
self,
const CVecBasic *args,
1897 const CVecBasic *exprs,
int perform_cse,
1900 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1903 void llvm_double_visitor_call(CLLVMDoubleVisitor *
self,
double *
const outs,
1904 const double *
const inps)
1906 self->m.call(outs, inps);
1909 void llvm_double_visitor_free(CLLVMDoubleVisitor *
self)
1914 struct CLLVMFloatVisitor {
1915 SymEngine::LLVMFloatVisitor m;
1918 CLLVMFloatVisitor *llvm_float_visitor_new()
1920 return new CLLVMFloatVisitor();
1923 void llvm_float_visitor_init(CLLVMFloatVisitor *
self,
const CVecBasic *args,
1924 const CVecBasic *exprs,
int perform_cse,
1927 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1930 void llvm_float_visitor_call(CLLVMFloatVisitor *
self,
float *
const outs,
1931 const float *
const inps)
1933 self->m.call(outs, inps);
1936 void llvm_float_visitor_free(CLLVMFloatVisitor *
self)
1940 #ifdef SYMENGINE_HAVE_LLVM_LONG_DOUBLE
1942 struct CLLVMLongDoubleVisitor {
1943 SymEngine::LLVMLongDoubleVisitor m;
1946 CLLVMLongDoubleVisitor *llvm_long_double_visitor_new()
1948 return new CLLVMLongDoubleVisitor();
1951 void llvm_long_double_visitor_init(CLLVMLongDoubleVisitor *
self,
1953 const CVecBasic *exprs,
int perform_cse,
1956 self->m.init(args->m, exprs->m, perform_cse, opt_level);
1959 void llvm_long_double_visitor_call(CLLVMLongDoubleVisitor *
self,
1960 long double *
const outs,
1961 const long double *
const inps)
1963 self->m.call(outs, inps);
1966 void llvm_long_double_visitor_free(CLLVMLongDoubleVisitor *
self)
1973 CWRAPPER_OUTPUT_TYPE basic_cse(
CVecBasic *replacement_syms,
1978 vec_pair replacements;
1979 SymEngine::cse(replacements, reduced_exprs->m, exprs->m);
1980 for (
auto &p : replacements) {
1982 replacement_exprs->m.
push_back(p.second);
1987 void symengine_print_stack_on_segfault()
1989 SymEngine::print_stack_on_segfault();
The base class for representing addition in symbolic expressions.
The lowest unit of symbolic representation.
std::string __str__() const
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.
bool is_a_Number(const Basic &b)
RCP< const Set > interval(const RCP< const Number > &start, const RCP< const Number > &end, const bool left_open=false, const bool right_open=false)
RCP< const Basic > cbrt(const RCP< const Basic > &arg)
RCP< const Complexes > complexes()
RCP< const Integer > nextprime(const Integer &a)
void fibonacci2(const Ptr< RCP< const Integer >> &g, const Ptr< RCP< const Integer >> &s, unsigned long n)
Fibonacci n and n-1.
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
RCP< const Symbol > symbol(const std::string &name)
inline version to return Symbol
RCP< const Reals > reals()
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
RCP< const EmptySet > emptyset()
RCP< const Integers > integers()
RCP< const Basic > sqrt(const RCP< const Basic > &arg)
RCP< const Integer > lucas(unsigned long n)
Lucas number.
RCP< const Integer > quotient_f(const Integer &n, const Integer &d)
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.
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)
int mod_inverse(const Ptr< RCP< const Integer >> &b, const Integer &a, const Integer &m)
inverse modulo
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.
void quotient_mod_f(const Ptr< RCP< const Integer >> &q, const Ptr< RCP< const Integer >> &r, const Integer &n, const Integer &d)
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
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).
RCP< const Constant > constant(const std::string &name)
inline version to return Constant
tribool is_zero(const Basic &b, const Assumptions *assumptions=nullptr)
Check if a number is zero.
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 Set > finiteset(const set_basic &container)
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b
RCP< const Basic > expand(const RCP< const Basic > &self, bool deep=true)
Expands self
RCP< const Basic > min(const vec_basic &arg)
Canonicalize Min:
bool is_a_Complex(const Basic &b)
RCP< const Rationals > rationals()
void quotient_mod(const Ptr< RCP< const Integer >> &q, const Ptr< RCP< const Integer >> &r, const Integer &n, const Integer &d)
void lucas2(const Ptr< RCP< const Integer >> &g, const Ptr< RCP< const Integer >> &s, unsigned long n)
Lucas number n and n-1.