3 * Implementation of GiNaC's initially known functions. */
6 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
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.
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.
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
35 #include "relational.h"
40 #ifndef NO_NAMESPACE_GINAC
42 #endif // ndef NO_NAMESPACE_GINAC
48 static ex abs_evalf(const ex & x)
54 return abs(ex_to_numeric(x));
57 static ex abs_eval(const ex & x)
59 if (is_ex_exactly_of_type(x, numeric))
60 return abs(ex_to_numeric(x));
65 REGISTER_FUNCTION(abs, evalf_func(abs_eval).
66 evalf_func(abs_evalf));
72 static ex Li2_eval(const ex & x)
76 if (x.is_equal(_ex1()))
77 return power(Pi, _ex2()) / _ex6();
78 if (x.is_equal(_ex_1()))
79 return -power(Pi, _ex2()) / _ex12();
83 REGISTER_FUNCTION(Li2, eval_func(Li2_eval));
89 static ex Li3_eval(const ex & x)
96 REGISTER_FUNCTION(Li3, eval_func(Li3_eval));
102 static ex factorial_evalf(const ex & x)
104 return factorial(x).hold();
107 static ex factorial_eval(const ex & x)
109 if (is_ex_exactly_of_type(x, numeric))
110 return factorial(ex_to_numeric(x));
112 return factorial(x).hold();
115 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
116 evalf_func(factorial_evalf));
122 static ex binomial_evalf(const ex & x, const ex & y)
124 return binomial(x, y).hold();
127 static ex binomial_eval(const ex & x, const ex &y)
129 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
130 return binomial(ex_to_numeric(x), ex_to_numeric(y));
132 return binomial(x, y).hold();
135 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
136 evalf_func(binomial_evalf));
139 // Order term function (for truncated power series)
142 static ex Order_eval(const ex & x)
144 if (is_ex_exactly_of_type(x, numeric)) {
147 return Order(_ex1()).hold();
149 } else if (is_ex_exactly_of_type(x, mul)) {
151 mul *m = static_cast<mul *>(x.bp);
152 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
155 return Order(x / m->op(m->nops() - 1)).hold();
158 return Order(x).hold();
161 static ex Order_series(const ex & x, const symbol & s, const ex & point, int order)
163 // Just wrap the function into a pseries object
165 new_seq.push_back(expair(Order(_ex1()), numeric(min(x.ldegree(s), order))));
166 return pseries(s, point, new_seq);
169 REGISTER_FUNCTION(Order, eval_func(Order_eval).
170 series_func(Order_series));
173 // Solve linear system
176 ex lsolve(const ex &eqns, const ex &symbols)
178 // solve a system of linear equations
179 if (eqns.info(info_flags::relation_equal)) {
180 if (!symbols.info(info_flags::symbol)) {
181 throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
183 ex sol=lsolve(lst(eqns),lst(symbols));
185 GINAC_ASSERT(sol.nops()==1);
186 GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
188 return sol.op(0).op(1); // return rhs of first solution
192 if (!eqns.info(info_flags::list)) {
193 throw(std::invalid_argument("lsolve: 1st argument must be a list"));
195 for (unsigned i=0; i<eqns.nops(); i++) {
196 if (!eqns.op(i).info(info_flags::relation_equal)) {
197 throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
200 if (!symbols.info(info_flags::list)) {
201 throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
203 for (unsigned i=0; i<symbols.nops(); i++) {
204 if (!symbols.op(i).info(info_flags::symbol)) {
205 throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
209 // build matrix from equation system
210 matrix sys(eqns.nops(),symbols.nops());
211 matrix rhs(eqns.nops(),1);
212 matrix vars(symbols.nops(),1);
214 for (unsigned r=0; r<eqns.nops(); r++) {
215 ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
217 for (unsigned c=0; c<symbols.nops(); c++) {
218 ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
219 linpart -= co*symbols.op(c);
222 linpart=linpart.expand();
223 rhs.set(r,0,-linpart);
226 // test if system is linear and fill vars matrix
227 for (unsigned i=0; i<symbols.nops(); i++) {
228 vars.set(i,0,symbols.op(i));
229 if (sys.has(symbols.op(i))) {
230 throw(std::logic_error("lsolve: system is not linear"));
232 if (rhs.has(symbols.op(i))) {
233 throw(std::logic_error("lsolve: system is not linear"));
237 //matrix solution=sys.solve(rhs);
240 solution=sys.fraction_free_elim(vars,rhs);
241 } catch (const runtime_error & e) {
242 // probably singular matrix (or other error)
243 // return empty solution list
244 // cerr << e.what() << endl;
248 // return a list of equations
249 if (solution.cols()!=1) {
250 throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
252 if (solution.rows()!=symbols.nops()) {
253 cout << "symbols.nops()=" << symbols.nops() << endl;
254 cout << "solution.rows()=" << solution.rows() << endl;
255 throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
258 // return list of the form lst(var1==sol1,var2==sol2,...)
260 for (unsigned i=0; i<symbols.nops(); i++) {
261 sollist.append(symbols.op(i)==solution(i,0));
267 /** non-commutative power. */
268 ex ncpower(const ex &basis, unsigned exponent)
276 for (unsigned i=0; i<exponent; ++i) {
283 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
284 * for static lib (so ginsh will see them). */
285 unsigned force_include_gamma = function_index_gamma;
286 unsigned force_include_zeta1 = function_index_zeta1;
288 #ifndef NO_NAMESPACE_GINAC
290 #endif // ndef NO_NAMESPACE_GINAC