> To solve this I tried to write a wrapper for
> sage.rings.integer.Integer.__mul__ however I get the error "can't set
> attributes of built-in/extension type 'sage.rings.integer.Integer'"
> when executing
>   sage: sage.rings.integer.Integer.__mul__ = my_new_multiplication

You're getting the error you're seeing because
sage.rings.integer.Integer isn't a regular Python class.  It is a C
extension type for speed reasons.  It is the same error you'd get if
you did "float.new_attribute = 5".

> Could someone enlighten me about either problem, and a possible way to
> fix it.

The problem you are having with your "MyFloat" class is more of a
Python problem than a Sage problem.  I assume that you're inheriting
the multiplication method from float somewhat like this:

class MyFloat(float):
    pass

The problem is that the multiplication routines in float don't know to
create an object of type MyFloat.  Thus, if you do "3*MyFloat(2.0)" in
pure Python (no Sage integers involved), you'll get a value of 6.0
with a type of float.  To fix this, you could define MyFloat in the
following way:

sage: class MyFloat(float):
....:     def __mul__(self, x):
....:         return MyFloat(float(self)*x)
....:     def __rmul__(self, x):
....:         return MyFloat(x*float(self))

Then, you get the following behavior which is what I assume you want:

sage: a = MyFloat(2)
sage: 3*a
6.0
sage: type(_)
<class '__main__.MyFloat'>
sage: a*3
6.0
sage: type(_)
<class '__main__.MyFloat'>

--Mike

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to