3 * This file implements several functions that work on univariate and
4 * multivariate polynomials and rational functions.
5 * These functions include polynomial quotient and remainder, GCD and LCM
6 * computation, square-free factorization and rational function normalization. */
9 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "expairseq.h"
42 #include "relational.h"
49 // If comparing expressions (ex::compare()) is fast, you can set this to 1.
50 // Some routines like quo(), rem() and gcd() will then return a quick answer
51 // when they are called with two identical arguments.
52 #define FAST_COMPARE 1
54 // Set this if you want divide_in_z() to use remembering
55 #define USE_REMEMBER 0
57 // Set this if you want divide_in_z() to use trial division followed by
58 // polynomial interpolation (always slower except for completely dense
60 #define USE_TRIAL_DIVISION 0
62 // Set this to enable some statistical output for the GCD routines
67 // Statistics variables
68 static int gcd_called = 0;
69 static int sr_gcd_called = 0;
70 static int heur_gcd_called = 0;
71 static int heur_gcd_failed = 0;
73 // Print statistics at end of program
74 static struct _stat_print {
77 cout << "gcd() called " << gcd_called << " times\n";
78 cout << "sr_gcd() called " << sr_gcd_called << " times\n";
79 cout << "heur_gcd() called " << heur_gcd_called << " times\n";
80 cout << "heur_gcd() failed " << heur_gcd_failed << " times\n";
86 /** Return pointer to first symbol found in expression. Due to GiNaCĀ“s
87 * internal ordering of terms, it may not be obvious which symbol this
88 * function returns for a given expression.
90 * @param e expression to search
91 * @param x pointer to first symbol found (returned)
92 * @return "false" if no symbol was found, "true" otherwise */
93 static bool get_first_symbol(const ex &e, const symbol *&x)
95 if (is_ex_exactly_of_type(e, symbol)) {
96 x = static_cast<symbol *>(e.bp);
98 } else if (is_ex_exactly_of_type(e, add) || is_ex_exactly_of_type(e, mul)) {
99 for (unsigned i=0; i<e.nops(); i++)
100 if (get_first_symbol(e.op(i), x))
102 } else if (is_ex_exactly_of_type(e, power)) {
103 if (get_first_symbol(e.op(0), x))
111 * Statistical information about symbols in polynomials
114 /** This structure holds information about the highest and lowest degrees
115 * in which a symbol appears in two multivariate polynomials "a" and "b".
116 * A vector of these structures with information about all symbols in
117 * two polynomials can be created with the function get_symbol_stats().
119 * @see get_symbol_stats */
121 /** Pointer to symbol */
124 /** Highest degree of symbol in polynomial "a" */
127 /** Highest degree of symbol in polynomial "b" */
130 /** Lowest degree of symbol in polynomial "a" */
133 /** Lowest degree of symbol in polynomial "b" */
136 /** Maximum of deg_a and deg_b (Used for sorting) */
139 /** Maximum number of terms of leading coefficient of symbol in both polynomials */
142 /** Commparison operator for sorting */
143 bool operator<(const sym_desc &x) const
145 if (max_deg == x.max_deg)
146 return max_lcnops < x.max_lcnops;
148 return max_deg < x.max_deg;
152 // Vector of sym_desc structures
153 typedef std::vector<sym_desc> sym_desc_vec;
155 // Add symbol the sym_desc_vec (used internally by get_symbol_stats())
156 static void add_symbol(const symbol *s, sym_desc_vec &v)
158 sym_desc_vec::iterator it = v.begin(), itend = v.end();
159 while (it != itend) {
160 if (it->sym->compare(*s) == 0) // If it's already in there, don't add it a second time
169 // Collect all symbols of an expression (used internally by get_symbol_stats())
170 static void collect_symbols(const ex &e, sym_desc_vec &v)
172 if (is_ex_exactly_of_type(e, symbol)) {
173 add_symbol(static_cast<symbol *>(e.bp), v);
174 } else if (is_ex_exactly_of_type(e, add) || is_ex_exactly_of_type(e, mul)) {
175 for (unsigned i=0; i<e.nops(); i++)
176 collect_symbols(e.op(i), v);
177 } else if (is_ex_exactly_of_type(e, power)) {
178 collect_symbols(e.op(0), v);
182 /** Collect statistical information about symbols in polynomials.
183 * This function fills in a vector of "sym_desc" structs which contain
184 * information about the highest and lowest degrees of all symbols that
185 * appear in two polynomials. The vector is then sorted by minimum
186 * degree (lowest to highest). The information gathered by this
187 * function is used by the GCD routines to identify trivial factors
188 * and to determine which variable to choose as the main variable
189 * for GCD computation.
191 * @param a first multivariate polynomial
192 * @param b second multivariate polynomial
193 * @param v vector of sym_desc structs (filled in) */
194 static void get_symbol_stats(const ex &a, const ex &b, sym_desc_vec &v)
196 collect_symbols(a.eval(), v); // eval() to expand assigned symbols
197 collect_symbols(b.eval(), v);
198 sym_desc_vec::iterator it = v.begin(), itend = v.end();
199 while (it != itend) {
200 int deg_a = a.degree(*(it->sym));
201 int deg_b = b.degree(*(it->sym));
204 it->max_deg = std::max(deg_a, deg_b);
205 it->max_lcnops = std::max(a.lcoeff(*(it->sym)).nops(), b.lcoeff(*(it->sym)).nops());
206 it->ldeg_a = a.ldegree(*(it->sym));
207 it->ldeg_b = b.ldegree(*(it->sym));
210 sort(v.begin(), v.end());
212 std::clog << "Symbols:\n";
213 it = v.begin(); itend = v.end();
214 while (it != itend) {
215 std::clog << " " << *it->sym << ": deg_a=" << it->deg_a << ", deg_b=" << it->deg_b << ", ldeg_a=" << it->ldeg_a << ", ldeg_b=" << it->ldeg_b << ", max_deg=" << it->max_deg << ", max_lcnops=" << it->max_lcnops << endl;
216 std::clog << " lcoeff_a=" << a.lcoeff(*(it->sym)) << ", lcoeff_b=" << b.lcoeff(*(it->sym)) << endl;
224 * Computation of LCM of denominators of coefficients of a polynomial
227 // Compute LCM of denominators of coefficients by going through the
228 // expression recursively (used internally by lcm_of_coefficients_denominators())
229 static numeric lcmcoeff(const ex &e, const numeric &l)
231 if (e.info(info_flags::rational))
232 return lcm(ex_to_numeric(e).denom(), l);
233 else if (is_ex_exactly_of_type(e, add)) {
235 for (unsigned i=0; i<e.nops(); i++)
236 c = lcmcoeff(e.op(i), c);
238 } else if (is_ex_exactly_of_type(e, mul)) {
240 for (unsigned i=0; i<e.nops(); i++)
241 c *= lcmcoeff(e.op(i), _num1());
243 } else if (is_ex_exactly_of_type(e, power))
244 return pow(lcmcoeff(e.op(0), l), ex_to_numeric(e.op(1)));
248 /** Compute LCM of denominators of coefficients of a polynomial.
249 * Given a polynomial with rational coefficients, this function computes
250 * the LCM of the denominators of all coefficients. This can be used
251 * to bring a polynomial from Q[X] to Z[X].
253 * @param e multivariate polynomial (need not be expanded)
254 * @return LCM of denominators of coefficients */
255 static numeric lcm_of_coefficients_denominators(const ex &e)
257 return lcmcoeff(e, _num1());
260 /** Bring polynomial from Q[X] to Z[X] by multiplying in the previously
261 * determined LCM of the coefficient's denominators.
263 * @param e multivariate polynomial (need not be expanded)
264 * @param lcm LCM to multiply in */
265 static ex multiply_lcm(const ex &e, const numeric &lcm)
267 if (is_ex_exactly_of_type(e, mul)) {
269 numeric lcm_accum = _num1();
270 for (unsigned i=0; i<e.nops(); i++) {
271 numeric op_lcm = lcmcoeff(e.op(i), _num1());
272 c *= multiply_lcm(e.op(i), op_lcm);
275 c *= lcm / lcm_accum;
277 } else if (is_ex_exactly_of_type(e, add)) {
279 for (unsigned i=0; i<e.nops(); i++)
280 c += multiply_lcm(e.op(i), lcm);
282 } else if (is_ex_exactly_of_type(e, power)) {
283 return pow(multiply_lcm(e.op(0), lcm.power(ex_to_numeric(e.op(1)).inverse())), e.op(1));
289 /** Compute the integer content (= GCD of all numeric coefficients) of an
290 * expanded polynomial.
292 * @param e expanded polynomial
293 * @return integer content */
294 numeric ex::integer_content(void) const
297 return bp->integer_content();
300 numeric basic::integer_content(void) const
305 numeric numeric::integer_content(void) const
310 numeric add::integer_content(void) const
312 epvector::const_iterator it = seq.begin();
313 epvector::const_iterator itend = seq.end();
315 while (it != itend) {
316 GINAC_ASSERT(!is_ex_exactly_of_type(it->rest,numeric));
317 GINAC_ASSERT(is_ex_exactly_of_type(it->coeff,numeric));
318 c = gcd(ex_to_numeric(it->coeff), c);
321 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
322 c = gcd(ex_to_numeric(overall_coeff),c);
326 numeric mul::integer_content(void) const
328 #ifdef DO_GINAC_ASSERT
329 epvector::const_iterator it = seq.begin();
330 epvector::const_iterator itend = seq.end();
331 while (it != itend) {
332 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*it),numeric));
335 #endif // def DO_GINAC_ASSERT
336 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
337 return abs(ex_to_numeric(overall_coeff));
342 * Polynomial quotients and remainders
345 /** Quotient q(x) of polynomials a(x) and b(x) in Q[x].
346 * It satisfies a(x)=b(x)*q(x)+r(x).
348 * @param a first polynomial in x (dividend)
349 * @param b second polynomial in x (divisor)
350 * @param x a and b are polynomials in x
351 * @param check_args check whether a and b are polynomials with rational
352 * coefficients (defaults to "true")
353 * @return quotient of a and b in Q[x] */
354 ex quo(const ex &a, const ex &b, const symbol &x, bool check_args)
357 throw(std::overflow_error("quo: division by zero"));
358 if (is_ex_exactly_of_type(a, numeric) && is_ex_exactly_of_type(b, numeric))
364 if (check_args && (!a.info(info_flags::rational_polynomial) || !b.info(info_flags::rational_polynomial)))
365 throw(std::invalid_argument("quo: arguments must be polynomials over the rationals"));
367 // Polynomial long division
372 int bdeg = b.degree(x);
373 int rdeg = r.degree(x);
374 ex blcoeff = b.expand().coeff(x, bdeg);
375 bool blcoeff_is_numeric = is_ex_exactly_of_type(blcoeff, numeric);
376 while (rdeg >= bdeg) {
377 ex term, rcoeff = r.coeff(x, rdeg);
378 if (blcoeff_is_numeric)
379 term = rcoeff / blcoeff;
381 if (!divide(rcoeff, blcoeff, term, false))
382 return *new ex(fail());
384 term *= power(x, rdeg - bdeg);
386 r -= (term * b).expand();
395 /** Remainder r(x) of polynomials a(x) and b(x) in Q[x].
396 * It satisfies a(x)=b(x)*q(x)+r(x).
398 * @param a first polynomial in x (dividend)
399 * @param b second polynomial in x (divisor)
400 * @param x a and b are polynomials in x
401 * @param check_args check whether a and b are polynomials with rational
402 * coefficients (defaults to "true")
403 * @return remainder of a(x) and b(x) in Q[x] */
404 ex rem(const ex &a, const ex &b, const symbol &x, bool check_args)
407 throw(std::overflow_error("rem: division by zero"));
408 if (is_ex_exactly_of_type(a, numeric)) {
409 if (is_ex_exactly_of_type(b, numeric))
418 if (check_args && (!a.info(info_flags::rational_polynomial) || !b.info(info_flags::rational_polynomial)))
419 throw(std::invalid_argument("rem: arguments must be polynomials over the rationals"));
421 // Polynomial long division
425 int bdeg = b.degree(x);
426 int rdeg = r.degree(x);
427 ex blcoeff = b.expand().coeff(x, bdeg);
428 bool blcoeff_is_numeric = is_ex_exactly_of_type(blcoeff, numeric);
429 while (rdeg >= bdeg) {
430 ex term, rcoeff = r.coeff(x, rdeg);
431 if (blcoeff_is_numeric)
432 term = rcoeff / blcoeff;
434 if (!divide(rcoeff, blcoeff, term, false))
435 return *new ex(fail());
437 term *= power(x, rdeg - bdeg);
438 r -= (term * b).expand();
447 /** Pseudo-remainder of polynomials a(x) and b(x) in Z[x].
449 * @param a first polynomial in x (dividend)
450 * @param b second polynomial in x (divisor)
451 * @param x a and b are polynomials in x
452 * @param check_args check whether a and b are polynomials with rational
453 * coefficients (defaults to "true")
454 * @return pseudo-remainder of a(x) and b(x) in Z[x] */
455 ex prem(const ex &a, const ex &b, const symbol &x, bool check_args)
458 throw(std::overflow_error("prem: division by zero"));
459 if (is_ex_exactly_of_type(a, numeric)) {
460 if (is_ex_exactly_of_type(b, numeric))
465 if (check_args && (!a.info(info_flags::rational_polynomial) || !b.info(info_flags::rational_polynomial)))
466 throw(std::invalid_argument("prem: arguments must be polynomials over the rationals"));
468 // Polynomial long division
471 int rdeg = r.degree(x);
472 int bdeg = eb.degree(x);
475 blcoeff = eb.coeff(x, bdeg);
479 eb -= blcoeff * power(x, bdeg);
483 int delta = rdeg - bdeg + 1, i = 0;
484 while (rdeg >= bdeg && !r.is_zero()) {
485 ex rlcoeff = r.coeff(x, rdeg);
486 ex term = (power(x, rdeg - bdeg) * eb * rlcoeff).expand();
490 r -= rlcoeff * power(x, rdeg);
491 r = (blcoeff * r).expand() - term;
495 return power(blcoeff, delta - i) * r;
499 /** Sparse pseudo-remainder of polynomials a(x) and b(x) in Z[x].
501 * @param a first polynomial in x (dividend)
502 * @param b second polynomial in x (divisor)
503 * @param x a and b are polynomials in x
504 * @param check_args check whether a and b are polynomials with rational
505 * coefficients (defaults to "true")
506 * @return sparse pseudo-remainder of a(x) and b(x) in Z[x] */
508 ex sprem(const ex &a, const ex &b, const symbol &x, bool check_args)
511 throw(std::overflow_error("prem: division by zero"));
512 if (is_ex_exactly_of_type(a, numeric)) {
513 if (is_ex_exactly_of_type(b, numeric))
518 if (check_args && (!a.info(info_flags::rational_polynomial) || !b.info(info_flags::rational_polynomial)))
519 throw(std::invalid_argument("prem: arguments must be polynomials over the rationals"));
521 // Polynomial long division
524 int rdeg = r.degree(x);
525 int bdeg = eb.degree(x);
528 blcoeff = eb.coeff(x, bdeg);
532 eb -= blcoeff * power(x, bdeg);
536 while (rdeg >= bdeg && !r.is_zero()) {
537 ex rlcoeff = r.coeff(x, rdeg);
538 ex term = (power(x, rdeg - bdeg) * eb * rlcoeff).expand();
542 r -= rlcoeff * power(x, rdeg);
543 r = (blcoeff * r).expand() - term;
550 /** Exact polynomial division of a(X) by b(X) in Q[X].
552 * @param a first multivariate polynomial (dividend)
553 * @param b second multivariate polynomial (divisor)
554 * @param q quotient (returned)
555 * @param check_args check whether a and b are polynomials with rational
556 * coefficients (defaults to "true")
557 * @return "true" when exact division succeeds (quotient returned in q),
558 * "false" otherwise */
559 bool divide(const ex &a, const ex &b, ex &q, bool check_args)
563 throw(std::overflow_error("divide: division by zero"));
566 if (is_ex_exactly_of_type(b, numeric)) {
569 } else if (is_ex_exactly_of_type(a, numeric))
577 if (check_args && (!a.info(info_flags::rational_polynomial) ||
578 !b.info(info_flags::rational_polynomial)))
579 throw(std::invalid_argument("divide: arguments must be polynomials over the rationals"));
583 if (!get_first_symbol(a, x) && !get_first_symbol(b, x))
584 throw(std::invalid_argument("invalid expression in divide()"));
586 // Polynomial long division (recursive)
590 int bdeg = b.degree(*x);
591 int rdeg = r.degree(*x);
592 ex blcoeff = b.expand().coeff(*x, bdeg);
593 bool blcoeff_is_numeric = is_ex_exactly_of_type(blcoeff, numeric);
594 while (rdeg >= bdeg) {
595 ex term, rcoeff = r.coeff(*x, rdeg);
596 if (blcoeff_is_numeric)
597 term = rcoeff / blcoeff;
599 if (!divide(rcoeff, blcoeff, term, false))
601 term *= power(*x, rdeg - bdeg);
603 r -= (term * b).expand();
617 typedef std::pair<ex, ex> ex2;
618 typedef std::pair<ex, bool> exbool;
621 bool operator() (const ex2 &p, const ex2 &q) const
623 int cmp = p.first.compare(q.first);
624 return ((cmp<0) || (!(cmp>0) && p.second.compare(q.second)<0));
628 typedef std::map<ex2, exbool, ex2_less> ex2_exbool_remember;
632 /** Exact polynomial division of a(X) by b(X) in Z[X].
633 * This functions works like divide() but the input and output polynomials are
634 * in Z[X] instead of Q[X] (i.e. they have integer coefficients). Unlike
635 * divide(), it doesnĀ“t check whether the input polynomials really are integer
636 * polynomials, so be careful of what you pass in. Also, you have to run
637 * get_symbol_stats() over the input polynomials before calling this function
638 * and pass an iterator to the first element of the sym_desc vector. This
639 * function is used internally by the heur_gcd().
641 * @param a first multivariate polynomial (dividend)
642 * @param b second multivariate polynomial (divisor)
643 * @param q quotient (returned)
644 * @param var iterator to first element of vector of sym_desc structs
645 * @return "true" when exact division succeeds (the quotient is returned in
646 * q), "false" otherwise.
647 * @see get_symbol_stats, heur_gcd */
648 static bool divide_in_z(const ex &a, const ex &b, ex &q, sym_desc_vec::const_iterator var)
652 throw(std::overflow_error("divide_in_z: division by zero"));
653 if (b.is_equal(_ex1())) {
657 if (is_ex_exactly_of_type(a, numeric)) {
658 if (is_ex_exactly_of_type(b, numeric)) {
660 return q.info(info_flags::integer);
673 static ex2_exbool_remember dr_remember;
674 ex2_exbool_remember::const_iterator remembered = dr_remember.find(ex2(a, b));
675 if (remembered != dr_remember.end()) {
676 q = remembered->second.first;
677 return remembered->second.second;
682 const symbol *x = var->sym;
685 int adeg = a.degree(*x), bdeg = b.degree(*x);
689 #if USE_TRIAL_DIVISION
691 // Trial division with polynomial interpolation
694 // Compute values at evaluation points 0..adeg
695 vector<numeric> alpha; alpha.reserve(adeg + 1);
696 exvector u; u.reserve(adeg + 1);
697 numeric point = _num0();
699 for (i=0; i<=adeg; i++) {
700 ex bs = b.subs(*x == point);
701 while (bs.is_zero()) {
703 bs = b.subs(*x == point);
705 if (!divide_in_z(a.subs(*x == point), bs, c, var+1))
707 alpha.push_back(point);
713 vector<numeric> rcp; rcp.reserve(adeg + 1);
714 rcp.push_back(_num0());
715 for (k=1; k<=adeg; k++) {
716 numeric product = alpha[k] - alpha[0];
718 product *= alpha[k] - alpha[i];
719 rcp.push_back(product.inverse());
722 // Compute Newton coefficients
723 exvector v; v.reserve(adeg + 1);
725 for (k=1; k<=adeg; k++) {
727 for (i=k-2; i>=0; i--)
728 temp = temp * (alpha[k] - alpha[i]) + v[i];
729 v.push_back((u[k] - temp) * rcp[k]);
732 // Convert from Newton form to standard form
734 for (k=adeg-1; k>=0; k--)
735 c = c * (*x - alpha[k]) + v[k];
737 if (c.degree(*x) == (adeg - bdeg)) {
745 // Polynomial long division (recursive)
751 ex blcoeff = eb.coeff(*x, bdeg);
752 while (rdeg >= bdeg) {
753 ex term, rcoeff = r.coeff(*x, rdeg);
754 if (!divide_in_z(rcoeff, blcoeff, term, var+1))
756 term = (term * power(*x, rdeg - bdeg)).expand();
758 r -= (term * eb).expand();
761 dr_remember[ex2(a, b)] = exbool(q, true);
768 dr_remember[ex2(a, b)] = exbool(q, false);
777 * Separation of unit part, content part and primitive part of polynomials
780 /** Compute unit part (= sign of leading coefficient) of a multivariate
781 * polynomial in Z[x]. The product of unit part, content part, and primitive
782 * part is the polynomial itself.
784 * @param x variable in which to compute the unit part
786 * @see ex::content, ex::primpart */
787 ex ex::unit(const symbol &x) const
789 ex c = expand().lcoeff(x);
790 if (is_ex_exactly_of_type(c, numeric))
791 return c < _ex0() ? _ex_1() : _ex1();
794 if (get_first_symbol(c, y))
797 throw(std::invalid_argument("invalid expression in unit()"));
802 /** Compute content part (= unit normal GCD of all coefficients) of a
803 * multivariate polynomial in Z[x]. The product of unit part, content part,
804 * and primitive part is the polynomial itself.
806 * @param x variable in which to compute the content part
807 * @return content part
808 * @see ex::unit, ex::primpart */
809 ex ex::content(const symbol &x) const
813 if (is_ex_exactly_of_type(*this, numeric))
814 return info(info_flags::negative) ? -*this : *this;
819 // First, try the integer content
820 ex c = e.integer_content();
822 ex lcoeff = r.lcoeff(x);
823 if (lcoeff.info(info_flags::integer))
826 // GCD of all coefficients
827 int deg = e.degree(x);
828 int ldeg = e.ldegree(x);
830 return e.lcoeff(x) / e.unit(x);
832 for (int i=ldeg; i<=deg; i++)
833 c = gcd(e.coeff(x, i), c, NULL, NULL, false);
838 /** Compute primitive part of a multivariate polynomial in Z[x].
839 * The product of unit part, content part, and primitive part is the
842 * @param x variable in which to compute the primitive part
843 * @return primitive part
844 * @see ex::unit, ex::content */
845 ex ex::primpart(const symbol &x) const
849 if (is_ex_exactly_of_type(*this, numeric))
856 if (is_ex_exactly_of_type(c, numeric))
857 return *this / (c * u);
859 return quo(*this, c * u, x, false);
863 /** Compute primitive part of a multivariate polynomial in Z[x] when the
864 * content part is already known. This function is faster in computing the
865 * primitive part than the previous function.
867 * @param x variable in which to compute the primitive part
868 * @param c previously computed content part
869 * @return primitive part */
870 ex ex::primpart(const symbol &x, const ex &c) const
876 if (is_ex_exactly_of_type(*this, numeric))
880 if (is_ex_exactly_of_type(c, numeric))
881 return *this / (c * u);
883 return quo(*this, c * u, x, false);
888 * GCD of multivariate polynomials
891 /** Compute GCD of polynomials in Q[X] using the Euclidean algorithm (not
892 * really suited for multivariate GCDs). This function is only provided for
895 * @param a first multivariate polynomial
896 * @param b second multivariate polynomial
897 * @param x pointer to symbol (main variable) in which to compute the GCD in
898 * @return the GCD as a new expression
901 static ex eu_gcd(const ex &a, const ex &b, const symbol *x)
903 //std::clog << "eu_gcd(" << a << "," << b << ")\n";
905 // Sort c and d so that c has higher degree
907 int adeg = a.degree(*x), bdeg = b.degree(*x);
917 c = c / c.lcoeff(*x);
918 d = d / d.lcoeff(*x);
920 // Euclidean algorithm
923 //std::clog << " d = " << d << endl;
924 r = rem(c, d, *x, false);
926 return d / d.lcoeff(*x);
933 /** Compute GCD of multivariate polynomials using the Euclidean PRS algorithm
934 * with pseudo-remainders ("World's Worst GCD Algorithm", staying in Z[X]).
935 * This function is only provided for testing purposes.
937 * @param a first multivariate polynomial
938 * @param b second multivariate polynomial
939 * @param x pointer to symbol (main variable) in which to compute the GCD in
940 * @return the GCD as a new expression
943 static ex euprem_gcd(const ex &a, const ex &b, const symbol *x)
945 //std::clog << "euprem_gcd(" << a << "," << b << ")\n";
947 // Sort c and d so that c has higher degree
949 int adeg = a.degree(*x), bdeg = b.degree(*x);
958 // Calculate GCD of contents
959 ex gamma = gcd(c.content(*x), d.content(*x), NULL, NULL, false);
961 // Euclidean algorithm with pseudo-remainders
964 //std::clog << " d = " << d << endl;
965 r = prem(c, d, *x, false);
967 return d.primpart(*x) * gamma;
974 /** Compute GCD of multivariate polynomials using the primitive Euclidean
975 * PRS algorithm (complete content removal at each step). This function is
976 * only provided for testing purposes.
978 * @param a first multivariate polynomial
979 * @param b second multivariate polynomial
980 * @param x pointer to symbol (main variable) in which to compute the GCD in
981 * @return the GCD as a new expression
984 static ex peu_gcd(const ex &a, const ex &b, const symbol *x)
986 //std::clog << "peu_gcd(" << a << "," << b << ")\n";
988 // Sort c and d so that c has higher degree
990 int adeg = a.degree(*x), bdeg = b.degree(*x);
1002 // Remove content from c and d, to be attached to GCD later
1003 ex cont_c = c.content(*x);
1004 ex cont_d = d.content(*x);
1005 ex gamma = gcd(cont_c, cont_d, NULL, NULL, false);
1008 c = c.primpart(*x, cont_c);
1009 d = d.primpart(*x, cont_d);
1011 // Euclidean algorithm with content removal
1014 //std::clog << " d = " << d << endl;
1015 r = prem(c, d, *x, false);
1024 /** Compute GCD of multivariate polynomials using the reduced PRS algorithm.
1025 * This function is only provided for testing purposes.
1027 * @param a first multivariate polynomial
1028 * @param b second multivariate polynomial
1029 * @param x pointer to symbol (main variable) in which to compute the GCD in
1030 * @return the GCD as a new expression
1033 static ex red_gcd(const ex &a, const ex &b, const symbol *x)
1035 //std::clog << "red_gcd(" << a << "," << b << ")\n";
1037 // Sort c and d so that c has higher degree
1039 int adeg = a.degree(*x), bdeg = b.degree(*x);
1053 // Remove content from c and d, to be attached to GCD later
1054 ex cont_c = c.content(*x);
1055 ex cont_d = d.content(*x);
1056 ex gamma = gcd(cont_c, cont_d, NULL, NULL, false);
1059 c = c.primpart(*x, cont_c);
1060 d = d.primpart(*x, cont_d);
1062 // First element of divisor sequence
1064 int delta = cdeg - ddeg;
1067 // Calculate polynomial pseudo-remainder
1068 //std::clog << " d = " << d << endl;
1069 r = prem(c, d, *x, false);
1071 return gamma * d.primpart(*x);
1075 if (!divide(r, pow(ri, delta), d, false))
1076 throw(std::runtime_error("invalid expression in red_gcd(), division failed"));
1077 ddeg = d.degree(*x);
1079 if (is_ex_exactly_of_type(r, numeric))
1082 return gamma * r.primpart(*x);
1085 ri = c.expand().lcoeff(*x);
1086 delta = cdeg - ddeg;
1091 /** Compute GCD of multivariate polynomials using the subresultant PRS
1092 * algorithm. This function is used internally by gcd().
1094 * @param a first multivariate polynomial
1095 * @param b second multivariate polynomial
1096 * @param var iterator to first element of vector of sym_desc structs
1097 * @return the GCD as a new expression
1100 static ex sr_gcd(const ex &a, const ex &b, sym_desc_vec::const_iterator var)
1102 //std::clog << "sr_gcd(" << a << "," << b << ")\n";
1107 // The first symbol is our main variable
1108 const symbol &x = *(var->sym);
1110 // Sort c and d so that c has higher degree
1112 int adeg = a.degree(x), bdeg = b.degree(x);
1126 // Remove content from c and d, to be attached to GCD later
1127 ex cont_c = c.content(x);
1128 ex cont_d = d.content(x);
1129 ex gamma = gcd(cont_c, cont_d, NULL, NULL, false);
1132 c = c.primpart(x, cont_c);
1133 d = d.primpart(x, cont_d);
1134 //std::clog << " content " << gamma << " removed, continuing with sr_gcd(" << c << "," << d << ")\n";
1136 // First element of subresultant sequence
1137 ex r = _ex0(), ri = _ex1(), psi = _ex1();
1138 int delta = cdeg - ddeg;
1141 // Calculate polynomial pseudo-remainder
1142 //std::clog << " start of loop, psi = " << psi << ", calculating pseudo-remainder...\n";
1143 //std::clog << " d = " << d << endl;
1144 r = prem(c, d, x, false);
1146 return gamma * d.primpart(x);
1149 //std::clog << " dividing...\n";
1150 if (!divide_in_z(r, ri * pow(psi, delta), d, var))
1151 throw(std::runtime_error("invalid expression in sr_gcd(), division failed"));
1154 if (is_ex_exactly_of_type(r, numeric))
1157 return gamma * r.primpart(x);
1160 // Next element of subresultant sequence
1161 //std::clog << " calculating next subresultant...\n";
1162 ri = c.expand().lcoeff(x);
1166 divide_in_z(pow(ri, delta), pow(psi, delta-1), psi, var+1);
1167 delta = cdeg - ddeg;
1172 /** Return maximum (absolute value) coefficient of a polynomial.
1173 * This function is used internally by heur_gcd().
1175 * @param e expanded multivariate polynomial
1176 * @return maximum coefficient
1178 numeric ex::max_coefficient(void) const
1180 GINAC_ASSERT(bp!=0);
1181 return bp->max_coefficient();
1184 /** Implementation ex::max_coefficient().
1186 numeric basic::max_coefficient(void) const
1191 numeric numeric::max_coefficient(void) const
1196 numeric add::max_coefficient(void) const
1198 epvector::const_iterator it = seq.begin();
1199 epvector::const_iterator itend = seq.end();
1200 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
1201 numeric cur_max = abs(ex_to_numeric(overall_coeff));
1202 while (it != itend) {
1204 GINAC_ASSERT(!is_ex_exactly_of_type(it->rest,numeric));
1205 a = abs(ex_to_numeric(it->coeff));
1213 numeric mul::max_coefficient(void) const
1215 #ifdef DO_GINAC_ASSERT
1216 epvector::const_iterator it = seq.begin();
1217 epvector::const_iterator itend = seq.end();
1218 while (it != itend) {
1219 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*it),numeric));
1222 #endif // def DO_GINAC_ASSERT
1223 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
1224 return abs(ex_to_numeric(overall_coeff));
1228 /** Apply symmetric modular homomorphism to a multivariate polynomial.
1229 * This function is used internally by heur_gcd().
1231 * @param e expanded multivariate polynomial
1233 * @return mapped polynomial
1235 ex ex::smod(const numeric &xi) const
1237 GINAC_ASSERT(bp!=0);
1238 return bp->smod(xi);
1241 ex basic::smod(const numeric &xi) const
1246 ex numeric::smod(const numeric &xi) const
1248 return GiNaC::smod(*this, xi);
1251 ex add::smod(const numeric &xi) const
1254 newseq.reserve(seq.size()+1);
1255 epvector::const_iterator it = seq.begin();
1256 epvector::const_iterator itend = seq.end();
1257 while (it != itend) {
1258 GINAC_ASSERT(!is_ex_exactly_of_type(it->rest,numeric));
1259 numeric coeff = GiNaC::smod(ex_to_numeric(it->coeff), xi);
1260 if (!coeff.is_zero())
1261 newseq.push_back(expair(it->rest, coeff));
1264 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
1265 numeric coeff = GiNaC::smod(ex_to_numeric(overall_coeff), xi);
1266 return (new add(newseq,coeff))->setflag(status_flags::dynallocated);
1269 ex mul::smod(const numeric &xi) const
1271 #ifdef DO_GINAC_ASSERT
1272 epvector::const_iterator it = seq.begin();
1273 epvector::const_iterator itend = seq.end();
1274 while (it != itend) {
1275 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*it),numeric));
1278 #endif // def DO_GINAC_ASSERT
1279 mul * mulcopyp=new mul(*this);
1280 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
1281 mulcopyp->overall_coeff = GiNaC::smod(ex_to_numeric(overall_coeff),xi);
1282 mulcopyp->clearflag(status_flags::evaluated);
1283 mulcopyp->clearflag(status_flags::hash_calculated);
1284 return mulcopyp->setflag(status_flags::dynallocated);
1288 /** xi-adic polynomial interpolation */
1289 static ex interpolate(const ex &gamma, const numeric &xi, const symbol &x)
1293 numeric rxi = xi.inverse();
1294 for (int i=0; !e.is_zero(); i++) {
1296 g += gi * power(x, i);
1302 /** Exception thrown by heur_gcd() to signal failure. */
1303 class gcdheu_failed {};
1305 /** Compute GCD of multivariate polynomials using the heuristic GCD algorithm.
1306 * get_symbol_stats() must have been called previously with the input
1307 * polynomials and an iterator to the first element of the sym_desc vector
1308 * passed in. This function is used internally by gcd().
1310 * @param a first multivariate polynomial (expanded)
1311 * @param b second multivariate polynomial (expanded)
1312 * @param ca cofactor of polynomial a (returned), NULL to suppress
1313 * calculation of cofactor
1314 * @param cb cofactor of polynomial b (returned), NULL to suppress
1315 * calculation of cofactor
1316 * @param var iterator to first element of vector of sym_desc structs
1317 * @return the GCD as a new expression
1319 * @exception gcdheu_failed() */
1320 static ex heur_gcd(const ex &a, const ex &b, ex *ca, ex *cb, sym_desc_vec::const_iterator var)
1322 //std::clog << "heur_gcd(" << a << "," << b << ")\n";
1327 // Algorithms only works for non-vanishing input polynomials
1328 if (a.is_zero() || b.is_zero())
1329 return *new ex(fail());
1331 // GCD of two numeric values -> CLN
1332 if (is_ex_exactly_of_type(a, numeric) && is_ex_exactly_of_type(b, numeric)) {
1333 numeric g = gcd(ex_to_numeric(a), ex_to_numeric(b));
1335 *ca = ex_to_numeric(a) / g;
1337 *cb = ex_to_numeric(b) / g;
1341 // The first symbol is our main variable
1342 const symbol &x = *(var->sym);
1344 // Remove integer content
1345 numeric gc = gcd(a.integer_content(), b.integer_content());
1346 numeric rgc = gc.inverse();
1349 int maxdeg = std::max(p.degree(x),q.degree(x));
1351 // Find evaluation point
1352 numeric mp = p.max_coefficient();
1353 numeric mq = q.max_coefficient();
1356 xi = mq * _num2() + _num2();
1358 xi = mp * _num2() + _num2();
1361 for (int t=0; t<6; t++) {
1362 if (xi.int_length() * maxdeg > 100000) {
1363 //std::clog << "giving up heur_gcd, xi.int_length = " << xi.int_length() << ", maxdeg = " << maxdeg << endl;
1364 throw gcdheu_failed();
1367 // Apply evaluation homomorphism and calculate GCD
1369 ex gamma = heur_gcd(p.subs(x == xi), q.subs(x == xi), &cp, &cq, var+1).expand();
1370 if (!is_ex_exactly_of_type(gamma, fail)) {
1372 // Reconstruct polynomial from GCD of mapped polynomials
1373 ex g = interpolate(gamma, xi, x);
1375 // Remove integer content
1376 g /= g.integer_content();
1378 // If the calculated polynomial divides both p and q, this is the GCD
1380 if (divide_in_z(p, g, ca ? *ca : dummy, var) && divide_in_z(q, g, cb ? *cb : dummy, var)) {
1382 ex lc = g.lcoeff(x);
1383 if (is_ex_exactly_of_type(lc, numeric) && ex_to_numeric(lc).is_negative())
1389 cp = interpolate(cp, xi, x);
1390 if (divide_in_z(cp, p, g, var)) {
1391 if (divide_in_z(g, q, cb ? *cb : dummy, var)) {
1395 ex lc = g.lcoeff(x);
1396 if (is_ex_exactly_of_type(lc, numeric) && ex_to_numeric(lc).is_negative())
1402 cq = interpolate(cq, xi, x);
1403 if (divide_in_z(cq, q, g, var)) {
1404 if (divide_in_z(g, p, ca ? *ca : dummy, var)) {
1408 ex lc = g.lcoeff(x);
1409 if (is_ex_exactly_of_type(lc, numeric) && ex_to_numeric(lc).is_negative())
1418 // Next evaluation point
1419 xi = iquo(xi * isqrt(isqrt(xi)) * numeric(73794), numeric(27011));
1421 return *new ex(fail());
1425 /** Compute GCD (Greatest Common Divisor) of multivariate polynomials a(X)
1428 * @param a first multivariate polynomial
1429 * @param b second multivariate polynomial
1430 * @param check_args check whether a and b are polynomials with rational
1431 * coefficients (defaults to "true")
1432 * @return the GCD as a new expression */
1433 ex gcd(const ex &a, const ex &b, ex *ca, ex *cb, bool check_args)
1435 //std::clog << "gcd(" << a << "," << b << ")\n";
1440 // GCD of numerics -> CLN
1441 if (is_ex_exactly_of_type(a, numeric) && is_ex_exactly_of_type(b, numeric)) {
1442 numeric g = gcd(ex_to_numeric(a), ex_to_numeric(b));
1451 *ca = ex_to_numeric(a) / g;
1453 *cb = ex_to_numeric(b) / g;
1460 if (check_args && (!a.info(info_flags::rational_polynomial) || !b.info(info_flags::rational_polynomial))) {
1461 throw(std::invalid_argument("gcd: arguments must be polynomials over the rationals"));
1464 // Partially factored cases (to avoid expanding large expressions)
1465 if (is_ex_exactly_of_type(a, mul)) {
1466 if (is_ex_exactly_of_type(b, mul) && b.nops() > a.nops())
1472 for (unsigned i=0; i<a.nops(); i++) {
1473 ex part_ca, part_cb;
1474 g *= gcd(a.op(i), part_b, &part_ca, &part_cb, check_args);
1483 } else if (is_ex_exactly_of_type(b, mul)) {
1484 if (is_ex_exactly_of_type(a, mul) && a.nops() > b.nops())
1490 for (unsigned i=0; i<b.nops(); i++) {
1491 ex part_ca, part_cb;
1492 g *= gcd(part_a, b.op(i), &part_ca, &part_cb, check_args);
1504 // Input polynomials of the form poly^n are sometimes also trivial
1505 if (is_ex_exactly_of_type(a, power)) {
1507 if (is_ex_exactly_of_type(b, power)) {
1508 if (p.is_equal(b.op(0))) {
1509 // a = p^n, b = p^m, gcd = p^min(n, m)
1510 ex exp_a = a.op(1), exp_b = b.op(1);
1511 if (exp_a < exp_b) {
1515 *cb = power(p, exp_b - exp_a);
1516 return power(p, exp_a);
1519 *ca = power(p, exp_a - exp_b);
1522 return power(p, exp_b);
1526 if (p.is_equal(b)) {
1527 // a = p^n, b = p, gcd = p
1529 *ca = power(p, a.op(1) - 1);
1535 } else if (is_ex_exactly_of_type(b, power)) {
1537 if (p.is_equal(a)) {
1538 // a = p, b = p^n, gcd = p
1542 *cb = power(p, b.op(1) - 1);
1548 // Some trivial cases
1549 ex aex = a.expand(), bex = b.expand();
1550 if (aex.is_zero()) {
1557 if (bex.is_zero()) {
1564 if (aex.is_equal(_ex1()) || bex.is_equal(_ex1())) {
1572 if (a.is_equal(b)) {
1581 // Gather symbol statistics
1582 sym_desc_vec sym_stats;
1583 get_symbol_stats(a, b, sym_stats);
1585 // The symbol with least degree is our main variable
1586 sym_desc_vec::const_iterator var = sym_stats.begin();
1587 const symbol &x = *(var->sym);
1589 // Cancel trivial common factor
1590 int ldeg_a = var->ldeg_a;
1591 int ldeg_b = var->ldeg_b;
1592 int min_ldeg = std::min(ldeg_a,ldeg_b);
1594 ex common = power(x, min_ldeg);
1595 //std::clog << "trivial common factor " << common << endl;
1596 return gcd((aex / common).expand(), (bex / common).expand(), ca, cb, false) * common;
1599 // Try to eliminate variables
1600 if (var->deg_a == 0) {
1601 //std::clog << "eliminating variable " << x << " from b" << endl;
1602 ex c = bex.content(x);
1603 ex g = gcd(aex, c, ca, cb, false);
1605 *cb *= bex.unit(x) * bex.primpart(x, c);
1607 } else if (var->deg_b == 0) {
1608 //std::clog << "eliminating variable " << x << " from a" << endl;
1609 ex c = aex.content(x);
1610 ex g = gcd(c, bex, ca, cb, false);
1612 *ca *= aex.unit(x) * aex.primpart(x, c);
1618 // Try heuristic algorithm first, fall back to PRS if that failed
1620 g = heur_gcd(aex, bex, ca, cb, var);
1621 } catch (gcdheu_failed) {
1622 g = *new ex(fail());
1624 if (is_ex_exactly_of_type(g, fail)) {
1625 //std::clog << "heuristics failed" << endl;
1630 // g = heur_gcd(aex, bex, ca, cb, var);
1631 // g = eu_gcd(aex, bex, &x);
1632 // g = euprem_gcd(aex, bex, &x);
1633 // g = peu_gcd(aex, bex, &x);
1634 // g = red_gcd(aex, bex, &x);
1635 g = sr_gcd(aex, bex, var);
1636 if (g.is_equal(_ex1())) {
1637 // Keep cofactors factored if possible
1644 divide(aex, g, *ca, false);
1646 divide(bex, g, *cb, false);
1650 if (g.is_equal(_ex1())) {
1651 // Keep cofactors factored if possible
1663 /** Compute LCM (Least Common Multiple) of multivariate polynomials in Z[X].
1665 * @param a first multivariate polynomial
1666 * @param b second multivariate polynomial
1667 * @param check_args check whether a and b are polynomials with rational
1668 * coefficients (defaults to "true")
1669 * @return the LCM as a new expression */
1670 ex lcm(const ex &a, const ex &b, bool check_args)
1672 if (is_ex_exactly_of_type(a, numeric) && is_ex_exactly_of_type(b, numeric))
1673 return lcm(ex_to_numeric(a), ex_to_numeric(b));
1674 if (check_args && (!a.info(info_flags::rational_polynomial) || !b.info(info_flags::rational_polynomial)))
1675 throw(std::invalid_argument("lcm: arguments must be polynomials over the rationals"));
1678 ex g = gcd(a, b, &ca, &cb, false);
1684 * Square-free factorization
1687 // Univariate GCD of polynomials in Q[x] (used internally by sqrfree()).
1688 // a and b can be multivariate polynomials but they are treated as univariate polynomials in x.
1689 static ex univariate_gcd(const ex &a, const ex &b, const symbol &x)
1695 if (a.is_equal(_ex1()) || b.is_equal(_ex1()))
1697 if (is_ex_of_type(a, numeric) && is_ex_of_type(b, numeric))
1698 return gcd(ex_to_numeric(a), ex_to_numeric(b));
1699 if (!a.info(info_flags::rational_polynomial) || !b.info(info_flags::rational_polynomial))
1700 throw(std::invalid_argument("univariate_gcd: arguments must be polynomials over the rationals"));
1702 // Euclidean algorithm
1704 if (a.degree(x) >= b.degree(x)) {
1712 r = rem(c, d, x, false);
1718 return d / d.lcoeff(x);
1722 /** Compute square-free factorization of multivariate polynomial a(x) using
1725 * @param a multivariate polynomial
1726 * @param x variable to factor in
1727 * @return factored polynomial */
1728 ex sqrfree(const ex &a, const symbol &x)
1733 ex c = univariate_gcd(a, b, x);
1735 if (c.is_equal(_ex1())) {
1739 ex y = quo(b, c, x);
1740 ex z = y - w.diff(x);
1741 while (!z.is_zero()) {
1742 ex g = univariate_gcd(w, z, x);
1750 return res * power(w, i);
1755 * Normal form of rational functions
1759 * Note: The internal normal() functions (= basic::normal() and overloaded
1760 * functions) all return lists of the form {numerator, denominator}. This
1761 * is to get around mul::eval()'s automatic expansion of numeric coefficients.
1762 * E.g. (a+b)/3 is automatically converted to a/3+b/3 but we want to keep
1763 * the information that (a+b) is the numerator and 3 is the denominator.
1766 /** Create a symbol for replacing the expression "e" (or return a previously
1767 * assigned symbol). The symbol is appended to sym_lst and returned, the
1768 * expression is appended to repl_lst.
1769 * @see ex::normal */
1770 static ex replace_with_symbol(const ex &e, lst &sym_lst, lst &repl_lst)
1772 // Expression already in repl_lst? Then return the assigned symbol
1773 for (unsigned i=0; i<repl_lst.nops(); i++)
1774 if (repl_lst.op(i).is_equal(e))
1775 return sym_lst.op(i);
1777 // Otherwise create new symbol and add to list, taking care that the
1778 // replacement expression doesn't contain symbols from the sym_lst
1779 // because subs() is not recursive
1782 ex e_replaced = e.subs(sym_lst, repl_lst);
1784 repl_lst.append(e_replaced);
1788 /** Create a symbol for replacing the expression "e" (or return a previously
1789 * assigned symbol). An expression of the form "symbol == expression" is added
1790 * to repl_lst and the symbol is returned.
1791 * @see ex::to_rational */
1792 static ex replace_with_symbol(const ex &e, lst &repl_lst)
1794 // Expression already in repl_lst? Then return the assigned symbol
1795 for (unsigned i=0; i<repl_lst.nops(); i++)
1796 if (repl_lst.op(i).op(1).is_equal(e))
1797 return repl_lst.op(i).op(0);
1799 // Otherwise create new symbol and add to list, taking care that the
1800 // replacement expression doesn't contain symbols from the sym_lst
1801 // because subs() is not recursive
1804 ex e_replaced = e.subs(repl_lst);
1805 repl_lst.append(es == e_replaced);
1809 /** Default implementation of ex::normal(). It replaces the object with a
1811 * @see ex::normal */
1812 ex basic::normal(lst &sym_lst, lst &repl_lst, int level) const
1814 return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
1818 /** Implementation of ex::normal() for symbols. This returns the unmodified symbol.
1819 * @see ex::normal */
1820 ex symbol::normal(lst &sym_lst, lst &repl_lst, int level) const
1822 return (new lst(*this, _ex1()))->setflag(status_flags::dynallocated);
1826 /** Implementation of ex::normal() for a numeric. It splits complex numbers
1827 * into re+I*im and replaces I and non-rational real numbers with a temporary
1829 * @see ex::normal */
1830 ex numeric::normal(lst &sym_lst, lst &repl_lst, int level) const
1832 numeric num = numer();
1835 if (num.is_real()) {
1836 if (!num.is_integer())
1837 numex = replace_with_symbol(numex, sym_lst, repl_lst);
1839 numeric re = num.real(), im = num.imag();
1840 ex re_ex = re.is_rational() ? re : replace_with_symbol(re, sym_lst, repl_lst);
1841 ex im_ex = im.is_rational() ? im : replace_with_symbol(im, sym_lst, repl_lst);
1842 numex = re_ex + im_ex * replace_with_symbol(I, sym_lst, repl_lst);
1845 // Denominator is always a real integer (see numeric::denom())
1846 return (new lst(numex, denom()))->setflag(status_flags::dynallocated);
1850 /** Fraction cancellation.
1851 * @param n numerator
1852 * @param d denominator
1853 * @return cancelled fraction {n, d} as a list */
1854 static ex frac_cancel(const ex &n, const ex &d)
1858 numeric pre_factor = _num1();
1860 //std::clog << "frac_cancel num = " << num << ", den = " << den << endl;
1862 // Handle trivial case where denominator is 1
1863 if (den.is_equal(_ex1()))
1864 return (new lst(num, den))->setflag(status_flags::dynallocated);
1866 // Handle special cases where numerator or denominator is 0
1868 return (new lst(num, _ex1()))->setflag(status_flags::dynallocated);
1869 if (den.expand().is_zero())
1870 throw(std::overflow_error("frac_cancel: division by zero in frac_cancel"));
1872 // Bring numerator and denominator to Z[X] by multiplying with
1873 // LCM of all coefficients' denominators
1874 numeric num_lcm = lcm_of_coefficients_denominators(num);
1875 numeric den_lcm = lcm_of_coefficients_denominators(den);
1876 num = multiply_lcm(num, num_lcm);
1877 den = multiply_lcm(den, den_lcm);
1878 pre_factor = den_lcm / num_lcm;
1880 // Cancel GCD from numerator and denominator
1882 if (gcd(num, den, &cnum, &cden, false) != _ex1()) {
1887 // Make denominator unit normal (i.e. coefficient of first symbol
1888 // as defined by get_first_symbol() is made positive)
1890 if (get_first_symbol(den, x)) {
1891 GINAC_ASSERT(is_ex_exactly_of_type(den.unit(*x),numeric));
1892 if (ex_to_numeric(den.unit(*x)).is_negative()) {
1898 // Return result as list
1899 //std::clog << " returns num = " << num << ", den = " << den << ", pre_factor = " << pre_factor << endl;
1900 return (new lst(num * pre_factor.numer(), den * pre_factor.denom()))->setflag(status_flags::dynallocated);
1904 /** Implementation of ex::normal() for a sum. It expands terms and performs
1905 * fractional addition.
1906 * @see ex::normal */
1907 ex add::normal(lst &sym_lst, lst &repl_lst, int level) const
1910 return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
1911 else if (level == -max_recursion_level)
1912 throw(std::runtime_error("max recursion level reached"));
1914 // Normalize children and split each one into numerator and denominator
1915 exvector nums, dens;
1916 nums.reserve(seq.size()+1);
1917 dens.reserve(seq.size()+1);
1918 epvector::const_iterator it = seq.begin(), itend = seq.end();
1919 while (it != itend) {
1920 ex n = recombine_pair_to_ex(*it).bp->normal(sym_lst, repl_lst, level-1);
1921 nums.push_back(n.op(0));
1922 dens.push_back(n.op(1));
1925 ex n = overall_coeff.bp->normal(sym_lst, repl_lst, level-1);
1926 nums.push_back(n.op(0));
1927 dens.push_back(n.op(1));
1928 GINAC_ASSERT(nums.size() == dens.size());
1930 // Now, nums is a vector of all numerators and dens is a vector of
1932 //std::clog << "add::normal uses " << nums.size() << " summands:\n";
1934 // Add fractions sequentially
1935 exvector::const_iterator num_it = nums.begin(), num_itend = nums.end();
1936 exvector::const_iterator den_it = dens.begin(), den_itend = dens.end();
1937 //std::clog << " num = " << *num_it << ", den = " << *den_it << endl;
1938 ex num = *num_it++, den = *den_it++;
1939 while (num_it != num_itend) {
1940 //std::clog << " num = " << *num_it << ", den = " << *den_it << endl;
1941 ex next_num = *num_it++, next_den = *den_it++;
1943 // Trivially add sequences of fractions with identical denominators
1944 while ((den_it != den_itend) && next_den.is_equal(*den_it)) {
1945 next_num += *num_it;
1949 // Additiion of two fractions, taking advantage of the fact that
1950 // the heuristic GCD algorithm computes the cofactors at no extra cost
1951 ex co_den1, co_den2;
1952 ex g = gcd(den, next_den, &co_den1, &co_den2, false);
1953 num = ((num * co_den2) + (next_num * co_den1)).expand();
1954 den *= co_den2; // this is the lcm(den, next_den)
1956 //std::clog << " common denominator = " << den << endl;
1958 // Cancel common factors from num/den
1959 return frac_cancel(num, den);
1963 /** Implementation of ex::normal() for a product. It cancels common factors
1965 * @see ex::normal() */
1966 ex mul::normal(lst &sym_lst, lst &repl_lst, int level) const
1969 return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
1970 else if (level == -max_recursion_level)
1971 throw(std::runtime_error("max recursion level reached"));
1973 // Normalize children, separate into numerator and denominator
1977 epvector::const_iterator it = seq.begin(), itend = seq.end();
1978 while (it != itend) {
1979 n = recombine_pair_to_ex(*it).bp->normal(sym_lst, repl_lst, level-1);
1984 n = overall_coeff.bp->normal(sym_lst, repl_lst, level-1);
1988 // Perform fraction cancellation
1989 return frac_cancel(num, den);
1993 /** Implementation of ex::normal() for powers. It normalizes the basis,
1994 * distributes integer exponents to numerator and denominator, and replaces
1995 * non-integer powers by temporary symbols.
1996 * @see ex::normal */
1997 ex power::normal(lst &sym_lst, lst &repl_lst, int level) const
2000 return (new lst(replace_with_symbol(*this, sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
2001 else if (level == -max_recursion_level)
2002 throw(std::runtime_error("max recursion level reached"));
2004 // Normalize basis and exponent (exponent gets reassembled)
2005 ex n_basis = basis.bp->normal(sym_lst, repl_lst, level-1);
2006 ex n_exponent = exponent.bp->normal(sym_lst, repl_lst, level-1);
2007 n_exponent = n_exponent.op(0) / n_exponent.op(1);
2009 if (n_exponent.info(info_flags::integer)) {
2011 if (n_exponent.info(info_flags::positive)) {
2013 // (a/b)^n -> {a^n, b^n}
2014 return (new lst(power(n_basis.op(0), n_exponent), power(n_basis.op(1), n_exponent)))->setflag(status_flags::dynallocated);
2016 } else if (n_exponent.info(info_flags::negative)) {
2018 // (a/b)^-n -> {b^n, a^n}
2019 return (new lst(power(n_basis.op(1), -n_exponent), power(n_basis.op(0), -n_exponent)))->setflag(status_flags::dynallocated);
2024 if (n_exponent.info(info_flags::positive)) {
2026 // (a/b)^x -> {sym((a/b)^x), 1}
2027 return (new lst(replace_with_symbol(power(n_basis.op(0) / n_basis.op(1), n_exponent), sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
2029 } else if (n_exponent.info(info_flags::negative)) {
2031 if (n_basis.op(1).is_equal(_ex1())) {
2033 // a^-x -> {1, sym(a^x)}
2034 return (new lst(_ex1(), replace_with_symbol(power(n_basis.op(0), -n_exponent), sym_lst, repl_lst)))->setflag(status_flags::dynallocated);
2038 // (a/b)^-x -> {sym((b/a)^x), 1}
2039 return (new lst(replace_with_symbol(power(n_basis.op(1) / n_basis.op(0), -n_exponent), sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
2042 } else { // n_exponent not numeric
2044 // (a/b)^x -> {sym((a/b)^x, 1}
2045 return (new lst(replace_with_symbol(power(n_basis.op(0) / n_basis.op(1), n_exponent), sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
2051 /** Implementation of ex::normal() for pseries. It normalizes each coefficient
2052 * and replaces the series by a temporary symbol.
2053 * @see ex::normal */
2054 ex pseries::normal(lst &sym_lst, lst &repl_lst, int level) const
2057 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
2058 ex restexp = i->rest.normal();
2059 if (!restexp.is_zero())
2060 newseq.push_back(expair(restexp, i->coeff));
2062 ex n = pseries(relational(var,point), newseq);
2063 return (new lst(replace_with_symbol(n, sym_lst, repl_lst), _ex1()))->setflag(status_flags::dynallocated);
2067 /** Implementation of ex::normal() for relationals. It normalizes both sides.
2068 * @see ex::normal */
2069 ex relational::normal(lst &sym_lst, lst &repl_lst, int level) const
2071 return (new lst(relational(lh.normal(), rh.normal(), o), _ex1()))->setflag(status_flags::dynallocated);
2075 /** Normalization of rational functions.
2076 * This function converts an expression to its normal form
2077 * "numerator/denominator", where numerator and denominator are (relatively
2078 * prime) polynomials. Any subexpressions which are not rational functions
2079 * (like non-rational numbers, non-integer powers or functions like sin(),
2080 * cos() etc.) are replaced by temporary symbols which are re-substituted by
2081 * the (normalized) subexpressions before normal() returns (this way, any
2082 * expression can be treated as a rational function). normal() is applied
2083 * recursively to arguments of functions etc.
2085 * @param level maximum depth of recursion
2086 * @return normalized expression */
2087 ex ex::normal(int level) const
2089 lst sym_lst, repl_lst;
2091 ex e = bp->normal(sym_lst, repl_lst, level);
2092 GINAC_ASSERT(is_ex_of_type(e, lst));
2094 // Re-insert replaced symbols
2095 if (sym_lst.nops() > 0)
2096 e = e.subs(sym_lst, repl_lst);
2098 // Convert {numerator, denominator} form back to fraction
2099 return e.op(0) / e.op(1);
2102 /** Numerator of an expression. If the expression is not of the normal form
2103 * "numerator/denominator", it is first converted to this form and then the
2104 * numerator is returned.
2107 * @return numerator */
2108 ex ex::numer(void) const
2110 lst sym_lst, repl_lst;
2112 ex e = bp->normal(sym_lst, repl_lst, 0);
2113 GINAC_ASSERT(is_ex_of_type(e, lst));
2115 // Re-insert replaced symbols
2116 if (sym_lst.nops() > 0)
2117 return e.op(0).subs(sym_lst, repl_lst);
2122 /** Denominator of an expression. If the expression is not of the normal form
2123 * "numerator/denominator", it is first converted to this form and then the
2124 * denominator is returned.
2127 * @return denominator */
2128 ex ex::denom(void) const
2130 lst sym_lst, repl_lst;
2132 ex e = bp->normal(sym_lst, repl_lst, 0);
2133 GINAC_ASSERT(is_ex_of_type(e, lst));
2135 // Re-insert replaced symbols
2136 if (sym_lst.nops() > 0)
2137 return e.op(1).subs(sym_lst, repl_lst);
2143 /** Default implementation of ex::to_rational(). It replaces the object with a
2145 * @see ex::to_rational */
2146 ex basic::to_rational(lst &repl_lst) const
2148 return replace_with_symbol(*this, repl_lst);
2152 /** Implementation of ex::to_rational() for symbols. This returns the
2153 * unmodified symbol.
2154 * @see ex::to_rational */
2155 ex symbol::to_rational(lst &repl_lst) const
2161 /** Implementation of ex::to_rational() for a numeric. It splits complex
2162 * numbers into re+I*im and replaces I and non-rational real numbers with a
2164 * @see ex::to_rational */
2165 ex numeric::to_rational(lst &repl_lst) const
2169 return replace_with_symbol(*this, repl_lst);
2171 numeric re = real();
2172 numeric im = imag();
2173 ex re_ex = re.is_rational() ? re : replace_with_symbol(re, repl_lst);
2174 ex im_ex = im.is_rational() ? im : replace_with_symbol(im, repl_lst);
2175 return re_ex + im_ex * replace_with_symbol(I, repl_lst);
2181 /** Implementation of ex::to_rational() for powers. It replaces non-integer
2182 * powers by temporary symbols.
2183 * @see ex::to_rational */
2184 ex power::to_rational(lst &repl_lst) const
2186 if (exponent.info(info_flags::integer))
2187 return power(basis.to_rational(repl_lst), exponent);
2189 return replace_with_symbol(*this, repl_lst);
2193 /** Implementation of ex::to_rational() for expairseqs.
2194 * @see ex::to_rational */
2195 ex expairseq::to_rational(lst &repl_lst) const
2198 s.reserve(seq.size());
2199 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
2200 s.push_back(split_ex_to_pair(recombine_pair_to_ex(*it).to_rational(repl_lst)));
2201 // s.push_back(combine_ex_with_coeff_to_pair((*it).rest.to_rational(repl_lst),
2203 ex oc = overall_coeff.to_rational(repl_lst);
2204 if (oc.info(info_flags::numeric))
2205 return thisexpairseq(s, overall_coeff);
2206 else s.push_back(combine_ex_with_coeff_to_pair(oc,_ex1()));
2207 return thisexpairseq(s, default_overall_coeff());
2211 /** Rationalization of non-rational functions.
2212 * This function converts a general expression to a rational polynomial
2213 * by replacing all non-rational subexpressions (like non-rational numbers,
2214 * non-integer powers or functions like sin(), cos() etc.) to temporary
2215 * symbols. This makes it possible to use functions like gcd() and divide()
2216 * on non-rational functions by applying to_rational() on the arguments,
2217 * calling the desired function and re-substituting the temporary symbols
2218 * in the result. To make the last step possible, all temporary symbols and
2219 * their associated expressions are collected in the list specified by the
2220 * repl_lst parameter in the form {symbol == expression}, ready to be passed
2221 * as an argument to ex::subs().
2223 * @param repl_lst collects a list of all temporary symbols and their replacements
2224 * @return rationalized expression */
2225 ex ex::to_rational(lst &repl_lst) const
2227 return bp->to_rational(repl_lst);
2231 } // namespace GiNaC