Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor should use the same domain for numerator and denominator #26577

Open
oscarbenjamin opened this issue May 8, 2024 · 0 comments
Open

factor should use the same domain for numerator and denominator #26577

oscarbenjamin opened this issue May 8, 2024 · 0 comments
Labels

Comments

@oscarbenjamin
Copy link
Contributor

After fixing gh-26497 I find the following for the OP example:

In [16]: expr
Out[16]: 
    4  2        4               4           3  2         3                     3           2aₙm     2aₙm     4     aₙ      2aₙm    4aₙm         3   2aₙ    2aₙm    4⋅ ↪
- ─────── - ─────── - aₙ  - ─────── + ────────── + ───────── + 2aₙ  + ─────── + ───────── + ──── ↪
    2         2               2          2            2                    2          2          2aₙ  + 1   aₙ  + 1         aₙ  + 1    aₙ  + 1      aₙ  + 1              aₙ  + 1    aₙ  + 1    aₙ   ↪

↪                              2aₙm            2aₙ      m         2m            1   
↪ ──── + 2aₙ + ─────── + ─────── + ─────── + 1 + ───────
↪                   2         2         2             2+ 1             aₙ  + 1   aₙ  + 1   aₙ  + 1       aₙ  + 1

In [17]: expr.factor()
Out[17]: 
         32    2-(aₙ - ) ⋅(aₙ + )⋅⎝aₙ  + m  + 2m + 2⎠ 
─────────────────────────────────────────
                   2                     
                 aₙ  + 1                 

In [18]: expr.cancel()
Out[18]: 
    4         3     2  2       2       2           2                        2          
- aₙ  + 2aₙ  - aₙm  - 2aₙm - aₙ  + 2aₙm  + 4aₙm + 4aₙ + m  + 2m + 2

In [19]: expr.cancel().factor()
Out[19]: 
         22    2-(aₙ - ) ⋅⎝aₙ  + m  + 2m + 2

I believe that all of these expressions are equivalent but ideally expr.cancel.factor() and expr.factor() would give the same result. In other words factor should make cancel redundant and both should be able to eliminate any cancelable demoninators. The fact that factor does not remove the denominator is because the numerator and denominator are not being factored over the same field. Otherwise an**2 + 1 in the denominator would factor:

In [21]: factor(a_n**2 + 1)
Out[21]: 
  2    
aₙ  + 1

In [22]: factor(a_n**2 + 1, domain=QQ_I)
Out[22]: (aₙ - )⋅(aₙ + )

This is also a bug although much less serious.

Reproducer:

import sympy
a_n, m = sympy.symbols('a_n m', real=True, positive=True)
expr = (-a_n**4 *m**2 / (a_n**2 + 1) - 2 * a_n**4 *m / (a_n**2 + 1) - a_n**4 - a_n**4 / (a_n**2 + 1) + 2 * sympy.I * a_n**3 *m**2 / (a_n**2 + 1) + 4 * sympy.I * a_n**3 *m / (a_n**2 + 1) + 2 * sympy.I * a_n**3 + 2 * sympy.I * a_n**3 / (a_n**2 + 1) + 2 * sympy.I * a_n *m**2 / (a_n**2 + 1) + 4 * sympy.I * a_n *m / (a_n**2 + 1) + 2 * sympy.I * a_n + 2 * sympy.I * a_n / (a_n**2 + 1) +m**2 / (a_n**2 + 1) + 2 *m / (a_n**2 + 1) + 1 + 1 / (a_n**2 + 1))
expr.factor()
skirpichev added a commit to skirpichev/diofant that referenced this issue May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant