Comments (5)
fraction
has the ability to disregard the sign of exponents; the default is False so the factors that you want to combine don't both appear in the numerator.
If this exact
capability were extended to numer
, denom
and numer_expand
and denom_expand
then you would be able to do expand_numer
on your expression and get the desired result. (These routines are in simplify.radsimp.)
def numer(expr, exact=False):
return fraction(expr)[0]
def denom(expr, exact=False):
return fraction(expr)[1]
def numer_expand(expr, **hints):
a, b = fraction(expr, hints.get('exact', False))
return a.expand(numer=True, **hints) / b
def denom_expand(expr, **hints):
a, b = fraction(expr, hints.get('exact', False))
return a / b.expand(denom=True, **hints)
A check would also be needed to see that when fraction
was being called from withing the expand
routines that the exact
flag was being passed.
The current workaround is
def nexpand(eq, exact=True, **hints):
n, d = fraction(eq, exact=exact)
return n.expand(**hints)/d
def dexpand(eq, exact=True, **hints):
n, d = fraction(eq, exact=exact)
return n/d.expand(**hints)
assert nexpand(myexp) == simplify(myexp*(k01 - k11))/(k01 - k11)
from sympy.
I think often the awkwardness in simplifying things like this comes from treating negative exponentials as part of the denominator e.g.:
In [12]: from sympy import *
...: t, k01, k11 = symbols('t k01 k11', real=True)
...: myexp = (exp(k01*t) - exp(k11*t))*exp(-t*(k01 + k11))/(k01 - k11)
In [13]: myexp
Out[13]:
⎛ k₀₁⋅t k₁₁⋅t⎞ -t⋅(k₀₁ + k₁₁)
⎝ℯ - ℯ ⎠⋅ℯ
─────────────────────────────────
k₀₁ - k₁₁
In [14]: myexp.expand()
Out[14]:
1 1
─────────────────────── - ───────────────────────
k₁₁⋅t k₁₁⋅t k₀₁⋅t k₀₁⋅t
k₀₁⋅ℯ - k₁₁⋅ℯ k₀₁⋅ℯ - k₁₁⋅ℯ
Analogously:
In [15]: myexp = (x - y)/((x*y)*(z - t))
In [16]: myexp
Out[16]:
x - y
────────────
x⋅y⋅(-t + z)
In [17]: myexp.expand()
Out[17]:
x y
────────────── - ──────────────
-t⋅x⋅y + x⋅y⋅z -t⋅x⋅y + x⋅y⋅z
In [18]: simplify(_)
Out[18]:
-x + y
───────────
x⋅y⋅(t - z)
The hoped for simplification is like wanting this to simplify to 1/y - 1/x
like expand
does:
In [19]: simplify((x - y)/(x*y))
Out[19]:
x - y
─────
x⋅y
In [20]: expand((x - y)/(x*y))
Out[20]:
1 1
─ - ─
y x
I'm not quite sure why expand
does not work out like that with the exponentials.
I have wondered whether this should be changed:
In [26]: myexp
Out[26]:
⎛ k₀₁⋅t k₁₁⋅t⎞ -t⋅(k₀₁ + k₁₁)
⎝ℯ - ℯ ⎠⋅ℯ
─────────────────────────────────
k₀₁ - k₁₁
In [27]: myexp.as_numer_denom()
Out[27]:
⎛ k₀₁⋅t k₁₁⋅t t⋅(k₀₁ + k₁₁)⎞
⎝ℯ - ℯ , (k₀₁ - k₁₁)⋅ℯ ⎠
from sympy.
fraction
is supposed to be more literal than as_numer_denom
. Perhaps it would have been better to default to exact=True
to make it even more literal. I suspect that changing this default would not be too impactful. What do others think?
from sympy.
If you think of a different idiom for the simplification, it can also work:
>>> factor_terms(myexp.expand())
(exp(-k11*t) - exp(-k01*t))/(k01 - k11)
From #26347 :
When SymPy works with factors that are powers with a leading negative coefficient, those are displayed in numerator instead of the denominator, but fraction (by default) puts them in the denominator. So (exp(-x)/x+exp(-y)/x) is treated like 1/(xexp(x)) + 1/(xexp(y)) when bringing the fraction together and a more complicated fraction results:
>>> (exp(-x)/x+exp(-y)/x).normal()
(x*exp(x) + x*exp(y))*exp(-x)*exp(-y)/x**2
If, factor_terms is used instead, a more-expected form results:
>>> factor_terms(exp(-x)/x+exp(-y)/x)
(exp(-y) + exp(-x))/x
Allowing expand to take the exact hint is a sort of work-around for working with such expressions.
from sympy.
Leaving this open because it would be nice if simplify
tested a factor_terms(expr)
against expr.normal()
(or equivalent) when seeking the smallest expression. using expr1 = shorter(_e, (_ex:=_mexpand(_e)).cancel(), factor_terms(_ex)) # issue 6829
in simplify
solves this issue, but (while making expressions a little shorter) it breaks several tests and one would have to evaluate if that is the right place to make the change.
from sympy.
Related Issues (20)
- RecursionError in simplify of non-commutative expressions with non-integer coefficient HOT 1
- Wrong answer calculating the inverse Laplace transform of a function HOT 1
- Derivative is incorrectly set to 0 for some summation expressions. HOT 2
- Rust Codegen doesn't generate public functions HOT 3
- coeff for non-integer powers fails
- wrong result integrating sin(a*(x+pi))**2 HOT 5
- Improving Beam Class to Handle Hinge Beams Using Singularity Functions to Describe Hinges
- integrate((x**8+1)**(-1/2),x) raises _CoeffExpValueError HOT 4
- Ideal membership testing raises `ValueError` HOT 5
- factor should use the same domain for numerator and denominator
- sympy factor function produces wrong result HOT 3
- sympy.integrate gives inconsistent results on a mathematically equivalent expression HOT 2
- derivative answer not correct HOT 3
- Deriving a sum of two matrix norms erroneously returns 0
- Manual Integrate cannot do (1/cos(x)) dx HOT 5
- Accessing Matrix row/col ranges produces incorrect results HOT 4
- inconsistent treatment of hyperbolic functions with simplify HOT 2
- SymPy gives a wrong solution for an ODE of first order HOT 5
- dsolve fails: Initial conditions produced too many solutions for constants
- Conversion from Sage matrix to sympy matrix fails for matrices with rows full of zeros HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sympy.