473,795 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generalized range

I need to create ranges that can start and end with real numbers.
Searching this newsgroup brought me to a function that I then modified
as follows:

def myRange(iMin, iMax=None, iStep=1):
"""Extends range to real numbers. Wherever possible, use Python's
range .
In other cases, make the behavior follow the spirit of Python's
range """
epsilon = 1.e-8

if iMax == None and iStep == 1:
return range(int(iMin) )

elif type(iMin).__na me__.lower() in ('int', 'long') and \
type(iMax).__na me__.lower() in ('int', 'long') and \
type(iStep).__n ame__.lower() in ('int', 'long') and iStep !=
0:
return range( iMin, iMax, iStep)

elif iMin <= iMax and iStep 0:
return [ iMin+i*iStep for i in range( int(math.ceil(( iMax -
iMin - epsilon)/iStep)) )]

elif iMin >= iMax and iStep < 0:
return [ iMin+i*iStep for i in range(-int(math.ceil(( iMin -
iMax + epsilon)/iStep)) )]

else:
raise ValueError, 'Cannot construct a range with steps of size
' + str(iStep) + ' between ' + str(iMin) + ' and ' + str(iMax)
The one part of my implementation that has me a bit queasy (i.e.
works in my current application, but I can see it misbehaving
elsewhere) is the addition/subtraction of a fixed epsilon to ensure
that my rounding goes the right way. A clean implementation would
modify epsilon based on the achievable level of precision given the
particular values of iMax, iMin and iStep. I suspect this requires a
detailed understanding of the implementation of floating point
arithmetic, and would appreciate hearing any thoughts you might have
on gilding this lily.

Sincerely

Thomas Philips

Apr 26 '07 #1
8 1890
tk****@hotmail. com schrieb:
I need to create ranges that can start and end with real numbers.
Searching this newsgroup brought me to a function that I then modified
as follows:

def myRange(iMin, iMax=None, iStep=1):
Just as a sidenote: it is not common to prefix your names with its type.
It could change at any time and min, max, step would be clear, too. IMO.
"""Extends range to real numbers. Wherever possible, use Python's
range .
In other cases, make the behavior follow the spirit of Python's
range """
If you want to stick to the "normal" range-implementation, myRange
should consider an one-argument-call as transmission of iMax.
epsilon = 1.e-8
I can't really say if your attempt using an epsilon-environment is good.
I think just increasing a "counter" from iMin to iMax should be fine,
achieving more precision by making computations fuzzy -- i don't know,
perhaps it's very good. I wouldn't do it.
If you like to care about precision, you should have a look at the
`decimal module <http://docs.python.org/lib/module-decimal.html>`_ .
>
if iMax == None and iStep == 1:
return range(int(iMin) )

elif type(iMin).__na me__.lower() in ('int', 'long') and \
type(iMax).__na me__.lower() in ('int', 'long') and \
type(iStep).__n ame__.lower() in ('int', 'long') and iStep !=
0:
return range( iMin, iMax, iStep)
Ouchie! *That* is a bad one. Checking for a name of an object is neither
safe nor good nor common. A better way would be directly comparing
type(yourobject ) with int/long/float/anytype. See
http://docs.python.org/lib/comparisons.html for details on comparisons.
Another way of type-checking in python is doing something like ``if
isinstance(iMin , (int, long))``. Would work for subclasses, too.
>
elif iMin <= iMax and iStep 0:
return [ iMin+i*iStep for i in range( int(math.ceil(( iMax -
iMin - epsilon)/iStep)) )]

elif iMin >= iMax and iStep < 0:
return [ iMin+i*iStep for i in range(-int(math.ceil(( iMin -
iMax + epsilon)/iStep)) )]
Will eat your memory. See below.
else:
raise ValueError, 'Cannot construct a range with steps of size
' + str(iStep) + ' between ' + str(iMin) + ' and ' + str(iMax)
In Python, it is common to use string interpolation instead. Like::
print 'Hello from %d to %d' % (iMin, iMax)
Read `String Formatting Operations
<http://docs.python.org/lib/typesseq-strings.html>`_ in the manual for
details.
>

The one part of my implementation that has me a bit queasy (i.e.
works in my current application, but I can see it misbehaving
elsewhere) is the addition/subtraction of a fixed epsilon to ensure
that my rounding goes the right way. A clean implementation would
modify epsilon based on the achievable level of precision given the
particular values of iMax, iMin and iStep. I suspect this requires a
detailed understanding of the implementation of floating point
arithmetic,
I think so, too. That's why it is a bad idea, IMO.
and would appreciate hearing any thoughts you might have
on gilding this lily.

Sincerely

Thomas Philips
I'd recommend you to have a look into `generators
<http://docs.python.org/ref/yield.html>`_, it is what `xrange
<http://docs.python.org/lib/built-in-funcs.html#l2h-80>`_ uses. You
don't put all numbers into your memory ("precompute them") but behave a
little bit lazy and compute them whenever the user needs the next one. I
expect Google to have lots of example implementations of range as a
generator in python for you. :-)

HTH,
Stargaming
Apr 26 '07 #2
tk****@hotmail. com wrote:
I need to create ranges that can start and end with real numbers.
Searching this newsgroup brought me to a function that I then modified
as follows:

def myRange(iMin, iMax=None, iStep=1):
"""Extends range to real numbers. Wherever possible, use Python's
range .
In other cases, make the behavior follow the spirit of Python's
range """
epsilon = 1.e-8

if iMax == None and iStep == 1:
return range(int(iMin) )

elif type(iMin).__na me__.lower() in ('int', 'long') and \
type(iMax).__na me__.lower() in ('int', 'long') and \
type(iStep).__n ame__.lower() in ('int', 'long') and iStep !=
0:
return range( iMin, iMax, iStep)

elif iMin <= iMax and iStep 0:
return [ iMin+i*iStep for i in range( int(math.ceil(( iMax -
iMin - epsilon)/iStep)) )]

elif iMin >= iMax and iStep < 0:
return [ iMin+i*iStep for i in range(-int(math.ceil(( iMin -
iMax + epsilon)/iStep)) )]

else:
raise ValueError, 'Cannot construct a range with steps of size
' + str(iStep) + ' between ' + str(iMin) + ' and ' + str(iMax)
The one part of my implementation that has me a bit queasy (i.e.
works in my current application, but I can see it misbehaving
elsewhere) is the addition/subtraction of a fixed epsilon to ensure
that my rounding goes the right way. A clean implementation would
modify epsilon based on the achievable level of precision given the
particular values of iMax, iMin and iStep. I suspect this requires a
detailed understanding of the implementation of floating point
arithmetic, and would appreciate hearing any thoughts you might have
on gilding this lily.
In addition to the comments of Stargaming, most of which I agree with, I
think you would be far better dropping the epsilon business and doing
something like:

# requires that minimum <= maximum; swap values if necessary
res = minimum
while res < maximum:
yield res
res += step
--
Michael Hoffman
Apr 26 '07 #3
Thanks - you have covered a fair bit of gorund here - I will modify
myRange taking your suggestions into account. The one suggestion that
I'm going to have to think through is repeatedly incrementing res.

I deliberately did not use this as repeated addition can cause
rounding errors to accumulate, making the loop run a little longer or
shorter than necessary. I thought I would be far less likely to run
into rounding issues with a multiplicative construct - hence my use of
epsilon, and my question about an appropriate value for it

Apr 26 '07 #4
tk****@hotmail. com wrote:
Thanks - you have covered a fair bit of gorund here - I will modify
myRange taking your suggestions into account. The one suggestion that
I'm going to have to think through is repeatedly incrementing res.

I deliberately did not use this as repeated addition can cause
rounding errors to accumulate, making the loop run a little longer or
shorter than necessary. I thought I would be far less likely to run
into rounding issues with a multiplicative construct - hence my use of
epsilon, and my question about an appropriate value for it
You are right about rounding issues--with a sufficiently small step, the
way I have done it, it could become an infinite loop. But you can still
do it with multiplication, without using an epsilon constant. How about
something like this:

index = 0
while res < maximum:
yield minimum + (step * index)
index += 1
--
Michael Hoffman
Apr 26 '07 #5
Michael Hoffman wrote:
How about something like this:

index = 0
while res < maximum:
yield minimum + (step * index)
index += 1
Well it really would have to be something LIKE that since I never
defined res. Let's try that again:

index = 0
res = minimum
while res < maximum:
yield res
res = minimum + (step * index)
index += 1
--
Michael Hoffman
Apr 26 '07 #6
Michael Hoffman <ca*******@mh39 1.invalidwrote:
tk****@hotmail. com wrote:
Thanks - you have covered a fair bit of gorund here - I will modify
myRange taking your suggestions into account. The one suggestion that
I'm going to have to think through is repeatedly incrementing res.

I deliberately did not use this as repeated addition can cause
rounding errors to accumulate, making the loop run a little longer or
shorter than necessary. I thought I would be far less likely to run
into rounding issues with a multiplicative construct - hence my use of
epsilon, and my question about an appropriate value for it

You are right about rounding issues--with a sufficiently small step, the
way I have done it, it could become an infinite loop. But you can still
do it with multiplication, without using an epsilon constant. How about
something like this:

index = 0
while res < maximum:
yield minimum + (step * index)
index += 1
Absolutely (with your later correction of actually assigning res), MUCH
better than the misguided attempts based on repeatedly adding 'step'.

I've taught "numerical computing" in university, and I would have had to
fail anybody who'd misunderstood floating-point computations badly
enough to try that "+=step" idea (including, sigh, the coders of several
Fortran compilers who were popular at that time).

These days, I _heartily_ recommend "Real Computing Made Real" by Foreman
Acton -- the best programming book without one line of code in any
language (it's all formulas and graphs). Anybody who has trouble
following it must understand they should NOT use floating point numbers
until they've gotten the prereqs (which aren't all that deep:
engineering-undergrad-level algebra and calculus, plus
<http://docs.sun.com/source/806-3568/ncg_goldberg.ht mland a couple
weeks' worth of refresher courses on fundamental algorithms such as
Newton's root-finding approach and the like -- I shudder to think how
many people with CS bachelor degrees and even post-grad degrees are just
never taught these fundamentals, yet end up having to program _some_
floating point computations, mistakenly thinking they can...).
Alex

Apr 27 '07 #7
On Apr 27, 1:32 am, a...@mac.com (Alex Martelli) wrote:
Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
tkp...@hotmail. com wrote:
Thanks - you have covered a fair bit of gorund here - I will modify
myRange taking your suggestions into account. The one suggestion that
I'm going to have to think through is repeatedly incrementing res.
I deliberately did not use this as repeated addition can cause
rounding errors to accumulate, making the loop run a little longer or
shorter than necessary. I thought I would be far less likely to run
into rounding issues with a multiplicative construct - hence my use of
epsilon, and my question about an appropriate value for it
You are right about rounding issues--with a sufficiently small step, the
way I have done it, it could become an infinite loop. But you can still
do it with multiplication, without using an epsilon constant. How about
something like this:
index = 0
while res < maximum:
yield minimum + (step * index)
index += 1

Absolutely (with your later correction of actually assigning res), MUCH
better than the misguided attempts based on repeatedly adding 'step'.

I've taught "numerical computing" in university, and I would have had to
fail anybody who'd misunderstood floating-point computations badly
enough to try that "+=step" idea (including, sigh, the coders of several Fortran compilers who were popular at that time).
You may be referring to the Fortran DO loop with a REAL loop variable,
for example

do x=1.5,3.5,0.5
print*,x
end do

This was part of standard Fortran 77, so one should blame the
standards committee, not the compiler writers. Very few features of
F77 were deleted in Fortran 95, but this was one of them. In practice,
nothing gets deleted from commercial Fortran compilers.

At the SciPy site http://www.scipy.org/Cookbook/OptimizationAndFitDemo1
there is some code

1 from enthought.chaco .wx import plt
2 from scipy import arange, optimize, special
3
4 plt.figure()
5 plt.hold()
6 w = []
7 z = []
8 x = arange(0,10,.01 )
9
10 for k in arange(1,5,.5):
11 y = special.jv(k,x)
12 plt.plot(x,y)
13 f = lambda x: -special.jv(k,x)
14 x_max = optimize.fminbo und(f,0,6)
15 w.append(x_max)
16 z.append(specia l.jv(k,x_max))
17
18 plt.plot(w,z, 'ro')
19 from scipy import interpolate
20 t = interpolate.spl rep(w, z, k=3)
21 s_fit3 = interpolate.spl ev(x,t)
22 plt.plot(x,s_fi t3, 'g-')
23 t5 = interpolate.spl rep(w, z, k=5)
24 s_fit5 = interpolate.spl ev(x,t5)
25 plt.plot(x,s_fi t5, 'y-')

I think the use of arange with a non-integer increment is poor style,
for reasons discussed in this thread.

Apr 27 '07 #8
Beliavsky <be*******@aol. comwrote:
...
I've taught "numerical computing" in university, and I would have had to
fail anybody who'd misunderstood floating-point computations badly
enough to try that "+=step" idea (including, sigh, the coders of several
Fortran compilers who were popular at that time).

You may be referring to the Fortran DO loop with a REAL loop variable,
for example

do x=1.5,3.5,0.5
print*,x
end do

This was part of standard Fortran 77, so one should blame the
standards committee, not the compiler writers. Very few features of
I was thinking of "Fortran IV" aka Fortran '66, where as I recall per
the standard you were _supposed_ to only use integers in a DO, but
several compilers supplied real loop variables as an extension, and got
its implementation wrong.
Alex
Apr 27 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5485
by: venkat | last post by:
Hi, I want to solve linear least sqaure problem( min||c-Ax||2 subject to Bx=d ). How do I do it in python. lapack has a routine for doing this (DGGLSE). Can I access this from python? TIA, venkat.
5
8392
by: squidco | last post by:
I've been trying to wean myself off of tables. I've been reading about the float property, and have implemented a couple of sites using divs instead of tables, and generally I'm happy with the results. Still, I'm having problems constructing a generalized approach to laying divs side by side without being very specific about the width of each div. The code below makes 3-columns of content, but unfortunately if the visitor resizes the...
9
1305
by: Bengt Richter | last post by:
;-) We have @deco def foo(): pass as sugar (unless there's an uncaught exception in the decorator) for def foo(): pass foo = deco(foo) The binding of a class name is similar, and class decorators would seem natural, i.e.,
6
1745
by: christopher diggins | last post by:
I wrote a dynamic matrix class similar to the one described in TCPL 3rd Edition. Rather than define two separate iterators for const and non-const scenarios I decided to be a lazy bastard and only have one and make the data representation (a std::valarray) mutable instead. My question is, how bad is that? Am I running the risk of undefined behaviour, or is the worst case scenario simply const violation? -- Christopher Diggins...
5
3088
by: Jean-Guillaume Pyraksos | last post by:
I want to work with "generalized lists" as in Lisp, say : ((23 () . 2) () ((10))) ; only pointers and integers So the basic element is a node, a struct with two fields car and cdr. Each of these fields can contain either a pointer (NULL or a pointer to another node), or an int. I want to define the functions cons, car and cdr of Lisp.
3
1649
by: Spectre1337 | last post by:
Hello, I've encountered a rather puzzling problem with SqlDataAdapter. I've set up a number of elaborate tables in the Microsoft SQL Server Management Studio Express (including several table key relationships), and I've been successfully accessing all of that information through the classes generated by the "visual" DataSet file in Web Developer Express (Solution > Add New Item > Data Set). Prior to my last code modification I've...
3
1369
by: | last post by:
I am enjoying making generalized methods to serve common needs, such as: ImageProcessing.MakeQuickResize(); ImageProcessing.Sharpen(); FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate() Sender.EmailCommaDelimitedList() What would be really cool is if I could make a generalized function that would accept other methods as parameters, and would perform the methods on another parameterized item that's fed to the function. So we might...
5
1578
by: er | last post by:
hi, is anyone aware of a library that generalizes transform, for_each? e.g. transform( vec1.begin(), vec1.end(), vec2.begin(), vec3.begin(),
0
1201
by: iain654 | last post by:
I have finally worked out how to automatically send a range of cells in the body of an email using Outlook but it is very clumsy and I have to build up the email using the limit of line continuations. Is there any quicker and easier way of sending this email as it is always a set range of cells that I want to sent. At the moment my code looks like this: Sub Email_DPV() Dim oFolder As Outlook.MAPIFolder Dim oItem As Outlook.MailItem Dim...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.