On Jan 4, 2009, at 11:47 AM, ggrafendorfer wrote: > Hi Robert, > thanks for your answer, > I not sure if I know the difference between coercion and conversion, > could you explain it to me?
A coercion is implicit and happens, for example, when you do arithmetic. sage: 1 + 1/2 # 1 is coerced into a rational number before the addition takes place 3/2 sage: 1.2 + 1/2 # 1/2 is coerced into RR 1.70000000000000 sage: 11 + mod(2, 5) # 11 is coerced into Z/5Z 3 sage: R.<t> = QQ[]; R Univariate Polynomial Ring in t over Rational Field sage: t^2 + 5 # 5 is coerce into Q[t] t^2 + 5 Coercions are nearly always canonical, and commute with each other (modulo perhaps rounding issues for inexact sets). Coercions must be defined on the entire domain. To see them explicitly one can do sage: R.coerce_map_from(ZZ) Call morphism: From: Integer Ring To: Univariate Polynomial Ring in t over Rational Field A conversion is explicit, and tries to make a new element of the right type if it makes sense at all. If there is a coercion from R to S, then "conversion" from R to S will be via this coercion, but otherwise it does the best it can. sage: ZZ(t + 1 - t) # there is no coercion from R to Z, but the constant polynomials convert. 1 sage: QQ(1.2) # there is no coercion from RR to QQ, but we do our best to find a good representative 6/5 sage: ZZ(t + 1 - t).parent() Integer Ring sage: ZZ(mod(3, 5)) + ZZ(mod(3,7)) 6 sage: R([1,2,3]) 3*t^2 + 2*t + 1 Hope this helps clarify things. - Robert --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---