On Thu, Jun 5, 2008 at 12:23 PM, Gonzalo Tornaria wrote:
> On Thu, Jun 5, 2008 at 1:47 AM, William Stein wrote:
>> ... How are you going to make, at
>>  runtime, new classes for each of Z/nZ say, for 1 <= n < 10^5,
>> i.e., something like this in Sage:
>>
>>   v = [Integers(n) for n in range(1,10^5)]
>>
>>  I do not think it is possible to concisely create a hundred thousand
>>  separate Python classes like that.
> ...
> WRT your example:
>
> sage: def classinteger(m):
> ...     class A:
> ...       def __init__(self, n):
> ...         self.__n = n % m
> ...       def __repr__(self):
> ...         return  "%d mod %d" % (self.__n, m)
> ...     A.__name__ = "classintegers mod %d" % m
> ...     return A
> sage: classinteger(5)(3)
> 3 mod 5
> sage: time v = [classinteger(n) for n in range(1,10^5)]
> CPU time: 3.64 s,  Wall time: 4.14 s
> sage: time w = [Integers(n) for n in range(1,10^5)]
> CPU time: 15.75 s,  Wall time: 16.21 s
> sage: v[1256](34251235)
> 499 mod 1257
> ...

Excellent example. Thank you!

> I was going to say that using classes built at runtime like this
> was fine but not a good option for performance reasons, but the
> example above shows the overhead of python class creation time
> is not so significant.
>

Agreed.

Python can treat types as fully first class objects. I think this
strongly supports William's initial decision to choose Python as the
basis for a new computer algebra system comparable to Magma and Axiom.

Regards,
Bill Page.

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