]> www.ginac.de Git - ginac.git/blob - ginac/inifcns.cpp
- removed inert Diff() function; only Derivative() remains
[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 // dilogarithm
70 //////////
71
72 static ex Li2_eval(const ex & x)
73 {
74     if (x.is_zero())
75         return x;
76     if (x.is_equal(_ex1()))
77         return power(Pi, _ex2()) / _ex6();
78     if (x.is_equal(_ex_1()))
79         return -power(Pi, _ex2()) / _ex12();
80     return Li2(x).hold();
81 }
82
83 REGISTER_FUNCTION(Li2, eval_func(Li2_eval));
84
85 //////////
86 // trilogarithm
87 //////////
88
89 static ex Li3_eval(const ex & x)
90 {
91     if (x.is_zero())
92         return x;
93     return Li3(x).hold();
94 }
95
96 REGISTER_FUNCTION(Li3, eval_func(Li3_eval));
97
98 //////////
99 // factorial
100 //////////
101
102 static ex factorial_evalf(const ex & x)
103 {
104     return factorial(x).hold();
105 }
106
107 static ex factorial_eval(const ex & x)
108 {
109     if (is_ex_exactly_of_type(x, numeric))
110         return factorial(ex_to_numeric(x));
111     else
112         return factorial(x).hold();
113 }
114
115 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
116                              evalf_func(factorial_evalf));
117
118 //////////
119 // binomial
120 //////////
121
122 static ex binomial_evalf(const ex & x, const ex & y)
123 {
124     return binomial(x, y).hold();
125 }
126
127 static ex binomial_eval(const ex & x, const ex &y)
128 {
129     if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
130         return binomial(ex_to_numeric(x), ex_to_numeric(y));
131     else
132         return binomial(x, y).hold();
133 }
134
135 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
136                             evalf_func(binomial_evalf));
137
138 //////////
139 // Order term function (for truncated power series)
140 //////////
141
142 static ex Order_eval(const ex & x)
143 {
144         if (is_ex_exactly_of_type(x, numeric)) {
145
146                 // O(c)=O(1)
147                 return Order(_ex1()).hold();
148
149         } else if (is_ex_exactly_of_type(x, mul)) {
150
151                 mul *m = static_cast<mul *>(x.bp);
152                 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
153
154                         // O(c*expr)=O(expr)
155                         return Order(x / m->op(m->nops() - 1)).hold();
156                 }
157         }
158         return Order(x).hold();
159 }
160
161 static ex Order_series(const ex & x, const symbol & s, const ex & point, int order)
162 {
163         // Just wrap the function into a pseries object
164         epvector new_seq;
165         new_seq.push_back(expair(Order(_ex1()), numeric(min(x.ldegree(s), order))));
166         return pseries(s, point, new_seq);
167 }
168
169 // Differentiation is handled in function::derivative because of its special requirements
170
171 REGISTER_FUNCTION(Order, eval_func(Order_eval).
172                          series_func(Order_series));
173
174 //////////
175 // Inert partial differentiation operator
176 //////////
177
178 static ex Derivative_eval(const ex & f, const ex & l)
179 {
180         if (!is_ex_exactly_of_type(f, function)) {
181         throw(std::invalid_argument("Derivative(): 1st argument must be a function"));
182         }
183     if (!is_ex_exactly_of_type(l, lst)) {
184         throw(std::invalid_argument("Derivative(): 2nd argument must be a list"));
185     }
186         return Derivative(f, l).hold();
187 }
188
189 REGISTER_FUNCTION(Derivative, eval_func(Derivative_eval));
190
191 //////////
192 // Solve linear system
193 //////////
194
195 ex lsolve(const ex &eqns, const ex &symbols)
196 {
197     // solve a system of linear equations
198     if (eqns.info(info_flags::relation_equal)) {
199         if (!symbols.info(info_flags::symbol)) {
200             throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
201         }
202         ex sol=lsolve(lst(eqns),lst(symbols));
203         
204         GINAC_ASSERT(sol.nops()==1);
205         GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
206         
207         return sol.op(0).op(1); // return rhs of first solution
208     }
209     
210     // syntax checks
211     if (!eqns.info(info_flags::list)) {
212         throw(std::invalid_argument("lsolve: 1st argument must be a list"));
213     }
214     for (unsigned i=0; i<eqns.nops(); i++) {
215         if (!eqns.op(i).info(info_flags::relation_equal)) {
216             throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
217         }
218     }
219     if (!symbols.info(info_flags::list)) {
220         throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
221     }
222     for (unsigned i=0; i<symbols.nops(); i++) {
223         if (!symbols.op(i).info(info_flags::symbol)) {
224             throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
225         }
226     }
227     
228     // build matrix from equation system
229     matrix sys(eqns.nops(),symbols.nops());
230     matrix rhs(eqns.nops(),1);
231     matrix vars(symbols.nops(),1);
232     
233     for (unsigned r=0; r<eqns.nops(); r++) {
234         ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
235         ex linpart = eq;
236         for (unsigned c=0; c<symbols.nops(); c++) {
237             ex co = eq.coeff(ex_to_symbol(symbols.op(c)),1);
238             linpart -= co*symbols.op(c);
239             sys.set(r,c,co);
240         }
241         linpart=linpart.expand();
242         rhs.set(r,0,-linpart);
243     }
244     
245     // test if system is linear and fill vars matrix
246     for (unsigned i=0; i<symbols.nops(); i++) {
247         vars.set(i,0,symbols.op(i));
248         if (sys.has(symbols.op(i)))
249             throw(std::logic_error("lsolve: system is not linear"));
250         if (rhs.has(symbols.op(i)))
251             throw(std::logic_error("lsolve: system is not linear"));
252     }
253     
254     //matrix solution=sys.solve(rhs);
255     matrix solution;
256     try {
257         solution = sys.fraction_free_elim(vars,rhs);
258     } catch (const runtime_error & e) {
259         // probably singular matrix (or other error)
260         // return empty solution list
261         // cerr << e.what() << endl;
262         return lst();
263     }
264     
265     // return a list of equations
266     if (solution.cols()!=1) {
267         throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
268     }
269     if (solution.rows()!=symbols.nops()) {
270         cout << "symbols.nops()=" << symbols.nops() << endl;
271         cout << "solution.rows()=" << solution.rows() << endl;
272         throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
273     }
274     
275     // return list of the form lst(var1==sol1,var2==sol2,...)
276     lst sollist;
277     for (unsigned i=0; i<symbols.nops(); i++) {
278         sollist.append(symbols.op(i)==solution(i,0));
279     }
280     
281     return sollist;
282 }
283
284 /** non-commutative power. */
285 ex ncpower(const ex &basis, unsigned exponent)
286 {
287     if (exponent==0) {
288         return _ex1();
289     }
290
291     exvector v;
292     v.reserve(exponent);
293     for (unsigned i=0; i<exponent; ++i) {
294         v.push_back(basis);
295     }
296
297     return ncmul(v,1);
298 }
299
300 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
301  *  for static lib (so ginsh will see them). */
302 unsigned force_include_gamma = function_index_Gamma;
303 unsigned force_include_zeta1 = function_index_zeta1;
304
305 #ifndef NO_NAMESPACE_GINAC
306 } // namespace GiNaC
307 #endif // ndef NO_NAMESPACE_GINAC