Sorry, I meant to. One problem is that the Sage pre-parsing results in different behavior than I am seeing, so pasting code into a Sage session will not exhibit the problem I am seeing.
Let me see if I can cajole Sage into giving similar behavior from the prompt. sage: import __builtin__ sage: fracs_list = [RealNumber(0.5), RealNumber(0.3), RealNumber(0.2)] sage: length = 10 sage: vector([__builtin__.round(frac*length) for frac in fracs_list]) --------------------------------------------------------------------------- <type 'exceptions.TypeError'> Traceback (most recent call last) ... <type 'exceptions.TypeError'>: unable to find a common ring for all elements sage: vector([int(__builtin__.round(frac*length)) for frac in fracs_list]) --------------------------------------------------------------------------- <type 'exceptions.TypeError'> Traceback (most recent call last) ... <type 'exceptions.TypeError'>: unable to find a common ring for all elements sage: vector([Integer(int(__builtin__.round(frac*length))) for frac in fracs_list]) (5, 3, 2) The last answer is what I want. It seems like two type casts is a little excessive. Maybe I should import the round() method from Sage instead of importing Integer. On Apr 11, 5:01 pm, "William Stein" <[EMAIL PROTECTED]> wrote: > On Fri, Apr 11, 2008 at 9:58 AM, Ryan Hinton <[EMAIL PROTECTED]> wrote: > > > There must be an easier way to do this. In a Python class I have a list > > of RealNumber elements, "fracs_list". I want to multiply them by a > > common scalar "length" and round them to get a vector of integers. I > > have tried many permutations of the following code, most of which fail. > > This works, but it's ugly! > > You'll likely get a much better response from sage-support if you post > some example session that works so people can start from that and > know that they are looking at the right problem. E.g., define fracs_list > and length below so that people can just paste in three lines and have > something work. Thanks! > > > > > > > answer = vector([Integer(int(round(frac*length))) for frac in fracs_list]) > > > ipdb> type(self.var_fracs[0]) > > <type 'sage.rings.real_mpfr.RealNumber'> > > > ipdb> type(length) > > <type 'sage.rings.integer.Integer'> > > > ipdb> type(length * fracs_list[0]) > > <type 'sage.rings.real_mpfr.RealNumber'> > > > ipdb> type(round(length * fracs_list[0])) > > <type 'float'> > > > ipdb> Integer(round(length * fracs_list[0])) > > > ipdb> answer = vector([round(frac*length) for frac in fracs_list]) > > *** TypeError: unable to find a common ring for all elements > > > ipdb> answer = vector([int(round(frac*length)) for frac in fracs_list]) > > *** TypeError: unable to find a common ring for all elements > > > ipdb> answer = vector([Integer(round(frac*length)) for frac in fracs_list]) > > *** TypeError: unable to coerce element to an integer > > > ipdb> answer = vector(IntegerRing(), [round(frac*length) for frac in > > fracs_list]) > > *** TypeError: unable to coerce element to an integer > > > Any suggestions on how to get this computation without less than the > > round and two type conversions? > > > Thanks! > > > --- > > Ryan Hinton > > [EMAIL PROTECTED] > > -- > William Stein > Associate Professor of Mathematics > University of Washingtonhttp://wstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@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-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---