]> www.ginac.de Git - ginac.git/blob - ginac/inifcns.cpp
- enforced GiNaC coding standards :-)
[ginac.git] / ginac / inifcns.cpp
1 /** @file inifcns.cpp
2  *
3  *  Implementation of GiNaC's initially known functions.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <vector>
23 #include <stdexcept>
24
25 #include "ginac.h"
26
27 //////////
28 // dilogarithm
29 //////////
30
31 ex Li2_eval(ex const & x)
32 {
33     if (x.is_zero())
34         return x;
35     if (x.is_equal(exONE()))
36         return power(Pi, 2) / 6;
37     if (x.is_equal(exMINUSONE()))
38         return -power(Pi, 2) / 12;
39     return Li2(x).hold();
40 }
41
42 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
43
44 //////////
45 // trilogarithm
46 //////////
47
48 ex Li3_eval(ex const & x)
49 {
50     if (x.is_zero())
51         return x;
52     return Li3(x).hold();
53 }
54
55 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
56
57 //////////
58 // factorial
59 //////////
60
61 ex factorial_evalf(ex const & x)
62 {
63     return factorial(x).hold();
64 }
65
66 ex factorial_eval(ex const & x)
67 {
68     if (is_ex_exactly_of_type(x, numeric))
69         return factorial(ex_to_numeric(x));
70     else
71         return factorial(x).hold();
72 }
73
74 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
75
76 //////////
77 // binomial
78 //////////
79
80 ex binomial_evalf(ex const & x, ex const & y)
81 {
82     return binomial(x, y).hold();
83 }
84
85 ex binomial_eval(ex const & x, ex const &y)
86 {
87     if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
88         return binomial(ex_to_numeric(x), ex_to_numeric(y));
89     else
90         return binomial(x, y).hold();
91 }
92
93 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
94
95 //////////
96 // Order term function (for truncated power series)
97 //////////
98
99 ex Order_eval(ex const & x)
100 {
101         if (is_ex_exactly_of_type(x, numeric)) {
102
103                 // O(c)=O(1)
104                 return Order(exONE()).hold();
105
106         } else if (is_ex_exactly_of_type(x, mul)) {
107
108                 mul *m = static_cast<mul *>(x.bp);
109                 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
110
111                         // O(c*expr)=O(expr)
112                         return Order(x / m->op(m->nops() - 1)).hold();
113                 }
114         }
115         return Order(x).hold();
116 }
117
118 ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
119 {
120         // Just wrap the function into a series object
121         epvector new_seq;
122         new_seq.push_back(expair(Order(exONE()), numeric(min(x.ldegree(s), order))));
123         return series(s, point, new_seq);
124 }
125
126 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
127
128 /** linear solve. */
129 ex lsolve(ex eqns, ex symbols)
130 {
131     // solve a system of linear equations
132     if (eqns.info(info_flags::relation_equal)) {
133         if (!symbols.info(info_flags::symbol)) {
134             throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
135         }
136         ex sol=lsolve(lst(eqns),lst(symbols));
137         
138         ASSERT(sol.nops()==1);
139         ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
140         
141         return sol.op(0).op(1); // return rhs of first solution
142     }
143     
144     // syntax checks
145     if (!eqns.info(info_flags::list)) {
146         throw(std::invalid_argument("lsolve: 1st argument must be a list"));
147     }
148     for (int i=0; i<eqns.nops(); i++) {
149         if (!eqns.op(i).info(info_flags::relation_equal)) {
150             throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
151         }
152     }
153     if (!symbols.info(info_flags::list)) {
154         throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
155     }
156     for (int i=0; i<symbols.nops(); i++) {
157         if (!symbols.op(i).info(info_flags::symbol)) {
158             throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
159         }
160     }
161     
162     // build matrix from equation system
163     matrix sys(eqns.nops(),symbols.nops());
164     matrix rhs(eqns.nops(),1);
165     matrix vars(symbols.nops(),1);
166
167     for (int r=0; r<eqns.nops(); r++) {
168         ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
169         ex linpart=eq;
170         for (int c=0; c<symbols.nops(); c++) {
171             ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
172             linpart -= co*symbols.op(c);
173             sys.set(r,c,co);
174         }
175         linpart=linpart.expand();
176         rhs.set(r,0,-linpart);
177     }
178     
179     // test if system is linear and fill vars matrix
180     for (int i=0; i<symbols.nops(); i++) {
181         vars.set(i,0,symbols.op(i));
182         if (sys.has(symbols.op(i))) {
183             throw(std::logic_error("lsolve: system is not linear"));
184         }
185         if (rhs.has(symbols.op(i))) {
186             throw(std::logic_error("lsolve: system is not linear"));
187         }
188     }
189     
190     //matrix solution=sys.solve(rhs);
191     matrix solution;
192     try {
193         solution=sys.fraction_free_elim(vars,rhs);
194     } catch (runtime_error const & e) {
195         // probably singular matrix (or other error)
196         // return empty solution list
197         cerr << e.what() << endl;
198         return lst();
199     }
200     
201     // return a list of equations
202     if (solution.cols()!=1) {
203         throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
204     }
205     if (solution.rows()!=symbols.nops()) {
206         cout << "symbols.nops()=" << symbols.nops() << endl;
207         cout << "solution.rows()=" << solution.rows() << endl;
208         throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
209     }
210     
211     // return list of the form lst(var1==sol1,var2==sol2,...)
212     lst sollist;
213     for (int i=0; i<symbols.nops(); i++) {
214         sollist.append(symbols.op(i)==solution(i,0));
215     }
216     
217     return sollist;
218 }
219
220 /** non-commutative power. */
221 ex ncpower(ex basis, unsigned exponent)
222 {
223     if (exponent==0) {
224         return exONE();
225     }
226
227     exvector v;
228     v.reserve(exponent);
229     for (unsigned i=0; i<exponent; ++i) {
230         v.push_back(basis);
231     }
232
233     return ncmul(v,1);
234 }
235