]> www.ginac.de Git - ginac.git/blob - ginac/inifcns.cpp
- Overwritten: pseries::printtree().
[ginac.git] / ginac / inifcns.cpp
1 /** @file inifcns.cpp
2  *
3  *  Implementation of GiNaC's initially known functions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <vector>
24 #include <stdexcept>
25
26 #include "inifcns.h"
27 #include "ex.h"
28 #include "constant.h"
29 #include "lst.h"
30 #include "matrix.h"
31 #include "mul.h"
32 #include "ncmul.h"
33 #include "numeric.h"
34 #include "power.h"
35 #include "relational.h"
36 #include "pseries.h"
37 #include "symbol.h"
38 #include "utils.h"
39
40 #ifndef NO_NAMESPACE_GINAC
41 namespace GiNaC {
42 #endif // ndef NO_NAMESPACE_GINAC
43
44 //////////
45 // absolute value
46 //////////
47
48 static ex abs_evalf(const ex & x)
49 {
50     BEGIN_TYPECHECK
51         TYPECHECK(x,numeric)
52     END_TYPECHECK(abs(x))
53     
54     return abs(ex_to_numeric(x));
55 }
56
57 static ex abs_eval(const ex & x)
58 {
59     if (is_ex_exactly_of_type(x, numeric))
60         return abs(ex_to_numeric(x));
61     else
62         return abs(x).hold();
63 }
64
65 REGISTER_FUNCTION(abs, eval_func(abs_eval).
66                        evalf_func(abs_evalf));
67
68
69 //////////
70 // Complex sign
71 //////////
72
73 static ex csgn_evalf(const ex & x)
74 {
75     BEGIN_TYPECHECK
76         TYPECHECK(x,numeric)
77     END_TYPECHECK(csgn(x))
78     
79     return csgn(ex_to_numeric(x));
80 }
81
82 static ex csgn_eval(const ex & x)
83 {
84     if (is_ex_exactly_of_type(x, numeric))
85         return csgn(ex_to_numeric(x));
86     
87     if (is_ex_exactly_of_type(x, mul)) {
88         numeric oc = ex_to_numeric(x.op(x.nops()-1));
89         if (oc.is_real()) {
90             if (oc > 0)
91                 // csgn(42*x) -> csgn(x)
92                 return csgn(x/oc).hold();
93             else
94                 // csgn(-42*x) -> -csgn(x)
95                 return -csgn(x/oc).hold();
96         }
97         if (oc.real().is_zero()) {
98             if (oc.imag() > 0)
99                 // csgn(42*I*x) -> csgn(I*x)
100                 return csgn(I*x/oc).hold();
101             else
102                 // csgn(-42*I*x) -> -csgn(I*x)
103                 return -csgn(I*x/oc).hold();
104         }
105     }
106     
107     return csgn(x).hold();
108 }
109
110 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
111                         evalf_func(csgn_evalf));
112
113 //////////
114 // dilogarithm
115 //////////
116
117 static ex Li2_eval(const ex & x)
118 {
119     if (x.is_zero())
120         return x;
121     if (x.is_equal(_ex1()))
122         return power(Pi, _ex2()) / _ex6();
123     if (x.is_equal(_ex_1()))
124         return -power(Pi, _ex2()) / _ex12();
125     return Li2(x).hold();
126 }
127
128 REGISTER_FUNCTION(Li2, eval_func(Li2_eval));
129
130 //////////
131 // trilogarithm
132 //////////
133
134 static ex Li3_eval(const ex & x)
135 {
136     if (x.is_zero())
137         return x;
138     return Li3(x).hold();
139 }
140
141 REGISTER_FUNCTION(Li3, eval_func(Li3_eval));
142
143 //////////
144 // factorial
145 //////////
146
147 static ex factorial_evalf(const ex & x)
148 {
149     return factorial(x).hold();
150 }
151
152 static ex factorial_eval(const ex & x)
153 {
154     if (is_ex_exactly_of_type(x, numeric))
155         return factorial(ex_to_numeric(x));
156     else
157         return factorial(x).hold();
158 }
159
160 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
161                              evalf_func(factorial_evalf));
162
163 //////////
164 // binomial
165 //////////
166
167 static ex binomial_evalf(const ex & x, const ex & y)
168 {
169     return binomial(x, y).hold();
170 }
171
172 static ex binomial_eval(const ex & x, const ex &y)
173 {
174     if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
175         return binomial(ex_to_numeric(x), ex_to_numeric(y));
176     else
177         return binomial(x, y).hold();
178 }
179
180 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
181                             evalf_func(binomial_evalf));
182
183 //////////
184 // Order term function (for truncated power series)
185 //////////
186
187 static ex Order_eval(const ex & x)
188 {
189         if (is_ex_exactly_of_type(x, numeric)) {
190
191                 // O(c)=O(1)
192                 return Order(_ex1()).hold();
193
194         } else if (is_ex_exactly_of_type(x, mul)) {
195
196                 mul *m = static_cast<mul *>(x.bp);
197                 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
198
199                         // O(c*expr)=O(expr)
200                         return Order(x / m->op(m->nops() - 1)).hold();
201                 }
202         }
203         return Order(x).hold();
204 }
205
206 static ex Order_series(const ex & x, const relational & r, int order)
207 {
208         // Just wrap the function into a pseries object
209         epvector new_seq;
210     GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
211     const symbol *s = static_cast<symbol *>(r.lhs().bp);
212         new_seq.push_back(expair(Order(_ex1()), numeric(min(x.ldegree(*s), order))));
213         return pseries(r, new_seq);
214 }
215
216 // Differentiation is handled in function::derivative because of its special requirements
217
218 REGISTER_FUNCTION(Order, eval_func(Order_eval).
219                          series_func(Order_series));
220
221 //////////
222 // Inert partial differentiation operator
223 //////////
224
225 static ex Derivative_eval(const ex & f, const ex & l)
226 {
227         if (!is_ex_exactly_of_type(f, function)) {
228         throw(std::invalid_argument("Derivative(): 1st argument must be a function"));
229         }
230     if (!is_ex_exactly_of_type(l, lst)) {
231         throw(std::invalid_argument("Derivative(): 2nd argument must be a list"));
232     }
233         return Derivative(f, l).hold();
234 }
235
236 REGISTER_FUNCTION(Derivative, eval_func(Derivative_eval));
237
238 //////////
239 // Solve linear system
240 //////////
241
242 ex lsolve(const ex &eqns, const ex &symbols)
243 {
244     // solve a system of linear equations
245     if (eqns.info(info_flags::relation_equal)) {
246         if (!symbols.info(info_flags::symbol)) {
247             throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
248         }
249         ex sol=lsolve(lst(eqns),lst(symbols));
250         
251         GINAC_ASSERT(sol.nops()==1);
252         GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
253         
254         return sol.op(0).op(1); // return rhs of first solution
255     }
256     
257     // syntax checks
258     if (!eqns.info(info_flags::list)) {
259         throw(std::invalid_argument("lsolve: 1st argument must be a list"));
260     }
261     for (unsigned i=0; i<eqns.nops(); i++) {
262         if (!eqns.op(i).info(info_flags::relation_equal)) {
263             throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
264         }
265     }
266     if (!symbols.info(info_flags::list)) {
267         throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
268     }
269     for (unsigned i=0; i<symbols.nops(); i++) {
270         if (!symbols.op(i).info(info_flags::symbol)) {
271             throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
272         }
273     }
274     
275     // build matrix from equation system
276     matrix sys(eqns.nops(),symbols.nops());
277     matrix rhs(eqns.nops(),1);
278     matrix vars(symbols.nops(),1);
279     
280     for (unsigned r=0; r<eqns.nops(); r++) {
281         ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
282         ex linpart = eq;
283         for (unsigned c=0; c<symbols.nops(); c++) {
284             ex co = eq.coeff(ex_to_symbol(symbols.op(c)),1);
285             linpart -= co*symbols.op(c);
286             sys.set(r,c,co);
287         }
288         linpart=linpart.expand();
289         rhs.set(r,0,-linpart);
290     }
291     
292     // test if system is linear and fill vars matrix
293     for (unsigned i=0; i<symbols.nops(); i++) {
294         vars.set(i,0,symbols.op(i));
295         if (sys.has(symbols.op(i)))
296             throw(std::logic_error("lsolve: system is not linear"));
297         if (rhs.has(symbols.op(i)))
298             throw(std::logic_error("lsolve: system is not linear"));
299     }
300     
301     //matrix solution=sys.solve(rhs);
302     matrix solution;
303     try {
304         solution = sys.fraction_free_elim(vars,rhs);
305     } catch (const runtime_error & e) {
306         // probably singular matrix (or other error)
307         // return empty solution list
308         // cerr << e.what() << endl;
309         return lst();
310     }
311     
312     // return a list of equations
313     if (solution.cols()!=1) {
314         throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
315     }
316     if (solution.rows()!=symbols.nops()) {
317         cout << "symbols.nops()=" << symbols.nops() << endl;
318         cout << "solution.rows()=" << solution.rows() << endl;
319         throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
320     }
321     
322     // return list of the form lst(var1==sol1,var2==sol2,...)
323     lst sollist;
324     for (unsigned i=0; i<symbols.nops(); i++) {
325         sollist.append(symbols.op(i)==solution(i,0));
326     }
327     
328     return sollist;
329 }
330
331 /** non-commutative power. */
332 ex ncpower(const ex &basis, unsigned exponent)
333 {
334     if (exponent==0) {
335         return _ex1();
336     }
337
338     exvector v;
339     v.reserve(exponent);
340     for (unsigned i=0; i<exponent; ++i) {
341         v.push_back(basis);
342     }
343
344     return ncmul(v,1);
345 }
346
347 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
348  *  for static lib (so ginsh will see them). */
349 unsigned force_include_tgamma = function_index_tgamma;
350 unsigned force_include_zeta1 = function_index_zeta1;
351
352 #ifndef NO_NAMESPACE_GINAC
353 } // namespace GiNaC
354 #endif // ndef NO_NAMESPACE_GINAC