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

Add get_part method to partition.py #11300

Closed
jbandlow opened this issue May 5, 2011 · 16 comments
Closed

Add get_part method to partition.py #11300

jbandlow opened this issue May 5, 2011 · 16 comments

Comments

@jbandlow
Copy link

jbandlow commented May 5, 2011

Add a method to partitions to return a default value for parts that do not exist.

sage: p = Partition([2,1])
sage: p.get_part(0)
2
sage: p.get_part(5)
0

Apply: attachment: trac_11300-get_part_partition-jb.2.patch

CC: @sagetrac-sage-combinat

Component: combinatorics

Keywords: partitions, days30

Author: Jason Bandlow

Reviewer: Florent Hivert

Merged: sage-4.7.1.alpha1

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

@jbandlow
Copy link
Author

jbandlow commented May 5, 2011

Attachment: trac_11300-get_part_partition-jb.patch.gz

@jbandlow

This comment has been minimized.

@jbandlow
Copy link
Author

jbandlow commented May 5, 2011

Author: jbandlow

@jbandlow

This comment has been minimized.

@hivert
Copy link

hivert commented May 5, 2011

Reviewer: Florent Hivert

@hivert
Copy link

hivert commented May 5, 2011

Changed author from jbandlow to Jason Bandlow

@hivert
Copy link

hivert commented May 5, 2011

Changed keywords from partitions, sagedays30 to partitions, days30

@hivert
Copy link

hivert commented May 5, 2011

comment:3

Looks good and is useful !

@mwhansen
Copy link
Contributor

mwhansen commented May 5, 2011

comment:4

The one issue with this is the following:

sage: p = Partition([4,3])
sage: type(p.get_part(0))
<type 'sage.rings.integer.Integer'>
sage: type(p.get_part(10))
<type 'int'>

Is there a use case for a changeable default value?

@hivert
Copy link

hivert commented May 5, 2011

comment:5

Replying to @mwhansen:

The one issue with this is the following:

> sage: p = Partition([4,3])
> sage: type(p.get_part(0))
> <type 'sage.rings.integer.Integer'>
> sage: type(p.get_part(10))
> <type 'int'>

That is a very good remark !

Is there a use case for a changeable default value?

If yes this can be corrected by changing default=0 to
default=Integer(0).

@jbandlow
Copy link
Author

jbandlow commented May 5, 2011

comment:6

I've changed the default value to Integer(0). I don't have a specific use case for another value, but it seems like it might come in handy. Do you have a reason you think it's a bad idea? I could probably be convinced to get rid of it.

@saliola
Copy link

saliola commented May 6, 2011

comment:7

Hello Jason. I'm curious: did you compare timings of using the try-except statement:

 	768	        try: 
 	769	            return self[i] 
 	770	        except IndexError: 
 	771	            return default

with an if statement, like this one:

    if i < len(self._list):
        return self._list[i]
    else: 
        return default

I would guess that try-except statements are slower, but I have not tried it.

@jbandlow
Copy link
Author

jbandlow commented May 6, 2011

comment:8

Attachment: trac_11300-get_part_partition-jb.2.patch.gz

Replying to @saliola:

Hello Jason. I'm curious: did you compare timings of using the try-except statement:
with an if statement, like this one:

I would guess that try-except statements are slower, but I have not tried it.

I tried it and you're right--'if' is significantly faster. I've replaced the patch.

@saliola
Copy link

saliola commented May 6, 2011

comment:9

Thanks for testing that. Positive review.

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link

Merged: sage-4.7.1.alpha1

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

5 participants