>From 0a58e91cc7922036f3a121eb2204df54ef726bac Mon Sep 17 00:00:00 2001
From: "Vladimir V. Kisil" <V.Kisilv@leeds.ac.uk>
Date: Fri, 10 Apr 2020 07:22:57 +0100
Subject: [PATCH] Make a stronger normalisation for expressions with exponents

If several exponents have arguments different by a rational number
factors they are replaced by monomials of the same variable.

Signed-off-by: Vladimir V. Kisil <V.Kisilv@leeds.ac.uk>
---
 ginac/add.h       |   2 +-
 ginac/basic.h     |   2 +-
 ginac/mul.h       |   2 +-
 ginac/normal.cpp  | 153 +++++++++++++++++++++++++++++++++++-----------
 ginac/numeric.h   |   2 +-
 ginac/power.h     |   2 +-
 ginac/pseries.h   |   2 +-
 ginac/structure.h |   2 +-
 ginac/symbol.h    |   2 +-
 9 files changed, 127 insertions(+), 42 deletions(-)

diff --git a/ginac/add.h b/ginac/add.h
index 3e07b2a6..f00facfa 100644
--- a/ginac/add.h
+++ b/ginac/add.h
@@ -55,7 +55,7 @@ public:
 	ex eval() const override;
 	ex evalm() const override;
 	ex series(const relational & r, int order, unsigned options = 0) const override;
-	ex normal(exmap & repl, exmap & rev_lookup) const override;
+	ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override;
 	numeric integer_content() const override;
 	ex smod(const numeric &xi) const override;
 	numeric max_coefficient() const override;
diff --git a/ginac/basic.h b/ginac/basic.h
index fd6a7471..8bfdf3a6 100644
--- a/ginac/basic.h
+++ b/ginac/basic.h
@@ -200,7 +200,7 @@ public:
 	virtual ex series(const relational & r, int order, unsigned options = 0) const;
 
 	// rational functions
-	virtual ex normal(exmap & repl, exmap & rev_lookup) const;
+	virtual ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const;
 	virtual ex to_rational(exmap & repl) const;
 	virtual ex to_polynomial(exmap & repl) const;
 
diff --git a/ginac/mul.h b/ginac/mul.h
index 60d56e02..cdaef5fe 100644
--- a/ginac/mul.h
+++ b/ginac/mul.h
@@ -61,7 +61,7 @@ public:
 	ex imag_part() const override;
 	ex evalm() const override;
 	ex series(const relational & s, int order, unsigned options = 0) const override;
-	ex normal(exmap & repl, exmap & rev_lookup) const override;
+	ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override;
 	numeric integer_content() const override;
 	ex smod(const numeric &xi) const override;
 	numeric max_coefficient() const override;
diff --git a/ginac/normal.cpp b/ginac/normal.cpp
index a80e1508..ee8f781d 100644
--- a/ginac/normal.cpp
+++ b/ginac/normal.cpp
@@ -2007,8 +2007,12 @@ ex sqrfree_parfrac(const ex & a, const symbol & x)
 /** Create a symbol for replacing the expression "e" (or return a previously
  *  assigned symbol). The symbol and expression are appended to repl, for
  *  a later application of subs().
+ *  An entry in the replacement table repl can be changed in some case.
+ *  If it was altered, we need to provide the modifier for the previously build expression.
+ *  The modifier is a list, because those substitutions need to be done in in the
+ *  incremental order.
  *  @see ex::normal */
-static ex replace_with_symbol(const ex & e, exmap & repl, exmap & rev_lookup)
+static ex replace_with_symbol(const ex & e, exmap & repl, exmap & rev_lookup, lst & modifier)
 {
 	// Since the repl contains replaced expressions we should search for them
 	ex e_replaced = e.subs(repl, subs_options::no_pattern);
@@ -2018,6 +2022,38 @@ static ex replace_with_symbol(const ex & e, exmap & repl, exmap & rev_lookup)
 	if (it != rev_lookup.end())
 		return it->second;
 
+	// We treat powers and the exponent functions differently because
+	// they can be rationalised more efficiently
+	if (is_a<function>(e_replaced) && ex_to<function>(e_replaced).get_name() == "exp") {
+		for (auto & it : repl) {
+			if (is_a<function>(it.second) && ex_to<function>(it.second).get_name() == "exp") {
+				ex ratio =  normal(e_replaced.op(0)/it.second.op(0)) ;
+				if (is_a<numeric>(ratio) && ex_to<numeric>(ratio).is_rational())  {
+					// Different exponents can be treated as powers of the same basic equation
+					if (ex_to<numeric>(ratio).is_integer()) {
+						// If ratio is an integer then this is simply the power of the existing symbol.
+						// std::clog << e_replaced << " is a " << ratio << " power of " << it.first << std::endl;
+						return pow(it.first, ratio);
+					} else {
+						// otherwise we need to give the replacement pattern to change
+						// the previous expression...
+						ex es = dynallocate<symbol>();
+						modifier.append(it.first == power(es, denom(ratio)));
+						// ... and  modify the replacement tables
+						rev_lookup.erase(it.second);
+						rev_lookup.insert(std::make_pair(exp(e_replaced.op(0)/numer(ratio)), es));
+						repl.erase(it.first);
+						repl.insert(std::make_pair(es, exp(e_replaced.op(0)/numer(ratio))));
+						// std::clog << e_replaced << " is a " << numer(ratio) << " power and "
+						//		  << it.first << " is a " << denom(ratio) << " power of the common base "
+						//		  << exp(e_replaced.op(0)/numer(ratio)) << std::endl;
+						return power(es, numer(ratio));
+					}
+				}
+			}
+		}
+	}
+
 	// Otherwise create new symbol and add to list, taking care that the
 	// replacement expression doesn't itself contain symbols from repl,
 	// because subs() is not recursive
@@ -2059,19 +2095,27 @@ struct normal_map_function : public map_function {
 /** Default implementation of ex::normal(). It normalizes the children and
  *  replaces the object with a temporary symbol.
  *  @see ex::normal */
-ex basic::normal(exmap & repl, exmap & rev_lookup) const
+ex basic::normal(exmap & repl, exmap & rev_lookup, lst & modifier) const
 {
 	if (nops() == 0)
-		return dynallocate<lst>({replace_with_symbol(*this, repl, rev_lookup), _ex1});
+		return dynallocate<lst>({replace_with_symbol(*this, repl, rev_lookup, modifier), _ex1});
 
 	normal_map_function map_normal;
-	return dynallocate<lst>({replace_with_symbol(map(map_normal), repl, rev_lookup), _ex1});
+	int nmod = modifier.nops(); // To watch new modifiers to the replacement list
+	lst result = dynallocate<lst>({replace_with_symbol(map(map_normal), repl, rev_lookup, modifier), _ex1});
+	for (int imod = nmod; imod < modifier.nops(); ++imod) {
+		exmap this_repl;
+		this_repl.insert(std::make_pair(modifier.op(imod).op(0), modifier.op(imod).op(1)));
+		result = ex_to<lst>(result.subs(this_repl, subs_options::no_pattern));
+	}
+
+	return result;
 }
 
 
 /** Implementation of ex::normal() for symbols. This returns the unmodified symbol.
  *  @see ex::normal */
-ex symbol::normal(exmap & repl, exmap & rev_lookup) const
+ex symbol::normal(exmap & repl, exmap & rev_lookup, lst & modifier) const
 {
 	return dynallocate<lst>({*this, _ex1});
 }
@@ -2081,19 +2125,19 @@ ex symbol::normal(exmap & repl, exmap & rev_lookup) const
  *  into re+I*im and replaces I and non-rational real numbers with a temporary
  *  symbol.
  *  @see ex::normal */
-ex numeric::normal(exmap & repl, exmap & rev_lookup) const
+ex numeric::normal(exmap & repl, exmap & rev_lookup, lst & modifier) const
 {
 	numeric num = numer();
 	ex numex = num;
 
 	if (num.is_real()) {
 		if (!num.is_integer())
-			numex = replace_with_symbol(numex, repl, rev_lookup);
+			numex = replace_with_symbol(numex, repl, rev_lookup, modifier);
 	} else { // complex
 		numeric re = num.real(), im = num.imag();
-		ex re_ex = re.is_rational() ? re : replace_with_symbol(re, repl, rev_lookup);
-		ex im_ex = im.is_rational() ? im : replace_with_symbol(im, repl, rev_lookup);
-		numex = re_ex + im_ex * replace_with_symbol(I, repl, rev_lookup);
+		ex re_ex = re.is_rational() ? re : replace_with_symbol(re, repl, rev_lookup, modifier);
+		ex im_ex = im.is_rational() ? im : replace_with_symbol(im, repl, rev_lookup, modifier);
+		numex = re_ex + im_ex * replace_with_symbol(I, repl, rev_lookup, modifier);
 	}
 
 	// Denominator is always a real integer (see numeric::denom())
@@ -2165,18 +2209,19 @@ static ex frac_cancel(const ex &n, const ex &d)
 /** Implementation of ex::normal() for a sum. It expands terms and performs
  *  fractional addition.
  *  @see ex::normal */
-ex add::normal(exmap & repl, exmap & rev_lookup) const
+ex add::normal(exmap & repl, exmap & rev_lookup, lst & modifier) const
 {
 	// Normalize children and split each one into numerator and denominator
 	exvector nums, dens;
 	nums.reserve(seq.size()+1);
 	dens.reserve(seq.size()+1);
+	int nmod = modifier.nops(); // To watch new modifiers to the replacement list
 	for (auto & it : seq) {
-		ex n = ex_to<basic>(recombine_pair_to_ex(it)).normal(repl, rev_lookup);
+		ex n = ex_to<basic>(recombine_pair_to_ex(it)).normal(repl, rev_lookup, modifier);
 		nums.push_back(n.op(0));
 		dens.push_back(n.op(1));
 	}
-	ex n = ex_to<numeric>(overall_coeff).normal(repl, rev_lookup);
+	ex n = ex_to<numeric>(overall_coeff).normal(repl, rev_lookup, modifier);
 	nums.push_back(n.op(0));
 	dens.push_back(n.op(1));
 	GINAC_ASSERT(nums.size() == dens.size());
@@ -2189,6 +2234,17 @@ ex add::normal(exmap & repl, exmap & rev_lookup) const
 	auto num_it = nums.begin(), num_itend = nums.end();
 	auto den_it = dens.begin(), den_itend = dens.end();
 //std::clog << " num = " << *num_it << ", den = " << *den_it << std::endl;
+	for (int imod = nmod; imod < modifier.nops(); ++imod)
+		while (num_it != num_itend) {
+			*num_it = num_it->subs(modifier.op(imod), subs_options::no_pattern);
+			++num_it;
+			*den_it = den_it->subs(modifier.op(imod), subs_options::no_pattern);
+			++den_it;
+		}
+
+	num_it = nums.begin();
+	den_it = dens.begin();
+
 	ex num = *num_it++, den = *den_it++;
 	while (num_it != num_itend) {
 //std::clog << " num = " << *num_it << ", den = " << *den_it << std::endl;
@@ -2217,35 +2273,45 @@ ex add::normal(exmap & repl, exmap & rev_lookup) const
 /** Implementation of ex::normal() for a product. It cancels common factors
  *  from fractions.
  *  @see ex::normal() */
-ex mul::normal(exmap & repl, exmap & rev_lookup) const
+ex mul::normal(exmap & repl, exmap & rev_lookup, lst & modifier) const
 {
 	// Normalize children, separate into numerator and denominator
 	exvector num; num.reserve(seq.size());
 	exvector den; den.reserve(seq.size());
 	ex n;
+	int nmod = modifier.nops(); // To watch new modifiers to the replacement list
 	for (auto & it : seq) {
-		n = ex_to<basic>(recombine_pair_to_ex(it)).normal(repl, rev_lookup);
+		n = ex_to<basic>(recombine_pair_to_ex(it)).normal(repl, rev_lookup, modifier);
 		num.push_back(n.op(0));
 		den.push_back(n.op(1));
 	}
-	n = ex_to<numeric>(overall_coeff).normal(repl, rev_lookup);
+	n = ex_to<numeric>(overall_coeff).normal(repl, rev_lookup, modifier);
 	num.push_back(n.op(0));
 	den.push_back(n.op(1));
+	auto num_it = num.begin(), num_itend = num.end();
+	auto den_it = den.begin(), den_itend = den.end();
+	for (int imod = nmod; imod < modifier.nops(); ++imod)
+		while (num_it != num_itend) {
+			*num_it = num_it->subs(modifier.op(imod), subs_options::no_pattern);
+			++num_it;
+			*den_it = den_it->subs(modifier.op(imod), subs_options::no_pattern);
+			++den_it;
+		}
 
 	// Perform fraction cancellation
 	return frac_cancel(dynallocate<mul>(num), dynallocate<mul>(den));
 }
 
 
-/** Implementation of ex::normal([B) for powers. It normalizes the basis,
+/** Implementation of ex::normal() for powers. It normalizes the basis,
  *  distributes integer exponents to numerator and denominator, and replaces
  *  non-integer powers by temporary symbols.
  *  @see ex::normal */
-ex power::normal(exmap & repl, exmap & rev_lookup) const
+ex power::normal(exmap & repl, exmap & rev_lookup, lst & modifier) const
 {
 	// Normalize basis and exponent (exponent gets reassembled)
-	ex n_basis = ex_to<basic>(basis).normal(repl, rev_lookup);
-	ex n_exponent = ex_to<basic>(exponent).normal(repl, rev_lookup);
+	ex n_basis = ex_to<basic>(basis).normal(repl, rev_lookup, modifier);
+	ex n_exponent = ex_to<basic>(exponent).normal(repl, rev_lookup, modifier);
 	n_exponent = n_exponent.op(0) / n_exponent.op(1);
 
 	if (n_exponent.info(info_flags::integer)) {
@@ -2266,32 +2332,32 @@ ex power::normal(exmap & repl, exmap & rev_lookup) const
 		if (n_exponent.info(info_flags::positive)) {
 
 			// (a/b)^x -> {sym((a/b)^x), 1}
-			return dynallocate<lst>({replace_with_symbol(pow(n_basis.op(0) / n_basis.op(1), n_exponent), repl, rev_lookup), _ex1});
+			return dynallocate<lst>({replace_with_symbol(pow(n_basis.op(0) / n_basis.op(1), n_exponent), repl, rev_lookup, modifier), _ex1});
 
 		} else if (n_exponent.info(info_flags::negative)) {
 
 			if (n_basis.op(1).is_equal(_ex1)) {
 
 				// a^-x -> {1, sym(a^x)}
-				return dynallocate<lst>({_ex1, replace_with_symbol(pow(n_basis.op(0), -n_exponent), repl, rev_lookup)});
+				return dynallocate<lst>({_ex1, replace_with_symbol(pow(n_basis.op(0), -n_exponent), repl, rev_lookup, modifier)});
 
 			} else {
 
 				// (a/b)^-x -> {sym((b/a)^x), 1}
-				return dynallocate<lst>({replace_with_symbol(pow(n_basis.op(1) / n_basis.op(0), -n_exponent), repl, rev_lookup), _ex1});
+				return dynallocate<lst>({replace_with_symbol(pow(n_basis.op(1) / n_basis.op(0), -n_exponent), repl, rev_lookup, modifier), _ex1});
 			}
 		}
 	}
 
 	// (a/b)^x -> {sym((a/b)^x, 1}
-	return dynallocate<lst>({replace_with_symbol(pow(n_basis.op(0) / n_basis.op(1), n_exponent), repl, rev_lookup), _ex1});
+	return dynallocate<lst>({replace_with_symbol(pow(n_basis.op(0) / n_basis.op(1), n_exponent), repl, rev_lookup, modifier), _ex1});
 }
 
 
 /** Implementation of ex::normal() for pseries. It normalizes each coefficient
  *  and replaces the series by a temporary symbol.
  *  @see ex::normal */
-ex pseries::normal(exmap & repl, exmap & rev_lookup) const
+ex pseries::normal(exmap & repl, exmap & rev_lookup, lst & modifier) const
 {
 	epvector newseq;
 	for (auto & it : seq) {
@@ -2300,7 +2366,7 @@ ex pseries::normal(exmap & repl, exmap & rev_lookup) const
 			newseq.push_back(expair(restexp, it.coeff));
 	}
 	ex n = pseries(relational(var,point), std::move(newseq));
-	return dynallocate<lst>({replace_with_symbol(n, repl, rev_lookup), _ex1});
+	return dynallocate<lst>({replace_with_symbol(n, repl, rev_lookup, modifier), _ex1});
 }
 
 
@@ -2318,13 +2384,17 @@ ex pseries::normal(exmap & repl, exmap & rev_lookup) const
 ex ex::normal() const
 {
 	exmap repl, rev_lookup;
+	lst modifier;
 
-	ex e = bp->normal(repl, rev_lookup);
+	ex e = bp->normal(repl, rev_lookup, modifier);
 	GINAC_ASSERT(is_a<lst>(e));
 
 	// Re-insert replaced symbols
-	if (!repl.empty())
+	if (!repl.empty()) {
+		for(int i=0; i < modifier.nops(); ++i)
+			e = e.subs(modifier.op(i), subs_options::no_pattern);
 		e = e.subs(repl, subs_options::no_pattern);
+	}
 
 	// Convert {numerator, denominator} form back to fraction
 	return e.op(0) / e.op(1);
@@ -2339,15 +2409,20 @@ ex ex::normal() const
 ex ex::numer() const
 {
 	exmap repl, rev_lookup;
+	lst modifier;
 
-	ex e = bp->normal(repl, rev_lookup);
+	ex e = bp->normal(repl, rev_lookup, modifier);
 	GINAC_ASSERT(is_a<lst>(e));
 
 	// Re-insert replaced symbols
 	if (repl.empty())
 		return e.op(0);
-	else
+	else {
+		for(int i=0; i < modifier.nops(); ++i)
+			e = e.subs(modifier.op(i), subs_options::no_pattern);
+
 		return e.op(0).subs(repl, subs_options::no_pattern);
+	}
 }
 
 /** Get denominator of an expression. If the expression is not of the normal
@@ -2359,15 +2434,20 @@ ex ex::numer() const
 ex ex::denom() const
 {
 	exmap repl, rev_lookup;
+	lst modifier;
 
-	ex e = bp->normal(repl, rev_lookup);
+	ex e = bp->normal(repl, rev_lookup, modifier);
 	GINAC_ASSERT(is_a<lst>(e));
 
 	// Re-insert replaced symbols
 	if (repl.empty())
 		return e.op(1);
-	else
+	else {
+		for(int i=0; i < modifier.nops(); ++i)
+			e = e.subs(modifier.op(i), subs_options::no_pattern);
+
 		return e.op(1).subs(repl, subs_options::no_pattern);
+	}
 }
 
 /** Get numerator and denominator of an expression. If the expression is not
@@ -2379,15 +2459,20 @@ ex ex::denom() const
 ex ex::numer_denom() const
 {
 	exmap repl, rev_lookup;
+	lst modifier;
 
-	ex e = bp->normal(repl, rev_lookup);
+	ex e = bp->normal(repl, rev_lookup, modifier);
 	GINAC_ASSERT(is_a<lst>(e));
 
 	// Re-insert replaced symbols
 	if (repl.empty())
 		return e;
-	else
+	else {
+		for(int i=0; i < modifier.nops(); ++i)
+			e = e.subs(modifier.op(i), subs_options::no_pattern);
+
 		return e.subs(repl, subs_options::no_pattern);
+	}
 }
 
 
diff --git a/ginac/numeric.h b/ginac/numeric.h
index a2933342..975da7a0 100644
--- a/ginac/numeric.h
+++ b/ginac/numeric.h
@@ -108,7 +108,7 @@ public:
 	ex eval() const override;
 	ex evalf() const override;
 	ex subs(const exmap & m, unsigned options = 0) const override { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
-	ex normal(exmap & repl, exmap & rev_lookup) const override;
+	ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override;
 	ex to_rational(exmap & repl) const override;
 	ex to_polynomial(exmap & repl) const override;
 	numeric integer_content() const override;
diff --git a/ginac/power.h b/ginac/power.h
index 5ca4fbe1..f431b7c2 100644
--- a/ginac/power.h
+++ b/ginac/power.h
@@ -65,7 +65,7 @@ public:
 	ex series(const relational & s, int order, unsigned options = 0) const override;
 	ex subs(const exmap & m, unsigned options = 0) const override;
 	bool has(const ex & other, unsigned options = 0) const override;
-	ex normal(exmap & repl, exmap & rev_lookup) const override;
+	ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override;
 	ex to_rational(exmap & repl) const override;
 	ex to_polynomial(exmap & repl) const override;
 	ex conjugate() const override;
diff --git a/ginac/pseries.h b/ginac/pseries.h
index 9300662f..2857bb7e 100644
--- a/ginac/pseries.h
+++ b/ginac/pseries.h
@@ -54,7 +54,7 @@ public:
 	ex evalf() const override;
 	ex series(const relational & r, int order, unsigned options = 0) const override;
 	ex subs(const exmap & m, unsigned options = 0) const override;
-	ex normal(exmap & repl, exmap & rev_lookup) const override;
+	ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override;
 	ex expand(unsigned options = 0) const override;
 	ex conjugate() const override;
 	ex real_part() const override;
diff --git a/ginac/structure.h b/ginac/structure.h
index a6b737ea..edd2c048 100644
--- a/ginac/structure.h
+++ b/ginac/structure.h
@@ -178,7 +178,7 @@ public:
 	ex series(const relational & r, int order, unsigned options = 0) const override { return inherited::series(r, order, options); }
 
 	// rational functions
-	ex normal(exmap & repl, exmap & rev_lookup) const override { return inherited::normal(repl, rev_lookup); }
+	ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override { return inherited::normal(repl, rev_lookup, modifier); }
 	ex to_rational(exmap & repl) const override { return inherited::to_rational(repl); }
 	ex to_polynomial(exmap & repl) const override { return inherited::to_polynomial(repl); }
 
diff --git a/ginac/symbol.h b/ginac/symbol.h
index 5cb84c25..c7535190 100644
--- a/ginac/symbol.h
+++ b/ginac/symbol.h
@@ -50,7 +50,7 @@ public:
 	ex evalf() const override { return *this; } // overwrites basic::evalf() for performance reasons
 	ex series(const relational & s, int order, unsigned options = 0) const override;
 	ex subs(const exmap & m, unsigned options = 0) const override { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
-	ex normal(exmap & repl, exmap & rev_lookup) const override;
+	ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override;
 	ex to_rational(exmap & repl) const override;
 	ex to_polynomial(exmap & repl) const override;
 	ex conjugate() const override;
-- 
2.25.1

