Hi Dinakur,

The best way of doing this would be to open a track ticket, edit 
partition.py, add your method to the Partition class and then submit this 
for review so that it can be included in a future release of sage. If you 
decide to go this route then you would need to add some documentation and 
some tests. I have not come across this definition before but if you think 
that it would be useful for others then I recommend doing this -- I could 
help you put it together. If you do want to submit this to sage then I'd  
recommend changing the name to something more meaningful like 
CarrellGoulding_partition.

If you don't want to go to the effort of incorporating this method into 
sage then here is a hack that I use occasionally, although I should add 
that python purists will probably disapprove of this (and perhaps vocally, 
in response to my post!). First define the following function somewhere:


def add_method_to_class(klass):
  r""" 
  Add a new method to a pre-existing class.
  """
  def add_func(func):
    setattr(klass,func.func_name,func)
    return func
  return add_func

Once you have this then you can write:

#\lambda^(i) from Carrell-Goulding paper
@add_method_to_class(Partition)
def i_part(self,i):
    if i==0:
        return self
    elif i<0:
        return (self.conjugate().i_part(-i)).conjugate()
    zero_one_sequence = self.zero_one_sequence()
    num_ones = zero_one_sequence.count(1)
    if num_ones < i:
        zero_one_sequence += (i-num_ones)*[1]
    index_one_list = [index for index in range(len(zero_one_sequence)) if 
zero_one_sequence[index] == 1]
    zero_one_sequence[index_one_list[i-1]]=0
    return Partitions().from_zero_one(zero_one_sequence)

Once you attach this file to your sage session your new method will be 
attached to any partition:

sage: mu=Partition([4,3,1])
sage: mu.i_part(0)
[4, 3, 1]
sage: mu.i_part(1)
[3, 2]
sage: mu.i_part(2)
[3, 2, 1, 1]
sage: mu.i_part(3)
[3, 2, 2, 1]
sage: mu.i_part(-3)
[5, 4]


Andrew

On Friday, 6 June 2014 07:50:22 UTC+10, Dinakar Muthiah wrote:
>
> I literally wrote the following at the top of my module:
>
> #\lambda^(i) from Carrell-Goulding paper
> def i_part(self,i):
>     if i==0:
>         return self
>     elif i<0:
>         return (self.conjugate().i_part(-i)).conjugate()
>     zero_one_sequence = self.zero_one_sequence()
>     num_ones = zero_one_sequence.count(1)
>     if num_ones < i:
>         zero_one_sequence += (i-num_ones)*[1]
>     index_one_list = [index for index in range(len(zero_one_sequence)) if 
> zero_one_sequence[index] == 1]
>     zero_one_sequence[index_one_list[i-1]]=0
>     return Partitions().from_zero_one(zero_one_sequence)
>
> Partition.i_part = i_part
>
> Then if later I wrote:
>
> p = Partition([3,2,1])
>
> I can call 
>
> p.i_part(2)
>
>
> This works. I just want to know whether there is a better or more standard 
> solution.
>
> When you say a new __init__ would be good, how do you mean to implement 
> that?
>
> Dinakar
>
>
> On Thursday, June 5, 2014 1:11:36 PM UTC-4, kcrisman wrote:
>>
>>
>>> I want to know the best practices for extending the functionality of a 
>>> sage class. For example, I would like to add the following method to the 
>>> Partition class in sage:
>>>
>>> #\lambda^(i) from Carrell-Goulding paper
>>> def i_part(self,i):
>>>     if i==0:
>>>         return self
>>>     elif i<0:
>>>         return (self.conjugate().i_part(-i)).conjugate()
>>>     zero_one_sequence = self.zero_one_sequence()
>>>     num_ones = zero_one_sequence.count(1)
>>>     if num_ones < i:
>>>         zero_one_sequence += (i-num_ones)*[1]
>>>     index_one_list = [index for index in range(len(zero_one_sequence)) 
>>> if zero_one_sequence[index] == 1]
>>>     zero_one_sequence[index_one_list[i-1]]=0
>>>     return Partitions().from_zero_one(zero_one_sequence)
>>>
>>>
>>> My first inclination is to subclass Partition and add this as a method 
>>> to the subclass. However, I am not able to make this work.
>>>
>>>
>> What exactly did you write?  A new initialize (__init__) would be a good 
>> first step.
>>  
>>
>>> So what I have done so far is to just include the above function 
>>> definition in a module followed by the following line:
>>>
>>> Partition.i_part = i_part
>>>
>>> So I am dynamically changing the Partition class. This doesn't seem like 
>>> a good practice in general, so I would like to know what is the preferred 
>>> solution.
>>>
>>> Dinakar
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to