3 * Implementation of GiNaC's initially known functions. */
6 * GiNaC Copyright (C) 1999 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"
39 #ifndef NO_GINAC_NAMESPACE
41 #endif // ndef NO_GINAC_NAMESPACE
47 static ex Li2_eval(ex const & x)
51 if (x.is_equal(exONE()))
52 return power(Pi, 2) / 6;
53 if (x.is_equal(exMINUSONE()))
54 return -power(Pi, 2) / 12;
58 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
64 static ex Li3_eval(ex const & x)
71 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
77 static ex factorial_evalf(ex const & x)
79 return factorial(x).hold();
82 static ex factorial_eval(ex const & x)
84 if (is_ex_exactly_of_type(x, numeric))
85 return factorial(ex_to_numeric(x));
87 return factorial(x).hold();
90 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
96 static ex binomial_evalf(ex const & x, ex const & y)
98 return binomial(x, y).hold();
101 static ex binomial_eval(ex const & x, ex const &y)
103 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
104 return binomial(ex_to_numeric(x), ex_to_numeric(y));
106 return binomial(x, y).hold();
109 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
112 // Order term function (for truncated power series)
115 static ex Order_eval(ex const & x)
117 if (is_ex_exactly_of_type(x, numeric)) {
120 return Order(exONE()).hold();
122 } else if (is_ex_exactly_of_type(x, mul)) {
124 mul *m = static_cast<mul *>(x.bp);
125 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
128 return Order(x / m->op(m->nops() - 1)).hold();
131 return Order(x).hold();
134 static ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
136 // Just wrap the function into a series object
138 new_seq.push_back(expair(Order(exONE()), numeric(min(x.ldegree(s), order))));
139 return series(s, point, new_seq);
142 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
145 ex lsolve(ex const &eqns, ex const &symbols)
147 // solve a system of linear equations
148 if (eqns.info(info_flags::relation_equal)) {
149 if (!symbols.info(info_flags::symbol)) {
150 throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
152 ex sol=lsolve(lst(eqns),lst(symbols));
154 GINAC_ASSERT(sol.nops()==1);
155 GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
157 return sol.op(0).op(1); // return rhs of first solution
161 if (!eqns.info(info_flags::list)) {
162 throw(std::invalid_argument("lsolve: 1st argument must be a list"));
164 for (int i=0; i<eqns.nops(); i++) {
165 if (!eqns.op(i).info(info_flags::relation_equal)) {
166 throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
169 if (!symbols.info(info_flags::list)) {
170 throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
172 for (int i=0; i<symbols.nops(); i++) {
173 if (!symbols.op(i).info(info_flags::symbol)) {
174 throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
178 // build matrix from equation system
179 matrix sys(eqns.nops(),symbols.nops());
180 matrix rhs(eqns.nops(),1);
181 matrix vars(symbols.nops(),1);
183 for (int r=0; r<eqns.nops(); r++) {
184 ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
186 for (int c=0; c<symbols.nops(); c++) {
187 ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
188 linpart -= co*symbols.op(c);
191 linpart=linpart.expand();
192 rhs.set(r,0,-linpart);
195 // test if system is linear and fill vars matrix
196 for (int i=0; i<symbols.nops(); i++) {
197 vars.set(i,0,symbols.op(i));
198 if (sys.has(symbols.op(i))) {
199 throw(std::logic_error("lsolve: system is not linear"));
201 if (rhs.has(symbols.op(i))) {
202 throw(std::logic_error("lsolve: system is not linear"));
206 //matrix solution=sys.solve(rhs);
209 solution=sys.fraction_free_elim(vars,rhs);
210 } catch (runtime_error const & e) {
211 // probably singular matrix (or other error)
212 // return empty solution list
213 // cerr << e.what() << endl;
217 // return a list of equations
218 if (solution.cols()!=1) {
219 throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
221 if (solution.rows()!=symbols.nops()) {
222 cout << "symbols.nops()=" << symbols.nops() << endl;
223 cout << "solution.rows()=" << solution.rows() << endl;
224 throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
227 // return list of the form lst(var1==sol1,var2==sol2,...)
229 for (int i=0; i<symbols.nops(); i++) {
230 sollist.append(symbols.op(i)==solution(i,0));
236 /** non-commutative power. */
237 ex ncpower(ex const &basis, unsigned exponent)
245 for (unsigned i=0; i<exponent; ++i) {
252 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
253 * for static lib (so ginsh will see them). */
254 unsigned force_include_gamma = function_index_gamma;
255 unsigned force_include_zeta = function_index_zeta;
257 #ifndef NO_GINAC_NAMESPACE
259 #endif // ndef NO_GINAC_NAMESPACE