]> www.ginac.de Git - ginac.git/commitdiff
added to_polynomial(), to complement to_rational()
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Thu, 30 Jan 2003 22:35:15 +0000 (22:35 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Thu, 30 Jan 2003 22:35:15 +0000 (22:35 +0000)
ginac/basic.h
ginac/ex.h
ginac/expairseq.h
ginac/normal.cpp
ginac/numeric.h
ginac/power.h
ginac/symbol.h

index a4963257d547ca8deb6a4156de79a9ec4eef6b3d..320b247fd7d168587b58f6a5f96fe5b64ae3ecff 100644 (file)
@@ -35,6 +35,7 @@
 namespace GiNaC {
 
 class ex;
+class ex_is_less;
 class symbol;
 class lst;
 class numeric;
@@ -115,6 +116,7 @@ public: // only const functions please (may break reference counting)
        virtual ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
        virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
        virtual ex to_rational(lst &repl_lst) const;
+       virtual ex to_polynomial(lst &repl_lst) const;
        virtual numeric integer_content(void) const;
        virtual ex smod(const numeric &xi) const;
        virtual numeric max_coefficient(void) const;
index 3a455b7cce9110117fc528fd99291ed23658c097..5bb47bf45e2bb0a7e2737231e28be9b3e4cb9047 100644 (file)
@@ -135,7 +135,8 @@ public:
        ex primpart(const symbol &x) const;
        ex primpart(const symbol &x, const ex &cont) const;
        ex normal(int level = 0) const;
-       ex to_rational(lst &repl_lst) const { return bp->to_rational(repl_lst); }
+       ex to_rational(lst &repl_lst) const;
+       ex to_polynomial(lst &repl_lst) const;
        ex smod(const numeric &xi) const { return bp->smod(xi); }
        numeric max_coefficient(void) const;
        ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
@@ -404,6 +405,9 @@ inline ex normal(const ex & thisex, int level=0)
 inline ex to_rational(const ex & thisex, lst & repl_lst)
 { return thisex.to_rational(repl_lst); }
 
+inline ex to_polynomial(const ex & thisex, lst & repl_lst)
+{ return thisex.to_polynomial(repl_lst); }
+
 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
 { return thisex.collect(s, distributed); }
 
index 9fc9edc36ed1ca2ad37bf0672477441d5792e0ba..559ca1e8b7d281f6b281640f024e937ce6cfe550 100644 (file)
@@ -95,6 +95,7 @@ public:
        ex map(map_function & f) const;
        ex eval(int level=0) const;
        ex to_rational(lst &repl_lst) const;
+       ex to_polynomial(lst &repl_lst) const;
        bool match(const ex & pattern, lst & repl_lst) const;
        ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
 protected:
index bcc9ee55fdf0e7310c25c248da0d4afbce260e18..3e28f683219039643b846b4f12587e079da9006b 100644 (file)
@@ -1953,7 +1953,8 @@ static ex replace_with_symbol(const ex &e, lst &sym_lst, lst &repl_lst)
 /** Create a symbol for replacing the expression "e" (or return a previously
  *  assigned symbol). An expression of the form "symbol == expression" is added
  *  to repl_lst and the symbol is returned.
- *  @see basic::to_rational */
+ *  @see basic::to_rational
+ *  @see basic::to_polynomial */
 static ex replace_with_symbol(const ex &e, lst &repl_lst)
 {
        // Expression already in repl_lst? Then return the assigned symbol
@@ -2340,7 +2341,7 @@ ex ex::numer_denom(void) const
 
 
 /** Rationalization of non-rational functions.
- *  This function converts a general expression to a rational polynomial
+ *  This function converts a general expression to a rational function
  *  by replacing all non-rational subexpressions (like non-rational numbers,
  *  non-integer powers or functions like sin(), cos() etc.) to temporary
  *  symbols. This makes it possible to use functions like gcd() and divide()
@@ -2353,11 +2354,29 @@ ex ex::numer_denom(void) const
  *
  *  @param repl_lst collects a list of all temporary symbols and their replacements
  *  @return rationalized expression */
+ex ex::to_rational(lst &repl_lst) const
+{
+       return bp->to_rational(repl_lst);
+}
+
+ex ex::to_polynomial(lst &repl_lst) const
+{
+       return bp->to_polynomial(repl_lst);
+}
+
+
+/** Default implementation of ex::to_rational(). This replaces the object with
+ *  a temporary symbol. */
 ex basic::to_rational(lst &repl_lst) const
 {
        return replace_with_symbol(*this, repl_lst);
 }
 
+ex basic::to_polynomial(lst &repl_lst) const
+{
+       return replace_with_symbol(*this, repl_lst);
+}
+
 
 /** Implementation of ex::to_rational() for symbols. This returns the
  *  unmodified symbol. */
@@ -2366,6 +2385,13 @@ ex symbol::to_rational(lst &repl_lst) const
        return *this;
 }
 
+/** Implementation of ex::to_polynomial() for symbols. This returns the
+ *  unmodified symbol. */
+ex symbol::to_polynomial(lst &repl_lst) const
+{
+       return *this;
+}
+
 
 /** Implementation of ex::to_rational() for a numeric. It splits complex
  *  numbers into re+I*im and replaces I and non-rational real numbers with a
@@ -2385,6 +2411,24 @@ ex numeric::to_rational(lst &repl_lst) const
        return *this;
 }
 
+/** Implementation of ex::to_polynomial() for a numeric. It splits complex
+ *  numbers into re+I*im and replaces I and non-integer real numbers with a
+ *  temporary symbol. */
+ex numeric::to_polynomial(lst &repl_lst) const
+{
+       if (is_real()) {
+               if (!is_integer())
+                       return replace_with_symbol(*this, repl_lst);
+       } else { // complex
+               numeric re = real();
+               numeric im = imag();
+               ex re_ex = re.is_integer() ? re : replace_with_symbol(re, repl_lst);
+               ex im_ex = im.is_integer() ? im : replace_with_symbol(im, repl_lst);
+               return re_ex + im_ex * replace_with_symbol(I, repl_lst);
+       }
+       return *this;
+}
+
 
 /** Implementation of ex::to_rational() for powers. It replaces non-integer
  *  powers by temporary symbols. */
@@ -2396,6 +2440,16 @@ ex power::to_rational(lst &repl_lst) const
                return replace_with_symbol(*this, repl_lst);
 }
 
+/** Implementation of ex::to_polynomial() for powers. It replaces non-posint
+ *  powers by temporary symbols. */
+ex power::to_polynomial(lst &repl_lst) const
+{
+       if (exponent.info(info_flags::posint))
+               return power(basis.to_rational(repl_lst), exponent);
+       else
+               return replace_with_symbol(*this, repl_lst);
+}
+
 
 /** Implementation of ex::to_rational() for expairseqs. */
 ex expairseq::to_rational(lst &repl_lst) const
@@ -2415,6 +2469,24 @@ ex expairseq::to_rational(lst &repl_lst) const
        return thisexpairseq(s, default_overall_coeff());
 }
 
+/** Implementation of ex::to_polynomial() for expairseqs. */
+ex expairseq::to_polynomial(lst &repl_lst) const
+{
+       epvector s;
+       s.reserve(seq.size());
+       epvector::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               s.push_back(split_ex_to_pair(recombine_pair_to_ex(*i).to_polynomial(repl_lst)));
+               ++i;
+       }
+       ex oc = overall_coeff.to_polynomial(repl_lst);
+       if (oc.info(info_flags::numeric))
+               return thisexpairseq(s, overall_coeff);
+       else
+               s.push_back(combine_ex_with_coeff_to_pair(oc, _ex1));
+       return thisexpairseq(s, default_overall_coeff());
+}
+
 
 /** Remove the common factor in the terms of a sum 'e' by calculating the GCD,
  *  and multiply it into the expression 'factor' (which needs to be initialized
@@ -2429,7 +2501,7 @@ static ex find_common_factor(const ex & e, ex & factor, lst & repl)
 
                // Find the common GCD
                for (unsigned i=0; i<num; i++) {
-                       ex x = e.op(i).to_rational(repl);
+                       ex x = e.op(i).to_polynomial(repl);
 
                        if (is_exactly_a<add>(x) || is_exactly_a<mul>(x)) {
                                ex f = 1;
@@ -2491,11 +2563,7 @@ term_done:       ;
 
        } else if (is_exactly_a<power>(e)) {
 
-               ex x = e.to_rational(repl);
-               if (is_exactly_a<power>(x) && x.op(1).info(info_flags::negative))
-                       return replace_with_symbol(x, repl);
-               else
-                       return x;
+               return e.to_polynomial(repl);
 
        } else
                return e;
index 85ad1321e2777f5e70423a8215cc994a63eaa981..e611484e003a1bbf9ef480d5a6407f2e258417f1 100644 (file)
@@ -93,6 +93,7 @@ public:
        ex evalf(int level = 0) const;
        ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
        ex to_rational(lst &repl_lst) const;
+       ex to_polynomial(lst &repl_lst) const;
        numeric integer_content(void) const;
        ex smod(const numeric &xi) const;
        numeric max_coefficient(void) const;
index 5eb1b845206f956b1b94e7a489baeec98af55e9c..6759f597d516550c2519e06a183c9f8e9c913748 100644 (file)
@@ -64,6 +64,7 @@ public:
        ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
        ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
        ex to_rational(lst &repl_lst) const;
+       ex to_polynomial(lst &repl_lst) const;
        exvector get_free_indices(void) const;
 protected:
        ex derivative(const symbol & s) const;
index 5dc869db03f7e1cd91ba9fce88f2be13b8a36a73..52f9ffc3c2eebdb76ad52d0beb867ed730c574a3 100644 (file)
@@ -66,6 +66,7 @@ public:
        ex series(const relational & s, int order, unsigned options = 0) const;
        ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
        ex to_rational(lst &repl_lst) const;
+       ex to_polynomial(lst &repl_lst) const;
 protected:
        ex derivative(const symbol & s) const;
        bool is_equal_same_type(const basic & other) const;