Convert string to mathematical function

J

jeremito

I am extending python with C++ and need some help. I would like to
convert a string to a mathematical function and then make this a C++
function. My C++ code would then refer to this function to calculate
what it needs. For example I want to tell my function to calculate
"x^2 + 3x +2", but later on I may want: "x + 3". Does anybody know how
I can get an arbitrary string in Python (but proper mathematical
function) turned into a C++ function? My one idea (although I don't
know how to implement it, I'm still new at this) is to pass to C++ a
pointer to a (Python) function. Will this work?
Thanks,
Jeremy
 
F

Felipe Almeida Lessa

Em Ter, 2006-08-01 às 18:45 -0700, jeremito escreveu:
I am extending python with C++ and need some help. I would like to
convert a string to a mathematical function and then make this a C++
function.

I may be wrong, but I don't think you can create new C++ functions
on-the-fly. At least I had the impression that only VMs could do it
(e.g. System.Reflection on .NET/Mono world and, of course, Python).
My one idea (although I don't
know how to implement it, I'm still new at this) is to pass to C++ a
pointer to a (Python) function. Will this work?

I think it will, but I can't tell you how =).
 
J

John Machin

jeremito said:
I am extending python with C++ and need some help. I would like to
convert a string to a mathematical function and then make this a C++
function. My C++ code would then refer to this function to calculate
what it needs. For example I want to tell my function to calculate
"x^2 + 3x +2", but later on I may want: "x + 3". Does anybody know how
I can get an arbitrary string in Python (but proper mathematical
function) turned into a C++ function? My one idea (although I don't
know how to implement it, I'm still new at this) is to pass to C++ a
pointer to a (Python) function. Will this work?

It won't "work" in the sense "you can treat that pointer exactly like a
pointer to a C++ function". You would need to embed Python in your C++
code -- read the "embedding" section of the "extending and embedding"
manual.

So far all we know is that you want Python to call C++ to call Python
back again -- the run-time overhead and the sheer pain of getting all
that working correctly are IMHO not worth it. What is the *total*
functionality of your C++ extension? Why C++ and not C? Why C++ and not
Pyrex?

Consider doing the "extension" as a pure Python module -- Python's
built-in eval() function will certainly do the
parse-and-execute-a-formula trick for you. Even if you have extensive
compute-bound requirements, I'd suggest prototyping in Python first.

If the formula is to be executed many times and speed is a concern,
then consider using the compile() built-in to get a code object, and
see whether psyco wiil speed up its execution. I've never tried that
but it should take you much less than an hour of fiddling about to test
the idea yourself.

Hope some of this helps,
John
 
N

Nick Vatamaniuc

Jeremy,

Which method of extension are you using? For example you can pass the
function to C++/C directly using weave, without the need to convert to
Python first. Python has a different syntax than C++. Also I assume you
want exponentiation in you example "x^2..." and not 'xor', well, in
Python that would be "x**2" and in C you would have to include math.h
and then do pow(x,2). In other words there is no easy way to map Python
expressions to C++ (if it would be, Python could just all be mapped to
C++ and run at C++ speed!).

Below is my attempt at using weave (I prefer pyrex myself...). Note:
this won't work too fast if your function changes on-the-fly because
each new version will need to be compiled (gcc will be called and so
on...). So it would only pay off if your function gets compiled ones
and then takes a __very__ long time to compute.

Anyway here is my example:
--------------------------------------------------------------------------------------------.... return_val=pow(a,N);
.... """
a=42
N=42
ans=weave.inline(c_code, ['a','N'], support_code=includes)
file changed
None
cc1plus: warning: command line option "-Wstrict-prototypes" is valid
for Ada/C/ObjC but not for C++
ans 1.5013093754529656e+68
a**N 150130937545296572356771972164254457814047970568738777235893533016064L
#now call the function again. no compilation this time, the result was cached!
ans=weave.inline(c_code, ['a','N'], support_code=includes)
ans
1.5013093754529656e+68
 
C

Colin J. Williams

Nick said:
Jeremy,

Which method of extension are you using? For example you can pass the
function to C++/C directly using weave, without the need to convert to
Python first. Python has a different syntax than C++. Also I assume you
want exponentiation in you example "x^2..." and not 'xor', well, in
Python that would be "x**2" and in C you would have to include math.h
and then do pow(x,2). In other words there is no easy way to map Python
expressions to C++ (if it would be, Python could just all be mapped to
C++ and run at C++ speed!).

Below is my attempt at using weave (I prefer pyrex myself...). Note:
this won't work too fast if your function changes on-the-fly because
each new version will need to be compiled (gcc will be called and so
on...). So it would only pay off if your function gets compiled ones
and then takes a __very__ long time to compute.

Anyway here is my example:
--------------------------------------------------------------------------------------------.... return_val=pow(a,N);
.... """
a=42
N=42
ans=weave.inline(c_code, ['a','N'], support_code=includes)
file changed
None
cc1plus: warning: command line option "-Wstrict-prototypes" is valid
for Ada/C/ObjC but not for C++
ans 1.5013093754529656e+68
a**N 150130937545296572356771972164254457814047970568738777235893533016064L
#now call the function again. no compilation this time, the result was cached!
ans=weave.inline(c_code, ['a','N'], support_code=includes)
ans
1.5013093754529656e+68
----------------------------------------------------------------------------------
Look up weave for Python (it is a part of scipy) for more examples...

Hope this helps,
Nick Vatamaniuc

I am extending python with C++ and need some help. I would like to
convert a string to a mathematical function and then make this a C++
function. My C++ code would then refer to this function to calculate
what it needs. For example I want to tell my function to calculate
"x^2 + 3x +2", but later on I may want: "x + 3". Does anybody know how
I can get an arbitrary string in Python (but proper mathematical
function) turned into a C++ function? My one idea (although I don't
know how to implement it, I'm still new at this) is to pass to C++ a
pointer to a (Python) function. Will this work?
Thanks,
Jeremy
Jeremy,

I presume that you have considered and rejected use of Python's eval
function.

To this nonC++ user, it seems the simpler way.

A someone else pointed out, you would need to import the math module.

Colin W.
 
J

jeremito

I was unaware of the exec and eval functions in Python. Without trying
them, they seem to be what I want to do. I'll play around with it and
see if I can figure it out. Thanks for the suggestions everyone.
Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top