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