Hi Nicolas, On 2013-09-04, Nicolas Borie <nicolas.bo...@univ-mlv.fr> wrote: > This way works ! But it overwrite the scalar multiplication for the > other usuals ring.
Just an idea: _get_action_ could try to determine whether the usual action (that relies on _rmul_/_lmul_) is available. If it is available then probably it is faster (because of Cython implementation). But hang on, I just see that you do something along these lines: > + def _get_action_(self, S, op=operator.mul, self_on_left=True): > + r""" > + """ > + if self.has_coerce_map_from(self.base_ring()): > + return None > + if S == self.base_ring(): > + return ActionFromBaseRing(self.base_ring(), self) > + return None Hence, if the category is properly initialised, such that there is a coercion from the base ring by virtue of __init_extra__, then the default action will still be used with your patch (which is a good thing), isn't it? > +from sage.categories.action import Action > + > +class ActionFromBaseRing(Action): > + r""" > + """ > + def _call_(self, elem, mat): > + r""" > + """ > + P = mat.parent() > + return P.diagonal_matrix(elem)*mat I guess creating a diagonal matrix and doing a matrix multiplication is very inefficient. I wonder if perhaps it would (after all) be a better idea to simply imitate what is done in _lmul_/_rmul_. As much as I understand, the main problem is that _lmul_/_rmul_ expects wrong types, hence, you could do in _call_ the same trickery with .set_unsafe() and and so on, but simply without expecting that the method argument is of a specific type sage.structure.element.... > I have now a scalar multiplication which is *really more* generic ( but > ok, I should multiply each entries by the right scalar after having > generated a copy ). +1 (see above...) > AttributeError: 'FreeAlgebra_with_category' object has no attribute > 'is_field' That's why I'd somehow like to see 'R.is_field()' replaced with 'R in Fields()'. If I recall correctly, the problem is that there are still many fields that are not (or can not be) properly initialised as fields, because the test whether it is a field or just a ring is expensive. Hence, Fields.__contains__ is a lot more complicated than Rings.__contains__. And while "R in Rings()" is about as fast as calling a non-cached Python method "R.is_field()" returning the constant "True" or "False", the test "R in Fields()" would be slower than "R.is_field()". Hence, for time critical applications, "R.is_field()" might be a good idea... That said, I believe Rings() should provide a (cached?) parent method Rings.ParentMethods.is_field() returning False by default. For properly initialised fields, this would be overridden by Fields.ParentMethods.is_field(), and sage.rings.field.Field.is_field() is available too, for fields that use the arithmetic base classes. > *************************************************************** > > Schur functions : > *************************************************************** > sage: Id = identity_matrix(SF, 2); > sage: Id.adjoint() > --------------------------------------------------------------------------- > TypeError Traceback (most recent call last) ><ipython-input-41-8ec4e1fe1f2c> in <module>() > ----> 1 Id.adjoint() > > /home/nborie/sage/local/lib/python2.7/site-packages/sage/matrix/matrix2.so > in sage.matrix.matrix2.Matrix.adjoint (sage/matrix/matrix2.c:40809)() > > /home/nborie/sage/local/lib/python2.7/site-packages/sage/matrix/matrix2.so > in sage.matrix.matrix2.Matrix._adjoint (sage/matrix/matrix2.c:41159)() > > /home/nborie/sage/local/lib/python2.7/site-packages/sage/matrix/matrix0.so > in sage.matrix.matrix0.Matrix.__neg__ (sage/matrix/matrix0.c:26187)() > > TypeError: Cannot convert > SymmetricFunctionAlgebra_schur_with_category.element_class to > sage.structure.element.RingElement > *************************************************************** This is because _lmul_ is called, which expects this type. Perhaps it makes sense to implement a cdef _lmul_unsafe_ in sage.matrix.matrix0 and use this both in the new action and in __neg__ and in other places that are currently calling _rmul_ and _lmul_? > TypeError: unsupported operand parent(s) for '/': 'Univariate Polynomial > Ring in z over An example of an algebra with basis: the free algebra on > the generators ('a', 'b', 'c') over Rational Field' and 'Univariate > Polynomial Ring in z over An example of an algebra with basis: the free > algebra on the generators ('a', 'b', 'c') over Rational Field' Why is this?? Can the fration field of this polynomial ring be constructed? Where is the problem? > Here come questions : > > - Does all rings should have a *is_field* method and if yes, where > should we place it ? (I guess in parent method of the category Rings() > .... ) They currently don't have, but I think they *should* have! See above. That said, if I recall correctly, some people where strongly opposed to this idea. But frankly I found the counter arguments not convincing and forgot what they were like... > If not, what should be the new test ? ( I guess something like > Fields() in self.base_ring().category().all_super_categories() ... ) Nonono, this would be very slow, and moreover it might not work on fields whose category isn't properly initialised! 'R in Fields()' would be the way to go, *unless* it is time-critical (see above), in which case it would be better to have a method 'R.is_field()'. This is why I suggested to have 'Rings.ParentMethos.is_field()'. Alternatively one could catch an attribute error, argueing that an object without an is_field() method is certainly no field: try: if R.is_field(): do something else: do something else except AttributeError: do something else > - What is the best way to generically invert element ? I don't know > between ~, .inverse(), .__invert__(), 1/... If I recall correctly, one implements ~ by means of __invert__, or by means of _div_ together whith establishing a coercion from ZZ to the ring, because then Sage can use a default implementation of __invert__ relying on 1/... Best regards, Simon -- You received this message because you are subscribed to the Google Groups "sage-combinat-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-combinat-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-combinat-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-combinat-devel. For more options, visit https://groups.google.com/groups/opt_out.