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

rising_factorial and falling_factorial should accept Python integers #20075

Closed
egourgoulhon opened this issue Feb 17, 2016 · 23 comments
Closed

Comments

@egourgoulhon
Copy link
Member

See the bug reported at
http://ask.sagemath.org/question/32565/error-in-rising_factorial/

sage: [rising_factorial(n,n) for n in range(6)]
AttributeError: 'int' object has no attribute 'parent'

Component: basic arithmetic

Keywords: factorial

Author: Eric Gourgoulhon

Branch/Commit: e77247c

Reviewer: Vincent Delecroix

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

@egourgoulhon
Copy link
Member Author

Changed keywords from none to factorial

@egourgoulhon
Copy link
Member Author

Commit: 11fd5e6

@egourgoulhon
Copy link
Member Author

Branch: public/20075

@egourgoulhon
Copy link
Member Author

New commits:

11fd5e6rising_factorial accepts Python integers

@tscrim
Copy link
Collaborator

tscrim commented Feb 17, 2016

comment:2

Since there is a belief that the input could be a long, I think you should also check against that as well.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 17, 2016

Changed commit from 11fd5e6 to 23fb2b1

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 17, 2016

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

23fb2b1rising_factorial and falling_factorial accept Python integers (int, long)

@egourgoulhon
Copy link
Member Author

comment:4

Replying to @tscrim:

Since there is a belief that the input could be a long, I think you should also check against that as well.

Done in the above commit. Btw, I've also modified falling_factorial for consistency.

@egourgoulhon egourgoulhon changed the title rising_factorial should accept Python integers rising_factorial and falling_factorial should accept Python integers Feb 17, 2016
@videlec
Copy link
Contributor

videlec commented Feb 17, 2016

comment:5

You would better use sage.structure.coerce.py_scalar_to_element. For example you forgot about numpy integers. You can simply add the line x = py_scalar_to_element(x) at the begining.

@videlec
Copy link
Contributor

videlec commented Feb 17, 2016

comment:6

And since int,long are changed to Integer there is no need to check for these types in the second pass.

@tscrim
Copy link
Collaborator

tscrim commented Feb 17, 2016

comment:7

Replying to @videlec:

And since int,long are changed to Integer there is no need to check for these types in the second pass.

The first check is x and the second is a.

@egourgoulhon
Copy link
Member Author

comment:8

Replying to @videlec:

And since int,long are changed to Integer there is no need to check for these types in the second pass.

What do you mean? In the code of these functions, there is no second pass for x (the other checks regard a, not x).

@videlec
Copy link
Contributor

videlec commented Feb 17, 2016

comment:9

yep. I read too fast. Sorry for that.

But [comment:5] can be applied to both x and a (and hence the code will also work with numpy integers).

@egourgoulhon
Copy link
Member Author

comment:10

Replying to @videlec:

yep. I read too fast. Sorry for that.

But [comment:5] can be applied to both x and a (and hence the code will also work with numpy integers).

In the current code, there is no need to force a to be a Sage element, for only x.parent() is invoked. So I would apply py_scalar_to_element only to x. Do you agree?

@egourgoulhon
Copy link
Member Author

comment:11

Replying to @egourgoulhon:

In the current code, there is no need to force a to be a Sage element, for only x.parent() is invoked. So I would apply py_scalar_to_element only to x. Do you agree?

PS: in particular, the current code already works with a being a numpy integer (I've just checked it).

@videlec
Copy link
Contributor

videlec commented Feb 17, 2016

comment:12

Replying to @egourgoulhon:

Replying to @egourgoulhon:

In the current code, there is no need to force a to be a Sage element, for only x.parent() is invoked. So I would apply py_scalar_to_element only to x. Do you agree?

PS: in particular, the current code already works with a being a numpy integer (I've just checked it).

Are you sure? What kind of numpy integers did you try?

sage: import numpy
sage: a = numpy.int8(10)
sage: b = numpy.int(5)
sage: isinstance(a, int)
False
sage: isinstance(b, int)
True

@egourgoulhon
Copy link
Member Author

comment:13

Replying to @videlec:

Are you sure? What kind of numpy integers did you try?

sage: import numpy
sage: a = numpy.int8(10)
sage: b = numpy.int(5)
sage: isinstance(a, int)
False
sage: isinstance(b, int)
True

Both work:

sage: import numpy
sage: a = numpy.int8(4)
sage: rising_factorial(3, a)
360
sage: a = numpy.int(4)
sage: rising_factorial(3, a)
360

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 17, 2016

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

e77247cUse py_scalar_to_element in rising_factorial and falling_factorial

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 17, 2016

Changed commit from 23fb2b1 to e77247c

@videlec
Copy link
Contributor

videlec commented Mar 9, 2016

Reviewer: Vincent Delecroix

@videlec
Copy link
Contributor

videlec commented Mar 9, 2016

comment:16

Sorry for the long delay...

@egourgoulhon
Copy link
Member Author

comment:17

Replying to @videlec:

Sorry for the long delay...

No problem. Thank you Vincent !

@vbraun
Copy link
Member

vbraun commented Mar 10, 2016

Changed branch from public/20075 to e77247c

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

4 participants