]> www.ginac.de Git - ginac.git/blob - ginac/inifcns.cpp
- numeric::numeric(const char*): parse complex numbers.
[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     else 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 static ex csgn_series(const ex & x, const relational & rel, int order)
111 {
112     const ex x_pt = x.subs(rel);
113     if (x_pt.info(info_flags::numeric)) {
114         if (ex_to_numeric(x_pt).real().is_zero())
115             throw (std::domain_error("csgn_series(): on imaginary axis"));
116         epvector seq;
117         seq.push_back(expair(csgn(x_pt), _ex0()));
118         return pseries(rel,seq);
119     }
120     epvector seq;
121     seq.push_back(expair(csgn(x_pt), _ex0()));
122     return pseries(rel,seq);
123 }
124
125 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
126                         evalf_func(csgn_evalf).
127                         series_func(csgn_series));
128
129 //////////
130 // dilogarithm
131 //////////
132
133 static ex Li2_eval(const ex & x)
134 {
135     if (x.is_zero())
136         return x;
137     if (x.is_equal(_ex1()))
138         return power(Pi, _ex2()) / _ex6();
139     if (x.is_equal(_ex_1()))
140         return -power(Pi, _ex2()) / _ex12();
141     return Li2(x).hold();
142 }
143
144 REGISTER_FUNCTION(Li2, eval_func(Li2_eval));
145
146 //////////
147 // trilogarithm
148 //////////
149
150 static ex Li3_eval(const ex & x)
151 {
152     if (x.is_zero())
153         return x;
154     return Li3(x).hold();
155 }
156
157 REGISTER_FUNCTION(Li3, eval_func(Li3_eval));
158
159 //////////
160 // factorial
161 //////////
162
163 static ex factorial_evalf(const ex & x)
164 {
165     return factorial(x).hold();
166 }
167
168 static ex factorial_eval(const ex & x)
169 {
170     if (is_ex_exactly_of_type(x, numeric))
171         return factorial(ex_to_numeric(x));
172     else
173         return factorial(x).hold();
174 }
175
176 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
177                              evalf_func(factorial_evalf));
178
179 //////////
180 // binomial
181 //////////
182
183 static ex binomial_evalf(const ex & x, const ex & y)
184 {
185     return binomial(x, y).hold();
186 }
187
188 static ex binomial_eval(const ex & x, const ex &y)
189 {
190     if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
191         return binomial(ex_to_numeric(x), ex_to_numeric(y));
192     else
193         return binomial(x, y).hold();
194 }
195
196 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
197                             evalf_func(binomial_evalf));
198
199 //////////
200 // Order term function (for truncated power series)
201 //////////
202
203 static ex Order_eval(const ex & x)
204 {
205         if (is_ex_exactly_of_type(x, numeric)) {
206
207                 // O(c)=O(1)
208                 return Order(_ex1()).hold();
209
210         } else if (is_ex_exactly_of_type(x, mul)) {
211
212                 mul *m = static_cast<mul *>(x.bp);
213                 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
214
215                         // O(c*expr)=O(expr)
216                         return Order(x / m->op(m->nops() - 1)).hold();
217                 }
218         }
219         return Order(x).hold();
220 }
221
222 static ex Order_series(const ex & x, const relational & r, int order)
223 {
224         // Just wrap the function into a pseries object
225         epvector new_seq;
226     GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
227     const symbol *s = static_cast<symbol *>(r.lhs().bp);
228         new_seq.push_back(expair(Order(_ex1()), numeric(min(x.ldegree(*s), order))));
229         return pseries(r, new_seq);
230 }
231
232 // Differentiation is handled in function::derivative because of its special requirements
233
234 REGISTER_FUNCTION(Order, eval_func(Order_eval).
235                          series_func(Order_series));
236
237 //////////
238 // Inert partial differentiation operator
239 //////////
240
241 static ex Derivative_eval(const ex & f, const ex & l)
242 {
243         if (!is_ex_exactly_of_type(f, function)) {
244         throw(std::invalid_argument("Derivative(): 1st argument must be a function"));
245         }
246     if (!is_ex_exactly_of_type(l, lst)) {
247         throw(std::invalid_argument("Derivative(): 2nd argument must be a list"));
248     }
249         return Derivative(f, l).hold();
250 }
251
252 REGISTER_FUNCTION(Derivative, eval_func(Derivative_eval));
253
254 //////////
255 // Solve linear system
256 //////////
257
258 ex lsolve(const ex &eqns, const ex &symbols)
259 {
260     // solve a system of linear equations
261     if (eqns.info(info_flags::relation_equal)) {
262         if (!symbols.info(info_flags::symbol)) {
263             throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
264         }
265         ex sol=lsolve(lst(eqns),lst(symbols));
266         
267         GINAC_ASSERT(sol.nops()==1);
268         GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
269         
270         return sol.op(0).op(1); // return rhs of first solution
271     }
272     
273     // syntax checks
274     if (!eqns.info(info_flags::list)) {
275         throw(std::invalid_argument("lsolve: 1st argument must be a list"));
276     }
277     for (unsigned i=0; i<eqns.nops(); i++) {
278         if (!eqns.op(i).info(info_flags::relation_equal)) {
279             throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
280         }
281     }
282     if (!symbols.info(info_flags::list)) {
283         throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
284     }
285     for (unsigned i=0; i<symbols.nops(); i++) {
286         if (!symbols.op(i).info(info_flags::symbol)) {
287             throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
288         }
289     }
290     
291     // build matrix from equation system
292     matrix sys(eqns.nops(),symbols.nops());
293     matrix rhs(eqns.nops(),1);
294     matrix vars(symbols.nops(),1);
295     
296     for (unsigned r=0; r<eqns.nops(); r++) {
297         ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
298         ex linpart = eq;
299         for (unsigned c=0; c<symbols.nops(); c++) {
300             ex co = eq.coeff(ex_to_symbol(symbols.op(c)),1);
301             linpart -= co*symbols.op(c);
302             sys.set(r,c,co);
303         }
304         linpart=linpart.expand();
305         rhs.set(r,0,-linpart);
306     }
307     
308     // test if system is linear and fill vars matrix
309     for (unsigned i=0; i<symbols.nops(); i++) {
310         vars.set(i,0,symbols.op(i));
311         if (sys.has(symbols.op(i)))
312             throw(std::logic_error("lsolve: system is not linear"));
313         if (rhs.has(symbols.op(i)))
314             throw(std::logic_error("lsolve: system is not linear"));
315     }
316     
317     //matrix solution=sys.solve(rhs);
318     matrix solution;
319     try {
320         solution = sys.fraction_free_elim(vars,rhs);
321     } catch (const runtime_error & e) {
322         // probably singular matrix (or other error)
323         // return empty solution list
324         // cerr << e.what() << endl;
325         return lst();
326     }
327     
328     // return a list of equations
329     if (solution.cols()!=1) {
330         throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
331     }
332     if (solution.rows()!=symbols.nops()) {
333         cout << "symbols.nops()=" << symbols.nops() << endl;
334         cout << "solution.rows()=" << solution.rows() << endl;
335         throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
336     }
337     
338     // return list of the form lst(var1==sol1,var2==sol2,...)
339     lst sollist;
340     for (unsigned i=0; i<symbols.nops(); i++) {
341         sollist.append(symbols.op(i)==solution(i,0));
342     }
343     
344     return sollist;
345 }
346
347 /** non-commutative power. */
348 ex ncpower(const ex &basis, unsigned exponent)
349 {
350     if (exponent==0) {
351         return _ex1();
352     }
353
354     exvector v;
355     v.reserve(exponent);
356     for (unsigned i=0; i<exponent; ++i) {
357         v.push_back(basis);
358     }
359
360     return ncmul(v,1);
361 }
362
363 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
364  *  for static lib (so ginsh will see them). */
365 unsigned force_include_tgamma = function_index_tgamma;
366 unsigned force_include_zeta1 = function_index_zeta1;
367
368 #ifndef NO_NAMESPACE_GINAC
369 } // namespace GiNaC
370 #endif // ndef NO_NAMESPACE_GINAC