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
31 ex Li2_eval(ex const & x)
35 if (x.is_equal(exONE()))
36 return power(Pi, 2) / 6;
37 if (x.is_equal(exMINUSONE()))
38 return -power(Pi, 2) / 12;
42 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
48 ex Li3_eval(ex const & x)
55 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
61 ex factorial_evalf(ex const & x)
63 return factorial(x).hold();
66 ex factorial_eval(ex const & x)
68 if (is_ex_exactly_of_type(x, numeric))
69 return factorial(ex_to_numeric(x));
71 return factorial(x).hold();
74 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
80 ex binomial_evalf(ex const & x, ex const & y)
82 return binomial(x, y).hold();
85 ex binomial_eval(ex const & x, ex const &y)
87 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
88 return binomial(ex_to_numeric(x), ex_to_numeric(y));
90 return binomial(x, y).hold();
93 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
96 // Order term function (for truncated power series)
99 ex Order_eval(ex const & x)
101 if (is_ex_exactly_of_type(x, numeric)) {
104 return Order(exONE()).hold();
106 } else if (is_ex_exactly_of_type(x, mul)) {
108 mul *m = static_cast<mul *>(x.bp);
109 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
112 return Order(x / m->op(m->nops() - 1)).hold();
115 return Order(x).hold();
118 ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
120 // Just wrap the function into a series object
122 new_seq.push_back(expair(Order(exONE()), numeric(min(x.ldegree(s), order))));
123 return series(s, point, new_seq);
126 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
129 ex lsolve(ex eqns, ex symbols)
131 // solve a system of linear equations
132 if (eqns.info(info_flags::relation_equal)) {
133 if (!symbols.info(info_flags::symbol)) {
134 throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
136 ex sol=lsolve(lst(eqns),lst(symbols));
138 ASSERT(sol.nops()==1);
139 ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
141 return sol.op(0).op(1); // return rhs of first solution
145 if (!eqns.info(info_flags::list)) {
146 throw(std::invalid_argument("lsolve: 1st argument must be a list"));
148 for (int i=0; i<eqns.nops(); i++) {
149 if (!eqns.op(i).info(info_flags::relation_equal)) {
150 throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
153 if (!symbols.info(info_flags::list)) {
154 throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
156 for (int i=0; i<symbols.nops(); i++) {
157 if (!symbols.op(i).info(info_flags::symbol)) {
158 throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
162 // build matrix from equation system
163 matrix sys(eqns.nops(),symbols.nops());
164 matrix rhs(eqns.nops(),1);
165 matrix vars(symbols.nops(),1);
167 for (int r=0; r<eqns.nops(); r++) {
168 ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
170 for (int c=0; c<symbols.nops(); c++) {
171 ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
172 linpart -= co*symbols.op(c);
175 linpart=linpart.expand();
176 rhs.set(r,0,-linpart);
179 // test if system is linear and fill vars matrix
180 for (int i=0; i<symbols.nops(); i++) {
181 vars.set(i,0,symbols.op(i));
182 if (sys.has(symbols.op(i))) {
183 throw(std::logic_error("lsolve: system is not linear"));
185 if (rhs.has(symbols.op(i))) {
186 throw(std::logic_error("lsolve: system is not linear"));
190 //matrix solution=sys.solve(rhs);
193 solution=sys.fraction_free_elim(vars,rhs);
194 } catch (runtime_error const & e) {
195 // probably singular matrix (or other error)
196 // return empty solution list
197 cerr << e.what() << endl;
201 // return a list of equations
202 if (solution.cols()!=1) {
203 throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
205 if (solution.rows()!=symbols.nops()) {
206 cout << "symbols.nops()=" << symbols.nops() << endl;
207 cout << "solution.rows()=" << solution.rows() << endl;
208 throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
211 // return list of the form lst(var1==sol1,var2==sol2,...)
213 for (int i=0; i<symbols.nops(); i++) {
214 sollist.append(symbols.op(i)==solution(i,0));
220 /** non-commutative power. */
221 ex ncpower(ex basis, unsigned exponent)
229 for (unsigned i=0; i<exponent; ++i) {