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