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