<div class="gmail_quote">On Thu, Apr 18, 2013 at 1:06 PM, abdelkader belahcene <span dir="ltr"><<a href="mailto:abelahcene@gmail.com" target="_blank">abelahcene@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div><div><div><div><div><div><div>Hi everybody, <br><br></div>I am new to python and I am discovering it.<br></div>I know C well,<br></div>and want to know if python knows how to manage Pointers<br></div>
like pointer to function here is a C example how to write it in python<br></div><div>Intergration with trapeze method<br></div><div><br></div>When we write Trapeze ( at the compilation level) we don't know which functions <br>
</div>Fonc to handle. Here for example we use sin and a user defined F1<br></div>The program is attached too<br><div><br><div><div><div><div><div><div><div><div><div>#include <stdio.h><br>#include <math.h><br>
<br>double F1 (double x){ <br> return x*x; <br>}<br>double Trapeze(double Fonc(double ), <br> double left, double right, double step){<br> double X1, X0, Y0, Y1, Z = 0; <br> for(X0=left; X0 < right ; X0 = X0 + step) {<br>
X1 = X0 + step;<br> Y1 = Fonc(X1); Y0 = Fonc(X0);<br> Z += (Y1 + Y0) * step * 0.5;<br> }<br> return Z;<br>}<br>int main(){ <br> double y;<br> y=Trapeze(sin, -2.5, 3.2, 0.1);<br> printf("\n\tValue for sin is : \t %8.3lf ", y);<br>
y=Trapeze(F1, 0, 3, 0.1);<br> printf("\n\tValue for F1 is : \t %8.3lf ", y);<br> return 0;<br>} <br>/**<br> Value for sin is : 0.197 <br> Value for F1 is : 9.005<br> */<br></div><br>
</div></div></div></div></div></div></div></div></div></div></blockquote></div>Python doesn't have pointers, but don't let that bother you.<br>A python version is actually a lot simpler.<br>See below (I didn't bother with getting the print formats just right)<br>
--<br>import math<br>def F1(x):<br> return x*x<br><br>def Trapeze(f, left, right, step):<br> X0 = left<br> Z = 0.0<br> while (X0 < right):<br> X1 = X0 + step<br> Y1 = f(X1)<br> Y0 = f(X0)<br>
Z += (Y1 + Y0) * step * 0.5<br> X0 = X1<br> return Z<br><br>def main():<br> y = Trapeze(math.sin, -2.5, 3.2, 0.1)<br> print("Value for sin is:{0} ".format(y))<br> y = Trapeze(F1, 0, 3, 0.1)<br>
print("Value for F1 is {0} ".format(y))<br><br>if __name__ == "__main__":<br> main()<br><br>