ccode user_functions arg does not work for sqrt, fabs, pow?

34 views
Skip to first unread message

John Peterson

unread,
Feb 26, 2015, 11:56:01 AM2/26/15
to sy...@googlegroups.com
Hi,

It's entirely possible I'm using ccode wrong, but I can't seem to get it to recognize my custom replacements for sqrt, fabs, or pow.  Here's a simple test case:

#!/usr/bin/env python
from sympy import *
 
print ccode(sympify('sqrt(x)'), user_functions={"sqrt": "std::sqrt"})
print ccode(sympify('Abs(x)'), user_functions={"fabs": "std::abs"})
print ccode(sympify('x**2'), user_functions={"pow": "std::pow"})

On my system, this prints:

sqrt(x)
fabs(x)
pow(x, 2)


I can of course work around this pretty easily with text replacement, so it's not a big deal...

Jason Moore

unread,
Feb 26, 2015, 12:53:30 PM2/26/15
to sy...@googlegroups.com
John,

The key's to the user_functions dictionary should be strings that map to SymPy functions in the expressions:

In [18]: print sympy.ccode(sympy.sin(sympy.symbols('x')), user_functions={"sin": "std::sin"})
std::sin(x)

In [19]: print sympy.ccode(sympy.Abs(sympy.symbols('x')), user_functions={"Abs": "std::abs"})
std::abs(x)

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/32b5380b-83e8-403c-a512-2d0417fbdf3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Moore

unread,
Feb 26, 2015, 12:54:27 PM2/26/15
to sy...@googlegroups.com
Also this one isn't obvious:

In [21]: print sympy.ccode(sympy.symbols('x')**2, user_functions={"Pow": "std::pow"})
std::pow(x, 2)

John Peterson

unread,
Feb 26, 2015, 1:04:49 PM2/26/15
to sy...@googlegroups.com


On Thursday, February 26, 2015 at 10:54:27 AM UTC-7, Jason Moore wrote:
Also this one isn't obvious:

In [21]: print sympy.ccode(sympy.symbols('x')**2, user_functions={"Pow": "std::pow"})
std::pow(x, 2)

Interestingly, this line of code still prints pow(x,2) for me.  May be a different in Sympy version?  I am using 0.7.5.

Jason Moore

unread,
Feb 26, 2015, 1:15:43 PM2/26/15
to sy...@googlegroups.com
I'm on 0.7.6, so maybe a bug was fixed.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Jason Moore

unread,
Feb 26, 2015, 1:17:14 PM2/26/15
to sy...@googlegroups.com
Another way to handle this is to sublcass the CCodePrinter and then define methods for the specific function names. You will have more control over the details if you do it that way.

Jason Moore

unread,
Feb 26, 2015, 6:07:11 PM2/26/15
to sy...@googlegroups.com
I tried to clarify the docstring a bit here: https://github.com/sympy/sympy/pull/9062

Let me know if that is more helpful.
Reply all
Reply to author
Forward
0 new messages