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"
40 #ifndef NO_GINAC_NAMESPACE
42 #endif // ndef NO_GINAC_NAMESPACE
48 static ex Li2_eval(ex const & x)
52 if (x.is_equal(_ex1()))
53 return power(Pi, 2) / 6;
54 if (x.is_equal(_ex_1()))
55 return -power(Pi, 2) / 12;
59 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
65 static ex Li3_eval(ex const & x)
72 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
78 static ex factorial_evalf(ex const & x)
80 return factorial(x).hold();
83 static ex factorial_eval(ex const & x)
85 if (is_ex_exactly_of_type(x, numeric))
86 return factorial(ex_to_numeric(x));
88 return factorial(x).hold();
91 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
97 static ex binomial_evalf(ex const & x, ex const & y)
99 return binomial(x, y).hold();
102 static ex binomial_eval(ex const & x, ex const &y)
104 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
105 return binomial(ex_to_numeric(x), ex_to_numeric(y));
107 return binomial(x, y).hold();
110 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
113 // Order term function (for truncated power series)
116 static ex Order_eval(ex const & x)
118 if (is_ex_exactly_of_type(x, numeric)) {
121 return Order(_ex1()).hold();
123 } else if (is_ex_exactly_of_type(x, mul)) {
125 mul *m = static_cast<mul *>(x.bp);
126 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
129 return Order(x / m->op(m->nops() - 1)).hold();
132 return Order(x).hold();
135 static ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
137 // Just wrap the function into a series object
139 new_seq.push_back(expair(Order(_ex1()), numeric(min(x.ldegree(s), order))));
140 return series(s, point, new_seq);
143 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
146 ex lsolve(ex const &eqns, ex const &symbols)
148 // solve a system of linear equations
149 if (eqns.info(info_flags::relation_equal)) {
150 if (!symbols.info(info_flags::symbol)) {
151 throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
153 ex sol=lsolve(lst(eqns),lst(symbols));
155 GINAC_ASSERT(sol.nops()==1);
156 GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
158 return sol.op(0).op(1); // return rhs of first solution
162 if (!eqns.info(info_flags::list)) {
163 throw(std::invalid_argument("lsolve: 1st argument must be a list"));
165 for (int i=0; i<eqns.nops(); i++) {
166 if (!eqns.op(i).info(info_flags::relation_equal)) {
167 throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
170 if (!symbols.info(info_flags::list)) {
171 throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
173 for (int i=0; i<symbols.nops(); i++) {
174 if (!symbols.op(i).info(info_flags::symbol)) {
175 throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
179 // build matrix from equation system
180 matrix sys(eqns.nops(),symbols.nops());
181 matrix rhs(eqns.nops(),1);
182 matrix vars(symbols.nops(),1);
184 for (int r=0; r<eqns.nops(); r++) {
185 ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
187 for (int c=0; c<symbols.nops(); c++) {
188 ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
189 linpart -= co*symbols.op(c);
192 linpart=linpart.expand();
193 rhs.set(r,0,-linpart);
196 // test if system is linear and fill vars matrix
197 for (int i=0; i<symbols.nops(); i++) {
198 vars.set(i,0,symbols.op(i));
199 if (sys.has(symbols.op(i))) {
200 throw(std::logic_error("lsolve: system is not linear"));
202 if (rhs.has(symbols.op(i))) {
203 throw(std::logic_error("lsolve: system is not linear"));
207 //matrix solution=sys.solve(rhs);
210 solution=sys.fraction_free_elim(vars,rhs);
211 } catch (runtime_error const & e) {
212 // probably singular matrix (or other error)
213 // return empty solution list
214 // cerr << e.what() << endl;
218 // return a list of equations
219 if (solution.cols()!=1) {
220 throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
222 if (solution.rows()!=symbols.nops()) {
223 cout << "symbols.nops()=" << symbols.nops() << endl;
224 cout << "solution.rows()=" << solution.rows() << endl;
225 throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
228 // return list of the form lst(var1==sol1,var2==sol2,...)
230 for (int i=0; i<symbols.nops(); i++) {
231 sollist.append(symbols.op(i)==solution(i,0));
237 /** non-commutative power. */
238 ex ncpower(ex const &basis, unsigned exponent)
246 for (unsigned i=0; i<exponent; ++i) {
253 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
254 * for static lib (so ginsh will see them). */
255 unsigned force_include_gamma = function_index_gamma;
256 unsigned force_include_zeta1 = function_index_zeta1;
258 #ifndef NO_GINAC_NAMESPACE
260 #endif // ndef NO_GINAC_NAMESPACE