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