Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107603

Re: Optimizing Memory Allocation in a Simple, but Long Function

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Subject Re: Optimizing Memory Allocation in a Simple, but Long Function
Date Mon, 25 Apr 2016 14:45:55 +0100
Lines 55
Message-ID <mailman.76.1461591982.32212.python-list@python.org> (permalink)
References <CAGuRns8KhOFL-a1_a=0hMP=NbktoJw78ih3j4uUSbvkF8bf3pw@mail.gmail.com> <CAPTjJmrQfk20EuoZVMLqKv8F5r=YyKj0nr8iNPw7DWoCEYLmYA@mail.gmail.com> <CAGuRns86c4G5BHX_FF-OBnV_eRVKyngx_Dfp0u7hoHRT0WN++w@mail.gmail.com> <CAPTjJmq5g848-PMTgHOg7sawm+JgA5mSfwDHA-ZZgsmYr6yLCA@mail.gmail.com> <CAGuRns8LsThq3iV-bkdBbg9fZO7s61z4ggBy=YkGDqhaB_J4Xw@mail.gmail.com> <CAGuRns9uvHVBXP79X1Sp8vsW4JpWF-3g1mJoWHOq-cGog61cug@mail.gmail.com> <CAPTjJmpvtP5pj_NiJNXGLmu2QCqSYZNgZjKisRRMRTtzvGHKyQ@mail.gmail.com> <CAHVvXxSwb8=a+b5WQfd3KeXTe8VuL4RMnwu3TSNp8129FJHJGw@mail.gmail.com> <CAGuRns911cXoppsS_8f657UDdCtVFhAzwY7ohafOqrsEuRq7vQ@mail.gmail.com> <CAGuRns9byxLC3pGrkPG8m-qLTzB1+PYxLHNy2=fOHcwWR21x6A@mail.gmail.com> <mailman.63.1461553965.32212.python-list@python.org> <do5vu5Fipb4U1@mid.individual.net> <CAHVvXxTU2k74S_hG=RtsEx=5uTk34Ppf4t2PhqBsat39Kv3ihQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de m7mYCtULUZ7eWca7gYwb9gCe+uKV3vZLsvcLY/anFjBw==
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'binary': 0.05; 'method.': 0.05; 'smallest': 0.07; 'subject:Function': 0.07; 'subject:Long': 0.09; '2016': 0.16; 'accuracy,': 0.16; 'gregory': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'to:name:python list': 0.16; 'truncation': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'math': 0.20; 'seems': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'required.': 0.26; 'error': 0.27; 'message- id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'integrating': 0.29; 'relative': 0.30; 'guess': 0.31; 'subject:Simple': 0.33; 'changing': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'could': 0.35; 'level': 0.35; 'needed': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'say': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'term': 0.60; 'chance': 0.60; 'high': 0.60; 'your': 0.60; 'skip:n 10': 0.62; 'goal': 0.64; 'oscar': 0.84; 'doubling': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=rDur8AIIGrlilPuI+fdXMIJX2gnpQTY8PvCfEqjwmvE=; b=siTHmPVlyrjnOeoFrfZgg2MTReaugU8neMWLtoQvllJSmoGkj91KtvWPm5j2XqE5gA vaLSlWqXrpiiAs/SIupUise5771Hr768yMDxDgO54YklLgLvkaFBdGPfdHNUhiTjmzKL 09R5hMMZLYitDHGsOgH3HHtZaqO10d1W8UI+4J+VErcP6iD4HMdAb+Hi7UmmfeMNtLsR D1gp1ELFNAClpI3eeRERhMYqEJaeG4MOL0VQRyTvLhzMm+4gN97O3tIW5PqZZCpeN2GI j/jWmSdEVOlXixk9sjtnTVbVUDkRXTKeXuAHxYM3TYq2BDPbbZrv+GXoD7p4w6gICfAd qsBw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=rDur8AIIGrlilPuI+fdXMIJX2gnpQTY8PvCfEqjwmvE=; b=X/gd3mPO+kwJ+XVHWDjoo74wVoSc0KEOpfPYbEcmMAWf7m39wPylfVgx9+s48G/uHE shNgoB0any+eQfPrxiEZLbb+qX+5Q0NtUohf3QsDX24jjY7N61ZgDFrNRGLI4PMOlGeK 3jWpHJkn6VJrWauV7IM0inz7bFI/j4nwbfjCuRamMpUUbJaSJuCQPUm85PMBS7LFpp2o mJdGONYH8yRxAxTWFNhtDHDoMTmLLKoMIkr9GyGn+kj4RvSTnNlcly4xIC/LIJ7Gr8RC 7mk8h7TJLe3LmVMo0gjQUJvwavz45Wtxk+d8Ctw1Ox658YBZCSd+Bi6o9Ctx16/jpGAW sd8w==
X-Gm-Message-State AOPr4FULWiQ4GFppDXJymzgV2UA+vUiOdL11mSL/KITM2zhSXLTi3umVB7WuCDYqpActhnd9u+n4v0JyzA/Bag==
X-Received by 10.25.218.1 with SMTP id r1mr14592578lfg.130.1461591974743; Mon, 25 Apr 2016 06:46:14 -0700 (PDT)
In-Reply-To <do5vu5Fipb4U1@mid.individual.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAHVvXxTU2k74S_hG=RtsEx=5uTk34Ppf4t2PhqBsat39Kv3ihQ@mail.gmail.com>
X-Mailman-Original-References <CAGuRns8KhOFL-a1_a=0hMP=NbktoJw78ih3j4uUSbvkF8bf3pw@mail.gmail.com> <CAPTjJmrQfk20EuoZVMLqKv8F5r=YyKj0nr8iNPw7DWoCEYLmYA@mail.gmail.com> <CAGuRns86c4G5BHX_FF-OBnV_eRVKyngx_Dfp0u7hoHRT0WN++w@mail.gmail.com> <CAPTjJmq5g848-PMTgHOg7sawm+JgA5mSfwDHA-ZZgsmYr6yLCA@mail.gmail.com> <CAGuRns8LsThq3iV-bkdBbg9fZO7s61z4ggBy=YkGDqhaB_J4Xw@mail.gmail.com> <CAGuRns9uvHVBXP79X1Sp8vsW4JpWF-3g1mJoWHOq-cGog61cug@mail.gmail.com> <CAPTjJmpvtP5pj_NiJNXGLmu2QCqSYZNgZjKisRRMRTtzvGHKyQ@mail.gmail.com> <CAHVvXxSwb8=a+b5WQfd3KeXTe8VuL4RMnwu3TSNp8129FJHJGw@mail.gmail.com> <CAGuRns911cXoppsS_8f657UDdCtVFhAzwY7ohafOqrsEuRq7vQ@mail.gmail.com> <CAGuRns9byxLC3pGrkPG8m-qLTzB1+PYxLHNy2=fOHcwWR21x6A@mail.gmail.com> <mailman.63.1461553965.32212.python-list@python.org> <do5vu5Fipb4U1@mid.individual.net>
Xref csiph.com comp.lang.python:107603

Show key headers only | View raw


On 25 April 2016 at 08:39, Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote:
> Derek Klinge wrote:
>>
>> Also, it seems to me if the goal is to use the smallest value of n to get
>> a
>> particular level of accuracy, changing your guess of N by doubling seems
>> to
>> have a high chance of overshoot.
>
>
> If you want to find the exact n required, once you overshoot
> you could use a binary search to narrow it down.

Also you can calculate the truncation error for Euler's method. Since

   f(t) = f(t0) + f'(t0)*(t - t0) + (1/2)f''(t0)*(t - t0)**2 + O((t - t0)**3)

Euler's method just uses the first two terms so

    x[n+1] = x[n] + dt*f(x[n])

the next term would be

   (1/2)*f'(x[n])*dt**2

Since in your case f'(x) = x and dt = 1/N that's

    (1/2)*x[n]*(1/N)**2

As a relative error (divide by x[n]) that's

    (1/2)*(1/N)**2

Let's add the relative error from N steps to get

    N*(1/2)*(1/N)**2 = 1/(2*N)

So the relative error integrating from 0 to 1 with N steps is 1/(2*N).
If we want a relative error of epsilon then the number of steps needed
is 1/(2*epsilon).

That is to say that for a relative error of 1e-4 we need N =
1/(2*1e-4) = 1e4/2 = 5e3 = 5000.

>>> import math
>>> N = 5000
>>> error = math.e - (1 + 1.0/N)**N
>>> relative_error = error / math.e
>>> relative_error
9.998167027596845e-05

Which is approximately 1e-4 as required.

--
Oscar

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Optimizing Memory Allocation in a Simple, but Long Function Derek Klinge <schilke.60@gmail.com> - 2016-04-24 20:12 -0700
  Re: Optimizing Memory Allocation in a Simple, but Long Function Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-25 19:39 +1200
    Re: Optimizing Memory Allocation in a Simple, but Long Function Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-04-25 14:45 +0100
    Re: Optimizing Memory Allocation in a Simple, but Long Function Derek Klinge <schilke.60@gmail.com> - 2016-04-25 14:35 +0000
    Re: Optimizing Memory Allocation in a Simple, but Long Function Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-04-26 10:54 +0100

csiph-web