From: Christian Bauer Date: Thu, 6 Apr 2000 19:58:15 +0000 (+0000) Subject: - added ex::to_rational() to convert general expression to rational expression X-Git-Tag: release_0-6-0~28 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=e3830d7f64627be5f6f4f265c0e8effb653be6b9;ds=sidebyside - added ex::to_rational() to convert general expression to rational expression by replacing all non-rational parts with temporary symbols, e.g.: ex a = pow(sin(x), 2) - pow(cos(x), 2); ex b = sin(x) + cos(x); ex d; lst l; divide(a.to_rational(l), b.to_rational(l), d); cout << d.subs(l) << endl; will print "sin(x)-cos(x)" --- diff --git a/ginac/basic.h b/ginac/basic.h index 574fa37c..ed049eaf 100644 --- a/ginac/basic.h +++ b/ginac/basic.h @@ -141,6 +141,7 @@ public: // only const functions please (may break reference counting) virtual ex series(const relational & r, int order) const; virtual ex subs(const lst & ls, const lst & lr) const; virtual ex normal(lst &sym_lst, lst &repl_lst, int level=0) const; + virtual ex to_rational(lst &repl_lst) const; virtual numeric integer_content(void) const; virtual ex smod(const numeric &xi) const; virtual numeric max_coefficient(void) const; diff --git a/ginac/ex.h b/ginac/ex.h index 9b1d0fa1..bf8d0d6f 100644 --- a/ginac/ex.h +++ b/ginac/ex.h @@ -260,6 +260,7 @@ public: ex primpart(const symbol &x, const basic &cont) const { return primpart(x,ex(cont)); } #endif // def CINT_CONVERSION_WORKAROUND ex normal(int level = 0) const; + ex to_rational(lst &repl_lst) const; ex smod(const numeric &xi) const; numeric max_coefficient(void) const; ex collect(const symbol & s) const; @@ -418,6 +419,9 @@ inline ex denom(const ex & thisex) inline ex normal(const ex & thisex, int level=0) { return thisex.normal(level); } +inline ex to_rational(const ex & thisex, lst & repl_lst) +{ return thisex.to_rational(repl_lst); } + inline ex collect(const ex & thisex, const symbol & s) { return thisex.collect(s); } diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp index 5c72362b..d4e13188 100644 --- a/ginac/expairseq.cpp +++ b/ginac/expairseq.cpp @@ -361,6 +361,17 @@ ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const return n.bp->basic::normal(sym_lst,repl_lst,level); } +ex expairseq::to_rational(lst &repl_lst) const +{ + epvector s; + s.reserve(seq.size()); + for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) { + s.push_back(combine_ex_with_coeff_to_pair((*it).rest.to_rational(repl_lst), + (*it).coeff)); + } + return thisexpairseq(s, overall_coeff); +} + ex expairseq::subs(const lst & ls, const lst & lr) const { epvector * vp=subschildren(ls,lr); diff --git a/ginac/expairseq.h b/ginac/expairseq.h index bcd2f66f..6284ce28 100644 --- a/ginac/expairseq.h +++ b/ginac/expairseq.h @@ -102,6 +102,7 @@ public: ex eval(int level=0) const; 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 subs(const lst & ls, const lst & lr) const; protected: ex derivative(const symbol & s) const; diff --git a/ginac/normal.cpp b/ginac/normal.cpp index 7d6ddca7..a4681122 100644 --- a/ginac/normal.cpp +++ b/ginac/normal.cpp @@ -3,8 +3,7 @@ * This file implements several functions that work on univariate and * multivariate polynomials and rational functions. * These functions include polynomial quotient and remainder, GCD and LCM - * computation, square-free factorization and rational function normalization. - */ + * computation, square-free factorization and rational function normalization. */ /* * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany @@ -1460,8 +1459,8 @@ ex sqrfree(const ex &a, const symbol &x) */ /** Create a symbol for replacing the expression "e" (or return a previously - * assigned symbol). The symbol is appended to sym_list and returned, the - * expression is appended to repl_list. + * assigned symbol). The symbol is appended to sym_lst and returned, the + * expression is appended to repl_lst. * @see ex::normal */ static ex replace_with_symbol(const ex &e, lst &sym_lst, lst &repl_lst) { @@ -1481,6 +1480,26 @@ static ex replace_with_symbol(const ex &e, lst &sym_lst, lst &repl_lst) return es; } +/** 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 ex::to_rational */ +static ex replace_with_symbol(const ex &e, lst &repl_lst) +{ + // Expression already in repl_lst? Then return the assigned symbol + for (unsigned i=0; ito_rational(repl_lst); +} + + #ifndef NO_NAMESPACE_GINAC } // namespace GiNaC #endif // ndef NO_NAMESPACE_GINAC diff --git a/ginac/normal.h b/ginac/normal.h index f3e8920d..25a1743b 100644 --- a/ginac/normal.h +++ b/ginac/normal.h @@ -1,7 +1,9 @@ /** @file normal.h * - * Functions for polynomial quotient and remainder, GCD and LCM computation - * and square-free factorization. */ + * This file defines several functions that work on univariate and + * multivariate polynomials and rational functions. + * These functions include polynomial quotient and remainder, GCD and LCM + * computation, square-free factorization and rational function normalization. */ /* * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany diff --git a/ginac/numeric.h b/ginac/numeric.h index 0c729c68..51ee5e8f 100644 --- a/ginac/numeric.h +++ b/ginac/numeric.h @@ -132,6 +132,7 @@ public: ex eval(int level=0) const; 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; numeric integer_content(void) const; ex smod(const numeric &xi) const; numeric max_coefficient(void) const; diff --git a/ginac/power.h b/ginac/power.h index afb5b094..b4faa178 100644 --- a/ginac/power.h +++ b/ginac/power.h @@ -76,6 +76,7 @@ public: ex series(const relational & s, int order) const; ex subs(const lst & ls, const lst & lr) const; ex normal(lst &sym_lst, lst &repl_lst, int level=0) const; + ex to_rational(lst &repl_lst) const; ex simplify_ncmul(const exvector & v) const; protected: ex derivative(const symbol & s) const; diff --git a/ginac/symbol.h b/ginac/symbol.h index 3a81f55d..d2bc6792 100644 --- a/ginac/symbol.h +++ b/ginac/symbol.h @@ -83,6 +83,7 @@ public: ex eval(int level = 0) const; ex series(const relational & s, int order) const; ex normal(lst &sym_lst, lst &repl_lst, int level=0) const; + ex to_rational(lst &repl_lst) const; ex subs(const lst & ls, const lst & lr) const; protected: ex derivative(const symbol & s) const;