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