On Mon, Apr 12, 2010 at 10:12:47PM +0930, ross kyprianou wrote:
> # This made things very interesting.
> # Using WordOptions(identifier='') does most of the work needed
> 
> ### Example 1 ###
> sage: (P,Q,R)= 
> MtxAlgebrasWithBasis(QQ).example(('P','Q','R')).algebra_generators()

Do you need a specific category for your application? In particular,
will you have several parents between which to share code?

Otherwise, you could just implement a parent.

> sage: P*(Q+R)
> 
> B[word: PQ] + B[word: PR]
> 
> ### Example 2 ###
> sage: WordOptions(identifier='')
> sage: P*(Q+R)
> 
> B[PQ] + B[PR]
> 
> # To go "all the way" a bit more code seems to be needed
> # this is based on the _repr_ referred to in previous tip from Nicolas
> 
> def _repr_(self):
>     v = self._monomial_coefficients.items()
>     try:
>         v = sorted(v)
>     except StandardError: # Sorting the output is a plus, but if we
> can't, no big deal
>         pass
>     repr_term = self.parent()._repr_term
> #   mons = [ repr_term(m) for (m, _) in v ]
>     mons = [ m.string_rep() for (m, _) in v ]
>     cffs = [ x for (_, x) in v ]
>     x = repr_lincomb(mons, cffs).replace("*1 "," ")
>     if x[len(x)-2:] == "*1":
>         return x[:len(x)-2]
>     else:
>         return x
> 
> # was tested using ...
> _repr_((P+Q)*R)       
> 
> 'PR + QR'
> 
> This is what I was aiming to be printed when I do   (P+Q)*R

Cool :-)

> Does this code (in the new _repr_) look ok?
> Im aiming to do things consistently or to standards.
> Anything better recommended?
> I should be returning a string from _repr_ - shouldnt I ?

If the only change is on the repr_term line, you might as well
override the method _repr_term in the parent; that's its purpose!

Best,
                                Nicolas
--
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

To unsubscribe, reply using "remove me" as the subject.

Reply via email to