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: 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; '<': 0.19; 'thu,': 0.19; 'example': 0.22; 'import': 0.22; 'email addr:gmail.com>': 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: References: Date: Thu, 18 Apr 2013 14:04:49 -0400 Subject: Re: equivalent to C pointer From: David Robinow To: abdelkader belahcene 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --001a11c2f2363e3c1f04daa67015 Content-Type: text/plain; charset=UTF-8 On Thu, Apr 18, 2013 at 1:06 PM, abdelkader belahcene 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 > #include > > 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() --001a11c2f2363e3c1f04daa67015 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Thu, Apr 18, 2013 at 1:06 PM, abdelkader bela= hcene <abelahcene@gmail.com> wrote:
Hi everybody,=C2=A0
I am new to python=C2=A0 and I am discovering it.
I kno= w C well,
and want to know if python knows how to manage Pointers<= br>
like pointer to function=C2=A0 here is a C example how to write it in pytho= n
Intergration with trapeze method

Wh= en we write Trapeze =C2=A0 ( at the compilation level) we don't know wh= ich functions
Fonc=C2=A0 to handle.=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Here for example = we use=C2=A0 sin and a user defined=C2=A0 F1
The program is attach= ed too

#include &l= t;stdio.h>
#include <math.h>
=C2=A0
double F1 (double x){
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 r= eturn x*x;
}
double Trapeze(double Fonc(double ),
=C2=A0=C2=A0= =C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2= =A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 double l= eft, double right, double step){
=C2=A0 double X1, X0, Y0, Y1, Z =3D 0;= =C2=A0=C2=A0=C2=A0
=C2=A0 for(X0=3Dleft; X0 < right ; X0 =3D X0 + st= ep) {
=C2=A0=C2=A0=C2=A0 X1 =3D X0 + step;
=C2=A0=C2=A0=C2=A0 Y1 =3D Fonc(X1);= =C2=A0=C2=A0=C2=A0 Y0 =3D Fonc(X0);
=C2=A0=C2=A0=C2=A0 Z=C2=A0 +=3D (Y1 = + Y0) * step * 0.5;
=C2=A0 }
=C2=A0=C2=A0 return Z;
}
int=C2=A0= main(){
=C2=A0 double y;
=C2=A0 y=3DTrapeze(sin, -2.5, 3.2, 0.1);=C2=A0 printf("\n\tValue for sin=C2=A0 is : \t %8.3lf ", y); =C2=A0 y=3DTrapeze(F1, 0, 3, 0.1);
=C2=A0 printf("\n\tValue for F1 = is : \t %8.3lf ", y);
=C2=A0 return 0;
}
/**
=C2=A0=C2=A0= =C2=A0 Value for sin=C2=A0 is : =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 0.197=
=C2=A0=C2=A0=C2=A0 Value for F1 is : =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0= =C2=A0=C2=A0 9.005
=C2=A0=C2=A0=C2=A0 */

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 bot= her with getting the print formats just right)
--
import math
def F1(x):
=C2=A0=C2=A0=C2=A0 return x*x

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

def main():
=C2=A0=C2=A0=C2=A0 y =3D Trapeze(math.sin, = -2.5, 3.2, 0.1)
=C2=A0=C2=A0=C2=A0 print("Value for sin is:{0} &quo= t;.format(y))
=C2=A0=C2=A0=C2=A0 y =3D Trapeze(F1, 0, 3, 0.1)
=C2=A0=C2=A0=C2=A0 print("Value for F1 is {0} ".format(y))
if __name__ =3D=3D "__main__":
=C2=A0=C2=A0=C2=A0 main()
<= br> --001a11c2f2363e3c1f04daa67015--