ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
*/
thanks a lot
basically everything in python is a reference (except for simple types like ints etc), so, yes you can do something like "pointer to a function". Eg try the following:
Code:
def x(f, a):
f(a)
def y(s):
print s
x( y, "Hello" )
Evo2.
PS. Please use [code] tags... it makes reading code, so much less painful.
sorry I don't see what the problem is. If you really can't work it out from the example I gave, please repost your c code in the simplest form, properly indeted in [code] tags, and I'll write the equivalent python. When I say simplest, I mean it just has to demonstrate the functionality you are looking for, not actually do anything useful.
Ok, I'm on my lunch break so I decided to do it...
Code:
import math
def F1(x):
return x*x
def Trapeze( f, left, right, step ):
x1=x0=y0=y1=z=0.0
for i in range( int((right-left)/step) ):
x0=left+i*step
x1=x0+step
y1=f(x1)
y0=f(x0)
z += (y1+y0)*step*0.5
return z
if __name__ == '__main__':
print 'Value for sin is %f' % Trapeze( math.sin, -2.5, 3.2, 0.1 )
print 'Value for F1 is %f' % Trapeze( F1, 0, 3, 0.1)
It runs and outputs the following:
Code:
Value for sin is 0.196987
Value for F1 is 9.005000
Note that I would not normally write code like this: I wrote it this way to make it as close as possible to the sample c code.
In high-level languages like Python, "a reference" is a very similar thing to "a pointer" ... except that it is smart.
The language system knows what sort of thing any reference is referring to.
The thing being referred to has a "reference count," which keeps it from being destroyed when something refers to it.
There is a "garbage collector" that will dispose of objects that are no longer being referenced.
This is, quite frankly, one of the most-important reasons why these language systems are routinely used. C/C++ is used to implement them, but with only a few lines of high-level language code you can (very efficiently) tap into "all sorts of language goodness" (tens of thousands of lines' worth of scrupulously optimized, magical voodoo C/C++ code) that makes your life considerably easier. The overhead of the system is predictable, and relatively inconsequential.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.