GithubHelp home page GithubHelp logo

Comments (5)

smichr avatar smichr commented on June 2, 2024

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.

oscarbenjamin avatar oscarbenjamin commented on June 2, 2024

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
────────────
xy⋅(-t + z)

In [17]: myexp.expand()
Out[17]:
      x                y
────────────── - ──────────────
-txy + xyz   -txy + xyz

In [18]: simplify(_)
Out[18]:
  -x + y
───────────
xy⋅(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
─────
 xy

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.

smichr avatar smichr commented on June 2, 2024

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.

smichr avatar smichr commented on June 2, 2024

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.

smichr avatar smichr commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.