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_GINAC_NAMESPACE
42 #endif // ndef NO_GINAC_NAMESPACE
48 static ex abs_evalf(ex const & x)
54 return abs(ex_to_numeric(x));
57 static ex abs_eval(ex const & x)
59 if (is_ex_exactly_of_type(x, numeric))
60 return abs(ex_to_numeric(x));
65 REGISTER_FUNCTION(abs, abs_eval, abs_evalf, NULL, NULL);
71 static ex Li2_eval(ex const & x)
75 if (x.is_equal(_ex1()))
76 return power(Pi, _ex2()) / _ex6();
77 if (x.is_equal(_ex_1()))
78 return -power(Pi, _ex2()) / _ex12();
82 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
88 static ex Li3_eval(ex const & x)
95 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
101 static ex factorial_evalf(ex const & x)
103 return factorial(x).hold();
106 static ex factorial_eval(ex const & x)
108 if (is_ex_exactly_of_type(x, numeric))
109 return factorial(ex_to_numeric(x));
111 return factorial(x).hold();
114 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
120 static ex binomial_evalf(ex const & x, ex const & y)
122 return binomial(x, y).hold();
125 static ex binomial_eval(ex const & x, ex const &y)
127 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
128 return binomial(ex_to_numeric(x), ex_to_numeric(y));
130 return binomial(x, y).hold();
133 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
136 // Order term function (for truncated power series)
139 static ex Order_eval(ex const & x)
141 if (is_ex_exactly_of_type(x, numeric)) {
144 return Order(_ex1()).hold();
146 } else if (is_ex_exactly_of_type(x, mul)) {
148 mul *m = static_cast<mul *>(x.bp);
149 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
152 return Order(x / m->op(m->nops() - 1)).hold();
155 return Order(x).hold();
158 static ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
160 // Just wrap the function into a series object
162 new_seq.push_back(expair(Order(_ex1()), numeric(min(x.ldegree(s), order))));
163 return series(s, point, new_seq);
166 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
169 // Solve linear system
172 ex lsolve(ex const &eqns, ex const &symbols)
174 // solve a system of linear equations
175 if (eqns.info(info_flags::relation_equal)) {
176 if (!symbols.info(info_flags::symbol)) {
177 throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
179 ex sol=lsolve(lst(eqns),lst(symbols));
181 GINAC_ASSERT(sol.nops()==1);
182 GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
184 return sol.op(0).op(1); // return rhs of first solution
188 if (!eqns.info(info_flags::list)) {
189 throw(std::invalid_argument("lsolve: 1st argument must be a list"));
191 for (unsigned i=0; i<eqns.nops(); i++) {
192 if (!eqns.op(i).info(info_flags::relation_equal)) {
193 throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
196 if (!symbols.info(info_flags::list)) {
197 throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
199 for (unsigned i=0; i<symbols.nops(); i++) {
200 if (!symbols.op(i).info(info_flags::symbol)) {
201 throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
205 // build matrix from equation system
206 matrix sys(eqns.nops(),symbols.nops());
207 matrix rhs(eqns.nops(),1);
208 matrix vars(symbols.nops(),1);
210 for (unsigned r=0; r<eqns.nops(); r++) {
211 ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
213 for (unsigned c=0; c<symbols.nops(); c++) {
214 ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
215 linpart -= co*symbols.op(c);
218 linpart=linpart.expand();
219 rhs.set(r,0,-linpart);
222 // test if system is linear and fill vars matrix
223 for (unsigned i=0; i<symbols.nops(); i++) {
224 vars.set(i,0,symbols.op(i));
225 if (sys.has(symbols.op(i))) {
226 throw(std::logic_error("lsolve: system is not linear"));
228 if (rhs.has(symbols.op(i))) {
229 throw(std::logic_error("lsolve: system is not linear"));
233 //matrix solution=sys.solve(rhs);
236 solution=sys.fraction_free_elim(vars,rhs);
237 } catch (runtime_error const & e) {
238 // probably singular matrix (or other error)
239 // return empty solution list
240 // cerr << e.what() << endl;
244 // return a list of equations
245 if (solution.cols()!=1) {
246 throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
248 if (solution.rows()!=symbols.nops()) {
249 cout << "symbols.nops()=" << symbols.nops() << endl;
250 cout << "solution.rows()=" << solution.rows() << endl;
251 throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
254 // return list of the form lst(var1==sol1,var2==sol2,...)
256 for (unsigned i=0; i<symbols.nops(); i++) {
257 sollist.append(symbols.op(i)==solution(i,0));
263 /** non-commutative power. */
264 ex ncpower(ex const &basis, unsigned exponent)
272 for (unsigned i=0; i<exponent; ++i) {
279 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
280 * for static lib (so ginsh will see them). */
281 unsigned force_include_gamma = function_index_gamma;
282 unsigned force_include_zeta1 = function_index_zeta1;
284 #ifndef NO_GINAC_NAMESPACE
286 #endif // ndef NO_GINAC_NAMESPACE