On Thu, Feb 10, 2011 at 05:33:15AM -0800, Dox wrote:
> I already define my class, and starts Ok, I have changed the operation
> to __mul__. But now I'd like to define an __add__ operation which
> surpass my knowledge...
> 
> Something like this,
> sage: A = MyClass( 3, "Hello")
> sage: B = MyClass( 4, "World!")
> sage: A+B
> ( 3, "Hello") + ( 4, "World!")
> 
> sage: C = MyClass( 5, "World!")
> sage: B+C
> ( 9, "World!")
> 
> I'm thinking could be a bit hard, since the function I'm defining has
> not a string but a matrix as second entry. The first is not a number
> either.

Sounds like you want to implement something like the algebra of the
multiplicative monoid of n x n matrices? The following may inspire you:

    sage: M = Monoids().example(); M
    An example of a monoid: the free monoid generated by ('a', 'b', 'c', 'd')
    sage: A = M.algebra(QQ); A
    Free module generated by An example of a monoid: the free monoid generated 
by ('a', 'b', 'c', 'd') over Rational Field
    sage: A.category()
    Category of monoid algebras over Rational Field
    sage: [a,b,c,d] = A.algebra_generators()
    sage: (a+b) * (c+2*d)
    2*B['bd'] + B['ac'] + 2*B['ad'] + B['bc']

So in your case, one would want to just take:

    sage: M = MatrixSpace(ZZ,2)

which is (among other things) a multiplicative monoid, and to consider
its multiplicative monoid algebra:

    sage: A = M.algebra(QQ, category=Monoids())

That almost works up to a technical issue: matrices are mutable by
default [1], and thus cannot be used to index the basis of a vector
space.  So what you would need to do is to start from the monoid
example, the sources of which you can get with:

    sage: M = Monoids().example()
    sage: M??

and to adapt it so that its elements would be objects x such that
x.value would be an immutable matrix (instead of a string).

This raises a suggestion; should Sage implement:

    sage: M = MatrixSpace(ZZ, 2, immutable=True)

Cheers,
                                Nicolas

[1] http://www.sagemath.org/doc/developer/coding_in_python.html#mutability

--
Nicolas M. ThiƩry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/

-- 
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