equivalent to C pointer
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 */ thanks a lot #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 */ -- http://mail.python.org/mailman/listinfo/python-list
Re: equivalent to C pointer
Thanks for answer, but with C we can compile the trapeze function and put it in librairy, If we try to save the trapeze alone in package to import it later, I think, I am not sure it will be refused because F1 and sin are not define !!! this is the power of the C pointers !!! the link is dynamic thanks On Thu, Apr 18, 2013 at 6:17 PM, Karim wrote: > > Hello, > > There is no such notion in python. > But the closest are iterators and generator functions. > > Cheers > Karim > > > On 18/04/2013 19:06, 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 > */ > thanks a lot > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: equivalent to C pointer
Thanks a lot, I think this does the task cheers On Thu, Apr 18, 2013 at 7:14 PM, David Robinow wrote: > On Thu, Apr 18, 2013 at 1:50 PM, abdelkader belahcene < > abelahc...@gmail.com> wrote: > >> Thanks for answer, >> but with C we can compile the trapeze function and put it in librairy, >> If we try to save the trapeze alone in package to import it later, I >> think, I am not sure >> it will be refused because F1 and sin are not define !!! this is the >> power of the C pointers !!! >> the link is dynamic >> > You don't need C pointers. The design below is demonstrative, not ideal. > > # file MyFuncs.py > > 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 > > > > # file UseMyFuncs.py > import math > import MyFuncs > > def main(): > y = MyFuncs.Trapeze(math.sin, -2.5, 3.2, 0.1) > > print("Value for sin is:{0} ".format(y)) > y = MyFuncs.Trapeze(MyFuncs.F1, 0, 3, 0.1) > > print("Value for F1 is {0} ".format(y)) > > if __name__ == "__main__": > main() > > ### > #python3 UseMyFuncs.py > ### > > -- http://mail.python.org/mailman/listinfo/python-list