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

Symbolic factor_list() should do integer factorisation of integers/rationals #21067

Open
rwst opened this issue Jul 21, 2016 · 24 comments
Open

Comments

@rwst
Copy link

rwst commented Jul 21, 2016

Let symbolic factor_list() do integer factorisation if given an integer or fraction.

sage: SR(50).factor_list()
[(50, 1)]
sage: SR(49/100).factor_list()
[(49/100, 1)]

Too much surprise for new users.

Component: symbolics

Author: Ralf Stephan, Vincent Delecroix

Branch/Commit: u/rws/21067 @ a821fb6

Reviewer: Daniel Krenn

Issue created by migration from https://trac.sagemath.org/ticket/21067

@rwst rwst added this to the sage-7.3 milestone Jul 21, 2016
@rwst
Copy link
Author

rwst commented Jul 21, 2016

@rwst
Copy link
Author

rwst commented Jul 21, 2016

Author: Ralf Stephan

@rwst
Copy link
Author

rwst commented Jul 21, 2016

New commits:

ac3d7cb21067: do ZZ/QQ factorization in ex.factor_list()

@rwst

This comment has been minimized.

@rwst
Copy link
Author

rwst commented Jul 21, 2016

Commit: ac3d7cb

@rwst rwst changed the title Symbolic factor() should do integer factorisation of integers/rationals Symbolic factor_li8st() should do integer factorisation of integers/rationals Jul 21, 2016
@rwst rwst changed the title Symbolic factor_li8st() should do integer factorisation of integers/rationals Symbolic factor_list() should do integer factorisation of integers/rationals Jul 21, 2016
@tscrim
Copy link
Collaborator

tscrim commented Jul 21, 2016

comment:4

One minor thing: could you move the import into the factor_list method? I worry that this could lead to import hell.

@nbruin
Copy link
Contributor

nbruin commented Jul 21, 2016

comment:5

Two comments:

  • I don't think SR needs to be imported at all. The very fact that we land in this factor_list implementation should mean that self.parent() should be SR. And if it isn't, that's probably the better parent to use anyway.

  • If we're going to do this anyway, I think it's better if we have

sage: (5/3*x/(x+1)).factor_list()
[(x + 1, -1), (x, 1), (5, 1), (3,-1)]

because it's closer to the idea that the factorisation of a product U*V has a tendency to be the concatenation of the factorisations of U and V.

It should be clear that factor_list just returns a factorisation:

sage: ((sqrt(5)+1)^2*(x^2-5)).expand_rational().factor_list()
[(x^2 - 5, 1), (sqrt(5) + 3, 1), (2, 1)]

but keeping the convention that we factor rational numbers into their prime factors should be fine and could be nice for newbies.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 22, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

eca869e21067: extend functionality

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 22, 2016

Changed commit from ac3d7cb to eca869e

@videlec
Copy link
Contributor

videlec commented Jul 22, 2016

comment:7

Instead of having a special check and a special method for integer/rationals couldn't we use something simpler and more generic like

def _factorization_from_pyobject(self):
    try:
        a = self.pyobject()
    except TypeError:
        return

    try:
        f = a.factor()
    except (AttributeError, NotImplementedError):
        return

    # from here I assume that a is a Sage object which might not be the case...
    P = self.parent()
    facs = [(P(p), P(e)) for p,e in f]
    if f.unit().is_one():
        return facs
    else:
        return [(P(f.unit()), P(1))] + facs

@rwst
Copy link
Author

rwst commented Jul 23, 2016

comment:8

Vincent, if you think it's better, then please go ahead on a new branch. I'm not so deep in Python that I could object.

@nbruin
Copy link
Contributor

nbruin commented Jul 23, 2016

comment:9

I think "factor" in general could be a little fickle to depend on:

sage: K.<a>=NumberField(x^2+5)
sage: K(2).factor()
ArithmeticError: non-principal ideal in factorization
sage:  SR(2).pyobject().factor()
2
sage: (2+I-I).pyobject().factor() 
(-I) * (I + 1)^2

@rwst
Copy link
Author

rwst commented Apr 7, 2017

comment:11

I think this ticket should also do:

sage: (2+6*x).factor_list()
[(3*x + 1, 1), (2, 1)]

@rwst
Copy link
Author

rwst commented Apr 16, 2017

@rwst
Copy link
Author

rwst commented Apr 16, 2017

Changed author from Ralf Stephan to Ralf Stephan, Vincent Delecroix

@rwst
Copy link
Author

rwst commented Apr 16, 2017

Changed commit from eca869e to 4769429

@rwst
Copy link
Author

rwst commented Apr 16, 2017

comment:13

I used Vincent's code for a new version that tries also to separate the content in a univariate polynomial.


New commits:

476942921067: Symbolic factor_list() should do integer factorisation of

@rwst rwst modified the milestones: sage-7.3, sage-8.0 Apr 16, 2017
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 9, 2017

Changed commit from 4769429 to 8091ff3

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 9, 2017

Branch pushed to git repo; I updated commit sha1. New commits:

8091ff3Merge branch 'develop' into t/21067/21067

@rwst
Copy link
Author

rwst commented Nov 28, 2017

comment:15

Doctest errors, see patchbot.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 6, 2017

Changed commit from 8091ff3 to a821fb6

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 6, 2017

Branch pushed to git repo; I updated commit sha1. New commits:

933a75fMerge branch 'develop' into t/21067/21067
a821fb621067: simplification, doctest fix

@rwst rwst modified the milestones: sage-8.0, sage-8.2 Dec 6, 2017
@dkrenn
Copy link
Contributor

dkrenn commented Mar 28, 2019

comment:18

LGTM, I only have one comment:

+            raise ValueError

This is only used internally, so it might be good to consider a customized exception:

class CannotFactor(RuntimeError):
    pass

And yes, I think RuntimeError is more appropriate (but if you insist on ValueError then I am good with it as well, although wanting to hear your arguments for that ;) )

@dkrenn
Copy link
Contributor

dkrenn commented Mar 28, 2019

Reviewer: Daniel Krenn

@mkoeppe mkoeppe removed this from the sage-8.2 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants