Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/c_lib/include/ginac_wrap.h
4045 views
1
/*******************************************************************************
2
SAGE: Open Source Mathematical Software
3
Copyright (C) 2008 William Stein <[email protected]>
4
Copyright (C) 2008-2009 Burcin Erocal <[email protected]>
5
Distributed under the terms of the GNU General Public License (GPL),
6
version 2 or any later version. The full text of the GPL is available at:
7
http://www.gnu.org/licenses/
8
*******************************************************************************/
9
10
#include "ccobject.h"
11
#include <pynac/ginac.h>
12
#include <string>
13
14
using namespace GiNaC;
15
16
void list_symbols(const ex& e, std::set<ex, ex_is_less> &s)
17
{
18
if (is_a<symbol>(e)) {
19
s.insert(e);
20
} else {
21
for (size_t i=0; i<e.nops(); i++)
22
list_symbols(e.op(i), s);
23
}
24
}
25
26
27
ex g_function_evalv(unsigned serial, exvector& vec, bool hold)
28
{
29
if (hold)
30
return function(serial, vec).hold();
31
return function(serial, vec);
32
}
33
34
ex g_function_eval0(unsigned serial, bool hold)
35
{
36
if (hold)
37
return function(serial).hold();
38
return function(serial);
39
}
40
41
ex g_function_eval1(unsigned serial, const ex& arg1, bool hold)
42
{
43
if (hold)
44
return function(serial, arg1).hold();
45
return function(serial, arg1);
46
}
47
48
ex g_function_eval2(unsigned serial, const ex& arg1, const ex& arg2, bool hold)
49
{
50
if (hold)
51
return function(serial, arg1, arg2).hold();
52
return function(serial, arg1, arg2);
53
}
54
55
ex g_function_eval3(unsigned serial, const ex& arg1, const ex& arg2,
56
const ex& arg3, bool hold)
57
{
58
if (hold)
59
return function(serial, arg1, arg2, arg3).hold();
60
return function(serial, arg1, arg2, arg3);
61
}
62
63
64
bool relational_to_bool(const ex& e) {
65
if (ex_to<relational>(e))
66
return 1;
67
else
68
return 0;
69
}
70
71
bool g_is_a_terminating_series(const ex& e) {
72
if (is_a<pseries>(e)) {
73
return (ex_to<pseries>(e)).is_terminating();
74
}
75
return false;
76
}
77
78
relational::operators relational_operator(const ex& e) {
79
// unsafe cast -- be damn sure the input is a relational.
80
return (ex_to<relational>(e)).the_operator();
81
}
82
83
relational::operators switch_operator(relational::operators o) {
84
switch(o) {
85
case relational::less:
86
return relational::greater;
87
case relational::less_or_equal:
88
return relational::greater_or_equal;
89
case relational::greater:
90
return relational::less;
91
case relational::greater_or_equal:
92
return relational::less_or_equal;
93
default:
94
return o;
95
}
96
}
97
98
bool is_negative(ex x) {
99
if (is_a<numeric>(x)) {
100
return (ex_to<numeric>(x)).is_negative();
101
}
102
return false;
103
}
104
105
PyObject* py_object_from_numeric(ex x) {
106
return (ex_to<numeric>(x)).to_pyobject();
107
}
108
109
template <class T>
110
PyObject* _to_PyString_latex(const T *x)
111
{
112
std::ostringstream instore;
113
instore << latex << (*x);
114
return PyString_FromString(instore.str().data());
115
}
116
117
constant* GConstant_construct(void* mem, char* name, char* texname, unsigned domain)
118
{
119
return new(mem) constant(name, ConstantEvalf, texname, domain);
120
}
121
122
#define ASSIGN_WRAP(x,y) x = y
123
124
#define ADD_WRAP(x,y) (x)+(y)
125
#define SUB_WRAP(x,y) (x)-(y)
126
#define MUL_WRAP(x,y) (x)*(y)
127
#define DIV_WRAP(x,y) (x)/(y)
128
129
#define LT_WRAP(x,y) (x)<(y)
130
#define EQ_WRAP(x,y) (x)==(y)
131
#define GT_WRAP(x,y) (x)>(y)
132
#define LE_WRAP(x,y) (x)<=(y)
133
#define NE_WRAP(x,y) (x)!=(y)
134
#define GE_WRAP(x,y) (x)>=(y)
135
136
#define HOLD(fn, x, hold_flag) hold_flag ? ex(fn(x).hold()) : fn(x)
137
138
#define HOLD2(fn, x, y, hold_flag) hold_flag ? ex(fn(x,y).hold()) : fn(x,y)
139
140
// declare the constant 1/2 so we can use it for a custom square root function
141
namespace GiNaC {
142
143
extern const ex _ex1_2;
144
145
}
146
147