Just a toy implementation as a very thin layer over dict (at least it
should be fast)
no doc
---------------------------------------------------------------------------------
first see it in action:

sage: x=Test()
sage: p=x.zero_element()
sage: time for i in range(10000): p+=x[i]
CPU times: user 0.10 s, sys: 0.00 s, total: 0.10 s
Wall time: 0.10 s
sage: time q=p*3
CPU times: user 0.04 s, sys: 0.00 s, total: 0.04 s
Wall time: 0.05 s
sage: time q-(2*p+p)
CPU times: user 0.10 s, sys: 0.00 s, total: 0.10 s
Wall time: 0.10 s
0
sage: time r=sum([x[i] for i in range(10000)],x.zero_element())
CPU times: user 2.69 s, sys: 0.00 s, total: 2.70 s
Wall time: 2.70 s
sage: time p==r
CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00 s
True
sage: x[1] + x[2.5] + x[Partition([3,2,1])] + x [ QQ ] + x[gap] + x[x
[3]+x[2]]
 1*B[2.50000000000000] + 1*B[1] + 1*B[Gap] + 1*B[ 1*B[2] + 1*B[3] ] +
1*B[[3, 2, 1]] + 1*B[Rational Field]

(NB: += is way faster than +)
---------------------------------------------------------------------------------
if it can help, here is the code:

class Test(UniqueRepresentation):
    def __getitem__(self,x):
        return TestElement(x)
    def zero_element(self):
        return TestElement({})

class TestElement:
    def __init__(self, x):
        if isinstance(x,dict):
            self._dict=dict(x)
        else:
            self._dict={x:1}

    def __add__(self,other):
        out = TestElement(self._dict)
        return out.__iadd__(other)

    def __sub__(self,other):
        out = TestElement(self._dict)
        return out.__isub__(other)

    def __mul__(self,k):
        out = TestElement(self._dict)
        return out.__imul__(k)

    def __rmul__(self,k):
        out = TestElement(self._dict)
        for x,v in out._dict.iteritems():
            out._dict[x]= k*v
        return out

    def __div__(self,k):
        out = TestElement(self._dict)
        return out.__idiv__(k)

    def __iadd__(self,other):
        d = self._dict
        for i,c in other._dict.iteritems():
            if i in d:
                d[i] += c
                if not d[i]:
                    del d[i]
            else:
                d[i] = c
        return self

    def __isub__(self,other):
        d = self._dict
        for i,c in other._dict.iteritems():
            if i in d:
                d[i] -= c
                if not d[i]:
                    del d[i]
            else:
                d[i] = c
        return self

    def __imul__(self,k):
        for x,v in self._dict.iteritems():
            self._dict[x]=v*k
        return self

    def __idiv__(self,k):
        for x,v in self._dict.iteritems():
            self._dict[x]=k/v
        return self

    def __getitem__(self,i):
        return self._dict.get(i,0)

    def __setitem__(self,x,v):
        self._dict[x]=v

    def __repr__(self):
        if len(self._dict) == 0:
            return "0"
        return '+'.join([" %s*B[%s] "%(str(v),str(x)) for x,v in
self._dict.iteritems()])

    def __hash__(self):
        return hash(frozenset(self._dict))

    def __cmp__(self,other):
        return self._dict.__cmp__(other._dict)

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to