-
Notifications
You must be signed in to change notification settings - Fork 269
/
cwrapper.cpp
1968 lines (1712 loc) · 50 KB
/
cwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdlib>
#include <cstring>
#include <symengine/symbol.h>
#include <symengine/cwrapper.h>
#include <symengine/printers.h>
#include <symengine/matrix.h>
#include <symengine/eval.h>
#include <symengine/parser.h>
#include <symengine/lambda_double.h>
#include <symengine/solve.h>
#ifdef HAVE_SYMENGINE_LLVM
#include <symengine/llvm_double.h>
using SymEngine::LLVMDoubleVisitor;
using SymEngine::LLVMFloatVisitor;
#ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
using SymEngine::LLVMLongDoubleVisitor;
#endif
#endif
#define xstr(s) str(s)
#define str(s) #s
using SymEngine::Basic;
using SymEngine::Complex;
using SymEngine::ComplexBase;
using SymEngine::ComplexDouble;
using SymEngine::CSRMatrix;
using SymEngine::DenseMatrix;
using SymEngine::down_cast;
using SymEngine::function_symbol;
using SymEngine::FunctionSymbol;
using SymEngine::has_symbol;
using SymEngine::Integer;
using SymEngine::integer_class;
using SymEngine::LambdaRealDoubleVisitor;
using SymEngine::Number;
using SymEngine::Rational;
using SymEngine::rational_class;
using SymEngine::RCP;
using SymEngine::RealDouble;
using SymEngine::Symbol;
using SymEngine::zero;
#ifdef HAVE_SYMENGINE_MPFR
using SymEngine::mpfr_class;
using SymEngine::RealMPFR;
#endif // HAVE_SYMENGINE_MPFR
#ifdef HAVE_SYMENGINE_MPC
using SymEngine::ComplexMPC;
#endif // HAVE_SYMENGINE_MPC
using SymEngine::FiniteSet;
using SymEngine::is_a;
using SymEngine::rcp_static_cast;
using SymEngine::RCPBasicKeyLess;
using SymEngine::Set;
using SymEngine::set_basic;
using SymEngine::vec_basic;
using SymEngine::vec_pair;
using SymEngine::vec_sym;
#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
using SymEngine::get_mpq_t;
using SymEngine::get_mpz_t;
#endif
using SymEngine::ccode;
using SymEngine::diag;
using SymEngine::eye;
using SymEngine::jscode;
using SymEngine::julia_str;
using SymEngine::latex;
using SymEngine::mathml;
using SymEngine::mp_get_si;
using SymEngine::mp_get_ui;
using SymEngine::numeric_cast;
using SymEngine::ones;
using SymEngine::parse;
using SymEngine::SymEngineException;
using SymEngine::zeros;
namespace SymEngine
{
template <typename T>
inline bool is_aligned(T *p, size_t n = alignof(T))
{
return 0 == reinterpret_cast<uintptr_t>(p) % n;
}
static std::string _str(const Basic &a)
{
return a.__str__();
}
} // namespace SymEngine
extern "C" {
#define CWRAPPER_BEGIN try {
#define CWRAPPER_END \
return SYMENGINE_NO_EXCEPTION; \
} \
catch (SymEngineException & e) \
{ \
return e.error_code(); \
} \
catch (...) \
{ \
return SYMENGINE_RUNTIME_ERROR; \
}
struct CRCPBasic {
SymEngine::RCP<const SymEngine::Basic> m;
};
struct CSetBasic {
SymEngine::set_basic m;
};
static_assert(sizeof(CRCPBasic) == sizeof(CRCPBasic_C),
"Size of 'basic' is not correct");
static_assert(std::alignment_of<CRCPBasic>::value
== std::alignment_of<CRCPBasic_C>::value,
"Alignment of 'basic' is not correct");
void basic_new_stack(basic s)
{
new (s) CRCPBasic();
}
void basic_free_stack(basic s)
{
s->m.~RCP();
}
basic_struct *basic_new_heap()
{
return new CRCPBasic();
}
void basic_free_heap(basic_struct *s)
{
delete s;
}
const char *symengine_version()
{
return SYMENGINE_VERSION;
}
void basic_const_set(basic s, const char *c)
{
s->m = SymEngine::constant(std::string(c));
}
void basic_const_zero(basic s)
{
s->m = SymEngine::zero;
}
void basic_const_one(basic s)
{
s->m = SymEngine::one;
}
void basic_const_minus_one(basic s)
{
s->m = SymEngine::minus_one;
}
void basic_const_I(basic s)
{
s->m = SymEngine::I;
}
void basic_const_pi(basic s)
{
s->m = SymEngine::pi;
}
void basic_const_E(basic s)
{
s->m = SymEngine::E;
}
void basic_const_EulerGamma(basic s)
{
s->m = SymEngine::EulerGamma;
}
void basic_const_Catalan(basic s)
{
s->m = SymEngine::Catalan;
}
void basic_const_GoldenRatio(basic s)
{
s->m = SymEngine::GoldenRatio;
}
void basic_const_infinity(basic s)
{
s->m = SymEngine::Inf;
}
void basic_const_neginfinity(basic s)
{
s->m = SymEngine::NegInf;
}
void basic_const_complex_infinity(basic s)
{
s->m = SymEngine::ComplexInf;
}
void basic_const_nan(basic s)
{
s->m = SymEngine::Nan;
}
TypeID basic_get_class_id(const char *c)
{
static std::map<std::string, TypeID> names = {
#define SYMENGINE_INCLUDE_ALL
#define SYMENGINE_ENUM(type, Class) {xstr(Class), type},
#include "symengine/type_codes.inc"
#undef SYMENGINE_ENUM
#undef SYMENGINE_INCLUDE_ALL
{"", SYMENGINE_TypeID_Count}};
return names[std::string(c)];
}
char *basic_get_class_from_id(TypeID id)
{
static std::map<TypeID, std::string> names = {
#define SYMENGINE_INCLUDE_ALL
#define SYMENGINE_ENUM(type, Class) {type, xstr(Class)},
#include "symengine/type_codes.inc"
#undef SYMENGINE_ENUM
#undef SYMENGINE_INCLUDE_ALL
{SYMENGINE_TypeID_Count, ""}};
std::string name = names[id];
auto cc = new char[name.length() + 1];
std::strcpy(cc, name.c_str());
return cc;
}
TypeID basic_get_type(const basic s)
{
return static_cast<TypeID>(s->m->get_type_code());
}
CWRAPPER_OUTPUT_TYPE symbol_set(basic s, const char *c)
{
CWRAPPER_BEGIN
s->m = SymEngine::symbol(std::string(c));
CWRAPPER_END
}
int number_is_zero(const basic s)
{
SYMENGINE_ASSERT(is_a_Number(*(s->m)));
return (int)((down_cast<const Number &>(*(s->m))).is_zero());
}
int number_is_negative(const basic s)
{
SYMENGINE_ASSERT(is_a_Number(*(s->m)));
return (int)((down_cast<const Number &>(*(s->m))).is_negative());
}
int number_is_positive(const basic s)
{
SYMENGINE_ASSERT(is_a_Number(*(s->m)));
return (int)((down_cast<const Number &>(*(s->m))).is_positive());
}
int number_is_complex(const basic s)
{
SYMENGINE_ASSERT(is_a_Number(*(s->m)));
return (int)((down_cast<const Number &>(*(s->m))).is_complex());
}
int basic_has_symbol(const basic e, const basic s)
{
return (int)(has_symbol(*(e->m), *(s->m)));
}
CWRAPPER_OUTPUT_TYPE integer_set_si(basic s, long i)
{
CWRAPPER_BEGIN
s->m = SymEngine::integer(integer_class(i));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE integer_set_ui(basic s, unsigned long i)
{
CWRAPPER_BEGIN
s->m = SymEngine::integer(integer_class(i));
CWRAPPER_END
}
#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
CWRAPPER_OUTPUT_TYPE integer_set_mpz(basic s, const mpz_t i)
{
CWRAPPER_BEGIN
s->m = SymEngine::integer(integer_class(i));
CWRAPPER_END
}
#endif
CWRAPPER_OUTPUT_TYPE integer_set_str(basic s, const char *c)
{
CWRAPPER_BEGIN
s->m = SymEngine::integer(integer_class(c));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE real_double_set_d(basic s, double d)
{
CWRAPPER_BEGIN
s->m = SymEngine::real_double(d);
CWRAPPER_END
}
double real_double_get_d(const basic s)
{
SYMENGINE_ASSERT(is_a<RealDouble>(*(s->m)));
return (down_cast<const RealDouble &>(*(s->m))).as_double();
}
#ifdef HAVE_SYMENGINE_MPFR
CWRAPPER_OUTPUT_TYPE real_mpfr_set_d(basic s, double d, int prec)
{
CWRAPPER_BEGIN
mpfr_class mc = mpfr_class(prec);
mpfr_set_d(mc.get_mpfr_t(), d, MPFR_RNDN);
s->m = SymEngine::real_mpfr(std::move(mc));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE real_mpfr_set_str(basic s, const char *c, int prec)
{
CWRAPPER_BEGIN
s->m = SymEngine::real_mpfr(mpfr_class(c, prec, 10));
CWRAPPER_END
}
double real_mpfr_get_d(const basic s)
{
SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
return mpfr_get_d(
((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
MPFR_RNDN);
}
CWRAPPER_OUTPUT_TYPE real_mpfr_set(basic s, mpfr_srcptr m)
{
CWRAPPER_BEGIN
s->m = SymEngine::real_mpfr(mpfr_class(m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE real_mpfr_get(mpfr_ptr m, const basic s)
{
CWRAPPER_BEGIN
SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
mpfr_set(m, ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_mpfr_t(),
MPFR_RNDN);
CWRAPPER_END
}
mpfr_prec_t real_mpfr_get_prec(const basic s)
{
SYMENGINE_ASSERT(is_a<RealMPFR>(*(s->m)));
return ((down_cast<const RealMPFR &>(*(s->m))).as_mpfr()).get_prec();
}
#endif // HAVE_SYMENGINE_MPFR
CWRAPPER_OUTPUT_TYPE complex_base_real_part(basic s, const basic com)
{
CWRAPPER_BEGIN
SYMENGINE_ASSERT(SymEngine::is_a_Complex(*(com->m)));
s->m = (down_cast<const ComplexBase &>(*(com->m))).real_part();
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE complex_base_imaginary_part(basic s, const basic com)
{
CWRAPPER_BEGIN
SYMENGINE_ASSERT(SymEngine::is_a_Complex(*(com->m)));
s->m = (down_cast<const ComplexBase &>(*(com->m))).imaginary_part();
CWRAPPER_END
}
signed long integer_get_si(const basic s)
{
SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
return mp_get_si((down_cast<const Integer &>(*(s->m))).as_integer_class());
}
unsigned long integer_get_ui(const basic s)
{
SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
return mp_get_ui((down_cast<const Integer &>(*(s->m))).as_integer_class());
}
#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
CWRAPPER_OUTPUT_TYPE integer_get_mpz(mpz_t a, const basic s)
{
CWRAPPER_BEGIN
SYMENGINE_ASSERT(is_a<Integer>(*(s->m)));
mpz_set(
a, get_mpz_t((down_cast<const Integer &>(*(s->m))).as_integer_class()));
CWRAPPER_END
}
#endif
CWRAPPER_OUTPUT_TYPE rational_set_si(basic s, long a, long b)
{
CWRAPPER_BEGIN
s->m = SymEngine::Rational::from_mpq(rational_class(a, b));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE rational_set_ui(basic s, unsigned long a, unsigned long b)
{
CWRAPPER_BEGIN
s->m = SymEngine::Rational::from_mpq(rational_class(a, b));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE rational_set(basic s, const basic a, const basic b)
{
if (not is_a_Integer(a) or not is_a_Integer(b)) {
return SYMENGINE_RUNTIME_ERROR;
}
s->m = SymEngine::Rational::from_two_ints(
*(rcp_static_cast<const Integer>(a->m)),
*(rcp_static_cast<const Integer>(b->m)));
return SYMENGINE_NO_EXCEPTION;
}
#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
CWRAPPER_OUTPUT_TYPE rational_get_mpq(mpq_t a, const basic s)
{
CWRAPPER_BEGIN
SYMENGINE_ASSERT(is_a<Rational>(*(s->m)));
mpq_set(a, get_mpq_t(
(down_cast<const Rational &>(*(s->m))).as_rational_class()));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE rational_set_mpq(basic s, const mpq_t i)
{
CWRAPPER_BEGIN
s->m = SymEngine::Rational::from_mpq(rational_class(i));
CWRAPPER_END
}
#endif
CWRAPPER_OUTPUT_TYPE complex_set(basic s, const basic re, const basic im)
{
CWRAPPER_BEGIN
s->m = SymEngine::Complex::from_two_nums(
*(rcp_static_cast<const Number>(re->m)),
*(rcp_static_cast<const Number>(im->m)));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE complex_set_rat(basic s, const basic re, const basic im)
{
CWRAPPER_BEGIN
s->m = SymEngine::Complex::from_two_rats(
*(rcp_static_cast<const Rational>(re->m)),
*(rcp_static_cast<const Rational>(im->m)));
CWRAPPER_END
}
#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
CWRAPPER_OUTPUT_TYPE complex_set_mpq(basic s, const mpq_t re, const mpq_t im)
{
CWRAPPER_BEGIN
s->m = SymEngine::Complex::from_mpq(rational_class(re), rational_class(im));
CWRAPPER_END
}
#endif
dcomplex complex_double_get(const basic s)
{
SYMENGINE_ASSERT(is_a<ComplexDouble>(*(s->m)));
dcomplex d;
d.real = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
.real();
d.imag = (down_cast<const ComplexDouble &>(*(s->m)).as_complex_double())
.imag();
return d;
}
char *basic_dumps(const basic s, unsigned long *size)
{
std::string str = s->m->dumps();
*size = str.length();
auto cc = new char[*size];
str.copy(cc, *size);
return cc;
}
CWRAPPER_OUTPUT_TYPE basic_loads(basic s, const char *c, unsigned long size)
{
CWRAPPER_BEGIN
std::string data(c, size);
s->m = Basic::loads(data);
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_diff(basic s, const basic expr, basic const symbol)
{
if (not is_a_Symbol(symbol))
return SYMENGINE_RUNTIME_ERROR;
CWRAPPER_BEGIN
s->m = expr->m->diff(rcp_static_cast<const Symbol>(symbol->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_assign(basic a, const basic b)
{
CWRAPPER_BEGIN
a->m = b->m;
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_parse(basic b, const char *str)
{
CWRAPPER_BEGIN
b->m = SymEngine::parse(str);
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_parse2(basic b, const char *str, int convert_xor)
{
CWRAPPER_BEGIN
if (convert_xor > 0) {
b->m = SymEngine::parse(str);
} else {
b->m = SymEngine::parse(str, false);
}
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_add(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = SymEngine::add(a->m, b->m);
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_sub(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = SymEngine::sub(a->m, b->m);
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_mul(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = SymEngine::mul(a->m, b->m);
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_pow(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = SymEngine::pow(a->m, b->m);
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_div(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = SymEngine::div(a->m, b->m);
CWRAPPER_END
}
int basic_eq(const basic a, const basic b)
{
return SymEngine::eq(*(a->m), *(b->m)) ? 1 : 0;
}
int basic_neq(const basic a, const basic b)
{
return SymEngine::neq(*(a->m), *(b->m)) ? 1 : 0;
}
#define IMPLEMENT_ONE_ARG_FUNC(func) \
CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a) \
{ \
CWRAPPER_BEGIN \
s->m = SymEngine::func(a->m); \
CWRAPPER_END \
}
IMPLEMENT_ONE_ARG_FUNC(expand)
IMPLEMENT_ONE_ARG_FUNC(neg)
IMPLEMENT_ONE_ARG_FUNC(abs)
IMPLEMENT_ONE_ARG_FUNC(erf)
IMPLEMENT_ONE_ARG_FUNC(erfc)
IMPLEMENT_ONE_ARG_FUNC(sin)
IMPLEMENT_ONE_ARG_FUNC(cos)
IMPLEMENT_ONE_ARG_FUNC(tan)
IMPLEMENT_ONE_ARG_FUNC(csc)
IMPLEMENT_ONE_ARG_FUNC(sec)
IMPLEMENT_ONE_ARG_FUNC(cot)
IMPLEMENT_ONE_ARG_FUNC(asin)
IMPLEMENT_ONE_ARG_FUNC(acos)
IMPLEMENT_ONE_ARG_FUNC(asec)
IMPLEMENT_ONE_ARG_FUNC(acsc)
IMPLEMENT_ONE_ARG_FUNC(atan)
IMPLEMENT_ONE_ARG_FUNC(acot)
IMPLEMENT_ONE_ARG_FUNC(sinh)
IMPLEMENT_ONE_ARG_FUNC(cosh)
IMPLEMENT_ONE_ARG_FUNC(tanh)
IMPLEMENT_ONE_ARG_FUNC(csch)
IMPLEMENT_ONE_ARG_FUNC(sech)
IMPLEMENT_ONE_ARG_FUNC(coth)
IMPLEMENT_ONE_ARG_FUNC(asinh)
IMPLEMENT_ONE_ARG_FUNC(acosh)
IMPLEMENT_ONE_ARG_FUNC(asech)
IMPLEMENT_ONE_ARG_FUNC(acsch)
IMPLEMENT_ONE_ARG_FUNC(atanh)
IMPLEMENT_ONE_ARG_FUNC(acoth)
IMPLEMENT_ONE_ARG_FUNC(lambertw)
IMPLEMENT_ONE_ARG_FUNC(zeta)
IMPLEMENT_ONE_ARG_FUNC(dirichlet_eta)
IMPLEMENT_ONE_ARG_FUNC(gamma)
IMPLEMENT_ONE_ARG_FUNC(loggamma)
IMPLEMENT_ONE_ARG_FUNC(sqrt)
IMPLEMENT_ONE_ARG_FUNC(cbrt)
IMPLEMENT_ONE_ARG_FUNC(exp)
IMPLEMENT_ONE_ARG_FUNC(log)
IMPLEMENT_ONE_ARG_FUNC(floor)
IMPLEMENT_ONE_ARG_FUNC(ceiling)
#define IMPLEMENT_TWO_ARG_FUNC(func) \
CWRAPPER_OUTPUT_TYPE basic_##func(basic s, const basic a, const basic b) \
{ \
CWRAPPER_BEGIN \
s->m = SymEngine::func(a->m, b->m); \
CWRAPPER_END \
}
IMPLEMENT_TWO_ARG_FUNC(atan2)
IMPLEMENT_TWO_ARG_FUNC(kronecker_delta)
IMPLEMENT_TWO_ARG_FUNC(lowergamma)
IMPLEMENT_TWO_ARG_FUNC(uppergamma)
IMPLEMENT_TWO_ARG_FUNC(beta)
IMPLEMENT_TWO_ARG_FUNC(polygamma)
#define IMPLEMENT_STR_CONVERSION(name, func) \
char *basic_##name(const basic s) \
{ \
std::string str; \
try { \
str = func(*s->m); \
} catch (SymEngineException & e) { \
return nullptr; \
} catch (...) { \
return nullptr; \
} \
auto cc = new char[str.length() + 1]; \
std::strcpy(cc, str.c_str()); \
return cc; \
}
IMPLEMENT_STR_CONVERSION(str, _str)
IMPLEMENT_STR_CONVERSION(str_julia, julia_str)
IMPLEMENT_STR_CONVERSION(str_mathml, mathml)
IMPLEMENT_STR_CONVERSION(str_latex, latex)
IMPLEMENT_STR_CONVERSION(str_ccode, ccode)
IMPLEMENT_STR_CONVERSION(str_jscode, jscode)
void basic_str_free(char *s)
{
delete[] s;
}
void bool_set_true(basic s)
{
s->m = SymEngine::boolTrue;
}
void bool_set_false(basic s)
{
s->m = SymEngine::boolFalse;
}
CWRAPPER_OUTPUT_TYPE basic_set_interval(basic s, const basic start,
const basic end, int left_open,
int right_open)
{
SYMENGINE_ASSERT(is_a_Number(*(start->m)));
SYMENGINE_ASSERT(is_a_Number(*(end->m)));
CWRAPPER_BEGIN
s->m = SymEngine::interval(rcp_static_cast<const Number>(start->m),
rcp_static_cast<const Number>(end->m),
(bool)left_open, (bool)right_open);
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_finiteset(basic s, const CSetBasic *container)
{
CWRAPPER_BEGIN
s->m = SymEngine::finiteset(container->m);
CWRAPPER_END
}
void basic_set_emptyset(basic s)
{
s->m = SymEngine::emptyset();
}
void basic_set_universalset(basic s)
{
s->m = SymEngine::emptyset();
}
void basic_set_complexes(basic s)
{
s->m = SymEngine::complexes();
}
void basic_set_reals(basic s)
{
s->m = SymEngine::reals();
}
void basic_set_rationals(basic s)
{
s->m = SymEngine::rationals();
}
void basic_set_integers(basic s)
{
s->m = SymEngine::integers();
}
CWRAPPER_OUTPUT_TYPE basic_set_union(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = rcp_static_cast<const Set>(a->m)->set_union(
rcp_static_cast<const Set>(b->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_intersection(basic s, const basic a,
const basic b)
{
CWRAPPER_BEGIN
s->m = rcp_static_cast<const Set>(a->m)->set_intersection(
rcp_static_cast<const Set>(b->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_complement(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = rcp_static_cast<const Set>(a->m)->set_complement(
rcp_static_cast<const Set>(b->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_contains(basic s, const basic a, const basic b)
{
CWRAPPER_BEGIN
s->m = rcp_static_cast<const Set>(a->m)->contains(b->m);
CWRAPPER_END
}
int basic_set_is_subset(const basic a, const basic b)
{
SYMENGINE_ASSERT(is_a_Set(*(a->m)));
SYMENGINE_ASSERT(is_a_Set(*(b->m)));
return rcp_static_cast<const Set>(a->m)->is_subset(
rcp_static_cast<const Set>(b->m));
}
int basic_set_is_proper_subset(const basic a, const basic b)
{
SYMENGINE_ASSERT(is_a_Set(*(a->m)));
SYMENGINE_ASSERT(is_a_Set(*(b->m)));
return rcp_static_cast<const Set>(a->m)->is_proper_subset(
rcp_static_cast<const Set>(b->m));
}
int basic_set_is_superset(const basic a, const basic b)
{
SYMENGINE_ASSERT(is_a_Set(*(a->m)));
SYMENGINE_ASSERT(is_a_Set(*(b->m)));
return rcp_static_cast<const Set>(a->m)->is_superset(
rcp_static_cast<const Set>(b->m));
}
int basic_set_is_proper_superset(const basic a, const basic b)
{
SYMENGINE_ASSERT(is_a_Set(*(a->m)));
SYMENGINE_ASSERT(is_a_Set(*(b->m)));
return rcp_static_cast<const Set>(a->m)->is_proper_superset(
rcp_static_cast<const Set>(b->m));
}
CWRAPPER_OUTPUT_TYPE basic_set_inf(basic s, const basic a)
{
CWRAPPER_BEGIN
s->m = SymEngine::inf(*rcp_static_cast<const Set>(a->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_sup(basic s, const basic a)
{
CWRAPPER_BEGIN
s->m = SymEngine::sup(*rcp_static_cast<const Set>(a->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_boundary(basic s, const basic a)
{
CWRAPPER_BEGIN
s->m = SymEngine::boundary(*rcp_static_cast<const Set>(a->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_interior(basic s, const basic a)
{
CWRAPPER_BEGIN
s->m = SymEngine::interior(*rcp_static_cast<const Set>(a->m));
CWRAPPER_END
}
CWRAPPER_OUTPUT_TYPE basic_set_closure(basic s, const basic a)
{
CWRAPPER_BEGIN
s->m = SymEngine::closure(*rcp_static_cast<const Set>(a->m));
CWRAPPER_END
}
int symengine_have_component(const char *c)
{
#ifdef HAVE_SYMENGINE_MPFR
if (std::strcmp("mpfr", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_MPC
if (std::strcmp("mpc", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_FLINT
if (std::strcmp("flint", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_ARB
if (std::strcmp("arb", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_ECM
if (std::strcmp("ecm", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_PRIMESIEVE
if (std::strcmp("primesieve", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_PIRANHA
if (std::strcmp("piranha", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_BOOST
if (std::strcmp("boost", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_PTHREAD
if (std::strcmp("pthread", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_LLVM
if (std::strcmp("llvm", c) == 0)
return 1;
#endif
#ifdef HAVE_SYMENGINE_LLVM_LONG_DOUBLE
if (std::strcmp("llvm_long_double", c) == 0)
return 1;
#endif
return 0;
}
int is_a_Number(const basic s)
{
return (int)is_a_Number(*(s->m));
}
int is_a_Integer(const basic c)
{
return is_a<Integer>(*(c->m));
}
int is_a_Rational(const basic c)
{
return is_a<Rational>(*(c->m));
}
int is_a_Symbol(const basic c)
{
return is_a<Symbol>(*(c->m));
}
int is_a_Complex(const basic c)
{
return is_a<Complex>(*(c->m));
}
int is_a_RealDouble(const basic c)
{
return is_a<RealDouble>(*(c->m));
}
int is_a_ComplexDouble(const basic c)
{
return is_a<ComplexDouble>(*(c->m));
}
int is_a_RealMPFR(const basic c)
{
#ifdef HAVE_SYMENGINE_MPFR
return is_a<RealMPFR>(*(c->m));
#else
return false;
#endif // HAVE_SYMENGINE_MPFR
}
int is_a_ComplexMPC(const basic c)
{
#ifdef HAVE_SYMENGINE_MPC
return is_a<ComplexMPC>(*(c->m));
#else
return false;
#endif // HAVE_SYMENGINE_MPC
}
int is_a_Set(const basic c)
{
return SymEngine::is_a_Set(*(c->m));
}
// C wrapper for std::vector<int>
struct CVectorInt {
std::vector<int> m;
};
CVectorInt *vectorint_new()
{
return new CVectorInt;
}
int vectorint_placement_new_check(void *data, size_t size)
{
CVectorInt *self = (CVectorInt *)data;
if (size < sizeof(CVectorInt))
return 1;
if (not SymEngine::is_aligned(self))
return 2;
return 0;
}
CVectorInt *vectorint_placement_new(void *data)
{
#if defined(WITH_SYMENGINE_ASSERT)
// if (size < sizeof(CVectorInt)) return 1; // Requires the 'size' argument
CVectorInt *self = (CVectorInt *)data;
SYMENGINE_ASSERT(SymEngine::is_aligned(self));
#endif
new (data) CVectorInt;
return (CVectorInt *)data;
}
void vectorint_placement_free(CVectorInt *self)
{
self->m.~vector<int>();
}
void vectorint_free(CVectorInt *self)
{
delete self;
}
void vectorint_push_back(CVectorInt *self, int value)
{
self->m.push_back(value);
}
int vectorint_get(CVectorInt *self, int n)
{
return self->m[n];
}
// C wrapper for vec_basic