3 * Implementation of GiNaC's initially known functions. */
14 ex Li2_eval(ex const & x)
18 if (x.is_equal(exONE()))
19 return power(Pi, 2) / 6;
20 if (x.is_equal(exMINUSONE()))
21 return -power(Pi, 2) / 12;
25 REGISTER_FUNCTION(Li2, Li2_eval, NULL, NULL, NULL);
31 ex Li3_eval(ex const & x)
38 REGISTER_FUNCTION(Li3, Li3_eval, NULL, NULL, NULL);
44 ex factorial_evalf(ex const & x)
46 return factorial(x).hold();
49 ex factorial_eval(ex const & x)
51 if (is_ex_exactly_of_type(x, numeric))
52 return factorial(ex_to_numeric(x));
54 return factorial(x).hold();
57 REGISTER_FUNCTION(factorial, factorial_eval, factorial_evalf, NULL, NULL);
63 ex binomial_evalf(ex const & x, ex const & y)
65 return binomial(x, y).hold();
68 ex binomial_eval(ex const & x, ex const &y)
70 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
71 return binomial(ex_to_numeric(x), ex_to_numeric(y));
73 return binomial(x, y).hold();
76 REGISTER_FUNCTION(binomial, binomial_eval, binomial_evalf, NULL, NULL);
79 // Order term function (for truncated power series)
82 ex Order_eval(ex const & x)
84 if (is_ex_exactly_of_type(x, numeric)) {
87 return Order(exONE()).hold();
89 } else if (is_ex_exactly_of_type(x, mul)) {
91 mul *m = static_cast<mul *>(x.bp);
92 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
95 return Order(x / m->op(m->nops() - 1)).hold();
98 return Order(x).hold();
101 ex Order_series(ex const & x, symbol const & s, ex const & point, int order)
103 // Just wrap the function into a series object
105 new_seq.push_back(expair(Order(exONE()), numeric(min(x.ldegree(s), order))));
106 return series(s, point, new_seq);
109 REGISTER_FUNCTION(Order, Order_eval, NULL, NULL, Order_series);
112 ex lsolve(ex eqns, ex symbols)
114 // solve a system of linear equations
115 if (eqns.info(info_flags::relation_equal)) {
116 if (!symbols.info(info_flags::symbol)) {
117 throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
119 ex sol=lsolve(lst(eqns),lst(symbols));
121 ASSERT(sol.nops()==1);
122 ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
124 return sol.op(0).op(1); // return rhs of first solution
128 if (!eqns.info(info_flags::list)) {
129 throw(std::invalid_argument("lsolve: 1st argument must be a list"));
131 for (int i=0; i<eqns.nops(); i++) {
132 if (!eqns.op(i).info(info_flags::relation_equal)) {
133 throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
136 if (!symbols.info(info_flags::list)) {
137 throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
139 for (int i=0; i<symbols.nops(); i++) {
140 if (!symbols.op(i).info(info_flags::symbol)) {
141 throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
145 // build matrix from equation system
146 matrix sys(eqns.nops(),symbols.nops());
147 matrix rhs(eqns.nops(),1);
148 matrix vars(symbols.nops(),1);
150 for (int r=0; r<eqns.nops(); r++) {
151 ex eq=eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
153 for (int c=0; c<symbols.nops(); c++) {
154 ex co=eq.coeff(ex_to_symbol(symbols.op(c)),1);
155 linpart -= co*symbols.op(c);
158 linpart=linpart.expand();
159 rhs.set(r,0,-linpart);
162 // test if system is linear and fill vars matrix
163 for (int i=0; i<symbols.nops(); i++) {
164 vars.set(i,0,symbols.op(i));
165 if (sys.has(symbols.op(i))) {
166 throw(std::logic_error("lsolve: system is not linear"));
168 if (rhs.has(symbols.op(i))) {
169 throw(std::logic_error("lsolve: system is not linear"));
173 //matrix solution=sys.solve(rhs);
176 solution=sys.fraction_free_elim(vars,rhs);
177 } catch (runtime_error const & e) {
178 // probably singular matrix (or other error)
179 // return empty solution list
180 cerr << e.what() << endl;
184 // return a list of equations
185 if (solution.cols()!=1) {
186 throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
188 if (solution.rows()!=symbols.nops()) {
189 cout << "symbols.nops()=" << symbols.nops() << endl;
190 cout << "solution.rows()=" << solution.rows() << endl;
191 throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
194 // return list of the form lst(var1==sol1,var2==sol2,...)
196 for (int i=0; i<symbols.nops(); i++) {
197 sollist.append(symbols.op(i)==solution(i,0));
203 /** non-commutative power. */
204 ex ncpower(ex basis, unsigned exponent)
212 for (unsigned i=0; i<exponent; ++i) {