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


Groups > comp.lang.python > #43846

Re: equivalent to C pointer

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <drobinow@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'everybody,': 0.07; '"__main__":': 0.09; '#include': 0.09; '__name__': 0.09; 'main()': 0.09; 'pointers': 0.09; 'received:209.85.219': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; '/**': 0.16; '0.1)': 0.16; '0.1);': 0.16; '0.5;': 0.16; 'left,': 0.16; 'main():': 0.16; 'pointers,': 0.16; 'x1,': 0.16; 'y1,': 0.16; '{0}': 0.16; 'wrote:': 0.18; '&lt;': 0.19; 'thu,': 0.19; 'example': 0.22; 'import': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'compilation': 0.24; 'pointer': 0.24; 'math': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'received:209.85': 0.35; 'knows': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; "didn't": 0.36; 'method': 0.36; 'too': 0.37; 'received:209': 0.37; 'step': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; 'skip:& 20': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'skip:\xc2 10': 0.60; 'new': 0.61; 'you.': 0.62; 'to:addr:gmail.com': 0.65; 'here': 0.66; '8bit%:40': 0.68; '8bit%:100': 0.72; '\xc2\xa0\xc2\xa0': 0.74; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; 'discovering': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=icJICAZk39UmH+9+zm7CRhMgrONmZeNLbNRhlHeFS2I=; b=wiL5QpGaJM7gPR+zyVsEohdgvmsWRCJvB2g9aaBTJ3jR6DwHcnMm73C0oDBEFw/hBR DKt2On7oawwf7VW/A9h5MRKdhMepowLHC6l0oQb7HP5GKHzT7vk+0pZd/Xw3aNYDC2NK uC2JVBDmeZlIsU+64d5qHL4hxqNGXyu3h3+1xs0TQxJuHDiYSZFFP7LltkVpltO5zxlD +n8HtW+qnyyMw8uu5SrUcAMumHcbVhqNrZMak8ArnOCzO6ekMEZrn49HsikeH+C9fdoi G0jKACUZy+nn95zwqqv+OIdxCGdvo0LpSmJ3oU4Xw3wN/TcHPJ6HEYZlSRvC2PtGpIXW 1PgA==
MIME-Version 1.0
X-Received by 10.182.165.105 with SMTP id yx9mr1241369obb.86.1366308289393; Thu, 18 Apr 2013 11:04:49 -0700 (PDT)
In-Reply-To <CAF3f0sQ_rsdKwaEOCmFHEeEwpVUaNUo-pOYgoqYA_F-k0CftcA@mail.gmail.com>
References <CAF3f0sQ_rsdKwaEOCmFHEeEwpVUaNUo-pOYgoqYA_F-k0CftcA@mail.gmail.com>
Date Thu, 18 Apr 2013 14:04:49 -0400
Subject Re: equivalent to C pointer
From David Robinow <drobinow@gmail.com>
To abdelkader belahcene <abelahcene@gmail.com>
Content-Type multipart/alternative; boundary=001a11c2f2363e3c1f04daa67015
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.784.1366308298.3114.python-list@python.org> (permalink)
Lines 140
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366308298 news.xs4all.nl 2270 [2001:888:2000:d::a6]:56777
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43846

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Thu, Apr 18, 2013 at 1:06 PM, abdelkader belahcene
<abelahcene@gmail.com>wrote:

> Hi everybody,
>
> I am new to python  and I am discovering it.
> I know C well,
> and want to know if python knows how to manage Pointers
> like pointer to function  here is a C example how to write it in python
> Intergration with trapeze method
>
> When we write Trapeze   ( at the compilation level) we don't know which
> functions
> Fonc  to handle.      Here for example we use  sin and a user defined  F1
> The program is attached too
>
> #include <stdio.h>
> #include <math.h>
>
> double F1 (double x){
>         return x*x;
> }
> double Trapeze(double Fonc(double ),
>                                 double left, double right, double step){
>   double X1, X0, Y0, Y1, Z = 0;
>   for(X0=left; X0 < right ; X0 = X0 + step) {
>     X1 = X0 + step;
>     Y1 = Fonc(X1);    Y0 = Fonc(X0);
>     Z  += (Y1 + Y0) * step * 0.5;
>   }
>    return Z;
> }
> int  main(){
>   double y;
>   y=Trapeze(sin, -2.5, 3.2, 0.1);
>   printf("\n\tValue for sin  is : \t %8.3lf ", y);
>   y=Trapeze(F1, 0, 3, 0.1);
>   printf("\n\tValue for F1 is : \t %8.3lf ", y);
>   return 0;
> }
> /**
>     Value for sin  is :         0.197
>     Value for F1 is :          9.005
>     */
>
> Python doesn't have pointers, but don't let that bother you.
A python version is actually a lot simpler.
See below (I didn't bother with getting the print formats just right)
--
import math
def F1(x):
    return x*x

def Trapeze(f, left, right, step):
    X0 = left
    Z = 0.0
    while (X0 < right):
        X1 = X0 + step
        Y1 = f(X1)
        Y0 = f(X0)
        Z += (Y1 + Y0) * step * 0.5
        X0 = X1
    return Z

def main():
    y = Trapeze(math.sin, -2.5, 3.2, 0.1)
    print("Value for sin is:{0} ".format(y))
    y = Trapeze(F1, 0, 3, 0.1)
    print("Value for F1 is {0} ".format(y))

if __name__ == "__main__":
    main()

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


Thread

Re: equivalent to C pointer David Robinow <drobinow@gmail.com> - 2013-04-18 14:04 -0400

csiph-web