On Tue, May 05, 2009 at 03:11:48PM -0700, Nicolas Thiéry wrote: > Dear Franco, dear all, > > Patch of the day: #5991 Add a standard constructor for dynamic classes > > Franco, since you are using dynamic classes in Sage-Words, could you > review or comment on it?
For information: I just added to the patch the following discussion on the relevance of the dynamic class paradigme for Sage. Best, Nicolas ------------------------------------------------------------------------------ ..topic: Why dynamic classes? The short answer: - Multiple inheritance is a powerful tool for constructing new classes by combining preexisting building blocks. - There is a combinatorial explosion in the number of potentially useful classes that can be produced this way. - The implementation of standard mathematical constructions calls for producing such combinations automatically. - Dynamic classes, i.e. classes created on the fly by the Python interpreter, are a natural mean to achieve this. The long answer: Say we want to construct a new class ``MyPermutation`` for permutations in a given set `S` (in Sage, `S` will be modelled by a parent, but we won't discuss this point here). First, we have to choose a data structure for the permutations, typically among the following: - Stored by cycle type - Stored by code - Stored in list notation - C arrays of short ints (for small permutations) - python lists of ints (for huge permutations) - ... - Stored by reduced word - Stored as a function - ... Luckily, the Sage library provides (or will provide) classes implementing each of those data structures. Those classes all share a common interface (or possibly a common abstract base class). So we can just derive our class from the chosen one:: class MyPermutation(PermutationCycleType): ... Then we may want to further choose a specific memory behavior (unique representation, copy-on-write) which (hopefuly) can again be achieved by inheritance. class MyPermutation(UniqueRepresentation, PermutationCycleType): ... Finaly, we may want to endow the permutations in `S` with further operations coming from the (algebraic) structure of `S`: - group operations - or just monoid operations (for a subset of permutations not stable by inverse) - poset operations (for left/right/Bruhat order) - word operations (searching for substrings, patterns, ...) Or any combination thereof. Now, our class typically looks like:: class MyPermutation(UniqueRepresentation, PermutationCycleType, PosetElement, GroupElement): ... Note the combinatorial explosion in the potential number of classes which can be created this way. In practice, such classes will be used in mathematical constructions like:: SymmetricGroup(5).subset(... TODO: find a good example in the context above ...) In such a construction, the structure of the result, and therefore the operations on its elements can only be determined at execution time. Let us take another standard construction:: A = cartesian_product( B, C ) Depending on the structure of `B` and `C`, and possibly on further options passed down by the user, `A` may be: - an enumerated set - a group - an algebra - a poset - ... Or any combination thereof. Hardcoding classes for all potential combinations would be at best tedious. Furthermore, this would require a cumbersome mechanism to lookup the appropriate class depending on the desired combination. Instead, one may use the ability of Python to create new classes dynamicaly: type("class name", tuple of base classes, dictionary of methods) This paradigm is powerful, but there are some technicalities to address. The purpose of this library is to standardize its use within Sage, and in particular to ensure that the constructed classes are reused whenever possible (unique representation), and can be pickled. ..topic: combining dynamic classes and Cython classes TODO -- Nicolas M. Thiéry "Isil" <nthi...@users.sf.net> http://Nicolas.Thiery.name/ --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---