3 * Implementation of GiNaC's initially known functions.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
34 #include "relational.h"
42 ex Li2_eval(ex const & 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;
53 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
59 ex Li3_eval(ex const & x)
66 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
72 ex factorial_evalf(ex const & x)
74 return factorial(x).hold();
77 ex factorial_eval(ex const & x)
79 if (is_ex_exactly_of_type(x, numeric))
80 return factorial(ex_to_numeric(x));
82 return factorial(x).hold();
85 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
91 ex binomial_evalf(ex const & x, ex const & y)
93 return binomial(x, y).hold();
96 ex binomial_eval(ex const & x, ex const &y)
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));
101 return binomial(x, y).hold();
104 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
107 // Order term function (for truncated power series)
110 ex Order_eval(ex const & x)
112 if (is_ex_exactly_of_type(x, numeric)) {
115 return Order(exONE()).hold();
117 } else if (is_ex_exactly_of_type(x, mul)) {
119 mul *m = static_cast<mul *>(x.bp);
120 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
123 return Order(x / m->op(m->nops() - 1)).hold();
126 return Order(x).hold();
129 ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
131 // Just wrap the function into a series object
133 new_seq.push_back(expair(Order(exONE()), numeric(min(x.ldegree(s), order))));
134 return series(s, point, new_seq);
137 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
140 ex lsolve(ex const &eqns, ex const &symbols)
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"));
147 ex sol=lsolve(lst(eqns),lst(symbols));
149 ASSERT(sol.nops()==1);
150 ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
152 return sol.op(0).op(1); // return rhs of first solution
156 if (!eqns.info(info_flags::list)) {
157 throw(std::invalid_argument("lsolve: 1st argument must be a list"));
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"));
164 if (!symbols.info(info_flags::list)) {
165 throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
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"));
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);
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
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);
186 linpart=linpart.expand();
187 rhs.set(r,0,-linpart);
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"));
196 if (rhs.has(symbols.op(i))) {
197 throw(std::logic_error("lsolve: system is not linear"));
201 //matrix solution=sys.solve(rhs);
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;
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"));
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"));
222 // return list of the form lst(var1==sol1,var2==sol2,...)
224 for (int i=0; i<symbols.nops(); i++) {
225 sollist.append(symbols.op(i)==solution(i,0));
231 /** non-commutative power. */
232 ex ncpower(ex const &basis, unsigned exponent)
240 for (unsigned i=0; i<exponent; ++i) {