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 Washington http://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 -~----------~----~----~----~------~----~------~--~---