You can do: sage: K.<sqrt2>=QuadraticField(2) sage: K.register_coercion(K.coerce_map_from(QQ) * QQ.coerce_map_from(ZZ))
For even more speed you can write your own custom Cython coercion from ZZ to K. David On Fri, May 10, 2013 at 12:59 PM, vdelecroix <20100.delecr...@gmail.com>wrote: > Hi, > > Let K be a quadratic number field (let say K = QuadraticField(2)). > There are "natural morphisms" implemented in Cython from ZZ to QQ and > from QQ to K. There is also an other way, somewhat more direct, to go > from ZZ to K. But at the end it is 10 times faster to go the first way > and Sage does not choose it! > {{{ > sage: K.<sqrt2>=QuadraticField(2) > sage: f = K.coerce_map_from(ZZ) > sage: g = K.coerce_map_from(QQ) * QQ.coerce_map_from(ZZ) > sage: timeit('f(1)') > 625 loops, best of 3: 13.8 µs per loop > sage: timeit('g(1)') > 625 loops, best of 3: 1.29 µs per loop > }}} > > What can I do to make Sage chooses the undirect way ? In some sense a > composition of "natural morphisms" (ie mathematically natural and > Cython implemented) should be prefered to "conversion". > > Best, > Vincent > > Note: I get some troubles with #13213 which pops out the natural > morphism from QQ to K... > > -- > You received this message because you are subscribed to the Google Groups > "sage-devel" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-devel+unsubscr...@googlegroups.com. > To post to this group, send email to sage-devel@googlegroups.com. > Visit this group at http://groups.google.com/group/sage-devel?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel?hl=en. For more options, visit https://groups.google.com/groups/opt_out.