Optimizing Memory Allocation in a Simple, but Long Function

Derek Klinge schilke.60 at gmail.com
Sun Apr 24 13:06:22 EDT 2016


I think my e-mail client may be stripping the indentation, here it is with
4-space indentation

## Write a method to approximate Euler's Number using Euler's Method
import math

class EulersNumber():
def __init__(self,n):
self.eulerSteps = n
self.e = self.EulersMethod(self.eulerSteps)
def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
return x + h * d
def EulersMethod(self, numberOfSteps): # Repeate linear approximation over
an even range
e = 1 # e**0 = 1
for step in range(numberOfSteps):
e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)= e**x,
f'(x)=f(x)
return e

def EulerStepWithGuess(accuracy,guessForN):
n = guessForN
e = EulersNumber(n)
while abs(e.e - math.e) > abs(accuracy):
n +=1
e = EulersNumber(n)
print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e - math.e)))
return e

def EulersNumberToAccuracy(PowerOfTen):
x = 1
theGuess = 1
thisE = EulersNumber(1)
while x <= abs(PowerOfTen):
thisE = EulerStepWithGuess(10**(-1*x),theGuess)
theGuess = thisE.eulerSteps * 10
x += 1
return thisE
On Sun, Apr 24, 2016 at 10:02 AM Derek Klinge <schilke.60 at gmail.com> wrote:

> Sorry about the code indentation, I was using Pythonista (iOS), and it did
> not have any problem with that indentation...
>
> Here is a new set of the code:
> ## Write a method to approximate Euler's Number using Euler's Method
> import math
>
> class EulersNumber():
> def __init__(self,n):
> self.eulerSteps = n
> self.e = self.EulersMethod(self.eulerSteps)
> def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
> return x + h * d
> def EulersMethod(self, numberOfSteps): # Repeate linear approximation over
> an even range
> e = 1 # e**0 = 1
> for step in range(numberOfSteps):
> e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)= e**x,
> f'(x)=f(x)
> return e
>
> def EulerStepWithGuess(accuracy,guessForN):
> n = guessForN
> e = EulersNumber(n)
> while abs(e.e - math.e) > abs(accuracy):
> n +=1
> e = EulersNumber(n)
> print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e - math.e)))
> return e
>
> def EulersNumberToAccuracy(PowerOfTen):
> x = 1
> theGuess = 1
> thisE = EulersNumber(1)
> while x <= abs(PowerOfTen):
> thisE = EulerStepWithGuess(10**(-1*x),theGuess)
> theGuess = thisE.eulerSteps * 10
> x += 1
> return thisE
>
> My problem is this: my attempt at Euler's Method involves creating a list
> of numbers that is n long. Is there a way I can iterate over the linear
> approximation method without creating a list of steps (maybe recursion, I
> am a bit new at this). Ideally I'd like to perform the linearApproximation
> method a arbitrary number of times (hopefully >10**10) and keep feeding the
> answers back into itself to get the new answer. I know this will be
> computationally time intensive, but how do I minimize memory usage (limit
> the size of my list)? I also may be misunderstanding the problem, in which
> case I am open to looking at it from a different perspective.
>
> Thanks,
> Derek
>
> On Sun, Apr 24, 2016 at 9:22 AM Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Sun, Apr 24, 2016 at 1:05 PM, Derek Klinge <schilke.60 at gmail.com>
>> wrote:
>> > I have been writing a python script to explore Euler's Method of
>> > approximating Euler's Number. I was hoping there might be a way to make
>> > this process work faster, as for sufficiently large eulerSteps, the
>> process
>> > below becomes quite slow and sometimes memory intensive. I'm hoping
>> someone
>> > can give me some insight as to how to optimize these algorithms, or
>> ways I
>> > might decrease memory usage. I have been thinking about finding a way
>> > around importing the math module, as it seems a bit unneeded except as
>> an
>> > easy reference.
>>
>> Are you sure memory is the real problem here?
>>
>> (The first problem you have, incidentally, is a formatting one. All
>> your indentation has been lost. Try posting your code again, in a way
>> that doesn't lose leading spaces/tabs, and then we'll be better able
>> to figure out what's going on.)
>>
>> If I'm reading your code correctly, you have two parts:
>>
>> 1) class EulersNumber, which iterates up to some specific count
>> 2) Module-level functions, which progressively increase the count of
>> constructed EulersNumbers.
>>
>> Between them, you appear to have an O(n*n) algorithm for finding a
>> "sufficiently-accurate" representation. You're starting over from
>> nothing every time. If, instead, you were to start from the previous
>> approximation and add another iteration, that ought to be immensely
>> faster.
>>
>> ChrisA
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>



More information about the Python-list mailing list